xref: /linux/net/core/dev.c (revision 6ffdbb93a59c60ee18bebd9acdbbca91e6bf0e64)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *      NET3    Protocol independent device support routines.
4  *
5  *	Derived from the non IP parts of dev.c 1.0.19
6  *              Authors:	Ross Biro
7  *				Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
8  *				Mark Evans, <evansmp@uhura.aston.ac.uk>
9  *
10  *	Additional Authors:
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>
17  *
18  *	Changes:
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
30  *					drivers
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
40  *					call a packet.
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
46  *					changes.
47  *		Rudi Cilibrasi	:	Pass the right thing to
48  *					set_mac_address()
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
54  *					1 device.
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
62  *					the backlog queue.
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
69  */
70 
71 #include <linux/uaccess.h>
72 #include <linux/bitmap.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/isolation.h>
81 #include <linux/sched/mm.h>
82 #include <linux/smpboot.h>
83 #include <linux/mutex.h>
84 #include <linux/rwsem.h>
85 #include <linux/string.h>
86 #include <linux/mm.h>
87 #include <linux/socket.h>
88 #include <linux/sockios.h>
89 #include <linux/errno.h>
90 #include <linux/interrupt.h>
91 #include <linux/if_ether.h>
92 #include <linux/netdevice.h>
93 #include <linux/etherdevice.h>
94 #include <linux/ethtool.h>
95 #include <linux/skbuff.h>
96 #include <linux/kthread.h>
97 #include <linux/bpf.h>
98 #include <linux/bpf_trace.h>
99 #include <net/net_namespace.h>
100 #include <net/sock.h>
101 #include <net/busy_poll.h>
102 #include <linux/rtnetlink.h>
103 #include <linux/stat.h>
104 #include <net/dsa.h>
105 #include <net/dst.h>
106 #include <net/dst_metadata.h>
107 #include <net/gro.h>
108 #include <net/pkt_sched.h>
109 #include <net/pkt_cls.h>
110 #include <net/checksum.h>
111 #include <net/xfrm.h>
112 #include <net/tcx.h>
113 #include <linux/highmem.h>
114 #include <linux/init.h>
115 #include <linux/module.h>
116 #include <linux/netpoll.h>
117 #include <linux/rcupdate.h>
118 #include <linux/delay.h>
119 #include <net/iw_handler.h>
120 #include <asm/current.h>
121 #include <linux/audit.h>
122 #include <linux/dmaengine.h>
123 #include <linux/err.h>
124 #include <linux/ctype.h>
125 #include <linux/if_arp.h>
126 #include <linux/if_vlan.h>
127 #include <linux/ip.h>
128 #include <net/ip.h>
129 #include <net/mpls.h>
130 #include <linux/ipv6.h>
131 #include <linux/in.h>
132 #include <linux/jhash.h>
133 #include <linux/random.h>
134 #include <trace/events/napi.h>
135 #include <trace/events/net.h>
136 #include <trace/events/skb.h>
137 #include <trace/events/qdisc.h>
138 #include <trace/events/xdp.h>
139 #include <linux/inetdevice.h>
140 #include <linux/cpu_rmap.h>
141 #include <linux/static_key.h>
142 #include <linux/hashtable.h>
143 #include <linux/vmalloc.h>
144 #include <linux/if_macvlan.h>
145 #include <linux/errqueue.h>
146 #include <linux/hrtimer.h>
147 #include <linux/netfilter_netdev.h>
148 #include <linux/crash_dump.h>
149 #include <linux/sctp.h>
150 #include <net/udp_tunnel.h>
151 #include <linux/net_namespace.h>
152 #include <linux/indirect_call_wrapper.h>
153 #include <net/devlink.h>
154 #include <linux/pm_runtime.h>
155 #include <linux/prandom.h>
156 #include <linux/once_lite.h>
157 #include <net/netdev_rx_queue.h>
158 #include <net/page_pool/types.h>
159 #include <net/page_pool/helpers.h>
160 #include <net/rps.h>
161 #include <linux/phy_link_topology.h>
162 
163 #include "dev.h"
164 #include "devmem.h"
165 #include "net-sysfs.h"
166 
167 static DEFINE_SPINLOCK(ptype_lock);
168 struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
169 
170 static int netif_rx_internal(struct sk_buff *skb);
171 static int call_netdevice_notifiers_extack(unsigned long val,
172 					   struct net_device *dev,
173 					   struct netlink_ext_ack *extack);
174 
175 static DEFINE_MUTEX(ifalias_mutex);
176 
177 /* protects napi_hash addition/deletion and napi_gen_id */
178 static DEFINE_SPINLOCK(napi_hash_lock);
179 
180 static unsigned int napi_gen_id = NR_CPUS;
181 static DEFINE_READ_MOSTLY_HASHTABLE(napi_hash, 8);
182 
183 static DECLARE_RWSEM(devnet_rename_sem);
184 
185 static inline void dev_base_seq_inc(struct net *net)
186 {
187 	unsigned int val = net->dev_base_seq + 1;
188 
189 	WRITE_ONCE(net->dev_base_seq, val ?: 1);
190 }
191 
192 static inline struct hlist_head *dev_name_hash(struct net *net, const char *name)
193 {
194 	unsigned int hash = full_name_hash(net, name, strnlen(name, IFNAMSIZ));
195 
196 	return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)];
197 }
198 
199 static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
200 {
201 	return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)];
202 }
203 
204 #ifndef CONFIG_PREEMPT_RT
205 
206 static DEFINE_STATIC_KEY_FALSE(use_backlog_threads_key);
207 
208 static int __init setup_backlog_napi_threads(char *arg)
209 {
210 	static_branch_enable(&use_backlog_threads_key);
211 	return 0;
212 }
213 early_param("thread_backlog_napi", setup_backlog_napi_threads);
214 
215 static bool use_backlog_threads(void)
216 {
217 	return static_branch_unlikely(&use_backlog_threads_key);
218 }
219 
220 #else
221 
222 static bool use_backlog_threads(void)
223 {
224 	return true;
225 }
226 
227 #endif
228 
229 static inline void backlog_lock_irq_save(struct softnet_data *sd,
230 					 unsigned long *flags)
231 {
232 	if (IS_ENABLED(CONFIG_RPS) || use_backlog_threads())
233 		spin_lock_irqsave(&sd->input_pkt_queue.lock, *flags);
234 	else
235 		local_irq_save(*flags);
236 }
237 
238 static inline void backlog_lock_irq_disable(struct softnet_data *sd)
239 {
240 	if (IS_ENABLED(CONFIG_RPS) || use_backlog_threads())
241 		spin_lock_irq(&sd->input_pkt_queue.lock);
242 	else
243 		local_irq_disable();
244 }
245 
246 static inline void backlog_unlock_irq_restore(struct softnet_data *sd,
247 					      unsigned long *flags)
248 {
249 	if (IS_ENABLED(CONFIG_RPS) || use_backlog_threads())
250 		spin_unlock_irqrestore(&sd->input_pkt_queue.lock, *flags);
251 	else
252 		local_irq_restore(*flags);
253 }
254 
255 static inline void backlog_unlock_irq_enable(struct softnet_data *sd)
256 {
257 	if (IS_ENABLED(CONFIG_RPS) || use_backlog_threads())
258 		spin_unlock_irq(&sd->input_pkt_queue.lock);
259 	else
260 		local_irq_enable();
261 }
262 
263 static struct netdev_name_node *netdev_name_node_alloc(struct net_device *dev,
264 						       const char *name)
265 {
266 	struct netdev_name_node *name_node;
267 
268 	name_node = kmalloc(sizeof(*name_node), GFP_KERNEL);
269 	if (!name_node)
270 		return NULL;
271 	INIT_HLIST_NODE(&name_node->hlist);
272 	name_node->dev = dev;
273 	name_node->name = name;
274 	return name_node;
275 }
276 
277 static struct netdev_name_node *
278 netdev_name_node_head_alloc(struct net_device *dev)
279 {
280 	struct netdev_name_node *name_node;
281 
282 	name_node = netdev_name_node_alloc(dev, dev->name);
283 	if (!name_node)
284 		return NULL;
285 	INIT_LIST_HEAD(&name_node->list);
286 	return name_node;
287 }
288 
289 static void netdev_name_node_free(struct netdev_name_node *name_node)
290 {
291 	kfree(name_node);
292 }
293 
294 static void netdev_name_node_add(struct net *net,
295 				 struct netdev_name_node *name_node)
296 {
297 	hlist_add_head_rcu(&name_node->hlist,
298 			   dev_name_hash(net, name_node->name));
299 }
300 
301 static void netdev_name_node_del(struct netdev_name_node *name_node)
302 {
303 	hlist_del_rcu(&name_node->hlist);
304 }
305 
306 static struct netdev_name_node *netdev_name_node_lookup(struct net *net,
307 							const char *name)
308 {
309 	struct hlist_head *head = dev_name_hash(net, name);
310 	struct netdev_name_node *name_node;
311 
312 	hlist_for_each_entry(name_node, head, hlist)
313 		if (!strcmp(name_node->name, name))
314 			return name_node;
315 	return NULL;
316 }
317 
318 static struct netdev_name_node *netdev_name_node_lookup_rcu(struct net *net,
319 							    const char *name)
320 {
321 	struct hlist_head *head = dev_name_hash(net, name);
322 	struct netdev_name_node *name_node;
323 
324 	hlist_for_each_entry_rcu(name_node, head, hlist)
325 		if (!strcmp(name_node->name, name))
326 			return name_node;
327 	return NULL;
328 }
329 
330 bool netdev_name_in_use(struct net *net, const char *name)
331 {
332 	return netdev_name_node_lookup(net, name);
333 }
334 EXPORT_SYMBOL(netdev_name_in_use);
335 
336 int netdev_name_node_alt_create(struct net_device *dev, const char *name)
337 {
338 	struct netdev_name_node *name_node;
339 	struct net *net = dev_net(dev);
340 
341 	name_node = netdev_name_node_lookup(net, name);
342 	if (name_node)
343 		return -EEXIST;
344 	name_node = netdev_name_node_alloc(dev, name);
345 	if (!name_node)
346 		return -ENOMEM;
347 	netdev_name_node_add(net, name_node);
348 	/* The node that holds dev->name acts as a head of per-device list. */
349 	list_add_tail_rcu(&name_node->list, &dev->name_node->list);
350 
351 	return 0;
352 }
353 
354 static void netdev_name_node_alt_free(struct rcu_head *head)
355 {
356 	struct netdev_name_node *name_node =
357 		container_of(head, struct netdev_name_node, rcu);
358 
359 	kfree(name_node->name);
360 	netdev_name_node_free(name_node);
361 }
362 
363 static void __netdev_name_node_alt_destroy(struct netdev_name_node *name_node)
364 {
365 	netdev_name_node_del(name_node);
366 	list_del(&name_node->list);
367 	call_rcu(&name_node->rcu, netdev_name_node_alt_free);
368 }
369 
370 int netdev_name_node_alt_destroy(struct net_device *dev, const char *name)
371 {
372 	struct netdev_name_node *name_node;
373 	struct net *net = dev_net(dev);
374 
375 	name_node = netdev_name_node_lookup(net, name);
376 	if (!name_node)
377 		return -ENOENT;
378 	/* lookup might have found our primary name or a name belonging
379 	 * to another device.
380 	 */
381 	if (name_node == dev->name_node || name_node->dev != dev)
382 		return -EINVAL;
383 
384 	__netdev_name_node_alt_destroy(name_node);
385 	return 0;
386 }
387 
388 static void netdev_name_node_alt_flush(struct net_device *dev)
389 {
390 	struct netdev_name_node *name_node, *tmp;
391 
392 	list_for_each_entry_safe(name_node, tmp, &dev->name_node->list, list) {
393 		list_del(&name_node->list);
394 		netdev_name_node_alt_free(&name_node->rcu);
395 	}
396 }
397 
398 /* Device list insertion */
399 static void list_netdevice(struct net_device *dev)
400 {
401 	struct netdev_name_node *name_node;
402 	struct net *net = dev_net(dev);
403 
404 	ASSERT_RTNL();
405 
406 	list_add_tail_rcu(&dev->dev_list, &net->dev_base_head);
407 	netdev_name_node_add(net, dev->name_node);
408 	hlist_add_head_rcu(&dev->index_hlist,
409 			   dev_index_hash(net, dev->ifindex));
410 
411 	netdev_for_each_altname(dev, name_node)
412 		netdev_name_node_add(net, name_node);
413 
414 	/* We reserved the ifindex, this can't fail */
415 	WARN_ON(xa_store(&net->dev_by_index, dev->ifindex, dev, GFP_KERNEL));
416 
417 	dev_base_seq_inc(net);
418 }
419 
420 /* Device list removal
421  * caller must respect a RCU grace period before freeing/reusing dev
422  */
423 static void unlist_netdevice(struct net_device *dev)
424 {
425 	struct netdev_name_node *name_node;
426 	struct net *net = dev_net(dev);
427 
428 	ASSERT_RTNL();
429 
430 	xa_erase(&net->dev_by_index, dev->ifindex);
431 
432 	netdev_for_each_altname(dev, name_node)
433 		netdev_name_node_del(name_node);
434 
435 	/* Unlink dev from the device chain */
436 	list_del_rcu(&dev->dev_list);
437 	netdev_name_node_del(dev->name_node);
438 	hlist_del_rcu(&dev->index_hlist);
439 
440 	dev_base_seq_inc(dev_net(dev));
441 }
442 
443 /*
444  *	Our notifier list
445  */
446 
447 static RAW_NOTIFIER_HEAD(netdev_chain);
448 
449 /*
450  *	Device drivers call our routines to queue packets here. We empty the
451  *	queue in the local softnet handler.
452  */
453 
454 DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data) = {
455 	.process_queue_bh_lock = INIT_LOCAL_LOCK(process_queue_bh_lock),
456 };
457 EXPORT_PER_CPU_SYMBOL(softnet_data);
458 
459 /* Page_pool has a lockless array/stack to alloc/recycle pages.
460  * PP consumers must pay attention to run APIs in the appropriate context
461  * (e.g. NAPI context).
462  */
463 DEFINE_PER_CPU(struct page_pool *, system_page_pool);
464 
465 #ifdef CONFIG_LOCKDEP
466 /*
467  * register_netdevice() inits txq->_xmit_lock and sets lockdep class
468  * according to dev->type
469  */
470 static const unsigned short netdev_lock_type[] = {
471 	 ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25,
472 	 ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET,
473 	 ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM,
474 	 ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP,
475 	 ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD,
476 	 ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25,
477 	 ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP,
478 	 ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD,
479 	 ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI,
480 	 ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE,
481 	 ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET,
482 	 ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL,
483 	 ARPHRD_FCFABRIC, ARPHRD_IEEE80211, ARPHRD_IEEE80211_PRISM,
484 	 ARPHRD_IEEE80211_RADIOTAP, ARPHRD_PHONET, ARPHRD_PHONET_PIPE,
485 	 ARPHRD_IEEE802154, ARPHRD_VOID, ARPHRD_NONE};
486 
487 static const char *const netdev_lock_name[] = {
488 	"_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25",
489 	"_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET",
490 	"_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM",
491 	"_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP",
492 	"_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD",
493 	"_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25",
494 	"_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP",
495 	"_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD",
496 	"_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI",
497 	"_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE",
498 	"_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET",
499 	"_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL",
500 	"_xmit_FCFABRIC", "_xmit_IEEE80211", "_xmit_IEEE80211_PRISM",
501 	"_xmit_IEEE80211_RADIOTAP", "_xmit_PHONET", "_xmit_PHONET_PIPE",
502 	"_xmit_IEEE802154", "_xmit_VOID", "_xmit_NONE"};
503 
504 static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)];
505 static struct lock_class_key netdev_addr_lock_key[ARRAY_SIZE(netdev_lock_type)];
506 
507 static inline unsigned short netdev_lock_pos(unsigned short dev_type)
508 {
509 	int i;
510 
511 	for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
512 		if (netdev_lock_type[i] == dev_type)
513 			return i;
514 	/* the last key is used by default */
515 	return ARRAY_SIZE(netdev_lock_type) - 1;
516 }
517 
518 static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
519 						 unsigned short dev_type)
520 {
521 	int i;
522 
523 	i = netdev_lock_pos(dev_type);
524 	lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i],
525 				   netdev_lock_name[i]);
526 }
527 
528 static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
529 {
530 	int i;
531 
532 	i = netdev_lock_pos(dev->type);
533 	lockdep_set_class_and_name(&dev->addr_list_lock,
534 				   &netdev_addr_lock_key[i],
535 				   netdev_lock_name[i]);
536 }
537 #else
538 static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
539 						 unsigned short dev_type)
540 {
541 }
542 
543 static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
544 {
545 }
546 #endif
547 
548 /*******************************************************************************
549  *
550  *		Protocol management and registration routines
551  *
552  *******************************************************************************/
553 
554 
555 /*
556  *	Add a protocol ID to the list. Now that the input handler is
557  *	smarter we can dispense with all the messy stuff that used to be
558  *	here.
559  *
560  *	BEWARE!!! Protocol handlers, mangling input packets,
561  *	MUST BE last in hash buckets and checking protocol handlers
562  *	MUST start from promiscuous ptype_all chain in net_bh.
563  *	It is true now, do not change it.
564  *	Explanation follows: if protocol handler, mangling packet, will
565  *	be the first on list, it is not able to sense, that packet
566  *	is cloned and should be copied-on-write, so that it will
567  *	change it and subsequent readers will get broken packet.
568  *							--ANK (980803)
569  */
570 
571 static inline struct list_head *ptype_head(const struct packet_type *pt)
572 {
573 	if (pt->type == htons(ETH_P_ALL))
574 		return pt->dev ? &pt->dev->ptype_all : &net_hotdata.ptype_all;
575 	else
576 		return pt->dev ? &pt->dev->ptype_specific :
577 				 &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK];
578 }
579 
580 /**
581  *	dev_add_pack - add packet handler
582  *	@pt: packet type declaration
583  *
584  *	Add a protocol handler to the networking stack. The passed &packet_type
585  *	is linked into kernel lists and may not be freed until it has been
586  *	removed from the kernel lists.
587  *
588  *	This call does not sleep therefore it can not
589  *	guarantee all CPU's that are in middle of receiving packets
590  *	will see the new packet type (until the next received packet).
591  */
592 
593 void dev_add_pack(struct packet_type *pt)
594 {
595 	struct list_head *head = ptype_head(pt);
596 
597 	spin_lock(&ptype_lock);
598 	list_add_rcu(&pt->list, head);
599 	spin_unlock(&ptype_lock);
600 }
601 EXPORT_SYMBOL(dev_add_pack);
602 
603 /**
604  *	__dev_remove_pack	 - remove packet handler
605  *	@pt: packet type declaration
606  *
607  *	Remove a protocol handler that was previously added to the kernel
608  *	protocol handlers by dev_add_pack(). The passed &packet_type is removed
609  *	from the kernel lists and can be freed or reused once this function
610  *	returns.
611  *
612  *      The packet type might still be in use by receivers
613  *	and must not be freed until after all the CPU's have gone
614  *	through a quiescent state.
615  */
616 void __dev_remove_pack(struct packet_type *pt)
617 {
618 	struct list_head *head = ptype_head(pt);
619 	struct packet_type *pt1;
620 
621 	spin_lock(&ptype_lock);
622 
623 	list_for_each_entry(pt1, head, list) {
624 		if (pt == pt1) {
625 			list_del_rcu(&pt->list);
626 			goto out;
627 		}
628 	}
629 
630 	pr_warn("dev_remove_pack: %p not found\n", pt);
631 out:
632 	spin_unlock(&ptype_lock);
633 }
634 EXPORT_SYMBOL(__dev_remove_pack);
635 
636 /**
637  *	dev_remove_pack	 - remove packet handler
638  *	@pt: packet type declaration
639  *
640  *	Remove a protocol handler that was previously added to the kernel
641  *	protocol handlers by dev_add_pack(). The passed &packet_type is removed
642  *	from the kernel lists and can be freed or reused once this function
643  *	returns.
644  *
645  *	This call sleeps to guarantee that no CPU is looking at the packet
646  *	type after return.
647  */
648 void dev_remove_pack(struct packet_type *pt)
649 {
650 	__dev_remove_pack(pt);
651 
652 	synchronize_net();
653 }
654 EXPORT_SYMBOL(dev_remove_pack);
655 
656 
657 /*******************************************************************************
658  *
659  *			    Device Interface Subroutines
660  *
661  *******************************************************************************/
662 
663 /**
664  *	dev_get_iflink	- get 'iflink' value of a interface
665  *	@dev: targeted interface
666  *
667  *	Indicates the ifindex the interface is linked to.
668  *	Physical interfaces have the same 'ifindex' and 'iflink' values.
669  */
670 
671 int dev_get_iflink(const struct net_device *dev)
672 {
673 	if (dev->netdev_ops && dev->netdev_ops->ndo_get_iflink)
674 		return dev->netdev_ops->ndo_get_iflink(dev);
675 
676 	return READ_ONCE(dev->ifindex);
677 }
678 EXPORT_SYMBOL(dev_get_iflink);
679 
680 /**
681  *	dev_fill_metadata_dst - Retrieve tunnel egress information.
682  *	@dev: targeted interface
683  *	@skb: The packet.
684  *
685  *	For better visibility of tunnel traffic OVS needs to retrieve
686  *	egress tunnel information for a packet. Following API allows
687  *	user to get this info.
688  */
689 int dev_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
690 {
691 	struct ip_tunnel_info *info;
692 
693 	if (!dev->netdev_ops  || !dev->netdev_ops->ndo_fill_metadata_dst)
694 		return -EINVAL;
695 
696 	info = skb_tunnel_info_unclone(skb);
697 	if (!info)
698 		return -ENOMEM;
699 	if (unlikely(!(info->mode & IP_TUNNEL_INFO_TX)))
700 		return -EINVAL;
701 
702 	return dev->netdev_ops->ndo_fill_metadata_dst(dev, skb);
703 }
704 EXPORT_SYMBOL_GPL(dev_fill_metadata_dst);
705 
706 static struct net_device_path *dev_fwd_path(struct net_device_path_stack *stack)
707 {
708 	int k = stack->num_paths++;
709 
710 	if (WARN_ON_ONCE(k >= NET_DEVICE_PATH_STACK_MAX))
711 		return NULL;
712 
713 	return &stack->path[k];
714 }
715 
716 int dev_fill_forward_path(const struct net_device *dev, const u8 *daddr,
717 			  struct net_device_path_stack *stack)
718 {
719 	const struct net_device *last_dev;
720 	struct net_device_path_ctx ctx = {
721 		.dev	= dev,
722 	};
723 	struct net_device_path *path;
724 	int ret = 0;
725 
726 	memcpy(ctx.daddr, daddr, sizeof(ctx.daddr));
727 	stack->num_paths = 0;
728 	while (ctx.dev && ctx.dev->netdev_ops->ndo_fill_forward_path) {
729 		last_dev = ctx.dev;
730 		path = dev_fwd_path(stack);
731 		if (!path)
732 			return -1;
733 
734 		memset(path, 0, sizeof(struct net_device_path));
735 		ret = ctx.dev->netdev_ops->ndo_fill_forward_path(&ctx, path);
736 		if (ret < 0)
737 			return -1;
738 
739 		if (WARN_ON_ONCE(last_dev == ctx.dev))
740 			return -1;
741 	}
742 
743 	if (!ctx.dev)
744 		return ret;
745 
746 	path = dev_fwd_path(stack);
747 	if (!path)
748 		return -1;
749 	path->type = DEV_PATH_ETHERNET;
750 	path->dev = ctx.dev;
751 
752 	return ret;
753 }
754 EXPORT_SYMBOL_GPL(dev_fill_forward_path);
755 
756 /**
757  *	__dev_get_by_name	- find a device by its name
758  *	@net: the applicable net namespace
759  *	@name: name to find
760  *
761  *	Find an interface by name. Must be called under RTNL semaphore.
762  *	If the name is found a pointer to the device is returned.
763  *	If the name is not found then %NULL is returned. The
764  *	reference counters are not incremented so the caller must be
765  *	careful with locks.
766  */
767 
768 struct net_device *__dev_get_by_name(struct net *net, const char *name)
769 {
770 	struct netdev_name_node *node_name;
771 
772 	node_name = netdev_name_node_lookup(net, name);
773 	return node_name ? node_name->dev : NULL;
774 }
775 EXPORT_SYMBOL(__dev_get_by_name);
776 
777 /**
778  * dev_get_by_name_rcu	- find a device by its name
779  * @net: the applicable net namespace
780  * @name: name to find
781  *
782  * Find an interface by name.
783  * If the name is found a pointer to the device is returned.
784  * If the name is not found then %NULL is returned.
785  * The reference counters are not incremented so the caller must be
786  * careful with locks. The caller must hold RCU lock.
787  */
788 
789 struct net_device *dev_get_by_name_rcu(struct net *net, const char *name)
790 {
791 	struct netdev_name_node *node_name;
792 
793 	node_name = netdev_name_node_lookup_rcu(net, name);
794 	return node_name ? node_name->dev : NULL;
795 }
796 EXPORT_SYMBOL(dev_get_by_name_rcu);
797 
798 /* Deprecated for new users, call netdev_get_by_name() instead */
799 struct net_device *dev_get_by_name(struct net *net, const char *name)
800 {
801 	struct net_device *dev;
802 
803 	rcu_read_lock();
804 	dev = dev_get_by_name_rcu(net, name);
805 	dev_hold(dev);
806 	rcu_read_unlock();
807 	return dev;
808 }
809 EXPORT_SYMBOL(dev_get_by_name);
810 
811 /**
812  *	netdev_get_by_name() - find a device by its name
813  *	@net: the applicable net namespace
814  *	@name: name to find
815  *	@tracker: tracking object for the acquired reference
816  *	@gfp: allocation flags for the tracker
817  *
818  *	Find an interface by name. This can be called from any
819  *	context and does its own locking. The returned handle has
820  *	the usage count incremented and the caller must use netdev_put() to
821  *	release it when it is no longer needed. %NULL is returned if no
822  *	matching device is found.
823  */
824 struct net_device *netdev_get_by_name(struct net *net, const char *name,
825 				      netdevice_tracker *tracker, gfp_t gfp)
826 {
827 	struct net_device *dev;
828 
829 	dev = dev_get_by_name(net, name);
830 	if (dev)
831 		netdev_tracker_alloc(dev, tracker, gfp);
832 	return dev;
833 }
834 EXPORT_SYMBOL(netdev_get_by_name);
835 
836 /**
837  *	__dev_get_by_index - find a device by its ifindex
838  *	@net: the applicable net namespace
839  *	@ifindex: index of device
840  *
841  *	Search for an interface by index. Returns %NULL if the device
842  *	is not found or a pointer to the device. The device has not
843  *	had its reference counter increased so the caller must be careful
844  *	about locking. The caller must hold the RTNL semaphore.
845  */
846 
847 struct net_device *__dev_get_by_index(struct net *net, int ifindex)
848 {
849 	struct net_device *dev;
850 	struct hlist_head *head = dev_index_hash(net, ifindex);
851 
852 	hlist_for_each_entry(dev, head, index_hlist)
853 		if (dev->ifindex == ifindex)
854 			return dev;
855 
856 	return NULL;
857 }
858 EXPORT_SYMBOL(__dev_get_by_index);
859 
860 /**
861  *	dev_get_by_index_rcu - find a device by its ifindex
862  *	@net: the applicable net namespace
863  *	@ifindex: index of device
864  *
865  *	Search for an interface by index. Returns %NULL if the device
866  *	is not found or a pointer to the device. The device has not
867  *	had its reference counter increased so the caller must be careful
868  *	about locking. The caller must hold RCU lock.
869  */
870 
871 struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex)
872 {
873 	struct net_device *dev;
874 	struct hlist_head *head = dev_index_hash(net, ifindex);
875 
876 	hlist_for_each_entry_rcu(dev, head, index_hlist)
877 		if (dev->ifindex == ifindex)
878 			return dev;
879 
880 	return NULL;
881 }
882 EXPORT_SYMBOL(dev_get_by_index_rcu);
883 
884 /* Deprecated for new users, call netdev_get_by_index() instead */
885 struct net_device *dev_get_by_index(struct net *net, int ifindex)
886 {
887 	struct net_device *dev;
888 
889 	rcu_read_lock();
890 	dev = dev_get_by_index_rcu(net, ifindex);
891 	dev_hold(dev);
892 	rcu_read_unlock();
893 	return dev;
894 }
895 EXPORT_SYMBOL(dev_get_by_index);
896 
897 /**
898  *	netdev_get_by_index() - find a device by its ifindex
899  *	@net: the applicable net namespace
900  *	@ifindex: index of device
901  *	@tracker: tracking object for the acquired reference
902  *	@gfp: allocation flags for the tracker
903  *
904  *	Search for an interface by index. Returns NULL if the device
905  *	is not found or a pointer to the device. The device returned has
906  *	had a reference added and the pointer is safe until the user calls
907  *	netdev_put() to indicate they have finished with it.
908  */
909 struct net_device *netdev_get_by_index(struct net *net, int ifindex,
910 				       netdevice_tracker *tracker, gfp_t gfp)
911 {
912 	struct net_device *dev;
913 
914 	dev = dev_get_by_index(net, ifindex);
915 	if (dev)
916 		netdev_tracker_alloc(dev, tracker, gfp);
917 	return dev;
918 }
919 EXPORT_SYMBOL(netdev_get_by_index);
920 
921 /**
922  *	dev_get_by_napi_id - find a device by napi_id
923  *	@napi_id: ID of the NAPI struct
924  *
925  *	Search for an interface by NAPI ID. Returns %NULL if the device
926  *	is not found or a pointer to the device. The device has not had
927  *	its reference counter increased so the caller must be careful
928  *	about locking. The caller must hold RCU lock.
929  */
930 
931 struct net_device *dev_get_by_napi_id(unsigned int napi_id)
932 {
933 	struct napi_struct *napi;
934 
935 	WARN_ON_ONCE(!rcu_read_lock_held());
936 
937 	if (napi_id < MIN_NAPI_ID)
938 		return NULL;
939 
940 	napi = napi_by_id(napi_id);
941 
942 	return napi ? napi->dev : NULL;
943 }
944 EXPORT_SYMBOL(dev_get_by_napi_id);
945 
946 static DEFINE_SEQLOCK(netdev_rename_lock);
947 
948 void netdev_copy_name(struct net_device *dev, char *name)
949 {
950 	unsigned int seq;
951 
952 	do {
953 		seq = read_seqbegin(&netdev_rename_lock);
954 		strscpy(name, dev->name, IFNAMSIZ);
955 	} while (read_seqretry(&netdev_rename_lock, seq));
956 }
957 
958 /**
959  *	netdev_get_name - get a netdevice name, knowing its ifindex.
960  *	@net: network namespace
961  *	@name: a pointer to the buffer where the name will be stored.
962  *	@ifindex: the ifindex of the interface to get the name from.
963  */
964 int netdev_get_name(struct net *net, char *name, int ifindex)
965 {
966 	struct net_device *dev;
967 	int ret;
968 
969 	rcu_read_lock();
970 
971 	dev = dev_get_by_index_rcu(net, ifindex);
972 	if (!dev) {
973 		ret = -ENODEV;
974 		goto out;
975 	}
976 
977 	netdev_copy_name(dev, name);
978 
979 	ret = 0;
980 out:
981 	rcu_read_unlock();
982 	return ret;
983 }
984 
985 /**
986  *	dev_getbyhwaddr_rcu - find a device by its hardware address
987  *	@net: the applicable net namespace
988  *	@type: media type of device
989  *	@ha: hardware address
990  *
991  *	Search for an interface by MAC address. Returns NULL if the device
992  *	is not found or a pointer to the device.
993  *	The caller must hold RCU or RTNL.
994  *	The returned device has not had its ref count increased
995  *	and the caller must therefore be careful about locking
996  *
997  */
998 
999 struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type,
1000 				       const char *ha)
1001 {
1002 	struct net_device *dev;
1003 
1004 	for_each_netdev_rcu(net, dev)
1005 		if (dev->type == type &&
1006 		    !memcmp(dev->dev_addr, ha, dev->addr_len))
1007 			return dev;
1008 
1009 	return NULL;
1010 }
1011 EXPORT_SYMBOL(dev_getbyhwaddr_rcu);
1012 
1013 struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
1014 {
1015 	struct net_device *dev, *ret = NULL;
1016 
1017 	rcu_read_lock();
1018 	for_each_netdev_rcu(net, dev)
1019 		if (dev->type == type) {
1020 			dev_hold(dev);
1021 			ret = dev;
1022 			break;
1023 		}
1024 	rcu_read_unlock();
1025 	return ret;
1026 }
1027 EXPORT_SYMBOL(dev_getfirstbyhwtype);
1028 
1029 /**
1030  *	__dev_get_by_flags - find any device with given flags
1031  *	@net: the applicable net namespace
1032  *	@if_flags: IFF_* values
1033  *	@mask: bitmask of bits in if_flags to check
1034  *
1035  *	Search for any interface with the given flags. Returns NULL if a device
1036  *	is not found or a pointer to the device. Must be called inside
1037  *	rtnl_lock(), and result refcount is unchanged.
1038  */
1039 
1040 struct net_device *__dev_get_by_flags(struct net *net, unsigned short if_flags,
1041 				      unsigned short mask)
1042 {
1043 	struct net_device *dev, *ret;
1044 
1045 	ASSERT_RTNL();
1046 
1047 	ret = NULL;
1048 	for_each_netdev(net, dev) {
1049 		if (((dev->flags ^ if_flags) & mask) == 0) {
1050 			ret = dev;
1051 			break;
1052 		}
1053 	}
1054 	return ret;
1055 }
1056 EXPORT_SYMBOL(__dev_get_by_flags);
1057 
1058 /**
1059  *	dev_valid_name - check if name is okay for network device
1060  *	@name: name string
1061  *
1062  *	Network device names need to be valid file names to
1063  *	allow sysfs to work.  We also disallow any kind of
1064  *	whitespace.
1065  */
1066 bool dev_valid_name(const char *name)
1067 {
1068 	if (*name == '\0')
1069 		return false;
1070 	if (strnlen(name, IFNAMSIZ) == IFNAMSIZ)
1071 		return false;
1072 	if (!strcmp(name, ".") || !strcmp(name, ".."))
1073 		return false;
1074 
1075 	while (*name) {
1076 		if (*name == '/' || *name == ':' || isspace(*name))
1077 			return false;
1078 		name++;
1079 	}
1080 	return true;
1081 }
1082 EXPORT_SYMBOL(dev_valid_name);
1083 
1084 /**
1085  *	__dev_alloc_name - allocate a name for a device
1086  *	@net: network namespace to allocate the device name in
1087  *	@name: name format string
1088  *	@res: result name string
1089  *
1090  *	Passed a format string - eg "lt%d" it will try and find a suitable
1091  *	id. It scans list of devices to build up a free map, then chooses
1092  *	the first empty slot. The caller must hold the dev_base or rtnl lock
1093  *	while allocating the name and adding the device in order to avoid
1094  *	duplicates.
1095  *	Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1096  *	Returns the number of the unit assigned or a negative errno code.
1097  */
1098 
1099 static int __dev_alloc_name(struct net *net, const char *name, char *res)
1100 {
1101 	int i = 0;
1102 	const char *p;
1103 	const int max_netdevices = 8*PAGE_SIZE;
1104 	unsigned long *inuse;
1105 	struct net_device *d;
1106 	char buf[IFNAMSIZ];
1107 
1108 	/* Verify the string as this thing may have come from the user.
1109 	 * There must be one "%d" and no other "%" characters.
1110 	 */
1111 	p = strchr(name, '%');
1112 	if (!p || p[1] != 'd' || strchr(p + 2, '%'))
1113 		return -EINVAL;
1114 
1115 	/* Use one page as a bit array of possible slots */
1116 	inuse = bitmap_zalloc(max_netdevices, GFP_ATOMIC);
1117 	if (!inuse)
1118 		return -ENOMEM;
1119 
1120 	for_each_netdev(net, d) {
1121 		struct netdev_name_node *name_node;
1122 
1123 		netdev_for_each_altname(d, name_node) {
1124 			if (!sscanf(name_node->name, name, &i))
1125 				continue;
1126 			if (i < 0 || i >= max_netdevices)
1127 				continue;
1128 
1129 			/* avoid cases where sscanf is not exact inverse of printf */
1130 			snprintf(buf, IFNAMSIZ, name, i);
1131 			if (!strncmp(buf, name_node->name, IFNAMSIZ))
1132 				__set_bit(i, inuse);
1133 		}
1134 		if (!sscanf(d->name, name, &i))
1135 			continue;
1136 		if (i < 0 || i >= max_netdevices)
1137 			continue;
1138 
1139 		/* avoid cases where sscanf is not exact inverse of printf */
1140 		snprintf(buf, IFNAMSIZ, name, i);
1141 		if (!strncmp(buf, d->name, IFNAMSIZ))
1142 			__set_bit(i, inuse);
1143 	}
1144 
1145 	i = find_first_zero_bit(inuse, max_netdevices);
1146 	bitmap_free(inuse);
1147 	if (i == max_netdevices)
1148 		return -ENFILE;
1149 
1150 	/* 'res' and 'name' could overlap, use 'buf' as an intermediate buffer */
1151 	strscpy(buf, name, IFNAMSIZ);
1152 	snprintf(res, IFNAMSIZ, buf, i);
1153 	return i;
1154 }
1155 
1156 /* Returns negative errno or allocated unit id (see __dev_alloc_name()) */
1157 static int dev_prep_valid_name(struct net *net, struct net_device *dev,
1158 			       const char *want_name, char *out_name,
1159 			       int dup_errno)
1160 {
1161 	if (!dev_valid_name(want_name))
1162 		return -EINVAL;
1163 
1164 	if (strchr(want_name, '%'))
1165 		return __dev_alloc_name(net, want_name, out_name);
1166 
1167 	if (netdev_name_in_use(net, want_name))
1168 		return -dup_errno;
1169 	if (out_name != want_name)
1170 		strscpy(out_name, want_name, IFNAMSIZ);
1171 	return 0;
1172 }
1173 
1174 /**
1175  *	dev_alloc_name - allocate a name for a device
1176  *	@dev: device
1177  *	@name: name format string
1178  *
1179  *	Passed a format string - eg "lt%d" it will try and find a suitable
1180  *	id. It scans list of devices to build up a free map, then chooses
1181  *	the first empty slot. The caller must hold the dev_base or rtnl lock
1182  *	while allocating the name and adding the device in order to avoid
1183  *	duplicates.
1184  *	Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1185  *	Returns the number of the unit assigned or a negative errno code.
1186  */
1187 
1188 int dev_alloc_name(struct net_device *dev, const char *name)
1189 {
1190 	return dev_prep_valid_name(dev_net(dev), dev, name, dev->name, ENFILE);
1191 }
1192 EXPORT_SYMBOL(dev_alloc_name);
1193 
1194 static int dev_get_valid_name(struct net *net, struct net_device *dev,
1195 			      const char *name)
1196 {
1197 	int ret;
1198 
1199 	ret = dev_prep_valid_name(net, dev, name, dev->name, EEXIST);
1200 	return ret < 0 ? ret : 0;
1201 }
1202 
1203 /**
1204  *	dev_change_name - change name of a device
1205  *	@dev: device
1206  *	@newname: name (or format string) must be at least IFNAMSIZ
1207  *
1208  *	Change name of a device, can pass format strings "eth%d".
1209  *	for wildcarding.
1210  */
1211 int dev_change_name(struct net_device *dev, const char *newname)
1212 {
1213 	unsigned char old_assign_type;
1214 	char oldname[IFNAMSIZ];
1215 	int err = 0;
1216 	int ret;
1217 	struct net *net;
1218 
1219 	ASSERT_RTNL();
1220 	BUG_ON(!dev_net(dev));
1221 
1222 	net = dev_net(dev);
1223 
1224 	down_write(&devnet_rename_sem);
1225 
1226 	if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
1227 		up_write(&devnet_rename_sem);
1228 		return 0;
1229 	}
1230 
1231 	memcpy(oldname, dev->name, IFNAMSIZ);
1232 
1233 	write_seqlock_bh(&netdev_rename_lock);
1234 	err = dev_get_valid_name(net, dev, newname);
1235 	write_sequnlock_bh(&netdev_rename_lock);
1236 
1237 	if (err < 0) {
1238 		up_write(&devnet_rename_sem);
1239 		return err;
1240 	}
1241 
1242 	if (oldname[0] && !strchr(oldname, '%'))
1243 		netdev_info(dev, "renamed from %s%s\n", oldname,
1244 			    dev->flags & IFF_UP ? " (while UP)" : "");
1245 
1246 	old_assign_type = dev->name_assign_type;
1247 	WRITE_ONCE(dev->name_assign_type, NET_NAME_RENAMED);
1248 
1249 rollback:
1250 	ret = device_rename(&dev->dev, dev->name);
1251 	if (ret) {
1252 		memcpy(dev->name, oldname, IFNAMSIZ);
1253 		WRITE_ONCE(dev->name_assign_type, old_assign_type);
1254 		up_write(&devnet_rename_sem);
1255 		return ret;
1256 	}
1257 
1258 	up_write(&devnet_rename_sem);
1259 
1260 	netdev_adjacent_rename_links(dev, oldname);
1261 
1262 	netdev_name_node_del(dev->name_node);
1263 
1264 	synchronize_net();
1265 
1266 	netdev_name_node_add(net, dev->name_node);
1267 
1268 	ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev);
1269 	ret = notifier_to_errno(ret);
1270 
1271 	if (ret) {
1272 		/* err >= 0 after dev_alloc_name() or stores the first errno */
1273 		if (err >= 0) {
1274 			err = ret;
1275 			down_write(&devnet_rename_sem);
1276 			write_seqlock_bh(&netdev_rename_lock);
1277 			memcpy(dev->name, oldname, IFNAMSIZ);
1278 			write_sequnlock_bh(&netdev_rename_lock);
1279 			memcpy(oldname, newname, IFNAMSIZ);
1280 			WRITE_ONCE(dev->name_assign_type, old_assign_type);
1281 			old_assign_type = NET_NAME_RENAMED;
1282 			goto rollback;
1283 		} else {
1284 			netdev_err(dev, "name change rollback failed: %d\n",
1285 				   ret);
1286 		}
1287 	}
1288 
1289 	return err;
1290 }
1291 
1292 /**
1293  *	dev_set_alias - change ifalias of a device
1294  *	@dev: device
1295  *	@alias: name up to IFALIASZ
1296  *	@len: limit of bytes to copy from info
1297  *
1298  *	Set ifalias for a device,
1299  */
1300 int dev_set_alias(struct net_device *dev, const char *alias, size_t len)
1301 {
1302 	struct dev_ifalias *new_alias = NULL;
1303 
1304 	if (len >= IFALIASZ)
1305 		return -EINVAL;
1306 
1307 	if (len) {
1308 		new_alias = kmalloc(sizeof(*new_alias) + len + 1, GFP_KERNEL);
1309 		if (!new_alias)
1310 			return -ENOMEM;
1311 
1312 		memcpy(new_alias->ifalias, alias, len);
1313 		new_alias->ifalias[len] = 0;
1314 	}
1315 
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);
1320 
1321 	if (new_alias)
1322 		kfree_rcu(new_alias, rcuhead);
1323 
1324 	return len;
1325 }
1326 EXPORT_SYMBOL(dev_set_alias);
1327 
1328 /**
1329  *	dev_get_alias - get ifalias of a device
1330  *	@dev: device
1331  *	@name: buffer to store name of ifalias
1332  *	@len: size of buffer
1333  *
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.
1336  */
1337 int dev_get_alias(const struct net_device *dev, char *name, size_t len)
1338 {
1339 	const struct dev_ifalias *alias;
1340 	int ret = 0;
1341 
1342 	rcu_read_lock();
1343 	alias = rcu_dereference(dev->ifalias);
1344 	if (alias)
1345 		ret = snprintf(name, len, "%s", alias->ifalias);
1346 	rcu_read_unlock();
1347 
1348 	return ret;
1349 }
1350 
1351 /**
1352  *	netdev_features_change - device changes features
1353  *	@dev: device to cause notification
1354  *
1355  *	Called to indicate a device has changed features.
1356  */
1357 void netdev_features_change(struct net_device *dev)
1358 {
1359 	call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev);
1360 }
1361 EXPORT_SYMBOL(netdev_features_change);
1362 
1363 /**
1364  *	netdev_state_change - device changes state
1365  *	@dev: device to cause notification
1366  *
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.
1370  */
1371 void netdev_state_change(struct net_device *dev)
1372 {
1373 	if (dev->flags & IFF_UP) {
1374 		struct netdev_notifier_change_info change_info = {
1375 			.info.dev = dev,
1376 		};
1377 
1378 		call_netdevice_notifiers_info(NETDEV_CHANGE,
1379 					      &change_info.info);
1380 		rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL, 0, NULL);
1381 	}
1382 }
1383 EXPORT_SYMBOL(netdev_state_change);
1384 
1385 /**
1386  * __netdev_notify_peers - notify network peers about existence of @dev,
1387  * to be called when rtnl lock is already held.
1388  * @dev: network device
1389  *
1390  * Generate traffic such that interested network peers are aware of
1391  * @dev, such as by generating a gratuitous ARP. This may be used when
1392  * a device wants to inform the rest of the network about some sort of
1393  * reconfiguration such as a failover event or virtual machine
1394  * migration.
1395  */
1396 void __netdev_notify_peers(struct net_device *dev)
1397 {
1398 	ASSERT_RTNL();
1399 	call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, dev);
1400 	call_netdevice_notifiers(NETDEV_RESEND_IGMP, dev);
1401 }
1402 EXPORT_SYMBOL(__netdev_notify_peers);
1403 
1404 /**
1405  * netdev_notify_peers - notify network peers about existence of @dev
1406  * @dev: network device
1407  *
1408  * Generate traffic such that interested network peers are aware of
1409  * @dev, such as by generating a gratuitous ARP. This may be used when
1410  * a device wants to inform the rest of the network about some sort of
1411  * reconfiguration such as a failover event or virtual machine
1412  * migration.
1413  */
1414 void netdev_notify_peers(struct net_device *dev)
1415 {
1416 	rtnl_lock();
1417 	__netdev_notify_peers(dev);
1418 	rtnl_unlock();
1419 }
1420 EXPORT_SYMBOL(netdev_notify_peers);
1421 
1422 static int napi_threaded_poll(void *data);
1423 
1424 static int napi_kthread_create(struct napi_struct *n)
1425 {
1426 	int err = 0;
1427 
1428 	/* Create and wake up the kthread once to put it in
1429 	 * TASK_INTERRUPTIBLE mode to avoid the blocked task
1430 	 * warning and work with loadavg.
1431 	 */
1432 	n->thread = kthread_run(napi_threaded_poll, n, "napi/%s-%d",
1433 				n->dev->name, n->napi_id);
1434 	if (IS_ERR(n->thread)) {
1435 		err = PTR_ERR(n->thread);
1436 		pr_err("kthread_run failed with err %d\n", err);
1437 		n->thread = NULL;
1438 	}
1439 
1440 	return err;
1441 }
1442 
1443 static int __dev_open(struct net_device *dev, struct netlink_ext_ack *extack)
1444 {
1445 	const struct net_device_ops *ops = dev->netdev_ops;
1446 	int ret;
1447 
1448 	ASSERT_RTNL();
1449 	dev_addr_check(dev);
1450 
1451 	if (!netif_device_present(dev)) {
1452 		/* may be detached because parent is runtime-suspended */
1453 		if (dev->dev.parent)
1454 			pm_runtime_resume(dev->dev.parent);
1455 		if (!netif_device_present(dev))
1456 			return -ENODEV;
1457 	}
1458 
1459 	/* Block netpoll from trying to do any rx path servicing.
1460 	 * If we don't do this there is a chance ndo_poll_controller
1461 	 * or ndo_poll may be running while we open the device
1462 	 */
1463 	netpoll_poll_disable(dev);
1464 
1465 	ret = call_netdevice_notifiers_extack(NETDEV_PRE_UP, dev, extack);
1466 	ret = notifier_to_errno(ret);
1467 	if (ret)
1468 		return ret;
1469 
1470 	set_bit(__LINK_STATE_START, &dev->state);
1471 
1472 	if (ops->ndo_validate_addr)
1473 		ret = ops->ndo_validate_addr(dev);
1474 
1475 	if (!ret && ops->ndo_open)
1476 		ret = ops->ndo_open(dev);
1477 
1478 	netpoll_poll_enable(dev);
1479 
1480 	if (ret)
1481 		clear_bit(__LINK_STATE_START, &dev->state);
1482 	else {
1483 		dev->flags |= IFF_UP;
1484 		dev_set_rx_mode(dev);
1485 		dev_activate(dev);
1486 		add_device_randomness(dev->dev_addr, dev->addr_len);
1487 	}
1488 
1489 	return ret;
1490 }
1491 
1492 /**
1493  *	dev_open	- prepare an interface for use.
1494  *	@dev: device to open
1495  *	@extack: netlink extended ack
1496  *
1497  *	Takes a device from down to up state. The device's private open
1498  *	function is invoked and then the multicast lists are loaded. Finally
1499  *	the device is moved into the up state and a %NETDEV_UP message is
1500  *	sent to the netdev notifier chain.
1501  *
1502  *	Calling this function on an active interface is a nop. On a failure
1503  *	a negative errno code is returned.
1504  */
1505 int dev_open(struct net_device *dev, struct netlink_ext_ack *extack)
1506 {
1507 	int ret;
1508 
1509 	if (dev->flags & IFF_UP)
1510 		return 0;
1511 
1512 	ret = __dev_open(dev, extack);
1513 	if (ret < 0)
1514 		return ret;
1515 
1516 	rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP | IFF_RUNNING, GFP_KERNEL, 0, NULL);
1517 	call_netdevice_notifiers(NETDEV_UP, dev);
1518 
1519 	return ret;
1520 }
1521 EXPORT_SYMBOL(dev_open);
1522 
1523 static void __dev_close_many(struct list_head *head)
1524 {
1525 	struct net_device *dev;
1526 
1527 	ASSERT_RTNL();
1528 	might_sleep();
1529 
1530 	list_for_each_entry(dev, head, close_list) {
1531 		/* Temporarily disable netpoll until the interface is down */
1532 		netpoll_poll_disable(dev);
1533 
1534 		call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
1535 
1536 		clear_bit(__LINK_STATE_START, &dev->state);
1537 
1538 		/* Synchronize to scheduled poll. We cannot touch poll list, it
1539 		 * can be even on different cpu. So just clear netif_running().
1540 		 *
1541 		 * dev->stop() will invoke napi_disable() on all of it's
1542 		 * napi_struct instances on this device.
1543 		 */
1544 		smp_mb__after_atomic(); /* Commit netif_running(). */
1545 	}
1546 
1547 	dev_deactivate_many(head);
1548 
1549 	list_for_each_entry(dev, head, close_list) {
1550 		const struct net_device_ops *ops = dev->netdev_ops;
1551 
1552 		/*
1553 		 *	Call the device specific close. This cannot fail.
1554 		 *	Only if device is UP
1555 		 *
1556 		 *	We allow it to be called even after a DETACH hot-plug
1557 		 *	event.
1558 		 */
1559 		if (ops->ndo_stop)
1560 			ops->ndo_stop(dev);
1561 
1562 		dev->flags &= ~IFF_UP;
1563 		netpoll_poll_enable(dev);
1564 	}
1565 }
1566 
1567 static void __dev_close(struct net_device *dev)
1568 {
1569 	LIST_HEAD(single);
1570 
1571 	list_add(&dev->close_list, &single);
1572 	__dev_close_many(&single);
1573 	list_del(&single);
1574 }
1575 
1576 void dev_close_many(struct list_head *head, bool unlink)
1577 {
1578 	struct net_device *dev, *tmp;
1579 
1580 	/* Remove the devices that don't need to be closed */
1581 	list_for_each_entry_safe(dev, tmp, head, close_list)
1582 		if (!(dev->flags & IFF_UP))
1583 			list_del_init(&dev->close_list);
1584 
1585 	__dev_close_many(head);
1586 
1587 	list_for_each_entry_safe(dev, tmp, head, close_list) {
1588 		rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP | IFF_RUNNING, GFP_KERNEL, 0, NULL);
1589 		call_netdevice_notifiers(NETDEV_DOWN, dev);
1590 		if (unlink)
1591 			list_del_init(&dev->close_list);
1592 	}
1593 }
1594 EXPORT_SYMBOL(dev_close_many);
1595 
1596 /**
1597  *	dev_close - shutdown an interface.
1598  *	@dev: device to shutdown
1599  *
1600  *	This function moves an active device into down state. A
1601  *	%NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
1602  *	is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
1603  *	chain.
1604  */
1605 void dev_close(struct net_device *dev)
1606 {
1607 	if (dev->flags & IFF_UP) {
1608 		LIST_HEAD(single);
1609 
1610 		list_add(&dev->close_list, &single);
1611 		dev_close_many(&single, true);
1612 		list_del(&single);
1613 	}
1614 }
1615 EXPORT_SYMBOL(dev_close);
1616 
1617 
1618 /**
1619  *	dev_disable_lro - disable Large Receive Offload on a device
1620  *	@dev: device
1621  *
1622  *	Disable Large Receive Offload (LRO) on a net device.  Must be
1623  *	called under RTNL.  This is needed if received packets may be
1624  *	forwarded to another interface.
1625  */
1626 void dev_disable_lro(struct net_device *dev)
1627 {
1628 	struct net_device *lower_dev;
1629 	struct list_head *iter;
1630 
1631 	dev->wanted_features &= ~NETIF_F_LRO;
1632 	netdev_update_features(dev);
1633 
1634 	if (unlikely(dev->features & NETIF_F_LRO))
1635 		netdev_WARN(dev, "failed to disable LRO!\n");
1636 
1637 	netdev_for_each_lower_dev(dev, lower_dev, iter)
1638 		dev_disable_lro(lower_dev);
1639 }
1640 EXPORT_SYMBOL(dev_disable_lro);
1641 
1642 /**
1643  *	dev_disable_gro_hw - disable HW Generic Receive Offload on a device
1644  *	@dev: device
1645  *
1646  *	Disable HW Generic Receive Offload (GRO_HW) on a net device.  Must be
1647  *	called under RTNL.  This is needed if Generic XDP is installed on
1648  *	the device.
1649  */
1650 static void dev_disable_gro_hw(struct net_device *dev)
1651 {
1652 	dev->wanted_features &= ~NETIF_F_GRO_HW;
1653 	netdev_update_features(dev);
1654 
1655 	if (unlikely(dev->features & NETIF_F_GRO_HW))
1656 		netdev_WARN(dev, "failed to disable GRO_HW!\n");
1657 }
1658 
1659 const char *netdev_cmd_to_name(enum netdev_cmd cmd)
1660 {
1661 #define N(val) 						\
1662 	case NETDEV_##val:				\
1663 		return "NETDEV_" __stringify(val);
1664 	switch (cmd) {
1665 	N(UP) N(DOWN) N(REBOOT) N(CHANGE) N(REGISTER) N(UNREGISTER)
1666 	N(CHANGEMTU) N(CHANGEADDR) N(GOING_DOWN) N(CHANGENAME) N(FEAT_CHANGE)
1667 	N(BONDING_FAILOVER) N(PRE_UP) N(PRE_TYPE_CHANGE) N(POST_TYPE_CHANGE)
1668 	N(POST_INIT) N(PRE_UNINIT) N(RELEASE) N(NOTIFY_PEERS) N(JOIN)
1669 	N(CHANGEUPPER) N(RESEND_IGMP) N(PRECHANGEMTU) N(CHANGEINFODATA)
1670 	N(BONDING_INFO) N(PRECHANGEUPPER) N(CHANGELOWERSTATE)
1671 	N(UDP_TUNNEL_PUSH_INFO) N(UDP_TUNNEL_DROP_INFO) N(CHANGE_TX_QUEUE_LEN)
1672 	N(CVLAN_FILTER_PUSH_INFO) N(CVLAN_FILTER_DROP_INFO)
1673 	N(SVLAN_FILTER_PUSH_INFO) N(SVLAN_FILTER_DROP_INFO)
1674 	N(PRE_CHANGEADDR) N(OFFLOAD_XSTATS_ENABLE) N(OFFLOAD_XSTATS_DISABLE)
1675 	N(OFFLOAD_XSTATS_REPORT_USED) N(OFFLOAD_XSTATS_REPORT_DELTA)
1676 	N(XDP_FEAT_CHANGE)
1677 	}
1678 #undef N
1679 	return "UNKNOWN_NETDEV_EVENT";
1680 }
1681 EXPORT_SYMBOL_GPL(netdev_cmd_to_name);
1682 
1683 static int call_netdevice_notifier(struct notifier_block *nb, unsigned long val,
1684 				   struct net_device *dev)
1685 {
1686 	struct netdev_notifier_info info = {
1687 		.dev = dev,
1688 	};
1689 
1690 	return nb->notifier_call(nb, val, &info);
1691 }
1692 
1693 static int call_netdevice_register_notifiers(struct notifier_block *nb,
1694 					     struct net_device *dev)
1695 {
1696 	int err;
1697 
1698 	err = call_netdevice_notifier(nb, NETDEV_REGISTER, dev);
1699 	err = notifier_to_errno(err);
1700 	if (err)
1701 		return err;
1702 
1703 	if (!(dev->flags & IFF_UP))
1704 		return 0;
1705 
1706 	call_netdevice_notifier(nb, NETDEV_UP, dev);
1707 	return 0;
1708 }
1709 
1710 static void call_netdevice_unregister_notifiers(struct notifier_block *nb,
1711 						struct net_device *dev)
1712 {
1713 	if (dev->flags & IFF_UP) {
1714 		call_netdevice_notifier(nb, NETDEV_GOING_DOWN,
1715 					dev);
1716 		call_netdevice_notifier(nb, NETDEV_DOWN, dev);
1717 	}
1718 	call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev);
1719 }
1720 
1721 static int call_netdevice_register_net_notifiers(struct notifier_block *nb,
1722 						 struct net *net)
1723 {
1724 	struct net_device *dev;
1725 	int err;
1726 
1727 	for_each_netdev(net, dev) {
1728 		err = call_netdevice_register_notifiers(nb, dev);
1729 		if (err)
1730 			goto rollback;
1731 	}
1732 	return 0;
1733 
1734 rollback:
1735 	for_each_netdev_continue_reverse(net, dev)
1736 		call_netdevice_unregister_notifiers(nb, dev);
1737 	return err;
1738 }
1739 
1740 static void call_netdevice_unregister_net_notifiers(struct notifier_block *nb,
1741 						    struct net *net)
1742 {
1743 	struct net_device *dev;
1744 
1745 	for_each_netdev(net, dev)
1746 		call_netdevice_unregister_notifiers(nb, dev);
1747 }
1748 
1749 static int dev_boot_phase = 1;
1750 
1751 /**
1752  * register_netdevice_notifier - register a network notifier block
1753  * @nb: notifier
1754  *
1755  * Register a notifier to be called when network device events occur.
1756  * The notifier passed is linked into the kernel structures and must
1757  * not be reused until it has been unregistered. A negative errno code
1758  * is returned on a failure.
1759  *
1760  * When registered all registration and up events are replayed
1761  * to the new notifier to allow device to have a race free
1762  * view of the network device list.
1763  */
1764 
1765 int register_netdevice_notifier(struct notifier_block *nb)
1766 {
1767 	struct net *net;
1768 	int err;
1769 
1770 	/* Close race with setup_net() and cleanup_net() */
1771 	down_write(&pernet_ops_rwsem);
1772 
1773 	/* When RTNL is removed, we need protection for netdev_chain. */
1774 	rtnl_lock();
1775 
1776 	err = raw_notifier_chain_register(&netdev_chain, nb);
1777 	if (err)
1778 		goto unlock;
1779 	if (dev_boot_phase)
1780 		goto unlock;
1781 	for_each_net(net) {
1782 		__rtnl_net_lock(net);
1783 		err = call_netdevice_register_net_notifiers(nb, net);
1784 		__rtnl_net_unlock(net);
1785 		if (err)
1786 			goto rollback;
1787 	}
1788 
1789 unlock:
1790 	rtnl_unlock();
1791 	up_write(&pernet_ops_rwsem);
1792 	return err;
1793 
1794 rollback:
1795 	for_each_net_continue_reverse(net) {
1796 		__rtnl_net_lock(net);
1797 		call_netdevice_unregister_net_notifiers(nb, net);
1798 		__rtnl_net_unlock(net);
1799 	}
1800 
1801 	raw_notifier_chain_unregister(&netdev_chain, nb);
1802 	goto unlock;
1803 }
1804 EXPORT_SYMBOL(register_netdevice_notifier);
1805 
1806 /**
1807  * unregister_netdevice_notifier - unregister a network notifier block
1808  * @nb: notifier
1809  *
1810  * Unregister a notifier previously registered by
1811  * register_netdevice_notifier(). The notifier is unlinked into the
1812  * kernel structures and may then be reused. A negative errno code
1813  * is returned on a failure.
1814  *
1815  * After unregistering unregister and down device events are synthesized
1816  * for all devices on the device list to the removed notifier to remove
1817  * the need for special case cleanup code.
1818  */
1819 
1820 int unregister_netdevice_notifier(struct notifier_block *nb)
1821 {
1822 	struct net *net;
1823 	int err;
1824 
1825 	/* Close race with setup_net() and cleanup_net() */
1826 	down_write(&pernet_ops_rwsem);
1827 	rtnl_lock();
1828 	err = raw_notifier_chain_unregister(&netdev_chain, nb);
1829 	if (err)
1830 		goto unlock;
1831 
1832 	for_each_net(net) {
1833 		__rtnl_net_lock(net);
1834 		call_netdevice_unregister_net_notifiers(nb, net);
1835 		__rtnl_net_unlock(net);
1836 	}
1837 
1838 unlock:
1839 	rtnl_unlock();
1840 	up_write(&pernet_ops_rwsem);
1841 	return err;
1842 }
1843 EXPORT_SYMBOL(unregister_netdevice_notifier);
1844 
1845 static int __register_netdevice_notifier_net(struct net *net,
1846 					     struct notifier_block *nb,
1847 					     bool ignore_call_fail)
1848 {
1849 	int err;
1850 
1851 	err = raw_notifier_chain_register(&net->netdev_chain, nb);
1852 	if (err)
1853 		return err;
1854 	if (dev_boot_phase)
1855 		return 0;
1856 
1857 	err = call_netdevice_register_net_notifiers(nb, net);
1858 	if (err && !ignore_call_fail)
1859 		goto chain_unregister;
1860 
1861 	return 0;
1862 
1863 chain_unregister:
1864 	raw_notifier_chain_unregister(&net->netdev_chain, nb);
1865 	return err;
1866 }
1867 
1868 static int __unregister_netdevice_notifier_net(struct net *net,
1869 					       struct notifier_block *nb)
1870 {
1871 	int err;
1872 
1873 	err = raw_notifier_chain_unregister(&net->netdev_chain, nb);
1874 	if (err)
1875 		return err;
1876 
1877 	call_netdevice_unregister_net_notifiers(nb, net);
1878 	return 0;
1879 }
1880 
1881 /**
1882  * register_netdevice_notifier_net - register a per-netns network notifier block
1883  * @net: network namespace
1884  * @nb: notifier
1885  *
1886  * Register a notifier to be called when network device events occur.
1887  * The notifier passed is linked into the kernel structures and must
1888  * not be reused until it has been unregistered. A negative errno code
1889  * is returned on a failure.
1890  *
1891  * When registered all registration and up events are replayed
1892  * to the new notifier to allow device to have a race free
1893  * view of the network device list.
1894  */
1895 
1896 int register_netdevice_notifier_net(struct net *net, struct notifier_block *nb)
1897 {
1898 	int err;
1899 
1900 	rtnl_net_lock(net);
1901 	err = __register_netdevice_notifier_net(net, nb, false);
1902 	rtnl_net_unlock(net);
1903 
1904 	return err;
1905 }
1906 EXPORT_SYMBOL(register_netdevice_notifier_net);
1907 
1908 /**
1909  * unregister_netdevice_notifier_net - unregister a per-netns
1910  *                                     network notifier block
1911  * @net: network namespace
1912  * @nb: notifier
1913  *
1914  * Unregister a notifier previously registered by
1915  * register_netdevice_notifier_net(). The notifier is unlinked from the
1916  * kernel structures and may then be reused. A negative errno code
1917  * is returned on a failure.
1918  *
1919  * After unregistering unregister and down device events are synthesized
1920  * for all devices on the device list to the removed notifier to remove
1921  * the need for special case cleanup code.
1922  */
1923 
1924 int unregister_netdevice_notifier_net(struct net *net,
1925 				      struct notifier_block *nb)
1926 {
1927 	int err;
1928 
1929 	rtnl_net_lock(net);
1930 	err = __unregister_netdevice_notifier_net(net, nb);
1931 	rtnl_net_unlock(net);
1932 
1933 	return err;
1934 }
1935 EXPORT_SYMBOL(unregister_netdevice_notifier_net);
1936 
1937 static void __move_netdevice_notifier_net(struct net *src_net,
1938 					  struct net *dst_net,
1939 					  struct notifier_block *nb)
1940 {
1941 	__unregister_netdevice_notifier_net(src_net, nb);
1942 	__register_netdevice_notifier_net(dst_net, nb, true);
1943 }
1944 
1945 int register_netdevice_notifier_dev_net(struct net_device *dev,
1946 					struct notifier_block *nb,
1947 					struct netdev_net_notifier *nn)
1948 {
1949 	struct net *net = dev_net(dev);
1950 	int err;
1951 
1952 	rtnl_net_lock(net);
1953 	err = __register_netdevice_notifier_net(net, nb, false);
1954 	if (!err) {
1955 		nn->nb = nb;
1956 		list_add(&nn->list, &dev->net_notifier_list);
1957 	}
1958 	rtnl_net_unlock(net);
1959 
1960 	return err;
1961 }
1962 EXPORT_SYMBOL(register_netdevice_notifier_dev_net);
1963 
1964 int unregister_netdevice_notifier_dev_net(struct net_device *dev,
1965 					  struct notifier_block *nb,
1966 					  struct netdev_net_notifier *nn)
1967 {
1968 	struct net *net = dev_net(dev);
1969 	int err;
1970 
1971 	rtnl_net_lock(net);
1972 	list_del(&nn->list);
1973 	err = __unregister_netdevice_notifier_net(net, nb);
1974 	rtnl_net_unlock(net);
1975 
1976 	return err;
1977 }
1978 EXPORT_SYMBOL(unregister_netdevice_notifier_dev_net);
1979 
1980 static void move_netdevice_notifiers_dev_net(struct net_device *dev,
1981 					     struct net *net)
1982 {
1983 	struct netdev_net_notifier *nn;
1984 
1985 	list_for_each_entry(nn, &dev->net_notifier_list, list)
1986 		__move_netdevice_notifier_net(dev_net(dev), net, nn->nb);
1987 }
1988 
1989 /**
1990  *	call_netdevice_notifiers_info - call all network notifier blocks
1991  *	@val: value passed unmodified to notifier function
1992  *	@info: notifier information data
1993  *
1994  *	Call all network notifier blocks.  Parameters and return value
1995  *	are as for raw_notifier_call_chain().
1996  */
1997 
1998 int call_netdevice_notifiers_info(unsigned long val,
1999 				  struct netdev_notifier_info *info)
2000 {
2001 	struct net *net = dev_net(info->dev);
2002 	int ret;
2003 
2004 	ASSERT_RTNL();
2005 
2006 	/* Run per-netns notifier block chain first, then run the global one.
2007 	 * Hopefully, one day, the global one is going to be removed after
2008 	 * all notifier block registrators get converted to be per-netns.
2009 	 */
2010 	ret = raw_notifier_call_chain(&net->netdev_chain, val, info);
2011 	if (ret & NOTIFY_STOP_MASK)
2012 		return ret;
2013 	return raw_notifier_call_chain(&netdev_chain, val, info);
2014 }
2015 
2016 /**
2017  *	call_netdevice_notifiers_info_robust - call per-netns notifier blocks
2018  *	                                       for and rollback on error
2019  *	@val_up: value passed unmodified to notifier function
2020  *	@val_down: value passed unmodified to the notifier function when
2021  *	           recovering from an error on @val_up
2022  *	@info: notifier information data
2023  *
2024  *	Call all per-netns network notifier blocks, but not notifier blocks on
2025  *	the global notifier chain. Parameters and return value are as for
2026  *	raw_notifier_call_chain_robust().
2027  */
2028 
2029 static int
2030 call_netdevice_notifiers_info_robust(unsigned long val_up,
2031 				     unsigned long val_down,
2032 				     struct netdev_notifier_info *info)
2033 {
2034 	struct net *net = dev_net(info->dev);
2035 
2036 	ASSERT_RTNL();
2037 
2038 	return raw_notifier_call_chain_robust(&net->netdev_chain,
2039 					      val_up, val_down, info);
2040 }
2041 
2042 static int call_netdevice_notifiers_extack(unsigned long val,
2043 					   struct net_device *dev,
2044 					   struct netlink_ext_ack *extack)
2045 {
2046 	struct netdev_notifier_info info = {
2047 		.dev = dev,
2048 		.extack = extack,
2049 	};
2050 
2051 	return call_netdevice_notifiers_info(val, &info);
2052 }
2053 
2054 /**
2055  *	call_netdevice_notifiers - call all network notifier blocks
2056  *      @val: value passed unmodified to notifier function
2057  *      @dev: net_device pointer passed unmodified to notifier function
2058  *
2059  *	Call all network notifier blocks.  Parameters and return value
2060  *	are as for raw_notifier_call_chain().
2061  */
2062 
2063 int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
2064 {
2065 	return call_netdevice_notifiers_extack(val, dev, NULL);
2066 }
2067 EXPORT_SYMBOL(call_netdevice_notifiers);
2068 
2069 /**
2070  *	call_netdevice_notifiers_mtu - call all network notifier blocks
2071  *	@val: value passed unmodified to notifier function
2072  *	@dev: net_device pointer passed unmodified to notifier function
2073  *	@arg: additional u32 argument passed to the notifier function
2074  *
2075  *	Call all network notifier blocks.  Parameters and return value
2076  *	are as for raw_notifier_call_chain().
2077  */
2078 static int call_netdevice_notifiers_mtu(unsigned long val,
2079 					struct net_device *dev, u32 arg)
2080 {
2081 	struct netdev_notifier_info_ext info = {
2082 		.info.dev = dev,
2083 		.ext.mtu = arg,
2084 	};
2085 
2086 	BUILD_BUG_ON(offsetof(struct netdev_notifier_info_ext, info) != 0);
2087 
2088 	return call_netdevice_notifiers_info(val, &info.info);
2089 }
2090 
2091 #ifdef CONFIG_NET_INGRESS
2092 static DEFINE_STATIC_KEY_FALSE(ingress_needed_key);
2093 
2094 void net_inc_ingress_queue(void)
2095 {
2096 	static_branch_inc(&ingress_needed_key);
2097 }
2098 EXPORT_SYMBOL_GPL(net_inc_ingress_queue);
2099 
2100 void net_dec_ingress_queue(void)
2101 {
2102 	static_branch_dec(&ingress_needed_key);
2103 }
2104 EXPORT_SYMBOL_GPL(net_dec_ingress_queue);
2105 #endif
2106 
2107 #ifdef CONFIG_NET_EGRESS
2108 static DEFINE_STATIC_KEY_FALSE(egress_needed_key);
2109 
2110 void net_inc_egress_queue(void)
2111 {
2112 	static_branch_inc(&egress_needed_key);
2113 }
2114 EXPORT_SYMBOL_GPL(net_inc_egress_queue);
2115 
2116 void net_dec_egress_queue(void)
2117 {
2118 	static_branch_dec(&egress_needed_key);
2119 }
2120 EXPORT_SYMBOL_GPL(net_dec_egress_queue);
2121 #endif
2122 
2123 #ifdef CONFIG_NET_CLS_ACT
2124 DEFINE_STATIC_KEY_FALSE(tcf_bypass_check_needed_key);
2125 EXPORT_SYMBOL(tcf_bypass_check_needed_key);
2126 #endif
2127 
2128 DEFINE_STATIC_KEY_FALSE(netstamp_needed_key);
2129 EXPORT_SYMBOL(netstamp_needed_key);
2130 #ifdef CONFIG_JUMP_LABEL
2131 static atomic_t netstamp_needed_deferred;
2132 static atomic_t netstamp_wanted;
2133 static void netstamp_clear(struct work_struct *work)
2134 {
2135 	int deferred = atomic_xchg(&netstamp_needed_deferred, 0);
2136 	int wanted;
2137 
2138 	wanted = atomic_add_return(deferred, &netstamp_wanted);
2139 	if (wanted > 0)
2140 		static_branch_enable(&netstamp_needed_key);
2141 	else
2142 		static_branch_disable(&netstamp_needed_key);
2143 }
2144 static DECLARE_WORK(netstamp_work, netstamp_clear);
2145 #endif
2146 
2147 void net_enable_timestamp(void)
2148 {
2149 #ifdef CONFIG_JUMP_LABEL
2150 	int wanted = atomic_read(&netstamp_wanted);
2151 
2152 	while (wanted > 0) {
2153 		if (atomic_try_cmpxchg(&netstamp_wanted, &wanted, wanted + 1))
2154 			return;
2155 	}
2156 	atomic_inc(&netstamp_needed_deferred);
2157 	schedule_work(&netstamp_work);
2158 #else
2159 	static_branch_inc(&netstamp_needed_key);
2160 #endif
2161 }
2162 EXPORT_SYMBOL(net_enable_timestamp);
2163 
2164 void net_disable_timestamp(void)
2165 {
2166 #ifdef CONFIG_JUMP_LABEL
2167 	int wanted = atomic_read(&netstamp_wanted);
2168 
2169 	while (wanted > 1) {
2170 		if (atomic_try_cmpxchg(&netstamp_wanted, &wanted, wanted - 1))
2171 			return;
2172 	}
2173 	atomic_dec(&netstamp_needed_deferred);
2174 	schedule_work(&netstamp_work);
2175 #else
2176 	static_branch_dec(&netstamp_needed_key);
2177 #endif
2178 }
2179 EXPORT_SYMBOL(net_disable_timestamp);
2180 
2181 static inline void net_timestamp_set(struct sk_buff *skb)
2182 {
2183 	skb->tstamp = 0;
2184 	skb->tstamp_type = SKB_CLOCK_REALTIME;
2185 	if (static_branch_unlikely(&netstamp_needed_key))
2186 		skb->tstamp = ktime_get_real();
2187 }
2188 
2189 #define net_timestamp_check(COND, SKB)				\
2190 	if (static_branch_unlikely(&netstamp_needed_key)) {	\
2191 		if ((COND) && !(SKB)->tstamp)			\
2192 			(SKB)->tstamp = ktime_get_real();	\
2193 	}							\
2194 
2195 bool is_skb_forwardable(const struct net_device *dev, const struct sk_buff *skb)
2196 {
2197 	return __is_skb_forwardable(dev, skb, true);
2198 }
2199 EXPORT_SYMBOL_GPL(is_skb_forwardable);
2200 
2201 static int __dev_forward_skb2(struct net_device *dev, struct sk_buff *skb,
2202 			      bool check_mtu)
2203 {
2204 	int ret = ____dev_forward_skb(dev, skb, check_mtu);
2205 
2206 	if (likely(!ret)) {
2207 		skb->protocol = eth_type_trans(skb, dev);
2208 		skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
2209 	}
2210 
2211 	return ret;
2212 }
2213 
2214 int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
2215 {
2216 	return __dev_forward_skb2(dev, skb, true);
2217 }
2218 EXPORT_SYMBOL_GPL(__dev_forward_skb);
2219 
2220 /**
2221  * dev_forward_skb - loopback an skb to another netif
2222  *
2223  * @dev: destination network device
2224  * @skb: buffer to forward
2225  *
2226  * return values:
2227  *	NET_RX_SUCCESS	(no congestion)
2228  *	NET_RX_DROP     (packet was dropped, but freed)
2229  *
2230  * dev_forward_skb can be used for injecting an skb from the
2231  * start_xmit function of one device into the receive queue
2232  * of another device.
2233  *
2234  * The receiving device may be in another namespace, so
2235  * we have to clear all information in the skb that could
2236  * impact namespace isolation.
2237  */
2238 int dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
2239 {
2240 	return __dev_forward_skb(dev, skb) ?: netif_rx_internal(skb);
2241 }
2242 EXPORT_SYMBOL_GPL(dev_forward_skb);
2243 
2244 int dev_forward_skb_nomtu(struct net_device *dev, struct sk_buff *skb)
2245 {
2246 	return __dev_forward_skb2(dev, skb, false) ?: netif_rx_internal(skb);
2247 }
2248 
2249 static inline int deliver_skb(struct sk_buff *skb,
2250 			      struct packet_type *pt_prev,
2251 			      struct net_device *orig_dev)
2252 {
2253 	if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
2254 		return -ENOMEM;
2255 	refcount_inc(&skb->users);
2256 	return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
2257 }
2258 
2259 static inline void deliver_ptype_list_skb(struct sk_buff *skb,
2260 					  struct packet_type **pt,
2261 					  struct net_device *orig_dev,
2262 					  __be16 type,
2263 					  struct list_head *ptype_list)
2264 {
2265 	struct packet_type *ptype, *pt_prev = *pt;
2266 
2267 	list_for_each_entry_rcu(ptype, ptype_list, list) {
2268 		if (ptype->type != type)
2269 			continue;
2270 		if (pt_prev)
2271 			deliver_skb(skb, pt_prev, orig_dev);
2272 		pt_prev = ptype;
2273 	}
2274 	*pt = pt_prev;
2275 }
2276 
2277 static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb)
2278 {
2279 	if (!ptype->af_packet_priv || !skb->sk)
2280 		return false;
2281 
2282 	if (ptype->id_match)
2283 		return ptype->id_match(ptype, skb->sk);
2284 	else if ((struct sock *)ptype->af_packet_priv == skb->sk)
2285 		return true;
2286 
2287 	return false;
2288 }
2289 
2290 /**
2291  * dev_nit_active - return true if any network interface taps are in use
2292  *
2293  * @dev: network device to check for the presence of taps
2294  */
2295 bool dev_nit_active(struct net_device *dev)
2296 {
2297 	return !list_empty(&net_hotdata.ptype_all) ||
2298 	       !list_empty(&dev->ptype_all);
2299 }
2300 EXPORT_SYMBOL_GPL(dev_nit_active);
2301 
2302 /*
2303  *	Support routine. Sends outgoing frames to any network
2304  *	taps currently in use.
2305  */
2306 
2307 void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
2308 {
2309 	struct list_head *ptype_list = &net_hotdata.ptype_all;
2310 	struct packet_type *ptype, *pt_prev = NULL;
2311 	struct sk_buff *skb2 = NULL;
2312 
2313 	rcu_read_lock();
2314 again:
2315 	list_for_each_entry_rcu(ptype, ptype_list, list) {
2316 		if (READ_ONCE(ptype->ignore_outgoing))
2317 			continue;
2318 
2319 		/* Never send packets back to the socket
2320 		 * they originated from - MvS (miquels@drinkel.ow.org)
2321 		 */
2322 		if (skb_loop_sk(ptype, skb))
2323 			continue;
2324 
2325 		if (pt_prev) {
2326 			deliver_skb(skb2, pt_prev, skb->dev);
2327 			pt_prev = ptype;
2328 			continue;
2329 		}
2330 
2331 		/* need to clone skb, done only once */
2332 		skb2 = skb_clone(skb, GFP_ATOMIC);
2333 		if (!skb2)
2334 			goto out_unlock;
2335 
2336 		net_timestamp_set(skb2);
2337 
2338 		/* skb->nh should be correctly
2339 		 * set by sender, so that the second statement is
2340 		 * just protection against buggy protocols.
2341 		 */
2342 		skb_reset_mac_header(skb2);
2343 
2344 		if (skb_network_header(skb2) < skb2->data ||
2345 		    skb_network_header(skb2) > skb_tail_pointer(skb2)) {
2346 			net_crit_ratelimited("protocol %04x is buggy, dev %s\n",
2347 					     ntohs(skb2->protocol),
2348 					     dev->name);
2349 			skb_reset_network_header(skb2);
2350 		}
2351 
2352 		skb2->transport_header = skb2->network_header;
2353 		skb2->pkt_type = PACKET_OUTGOING;
2354 		pt_prev = ptype;
2355 	}
2356 
2357 	if (ptype_list == &net_hotdata.ptype_all) {
2358 		ptype_list = &dev->ptype_all;
2359 		goto again;
2360 	}
2361 out_unlock:
2362 	if (pt_prev) {
2363 		if (!skb_orphan_frags_rx(skb2, GFP_ATOMIC))
2364 			pt_prev->func(skb2, skb->dev, pt_prev, skb->dev);
2365 		else
2366 			kfree_skb(skb2);
2367 	}
2368 	rcu_read_unlock();
2369 }
2370 EXPORT_SYMBOL_GPL(dev_queue_xmit_nit);
2371 
2372 /**
2373  * netif_setup_tc - Handle tc mappings on real_num_tx_queues change
2374  * @dev: Network device
2375  * @txq: number of queues available
2376  *
2377  * If real_num_tx_queues is changed the tc mappings may no longer be
2378  * valid. To resolve this verify the tc mapping remains valid and if
2379  * not NULL the mapping. With no priorities mapping to this
2380  * offset/count pair it will no longer be used. In the worst case TC0
2381  * is invalid nothing can be done so disable priority mappings. If is
2382  * expected that drivers will fix this mapping if they can before
2383  * calling netif_set_real_num_tx_queues.
2384  */
2385 static void netif_setup_tc(struct net_device *dev, unsigned int txq)
2386 {
2387 	int i;
2388 	struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
2389 
2390 	/* If TC0 is invalidated disable TC mapping */
2391 	if (tc->offset + tc->count > txq) {
2392 		netdev_warn(dev, "Number of in use tx queues changed invalidating tc mappings. Priority traffic classification disabled!\n");
2393 		dev->num_tc = 0;
2394 		return;
2395 	}
2396 
2397 	/* Invalidated prio to tc mappings set to TC0 */
2398 	for (i = 1; i < TC_BITMASK + 1; i++) {
2399 		int q = netdev_get_prio_tc_map(dev, i);
2400 
2401 		tc = &dev->tc_to_txq[q];
2402 		if (tc->offset + tc->count > txq) {
2403 			netdev_warn(dev, "Number of in use tx queues changed. Priority %i to tc mapping %i is no longer valid. Setting map to 0\n",
2404 				    i, q);
2405 			netdev_set_prio_tc_map(dev, i, 0);
2406 		}
2407 	}
2408 }
2409 
2410 int netdev_txq_to_tc(struct net_device *dev, unsigned int txq)
2411 {
2412 	if (dev->num_tc) {
2413 		struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
2414 		int i;
2415 
2416 		/* walk through the TCs and see if it falls into any of them */
2417 		for (i = 0; i < TC_MAX_QUEUE; i++, tc++) {
2418 			if ((txq - tc->offset) < tc->count)
2419 				return i;
2420 		}
2421 
2422 		/* didn't find it, just return -1 to indicate no match */
2423 		return -1;
2424 	}
2425 
2426 	return 0;
2427 }
2428 EXPORT_SYMBOL(netdev_txq_to_tc);
2429 
2430 #ifdef CONFIG_XPS
2431 static struct static_key xps_needed __read_mostly;
2432 static struct static_key xps_rxqs_needed __read_mostly;
2433 static DEFINE_MUTEX(xps_map_mutex);
2434 #define xmap_dereference(P)		\
2435 	rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex))
2436 
2437 static bool remove_xps_queue(struct xps_dev_maps *dev_maps,
2438 			     struct xps_dev_maps *old_maps, int tci, u16 index)
2439 {
2440 	struct xps_map *map = NULL;
2441 	int pos;
2442 
2443 	map = xmap_dereference(dev_maps->attr_map[tci]);
2444 	if (!map)
2445 		return false;
2446 
2447 	for (pos = map->len; pos--;) {
2448 		if (map->queues[pos] != index)
2449 			continue;
2450 
2451 		if (map->len > 1) {
2452 			map->queues[pos] = map->queues[--map->len];
2453 			break;
2454 		}
2455 
2456 		if (old_maps)
2457 			RCU_INIT_POINTER(old_maps->attr_map[tci], NULL);
2458 		RCU_INIT_POINTER(dev_maps->attr_map[tci], NULL);
2459 		kfree_rcu(map, rcu);
2460 		return false;
2461 	}
2462 
2463 	return true;
2464 }
2465 
2466 static bool remove_xps_queue_cpu(struct net_device *dev,
2467 				 struct xps_dev_maps *dev_maps,
2468 				 int cpu, u16 offset, u16 count)
2469 {
2470 	int num_tc = dev_maps->num_tc;
2471 	bool active = false;
2472 	int tci;
2473 
2474 	for (tci = cpu * num_tc; num_tc--; tci++) {
2475 		int i, j;
2476 
2477 		for (i = count, j = offset; i--; j++) {
2478 			if (!remove_xps_queue(dev_maps, NULL, tci, j))
2479 				break;
2480 		}
2481 
2482 		active |= i < 0;
2483 	}
2484 
2485 	return active;
2486 }
2487 
2488 static void reset_xps_maps(struct net_device *dev,
2489 			   struct xps_dev_maps *dev_maps,
2490 			   enum xps_map_type type)
2491 {
2492 	static_key_slow_dec_cpuslocked(&xps_needed);
2493 	if (type == XPS_RXQS)
2494 		static_key_slow_dec_cpuslocked(&xps_rxqs_needed);
2495 
2496 	RCU_INIT_POINTER(dev->xps_maps[type], NULL);
2497 
2498 	kfree_rcu(dev_maps, rcu);
2499 }
2500 
2501 static void clean_xps_maps(struct net_device *dev, enum xps_map_type type,
2502 			   u16 offset, u16 count)
2503 {
2504 	struct xps_dev_maps *dev_maps;
2505 	bool active = false;
2506 	int i, j;
2507 
2508 	dev_maps = xmap_dereference(dev->xps_maps[type]);
2509 	if (!dev_maps)
2510 		return;
2511 
2512 	for (j = 0; j < dev_maps->nr_ids; j++)
2513 		active |= remove_xps_queue_cpu(dev, dev_maps, j, offset, count);
2514 	if (!active)
2515 		reset_xps_maps(dev, dev_maps, type);
2516 
2517 	if (type == XPS_CPUS) {
2518 		for (i = offset + (count - 1); count--; i--)
2519 			netdev_queue_numa_node_write(
2520 				netdev_get_tx_queue(dev, i), NUMA_NO_NODE);
2521 	}
2522 }
2523 
2524 static void netif_reset_xps_queues(struct net_device *dev, u16 offset,
2525 				   u16 count)
2526 {
2527 	if (!static_key_false(&xps_needed))
2528 		return;
2529 
2530 	cpus_read_lock();
2531 	mutex_lock(&xps_map_mutex);
2532 
2533 	if (static_key_false(&xps_rxqs_needed))
2534 		clean_xps_maps(dev, XPS_RXQS, offset, count);
2535 
2536 	clean_xps_maps(dev, XPS_CPUS, offset, count);
2537 
2538 	mutex_unlock(&xps_map_mutex);
2539 	cpus_read_unlock();
2540 }
2541 
2542 static void netif_reset_xps_queues_gt(struct net_device *dev, u16 index)
2543 {
2544 	netif_reset_xps_queues(dev, index, dev->num_tx_queues - index);
2545 }
2546 
2547 static struct xps_map *expand_xps_map(struct xps_map *map, int attr_index,
2548 				      u16 index, bool is_rxqs_map)
2549 {
2550 	struct xps_map *new_map;
2551 	int alloc_len = XPS_MIN_MAP_ALLOC;
2552 	int i, pos;
2553 
2554 	for (pos = 0; map && pos < map->len; pos++) {
2555 		if (map->queues[pos] != index)
2556 			continue;
2557 		return map;
2558 	}
2559 
2560 	/* Need to add tx-queue to this CPU's/rx-queue's existing map */
2561 	if (map) {
2562 		if (pos < map->alloc_len)
2563 			return map;
2564 
2565 		alloc_len = map->alloc_len * 2;
2566 	}
2567 
2568 	/* Need to allocate new map to store tx-queue on this CPU's/rx-queue's
2569 	 *  map
2570 	 */
2571 	if (is_rxqs_map)
2572 		new_map = kzalloc(XPS_MAP_SIZE(alloc_len), GFP_KERNEL);
2573 	else
2574 		new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), GFP_KERNEL,
2575 				       cpu_to_node(attr_index));
2576 	if (!new_map)
2577 		return NULL;
2578 
2579 	for (i = 0; i < pos; i++)
2580 		new_map->queues[i] = map->queues[i];
2581 	new_map->alloc_len = alloc_len;
2582 	new_map->len = pos;
2583 
2584 	return new_map;
2585 }
2586 
2587 /* Copy xps maps at a given index */
2588 static void xps_copy_dev_maps(struct xps_dev_maps *dev_maps,
2589 			      struct xps_dev_maps *new_dev_maps, int index,
2590 			      int tc, bool skip_tc)
2591 {
2592 	int i, tci = index * dev_maps->num_tc;
2593 	struct xps_map *map;
2594 
2595 	/* copy maps belonging to foreign traffic classes */
2596 	for (i = 0; i < dev_maps->num_tc; i++, tci++) {
2597 		if (i == tc && skip_tc)
2598 			continue;
2599 
2600 		/* fill in the new device map from the old device map */
2601 		map = xmap_dereference(dev_maps->attr_map[tci]);
2602 		RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
2603 	}
2604 }
2605 
2606 /* Must be called under cpus_read_lock */
2607 int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask,
2608 			  u16 index, enum xps_map_type type)
2609 {
2610 	struct xps_dev_maps *dev_maps, *new_dev_maps = NULL, *old_dev_maps = NULL;
2611 	const unsigned long *online_mask = NULL;
2612 	bool active = false, copy = false;
2613 	int i, j, tci, numa_node_id = -2;
2614 	int maps_sz, num_tc = 1, tc = 0;
2615 	struct xps_map *map, *new_map;
2616 	unsigned int nr_ids;
2617 
2618 	WARN_ON_ONCE(index >= dev->num_tx_queues);
2619 
2620 	if (dev->num_tc) {
2621 		/* Do not allow XPS on subordinate device directly */
2622 		num_tc = dev->num_tc;
2623 		if (num_tc < 0)
2624 			return -EINVAL;
2625 
2626 		/* If queue belongs to subordinate dev use its map */
2627 		dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev;
2628 
2629 		tc = netdev_txq_to_tc(dev, index);
2630 		if (tc < 0)
2631 			return -EINVAL;
2632 	}
2633 
2634 	mutex_lock(&xps_map_mutex);
2635 
2636 	dev_maps = xmap_dereference(dev->xps_maps[type]);
2637 	if (type == XPS_RXQS) {
2638 		maps_sz = XPS_RXQ_DEV_MAPS_SIZE(num_tc, dev->num_rx_queues);
2639 		nr_ids = dev->num_rx_queues;
2640 	} else {
2641 		maps_sz = XPS_CPU_DEV_MAPS_SIZE(num_tc);
2642 		if (num_possible_cpus() > 1)
2643 			online_mask = cpumask_bits(cpu_online_mask);
2644 		nr_ids = nr_cpu_ids;
2645 	}
2646 
2647 	if (maps_sz < L1_CACHE_BYTES)
2648 		maps_sz = L1_CACHE_BYTES;
2649 
2650 	/* The old dev_maps could be larger or smaller than the one we're
2651 	 * setting up now, as dev->num_tc or nr_ids could have been updated in
2652 	 * between. We could try to be smart, but let's be safe instead and only
2653 	 * copy foreign traffic classes if the two map sizes match.
2654 	 */
2655 	if (dev_maps &&
2656 	    dev_maps->num_tc == num_tc && dev_maps->nr_ids == nr_ids)
2657 		copy = true;
2658 
2659 	/* allocate memory for queue storage */
2660 	for (j = -1; j = netif_attrmask_next_and(j, online_mask, mask, nr_ids),
2661 	     j < nr_ids;) {
2662 		if (!new_dev_maps) {
2663 			new_dev_maps = kzalloc(maps_sz, GFP_KERNEL);
2664 			if (!new_dev_maps) {
2665 				mutex_unlock(&xps_map_mutex);
2666 				return -ENOMEM;
2667 			}
2668 
2669 			new_dev_maps->nr_ids = nr_ids;
2670 			new_dev_maps->num_tc = num_tc;
2671 		}
2672 
2673 		tci = j * num_tc + tc;
2674 		map = copy ? xmap_dereference(dev_maps->attr_map[tci]) : NULL;
2675 
2676 		map = expand_xps_map(map, j, index, type == XPS_RXQS);
2677 		if (!map)
2678 			goto error;
2679 
2680 		RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
2681 	}
2682 
2683 	if (!new_dev_maps)
2684 		goto out_no_new_maps;
2685 
2686 	if (!dev_maps) {
2687 		/* Increment static keys at most once per type */
2688 		static_key_slow_inc_cpuslocked(&xps_needed);
2689 		if (type == XPS_RXQS)
2690 			static_key_slow_inc_cpuslocked(&xps_rxqs_needed);
2691 	}
2692 
2693 	for (j = 0; j < nr_ids; j++) {
2694 		bool skip_tc = false;
2695 
2696 		tci = j * num_tc + tc;
2697 		if (netif_attr_test_mask(j, mask, nr_ids) &&
2698 		    netif_attr_test_online(j, online_mask, nr_ids)) {
2699 			/* add tx-queue to CPU/rx-queue maps */
2700 			int pos = 0;
2701 
2702 			skip_tc = true;
2703 
2704 			map = xmap_dereference(new_dev_maps->attr_map[tci]);
2705 			while ((pos < map->len) && (map->queues[pos] != index))
2706 				pos++;
2707 
2708 			if (pos == map->len)
2709 				map->queues[map->len++] = index;
2710 #ifdef CONFIG_NUMA
2711 			if (type == XPS_CPUS) {
2712 				if (numa_node_id == -2)
2713 					numa_node_id = cpu_to_node(j);
2714 				else if (numa_node_id != cpu_to_node(j))
2715 					numa_node_id = -1;
2716 			}
2717 #endif
2718 		}
2719 
2720 		if (copy)
2721 			xps_copy_dev_maps(dev_maps, new_dev_maps, j, tc,
2722 					  skip_tc);
2723 	}
2724 
2725 	rcu_assign_pointer(dev->xps_maps[type], new_dev_maps);
2726 
2727 	/* Cleanup old maps */
2728 	if (!dev_maps)
2729 		goto out_no_old_maps;
2730 
2731 	for (j = 0; j < dev_maps->nr_ids; j++) {
2732 		for (i = num_tc, tci = j * dev_maps->num_tc; i--; tci++) {
2733 			map = xmap_dereference(dev_maps->attr_map[tci]);
2734 			if (!map)
2735 				continue;
2736 
2737 			if (copy) {
2738 				new_map = xmap_dereference(new_dev_maps->attr_map[tci]);
2739 				if (map == new_map)
2740 					continue;
2741 			}
2742 
2743 			RCU_INIT_POINTER(dev_maps->attr_map[tci], NULL);
2744 			kfree_rcu(map, rcu);
2745 		}
2746 	}
2747 
2748 	old_dev_maps = dev_maps;
2749 
2750 out_no_old_maps:
2751 	dev_maps = new_dev_maps;
2752 	active = true;
2753 
2754 out_no_new_maps:
2755 	if (type == XPS_CPUS)
2756 		/* update Tx queue numa node */
2757 		netdev_queue_numa_node_write(netdev_get_tx_queue(dev, index),
2758 					     (numa_node_id >= 0) ?
2759 					     numa_node_id : NUMA_NO_NODE);
2760 
2761 	if (!dev_maps)
2762 		goto out_no_maps;
2763 
2764 	/* removes tx-queue from unused CPUs/rx-queues */
2765 	for (j = 0; j < dev_maps->nr_ids; j++) {
2766 		tci = j * dev_maps->num_tc;
2767 
2768 		for (i = 0; i < dev_maps->num_tc; i++, tci++) {
2769 			if (i == tc &&
2770 			    netif_attr_test_mask(j, mask, dev_maps->nr_ids) &&
2771 			    netif_attr_test_online(j, online_mask, dev_maps->nr_ids))
2772 				continue;
2773 
2774 			active |= remove_xps_queue(dev_maps,
2775 						   copy ? old_dev_maps : NULL,
2776 						   tci, index);
2777 		}
2778 	}
2779 
2780 	if (old_dev_maps)
2781 		kfree_rcu(old_dev_maps, rcu);
2782 
2783 	/* free map if not active */
2784 	if (!active)
2785 		reset_xps_maps(dev, dev_maps, type);
2786 
2787 out_no_maps:
2788 	mutex_unlock(&xps_map_mutex);
2789 
2790 	return 0;
2791 error:
2792 	/* remove any maps that we added */
2793 	for (j = 0; j < nr_ids; j++) {
2794 		for (i = num_tc, tci = j * num_tc; i--; tci++) {
2795 			new_map = xmap_dereference(new_dev_maps->attr_map[tci]);
2796 			map = copy ?
2797 			      xmap_dereference(dev_maps->attr_map[tci]) :
2798 			      NULL;
2799 			if (new_map && new_map != map)
2800 				kfree(new_map);
2801 		}
2802 	}
2803 
2804 	mutex_unlock(&xps_map_mutex);
2805 
2806 	kfree(new_dev_maps);
2807 	return -ENOMEM;
2808 }
2809 EXPORT_SYMBOL_GPL(__netif_set_xps_queue);
2810 
2811 int netif_set_xps_queue(struct net_device *dev, const struct cpumask *mask,
2812 			u16 index)
2813 {
2814 	int ret;
2815 
2816 	cpus_read_lock();
2817 	ret =  __netif_set_xps_queue(dev, cpumask_bits(mask), index, XPS_CPUS);
2818 	cpus_read_unlock();
2819 
2820 	return ret;
2821 }
2822 EXPORT_SYMBOL(netif_set_xps_queue);
2823 
2824 #endif
2825 static void netdev_unbind_all_sb_channels(struct net_device *dev)
2826 {
2827 	struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues];
2828 
2829 	/* Unbind any subordinate channels */
2830 	while (txq-- != &dev->_tx[0]) {
2831 		if (txq->sb_dev)
2832 			netdev_unbind_sb_channel(dev, txq->sb_dev);
2833 	}
2834 }
2835 
2836 void netdev_reset_tc(struct net_device *dev)
2837 {
2838 #ifdef CONFIG_XPS
2839 	netif_reset_xps_queues_gt(dev, 0);
2840 #endif
2841 	netdev_unbind_all_sb_channels(dev);
2842 
2843 	/* Reset TC configuration of device */
2844 	dev->num_tc = 0;
2845 	memset(dev->tc_to_txq, 0, sizeof(dev->tc_to_txq));
2846 	memset(dev->prio_tc_map, 0, sizeof(dev->prio_tc_map));
2847 }
2848 EXPORT_SYMBOL(netdev_reset_tc);
2849 
2850 int netdev_set_tc_queue(struct net_device *dev, u8 tc, u16 count, u16 offset)
2851 {
2852 	if (tc >= dev->num_tc)
2853 		return -EINVAL;
2854 
2855 #ifdef CONFIG_XPS
2856 	netif_reset_xps_queues(dev, offset, count);
2857 #endif
2858 	dev->tc_to_txq[tc].count = count;
2859 	dev->tc_to_txq[tc].offset = offset;
2860 	return 0;
2861 }
2862 EXPORT_SYMBOL(netdev_set_tc_queue);
2863 
2864 int netdev_set_num_tc(struct net_device *dev, u8 num_tc)
2865 {
2866 	if (num_tc > TC_MAX_QUEUE)
2867 		return -EINVAL;
2868 
2869 #ifdef CONFIG_XPS
2870 	netif_reset_xps_queues_gt(dev, 0);
2871 #endif
2872 	netdev_unbind_all_sb_channels(dev);
2873 
2874 	dev->num_tc = num_tc;
2875 	return 0;
2876 }
2877 EXPORT_SYMBOL(netdev_set_num_tc);
2878 
2879 void netdev_unbind_sb_channel(struct net_device *dev,
2880 			      struct net_device *sb_dev)
2881 {
2882 	struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues];
2883 
2884 #ifdef CONFIG_XPS
2885 	netif_reset_xps_queues_gt(sb_dev, 0);
2886 #endif
2887 	memset(sb_dev->tc_to_txq, 0, sizeof(sb_dev->tc_to_txq));
2888 	memset(sb_dev->prio_tc_map, 0, sizeof(sb_dev->prio_tc_map));
2889 
2890 	while (txq-- != &dev->_tx[0]) {
2891 		if (txq->sb_dev == sb_dev)
2892 			txq->sb_dev = NULL;
2893 	}
2894 }
2895 EXPORT_SYMBOL(netdev_unbind_sb_channel);
2896 
2897 int netdev_bind_sb_channel_queue(struct net_device *dev,
2898 				 struct net_device *sb_dev,
2899 				 u8 tc, u16 count, u16 offset)
2900 {
2901 	/* Make certain the sb_dev and dev are already configured */
2902 	if (sb_dev->num_tc >= 0 || tc >= dev->num_tc)
2903 		return -EINVAL;
2904 
2905 	/* We cannot hand out queues we don't have */
2906 	if ((offset + count) > dev->real_num_tx_queues)
2907 		return -EINVAL;
2908 
2909 	/* Record the mapping */
2910 	sb_dev->tc_to_txq[tc].count = count;
2911 	sb_dev->tc_to_txq[tc].offset = offset;
2912 
2913 	/* Provide a way for Tx queue to find the tc_to_txq map or
2914 	 * XPS map for itself.
2915 	 */
2916 	while (count--)
2917 		netdev_get_tx_queue(dev, count + offset)->sb_dev = sb_dev;
2918 
2919 	return 0;
2920 }
2921 EXPORT_SYMBOL(netdev_bind_sb_channel_queue);
2922 
2923 int netdev_set_sb_channel(struct net_device *dev, u16 channel)
2924 {
2925 	/* Do not use a multiqueue device to represent a subordinate channel */
2926 	if (netif_is_multiqueue(dev))
2927 		return -ENODEV;
2928 
2929 	/* We allow channels 1 - 32767 to be used for subordinate channels.
2930 	 * Channel 0 is meant to be "native" mode and used only to represent
2931 	 * the main root device. We allow writing 0 to reset the device back
2932 	 * to normal mode after being used as a subordinate channel.
2933 	 */
2934 	if (channel > S16_MAX)
2935 		return -EINVAL;
2936 
2937 	dev->num_tc = -channel;
2938 
2939 	return 0;
2940 }
2941 EXPORT_SYMBOL(netdev_set_sb_channel);
2942 
2943 /*
2944  * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues
2945  * greater than real_num_tx_queues stale skbs on the qdisc must be flushed.
2946  */
2947 int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq)
2948 {
2949 	bool disabling;
2950 	int rc;
2951 
2952 	disabling = txq < dev->real_num_tx_queues;
2953 
2954 	if (txq < 1 || txq > dev->num_tx_queues)
2955 		return -EINVAL;
2956 
2957 	if (dev->reg_state == NETREG_REGISTERED ||
2958 	    dev->reg_state == NETREG_UNREGISTERING) {
2959 		ASSERT_RTNL();
2960 
2961 		rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues,
2962 						  txq);
2963 		if (rc)
2964 			return rc;
2965 
2966 		if (dev->num_tc)
2967 			netif_setup_tc(dev, txq);
2968 
2969 		net_shaper_set_real_num_tx_queues(dev, txq);
2970 
2971 		dev_qdisc_change_real_num_tx(dev, txq);
2972 
2973 		dev->real_num_tx_queues = txq;
2974 
2975 		if (disabling) {
2976 			synchronize_net();
2977 			qdisc_reset_all_tx_gt(dev, txq);
2978 #ifdef CONFIG_XPS
2979 			netif_reset_xps_queues_gt(dev, txq);
2980 #endif
2981 		}
2982 	} else {
2983 		dev->real_num_tx_queues = txq;
2984 	}
2985 
2986 	return 0;
2987 }
2988 EXPORT_SYMBOL(netif_set_real_num_tx_queues);
2989 
2990 #ifdef CONFIG_SYSFS
2991 /**
2992  *	netif_set_real_num_rx_queues - set actual number of RX queues used
2993  *	@dev: Network device
2994  *	@rxq: Actual number of RX queues
2995  *
2996  *	This must be called either with the rtnl_lock held or before
2997  *	registration of the net device.  Returns 0 on success, or a
2998  *	negative error code.  If called before registration, it always
2999  *	succeeds.
3000  */
3001 int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq)
3002 {
3003 	int rc;
3004 
3005 	if (rxq < 1 || rxq > dev->num_rx_queues)
3006 		return -EINVAL;
3007 
3008 	if (dev->reg_state == NETREG_REGISTERED) {
3009 		ASSERT_RTNL();
3010 
3011 		rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues,
3012 						  rxq);
3013 		if (rc)
3014 			return rc;
3015 	}
3016 
3017 	dev->real_num_rx_queues = rxq;
3018 	return 0;
3019 }
3020 EXPORT_SYMBOL(netif_set_real_num_rx_queues);
3021 #endif
3022 
3023 /**
3024  *	netif_set_real_num_queues - set actual number of RX and TX queues used
3025  *	@dev: Network device
3026  *	@txq: Actual number of TX queues
3027  *	@rxq: Actual number of RX queues
3028  *
3029  *	Set the real number of both TX and RX queues.
3030  *	Does nothing if the number of queues is already correct.
3031  */
3032 int netif_set_real_num_queues(struct net_device *dev,
3033 			      unsigned int txq, unsigned int rxq)
3034 {
3035 	unsigned int old_rxq = dev->real_num_rx_queues;
3036 	int err;
3037 
3038 	if (txq < 1 || txq > dev->num_tx_queues ||
3039 	    rxq < 1 || rxq > dev->num_rx_queues)
3040 		return -EINVAL;
3041 
3042 	/* Start from increases, so the error path only does decreases -
3043 	 * decreases can't fail.
3044 	 */
3045 	if (rxq > dev->real_num_rx_queues) {
3046 		err = netif_set_real_num_rx_queues(dev, rxq);
3047 		if (err)
3048 			return err;
3049 	}
3050 	if (txq > dev->real_num_tx_queues) {
3051 		err = netif_set_real_num_tx_queues(dev, txq);
3052 		if (err)
3053 			goto undo_rx;
3054 	}
3055 	if (rxq < dev->real_num_rx_queues)
3056 		WARN_ON(netif_set_real_num_rx_queues(dev, rxq));
3057 	if (txq < dev->real_num_tx_queues)
3058 		WARN_ON(netif_set_real_num_tx_queues(dev, txq));
3059 
3060 	return 0;
3061 undo_rx:
3062 	WARN_ON(netif_set_real_num_rx_queues(dev, old_rxq));
3063 	return err;
3064 }
3065 EXPORT_SYMBOL(netif_set_real_num_queues);
3066 
3067 /**
3068  * netif_set_tso_max_size() - set the max size of TSO frames supported
3069  * @dev:	netdev to update
3070  * @size:	max skb->len of a TSO frame
3071  *
3072  * Set the limit on the size of TSO super-frames the device can handle.
3073  * Unless explicitly set the stack will assume the value of
3074  * %GSO_LEGACY_MAX_SIZE.
3075  */
3076 void netif_set_tso_max_size(struct net_device *dev, unsigned int size)
3077 {
3078 	dev->tso_max_size = min(GSO_MAX_SIZE, size);
3079 	if (size < READ_ONCE(dev->gso_max_size))
3080 		netif_set_gso_max_size(dev, size);
3081 	if (size < READ_ONCE(dev->gso_ipv4_max_size))
3082 		netif_set_gso_ipv4_max_size(dev, size);
3083 }
3084 EXPORT_SYMBOL(netif_set_tso_max_size);
3085 
3086 /**
3087  * netif_set_tso_max_segs() - set the max number of segs supported for TSO
3088  * @dev:	netdev to update
3089  * @segs:	max number of TCP segments
3090  *
3091  * Set the limit on the number of TCP segments the device can generate from
3092  * a single TSO super-frame.
3093  * Unless explicitly set the stack will assume the value of %GSO_MAX_SEGS.
3094  */
3095 void netif_set_tso_max_segs(struct net_device *dev, unsigned int segs)
3096 {
3097 	dev->tso_max_segs = segs;
3098 	if (segs < READ_ONCE(dev->gso_max_segs))
3099 		netif_set_gso_max_segs(dev, segs);
3100 }
3101 EXPORT_SYMBOL(netif_set_tso_max_segs);
3102 
3103 /**
3104  * netif_inherit_tso_max() - copy all TSO limits from a lower device to an upper
3105  * @to:		netdev to update
3106  * @from:	netdev from which to copy the limits
3107  */
3108 void netif_inherit_tso_max(struct net_device *to, const struct net_device *from)
3109 {
3110 	netif_set_tso_max_size(to, from->tso_max_size);
3111 	netif_set_tso_max_segs(to, from->tso_max_segs);
3112 }
3113 EXPORT_SYMBOL(netif_inherit_tso_max);
3114 
3115 /**
3116  * netif_get_num_default_rss_queues - default number of RSS queues
3117  *
3118  * Default value is the number of physical cores if there are only 1 or 2, or
3119  * divided by 2 if there are more.
3120  */
3121 int netif_get_num_default_rss_queues(void)
3122 {
3123 	cpumask_var_t cpus;
3124 	int cpu, count = 0;
3125 
3126 	if (unlikely(is_kdump_kernel() || !zalloc_cpumask_var(&cpus, GFP_KERNEL)))
3127 		return 1;
3128 
3129 	cpumask_copy(cpus, cpu_online_mask);
3130 	for_each_cpu(cpu, cpus) {
3131 		++count;
3132 		cpumask_andnot(cpus, cpus, topology_sibling_cpumask(cpu));
3133 	}
3134 	free_cpumask_var(cpus);
3135 
3136 	return count > 2 ? DIV_ROUND_UP(count, 2) : count;
3137 }
3138 EXPORT_SYMBOL(netif_get_num_default_rss_queues);
3139 
3140 static void __netif_reschedule(struct Qdisc *q)
3141 {
3142 	struct softnet_data *sd;
3143 	unsigned long flags;
3144 
3145 	local_irq_save(flags);
3146 	sd = this_cpu_ptr(&softnet_data);
3147 	q->next_sched = NULL;
3148 	*sd->output_queue_tailp = q;
3149 	sd->output_queue_tailp = &q->next_sched;
3150 	raise_softirq_irqoff(NET_TX_SOFTIRQ);
3151 	local_irq_restore(flags);
3152 }
3153 
3154 void __netif_schedule(struct Qdisc *q)
3155 {
3156 	if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state))
3157 		__netif_reschedule(q);
3158 }
3159 EXPORT_SYMBOL(__netif_schedule);
3160 
3161 struct dev_kfree_skb_cb {
3162 	enum skb_drop_reason reason;
3163 };
3164 
3165 static struct dev_kfree_skb_cb *get_kfree_skb_cb(const struct sk_buff *skb)
3166 {
3167 	return (struct dev_kfree_skb_cb *)skb->cb;
3168 }
3169 
3170 void netif_schedule_queue(struct netdev_queue *txq)
3171 {
3172 	rcu_read_lock();
3173 	if (!netif_xmit_stopped(txq)) {
3174 		struct Qdisc *q = rcu_dereference(txq->qdisc);
3175 
3176 		__netif_schedule(q);
3177 	}
3178 	rcu_read_unlock();
3179 }
3180 EXPORT_SYMBOL(netif_schedule_queue);
3181 
3182 void netif_tx_wake_queue(struct netdev_queue *dev_queue)
3183 {
3184 	if (test_and_clear_bit(__QUEUE_STATE_DRV_XOFF, &dev_queue->state)) {
3185 		struct Qdisc *q;
3186 
3187 		rcu_read_lock();
3188 		q = rcu_dereference(dev_queue->qdisc);
3189 		__netif_schedule(q);
3190 		rcu_read_unlock();
3191 	}
3192 }
3193 EXPORT_SYMBOL(netif_tx_wake_queue);
3194 
3195 void dev_kfree_skb_irq_reason(struct sk_buff *skb, enum skb_drop_reason reason)
3196 {
3197 	unsigned long flags;
3198 
3199 	if (unlikely(!skb))
3200 		return;
3201 
3202 	if (likely(refcount_read(&skb->users) == 1)) {
3203 		smp_rmb();
3204 		refcount_set(&skb->users, 0);
3205 	} else if (likely(!refcount_dec_and_test(&skb->users))) {
3206 		return;
3207 	}
3208 	get_kfree_skb_cb(skb)->reason = reason;
3209 	local_irq_save(flags);
3210 	skb->next = __this_cpu_read(softnet_data.completion_queue);
3211 	__this_cpu_write(softnet_data.completion_queue, skb);
3212 	raise_softirq_irqoff(NET_TX_SOFTIRQ);
3213 	local_irq_restore(flags);
3214 }
3215 EXPORT_SYMBOL(dev_kfree_skb_irq_reason);
3216 
3217 void dev_kfree_skb_any_reason(struct sk_buff *skb, enum skb_drop_reason reason)
3218 {
3219 	if (in_hardirq() || irqs_disabled())
3220 		dev_kfree_skb_irq_reason(skb, reason);
3221 	else
3222 		kfree_skb_reason(skb, reason);
3223 }
3224 EXPORT_SYMBOL(dev_kfree_skb_any_reason);
3225 
3226 
3227 /**
3228  * netif_device_detach - mark device as removed
3229  * @dev: network device
3230  *
3231  * Mark device as removed from system and therefore no longer available.
3232  */
3233 void netif_device_detach(struct net_device *dev)
3234 {
3235 	if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
3236 	    netif_running(dev)) {
3237 		netif_tx_stop_all_queues(dev);
3238 	}
3239 }
3240 EXPORT_SYMBOL(netif_device_detach);
3241 
3242 /**
3243  * netif_device_attach - mark device as attached
3244  * @dev: network device
3245  *
3246  * Mark device as attached from system and restart if needed.
3247  */
3248 void netif_device_attach(struct net_device *dev)
3249 {
3250 	if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
3251 	    netif_running(dev)) {
3252 		netif_tx_wake_all_queues(dev);
3253 		netdev_watchdog_up(dev);
3254 	}
3255 }
3256 EXPORT_SYMBOL(netif_device_attach);
3257 
3258 /*
3259  * Returns a Tx hash based on the given packet descriptor a Tx queues' number
3260  * to be used as a distribution range.
3261  */
3262 static u16 skb_tx_hash(const struct net_device *dev,
3263 		       const struct net_device *sb_dev,
3264 		       struct sk_buff *skb)
3265 {
3266 	u32 hash;
3267 	u16 qoffset = 0;
3268 	u16 qcount = dev->real_num_tx_queues;
3269 
3270 	if (dev->num_tc) {
3271 		u8 tc = netdev_get_prio_tc_map(dev, skb->priority);
3272 
3273 		qoffset = sb_dev->tc_to_txq[tc].offset;
3274 		qcount = sb_dev->tc_to_txq[tc].count;
3275 		if (unlikely(!qcount)) {
3276 			net_warn_ratelimited("%s: invalid qcount, qoffset %u for tc %u\n",
3277 					     sb_dev->name, qoffset, tc);
3278 			qoffset = 0;
3279 			qcount = dev->real_num_tx_queues;
3280 		}
3281 	}
3282 
3283 	if (skb_rx_queue_recorded(skb)) {
3284 		DEBUG_NET_WARN_ON_ONCE(qcount == 0);
3285 		hash = skb_get_rx_queue(skb);
3286 		if (hash >= qoffset)
3287 			hash -= qoffset;
3288 		while (unlikely(hash >= qcount))
3289 			hash -= qcount;
3290 		return hash + qoffset;
3291 	}
3292 
3293 	return (u16) reciprocal_scale(skb_get_hash(skb), qcount) + qoffset;
3294 }
3295 
3296 void skb_warn_bad_offload(const struct sk_buff *skb)
3297 {
3298 	static const netdev_features_t null_features;
3299 	struct net_device *dev = skb->dev;
3300 	const char *name = "";
3301 
3302 	if (!net_ratelimit())
3303 		return;
3304 
3305 	if (dev) {
3306 		if (dev->dev.parent)
3307 			name = dev_driver_string(dev->dev.parent);
3308 		else
3309 			name = netdev_name(dev);
3310 	}
3311 	skb_dump(KERN_WARNING, skb, false);
3312 	WARN(1, "%s: caps=(%pNF, %pNF)\n",
3313 	     name, dev ? &dev->features : &null_features,
3314 	     skb->sk ? &skb->sk->sk_route_caps : &null_features);
3315 }
3316 
3317 /*
3318  * Invalidate hardware checksum when packet is to be mangled, and
3319  * complete checksum manually on outgoing path.
3320  */
3321 int skb_checksum_help(struct sk_buff *skb)
3322 {
3323 	__wsum csum;
3324 	int ret = 0, offset;
3325 
3326 	if (skb->ip_summed == CHECKSUM_COMPLETE)
3327 		goto out_set_summed;
3328 
3329 	if (unlikely(skb_is_gso(skb))) {
3330 		skb_warn_bad_offload(skb);
3331 		return -EINVAL;
3332 	}
3333 
3334 	if (!skb_frags_readable(skb)) {
3335 		return -EFAULT;
3336 	}
3337 
3338 	/* Before computing a checksum, we should make sure no frag could
3339 	 * be modified by an external entity : checksum could be wrong.
3340 	 */
3341 	if (skb_has_shared_frag(skb)) {
3342 		ret = __skb_linearize(skb);
3343 		if (ret)
3344 			goto out;
3345 	}
3346 
3347 	offset = skb_checksum_start_offset(skb);
3348 	ret = -EINVAL;
3349 	if (unlikely(offset >= skb_headlen(skb))) {
3350 		DO_ONCE_LITE(skb_dump, KERN_ERR, skb, false);
3351 		WARN_ONCE(true, "offset (%d) >= skb_headlen() (%u)\n",
3352 			  offset, skb_headlen(skb));
3353 		goto out;
3354 	}
3355 	csum = skb_checksum(skb, offset, skb->len - offset, 0);
3356 
3357 	offset += skb->csum_offset;
3358 	if (unlikely(offset + sizeof(__sum16) > skb_headlen(skb))) {
3359 		DO_ONCE_LITE(skb_dump, KERN_ERR, skb, false);
3360 		WARN_ONCE(true, "offset+2 (%zu) > skb_headlen() (%u)\n",
3361 			  offset + sizeof(__sum16), skb_headlen(skb));
3362 		goto out;
3363 	}
3364 	ret = skb_ensure_writable(skb, offset + sizeof(__sum16));
3365 	if (ret)
3366 		goto out;
3367 
3368 	*(__sum16 *)(skb->data + offset) = csum_fold(csum) ?: CSUM_MANGLED_0;
3369 out_set_summed:
3370 	skb->ip_summed = CHECKSUM_NONE;
3371 out:
3372 	return ret;
3373 }
3374 EXPORT_SYMBOL(skb_checksum_help);
3375 
3376 int skb_crc32c_csum_help(struct sk_buff *skb)
3377 {
3378 	__le32 crc32c_csum;
3379 	int ret = 0, offset, start;
3380 
3381 	if (skb->ip_summed != CHECKSUM_PARTIAL)
3382 		goto out;
3383 
3384 	if (unlikely(skb_is_gso(skb)))
3385 		goto out;
3386 
3387 	/* Before computing a checksum, we should make sure no frag could
3388 	 * be modified by an external entity : checksum could be wrong.
3389 	 */
3390 	if (unlikely(skb_has_shared_frag(skb))) {
3391 		ret = __skb_linearize(skb);
3392 		if (ret)
3393 			goto out;
3394 	}
3395 	start = skb_checksum_start_offset(skb);
3396 	offset = start + offsetof(struct sctphdr, checksum);
3397 	if (WARN_ON_ONCE(offset >= skb_headlen(skb))) {
3398 		ret = -EINVAL;
3399 		goto out;
3400 	}
3401 
3402 	ret = skb_ensure_writable(skb, offset + sizeof(__le32));
3403 	if (ret)
3404 		goto out;
3405 
3406 	crc32c_csum = cpu_to_le32(~__skb_checksum(skb, start,
3407 						  skb->len - start, ~(__u32)0,
3408 						  crc32c_csum_stub));
3409 	*(__le32 *)(skb->data + offset) = crc32c_csum;
3410 	skb_reset_csum_not_inet(skb);
3411 out:
3412 	return ret;
3413 }
3414 EXPORT_SYMBOL(skb_crc32c_csum_help);
3415 
3416 __be16 skb_network_protocol(struct sk_buff *skb, int *depth)
3417 {
3418 	__be16 type = skb->protocol;
3419 
3420 	/* Tunnel gso handlers can set protocol to ethernet. */
3421 	if (type == htons(ETH_P_TEB)) {
3422 		struct ethhdr *eth;
3423 
3424 		if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr))))
3425 			return 0;
3426 
3427 		eth = (struct ethhdr *)skb->data;
3428 		type = eth->h_proto;
3429 	}
3430 
3431 	return vlan_get_protocol_and_depth(skb, type, depth);
3432 }
3433 
3434 
3435 /* Take action when hardware reception checksum errors are detected. */
3436 #ifdef CONFIG_BUG
3437 static void do_netdev_rx_csum_fault(struct net_device *dev, struct sk_buff *skb)
3438 {
3439 	netdev_err(dev, "hw csum failure\n");
3440 	skb_dump(KERN_ERR, skb, true);
3441 	dump_stack();
3442 }
3443 
3444 void netdev_rx_csum_fault(struct net_device *dev, struct sk_buff *skb)
3445 {
3446 	DO_ONCE_LITE(do_netdev_rx_csum_fault, dev, skb);
3447 }
3448 EXPORT_SYMBOL(netdev_rx_csum_fault);
3449 #endif
3450 
3451 /* XXX: check that highmem exists at all on the given machine. */
3452 static int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
3453 {
3454 #ifdef CONFIG_HIGHMEM
3455 	int i;
3456 
3457 	if (!(dev->features & NETIF_F_HIGHDMA)) {
3458 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3459 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3460 			struct page *page = skb_frag_page(frag);
3461 
3462 			if (page && PageHighMem(page))
3463 				return 1;
3464 		}
3465 	}
3466 #endif
3467 	return 0;
3468 }
3469 
3470 /* If MPLS offload request, verify we are testing hardware MPLS features
3471  * instead of standard features for the netdev.
3472  */
3473 #if IS_ENABLED(CONFIG_NET_MPLS_GSO)
3474 static netdev_features_t net_mpls_features(struct sk_buff *skb,
3475 					   netdev_features_t features,
3476 					   __be16 type)
3477 {
3478 	if (eth_p_mpls(type))
3479 		features &= skb->dev->mpls_features;
3480 
3481 	return features;
3482 }
3483 #else
3484 static netdev_features_t net_mpls_features(struct sk_buff *skb,
3485 					   netdev_features_t features,
3486 					   __be16 type)
3487 {
3488 	return features;
3489 }
3490 #endif
3491 
3492 static netdev_features_t harmonize_features(struct sk_buff *skb,
3493 	netdev_features_t features)
3494 {
3495 	__be16 type;
3496 
3497 	type = skb_network_protocol(skb, NULL);
3498 	features = net_mpls_features(skb, features, type);
3499 
3500 	if (skb->ip_summed != CHECKSUM_NONE &&
3501 	    !can_checksum_protocol(features, type)) {
3502 		features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
3503 	}
3504 	if (illegal_highdma(skb->dev, skb))
3505 		features &= ~NETIF_F_SG;
3506 
3507 	return features;
3508 }
3509 
3510 netdev_features_t passthru_features_check(struct sk_buff *skb,
3511 					  struct net_device *dev,
3512 					  netdev_features_t features)
3513 {
3514 	return features;
3515 }
3516 EXPORT_SYMBOL(passthru_features_check);
3517 
3518 static netdev_features_t dflt_features_check(struct sk_buff *skb,
3519 					     struct net_device *dev,
3520 					     netdev_features_t features)
3521 {
3522 	return vlan_features_check(skb, features);
3523 }
3524 
3525 static netdev_features_t gso_features_check(const struct sk_buff *skb,
3526 					    struct net_device *dev,
3527 					    netdev_features_t features)
3528 {
3529 	u16 gso_segs = skb_shinfo(skb)->gso_segs;
3530 
3531 	if (gso_segs > READ_ONCE(dev->gso_max_segs))
3532 		return features & ~NETIF_F_GSO_MASK;
3533 
3534 	if (unlikely(skb->len >= netif_get_gso_max_size(dev, skb)))
3535 		return features & ~NETIF_F_GSO_MASK;
3536 
3537 	if (!skb_shinfo(skb)->gso_type) {
3538 		skb_warn_bad_offload(skb);
3539 		return features & ~NETIF_F_GSO_MASK;
3540 	}
3541 
3542 	/* Support for GSO partial features requires software
3543 	 * intervention before we can actually process the packets
3544 	 * so we need to strip support for any partial features now
3545 	 * and we can pull them back in after we have partially
3546 	 * segmented the frame.
3547 	 */
3548 	if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL))
3549 		features &= ~dev->gso_partial_features;
3550 
3551 	/* Make sure to clear the IPv4 ID mangling feature if the
3552 	 * IPv4 header has the potential to be fragmented.
3553 	 */
3554 	if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
3555 		struct iphdr *iph = skb->encapsulation ?
3556 				    inner_ip_hdr(skb) : ip_hdr(skb);
3557 
3558 		if (!(iph->frag_off & htons(IP_DF)))
3559 			features &= ~NETIF_F_TSO_MANGLEID;
3560 	}
3561 
3562 	return features;
3563 }
3564 
3565 netdev_features_t netif_skb_features(struct sk_buff *skb)
3566 {
3567 	struct net_device *dev = skb->dev;
3568 	netdev_features_t features = dev->features;
3569 
3570 	if (skb_is_gso(skb))
3571 		features = gso_features_check(skb, dev, features);
3572 
3573 	/* If encapsulation offload request, verify we are testing
3574 	 * hardware encapsulation features instead of standard
3575 	 * features for the netdev
3576 	 */
3577 	if (skb->encapsulation)
3578 		features &= dev->hw_enc_features;
3579 
3580 	if (skb_vlan_tagged(skb))
3581 		features = netdev_intersect_features(features,
3582 						     dev->vlan_features |
3583 						     NETIF_F_HW_VLAN_CTAG_TX |
3584 						     NETIF_F_HW_VLAN_STAG_TX);
3585 
3586 	if (dev->netdev_ops->ndo_features_check)
3587 		features &= dev->netdev_ops->ndo_features_check(skb, dev,
3588 								features);
3589 	else
3590 		features &= dflt_features_check(skb, dev, features);
3591 
3592 	return harmonize_features(skb, features);
3593 }
3594 EXPORT_SYMBOL(netif_skb_features);
3595 
3596 static int xmit_one(struct sk_buff *skb, struct net_device *dev,
3597 		    struct netdev_queue *txq, bool more)
3598 {
3599 	unsigned int len;
3600 	int rc;
3601 
3602 	if (dev_nit_active(dev))
3603 		dev_queue_xmit_nit(skb, dev);
3604 
3605 	len = skb->len;
3606 	trace_net_dev_start_xmit(skb, dev);
3607 	rc = netdev_start_xmit(skb, dev, txq, more);
3608 	trace_net_dev_xmit(skb, rc, dev, len);
3609 
3610 	return rc;
3611 }
3612 
3613 struct sk_buff *dev_hard_start_xmit(struct sk_buff *first, struct net_device *dev,
3614 				    struct netdev_queue *txq, int *ret)
3615 {
3616 	struct sk_buff *skb = first;
3617 	int rc = NETDEV_TX_OK;
3618 
3619 	while (skb) {
3620 		struct sk_buff *next = skb->next;
3621 
3622 		skb_mark_not_on_list(skb);
3623 		rc = xmit_one(skb, dev, txq, next != NULL);
3624 		if (unlikely(!dev_xmit_complete(rc))) {
3625 			skb->next = next;
3626 			goto out;
3627 		}
3628 
3629 		skb = next;
3630 		if (netif_tx_queue_stopped(txq) && skb) {
3631 			rc = NETDEV_TX_BUSY;
3632 			break;
3633 		}
3634 	}
3635 
3636 out:
3637 	*ret = rc;
3638 	return skb;
3639 }
3640 
3641 static struct sk_buff *validate_xmit_vlan(struct sk_buff *skb,
3642 					  netdev_features_t features)
3643 {
3644 	if (skb_vlan_tag_present(skb) &&
3645 	    !vlan_hw_offload_capable(features, skb->vlan_proto))
3646 		skb = __vlan_hwaccel_push_inside(skb);
3647 	return skb;
3648 }
3649 
3650 int skb_csum_hwoffload_help(struct sk_buff *skb,
3651 			    const netdev_features_t features)
3652 {
3653 	if (unlikely(skb_csum_is_sctp(skb)))
3654 		return !!(features & NETIF_F_SCTP_CRC) ? 0 :
3655 			skb_crc32c_csum_help(skb);
3656 
3657 	if (features & NETIF_F_HW_CSUM)
3658 		return 0;
3659 
3660 	if (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
3661 		if (vlan_get_protocol(skb) == htons(ETH_P_IPV6) &&
3662 		    skb_network_header_len(skb) != sizeof(struct ipv6hdr) &&
3663 		    !ipv6_has_hopopt_jumbo(skb))
3664 			goto sw_checksum;
3665 
3666 		switch (skb->csum_offset) {
3667 		case offsetof(struct tcphdr, check):
3668 		case offsetof(struct udphdr, check):
3669 			return 0;
3670 		}
3671 	}
3672 
3673 sw_checksum:
3674 	return skb_checksum_help(skb);
3675 }
3676 EXPORT_SYMBOL(skb_csum_hwoffload_help);
3677 
3678 static struct sk_buff *validate_xmit_skb(struct sk_buff *skb, struct net_device *dev, bool *again)
3679 {
3680 	netdev_features_t features;
3681 
3682 	features = netif_skb_features(skb);
3683 	skb = validate_xmit_vlan(skb, features);
3684 	if (unlikely(!skb))
3685 		goto out_null;
3686 
3687 	skb = sk_validate_xmit_skb(skb, dev);
3688 	if (unlikely(!skb))
3689 		goto out_null;
3690 
3691 	if (netif_needs_gso(skb, features)) {
3692 		struct sk_buff *segs;
3693 
3694 		segs = skb_gso_segment(skb, features);
3695 		if (IS_ERR(segs)) {
3696 			goto out_kfree_skb;
3697 		} else if (segs) {
3698 			consume_skb(skb);
3699 			skb = segs;
3700 		}
3701 	} else {
3702 		if (skb_needs_linearize(skb, features) &&
3703 		    __skb_linearize(skb))
3704 			goto out_kfree_skb;
3705 
3706 		/* If packet is not checksummed and device does not
3707 		 * support checksumming for this protocol, complete
3708 		 * checksumming here.
3709 		 */
3710 		if (skb->ip_summed == CHECKSUM_PARTIAL) {
3711 			if (skb->encapsulation)
3712 				skb_set_inner_transport_header(skb,
3713 							       skb_checksum_start_offset(skb));
3714 			else
3715 				skb_set_transport_header(skb,
3716 							 skb_checksum_start_offset(skb));
3717 			if (skb_csum_hwoffload_help(skb, features))
3718 				goto out_kfree_skb;
3719 		}
3720 	}
3721 
3722 	skb = validate_xmit_xfrm(skb, features, again);
3723 
3724 	return skb;
3725 
3726 out_kfree_skb:
3727 	kfree_skb(skb);
3728 out_null:
3729 	dev_core_stats_tx_dropped_inc(dev);
3730 	return NULL;
3731 }
3732 
3733 struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *dev, bool *again)
3734 {
3735 	struct sk_buff *next, *head = NULL, *tail;
3736 
3737 	for (; skb != NULL; skb = next) {
3738 		next = skb->next;
3739 		skb_mark_not_on_list(skb);
3740 
3741 		/* in case skb won't be segmented, point to itself */
3742 		skb->prev = skb;
3743 
3744 		skb = validate_xmit_skb(skb, dev, again);
3745 		if (!skb)
3746 			continue;
3747 
3748 		if (!head)
3749 			head = skb;
3750 		else
3751 			tail->next = skb;
3752 		/* If skb was segmented, skb->prev points to
3753 		 * the last segment. If not, it still contains skb.
3754 		 */
3755 		tail = skb->prev;
3756 	}
3757 	return head;
3758 }
3759 EXPORT_SYMBOL_GPL(validate_xmit_skb_list);
3760 
3761 static void qdisc_pkt_len_init(struct sk_buff *skb)
3762 {
3763 	const struct skb_shared_info *shinfo = skb_shinfo(skb);
3764 
3765 	qdisc_skb_cb(skb)->pkt_len = skb->len;
3766 
3767 	/* To get more precise estimation of bytes sent on wire,
3768 	 * we add to pkt_len the headers size of all segments
3769 	 */
3770 	if (shinfo->gso_size && skb_transport_header_was_set(skb)) {
3771 		u16 gso_segs = shinfo->gso_segs;
3772 		unsigned int hdr_len;
3773 
3774 		/* mac layer + network layer */
3775 		hdr_len = skb_transport_offset(skb);
3776 
3777 		/* + transport layer */
3778 		if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
3779 			const struct tcphdr *th;
3780 			struct tcphdr _tcphdr;
3781 
3782 			th = skb_header_pointer(skb, hdr_len,
3783 						sizeof(_tcphdr), &_tcphdr);
3784 			if (likely(th))
3785 				hdr_len += __tcp_hdrlen(th);
3786 		} else if (shinfo->gso_type & SKB_GSO_UDP_L4) {
3787 			struct udphdr _udphdr;
3788 
3789 			if (skb_header_pointer(skb, hdr_len,
3790 					       sizeof(_udphdr), &_udphdr))
3791 				hdr_len += sizeof(struct udphdr);
3792 		}
3793 
3794 		if (unlikely(shinfo->gso_type & SKB_GSO_DODGY)) {
3795 			int payload = skb->len - hdr_len;
3796 
3797 			/* Malicious packet. */
3798 			if (payload <= 0)
3799 				return;
3800 			gso_segs = DIV_ROUND_UP(payload, shinfo->gso_size);
3801 		}
3802 		qdisc_skb_cb(skb)->pkt_len += (gso_segs - 1) * hdr_len;
3803 	}
3804 }
3805 
3806 static int dev_qdisc_enqueue(struct sk_buff *skb, struct Qdisc *q,
3807 			     struct sk_buff **to_free,
3808 			     struct netdev_queue *txq)
3809 {
3810 	int rc;
3811 
3812 	rc = q->enqueue(skb, q, to_free) & NET_XMIT_MASK;
3813 	if (rc == NET_XMIT_SUCCESS)
3814 		trace_qdisc_enqueue(q, txq, skb);
3815 	return rc;
3816 }
3817 
3818 static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
3819 				 struct net_device *dev,
3820 				 struct netdev_queue *txq)
3821 {
3822 	spinlock_t *root_lock = qdisc_lock(q);
3823 	struct sk_buff *to_free = NULL;
3824 	bool contended;
3825 	int rc;
3826 
3827 	qdisc_calculate_pkt_len(skb, q);
3828 
3829 	tcf_set_drop_reason(skb, SKB_DROP_REASON_QDISC_DROP);
3830 
3831 	if (q->flags & TCQ_F_NOLOCK) {
3832 		if (q->flags & TCQ_F_CAN_BYPASS && nolock_qdisc_is_empty(q) &&
3833 		    qdisc_run_begin(q)) {
3834 			/* Retest nolock_qdisc_is_empty() within the protection
3835 			 * of q->seqlock to protect from racing with requeuing.
3836 			 */
3837 			if (unlikely(!nolock_qdisc_is_empty(q))) {
3838 				rc = dev_qdisc_enqueue(skb, q, &to_free, txq);
3839 				__qdisc_run(q);
3840 				qdisc_run_end(q);
3841 
3842 				goto no_lock_out;
3843 			}
3844 
3845 			qdisc_bstats_cpu_update(q, skb);
3846 			if (sch_direct_xmit(skb, q, dev, txq, NULL, true) &&
3847 			    !nolock_qdisc_is_empty(q))
3848 				__qdisc_run(q);
3849 
3850 			qdisc_run_end(q);
3851 			return NET_XMIT_SUCCESS;
3852 		}
3853 
3854 		rc = dev_qdisc_enqueue(skb, q, &to_free, txq);
3855 		qdisc_run(q);
3856 
3857 no_lock_out:
3858 		if (unlikely(to_free))
3859 			kfree_skb_list_reason(to_free,
3860 					      tcf_get_drop_reason(to_free));
3861 		return rc;
3862 	}
3863 
3864 	if (unlikely(READ_ONCE(q->owner) == smp_processor_id())) {
3865 		kfree_skb_reason(skb, SKB_DROP_REASON_TC_RECLASSIFY_LOOP);
3866 		return NET_XMIT_DROP;
3867 	}
3868 	/*
3869 	 * Heuristic to force contended enqueues to serialize on a
3870 	 * separate lock before trying to get qdisc main lock.
3871 	 * This permits qdisc->running owner to get the lock more
3872 	 * often and dequeue packets faster.
3873 	 * On PREEMPT_RT it is possible to preempt the qdisc owner during xmit
3874 	 * and then other tasks will only enqueue packets. The packets will be
3875 	 * sent after the qdisc owner is scheduled again. To prevent this
3876 	 * scenario the task always serialize on the lock.
3877 	 */
3878 	contended = qdisc_is_running(q) || IS_ENABLED(CONFIG_PREEMPT_RT);
3879 	if (unlikely(contended))
3880 		spin_lock(&q->busylock);
3881 
3882 	spin_lock(root_lock);
3883 	if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
3884 		__qdisc_drop(skb, &to_free);
3885 		rc = NET_XMIT_DROP;
3886 	} else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) &&
3887 		   qdisc_run_begin(q)) {
3888 		/*
3889 		 * This is a work-conserving queue; there are no old skbs
3890 		 * waiting to be sent out; and the qdisc is not running -
3891 		 * xmit the skb directly.
3892 		 */
3893 
3894 		qdisc_bstats_update(q, skb);
3895 
3896 		if (sch_direct_xmit(skb, q, dev, txq, root_lock, true)) {
3897 			if (unlikely(contended)) {
3898 				spin_unlock(&q->busylock);
3899 				contended = false;
3900 			}
3901 			__qdisc_run(q);
3902 		}
3903 
3904 		qdisc_run_end(q);
3905 		rc = NET_XMIT_SUCCESS;
3906 	} else {
3907 		WRITE_ONCE(q->owner, smp_processor_id());
3908 		rc = dev_qdisc_enqueue(skb, q, &to_free, txq);
3909 		WRITE_ONCE(q->owner, -1);
3910 		if (qdisc_run_begin(q)) {
3911 			if (unlikely(contended)) {
3912 				spin_unlock(&q->busylock);
3913 				contended = false;
3914 			}
3915 			__qdisc_run(q);
3916 			qdisc_run_end(q);
3917 		}
3918 	}
3919 	spin_unlock(root_lock);
3920 	if (unlikely(to_free))
3921 		kfree_skb_list_reason(to_free,
3922 				      tcf_get_drop_reason(to_free));
3923 	if (unlikely(contended))
3924 		spin_unlock(&q->busylock);
3925 	return rc;
3926 }
3927 
3928 #if IS_ENABLED(CONFIG_CGROUP_NET_PRIO)
3929 static void skb_update_prio(struct sk_buff *skb)
3930 {
3931 	const struct netprio_map *map;
3932 	const struct sock *sk;
3933 	unsigned int prioidx;
3934 
3935 	if (skb->priority)
3936 		return;
3937 	map = rcu_dereference_bh(skb->dev->priomap);
3938 	if (!map)
3939 		return;
3940 	sk = skb_to_full_sk(skb);
3941 	if (!sk)
3942 		return;
3943 
3944 	prioidx = sock_cgroup_prioidx(&sk->sk_cgrp_data);
3945 
3946 	if (prioidx < map->priomap_len)
3947 		skb->priority = map->priomap[prioidx];
3948 }
3949 #else
3950 #define skb_update_prio(skb)
3951 #endif
3952 
3953 /**
3954  *	dev_loopback_xmit - loop back @skb
3955  *	@net: network namespace this loopback is happening in
3956  *	@sk:  sk needed to be a netfilter okfn
3957  *	@skb: buffer to transmit
3958  */
3959 int dev_loopback_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
3960 {
3961 	skb_reset_mac_header(skb);
3962 	__skb_pull(skb, skb_network_offset(skb));
3963 	skb->pkt_type = PACKET_LOOPBACK;
3964 	if (skb->ip_summed == CHECKSUM_NONE)
3965 		skb->ip_summed = CHECKSUM_UNNECESSARY;
3966 	DEBUG_NET_WARN_ON_ONCE(!skb_dst(skb));
3967 	skb_dst_force(skb);
3968 	netif_rx(skb);
3969 	return 0;
3970 }
3971 EXPORT_SYMBOL(dev_loopback_xmit);
3972 
3973 #ifdef CONFIG_NET_EGRESS
3974 static struct netdev_queue *
3975 netdev_tx_queue_mapping(struct net_device *dev, struct sk_buff *skb)
3976 {
3977 	int qm = skb_get_queue_mapping(skb);
3978 
3979 	return netdev_get_tx_queue(dev, netdev_cap_txqueue(dev, qm));
3980 }
3981 
3982 #ifndef CONFIG_PREEMPT_RT
3983 static bool netdev_xmit_txqueue_skipped(void)
3984 {
3985 	return __this_cpu_read(softnet_data.xmit.skip_txqueue);
3986 }
3987 
3988 void netdev_xmit_skip_txqueue(bool skip)
3989 {
3990 	__this_cpu_write(softnet_data.xmit.skip_txqueue, skip);
3991 }
3992 EXPORT_SYMBOL_GPL(netdev_xmit_skip_txqueue);
3993 
3994 #else
3995 static bool netdev_xmit_txqueue_skipped(void)
3996 {
3997 	return current->net_xmit.skip_txqueue;
3998 }
3999 
4000 void netdev_xmit_skip_txqueue(bool skip)
4001 {
4002 	current->net_xmit.skip_txqueue = skip;
4003 }
4004 EXPORT_SYMBOL_GPL(netdev_xmit_skip_txqueue);
4005 #endif
4006 #endif /* CONFIG_NET_EGRESS */
4007 
4008 #ifdef CONFIG_NET_XGRESS
4009 static int tc_run(struct tcx_entry *entry, struct sk_buff *skb,
4010 		  enum skb_drop_reason *drop_reason)
4011 {
4012 	int ret = TC_ACT_UNSPEC;
4013 #ifdef CONFIG_NET_CLS_ACT
4014 	struct mini_Qdisc *miniq = rcu_dereference_bh(entry->miniq);
4015 	struct tcf_result res;
4016 
4017 	if (!miniq)
4018 		return ret;
4019 
4020 	if (static_branch_unlikely(&tcf_bypass_check_needed_key)) {
4021 		if (tcf_block_bypass_sw(miniq->block))
4022 			return ret;
4023 	}
4024 
4025 	tc_skb_cb(skb)->mru = 0;
4026 	tc_skb_cb(skb)->post_ct = false;
4027 	tcf_set_drop_reason(skb, *drop_reason);
4028 
4029 	mini_qdisc_bstats_cpu_update(miniq, skb);
4030 	ret = tcf_classify(skb, miniq->block, miniq->filter_list, &res, false);
4031 	/* Only tcf related quirks below. */
4032 	switch (ret) {
4033 	case TC_ACT_SHOT:
4034 		*drop_reason = tcf_get_drop_reason(skb);
4035 		mini_qdisc_qstats_cpu_drop(miniq);
4036 		break;
4037 	case TC_ACT_OK:
4038 	case TC_ACT_RECLASSIFY:
4039 		skb->tc_index = TC_H_MIN(res.classid);
4040 		break;
4041 	}
4042 #endif /* CONFIG_NET_CLS_ACT */
4043 	return ret;
4044 }
4045 
4046 static DEFINE_STATIC_KEY_FALSE(tcx_needed_key);
4047 
4048 void tcx_inc(void)
4049 {
4050 	static_branch_inc(&tcx_needed_key);
4051 }
4052 
4053 void tcx_dec(void)
4054 {
4055 	static_branch_dec(&tcx_needed_key);
4056 }
4057 
4058 static __always_inline enum tcx_action_base
4059 tcx_run(const struct bpf_mprog_entry *entry, struct sk_buff *skb,
4060 	const bool needs_mac)
4061 {
4062 	const struct bpf_mprog_fp *fp;
4063 	const struct bpf_prog *prog;
4064 	int ret = TCX_NEXT;
4065 
4066 	if (needs_mac)
4067 		__skb_push(skb, skb->mac_len);
4068 	bpf_mprog_foreach_prog(entry, fp, prog) {
4069 		bpf_compute_data_pointers(skb);
4070 		ret = bpf_prog_run(prog, skb);
4071 		if (ret != TCX_NEXT)
4072 			break;
4073 	}
4074 	if (needs_mac)
4075 		__skb_pull(skb, skb->mac_len);
4076 	return tcx_action_code(skb, ret);
4077 }
4078 
4079 static __always_inline struct sk_buff *
4080 sch_handle_ingress(struct sk_buff *skb, struct packet_type **pt_prev, int *ret,
4081 		   struct net_device *orig_dev, bool *another)
4082 {
4083 	struct bpf_mprog_entry *entry = rcu_dereference_bh(skb->dev->tcx_ingress);
4084 	enum skb_drop_reason drop_reason = SKB_DROP_REASON_TC_INGRESS;
4085 	struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx;
4086 	int sch_ret;
4087 
4088 	if (!entry)
4089 		return skb;
4090 
4091 	bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx);
4092 	if (*pt_prev) {
4093 		*ret = deliver_skb(skb, *pt_prev, orig_dev);
4094 		*pt_prev = NULL;
4095 	}
4096 
4097 	qdisc_skb_cb(skb)->pkt_len = skb->len;
4098 	tcx_set_ingress(skb, true);
4099 
4100 	if (static_branch_unlikely(&tcx_needed_key)) {
4101 		sch_ret = tcx_run(entry, skb, true);
4102 		if (sch_ret != TC_ACT_UNSPEC)
4103 			goto ingress_verdict;
4104 	}
4105 	sch_ret = tc_run(tcx_entry(entry), skb, &drop_reason);
4106 ingress_verdict:
4107 	switch (sch_ret) {
4108 	case TC_ACT_REDIRECT:
4109 		/* skb_mac_header check was done by BPF, so we can safely
4110 		 * push the L2 header back before redirecting to another
4111 		 * netdev.
4112 		 */
4113 		__skb_push(skb, skb->mac_len);
4114 		if (skb_do_redirect(skb) == -EAGAIN) {
4115 			__skb_pull(skb, skb->mac_len);
4116 			*another = true;
4117 			break;
4118 		}
4119 		*ret = NET_RX_SUCCESS;
4120 		bpf_net_ctx_clear(bpf_net_ctx);
4121 		return NULL;
4122 	case TC_ACT_SHOT:
4123 		kfree_skb_reason(skb, drop_reason);
4124 		*ret = NET_RX_DROP;
4125 		bpf_net_ctx_clear(bpf_net_ctx);
4126 		return NULL;
4127 	/* used by tc_run */
4128 	case TC_ACT_STOLEN:
4129 	case TC_ACT_QUEUED:
4130 	case TC_ACT_TRAP:
4131 		consume_skb(skb);
4132 		fallthrough;
4133 	case TC_ACT_CONSUMED:
4134 		*ret = NET_RX_SUCCESS;
4135 		bpf_net_ctx_clear(bpf_net_ctx);
4136 		return NULL;
4137 	}
4138 	bpf_net_ctx_clear(bpf_net_ctx);
4139 
4140 	return skb;
4141 }
4142 
4143 static __always_inline struct sk_buff *
4144 sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev)
4145 {
4146 	struct bpf_mprog_entry *entry = rcu_dereference_bh(dev->tcx_egress);
4147 	enum skb_drop_reason drop_reason = SKB_DROP_REASON_TC_EGRESS;
4148 	struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx;
4149 	int sch_ret;
4150 
4151 	if (!entry)
4152 		return skb;
4153 
4154 	bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx);
4155 
4156 	/* qdisc_skb_cb(skb)->pkt_len & tcx_set_ingress() was
4157 	 * already set by the caller.
4158 	 */
4159 	if (static_branch_unlikely(&tcx_needed_key)) {
4160 		sch_ret = tcx_run(entry, skb, false);
4161 		if (sch_ret != TC_ACT_UNSPEC)
4162 			goto egress_verdict;
4163 	}
4164 	sch_ret = tc_run(tcx_entry(entry), skb, &drop_reason);
4165 egress_verdict:
4166 	switch (sch_ret) {
4167 	case TC_ACT_REDIRECT:
4168 		/* No need to push/pop skb's mac_header here on egress! */
4169 		skb_do_redirect(skb);
4170 		*ret = NET_XMIT_SUCCESS;
4171 		bpf_net_ctx_clear(bpf_net_ctx);
4172 		return NULL;
4173 	case TC_ACT_SHOT:
4174 		kfree_skb_reason(skb, drop_reason);
4175 		*ret = NET_XMIT_DROP;
4176 		bpf_net_ctx_clear(bpf_net_ctx);
4177 		return NULL;
4178 	/* used by tc_run */
4179 	case TC_ACT_STOLEN:
4180 	case TC_ACT_QUEUED:
4181 	case TC_ACT_TRAP:
4182 		consume_skb(skb);
4183 		fallthrough;
4184 	case TC_ACT_CONSUMED:
4185 		*ret = NET_XMIT_SUCCESS;
4186 		bpf_net_ctx_clear(bpf_net_ctx);
4187 		return NULL;
4188 	}
4189 	bpf_net_ctx_clear(bpf_net_ctx);
4190 
4191 	return skb;
4192 }
4193 #else
4194 static __always_inline struct sk_buff *
4195 sch_handle_ingress(struct sk_buff *skb, struct packet_type **pt_prev, int *ret,
4196 		   struct net_device *orig_dev, bool *another)
4197 {
4198 	return skb;
4199 }
4200 
4201 static __always_inline struct sk_buff *
4202 sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev)
4203 {
4204 	return skb;
4205 }
4206 #endif /* CONFIG_NET_XGRESS */
4207 
4208 #ifdef CONFIG_XPS
4209 static int __get_xps_queue_idx(struct net_device *dev, struct sk_buff *skb,
4210 			       struct xps_dev_maps *dev_maps, unsigned int tci)
4211 {
4212 	int tc = netdev_get_prio_tc_map(dev, skb->priority);
4213 	struct xps_map *map;
4214 	int queue_index = -1;
4215 
4216 	if (tc >= dev_maps->num_tc || tci >= dev_maps->nr_ids)
4217 		return queue_index;
4218 
4219 	tci *= dev_maps->num_tc;
4220 	tci += tc;
4221 
4222 	map = rcu_dereference(dev_maps->attr_map[tci]);
4223 	if (map) {
4224 		if (map->len == 1)
4225 			queue_index = map->queues[0];
4226 		else
4227 			queue_index = map->queues[reciprocal_scale(
4228 						skb_get_hash(skb), map->len)];
4229 		if (unlikely(queue_index >= dev->real_num_tx_queues))
4230 			queue_index = -1;
4231 	}
4232 	return queue_index;
4233 }
4234 #endif
4235 
4236 static int get_xps_queue(struct net_device *dev, struct net_device *sb_dev,
4237 			 struct sk_buff *skb)
4238 {
4239 #ifdef CONFIG_XPS
4240 	struct xps_dev_maps *dev_maps;
4241 	struct sock *sk = skb->sk;
4242 	int queue_index = -1;
4243 
4244 	if (!static_key_false(&xps_needed))
4245 		return -1;
4246 
4247 	rcu_read_lock();
4248 	if (!static_key_false(&xps_rxqs_needed))
4249 		goto get_cpus_map;
4250 
4251 	dev_maps = rcu_dereference(sb_dev->xps_maps[XPS_RXQS]);
4252 	if (dev_maps) {
4253 		int tci = sk_rx_queue_get(sk);
4254 
4255 		if (tci >= 0)
4256 			queue_index = __get_xps_queue_idx(dev, skb, dev_maps,
4257 							  tci);
4258 	}
4259 
4260 get_cpus_map:
4261 	if (queue_index < 0) {
4262 		dev_maps = rcu_dereference(sb_dev->xps_maps[XPS_CPUS]);
4263 		if (dev_maps) {
4264 			unsigned int tci = skb->sender_cpu - 1;
4265 
4266 			queue_index = __get_xps_queue_idx(dev, skb, dev_maps,
4267 							  tci);
4268 		}
4269 	}
4270 	rcu_read_unlock();
4271 
4272 	return queue_index;
4273 #else
4274 	return -1;
4275 #endif
4276 }
4277 
4278 u16 dev_pick_tx_zero(struct net_device *dev, struct sk_buff *skb,
4279 		     struct net_device *sb_dev)
4280 {
4281 	return 0;
4282 }
4283 EXPORT_SYMBOL(dev_pick_tx_zero);
4284 
4285 u16 netdev_pick_tx(struct net_device *dev, struct sk_buff *skb,
4286 		     struct net_device *sb_dev)
4287 {
4288 	struct sock *sk = skb->sk;
4289 	int queue_index = sk_tx_queue_get(sk);
4290 
4291 	sb_dev = sb_dev ? : dev;
4292 
4293 	if (queue_index < 0 || skb->ooo_okay ||
4294 	    queue_index >= dev->real_num_tx_queues) {
4295 		int new_index = get_xps_queue(dev, sb_dev, skb);
4296 
4297 		if (new_index < 0)
4298 			new_index = skb_tx_hash(dev, sb_dev, skb);
4299 
4300 		if (queue_index != new_index && sk &&
4301 		    sk_fullsock(sk) &&
4302 		    rcu_access_pointer(sk->sk_dst_cache))
4303 			sk_tx_queue_set(sk, new_index);
4304 
4305 		queue_index = new_index;
4306 	}
4307 
4308 	return queue_index;
4309 }
4310 EXPORT_SYMBOL(netdev_pick_tx);
4311 
4312 struct netdev_queue *netdev_core_pick_tx(struct net_device *dev,
4313 					 struct sk_buff *skb,
4314 					 struct net_device *sb_dev)
4315 {
4316 	int queue_index = 0;
4317 
4318 #ifdef CONFIG_XPS
4319 	u32 sender_cpu = skb->sender_cpu - 1;
4320 
4321 	if (sender_cpu >= (u32)NR_CPUS)
4322 		skb->sender_cpu = raw_smp_processor_id() + 1;
4323 #endif
4324 
4325 	if (dev->real_num_tx_queues != 1) {
4326 		const struct net_device_ops *ops = dev->netdev_ops;
4327 
4328 		if (ops->ndo_select_queue)
4329 			queue_index = ops->ndo_select_queue(dev, skb, sb_dev);
4330 		else
4331 			queue_index = netdev_pick_tx(dev, skb, sb_dev);
4332 
4333 		queue_index = netdev_cap_txqueue(dev, queue_index);
4334 	}
4335 
4336 	skb_set_queue_mapping(skb, queue_index);
4337 	return netdev_get_tx_queue(dev, queue_index);
4338 }
4339 
4340 /**
4341  * __dev_queue_xmit() - transmit a buffer
4342  * @skb:	buffer to transmit
4343  * @sb_dev:	suboordinate device used for L2 forwarding offload
4344  *
4345  * Queue a buffer for transmission to a network device. The caller must
4346  * have set the device and priority and built the buffer before calling
4347  * this function. The function can be called from an interrupt.
4348  *
4349  * When calling this method, interrupts MUST be enabled. This is because
4350  * the BH enable code must have IRQs enabled so that it will not deadlock.
4351  *
4352  * Regardless of the return value, the skb is consumed, so it is currently
4353  * difficult to retry a send to this method. (You can bump the ref count
4354  * before sending to hold a reference for retry if you are careful.)
4355  *
4356  * Return:
4357  * * 0				- buffer successfully transmitted
4358  * * positive qdisc return code	- NET_XMIT_DROP etc.
4359  * * negative errno		- other errors
4360  */
4361 int __dev_queue_xmit(struct sk_buff *skb, struct net_device *sb_dev)
4362 {
4363 	struct net_device *dev = skb->dev;
4364 	struct netdev_queue *txq = NULL;
4365 	struct Qdisc *q;
4366 	int rc = -ENOMEM;
4367 	bool again = false;
4368 
4369 	skb_reset_mac_header(skb);
4370 	skb_assert_len(skb);
4371 
4372 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_SCHED_TSTAMP))
4373 		__skb_tstamp_tx(skb, NULL, NULL, skb->sk, SCM_TSTAMP_SCHED);
4374 
4375 	/* Disable soft irqs for various locks below. Also
4376 	 * stops preemption for RCU.
4377 	 */
4378 	rcu_read_lock_bh();
4379 
4380 	skb_update_prio(skb);
4381 
4382 	qdisc_pkt_len_init(skb);
4383 	tcx_set_ingress(skb, false);
4384 #ifdef CONFIG_NET_EGRESS
4385 	if (static_branch_unlikely(&egress_needed_key)) {
4386 		if (nf_hook_egress_active()) {
4387 			skb = nf_hook_egress(skb, &rc, dev);
4388 			if (!skb)
4389 				goto out;
4390 		}
4391 
4392 		netdev_xmit_skip_txqueue(false);
4393 
4394 		nf_skip_egress(skb, true);
4395 		skb = sch_handle_egress(skb, &rc, dev);
4396 		if (!skb)
4397 			goto out;
4398 		nf_skip_egress(skb, false);
4399 
4400 		if (netdev_xmit_txqueue_skipped())
4401 			txq = netdev_tx_queue_mapping(dev, skb);
4402 	}
4403 #endif
4404 	/* If device/qdisc don't need skb->dst, release it right now while
4405 	 * its hot in this cpu cache.
4406 	 */
4407 	if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
4408 		skb_dst_drop(skb);
4409 	else
4410 		skb_dst_force(skb);
4411 
4412 	if (!txq)
4413 		txq = netdev_core_pick_tx(dev, skb, sb_dev);
4414 
4415 	q = rcu_dereference_bh(txq->qdisc);
4416 
4417 	trace_net_dev_queue(skb);
4418 	if (q->enqueue) {
4419 		rc = __dev_xmit_skb(skb, q, dev, txq);
4420 		goto out;
4421 	}
4422 
4423 	/* The device has no queue. Common case for software devices:
4424 	 * loopback, all the sorts of tunnels...
4425 
4426 	 * Really, it is unlikely that netif_tx_lock protection is necessary
4427 	 * here.  (f.e. loopback and IP tunnels are clean ignoring statistics
4428 	 * counters.)
4429 	 * However, it is possible, that they rely on protection
4430 	 * made by us here.
4431 
4432 	 * Check this and shot the lock. It is not prone from deadlocks.
4433 	 *Either shot noqueue qdisc, it is even simpler 8)
4434 	 */
4435 	if (dev->flags & IFF_UP) {
4436 		int cpu = smp_processor_id(); /* ok because BHs are off */
4437 
4438 		/* Other cpus might concurrently change txq->xmit_lock_owner
4439 		 * to -1 or to their cpu id, but not to our id.
4440 		 */
4441 		if (READ_ONCE(txq->xmit_lock_owner) != cpu) {
4442 			if (dev_xmit_recursion())
4443 				goto recursion_alert;
4444 
4445 			skb = validate_xmit_skb(skb, dev, &again);
4446 			if (!skb)
4447 				goto out;
4448 
4449 			HARD_TX_LOCK(dev, txq, cpu);
4450 
4451 			if (!netif_xmit_stopped(txq)) {
4452 				dev_xmit_recursion_inc();
4453 				skb = dev_hard_start_xmit(skb, dev, txq, &rc);
4454 				dev_xmit_recursion_dec();
4455 				if (dev_xmit_complete(rc)) {
4456 					HARD_TX_UNLOCK(dev, txq);
4457 					goto out;
4458 				}
4459 			}
4460 			HARD_TX_UNLOCK(dev, txq);
4461 			net_crit_ratelimited("Virtual device %s asks to queue packet!\n",
4462 					     dev->name);
4463 		} else {
4464 			/* Recursion is detected! It is possible,
4465 			 * unfortunately
4466 			 */
4467 recursion_alert:
4468 			net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n",
4469 					     dev->name);
4470 		}
4471 	}
4472 
4473 	rc = -ENETDOWN;
4474 	rcu_read_unlock_bh();
4475 
4476 	dev_core_stats_tx_dropped_inc(dev);
4477 	kfree_skb_list(skb);
4478 	return rc;
4479 out:
4480 	rcu_read_unlock_bh();
4481 	return rc;
4482 }
4483 EXPORT_SYMBOL(__dev_queue_xmit);
4484 
4485 int __dev_direct_xmit(struct sk_buff *skb, u16 queue_id)
4486 {
4487 	struct net_device *dev = skb->dev;
4488 	struct sk_buff *orig_skb = skb;
4489 	struct netdev_queue *txq;
4490 	int ret = NETDEV_TX_BUSY;
4491 	bool again = false;
4492 
4493 	if (unlikely(!netif_running(dev) ||
4494 		     !netif_carrier_ok(dev)))
4495 		goto drop;
4496 
4497 	skb = validate_xmit_skb_list(skb, dev, &again);
4498 	if (skb != orig_skb)
4499 		goto drop;
4500 
4501 	skb_set_queue_mapping(skb, queue_id);
4502 	txq = skb_get_tx_queue(dev, skb);
4503 
4504 	local_bh_disable();
4505 
4506 	dev_xmit_recursion_inc();
4507 	HARD_TX_LOCK(dev, txq, smp_processor_id());
4508 	if (!netif_xmit_frozen_or_drv_stopped(txq))
4509 		ret = netdev_start_xmit(skb, dev, txq, false);
4510 	HARD_TX_UNLOCK(dev, txq);
4511 	dev_xmit_recursion_dec();
4512 
4513 	local_bh_enable();
4514 	return ret;
4515 drop:
4516 	dev_core_stats_tx_dropped_inc(dev);
4517 	kfree_skb_list(skb);
4518 	return NET_XMIT_DROP;
4519 }
4520 EXPORT_SYMBOL(__dev_direct_xmit);
4521 
4522 /*************************************************************************
4523  *			Receiver routines
4524  *************************************************************************/
4525 static DEFINE_PER_CPU(struct task_struct *, backlog_napi);
4526 
4527 int weight_p __read_mostly = 64;           /* old backlog weight */
4528 int dev_weight_rx_bias __read_mostly = 1;  /* bias for backlog weight */
4529 int dev_weight_tx_bias __read_mostly = 1;  /* bias for output_queue quota */
4530 
4531 /* Called with irq disabled */
4532 static inline void ____napi_schedule(struct softnet_data *sd,
4533 				     struct napi_struct *napi)
4534 {
4535 	struct task_struct *thread;
4536 
4537 	lockdep_assert_irqs_disabled();
4538 
4539 	if (test_bit(NAPI_STATE_THREADED, &napi->state)) {
4540 		/* Paired with smp_mb__before_atomic() in
4541 		 * napi_enable()/dev_set_threaded().
4542 		 * Use READ_ONCE() to guarantee a complete
4543 		 * read on napi->thread. Only call
4544 		 * wake_up_process() when it's not NULL.
4545 		 */
4546 		thread = READ_ONCE(napi->thread);
4547 		if (thread) {
4548 			if (use_backlog_threads() && thread == raw_cpu_read(backlog_napi))
4549 				goto use_local_napi;
4550 
4551 			set_bit(NAPI_STATE_SCHED_THREADED, &napi->state);
4552 			wake_up_process(thread);
4553 			return;
4554 		}
4555 	}
4556 
4557 use_local_napi:
4558 	list_add_tail(&napi->poll_list, &sd->poll_list);
4559 	WRITE_ONCE(napi->list_owner, smp_processor_id());
4560 	/* If not called from net_rx_action()
4561 	 * we have to raise NET_RX_SOFTIRQ.
4562 	 */
4563 	if (!sd->in_net_rx_action)
4564 		__raise_softirq_irqoff(NET_RX_SOFTIRQ);
4565 }
4566 
4567 #ifdef CONFIG_RPS
4568 
4569 struct static_key_false rps_needed __read_mostly;
4570 EXPORT_SYMBOL(rps_needed);
4571 struct static_key_false rfs_needed __read_mostly;
4572 EXPORT_SYMBOL(rfs_needed);
4573 
4574 static struct rps_dev_flow *
4575 set_rps_cpu(struct net_device *dev, struct sk_buff *skb,
4576 	    struct rps_dev_flow *rflow, u16 next_cpu)
4577 {
4578 	if (next_cpu < nr_cpu_ids) {
4579 		u32 head;
4580 #ifdef CONFIG_RFS_ACCEL
4581 		struct netdev_rx_queue *rxqueue;
4582 		struct rps_dev_flow_table *flow_table;
4583 		struct rps_dev_flow *old_rflow;
4584 		u16 rxq_index;
4585 		u32 flow_id;
4586 		int rc;
4587 
4588 		/* Should we steer this flow to a different hardware queue? */
4589 		if (!skb_rx_queue_recorded(skb) || !dev->rx_cpu_rmap ||
4590 		    !(dev->features & NETIF_F_NTUPLE))
4591 			goto out;
4592 		rxq_index = cpu_rmap_lookup_index(dev->rx_cpu_rmap, next_cpu);
4593 		if (rxq_index == skb_get_rx_queue(skb))
4594 			goto out;
4595 
4596 		rxqueue = dev->_rx + rxq_index;
4597 		flow_table = rcu_dereference(rxqueue->rps_flow_table);
4598 		if (!flow_table)
4599 			goto out;
4600 		flow_id = skb_get_hash(skb) & flow_table->mask;
4601 		rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb,
4602 							rxq_index, flow_id);
4603 		if (rc < 0)
4604 			goto out;
4605 		old_rflow = rflow;
4606 		rflow = &flow_table->flows[flow_id];
4607 		WRITE_ONCE(rflow->filter, rc);
4608 		if (old_rflow->filter == rc)
4609 			WRITE_ONCE(old_rflow->filter, RPS_NO_FILTER);
4610 	out:
4611 #endif
4612 		head = READ_ONCE(per_cpu(softnet_data, next_cpu).input_queue_head);
4613 		rps_input_queue_tail_save(&rflow->last_qtail, head);
4614 	}
4615 
4616 	WRITE_ONCE(rflow->cpu, next_cpu);
4617 	return rflow;
4618 }
4619 
4620 /*
4621  * get_rps_cpu is called from netif_receive_skb and returns the target
4622  * CPU from the RPS map of the receiving queue for a given skb.
4623  * rcu_read_lock must be held on entry.
4624  */
4625 static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb,
4626 		       struct rps_dev_flow **rflowp)
4627 {
4628 	const struct rps_sock_flow_table *sock_flow_table;
4629 	struct netdev_rx_queue *rxqueue = dev->_rx;
4630 	struct rps_dev_flow_table *flow_table;
4631 	struct rps_map *map;
4632 	int cpu = -1;
4633 	u32 tcpu;
4634 	u32 hash;
4635 
4636 	if (skb_rx_queue_recorded(skb)) {
4637 		u16 index = skb_get_rx_queue(skb);
4638 
4639 		if (unlikely(index >= dev->real_num_rx_queues)) {
4640 			WARN_ONCE(dev->real_num_rx_queues > 1,
4641 				  "%s received packet on queue %u, but number "
4642 				  "of RX queues is %u\n",
4643 				  dev->name, index, dev->real_num_rx_queues);
4644 			goto done;
4645 		}
4646 		rxqueue += index;
4647 	}
4648 
4649 	/* Avoid computing hash if RFS/RPS is not active for this rxqueue */
4650 
4651 	flow_table = rcu_dereference(rxqueue->rps_flow_table);
4652 	map = rcu_dereference(rxqueue->rps_map);
4653 	if (!flow_table && !map)
4654 		goto done;
4655 
4656 	skb_reset_network_header(skb);
4657 	hash = skb_get_hash(skb);
4658 	if (!hash)
4659 		goto done;
4660 
4661 	sock_flow_table = rcu_dereference(net_hotdata.rps_sock_flow_table);
4662 	if (flow_table && sock_flow_table) {
4663 		struct rps_dev_flow *rflow;
4664 		u32 next_cpu;
4665 		u32 ident;
4666 
4667 		/* First check into global flow table if there is a match.
4668 		 * This READ_ONCE() pairs with WRITE_ONCE() from rps_record_sock_flow().
4669 		 */
4670 		ident = READ_ONCE(sock_flow_table->ents[hash & sock_flow_table->mask]);
4671 		if ((ident ^ hash) & ~net_hotdata.rps_cpu_mask)
4672 			goto try_rps;
4673 
4674 		next_cpu = ident & net_hotdata.rps_cpu_mask;
4675 
4676 		/* OK, now we know there is a match,
4677 		 * we can look at the local (per receive queue) flow table
4678 		 */
4679 		rflow = &flow_table->flows[hash & flow_table->mask];
4680 		tcpu = rflow->cpu;
4681 
4682 		/*
4683 		 * If the desired CPU (where last recvmsg was done) is
4684 		 * different from current CPU (one in the rx-queue flow
4685 		 * table entry), switch if one of the following holds:
4686 		 *   - Current CPU is unset (>= nr_cpu_ids).
4687 		 *   - Current CPU is offline.
4688 		 *   - The current CPU's queue tail has advanced beyond the
4689 		 *     last packet that was enqueued using this table entry.
4690 		 *     This guarantees that all previous packets for the flow
4691 		 *     have been dequeued, thus preserving in order delivery.
4692 		 */
4693 		if (unlikely(tcpu != next_cpu) &&
4694 		    (tcpu >= nr_cpu_ids || !cpu_online(tcpu) ||
4695 		     ((int)(READ_ONCE(per_cpu(softnet_data, tcpu).input_queue_head) -
4696 		      rflow->last_qtail)) >= 0)) {
4697 			tcpu = next_cpu;
4698 			rflow = set_rps_cpu(dev, skb, rflow, next_cpu);
4699 		}
4700 
4701 		if (tcpu < nr_cpu_ids && cpu_online(tcpu)) {
4702 			*rflowp = rflow;
4703 			cpu = tcpu;
4704 			goto done;
4705 		}
4706 	}
4707 
4708 try_rps:
4709 
4710 	if (map) {
4711 		tcpu = map->cpus[reciprocal_scale(hash, map->len)];
4712 		if (cpu_online(tcpu)) {
4713 			cpu = tcpu;
4714 			goto done;
4715 		}
4716 	}
4717 
4718 done:
4719 	return cpu;
4720 }
4721 
4722 #ifdef CONFIG_RFS_ACCEL
4723 
4724 /**
4725  * rps_may_expire_flow - check whether an RFS hardware filter may be removed
4726  * @dev: Device on which the filter was set
4727  * @rxq_index: RX queue index
4728  * @flow_id: Flow ID passed to ndo_rx_flow_steer()
4729  * @filter_id: Filter ID returned by ndo_rx_flow_steer()
4730  *
4731  * Drivers that implement ndo_rx_flow_steer() should periodically call
4732  * this function for each installed filter and remove the filters for
4733  * which it returns %true.
4734  */
4735 bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index,
4736 			 u32 flow_id, u16 filter_id)
4737 {
4738 	struct netdev_rx_queue *rxqueue = dev->_rx + rxq_index;
4739 	struct rps_dev_flow_table *flow_table;
4740 	struct rps_dev_flow *rflow;
4741 	bool expire = true;
4742 	unsigned int cpu;
4743 
4744 	rcu_read_lock();
4745 	flow_table = rcu_dereference(rxqueue->rps_flow_table);
4746 	if (flow_table && flow_id <= flow_table->mask) {
4747 		rflow = &flow_table->flows[flow_id];
4748 		cpu = READ_ONCE(rflow->cpu);
4749 		if (READ_ONCE(rflow->filter) == filter_id && cpu < nr_cpu_ids &&
4750 		    ((int)(READ_ONCE(per_cpu(softnet_data, cpu).input_queue_head) -
4751 			   READ_ONCE(rflow->last_qtail)) <
4752 		     (int)(10 * flow_table->mask)))
4753 			expire = false;
4754 	}
4755 	rcu_read_unlock();
4756 	return expire;
4757 }
4758 EXPORT_SYMBOL(rps_may_expire_flow);
4759 
4760 #endif /* CONFIG_RFS_ACCEL */
4761 
4762 /* Called from hardirq (IPI) context */
4763 static void rps_trigger_softirq(void *data)
4764 {
4765 	struct softnet_data *sd = data;
4766 
4767 	____napi_schedule(sd, &sd->backlog);
4768 	sd->received_rps++;
4769 }
4770 
4771 #endif /* CONFIG_RPS */
4772 
4773 /* Called from hardirq (IPI) context */
4774 static void trigger_rx_softirq(void *data)
4775 {
4776 	struct softnet_data *sd = data;
4777 
4778 	__raise_softirq_irqoff(NET_RX_SOFTIRQ);
4779 	smp_store_release(&sd->defer_ipi_scheduled, 0);
4780 }
4781 
4782 /*
4783  * After we queued a packet into sd->input_pkt_queue,
4784  * we need to make sure this queue is serviced soon.
4785  *
4786  * - If this is another cpu queue, link it to our rps_ipi_list,
4787  *   and make sure we will process rps_ipi_list from net_rx_action().
4788  *
4789  * - If this is our own queue, NAPI schedule our backlog.
4790  *   Note that this also raises NET_RX_SOFTIRQ.
4791  */
4792 static void napi_schedule_rps(struct softnet_data *sd)
4793 {
4794 	struct softnet_data *mysd = this_cpu_ptr(&softnet_data);
4795 
4796 #ifdef CONFIG_RPS
4797 	if (sd != mysd) {
4798 		if (use_backlog_threads()) {
4799 			__napi_schedule_irqoff(&sd->backlog);
4800 			return;
4801 		}
4802 
4803 		sd->rps_ipi_next = mysd->rps_ipi_list;
4804 		mysd->rps_ipi_list = sd;
4805 
4806 		/* If not called from net_rx_action() or napi_threaded_poll()
4807 		 * we have to raise NET_RX_SOFTIRQ.
4808 		 */
4809 		if (!mysd->in_net_rx_action && !mysd->in_napi_threaded_poll)
4810 			__raise_softirq_irqoff(NET_RX_SOFTIRQ);
4811 		return;
4812 	}
4813 #endif /* CONFIG_RPS */
4814 	__napi_schedule_irqoff(&mysd->backlog);
4815 }
4816 
4817 void kick_defer_list_purge(struct softnet_data *sd, unsigned int cpu)
4818 {
4819 	unsigned long flags;
4820 
4821 	if (use_backlog_threads()) {
4822 		backlog_lock_irq_save(sd, &flags);
4823 
4824 		if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state))
4825 			__napi_schedule_irqoff(&sd->backlog);
4826 
4827 		backlog_unlock_irq_restore(sd, &flags);
4828 
4829 	} else if (!cmpxchg(&sd->defer_ipi_scheduled, 0, 1)) {
4830 		smp_call_function_single_async(cpu, &sd->defer_csd);
4831 	}
4832 }
4833 
4834 #ifdef CONFIG_NET_FLOW_LIMIT
4835 int netdev_flow_limit_table_len __read_mostly = (1 << 12);
4836 #endif
4837 
4838 static bool skb_flow_limit(struct sk_buff *skb, unsigned int qlen)
4839 {
4840 #ifdef CONFIG_NET_FLOW_LIMIT
4841 	struct sd_flow_limit *fl;
4842 	struct softnet_data *sd;
4843 	unsigned int old_flow, new_flow;
4844 
4845 	if (qlen < (READ_ONCE(net_hotdata.max_backlog) >> 1))
4846 		return false;
4847 
4848 	sd = this_cpu_ptr(&softnet_data);
4849 
4850 	rcu_read_lock();
4851 	fl = rcu_dereference(sd->flow_limit);
4852 	if (fl) {
4853 		new_flow = skb_get_hash(skb) & (fl->num_buckets - 1);
4854 		old_flow = fl->history[fl->history_head];
4855 		fl->history[fl->history_head] = new_flow;
4856 
4857 		fl->history_head++;
4858 		fl->history_head &= FLOW_LIMIT_HISTORY - 1;
4859 
4860 		if (likely(fl->buckets[old_flow]))
4861 			fl->buckets[old_flow]--;
4862 
4863 		if (++fl->buckets[new_flow] > (FLOW_LIMIT_HISTORY >> 1)) {
4864 			fl->count++;
4865 			rcu_read_unlock();
4866 			return true;
4867 		}
4868 	}
4869 	rcu_read_unlock();
4870 #endif
4871 	return false;
4872 }
4873 
4874 /*
4875  * enqueue_to_backlog is called to queue an skb to a per CPU backlog
4876  * queue (may be a remote CPU queue).
4877  */
4878 static int enqueue_to_backlog(struct sk_buff *skb, int cpu,
4879 			      unsigned int *qtail)
4880 {
4881 	enum skb_drop_reason reason;
4882 	struct softnet_data *sd;
4883 	unsigned long flags;
4884 	unsigned int qlen;
4885 	int max_backlog;
4886 	u32 tail;
4887 
4888 	reason = SKB_DROP_REASON_DEV_READY;
4889 	if (!netif_running(skb->dev))
4890 		goto bad_dev;
4891 
4892 	reason = SKB_DROP_REASON_CPU_BACKLOG;
4893 	sd = &per_cpu(softnet_data, cpu);
4894 
4895 	qlen = skb_queue_len_lockless(&sd->input_pkt_queue);
4896 	max_backlog = READ_ONCE(net_hotdata.max_backlog);
4897 	if (unlikely(qlen > max_backlog))
4898 		goto cpu_backlog_drop;
4899 	backlog_lock_irq_save(sd, &flags);
4900 	qlen = skb_queue_len(&sd->input_pkt_queue);
4901 	if (qlen <= max_backlog && !skb_flow_limit(skb, qlen)) {
4902 		if (!qlen) {
4903 			/* Schedule NAPI for backlog device. We can use
4904 			 * non atomic operation as we own the queue lock.
4905 			 */
4906 			if (!__test_and_set_bit(NAPI_STATE_SCHED,
4907 						&sd->backlog.state))
4908 				napi_schedule_rps(sd);
4909 		}
4910 		__skb_queue_tail(&sd->input_pkt_queue, skb);
4911 		tail = rps_input_queue_tail_incr(sd);
4912 		backlog_unlock_irq_restore(sd, &flags);
4913 
4914 		/* save the tail outside of the critical section */
4915 		rps_input_queue_tail_save(qtail, tail);
4916 		return NET_RX_SUCCESS;
4917 	}
4918 
4919 	backlog_unlock_irq_restore(sd, &flags);
4920 
4921 cpu_backlog_drop:
4922 	atomic_inc(&sd->dropped);
4923 bad_dev:
4924 	dev_core_stats_rx_dropped_inc(skb->dev);
4925 	kfree_skb_reason(skb, reason);
4926 	return NET_RX_DROP;
4927 }
4928 
4929 static struct netdev_rx_queue *netif_get_rxqueue(struct sk_buff *skb)
4930 {
4931 	struct net_device *dev = skb->dev;
4932 	struct netdev_rx_queue *rxqueue;
4933 
4934 	rxqueue = dev->_rx;
4935 
4936 	if (skb_rx_queue_recorded(skb)) {
4937 		u16 index = skb_get_rx_queue(skb);
4938 
4939 		if (unlikely(index >= dev->real_num_rx_queues)) {
4940 			WARN_ONCE(dev->real_num_rx_queues > 1,
4941 				  "%s received packet on queue %u, but number "
4942 				  "of RX queues is %u\n",
4943 				  dev->name, index, dev->real_num_rx_queues);
4944 
4945 			return rxqueue; /* Return first rxqueue */
4946 		}
4947 		rxqueue += index;
4948 	}
4949 	return rxqueue;
4950 }
4951 
4952 u32 bpf_prog_run_generic_xdp(struct sk_buff *skb, struct xdp_buff *xdp,
4953 			     const struct bpf_prog *xdp_prog)
4954 {
4955 	void *orig_data, *orig_data_end, *hard_start;
4956 	struct netdev_rx_queue *rxqueue;
4957 	bool orig_bcast, orig_host;
4958 	u32 mac_len, frame_sz;
4959 	__be16 orig_eth_type;
4960 	struct ethhdr *eth;
4961 	u32 metalen, act;
4962 	int off;
4963 
4964 	/* The XDP program wants to see the packet starting at the MAC
4965 	 * header.
4966 	 */
4967 	mac_len = skb->data - skb_mac_header(skb);
4968 	hard_start = skb->data - skb_headroom(skb);
4969 
4970 	/* SKB "head" area always have tailroom for skb_shared_info */
4971 	frame_sz = (void *)skb_end_pointer(skb) - hard_start;
4972 	frame_sz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4973 
4974 	rxqueue = netif_get_rxqueue(skb);
4975 	xdp_init_buff(xdp, frame_sz, &rxqueue->xdp_rxq);
4976 	xdp_prepare_buff(xdp, hard_start, skb_headroom(skb) - mac_len,
4977 			 skb_headlen(skb) + mac_len, true);
4978 	if (skb_is_nonlinear(skb)) {
4979 		skb_shinfo(skb)->xdp_frags_size = skb->data_len;
4980 		xdp_buff_set_frags_flag(xdp);
4981 	} else {
4982 		xdp_buff_clear_frags_flag(xdp);
4983 	}
4984 
4985 	orig_data_end = xdp->data_end;
4986 	orig_data = xdp->data;
4987 	eth = (struct ethhdr *)xdp->data;
4988 	orig_host = ether_addr_equal_64bits(eth->h_dest, skb->dev->dev_addr);
4989 	orig_bcast = is_multicast_ether_addr_64bits(eth->h_dest);
4990 	orig_eth_type = eth->h_proto;
4991 
4992 	act = bpf_prog_run_xdp(xdp_prog, xdp);
4993 
4994 	/* check if bpf_xdp_adjust_head was used */
4995 	off = xdp->data - orig_data;
4996 	if (off) {
4997 		if (off > 0)
4998 			__skb_pull(skb, off);
4999 		else if (off < 0)
5000 			__skb_push(skb, -off);
5001 
5002 		skb->mac_header += off;
5003 		skb_reset_network_header(skb);
5004 	}
5005 
5006 	/* check if bpf_xdp_adjust_tail was used */
5007 	off = xdp->data_end - orig_data_end;
5008 	if (off != 0) {
5009 		skb_set_tail_pointer(skb, xdp->data_end - xdp->data);
5010 		skb->len += off; /* positive on grow, negative on shrink */
5011 	}
5012 
5013 	/* XDP frag metadata (e.g. nr_frags) are updated in eBPF helpers
5014 	 * (e.g. bpf_xdp_adjust_tail), we need to update data_len here.
5015 	 */
5016 	if (xdp_buff_has_frags(xdp))
5017 		skb->data_len = skb_shinfo(skb)->xdp_frags_size;
5018 	else
5019 		skb->data_len = 0;
5020 
5021 	/* check if XDP changed eth hdr such SKB needs update */
5022 	eth = (struct ethhdr *)xdp->data;
5023 	if ((orig_eth_type != eth->h_proto) ||
5024 	    (orig_host != ether_addr_equal_64bits(eth->h_dest,
5025 						  skb->dev->dev_addr)) ||
5026 	    (orig_bcast != is_multicast_ether_addr_64bits(eth->h_dest))) {
5027 		__skb_push(skb, ETH_HLEN);
5028 		skb->pkt_type = PACKET_HOST;
5029 		skb->protocol = eth_type_trans(skb, skb->dev);
5030 	}
5031 
5032 	/* Redirect/Tx gives L2 packet, code that will reuse skb must __skb_pull
5033 	 * before calling us again on redirect path. We do not call do_redirect
5034 	 * as we leave that up to the caller.
5035 	 *
5036 	 * Caller is responsible for managing lifetime of skb (i.e. calling
5037 	 * kfree_skb in response to actions it cannot handle/XDP_DROP).
5038 	 */
5039 	switch (act) {
5040 	case XDP_REDIRECT:
5041 	case XDP_TX:
5042 		__skb_push(skb, mac_len);
5043 		break;
5044 	case XDP_PASS:
5045 		metalen = xdp->data - xdp->data_meta;
5046 		if (metalen)
5047 			skb_metadata_set(skb, metalen);
5048 		break;
5049 	}
5050 
5051 	return act;
5052 }
5053 
5054 static int
5055 netif_skb_check_for_xdp(struct sk_buff **pskb, const struct bpf_prog *prog)
5056 {
5057 	struct sk_buff *skb = *pskb;
5058 	int err, hroom, troom;
5059 
5060 	if (!skb_cow_data_for_xdp(this_cpu_read(system_page_pool), pskb, prog))
5061 		return 0;
5062 
5063 	/* In case we have to go down the path and also linearize,
5064 	 * then lets do the pskb_expand_head() work just once here.
5065 	 */
5066 	hroom = XDP_PACKET_HEADROOM - skb_headroom(skb);
5067 	troom = skb->tail + skb->data_len - skb->end;
5068 	err = pskb_expand_head(skb,
5069 			       hroom > 0 ? ALIGN(hroom, NET_SKB_PAD) : 0,
5070 			       troom > 0 ? troom + 128 : 0, GFP_ATOMIC);
5071 	if (err)
5072 		return err;
5073 
5074 	return skb_linearize(skb);
5075 }
5076 
5077 static u32 netif_receive_generic_xdp(struct sk_buff **pskb,
5078 				     struct xdp_buff *xdp,
5079 				     const struct bpf_prog *xdp_prog)
5080 {
5081 	struct sk_buff *skb = *pskb;
5082 	u32 mac_len, act = XDP_DROP;
5083 
5084 	/* Reinjected packets coming from act_mirred or similar should
5085 	 * not get XDP generic processing.
5086 	 */
5087 	if (skb_is_redirected(skb))
5088 		return XDP_PASS;
5089 
5090 	/* XDP packets must have sufficient headroom of XDP_PACKET_HEADROOM
5091 	 * bytes. This is the guarantee that also native XDP provides,
5092 	 * thus we need to do it here as well.
5093 	 */
5094 	mac_len = skb->data - skb_mac_header(skb);
5095 	__skb_push(skb, mac_len);
5096 
5097 	if (skb_cloned(skb) || skb_is_nonlinear(skb) ||
5098 	    skb_headroom(skb) < XDP_PACKET_HEADROOM) {
5099 		if (netif_skb_check_for_xdp(pskb, xdp_prog))
5100 			goto do_drop;
5101 	}
5102 
5103 	__skb_pull(*pskb, mac_len);
5104 
5105 	act = bpf_prog_run_generic_xdp(*pskb, xdp, xdp_prog);
5106 	switch (act) {
5107 	case XDP_REDIRECT:
5108 	case XDP_TX:
5109 	case XDP_PASS:
5110 		break;
5111 	default:
5112 		bpf_warn_invalid_xdp_action((*pskb)->dev, xdp_prog, act);
5113 		fallthrough;
5114 	case XDP_ABORTED:
5115 		trace_xdp_exception((*pskb)->dev, xdp_prog, act);
5116 		fallthrough;
5117 	case XDP_DROP:
5118 	do_drop:
5119 		kfree_skb(*pskb);
5120 		break;
5121 	}
5122 
5123 	return act;
5124 }
5125 
5126 /* When doing generic XDP we have to bypass the qdisc layer and the
5127  * network taps in order to match in-driver-XDP behavior. This also means
5128  * that XDP packets are able to starve other packets going through a qdisc,
5129  * and DDOS attacks will be more effective. In-driver-XDP use dedicated TX
5130  * queues, so they do not have this starvation issue.
5131  */
5132 void generic_xdp_tx(struct sk_buff *skb, const struct bpf_prog *xdp_prog)
5133 {
5134 	struct net_device *dev = skb->dev;
5135 	struct netdev_queue *txq;
5136 	bool free_skb = true;
5137 	int cpu, rc;
5138 
5139 	txq = netdev_core_pick_tx(dev, skb, NULL);
5140 	cpu = smp_processor_id();
5141 	HARD_TX_LOCK(dev, txq, cpu);
5142 	if (!netif_xmit_frozen_or_drv_stopped(txq)) {
5143 		rc = netdev_start_xmit(skb, dev, txq, 0);
5144 		if (dev_xmit_complete(rc))
5145 			free_skb = false;
5146 	}
5147 	HARD_TX_UNLOCK(dev, txq);
5148 	if (free_skb) {
5149 		trace_xdp_exception(dev, xdp_prog, XDP_TX);
5150 		dev_core_stats_tx_dropped_inc(dev);
5151 		kfree_skb(skb);
5152 	}
5153 }
5154 
5155 static DEFINE_STATIC_KEY_FALSE(generic_xdp_needed_key);
5156 
5157 int do_xdp_generic(const struct bpf_prog *xdp_prog, struct sk_buff **pskb)
5158 {
5159 	struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx;
5160 
5161 	if (xdp_prog) {
5162 		struct xdp_buff xdp;
5163 		u32 act;
5164 		int err;
5165 
5166 		bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx);
5167 		act = netif_receive_generic_xdp(pskb, &xdp, xdp_prog);
5168 		if (act != XDP_PASS) {
5169 			switch (act) {
5170 			case XDP_REDIRECT:
5171 				err = xdp_do_generic_redirect((*pskb)->dev, *pskb,
5172 							      &xdp, xdp_prog);
5173 				if (err)
5174 					goto out_redir;
5175 				break;
5176 			case XDP_TX:
5177 				generic_xdp_tx(*pskb, xdp_prog);
5178 				break;
5179 			}
5180 			bpf_net_ctx_clear(bpf_net_ctx);
5181 			return XDP_DROP;
5182 		}
5183 		bpf_net_ctx_clear(bpf_net_ctx);
5184 	}
5185 	return XDP_PASS;
5186 out_redir:
5187 	bpf_net_ctx_clear(bpf_net_ctx);
5188 	kfree_skb_reason(*pskb, SKB_DROP_REASON_XDP);
5189 	return XDP_DROP;
5190 }
5191 EXPORT_SYMBOL_GPL(do_xdp_generic);
5192 
5193 static int netif_rx_internal(struct sk_buff *skb)
5194 {
5195 	int ret;
5196 
5197 	net_timestamp_check(READ_ONCE(net_hotdata.tstamp_prequeue), skb);
5198 
5199 	trace_netif_rx(skb);
5200 
5201 #ifdef CONFIG_RPS
5202 	if (static_branch_unlikely(&rps_needed)) {
5203 		struct rps_dev_flow voidflow, *rflow = &voidflow;
5204 		int cpu;
5205 
5206 		rcu_read_lock();
5207 
5208 		cpu = get_rps_cpu(skb->dev, skb, &rflow);
5209 		if (cpu < 0)
5210 			cpu = smp_processor_id();
5211 
5212 		ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
5213 
5214 		rcu_read_unlock();
5215 	} else
5216 #endif
5217 	{
5218 		unsigned int qtail;
5219 
5220 		ret = enqueue_to_backlog(skb, smp_processor_id(), &qtail);
5221 	}
5222 	return ret;
5223 }
5224 
5225 /**
5226  *	__netif_rx	-	Slightly optimized version of netif_rx
5227  *	@skb: buffer to post
5228  *
5229  *	This behaves as netif_rx except that it does not disable bottom halves.
5230  *	As a result this function may only be invoked from the interrupt context
5231  *	(either hard or soft interrupt).
5232  */
5233 int __netif_rx(struct sk_buff *skb)
5234 {
5235 	int ret;
5236 
5237 	lockdep_assert_once(hardirq_count() | softirq_count());
5238 
5239 	trace_netif_rx_entry(skb);
5240 	ret = netif_rx_internal(skb);
5241 	trace_netif_rx_exit(ret);
5242 	return ret;
5243 }
5244 EXPORT_SYMBOL(__netif_rx);
5245 
5246 /**
5247  *	netif_rx	-	post buffer to the network code
5248  *	@skb: buffer to post
5249  *
5250  *	This function receives a packet from a device driver and queues it for
5251  *	the upper (protocol) levels to process via the backlog NAPI device. It
5252  *	always succeeds. The buffer may be dropped during processing for
5253  *	congestion control or by the protocol layers.
5254  *	The network buffer is passed via the backlog NAPI device. Modern NIC
5255  *	driver should use NAPI and GRO.
5256  *	This function can used from interrupt and from process context. The
5257  *	caller from process context must not disable interrupts before invoking
5258  *	this function.
5259  *
5260  *	return values:
5261  *	NET_RX_SUCCESS	(no congestion)
5262  *	NET_RX_DROP     (packet was dropped)
5263  *
5264  */
5265 int netif_rx(struct sk_buff *skb)
5266 {
5267 	bool need_bh_off = !(hardirq_count() | softirq_count());
5268 	int ret;
5269 
5270 	if (need_bh_off)
5271 		local_bh_disable();
5272 	trace_netif_rx_entry(skb);
5273 	ret = netif_rx_internal(skb);
5274 	trace_netif_rx_exit(ret);
5275 	if (need_bh_off)
5276 		local_bh_enable();
5277 	return ret;
5278 }
5279 EXPORT_SYMBOL(netif_rx);
5280 
5281 static __latent_entropy void net_tx_action(void)
5282 {
5283 	struct softnet_data *sd = this_cpu_ptr(&softnet_data);
5284 
5285 	if (sd->completion_queue) {
5286 		struct sk_buff *clist;
5287 
5288 		local_irq_disable();
5289 		clist = sd->completion_queue;
5290 		sd->completion_queue = NULL;
5291 		local_irq_enable();
5292 
5293 		while (clist) {
5294 			struct sk_buff *skb = clist;
5295 
5296 			clist = clist->next;
5297 
5298 			WARN_ON(refcount_read(&skb->users));
5299 			if (likely(get_kfree_skb_cb(skb)->reason == SKB_CONSUMED))
5300 				trace_consume_skb(skb, net_tx_action);
5301 			else
5302 				trace_kfree_skb(skb, net_tx_action,
5303 						get_kfree_skb_cb(skb)->reason, NULL);
5304 
5305 			if (skb->fclone != SKB_FCLONE_UNAVAILABLE)
5306 				__kfree_skb(skb);
5307 			else
5308 				__napi_kfree_skb(skb,
5309 						 get_kfree_skb_cb(skb)->reason);
5310 		}
5311 	}
5312 
5313 	if (sd->output_queue) {
5314 		struct Qdisc *head;
5315 
5316 		local_irq_disable();
5317 		head = sd->output_queue;
5318 		sd->output_queue = NULL;
5319 		sd->output_queue_tailp = &sd->output_queue;
5320 		local_irq_enable();
5321 
5322 		rcu_read_lock();
5323 
5324 		while (head) {
5325 			struct Qdisc *q = head;
5326 			spinlock_t *root_lock = NULL;
5327 
5328 			head = head->next_sched;
5329 
5330 			/* We need to make sure head->next_sched is read
5331 			 * before clearing __QDISC_STATE_SCHED
5332 			 */
5333 			smp_mb__before_atomic();
5334 
5335 			if (!(q->flags & TCQ_F_NOLOCK)) {
5336 				root_lock = qdisc_lock(q);
5337 				spin_lock(root_lock);
5338 			} else if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED,
5339 						     &q->state))) {
5340 				/* There is a synchronize_net() between
5341 				 * STATE_DEACTIVATED flag being set and
5342 				 * qdisc_reset()/some_qdisc_is_busy() in
5343 				 * dev_deactivate(), so we can safely bail out
5344 				 * early here to avoid data race between
5345 				 * qdisc_deactivate() and some_qdisc_is_busy()
5346 				 * for lockless qdisc.
5347 				 */
5348 				clear_bit(__QDISC_STATE_SCHED, &q->state);
5349 				continue;
5350 			}
5351 
5352 			clear_bit(__QDISC_STATE_SCHED, &q->state);
5353 			qdisc_run(q);
5354 			if (root_lock)
5355 				spin_unlock(root_lock);
5356 		}
5357 
5358 		rcu_read_unlock();
5359 	}
5360 
5361 	xfrm_dev_backlog(sd);
5362 }
5363 
5364 #if IS_ENABLED(CONFIG_BRIDGE) && IS_ENABLED(CONFIG_ATM_LANE)
5365 /* This hook is defined here for ATM LANE */
5366 int (*br_fdb_test_addr_hook)(struct net_device *dev,
5367 			     unsigned char *addr) __read_mostly;
5368 EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook);
5369 #endif
5370 
5371 /**
5372  *	netdev_is_rx_handler_busy - check if receive handler is registered
5373  *	@dev: device to check
5374  *
5375  *	Check if a receive handler is already registered for a given device.
5376  *	Return true if there one.
5377  *
5378  *	The caller must hold the rtnl_mutex.
5379  */
5380 bool netdev_is_rx_handler_busy(struct net_device *dev)
5381 {
5382 	ASSERT_RTNL();
5383 	return dev && rtnl_dereference(dev->rx_handler);
5384 }
5385 EXPORT_SYMBOL_GPL(netdev_is_rx_handler_busy);
5386 
5387 /**
5388  *	netdev_rx_handler_register - register receive handler
5389  *	@dev: device to register a handler for
5390  *	@rx_handler: receive handler to register
5391  *	@rx_handler_data: data pointer that is used by rx handler
5392  *
5393  *	Register a receive handler for a device. This handler will then be
5394  *	called from __netif_receive_skb. A negative errno code is returned
5395  *	on a failure.
5396  *
5397  *	The caller must hold the rtnl_mutex.
5398  *
5399  *	For a general description of rx_handler, see enum rx_handler_result.
5400  */
5401 int netdev_rx_handler_register(struct net_device *dev,
5402 			       rx_handler_func_t *rx_handler,
5403 			       void *rx_handler_data)
5404 {
5405 	if (netdev_is_rx_handler_busy(dev))
5406 		return -EBUSY;
5407 
5408 	if (dev->priv_flags & IFF_NO_RX_HANDLER)
5409 		return -EINVAL;
5410 
5411 	/* Note: rx_handler_data must be set before rx_handler */
5412 	rcu_assign_pointer(dev->rx_handler_data, rx_handler_data);
5413 	rcu_assign_pointer(dev->rx_handler, rx_handler);
5414 
5415 	return 0;
5416 }
5417 EXPORT_SYMBOL_GPL(netdev_rx_handler_register);
5418 
5419 /**
5420  *	netdev_rx_handler_unregister - unregister receive handler
5421  *	@dev: device to unregister a handler from
5422  *
5423  *	Unregister a receive handler from a device.
5424  *
5425  *	The caller must hold the rtnl_mutex.
5426  */
5427 void netdev_rx_handler_unregister(struct net_device *dev)
5428 {
5429 
5430 	ASSERT_RTNL();
5431 	RCU_INIT_POINTER(dev->rx_handler, NULL);
5432 	/* a reader seeing a non NULL rx_handler in a rcu_read_lock()
5433 	 * section has a guarantee to see a non NULL rx_handler_data
5434 	 * as well.
5435 	 */
5436 	synchronize_net();
5437 	RCU_INIT_POINTER(dev->rx_handler_data, NULL);
5438 }
5439 EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister);
5440 
5441 /*
5442  * Limit the use of PFMEMALLOC reserves to those protocols that implement
5443  * the special handling of PFMEMALLOC skbs.
5444  */
5445 static bool skb_pfmemalloc_protocol(struct sk_buff *skb)
5446 {
5447 	switch (skb->protocol) {
5448 	case htons(ETH_P_ARP):
5449 	case htons(ETH_P_IP):
5450 	case htons(ETH_P_IPV6):
5451 	case htons(ETH_P_8021Q):
5452 	case htons(ETH_P_8021AD):
5453 		return true;
5454 	default:
5455 		return false;
5456 	}
5457 }
5458 
5459 static inline int nf_ingress(struct sk_buff *skb, struct packet_type **pt_prev,
5460 			     int *ret, struct net_device *orig_dev)
5461 {
5462 	if (nf_hook_ingress_active(skb)) {
5463 		int ingress_retval;
5464 
5465 		if (*pt_prev) {
5466 			*ret = deliver_skb(skb, *pt_prev, orig_dev);
5467 			*pt_prev = NULL;
5468 		}
5469 
5470 		rcu_read_lock();
5471 		ingress_retval = nf_hook_ingress(skb);
5472 		rcu_read_unlock();
5473 		return ingress_retval;
5474 	}
5475 	return 0;
5476 }
5477 
5478 static int __netif_receive_skb_core(struct sk_buff **pskb, bool pfmemalloc,
5479 				    struct packet_type **ppt_prev)
5480 {
5481 	struct packet_type *ptype, *pt_prev;
5482 	rx_handler_func_t *rx_handler;
5483 	struct sk_buff *skb = *pskb;
5484 	struct net_device *orig_dev;
5485 	bool deliver_exact = false;
5486 	int ret = NET_RX_DROP;
5487 	__be16 type;
5488 
5489 	net_timestamp_check(!READ_ONCE(net_hotdata.tstamp_prequeue), skb);
5490 
5491 	trace_netif_receive_skb(skb);
5492 
5493 	orig_dev = skb->dev;
5494 
5495 	skb_reset_network_header(skb);
5496 	if (!skb_transport_header_was_set(skb))
5497 		skb_reset_transport_header(skb);
5498 	skb_reset_mac_len(skb);
5499 
5500 	pt_prev = NULL;
5501 
5502 another_round:
5503 	skb->skb_iif = skb->dev->ifindex;
5504 
5505 	__this_cpu_inc(softnet_data.processed);
5506 
5507 	if (static_branch_unlikely(&generic_xdp_needed_key)) {
5508 		int ret2;
5509 
5510 		migrate_disable();
5511 		ret2 = do_xdp_generic(rcu_dereference(skb->dev->xdp_prog),
5512 				      &skb);
5513 		migrate_enable();
5514 
5515 		if (ret2 != XDP_PASS) {
5516 			ret = NET_RX_DROP;
5517 			goto out;
5518 		}
5519 	}
5520 
5521 	if (eth_type_vlan(skb->protocol)) {
5522 		skb = skb_vlan_untag(skb);
5523 		if (unlikely(!skb))
5524 			goto out;
5525 	}
5526 
5527 	if (skb_skip_tc_classify(skb))
5528 		goto skip_classify;
5529 
5530 	if (pfmemalloc)
5531 		goto skip_taps;
5532 
5533 	list_for_each_entry_rcu(ptype, &net_hotdata.ptype_all, list) {
5534 		if (pt_prev)
5535 			ret = deliver_skb(skb, pt_prev, orig_dev);
5536 		pt_prev = ptype;
5537 	}
5538 
5539 	list_for_each_entry_rcu(ptype, &skb->dev->ptype_all, list) {
5540 		if (pt_prev)
5541 			ret = deliver_skb(skb, pt_prev, orig_dev);
5542 		pt_prev = ptype;
5543 	}
5544 
5545 skip_taps:
5546 #ifdef CONFIG_NET_INGRESS
5547 	if (static_branch_unlikely(&ingress_needed_key)) {
5548 		bool another = false;
5549 
5550 		nf_skip_egress(skb, true);
5551 		skb = sch_handle_ingress(skb, &pt_prev, &ret, orig_dev,
5552 					 &another);
5553 		if (another)
5554 			goto another_round;
5555 		if (!skb)
5556 			goto out;
5557 
5558 		nf_skip_egress(skb, false);
5559 		if (nf_ingress(skb, &pt_prev, &ret, orig_dev) < 0)
5560 			goto out;
5561 	}
5562 #endif
5563 	skb_reset_redirect(skb);
5564 skip_classify:
5565 	if (pfmemalloc && !skb_pfmemalloc_protocol(skb))
5566 		goto drop;
5567 
5568 	if (skb_vlan_tag_present(skb)) {
5569 		if (pt_prev) {
5570 			ret = deliver_skb(skb, pt_prev, orig_dev);
5571 			pt_prev = NULL;
5572 		}
5573 		if (vlan_do_receive(&skb))
5574 			goto another_round;
5575 		else if (unlikely(!skb))
5576 			goto out;
5577 	}
5578 
5579 	rx_handler = rcu_dereference(skb->dev->rx_handler);
5580 	if (rx_handler) {
5581 		if (pt_prev) {
5582 			ret = deliver_skb(skb, pt_prev, orig_dev);
5583 			pt_prev = NULL;
5584 		}
5585 		switch (rx_handler(&skb)) {
5586 		case RX_HANDLER_CONSUMED:
5587 			ret = NET_RX_SUCCESS;
5588 			goto out;
5589 		case RX_HANDLER_ANOTHER:
5590 			goto another_round;
5591 		case RX_HANDLER_EXACT:
5592 			deliver_exact = true;
5593 			break;
5594 		case RX_HANDLER_PASS:
5595 			break;
5596 		default:
5597 			BUG();
5598 		}
5599 	}
5600 
5601 	if (unlikely(skb_vlan_tag_present(skb)) && !netdev_uses_dsa(skb->dev)) {
5602 check_vlan_id:
5603 		if (skb_vlan_tag_get_id(skb)) {
5604 			/* Vlan id is non 0 and vlan_do_receive() above couldn't
5605 			 * find vlan device.
5606 			 */
5607 			skb->pkt_type = PACKET_OTHERHOST;
5608 		} else if (eth_type_vlan(skb->protocol)) {
5609 			/* Outer header is 802.1P with vlan 0, inner header is
5610 			 * 802.1Q or 802.1AD and vlan_do_receive() above could
5611 			 * not find vlan dev for vlan id 0.
5612 			 */
5613 			__vlan_hwaccel_clear_tag(skb);
5614 			skb = skb_vlan_untag(skb);
5615 			if (unlikely(!skb))
5616 				goto out;
5617 			if (vlan_do_receive(&skb))
5618 				/* After stripping off 802.1P header with vlan 0
5619 				 * vlan dev is found for inner header.
5620 				 */
5621 				goto another_round;
5622 			else if (unlikely(!skb))
5623 				goto out;
5624 			else
5625 				/* We have stripped outer 802.1P vlan 0 header.
5626 				 * But could not find vlan dev.
5627 				 * check again for vlan id to set OTHERHOST.
5628 				 */
5629 				goto check_vlan_id;
5630 		}
5631 		/* Note: we might in the future use prio bits
5632 		 * and set skb->priority like in vlan_do_receive()
5633 		 * For the time being, just ignore Priority Code Point
5634 		 */
5635 		__vlan_hwaccel_clear_tag(skb);
5636 	}
5637 
5638 	type = skb->protocol;
5639 
5640 	/* deliver only exact match when indicated */
5641 	if (likely(!deliver_exact)) {
5642 		deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5643 				       &ptype_base[ntohs(type) &
5644 						   PTYPE_HASH_MASK]);
5645 	}
5646 
5647 	deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5648 			       &orig_dev->ptype_specific);
5649 
5650 	if (unlikely(skb->dev != orig_dev)) {
5651 		deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5652 				       &skb->dev->ptype_specific);
5653 	}
5654 
5655 	if (pt_prev) {
5656 		if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
5657 			goto drop;
5658 		*ppt_prev = pt_prev;
5659 	} else {
5660 drop:
5661 		if (!deliver_exact)
5662 			dev_core_stats_rx_dropped_inc(skb->dev);
5663 		else
5664 			dev_core_stats_rx_nohandler_inc(skb->dev);
5665 		kfree_skb_reason(skb, SKB_DROP_REASON_UNHANDLED_PROTO);
5666 		/* Jamal, now you will not able to escape explaining
5667 		 * me how you were going to use this. :-)
5668 		 */
5669 		ret = NET_RX_DROP;
5670 	}
5671 
5672 out:
5673 	/* The invariant here is that if *ppt_prev is not NULL
5674 	 * then skb should also be non-NULL.
5675 	 *
5676 	 * Apparently *ppt_prev assignment above holds this invariant due to
5677 	 * skb dereferencing near it.
5678 	 */
5679 	*pskb = skb;
5680 	return ret;
5681 }
5682 
5683 static int __netif_receive_skb_one_core(struct sk_buff *skb, bool pfmemalloc)
5684 {
5685 	struct net_device *orig_dev = skb->dev;
5686 	struct packet_type *pt_prev = NULL;
5687 	int ret;
5688 
5689 	ret = __netif_receive_skb_core(&skb, pfmemalloc, &pt_prev);
5690 	if (pt_prev)
5691 		ret = INDIRECT_CALL_INET(pt_prev->func, ipv6_rcv, ip_rcv, skb,
5692 					 skb->dev, pt_prev, orig_dev);
5693 	return ret;
5694 }
5695 
5696 /**
5697  *	netif_receive_skb_core - special purpose version of netif_receive_skb
5698  *	@skb: buffer to process
5699  *
5700  *	More direct receive version of netif_receive_skb().  It should
5701  *	only be used by callers that have a need to skip RPS and Generic XDP.
5702  *	Caller must also take care of handling if ``(page_is_)pfmemalloc``.
5703  *
5704  *	This function may only be called from softirq context and interrupts
5705  *	should be enabled.
5706  *
5707  *	Return values (usually ignored):
5708  *	NET_RX_SUCCESS: no congestion
5709  *	NET_RX_DROP: packet was dropped
5710  */
5711 int netif_receive_skb_core(struct sk_buff *skb)
5712 {
5713 	int ret;
5714 
5715 	rcu_read_lock();
5716 	ret = __netif_receive_skb_one_core(skb, false);
5717 	rcu_read_unlock();
5718 
5719 	return ret;
5720 }
5721 EXPORT_SYMBOL(netif_receive_skb_core);
5722 
5723 static inline void __netif_receive_skb_list_ptype(struct list_head *head,
5724 						  struct packet_type *pt_prev,
5725 						  struct net_device *orig_dev)
5726 {
5727 	struct sk_buff *skb, *next;
5728 
5729 	if (!pt_prev)
5730 		return;
5731 	if (list_empty(head))
5732 		return;
5733 	if (pt_prev->list_func != NULL)
5734 		INDIRECT_CALL_INET(pt_prev->list_func, ipv6_list_rcv,
5735 				   ip_list_rcv, head, pt_prev, orig_dev);
5736 	else
5737 		list_for_each_entry_safe(skb, next, head, list) {
5738 			skb_list_del_init(skb);
5739 			pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
5740 		}
5741 }
5742 
5743 static void __netif_receive_skb_list_core(struct list_head *head, bool pfmemalloc)
5744 {
5745 	/* Fast-path assumptions:
5746 	 * - There is no RX handler.
5747 	 * - Only one packet_type matches.
5748 	 * If either of these fails, we will end up doing some per-packet
5749 	 * processing in-line, then handling the 'last ptype' for the whole
5750 	 * sublist.  This can't cause out-of-order delivery to any single ptype,
5751 	 * because the 'last ptype' must be constant across the sublist, and all
5752 	 * other ptypes are handled per-packet.
5753 	 */
5754 	/* Current (common) ptype of sublist */
5755 	struct packet_type *pt_curr = NULL;
5756 	/* Current (common) orig_dev of sublist */
5757 	struct net_device *od_curr = NULL;
5758 	struct sk_buff *skb, *next;
5759 	LIST_HEAD(sublist);
5760 
5761 	list_for_each_entry_safe(skb, next, head, list) {
5762 		struct net_device *orig_dev = skb->dev;
5763 		struct packet_type *pt_prev = NULL;
5764 
5765 		skb_list_del_init(skb);
5766 		__netif_receive_skb_core(&skb, pfmemalloc, &pt_prev);
5767 		if (!pt_prev)
5768 			continue;
5769 		if (pt_curr != pt_prev || od_curr != orig_dev) {
5770 			/* dispatch old sublist */
5771 			__netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr);
5772 			/* start new sublist */
5773 			INIT_LIST_HEAD(&sublist);
5774 			pt_curr = pt_prev;
5775 			od_curr = orig_dev;
5776 		}
5777 		list_add_tail(&skb->list, &sublist);
5778 	}
5779 
5780 	/* dispatch final sublist */
5781 	__netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr);
5782 }
5783 
5784 static int __netif_receive_skb(struct sk_buff *skb)
5785 {
5786 	int ret;
5787 
5788 	if (sk_memalloc_socks() && skb_pfmemalloc(skb)) {
5789 		unsigned int noreclaim_flag;
5790 
5791 		/*
5792 		 * PFMEMALLOC skbs are special, they should
5793 		 * - be delivered to SOCK_MEMALLOC sockets only
5794 		 * - stay away from userspace
5795 		 * - have bounded memory usage
5796 		 *
5797 		 * Use PF_MEMALLOC as this saves us from propagating the allocation
5798 		 * context down to all allocation sites.
5799 		 */
5800 		noreclaim_flag = memalloc_noreclaim_save();
5801 		ret = __netif_receive_skb_one_core(skb, true);
5802 		memalloc_noreclaim_restore(noreclaim_flag);
5803 	} else
5804 		ret = __netif_receive_skb_one_core(skb, false);
5805 
5806 	return ret;
5807 }
5808 
5809 static void __netif_receive_skb_list(struct list_head *head)
5810 {
5811 	unsigned long noreclaim_flag = 0;
5812 	struct sk_buff *skb, *next;
5813 	bool pfmemalloc = false; /* Is current sublist PF_MEMALLOC? */
5814 
5815 	list_for_each_entry_safe(skb, next, head, list) {
5816 		if ((sk_memalloc_socks() && skb_pfmemalloc(skb)) != pfmemalloc) {
5817 			struct list_head sublist;
5818 
5819 			/* Handle the previous sublist */
5820 			list_cut_before(&sublist, head, &skb->list);
5821 			if (!list_empty(&sublist))
5822 				__netif_receive_skb_list_core(&sublist, pfmemalloc);
5823 			pfmemalloc = !pfmemalloc;
5824 			/* See comments in __netif_receive_skb */
5825 			if (pfmemalloc)
5826 				noreclaim_flag = memalloc_noreclaim_save();
5827 			else
5828 				memalloc_noreclaim_restore(noreclaim_flag);
5829 		}
5830 	}
5831 	/* Handle the remaining sublist */
5832 	if (!list_empty(head))
5833 		__netif_receive_skb_list_core(head, pfmemalloc);
5834 	/* Restore pflags */
5835 	if (pfmemalloc)
5836 		memalloc_noreclaim_restore(noreclaim_flag);
5837 }
5838 
5839 static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp)
5840 {
5841 	struct bpf_prog *old = rtnl_dereference(dev->xdp_prog);
5842 	struct bpf_prog *new = xdp->prog;
5843 	int ret = 0;
5844 
5845 	switch (xdp->command) {
5846 	case XDP_SETUP_PROG:
5847 		rcu_assign_pointer(dev->xdp_prog, new);
5848 		if (old)
5849 			bpf_prog_put(old);
5850 
5851 		if (old && !new) {
5852 			static_branch_dec(&generic_xdp_needed_key);
5853 		} else if (new && !old) {
5854 			static_branch_inc(&generic_xdp_needed_key);
5855 			dev_disable_lro(dev);
5856 			dev_disable_gro_hw(dev);
5857 		}
5858 		break;
5859 
5860 	default:
5861 		ret = -EINVAL;
5862 		break;
5863 	}
5864 
5865 	return ret;
5866 }
5867 
5868 static int netif_receive_skb_internal(struct sk_buff *skb)
5869 {
5870 	int ret;
5871 
5872 	net_timestamp_check(READ_ONCE(net_hotdata.tstamp_prequeue), skb);
5873 
5874 	if (skb_defer_rx_timestamp(skb))
5875 		return NET_RX_SUCCESS;
5876 
5877 	rcu_read_lock();
5878 #ifdef CONFIG_RPS
5879 	if (static_branch_unlikely(&rps_needed)) {
5880 		struct rps_dev_flow voidflow, *rflow = &voidflow;
5881 		int cpu = get_rps_cpu(skb->dev, skb, &rflow);
5882 
5883 		if (cpu >= 0) {
5884 			ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
5885 			rcu_read_unlock();
5886 			return ret;
5887 		}
5888 	}
5889 #endif
5890 	ret = __netif_receive_skb(skb);
5891 	rcu_read_unlock();
5892 	return ret;
5893 }
5894 
5895 void netif_receive_skb_list_internal(struct list_head *head)
5896 {
5897 	struct sk_buff *skb, *next;
5898 	LIST_HEAD(sublist);
5899 
5900 	list_for_each_entry_safe(skb, next, head, list) {
5901 		net_timestamp_check(READ_ONCE(net_hotdata.tstamp_prequeue),
5902 				    skb);
5903 		skb_list_del_init(skb);
5904 		if (!skb_defer_rx_timestamp(skb))
5905 			list_add_tail(&skb->list, &sublist);
5906 	}
5907 	list_splice_init(&sublist, head);
5908 
5909 	rcu_read_lock();
5910 #ifdef CONFIG_RPS
5911 	if (static_branch_unlikely(&rps_needed)) {
5912 		list_for_each_entry_safe(skb, next, head, list) {
5913 			struct rps_dev_flow voidflow, *rflow = &voidflow;
5914 			int cpu = get_rps_cpu(skb->dev, skb, &rflow);
5915 
5916 			if (cpu >= 0) {
5917 				/* Will be handled, remove from list */
5918 				skb_list_del_init(skb);
5919 				enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
5920 			}
5921 		}
5922 	}
5923 #endif
5924 	__netif_receive_skb_list(head);
5925 	rcu_read_unlock();
5926 }
5927 
5928 /**
5929  *	netif_receive_skb - process receive buffer from network
5930  *	@skb: buffer to process
5931  *
5932  *	netif_receive_skb() is the main receive data processing function.
5933  *	It always succeeds. The buffer may be dropped during processing
5934  *	for congestion control or by the protocol layers.
5935  *
5936  *	This function may only be called from softirq context and interrupts
5937  *	should be enabled.
5938  *
5939  *	Return values (usually ignored):
5940  *	NET_RX_SUCCESS: no congestion
5941  *	NET_RX_DROP: packet was dropped
5942  */
5943 int netif_receive_skb(struct sk_buff *skb)
5944 {
5945 	int ret;
5946 
5947 	trace_netif_receive_skb_entry(skb);
5948 
5949 	ret = netif_receive_skb_internal(skb);
5950 	trace_netif_receive_skb_exit(ret);
5951 
5952 	return ret;
5953 }
5954 EXPORT_SYMBOL(netif_receive_skb);
5955 
5956 /**
5957  *	netif_receive_skb_list - process many receive buffers from network
5958  *	@head: list of skbs to process.
5959  *
5960  *	Since return value of netif_receive_skb() is normally ignored, and
5961  *	wouldn't be meaningful for a list, this function returns void.
5962  *
5963  *	This function may only be called from softirq context and interrupts
5964  *	should be enabled.
5965  */
5966 void netif_receive_skb_list(struct list_head *head)
5967 {
5968 	struct sk_buff *skb;
5969 
5970 	if (list_empty(head))
5971 		return;
5972 	if (trace_netif_receive_skb_list_entry_enabled()) {
5973 		list_for_each_entry(skb, head, list)
5974 			trace_netif_receive_skb_list_entry(skb);
5975 	}
5976 	netif_receive_skb_list_internal(head);
5977 	trace_netif_receive_skb_list_exit(0);
5978 }
5979 EXPORT_SYMBOL(netif_receive_skb_list);
5980 
5981 static DEFINE_PER_CPU(struct work_struct, flush_works);
5982 
5983 /* Network device is going away, flush any packets still pending */
5984 static void flush_backlog(struct work_struct *work)
5985 {
5986 	struct sk_buff *skb, *tmp;
5987 	struct softnet_data *sd;
5988 
5989 	local_bh_disable();
5990 	sd = this_cpu_ptr(&softnet_data);
5991 
5992 	backlog_lock_irq_disable(sd);
5993 	skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
5994 		if (skb->dev->reg_state == NETREG_UNREGISTERING) {
5995 			__skb_unlink(skb, &sd->input_pkt_queue);
5996 			dev_kfree_skb_irq(skb);
5997 			rps_input_queue_head_incr(sd);
5998 		}
5999 	}
6000 	backlog_unlock_irq_enable(sd);
6001 
6002 	local_lock_nested_bh(&softnet_data.process_queue_bh_lock);
6003 	skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
6004 		if (skb->dev->reg_state == NETREG_UNREGISTERING) {
6005 			__skb_unlink(skb, &sd->process_queue);
6006 			kfree_skb(skb);
6007 			rps_input_queue_head_incr(sd);
6008 		}
6009 	}
6010 	local_unlock_nested_bh(&softnet_data.process_queue_bh_lock);
6011 	local_bh_enable();
6012 }
6013 
6014 static bool flush_required(int cpu)
6015 {
6016 #if IS_ENABLED(CONFIG_RPS)
6017 	struct softnet_data *sd = &per_cpu(softnet_data, cpu);
6018 	bool do_flush;
6019 
6020 	backlog_lock_irq_disable(sd);
6021 
6022 	/* as insertion into process_queue happens with the rps lock held,
6023 	 * process_queue access may race only with dequeue
6024 	 */
6025 	do_flush = !skb_queue_empty(&sd->input_pkt_queue) ||
6026 		   !skb_queue_empty_lockless(&sd->process_queue);
6027 	backlog_unlock_irq_enable(sd);
6028 
6029 	return do_flush;
6030 #endif
6031 	/* without RPS we can't safely check input_pkt_queue: during a
6032 	 * concurrent remote skb_queue_splice() we can detect as empty both
6033 	 * input_pkt_queue and process_queue even if the latter could end-up
6034 	 * containing a lot of packets.
6035 	 */
6036 	return true;
6037 }
6038 
6039 static void flush_all_backlogs(void)
6040 {
6041 	static cpumask_t flush_cpus;
6042 	unsigned int cpu;
6043 
6044 	/* since we are under rtnl lock protection we can use static data
6045 	 * for the cpumask and avoid allocating on stack the possibly
6046 	 * large mask
6047 	 */
6048 	ASSERT_RTNL();
6049 
6050 	cpus_read_lock();
6051 
6052 	cpumask_clear(&flush_cpus);
6053 	for_each_online_cpu(cpu) {
6054 		if (flush_required(cpu)) {
6055 			queue_work_on(cpu, system_highpri_wq,
6056 				      per_cpu_ptr(&flush_works, cpu));
6057 			cpumask_set_cpu(cpu, &flush_cpus);
6058 		}
6059 	}
6060 
6061 	/* we can have in flight packet[s] on the cpus we are not flushing,
6062 	 * synchronize_net() in unregister_netdevice_many() will take care of
6063 	 * them
6064 	 */
6065 	for_each_cpu(cpu, &flush_cpus)
6066 		flush_work(per_cpu_ptr(&flush_works, cpu));
6067 
6068 	cpus_read_unlock();
6069 }
6070 
6071 static void net_rps_send_ipi(struct softnet_data *remsd)
6072 {
6073 #ifdef CONFIG_RPS
6074 	while (remsd) {
6075 		struct softnet_data *next = remsd->rps_ipi_next;
6076 
6077 		if (cpu_online(remsd->cpu))
6078 			smp_call_function_single_async(remsd->cpu, &remsd->csd);
6079 		remsd = next;
6080 	}
6081 #endif
6082 }
6083 
6084 /*
6085  * net_rps_action_and_irq_enable sends any pending IPI's for rps.
6086  * Note: called with local irq disabled, but exits with local irq enabled.
6087  */
6088 static void net_rps_action_and_irq_enable(struct softnet_data *sd)
6089 {
6090 #ifdef CONFIG_RPS
6091 	struct softnet_data *remsd = sd->rps_ipi_list;
6092 
6093 	if (!use_backlog_threads() && remsd) {
6094 		sd->rps_ipi_list = NULL;
6095 
6096 		local_irq_enable();
6097 
6098 		/* Send pending IPI's to kick RPS processing on remote cpus. */
6099 		net_rps_send_ipi(remsd);
6100 	} else
6101 #endif
6102 		local_irq_enable();
6103 }
6104 
6105 static bool sd_has_rps_ipi_waiting(struct softnet_data *sd)
6106 {
6107 #ifdef CONFIG_RPS
6108 	return !use_backlog_threads() && sd->rps_ipi_list;
6109 #else
6110 	return false;
6111 #endif
6112 }
6113 
6114 static int process_backlog(struct napi_struct *napi, int quota)
6115 {
6116 	struct softnet_data *sd = container_of(napi, struct softnet_data, backlog);
6117 	bool again = true;
6118 	int work = 0;
6119 
6120 	/* Check if we have pending ipi, its better to send them now,
6121 	 * not waiting net_rx_action() end.
6122 	 */
6123 	if (sd_has_rps_ipi_waiting(sd)) {
6124 		local_irq_disable();
6125 		net_rps_action_and_irq_enable(sd);
6126 	}
6127 
6128 	napi->weight = READ_ONCE(net_hotdata.dev_rx_weight);
6129 	while (again) {
6130 		struct sk_buff *skb;
6131 
6132 		local_lock_nested_bh(&softnet_data.process_queue_bh_lock);
6133 		while ((skb = __skb_dequeue(&sd->process_queue))) {
6134 			local_unlock_nested_bh(&softnet_data.process_queue_bh_lock);
6135 			rcu_read_lock();
6136 			__netif_receive_skb(skb);
6137 			rcu_read_unlock();
6138 			if (++work >= quota) {
6139 				rps_input_queue_head_add(sd, work);
6140 				return work;
6141 			}
6142 
6143 			local_lock_nested_bh(&softnet_data.process_queue_bh_lock);
6144 		}
6145 		local_unlock_nested_bh(&softnet_data.process_queue_bh_lock);
6146 
6147 		backlog_lock_irq_disable(sd);
6148 		if (skb_queue_empty(&sd->input_pkt_queue)) {
6149 			/*
6150 			 * Inline a custom version of __napi_complete().
6151 			 * only current cpu owns and manipulates this napi,
6152 			 * and NAPI_STATE_SCHED is the only possible flag set
6153 			 * on backlog.
6154 			 * We can use a plain write instead of clear_bit(),
6155 			 * and we dont need an smp_mb() memory barrier.
6156 			 */
6157 			napi->state &= NAPIF_STATE_THREADED;
6158 			again = false;
6159 		} else {
6160 			local_lock_nested_bh(&softnet_data.process_queue_bh_lock);
6161 			skb_queue_splice_tail_init(&sd->input_pkt_queue,
6162 						   &sd->process_queue);
6163 			local_unlock_nested_bh(&softnet_data.process_queue_bh_lock);
6164 		}
6165 		backlog_unlock_irq_enable(sd);
6166 	}
6167 
6168 	if (work)
6169 		rps_input_queue_head_add(sd, work);
6170 	return work;
6171 }
6172 
6173 /**
6174  * __napi_schedule - schedule for receive
6175  * @n: entry to schedule
6176  *
6177  * The entry's receive function will be scheduled to run.
6178  * Consider using __napi_schedule_irqoff() if hard irqs are masked.
6179  */
6180 void __napi_schedule(struct napi_struct *n)
6181 {
6182 	unsigned long flags;
6183 
6184 	local_irq_save(flags);
6185 	____napi_schedule(this_cpu_ptr(&softnet_data), n);
6186 	local_irq_restore(flags);
6187 }
6188 EXPORT_SYMBOL(__napi_schedule);
6189 
6190 /**
6191  *	napi_schedule_prep - check if napi can be scheduled
6192  *	@n: napi context
6193  *
6194  * Test if NAPI routine is already running, and if not mark
6195  * it as running.  This is used as a condition variable to
6196  * insure only one NAPI poll instance runs.  We also make
6197  * sure there is no pending NAPI disable.
6198  */
6199 bool napi_schedule_prep(struct napi_struct *n)
6200 {
6201 	unsigned long new, val = READ_ONCE(n->state);
6202 
6203 	do {
6204 		if (unlikely(val & NAPIF_STATE_DISABLE))
6205 			return false;
6206 		new = val | NAPIF_STATE_SCHED;
6207 
6208 		/* Sets STATE_MISSED bit if STATE_SCHED was already set
6209 		 * This was suggested by Alexander Duyck, as compiler
6210 		 * emits better code than :
6211 		 * if (val & NAPIF_STATE_SCHED)
6212 		 *     new |= NAPIF_STATE_MISSED;
6213 		 */
6214 		new |= (val & NAPIF_STATE_SCHED) / NAPIF_STATE_SCHED *
6215 						   NAPIF_STATE_MISSED;
6216 	} while (!try_cmpxchg(&n->state, &val, new));
6217 
6218 	return !(val & NAPIF_STATE_SCHED);
6219 }
6220 EXPORT_SYMBOL(napi_schedule_prep);
6221 
6222 /**
6223  * __napi_schedule_irqoff - schedule for receive
6224  * @n: entry to schedule
6225  *
6226  * Variant of __napi_schedule() assuming hard irqs are masked.
6227  *
6228  * On PREEMPT_RT enabled kernels this maps to __napi_schedule()
6229  * because the interrupt disabled assumption might not be true
6230  * due to force-threaded interrupts and spinlock substitution.
6231  */
6232 void __napi_schedule_irqoff(struct napi_struct *n)
6233 {
6234 	if (!IS_ENABLED(CONFIG_PREEMPT_RT))
6235 		____napi_schedule(this_cpu_ptr(&softnet_data), n);
6236 	else
6237 		__napi_schedule(n);
6238 }
6239 EXPORT_SYMBOL(__napi_schedule_irqoff);
6240 
6241 bool napi_complete_done(struct napi_struct *n, int work_done)
6242 {
6243 	unsigned long flags, val, new, timeout = 0;
6244 	bool ret = true;
6245 
6246 	/*
6247 	 * 1) Don't let napi dequeue from the cpu poll list
6248 	 *    just in case its running on a different cpu.
6249 	 * 2) If we are busy polling, do nothing here, we have
6250 	 *    the guarantee we will be called later.
6251 	 */
6252 	if (unlikely(n->state & (NAPIF_STATE_NPSVC |
6253 				 NAPIF_STATE_IN_BUSY_POLL)))
6254 		return false;
6255 
6256 	if (work_done) {
6257 		if (n->gro_bitmask)
6258 			timeout = napi_get_gro_flush_timeout(n);
6259 		n->defer_hard_irqs_count = napi_get_defer_hard_irqs(n);
6260 	}
6261 	if (n->defer_hard_irqs_count > 0) {
6262 		n->defer_hard_irqs_count--;
6263 		timeout = napi_get_gro_flush_timeout(n);
6264 		if (timeout)
6265 			ret = false;
6266 	}
6267 	if (n->gro_bitmask) {
6268 		/* When the NAPI instance uses a timeout and keeps postponing
6269 		 * it, we need to bound somehow the time packets are kept in
6270 		 * the GRO layer
6271 		 */
6272 		napi_gro_flush(n, !!timeout);
6273 	}
6274 
6275 	gro_normal_list(n);
6276 
6277 	if (unlikely(!list_empty(&n->poll_list))) {
6278 		/* If n->poll_list is not empty, we need to mask irqs */
6279 		local_irq_save(flags);
6280 		list_del_init(&n->poll_list);
6281 		local_irq_restore(flags);
6282 	}
6283 	WRITE_ONCE(n->list_owner, -1);
6284 
6285 	val = READ_ONCE(n->state);
6286 	do {
6287 		WARN_ON_ONCE(!(val & NAPIF_STATE_SCHED));
6288 
6289 		new = val & ~(NAPIF_STATE_MISSED | NAPIF_STATE_SCHED |
6290 			      NAPIF_STATE_SCHED_THREADED |
6291 			      NAPIF_STATE_PREFER_BUSY_POLL);
6292 
6293 		/* If STATE_MISSED was set, leave STATE_SCHED set,
6294 		 * because we will call napi->poll() one more time.
6295 		 * This C code was suggested by Alexander Duyck to help gcc.
6296 		 */
6297 		new |= (val & NAPIF_STATE_MISSED) / NAPIF_STATE_MISSED *
6298 						    NAPIF_STATE_SCHED;
6299 	} while (!try_cmpxchg(&n->state, &val, new));
6300 
6301 	if (unlikely(val & NAPIF_STATE_MISSED)) {
6302 		__napi_schedule(n);
6303 		return false;
6304 	}
6305 
6306 	if (timeout)
6307 		hrtimer_start(&n->timer, ns_to_ktime(timeout),
6308 			      HRTIMER_MODE_REL_PINNED);
6309 	return ret;
6310 }
6311 EXPORT_SYMBOL(napi_complete_done);
6312 
6313 /* must be called under rcu_read_lock(), as we dont take a reference */
6314 struct napi_struct *napi_by_id(unsigned int napi_id)
6315 {
6316 	unsigned int hash = napi_id % HASH_SIZE(napi_hash);
6317 	struct napi_struct *napi;
6318 
6319 	hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node)
6320 		if (napi->napi_id == napi_id)
6321 			return napi;
6322 
6323 	return NULL;
6324 }
6325 
6326 static void skb_defer_free_flush(struct softnet_data *sd)
6327 {
6328 	struct sk_buff *skb, *next;
6329 
6330 	/* Paired with WRITE_ONCE() in skb_attempt_defer_free() */
6331 	if (!READ_ONCE(sd->defer_list))
6332 		return;
6333 
6334 	spin_lock(&sd->defer_lock);
6335 	skb = sd->defer_list;
6336 	sd->defer_list = NULL;
6337 	sd->defer_count = 0;
6338 	spin_unlock(&sd->defer_lock);
6339 
6340 	while (skb != NULL) {
6341 		next = skb->next;
6342 		napi_consume_skb(skb, 1);
6343 		skb = next;
6344 	}
6345 }
6346 
6347 #if defined(CONFIG_NET_RX_BUSY_POLL)
6348 
6349 static void __busy_poll_stop(struct napi_struct *napi, bool skip_schedule)
6350 {
6351 	if (!skip_schedule) {
6352 		gro_normal_list(napi);
6353 		__napi_schedule(napi);
6354 		return;
6355 	}
6356 
6357 	if (napi->gro_bitmask) {
6358 		/* flush too old packets
6359 		 * If HZ < 1000, flush all packets.
6360 		 */
6361 		napi_gro_flush(napi, HZ >= 1000);
6362 	}
6363 
6364 	gro_normal_list(napi);
6365 	clear_bit(NAPI_STATE_SCHED, &napi->state);
6366 }
6367 
6368 enum {
6369 	NAPI_F_PREFER_BUSY_POLL	= 1,
6370 	NAPI_F_END_ON_RESCHED	= 2,
6371 };
6372 
6373 static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock,
6374 			   unsigned flags, u16 budget)
6375 {
6376 	struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx;
6377 	bool skip_schedule = false;
6378 	unsigned long timeout;
6379 	int rc;
6380 
6381 	/* Busy polling means there is a high chance device driver hard irq
6382 	 * could not grab NAPI_STATE_SCHED, and that NAPI_STATE_MISSED was
6383 	 * set in napi_schedule_prep().
6384 	 * Since we are about to call napi->poll() once more, we can safely
6385 	 * clear NAPI_STATE_MISSED.
6386 	 *
6387 	 * Note: x86 could use a single "lock and ..." instruction
6388 	 * to perform these two clear_bit()
6389 	 */
6390 	clear_bit(NAPI_STATE_MISSED, &napi->state);
6391 	clear_bit(NAPI_STATE_IN_BUSY_POLL, &napi->state);
6392 
6393 	local_bh_disable();
6394 	bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx);
6395 
6396 	if (flags & NAPI_F_PREFER_BUSY_POLL) {
6397 		napi->defer_hard_irqs_count = napi_get_defer_hard_irqs(napi);
6398 		timeout = napi_get_gro_flush_timeout(napi);
6399 		if (napi->defer_hard_irqs_count && timeout) {
6400 			hrtimer_start(&napi->timer, ns_to_ktime(timeout), HRTIMER_MODE_REL_PINNED);
6401 			skip_schedule = true;
6402 		}
6403 	}
6404 
6405 	/* All we really want here is to re-enable device interrupts.
6406 	 * Ideally, a new ndo_busy_poll_stop() could avoid another round.
6407 	 */
6408 	rc = napi->poll(napi, budget);
6409 	/* We can't gro_normal_list() here, because napi->poll() might have
6410 	 * rearmed the napi (napi_complete_done()) in which case it could
6411 	 * already be running on another CPU.
6412 	 */
6413 	trace_napi_poll(napi, rc, budget);
6414 	netpoll_poll_unlock(have_poll_lock);
6415 	if (rc == budget)
6416 		__busy_poll_stop(napi, skip_schedule);
6417 	bpf_net_ctx_clear(bpf_net_ctx);
6418 	local_bh_enable();
6419 }
6420 
6421 static void __napi_busy_loop(unsigned int napi_id,
6422 		      bool (*loop_end)(void *, unsigned long),
6423 		      void *loop_end_arg, unsigned flags, u16 budget)
6424 {
6425 	unsigned long start_time = loop_end ? busy_loop_current_time() : 0;
6426 	int (*napi_poll)(struct napi_struct *napi, int budget);
6427 	struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx;
6428 	void *have_poll_lock = NULL;
6429 	struct napi_struct *napi;
6430 
6431 	WARN_ON_ONCE(!rcu_read_lock_held());
6432 
6433 restart:
6434 	napi_poll = NULL;
6435 
6436 	napi = napi_by_id(napi_id);
6437 	if (!napi)
6438 		return;
6439 
6440 	if (!IS_ENABLED(CONFIG_PREEMPT_RT))
6441 		preempt_disable();
6442 	for (;;) {
6443 		int work = 0;
6444 
6445 		local_bh_disable();
6446 		bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx);
6447 		if (!napi_poll) {
6448 			unsigned long val = READ_ONCE(napi->state);
6449 
6450 			/* If multiple threads are competing for this napi,
6451 			 * we avoid dirtying napi->state as much as we can.
6452 			 */
6453 			if (val & (NAPIF_STATE_DISABLE | NAPIF_STATE_SCHED |
6454 				   NAPIF_STATE_IN_BUSY_POLL)) {
6455 				if (flags & NAPI_F_PREFER_BUSY_POLL)
6456 					set_bit(NAPI_STATE_PREFER_BUSY_POLL, &napi->state);
6457 				goto count;
6458 			}
6459 			if (cmpxchg(&napi->state, val,
6460 				    val | NAPIF_STATE_IN_BUSY_POLL |
6461 					  NAPIF_STATE_SCHED) != val) {
6462 				if (flags & NAPI_F_PREFER_BUSY_POLL)
6463 					set_bit(NAPI_STATE_PREFER_BUSY_POLL, &napi->state);
6464 				goto count;
6465 			}
6466 			have_poll_lock = netpoll_poll_lock(napi);
6467 			napi_poll = napi->poll;
6468 		}
6469 		work = napi_poll(napi, budget);
6470 		trace_napi_poll(napi, work, budget);
6471 		gro_normal_list(napi);
6472 count:
6473 		if (work > 0)
6474 			__NET_ADD_STATS(dev_net(napi->dev),
6475 					LINUX_MIB_BUSYPOLLRXPACKETS, work);
6476 		skb_defer_free_flush(this_cpu_ptr(&softnet_data));
6477 		bpf_net_ctx_clear(bpf_net_ctx);
6478 		local_bh_enable();
6479 
6480 		if (!loop_end || loop_end(loop_end_arg, start_time))
6481 			break;
6482 
6483 		if (unlikely(need_resched())) {
6484 			if (flags & NAPI_F_END_ON_RESCHED)
6485 				break;
6486 			if (napi_poll)
6487 				busy_poll_stop(napi, have_poll_lock, flags, budget);
6488 			if (!IS_ENABLED(CONFIG_PREEMPT_RT))
6489 				preempt_enable();
6490 			rcu_read_unlock();
6491 			cond_resched();
6492 			rcu_read_lock();
6493 			if (loop_end(loop_end_arg, start_time))
6494 				return;
6495 			goto restart;
6496 		}
6497 		cpu_relax();
6498 	}
6499 	if (napi_poll)
6500 		busy_poll_stop(napi, have_poll_lock, flags, budget);
6501 	if (!IS_ENABLED(CONFIG_PREEMPT_RT))
6502 		preempt_enable();
6503 }
6504 
6505 void napi_busy_loop_rcu(unsigned int napi_id,
6506 			bool (*loop_end)(void *, unsigned long),
6507 			void *loop_end_arg, bool prefer_busy_poll, u16 budget)
6508 {
6509 	unsigned flags = NAPI_F_END_ON_RESCHED;
6510 
6511 	if (prefer_busy_poll)
6512 		flags |= NAPI_F_PREFER_BUSY_POLL;
6513 
6514 	__napi_busy_loop(napi_id, loop_end, loop_end_arg, flags, budget);
6515 }
6516 
6517 void napi_busy_loop(unsigned int napi_id,
6518 		    bool (*loop_end)(void *, unsigned long),
6519 		    void *loop_end_arg, bool prefer_busy_poll, u16 budget)
6520 {
6521 	unsigned flags = prefer_busy_poll ? NAPI_F_PREFER_BUSY_POLL : 0;
6522 
6523 	rcu_read_lock();
6524 	__napi_busy_loop(napi_id, loop_end, loop_end_arg, flags, budget);
6525 	rcu_read_unlock();
6526 }
6527 EXPORT_SYMBOL(napi_busy_loop);
6528 
6529 void napi_suspend_irqs(unsigned int napi_id)
6530 {
6531 	struct napi_struct *napi;
6532 
6533 	rcu_read_lock();
6534 	napi = napi_by_id(napi_id);
6535 	if (napi) {
6536 		unsigned long timeout = napi_get_irq_suspend_timeout(napi);
6537 
6538 		if (timeout)
6539 			hrtimer_start(&napi->timer, ns_to_ktime(timeout),
6540 				      HRTIMER_MODE_REL_PINNED);
6541 	}
6542 	rcu_read_unlock();
6543 }
6544 
6545 void napi_resume_irqs(unsigned int napi_id)
6546 {
6547 	struct napi_struct *napi;
6548 
6549 	rcu_read_lock();
6550 	napi = napi_by_id(napi_id);
6551 	if (napi) {
6552 		/* If irq_suspend_timeout is set to 0 between the call to
6553 		 * napi_suspend_irqs and now, the original value still
6554 		 * determines the safety timeout as intended and napi_watchdog
6555 		 * will resume irq processing.
6556 		 */
6557 		if (napi_get_irq_suspend_timeout(napi)) {
6558 			local_bh_disable();
6559 			napi_schedule(napi);
6560 			local_bh_enable();
6561 		}
6562 	}
6563 	rcu_read_unlock();
6564 }
6565 
6566 #endif /* CONFIG_NET_RX_BUSY_POLL */
6567 
6568 static void __napi_hash_add_with_id(struct napi_struct *napi,
6569 				    unsigned int napi_id)
6570 {
6571 	napi->napi_id = napi_id;
6572 	hlist_add_head_rcu(&napi->napi_hash_node,
6573 			   &napi_hash[napi->napi_id % HASH_SIZE(napi_hash)]);
6574 }
6575 
6576 static void napi_hash_add_with_id(struct napi_struct *napi,
6577 				  unsigned int napi_id)
6578 {
6579 	unsigned long flags;
6580 
6581 	spin_lock_irqsave(&napi_hash_lock, flags);
6582 	WARN_ON_ONCE(napi_by_id(napi_id));
6583 	__napi_hash_add_with_id(napi, napi_id);
6584 	spin_unlock_irqrestore(&napi_hash_lock, flags);
6585 }
6586 
6587 static void napi_hash_add(struct napi_struct *napi)
6588 {
6589 	unsigned long flags;
6590 
6591 	if (test_bit(NAPI_STATE_NO_BUSY_POLL, &napi->state))
6592 		return;
6593 
6594 	spin_lock_irqsave(&napi_hash_lock, flags);
6595 
6596 	/* 0..NR_CPUS range is reserved for sender_cpu use */
6597 	do {
6598 		if (unlikely(++napi_gen_id < MIN_NAPI_ID))
6599 			napi_gen_id = MIN_NAPI_ID;
6600 	} while (napi_by_id(napi_gen_id));
6601 
6602 	__napi_hash_add_with_id(napi, napi_gen_id);
6603 
6604 	spin_unlock_irqrestore(&napi_hash_lock, flags);
6605 }
6606 
6607 /* Warning : caller is responsible to make sure rcu grace period
6608  * is respected before freeing memory containing @napi
6609  */
6610 static void napi_hash_del(struct napi_struct *napi)
6611 {
6612 	unsigned long flags;
6613 
6614 	spin_lock_irqsave(&napi_hash_lock, flags);
6615 
6616 	hlist_del_init_rcu(&napi->napi_hash_node);
6617 
6618 	spin_unlock_irqrestore(&napi_hash_lock, flags);
6619 }
6620 
6621 static enum hrtimer_restart napi_watchdog(struct hrtimer *timer)
6622 {
6623 	struct napi_struct *napi;
6624 
6625 	napi = container_of(timer, struct napi_struct, timer);
6626 
6627 	/* Note : we use a relaxed variant of napi_schedule_prep() not setting
6628 	 * NAPI_STATE_MISSED, since we do not react to a device IRQ.
6629 	 */
6630 	if (!napi_disable_pending(napi) &&
6631 	    !test_and_set_bit(NAPI_STATE_SCHED, &napi->state)) {
6632 		clear_bit(NAPI_STATE_PREFER_BUSY_POLL, &napi->state);
6633 		__napi_schedule_irqoff(napi);
6634 	}
6635 
6636 	return HRTIMER_NORESTART;
6637 }
6638 
6639 static void init_gro_hash(struct napi_struct *napi)
6640 {
6641 	int i;
6642 
6643 	for (i = 0; i < GRO_HASH_BUCKETS; i++) {
6644 		INIT_LIST_HEAD(&napi->gro_hash[i].list);
6645 		napi->gro_hash[i].count = 0;
6646 	}
6647 	napi->gro_bitmask = 0;
6648 }
6649 
6650 int dev_set_threaded(struct net_device *dev, bool threaded)
6651 {
6652 	struct napi_struct *napi;
6653 	int err = 0;
6654 
6655 	if (dev->threaded == threaded)
6656 		return 0;
6657 
6658 	if (threaded) {
6659 		list_for_each_entry(napi, &dev->napi_list, dev_list) {
6660 			if (!napi->thread) {
6661 				err = napi_kthread_create(napi);
6662 				if (err) {
6663 					threaded = false;
6664 					break;
6665 				}
6666 			}
6667 		}
6668 	}
6669 
6670 	WRITE_ONCE(dev->threaded, threaded);
6671 
6672 	/* Make sure kthread is created before THREADED bit
6673 	 * is set.
6674 	 */
6675 	smp_mb__before_atomic();
6676 
6677 	/* Setting/unsetting threaded mode on a napi might not immediately
6678 	 * take effect, if the current napi instance is actively being
6679 	 * polled. In this case, the switch between threaded mode and
6680 	 * softirq mode will happen in the next round of napi_schedule().
6681 	 * This should not cause hiccups/stalls to the live traffic.
6682 	 */
6683 	list_for_each_entry(napi, &dev->napi_list, dev_list)
6684 		assign_bit(NAPI_STATE_THREADED, &napi->state, threaded);
6685 
6686 	return err;
6687 }
6688 EXPORT_SYMBOL(dev_set_threaded);
6689 
6690 /**
6691  * netif_queue_set_napi - Associate queue with the napi
6692  * @dev: device to which NAPI and queue belong
6693  * @queue_index: Index of queue
6694  * @type: queue type as RX or TX
6695  * @napi: NAPI context, pass NULL to clear previously set NAPI
6696  *
6697  * Set queue with its corresponding napi context. This should be done after
6698  * registering the NAPI handler for the queue-vector and the queues have been
6699  * mapped to the corresponding interrupt vector.
6700  */
6701 void netif_queue_set_napi(struct net_device *dev, unsigned int queue_index,
6702 			  enum netdev_queue_type type, struct napi_struct *napi)
6703 {
6704 	struct netdev_rx_queue *rxq;
6705 	struct netdev_queue *txq;
6706 
6707 	if (WARN_ON_ONCE(napi && !napi->dev))
6708 		return;
6709 	if (dev->reg_state >= NETREG_REGISTERED)
6710 		ASSERT_RTNL();
6711 
6712 	switch (type) {
6713 	case NETDEV_QUEUE_TYPE_RX:
6714 		rxq = __netif_get_rx_queue(dev, queue_index);
6715 		rxq->napi = napi;
6716 		return;
6717 	case NETDEV_QUEUE_TYPE_TX:
6718 		txq = netdev_get_tx_queue(dev, queue_index);
6719 		txq->napi = napi;
6720 		return;
6721 	default:
6722 		return;
6723 	}
6724 }
6725 EXPORT_SYMBOL(netif_queue_set_napi);
6726 
6727 static void napi_restore_config(struct napi_struct *n)
6728 {
6729 	n->defer_hard_irqs = n->config->defer_hard_irqs;
6730 	n->gro_flush_timeout = n->config->gro_flush_timeout;
6731 	n->irq_suspend_timeout = n->config->irq_suspend_timeout;
6732 	/* a NAPI ID might be stored in the config, if so use it. if not, use
6733 	 * napi_hash_add to generate one for us. It will be saved to the config
6734 	 * in napi_disable.
6735 	 */
6736 	if (n->config->napi_id)
6737 		napi_hash_add_with_id(n, n->config->napi_id);
6738 	else
6739 		napi_hash_add(n);
6740 }
6741 
6742 static void napi_save_config(struct napi_struct *n)
6743 {
6744 	n->config->defer_hard_irqs = n->defer_hard_irqs;
6745 	n->config->gro_flush_timeout = n->gro_flush_timeout;
6746 	n->config->irq_suspend_timeout = n->irq_suspend_timeout;
6747 	n->config->napi_id = n->napi_id;
6748 	napi_hash_del(n);
6749 }
6750 
6751 void netif_napi_add_weight(struct net_device *dev, struct napi_struct *napi,
6752 			   int (*poll)(struct napi_struct *, int), int weight)
6753 {
6754 	if (WARN_ON(test_and_set_bit(NAPI_STATE_LISTED, &napi->state)))
6755 		return;
6756 
6757 	INIT_LIST_HEAD(&napi->poll_list);
6758 	INIT_HLIST_NODE(&napi->napi_hash_node);
6759 	hrtimer_init(&napi->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
6760 	napi->timer.function = napi_watchdog;
6761 	init_gro_hash(napi);
6762 	napi->skb = NULL;
6763 	INIT_LIST_HEAD(&napi->rx_list);
6764 	napi->rx_count = 0;
6765 	napi->poll = poll;
6766 	if (weight > NAPI_POLL_WEIGHT)
6767 		netdev_err_once(dev, "%s() called with weight %d\n", __func__,
6768 				weight);
6769 	napi->weight = weight;
6770 	napi->dev = dev;
6771 #ifdef CONFIG_NETPOLL
6772 	napi->poll_owner = -1;
6773 #endif
6774 	napi->list_owner = -1;
6775 	set_bit(NAPI_STATE_SCHED, &napi->state);
6776 	set_bit(NAPI_STATE_NPSVC, &napi->state);
6777 	list_add_rcu(&napi->dev_list, &dev->napi_list);
6778 
6779 	/* default settings from sysfs are applied to all NAPIs. any per-NAPI
6780 	 * configuration will be loaded in napi_enable
6781 	 */
6782 	napi_set_defer_hard_irqs(napi, READ_ONCE(dev->napi_defer_hard_irqs));
6783 	napi_set_gro_flush_timeout(napi, READ_ONCE(dev->gro_flush_timeout));
6784 
6785 	napi_get_frags_check(napi);
6786 	/* Create kthread for this napi if dev->threaded is set.
6787 	 * Clear dev->threaded if kthread creation failed so that
6788 	 * threaded mode will not be enabled in napi_enable().
6789 	 */
6790 	if (dev->threaded && napi_kthread_create(napi))
6791 		dev->threaded = false;
6792 	netif_napi_set_irq(napi, -1);
6793 }
6794 EXPORT_SYMBOL(netif_napi_add_weight);
6795 
6796 void napi_disable(struct napi_struct *n)
6797 {
6798 	unsigned long val, new;
6799 
6800 	might_sleep();
6801 	set_bit(NAPI_STATE_DISABLE, &n->state);
6802 
6803 	val = READ_ONCE(n->state);
6804 	do {
6805 		while (val & (NAPIF_STATE_SCHED | NAPIF_STATE_NPSVC)) {
6806 			usleep_range(20, 200);
6807 			val = READ_ONCE(n->state);
6808 		}
6809 
6810 		new = val | NAPIF_STATE_SCHED | NAPIF_STATE_NPSVC;
6811 		new &= ~(NAPIF_STATE_THREADED | NAPIF_STATE_PREFER_BUSY_POLL);
6812 	} while (!try_cmpxchg(&n->state, &val, new));
6813 
6814 	hrtimer_cancel(&n->timer);
6815 
6816 	if (n->config)
6817 		napi_save_config(n);
6818 	else
6819 		napi_hash_del(n);
6820 
6821 	clear_bit(NAPI_STATE_DISABLE, &n->state);
6822 }
6823 EXPORT_SYMBOL(napi_disable);
6824 
6825 /**
6826  *	napi_enable - enable NAPI scheduling
6827  *	@n: NAPI context
6828  *
6829  * Resume NAPI from being scheduled on this context.
6830  * Must be paired with napi_disable.
6831  */
6832 void napi_enable(struct napi_struct *n)
6833 {
6834 	unsigned long new, val = READ_ONCE(n->state);
6835 
6836 	if (n->config)
6837 		napi_restore_config(n);
6838 	else
6839 		napi_hash_add(n);
6840 
6841 	do {
6842 		BUG_ON(!test_bit(NAPI_STATE_SCHED, &val));
6843 
6844 		new = val & ~(NAPIF_STATE_SCHED | NAPIF_STATE_NPSVC);
6845 		if (n->dev->threaded && n->thread)
6846 			new |= NAPIF_STATE_THREADED;
6847 	} while (!try_cmpxchg(&n->state, &val, new));
6848 }
6849 EXPORT_SYMBOL(napi_enable);
6850 
6851 static void flush_gro_hash(struct napi_struct *napi)
6852 {
6853 	int i;
6854 
6855 	for (i = 0; i < GRO_HASH_BUCKETS; i++) {
6856 		struct sk_buff *skb, *n;
6857 
6858 		list_for_each_entry_safe(skb, n, &napi->gro_hash[i].list, list)
6859 			kfree_skb(skb);
6860 		napi->gro_hash[i].count = 0;
6861 	}
6862 }
6863 
6864 /* Must be called in process context */
6865 void __netif_napi_del(struct napi_struct *napi)
6866 {
6867 	if (!test_and_clear_bit(NAPI_STATE_LISTED, &napi->state))
6868 		return;
6869 
6870 	if (napi->config) {
6871 		napi->index = -1;
6872 		napi->config = NULL;
6873 	}
6874 
6875 	list_del_rcu(&napi->dev_list);
6876 	napi_free_frags(napi);
6877 
6878 	flush_gro_hash(napi);
6879 	napi->gro_bitmask = 0;
6880 
6881 	if (napi->thread) {
6882 		kthread_stop(napi->thread);
6883 		napi->thread = NULL;
6884 	}
6885 }
6886 EXPORT_SYMBOL(__netif_napi_del);
6887 
6888 static int __napi_poll(struct napi_struct *n, bool *repoll)
6889 {
6890 	int work, weight;
6891 
6892 	weight = n->weight;
6893 
6894 	/* This NAPI_STATE_SCHED test is for avoiding a race
6895 	 * with netpoll's poll_napi().  Only the entity which
6896 	 * obtains the lock and sees NAPI_STATE_SCHED set will
6897 	 * actually make the ->poll() call.  Therefore we avoid
6898 	 * accidentally calling ->poll() when NAPI is not scheduled.
6899 	 */
6900 	work = 0;
6901 	if (napi_is_scheduled(n)) {
6902 		work = n->poll(n, weight);
6903 		trace_napi_poll(n, work, weight);
6904 
6905 		xdp_do_check_flushed(n);
6906 	}
6907 
6908 	if (unlikely(work > weight))
6909 		netdev_err_once(n->dev, "NAPI poll function %pS returned %d, exceeding its budget of %d.\n",
6910 				n->poll, work, weight);
6911 
6912 	if (likely(work < weight))
6913 		return work;
6914 
6915 	/* Drivers must not modify the NAPI state if they
6916 	 * consume the entire weight.  In such cases this code
6917 	 * still "owns" the NAPI instance and therefore can
6918 	 * move the instance around on the list at-will.
6919 	 */
6920 	if (unlikely(napi_disable_pending(n))) {
6921 		napi_complete(n);
6922 		return work;
6923 	}
6924 
6925 	/* The NAPI context has more processing work, but busy-polling
6926 	 * is preferred. Exit early.
6927 	 */
6928 	if (napi_prefer_busy_poll(n)) {
6929 		if (napi_complete_done(n, work)) {
6930 			/* If timeout is not set, we need to make sure
6931 			 * that the NAPI is re-scheduled.
6932 			 */
6933 			napi_schedule(n);
6934 		}
6935 		return work;
6936 	}
6937 
6938 	if (n->gro_bitmask) {
6939 		/* flush too old packets
6940 		 * If HZ < 1000, flush all packets.
6941 		 */
6942 		napi_gro_flush(n, HZ >= 1000);
6943 	}
6944 
6945 	gro_normal_list(n);
6946 
6947 	/* Some drivers may have called napi_schedule
6948 	 * prior to exhausting their budget.
6949 	 */
6950 	if (unlikely(!list_empty(&n->poll_list))) {
6951 		pr_warn_once("%s: Budget exhausted after napi rescheduled\n",
6952 			     n->dev ? n->dev->name : "backlog");
6953 		return work;
6954 	}
6955 
6956 	*repoll = true;
6957 
6958 	return work;
6959 }
6960 
6961 static int napi_poll(struct napi_struct *n, struct list_head *repoll)
6962 {
6963 	bool do_repoll = false;
6964 	void *have;
6965 	int work;
6966 
6967 	list_del_init(&n->poll_list);
6968 
6969 	have = netpoll_poll_lock(n);
6970 
6971 	work = __napi_poll(n, &do_repoll);
6972 
6973 	if (do_repoll)
6974 		list_add_tail(&n->poll_list, repoll);
6975 
6976 	netpoll_poll_unlock(have);
6977 
6978 	return work;
6979 }
6980 
6981 static int napi_thread_wait(struct napi_struct *napi)
6982 {
6983 	set_current_state(TASK_INTERRUPTIBLE);
6984 
6985 	while (!kthread_should_stop()) {
6986 		/* Testing SCHED_THREADED bit here to make sure the current
6987 		 * kthread owns this napi and could poll on this napi.
6988 		 * Testing SCHED bit is not enough because SCHED bit might be
6989 		 * set by some other busy poll thread or by napi_disable().
6990 		 */
6991 		if (test_bit(NAPI_STATE_SCHED_THREADED, &napi->state)) {
6992 			WARN_ON(!list_empty(&napi->poll_list));
6993 			__set_current_state(TASK_RUNNING);
6994 			return 0;
6995 		}
6996 
6997 		schedule();
6998 		set_current_state(TASK_INTERRUPTIBLE);
6999 	}
7000 	__set_current_state(TASK_RUNNING);
7001 
7002 	return -1;
7003 }
7004 
7005 static void napi_threaded_poll_loop(struct napi_struct *napi)
7006 {
7007 	struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx;
7008 	struct softnet_data *sd;
7009 	unsigned long last_qs = jiffies;
7010 
7011 	for (;;) {
7012 		bool repoll = false;
7013 		void *have;
7014 
7015 		local_bh_disable();
7016 		bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx);
7017 
7018 		sd = this_cpu_ptr(&softnet_data);
7019 		sd->in_napi_threaded_poll = true;
7020 
7021 		have = netpoll_poll_lock(napi);
7022 		__napi_poll(napi, &repoll);
7023 		netpoll_poll_unlock(have);
7024 
7025 		sd->in_napi_threaded_poll = false;
7026 		barrier();
7027 
7028 		if (sd_has_rps_ipi_waiting(sd)) {
7029 			local_irq_disable();
7030 			net_rps_action_and_irq_enable(sd);
7031 		}
7032 		skb_defer_free_flush(sd);
7033 		bpf_net_ctx_clear(bpf_net_ctx);
7034 		local_bh_enable();
7035 
7036 		if (!repoll)
7037 			break;
7038 
7039 		rcu_softirq_qs_periodic(last_qs);
7040 		cond_resched();
7041 	}
7042 }
7043 
7044 static int napi_threaded_poll(void *data)
7045 {
7046 	struct napi_struct *napi = data;
7047 
7048 	while (!napi_thread_wait(napi))
7049 		napi_threaded_poll_loop(napi);
7050 
7051 	return 0;
7052 }
7053 
7054 static __latent_entropy void net_rx_action(void)
7055 {
7056 	struct softnet_data *sd = this_cpu_ptr(&softnet_data);
7057 	unsigned long time_limit = jiffies +
7058 		usecs_to_jiffies(READ_ONCE(net_hotdata.netdev_budget_usecs));
7059 	struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx;
7060 	int budget = READ_ONCE(net_hotdata.netdev_budget);
7061 	LIST_HEAD(list);
7062 	LIST_HEAD(repoll);
7063 
7064 	bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx);
7065 start:
7066 	sd->in_net_rx_action = true;
7067 	local_irq_disable();
7068 	list_splice_init(&sd->poll_list, &list);
7069 	local_irq_enable();
7070 
7071 	for (;;) {
7072 		struct napi_struct *n;
7073 
7074 		skb_defer_free_flush(sd);
7075 
7076 		if (list_empty(&list)) {
7077 			if (list_empty(&repoll)) {
7078 				sd->in_net_rx_action = false;
7079 				barrier();
7080 				/* We need to check if ____napi_schedule()
7081 				 * had refilled poll_list while
7082 				 * sd->in_net_rx_action was true.
7083 				 */
7084 				if (!list_empty(&sd->poll_list))
7085 					goto start;
7086 				if (!sd_has_rps_ipi_waiting(sd))
7087 					goto end;
7088 			}
7089 			break;
7090 		}
7091 
7092 		n = list_first_entry(&list, struct napi_struct, poll_list);
7093 		budget -= napi_poll(n, &repoll);
7094 
7095 		/* If softirq window is exhausted then punt.
7096 		 * Allow this to run for 2 jiffies since which will allow
7097 		 * an average latency of 1.5/HZ.
7098 		 */
7099 		if (unlikely(budget <= 0 ||
7100 			     time_after_eq(jiffies, time_limit))) {
7101 			sd->time_squeeze++;
7102 			break;
7103 		}
7104 	}
7105 
7106 	local_irq_disable();
7107 
7108 	list_splice_tail_init(&sd->poll_list, &list);
7109 	list_splice_tail(&repoll, &list);
7110 	list_splice(&list, &sd->poll_list);
7111 	if (!list_empty(&sd->poll_list))
7112 		__raise_softirq_irqoff(NET_RX_SOFTIRQ);
7113 	else
7114 		sd->in_net_rx_action = false;
7115 
7116 	net_rps_action_and_irq_enable(sd);
7117 end:
7118 	bpf_net_ctx_clear(bpf_net_ctx);
7119 }
7120 
7121 struct netdev_adjacent {
7122 	struct net_device *dev;
7123 	netdevice_tracker dev_tracker;
7124 
7125 	/* upper master flag, there can only be one master device per list */
7126 	bool master;
7127 
7128 	/* lookup ignore flag */
7129 	bool ignore;
7130 
7131 	/* counter for the number of times this device was added to us */
7132 	u16 ref_nr;
7133 
7134 	/* private field for the users */
7135 	void *private;
7136 
7137 	struct list_head list;
7138 	struct rcu_head rcu;
7139 };
7140 
7141 static struct netdev_adjacent *__netdev_find_adj(struct net_device *adj_dev,
7142 						 struct list_head *adj_list)
7143 {
7144 	struct netdev_adjacent *adj;
7145 
7146 	list_for_each_entry(adj, adj_list, list) {
7147 		if (adj->dev == adj_dev)
7148 			return adj;
7149 	}
7150 	return NULL;
7151 }
7152 
7153 static int ____netdev_has_upper_dev(struct net_device *upper_dev,
7154 				    struct netdev_nested_priv *priv)
7155 {
7156 	struct net_device *dev = (struct net_device *)priv->data;
7157 
7158 	return upper_dev == dev;
7159 }
7160 
7161 /**
7162  * netdev_has_upper_dev - Check if device is linked to an upper device
7163  * @dev: device
7164  * @upper_dev: upper device to check
7165  *
7166  * Find out if a device is linked to specified upper device and return true
7167  * in case it is. Note that this checks only immediate upper device,
7168  * not through a complete stack of devices. The caller must hold the RTNL lock.
7169  */
7170 bool netdev_has_upper_dev(struct net_device *dev,
7171 			  struct net_device *upper_dev)
7172 {
7173 	struct netdev_nested_priv priv = {
7174 		.data = (void *)upper_dev,
7175 	};
7176 
7177 	ASSERT_RTNL();
7178 
7179 	return netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev,
7180 					     &priv);
7181 }
7182 EXPORT_SYMBOL(netdev_has_upper_dev);
7183 
7184 /**
7185  * netdev_has_upper_dev_all_rcu - Check if device is linked to an upper device
7186  * @dev: device
7187  * @upper_dev: upper device to check
7188  *
7189  * Find out if a device is linked to specified upper device and return true
7190  * in case it is. Note that this checks the entire upper device chain.
7191  * The caller must hold rcu lock.
7192  */
7193 
7194 bool netdev_has_upper_dev_all_rcu(struct net_device *dev,
7195 				  struct net_device *upper_dev)
7196 {
7197 	struct netdev_nested_priv priv = {
7198 		.data = (void *)upper_dev,
7199 	};
7200 
7201 	return !!netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev,
7202 					       &priv);
7203 }
7204 EXPORT_SYMBOL(netdev_has_upper_dev_all_rcu);
7205 
7206 /**
7207  * netdev_has_any_upper_dev - Check if device is linked to some device
7208  * @dev: device
7209  *
7210  * Find out if a device is linked to an upper device and return true in case
7211  * it is. The caller must hold the RTNL lock.
7212  */
7213 bool netdev_has_any_upper_dev(struct net_device *dev)
7214 {
7215 	ASSERT_RTNL();
7216 
7217 	return !list_empty(&dev->adj_list.upper);
7218 }
7219 EXPORT_SYMBOL(netdev_has_any_upper_dev);
7220 
7221 /**
7222  * netdev_master_upper_dev_get - Get master upper device
7223  * @dev: device
7224  *
7225  * Find a master upper device and return pointer to it or NULL in case
7226  * it's not there. The caller must hold the RTNL lock.
7227  */
7228 struct net_device *netdev_master_upper_dev_get(struct net_device *dev)
7229 {
7230 	struct netdev_adjacent *upper;
7231 
7232 	ASSERT_RTNL();
7233 
7234 	if (list_empty(&dev->adj_list.upper))
7235 		return NULL;
7236 
7237 	upper = list_first_entry(&dev->adj_list.upper,
7238 				 struct netdev_adjacent, list);
7239 	if (likely(upper->master))
7240 		return upper->dev;
7241 	return NULL;
7242 }
7243 EXPORT_SYMBOL(netdev_master_upper_dev_get);
7244 
7245 static struct net_device *__netdev_master_upper_dev_get(struct net_device *dev)
7246 {
7247 	struct netdev_adjacent *upper;
7248 
7249 	ASSERT_RTNL();
7250 
7251 	if (list_empty(&dev->adj_list.upper))
7252 		return NULL;
7253 
7254 	upper = list_first_entry(&dev->adj_list.upper,
7255 				 struct netdev_adjacent, list);
7256 	if (likely(upper->master) && !upper->ignore)
7257 		return upper->dev;
7258 	return NULL;
7259 }
7260 
7261 /**
7262  * netdev_has_any_lower_dev - Check if device is linked to some device
7263  * @dev: device
7264  *
7265  * Find out if a device is linked to a lower device and return true in case
7266  * it is. The caller must hold the RTNL lock.
7267  */
7268 static bool netdev_has_any_lower_dev(struct net_device *dev)
7269 {
7270 	ASSERT_RTNL();
7271 
7272 	return !list_empty(&dev->adj_list.lower);
7273 }
7274 
7275 void *netdev_adjacent_get_private(struct list_head *adj_list)
7276 {
7277 	struct netdev_adjacent *adj;
7278 
7279 	adj = list_entry(adj_list, struct netdev_adjacent, list);
7280 
7281 	return adj->private;
7282 }
7283 EXPORT_SYMBOL(netdev_adjacent_get_private);
7284 
7285 /**
7286  * netdev_upper_get_next_dev_rcu - Get the next dev from upper list
7287  * @dev: device
7288  * @iter: list_head ** of the current position
7289  *
7290  * Gets the next device from the dev's upper list, starting from iter
7291  * position. The caller must hold RCU read lock.
7292  */
7293 struct net_device *netdev_upper_get_next_dev_rcu(struct net_device *dev,
7294 						 struct list_head **iter)
7295 {
7296 	struct netdev_adjacent *upper;
7297 
7298 	WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
7299 
7300 	upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
7301 
7302 	if (&upper->list == &dev->adj_list.upper)
7303 		return NULL;
7304 
7305 	*iter = &upper->list;
7306 
7307 	return upper->dev;
7308 }
7309 EXPORT_SYMBOL(netdev_upper_get_next_dev_rcu);
7310 
7311 static struct net_device *__netdev_next_upper_dev(struct net_device *dev,
7312 						  struct list_head **iter,
7313 						  bool *ignore)
7314 {
7315 	struct netdev_adjacent *upper;
7316 
7317 	upper = list_entry((*iter)->next, struct netdev_adjacent, list);
7318 
7319 	if (&upper->list == &dev->adj_list.upper)
7320 		return NULL;
7321 
7322 	*iter = &upper->list;
7323 	*ignore = upper->ignore;
7324 
7325 	return upper->dev;
7326 }
7327 
7328 static struct net_device *netdev_next_upper_dev_rcu(struct net_device *dev,
7329 						    struct list_head **iter)
7330 {
7331 	struct netdev_adjacent *upper;
7332 
7333 	WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
7334 
7335 	upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
7336 
7337 	if (&upper->list == &dev->adj_list.upper)
7338 		return NULL;
7339 
7340 	*iter = &upper->list;
7341 
7342 	return upper->dev;
7343 }
7344 
7345 static int __netdev_walk_all_upper_dev(struct net_device *dev,
7346 				       int (*fn)(struct net_device *dev,
7347 					 struct netdev_nested_priv *priv),
7348 				       struct netdev_nested_priv *priv)
7349 {
7350 	struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7351 	struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7352 	int ret, cur = 0;
7353 	bool ignore;
7354 
7355 	now = dev;
7356 	iter = &dev->adj_list.upper;
7357 
7358 	while (1) {
7359 		if (now != dev) {
7360 			ret = fn(now, priv);
7361 			if (ret)
7362 				return ret;
7363 		}
7364 
7365 		next = NULL;
7366 		while (1) {
7367 			udev = __netdev_next_upper_dev(now, &iter, &ignore);
7368 			if (!udev)
7369 				break;
7370 			if (ignore)
7371 				continue;
7372 
7373 			next = udev;
7374 			niter = &udev->adj_list.upper;
7375 			dev_stack[cur] = now;
7376 			iter_stack[cur++] = iter;
7377 			break;
7378 		}
7379 
7380 		if (!next) {
7381 			if (!cur)
7382 				return 0;
7383 			next = dev_stack[--cur];
7384 			niter = iter_stack[cur];
7385 		}
7386 
7387 		now = next;
7388 		iter = niter;
7389 	}
7390 
7391 	return 0;
7392 }
7393 
7394 int netdev_walk_all_upper_dev_rcu(struct net_device *dev,
7395 				  int (*fn)(struct net_device *dev,
7396 					    struct netdev_nested_priv *priv),
7397 				  struct netdev_nested_priv *priv)
7398 {
7399 	struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7400 	struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7401 	int ret, cur = 0;
7402 
7403 	now = dev;
7404 	iter = &dev->adj_list.upper;
7405 
7406 	while (1) {
7407 		if (now != dev) {
7408 			ret = fn(now, priv);
7409 			if (ret)
7410 				return ret;
7411 		}
7412 
7413 		next = NULL;
7414 		while (1) {
7415 			udev = netdev_next_upper_dev_rcu(now, &iter);
7416 			if (!udev)
7417 				break;
7418 
7419 			next = udev;
7420 			niter = &udev->adj_list.upper;
7421 			dev_stack[cur] = now;
7422 			iter_stack[cur++] = iter;
7423 			break;
7424 		}
7425 
7426 		if (!next) {
7427 			if (!cur)
7428 				return 0;
7429 			next = dev_stack[--cur];
7430 			niter = iter_stack[cur];
7431 		}
7432 
7433 		now = next;
7434 		iter = niter;
7435 	}
7436 
7437 	return 0;
7438 }
7439 EXPORT_SYMBOL_GPL(netdev_walk_all_upper_dev_rcu);
7440 
7441 static bool __netdev_has_upper_dev(struct net_device *dev,
7442 				   struct net_device *upper_dev)
7443 {
7444 	struct netdev_nested_priv priv = {
7445 		.flags = 0,
7446 		.data = (void *)upper_dev,
7447 	};
7448 
7449 	ASSERT_RTNL();
7450 
7451 	return __netdev_walk_all_upper_dev(dev, ____netdev_has_upper_dev,
7452 					   &priv);
7453 }
7454 
7455 /**
7456  * netdev_lower_get_next_private - Get the next ->private from the
7457  *				   lower neighbour list
7458  * @dev: device
7459  * @iter: list_head ** of the current position
7460  *
7461  * Gets the next netdev_adjacent->private from the dev's lower neighbour
7462  * list, starting from iter position. The caller must hold either hold the
7463  * RTNL lock or its own locking that guarantees that the neighbour lower
7464  * list will remain unchanged.
7465  */
7466 void *netdev_lower_get_next_private(struct net_device *dev,
7467 				    struct list_head **iter)
7468 {
7469 	struct netdev_adjacent *lower;
7470 
7471 	lower = list_entry(*iter, struct netdev_adjacent, list);
7472 
7473 	if (&lower->list == &dev->adj_list.lower)
7474 		return NULL;
7475 
7476 	*iter = lower->list.next;
7477 
7478 	return lower->private;
7479 }
7480 EXPORT_SYMBOL(netdev_lower_get_next_private);
7481 
7482 /**
7483  * netdev_lower_get_next_private_rcu - Get the next ->private from the
7484  *				       lower neighbour list, RCU
7485  *				       variant
7486  * @dev: device
7487  * @iter: list_head ** of the current position
7488  *
7489  * Gets the next netdev_adjacent->private from the dev's lower neighbour
7490  * list, starting from iter position. The caller must hold RCU read lock.
7491  */
7492 void *netdev_lower_get_next_private_rcu(struct net_device *dev,
7493 					struct list_head **iter)
7494 {
7495 	struct netdev_adjacent *lower;
7496 
7497 	WARN_ON_ONCE(!rcu_read_lock_held() && !rcu_read_lock_bh_held());
7498 
7499 	lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
7500 
7501 	if (&lower->list == &dev->adj_list.lower)
7502 		return NULL;
7503 
7504 	*iter = &lower->list;
7505 
7506 	return lower->private;
7507 }
7508 EXPORT_SYMBOL(netdev_lower_get_next_private_rcu);
7509 
7510 /**
7511  * netdev_lower_get_next - Get the next device from the lower neighbour
7512  *                         list
7513  * @dev: device
7514  * @iter: list_head ** of the current position
7515  *
7516  * Gets the next netdev_adjacent from the dev's lower neighbour
7517  * list, starting from iter position. The caller must hold RTNL lock or
7518  * its own locking that guarantees that the neighbour lower
7519  * list will remain unchanged.
7520  */
7521 void *netdev_lower_get_next(struct net_device *dev, struct list_head **iter)
7522 {
7523 	struct netdev_adjacent *lower;
7524 
7525 	lower = list_entry(*iter, struct netdev_adjacent, list);
7526 
7527 	if (&lower->list == &dev->adj_list.lower)
7528 		return NULL;
7529 
7530 	*iter = lower->list.next;
7531 
7532 	return lower->dev;
7533 }
7534 EXPORT_SYMBOL(netdev_lower_get_next);
7535 
7536 static struct net_device *netdev_next_lower_dev(struct net_device *dev,
7537 						struct list_head **iter)
7538 {
7539 	struct netdev_adjacent *lower;
7540 
7541 	lower = list_entry((*iter)->next, struct netdev_adjacent, list);
7542 
7543 	if (&lower->list == &dev->adj_list.lower)
7544 		return NULL;
7545 
7546 	*iter = &lower->list;
7547 
7548 	return lower->dev;
7549 }
7550 
7551 static struct net_device *__netdev_next_lower_dev(struct net_device *dev,
7552 						  struct list_head **iter,
7553 						  bool *ignore)
7554 {
7555 	struct netdev_adjacent *lower;
7556 
7557 	lower = list_entry((*iter)->next, struct netdev_adjacent, list);
7558 
7559 	if (&lower->list == &dev->adj_list.lower)
7560 		return NULL;
7561 
7562 	*iter = &lower->list;
7563 	*ignore = lower->ignore;
7564 
7565 	return lower->dev;
7566 }
7567 
7568 int netdev_walk_all_lower_dev(struct net_device *dev,
7569 			      int (*fn)(struct net_device *dev,
7570 					struct netdev_nested_priv *priv),
7571 			      struct netdev_nested_priv *priv)
7572 {
7573 	struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7574 	struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7575 	int ret, cur = 0;
7576 
7577 	now = dev;
7578 	iter = &dev->adj_list.lower;
7579 
7580 	while (1) {
7581 		if (now != dev) {
7582 			ret = fn(now, priv);
7583 			if (ret)
7584 				return ret;
7585 		}
7586 
7587 		next = NULL;
7588 		while (1) {
7589 			ldev = netdev_next_lower_dev(now, &iter);
7590 			if (!ldev)
7591 				break;
7592 
7593 			next = ldev;
7594 			niter = &ldev->adj_list.lower;
7595 			dev_stack[cur] = now;
7596 			iter_stack[cur++] = iter;
7597 			break;
7598 		}
7599 
7600 		if (!next) {
7601 			if (!cur)
7602 				return 0;
7603 			next = dev_stack[--cur];
7604 			niter = iter_stack[cur];
7605 		}
7606 
7607 		now = next;
7608 		iter = niter;
7609 	}
7610 
7611 	return 0;
7612 }
7613 EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev);
7614 
7615 static int __netdev_walk_all_lower_dev(struct net_device *dev,
7616 				       int (*fn)(struct net_device *dev,
7617 					 struct netdev_nested_priv *priv),
7618 				       struct netdev_nested_priv *priv)
7619 {
7620 	struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7621 	struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7622 	int ret, cur = 0;
7623 	bool ignore;
7624 
7625 	now = dev;
7626 	iter = &dev->adj_list.lower;
7627 
7628 	while (1) {
7629 		if (now != dev) {
7630 			ret = fn(now, priv);
7631 			if (ret)
7632 				return ret;
7633 		}
7634 
7635 		next = NULL;
7636 		while (1) {
7637 			ldev = __netdev_next_lower_dev(now, &iter, &ignore);
7638 			if (!ldev)
7639 				break;
7640 			if (ignore)
7641 				continue;
7642 
7643 			next = ldev;
7644 			niter = &ldev->adj_list.lower;
7645 			dev_stack[cur] = now;
7646 			iter_stack[cur++] = iter;
7647 			break;
7648 		}
7649 
7650 		if (!next) {
7651 			if (!cur)
7652 				return 0;
7653 			next = dev_stack[--cur];
7654 			niter = iter_stack[cur];
7655 		}
7656 
7657 		now = next;
7658 		iter = niter;
7659 	}
7660 
7661 	return 0;
7662 }
7663 
7664 struct net_device *netdev_next_lower_dev_rcu(struct net_device *dev,
7665 					     struct list_head **iter)
7666 {
7667 	struct netdev_adjacent *lower;
7668 
7669 	lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
7670 	if (&lower->list == &dev->adj_list.lower)
7671 		return NULL;
7672 
7673 	*iter = &lower->list;
7674 
7675 	return lower->dev;
7676 }
7677 EXPORT_SYMBOL(netdev_next_lower_dev_rcu);
7678 
7679 static u8 __netdev_upper_depth(struct net_device *dev)
7680 {
7681 	struct net_device *udev;
7682 	struct list_head *iter;
7683 	u8 max_depth = 0;
7684 	bool ignore;
7685 
7686 	for (iter = &dev->adj_list.upper,
7687 	     udev = __netdev_next_upper_dev(dev, &iter, &ignore);
7688 	     udev;
7689 	     udev = __netdev_next_upper_dev(dev, &iter, &ignore)) {
7690 		if (ignore)
7691 			continue;
7692 		if (max_depth < udev->upper_level)
7693 			max_depth = udev->upper_level;
7694 	}
7695 
7696 	return max_depth;
7697 }
7698 
7699 static u8 __netdev_lower_depth(struct net_device *dev)
7700 {
7701 	struct net_device *ldev;
7702 	struct list_head *iter;
7703 	u8 max_depth = 0;
7704 	bool ignore;
7705 
7706 	for (iter = &dev->adj_list.lower,
7707 	     ldev = __netdev_next_lower_dev(dev, &iter, &ignore);
7708 	     ldev;
7709 	     ldev = __netdev_next_lower_dev(dev, &iter, &ignore)) {
7710 		if (ignore)
7711 			continue;
7712 		if (max_depth < ldev->lower_level)
7713 			max_depth = ldev->lower_level;
7714 	}
7715 
7716 	return max_depth;
7717 }
7718 
7719 static int __netdev_update_upper_level(struct net_device *dev,
7720 				       struct netdev_nested_priv *__unused)
7721 {
7722 	dev->upper_level = __netdev_upper_depth(dev) + 1;
7723 	return 0;
7724 }
7725 
7726 #ifdef CONFIG_LOCKDEP
7727 static LIST_HEAD(net_unlink_list);
7728 
7729 static void net_unlink_todo(struct net_device *dev)
7730 {
7731 	if (list_empty(&dev->unlink_list))
7732 		list_add_tail(&dev->unlink_list, &net_unlink_list);
7733 }
7734 #endif
7735 
7736 static int __netdev_update_lower_level(struct net_device *dev,
7737 				       struct netdev_nested_priv *priv)
7738 {
7739 	dev->lower_level = __netdev_lower_depth(dev) + 1;
7740 
7741 #ifdef CONFIG_LOCKDEP
7742 	if (!priv)
7743 		return 0;
7744 
7745 	if (priv->flags & NESTED_SYNC_IMM)
7746 		dev->nested_level = dev->lower_level - 1;
7747 	if (priv->flags & NESTED_SYNC_TODO)
7748 		net_unlink_todo(dev);
7749 #endif
7750 	return 0;
7751 }
7752 
7753 int netdev_walk_all_lower_dev_rcu(struct net_device *dev,
7754 				  int (*fn)(struct net_device *dev,
7755 					    struct netdev_nested_priv *priv),
7756 				  struct netdev_nested_priv *priv)
7757 {
7758 	struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7759 	struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7760 	int ret, cur = 0;
7761 
7762 	now = dev;
7763 	iter = &dev->adj_list.lower;
7764 
7765 	while (1) {
7766 		if (now != dev) {
7767 			ret = fn(now, priv);
7768 			if (ret)
7769 				return ret;
7770 		}
7771 
7772 		next = NULL;
7773 		while (1) {
7774 			ldev = netdev_next_lower_dev_rcu(now, &iter);
7775 			if (!ldev)
7776 				break;
7777 
7778 			next = ldev;
7779 			niter = &ldev->adj_list.lower;
7780 			dev_stack[cur] = now;
7781 			iter_stack[cur++] = iter;
7782 			break;
7783 		}
7784 
7785 		if (!next) {
7786 			if (!cur)
7787 				return 0;
7788 			next = dev_stack[--cur];
7789 			niter = iter_stack[cur];
7790 		}
7791 
7792 		now = next;
7793 		iter = niter;
7794 	}
7795 
7796 	return 0;
7797 }
7798 EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev_rcu);
7799 
7800 /**
7801  * netdev_lower_get_first_private_rcu - Get the first ->private from the
7802  *				       lower neighbour list, RCU
7803  *				       variant
7804  * @dev: device
7805  *
7806  * Gets the first netdev_adjacent->private from the dev's lower neighbour
7807  * list. The caller must hold RCU read lock.
7808  */
7809 void *netdev_lower_get_first_private_rcu(struct net_device *dev)
7810 {
7811 	struct netdev_adjacent *lower;
7812 
7813 	lower = list_first_or_null_rcu(&dev->adj_list.lower,
7814 			struct netdev_adjacent, list);
7815 	if (lower)
7816 		return lower->private;
7817 	return NULL;
7818 }
7819 EXPORT_SYMBOL(netdev_lower_get_first_private_rcu);
7820 
7821 /**
7822  * netdev_master_upper_dev_get_rcu - Get master upper device
7823  * @dev: device
7824  *
7825  * Find a master upper device and return pointer to it or NULL in case
7826  * it's not there. The caller must hold the RCU read lock.
7827  */
7828 struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev)
7829 {
7830 	struct netdev_adjacent *upper;
7831 
7832 	upper = list_first_or_null_rcu(&dev->adj_list.upper,
7833 				       struct netdev_adjacent, list);
7834 	if (upper && likely(upper->master))
7835 		return upper->dev;
7836 	return NULL;
7837 }
7838 EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu);
7839 
7840 static int netdev_adjacent_sysfs_add(struct net_device *dev,
7841 			      struct net_device *adj_dev,
7842 			      struct list_head *dev_list)
7843 {
7844 	char linkname[IFNAMSIZ+7];
7845 
7846 	sprintf(linkname, dev_list == &dev->adj_list.upper ?
7847 		"upper_%s" : "lower_%s", adj_dev->name);
7848 	return sysfs_create_link(&(dev->dev.kobj), &(adj_dev->dev.kobj),
7849 				 linkname);
7850 }
7851 static void netdev_adjacent_sysfs_del(struct net_device *dev,
7852 			       char *name,
7853 			       struct list_head *dev_list)
7854 {
7855 	char linkname[IFNAMSIZ+7];
7856 
7857 	sprintf(linkname, dev_list == &dev->adj_list.upper ?
7858 		"upper_%s" : "lower_%s", name);
7859 	sysfs_remove_link(&(dev->dev.kobj), linkname);
7860 }
7861 
7862 static inline bool netdev_adjacent_is_neigh_list(struct net_device *dev,
7863 						 struct net_device *adj_dev,
7864 						 struct list_head *dev_list)
7865 {
7866 	return (dev_list == &dev->adj_list.upper ||
7867 		dev_list == &dev->adj_list.lower) &&
7868 		net_eq(dev_net(dev), dev_net(adj_dev));
7869 }
7870 
7871 static int __netdev_adjacent_dev_insert(struct net_device *dev,
7872 					struct net_device *adj_dev,
7873 					struct list_head *dev_list,
7874 					void *private, bool master)
7875 {
7876 	struct netdev_adjacent *adj;
7877 	int ret;
7878 
7879 	adj = __netdev_find_adj(adj_dev, dev_list);
7880 
7881 	if (adj) {
7882 		adj->ref_nr += 1;
7883 		pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d\n",
7884 			 dev->name, adj_dev->name, adj->ref_nr);
7885 
7886 		return 0;
7887 	}
7888 
7889 	adj = kmalloc(sizeof(*adj), GFP_KERNEL);
7890 	if (!adj)
7891 		return -ENOMEM;
7892 
7893 	adj->dev = adj_dev;
7894 	adj->master = master;
7895 	adj->ref_nr = 1;
7896 	adj->private = private;
7897 	adj->ignore = false;
7898 	netdev_hold(adj_dev, &adj->dev_tracker, GFP_KERNEL);
7899 
7900 	pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d; dev_hold on %s\n",
7901 		 dev->name, adj_dev->name, adj->ref_nr, adj_dev->name);
7902 
7903 	if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) {
7904 		ret = netdev_adjacent_sysfs_add(dev, adj_dev, dev_list);
7905 		if (ret)
7906 			goto free_adj;
7907 	}
7908 
7909 	/* Ensure that master link is always the first item in list. */
7910 	if (master) {
7911 		ret = sysfs_create_link(&(dev->dev.kobj),
7912 					&(adj_dev->dev.kobj), "master");
7913 		if (ret)
7914 			goto remove_symlinks;
7915 
7916 		list_add_rcu(&adj->list, dev_list);
7917 	} else {
7918 		list_add_tail_rcu(&adj->list, dev_list);
7919 	}
7920 
7921 	return 0;
7922 
7923 remove_symlinks:
7924 	if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
7925 		netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
7926 free_adj:
7927 	netdev_put(adj_dev, &adj->dev_tracker);
7928 	kfree(adj);
7929 
7930 	return ret;
7931 }
7932 
7933 static void __netdev_adjacent_dev_remove(struct net_device *dev,
7934 					 struct net_device *adj_dev,
7935 					 u16 ref_nr,
7936 					 struct list_head *dev_list)
7937 {
7938 	struct netdev_adjacent *adj;
7939 
7940 	pr_debug("Remove adjacency: dev %s adj_dev %s ref_nr %d\n",
7941 		 dev->name, adj_dev->name, ref_nr);
7942 
7943 	adj = __netdev_find_adj(adj_dev, dev_list);
7944 
7945 	if (!adj) {
7946 		pr_err("Adjacency does not exist for device %s from %s\n",
7947 		       dev->name, adj_dev->name);
7948 		WARN_ON(1);
7949 		return;
7950 	}
7951 
7952 	if (adj->ref_nr > ref_nr) {
7953 		pr_debug("adjacency: %s to %s ref_nr - %d = %d\n",
7954 			 dev->name, adj_dev->name, ref_nr,
7955 			 adj->ref_nr - ref_nr);
7956 		adj->ref_nr -= ref_nr;
7957 		return;
7958 	}
7959 
7960 	if (adj->master)
7961 		sysfs_remove_link(&(dev->dev.kobj), "master");
7962 
7963 	if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
7964 		netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
7965 
7966 	list_del_rcu(&adj->list);
7967 	pr_debug("adjacency: dev_put for %s, because link removed from %s to %s\n",
7968 		 adj_dev->name, dev->name, adj_dev->name);
7969 	netdev_put(adj_dev, &adj->dev_tracker);
7970 	kfree_rcu(adj, rcu);
7971 }
7972 
7973 static int __netdev_adjacent_dev_link_lists(struct net_device *dev,
7974 					    struct net_device *upper_dev,
7975 					    struct list_head *up_list,
7976 					    struct list_head *down_list,
7977 					    void *private, bool master)
7978 {
7979 	int ret;
7980 
7981 	ret = __netdev_adjacent_dev_insert(dev, upper_dev, up_list,
7982 					   private, master);
7983 	if (ret)
7984 		return ret;
7985 
7986 	ret = __netdev_adjacent_dev_insert(upper_dev, dev, down_list,
7987 					   private, false);
7988 	if (ret) {
7989 		__netdev_adjacent_dev_remove(dev, upper_dev, 1, up_list);
7990 		return ret;
7991 	}
7992 
7993 	return 0;
7994 }
7995 
7996 static void __netdev_adjacent_dev_unlink_lists(struct net_device *dev,
7997 					       struct net_device *upper_dev,
7998 					       u16 ref_nr,
7999 					       struct list_head *up_list,
8000 					       struct list_head *down_list)
8001 {
8002 	__netdev_adjacent_dev_remove(dev, upper_dev, ref_nr, up_list);
8003 	__netdev_adjacent_dev_remove(upper_dev, dev, ref_nr, down_list);
8004 }
8005 
8006 static int __netdev_adjacent_dev_link_neighbour(struct net_device *dev,
8007 						struct net_device *upper_dev,
8008 						void *private, bool master)
8009 {
8010 	return __netdev_adjacent_dev_link_lists(dev, upper_dev,
8011 						&dev->adj_list.upper,
8012 						&upper_dev->adj_list.lower,
8013 						private, master);
8014 }
8015 
8016 static void __netdev_adjacent_dev_unlink_neighbour(struct net_device *dev,
8017 						   struct net_device *upper_dev)
8018 {
8019 	__netdev_adjacent_dev_unlink_lists(dev, upper_dev, 1,
8020 					   &dev->adj_list.upper,
8021 					   &upper_dev->adj_list.lower);
8022 }
8023 
8024 static int __netdev_upper_dev_link(struct net_device *dev,
8025 				   struct net_device *upper_dev, bool master,
8026 				   void *upper_priv, void *upper_info,
8027 				   struct netdev_nested_priv *priv,
8028 				   struct netlink_ext_ack *extack)
8029 {
8030 	struct netdev_notifier_changeupper_info changeupper_info = {
8031 		.info = {
8032 			.dev = dev,
8033 			.extack = extack,
8034 		},
8035 		.upper_dev = upper_dev,
8036 		.master = master,
8037 		.linking = true,
8038 		.upper_info = upper_info,
8039 	};
8040 	struct net_device *master_dev;
8041 	int ret = 0;
8042 
8043 	ASSERT_RTNL();
8044 
8045 	if (dev == upper_dev)
8046 		return -EBUSY;
8047 
8048 	/* To prevent loops, check if dev is not upper device to upper_dev. */
8049 	if (__netdev_has_upper_dev(upper_dev, dev))
8050 		return -EBUSY;
8051 
8052 	if ((dev->lower_level + upper_dev->upper_level) > MAX_NEST_DEV)
8053 		return -EMLINK;
8054 
8055 	if (!master) {
8056 		if (__netdev_has_upper_dev(dev, upper_dev))
8057 			return -EEXIST;
8058 	} else {
8059 		master_dev = __netdev_master_upper_dev_get(dev);
8060 		if (master_dev)
8061 			return master_dev == upper_dev ? -EEXIST : -EBUSY;
8062 	}
8063 
8064 	ret = call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER,
8065 					    &changeupper_info.info);
8066 	ret = notifier_to_errno(ret);
8067 	if (ret)
8068 		return ret;
8069 
8070 	ret = __netdev_adjacent_dev_link_neighbour(dev, upper_dev, upper_priv,
8071 						   master);
8072 	if (ret)
8073 		return ret;
8074 
8075 	ret = call_netdevice_notifiers_info(NETDEV_CHANGEUPPER,
8076 					    &changeupper_info.info);
8077 	ret = notifier_to_errno(ret);
8078 	if (ret)
8079 		goto rollback;
8080 
8081 	__netdev_update_upper_level(dev, NULL);
8082 	__netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL);
8083 
8084 	__netdev_update_lower_level(upper_dev, priv);
8085 	__netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level,
8086 				    priv);
8087 
8088 	return 0;
8089 
8090 rollback:
8091 	__netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
8092 
8093 	return ret;
8094 }
8095 
8096 /**
8097  * netdev_upper_dev_link - Add a link to the upper device
8098  * @dev: device
8099  * @upper_dev: new upper device
8100  * @extack: netlink extended ack
8101  *
8102  * Adds a link to device which is upper to this one. The caller must hold
8103  * the RTNL lock. On a failure a negative errno code is returned.
8104  * On success the reference counts are adjusted and the function
8105  * returns zero.
8106  */
8107 int netdev_upper_dev_link(struct net_device *dev,
8108 			  struct net_device *upper_dev,
8109 			  struct netlink_ext_ack *extack)
8110 {
8111 	struct netdev_nested_priv priv = {
8112 		.flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO,
8113 		.data = NULL,
8114 	};
8115 
8116 	return __netdev_upper_dev_link(dev, upper_dev, false,
8117 				       NULL, NULL, &priv, extack);
8118 }
8119 EXPORT_SYMBOL(netdev_upper_dev_link);
8120 
8121 /**
8122  * netdev_master_upper_dev_link - Add a master link to the upper device
8123  * @dev: device
8124  * @upper_dev: new upper device
8125  * @upper_priv: upper device private
8126  * @upper_info: upper info to be passed down via notifier
8127  * @extack: netlink extended ack
8128  *
8129  * Adds a link to device which is upper to this one. In this case, only
8130  * one master upper device can be linked, although other non-master devices
8131  * might be linked as well. The caller must hold the RTNL lock.
8132  * On a failure a negative errno code is returned. On success the reference
8133  * counts are adjusted and the function returns zero.
8134  */
8135 int netdev_master_upper_dev_link(struct net_device *dev,
8136 				 struct net_device *upper_dev,
8137 				 void *upper_priv, void *upper_info,
8138 				 struct netlink_ext_ack *extack)
8139 {
8140 	struct netdev_nested_priv priv = {
8141 		.flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO,
8142 		.data = NULL,
8143 	};
8144 
8145 	return __netdev_upper_dev_link(dev, upper_dev, true,
8146 				       upper_priv, upper_info, &priv, extack);
8147 }
8148 EXPORT_SYMBOL(netdev_master_upper_dev_link);
8149 
8150 static void __netdev_upper_dev_unlink(struct net_device *dev,
8151 				      struct net_device *upper_dev,
8152 				      struct netdev_nested_priv *priv)
8153 {
8154 	struct netdev_notifier_changeupper_info changeupper_info = {
8155 		.info = {
8156 			.dev = dev,
8157 		},
8158 		.upper_dev = upper_dev,
8159 		.linking = false,
8160 	};
8161 
8162 	ASSERT_RTNL();
8163 
8164 	changeupper_info.master = netdev_master_upper_dev_get(dev) == upper_dev;
8165 
8166 	call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER,
8167 				      &changeupper_info.info);
8168 
8169 	__netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
8170 
8171 	call_netdevice_notifiers_info(NETDEV_CHANGEUPPER,
8172 				      &changeupper_info.info);
8173 
8174 	__netdev_update_upper_level(dev, NULL);
8175 	__netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL);
8176 
8177 	__netdev_update_lower_level(upper_dev, priv);
8178 	__netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level,
8179 				    priv);
8180 }
8181 
8182 /**
8183  * netdev_upper_dev_unlink - Removes a link to upper device
8184  * @dev: device
8185  * @upper_dev: new upper device
8186  *
8187  * Removes a link to device which is upper to this one. The caller must hold
8188  * the RTNL lock.
8189  */
8190 void netdev_upper_dev_unlink(struct net_device *dev,
8191 			     struct net_device *upper_dev)
8192 {
8193 	struct netdev_nested_priv priv = {
8194 		.flags = NESTED_SYNC_TODO,
8195 		.data = NULL,
8196 	};
8197 
8198 	__netdev_upper_dev_unlink(dev, upper_dev, &priv);
8199 }
8200 EXPORT_SYMBOL(netdev_upper_dev_unlink);
8201 
8202 static void __netdev_adjacent_dev_set(struct net_device *upper_dev,
8203 				      struct net_device *lower_dev,
8204 				      bool val)
8205 {
8206 	struct netdev_adjacent *adj;
8207 
8208 	adj = __netdev_find_adj(lower_dev, &upper_dev->adj_list.lower);
8209 	if (adj)
8210 		adj->ignore = val;
8211 
8212 	adj = __netdev_find_adj(upper_dev, &lower_dev->adj_list.upper);
8213 	if (adj)
8214 		adj->ignore = val;
8215 }
8216 
8217 static void netdev_adjacent_dev_disable(struct net_device *upper_dev,
8218 					struct net_device *lower_dev)
8219 {
8220 	__netdev_adjacent_dev_set(upper_dev, lower_dev, true);
8221 }
8222 
8223 static void netdev_adjacent_dev_enable(struct net_device *upper_dev,
8224 				       struct net_device *lower_dev)
8225 {
8226 	__netdev_adjacent_dev_set(upper_dev, lower_dev, false);
8227 }
8228 
8229 int netdev_adjacent_change_prepare(struct net_device *old_dev,
8230 				   struct net_device *new_dev,
8231 				   struct net_device *dev,
8232 				   struct netlink_ext_ack *extack)
8233 {
8234 	struct netdev_nested_priv priv = {
8235 		.flags = 0,
8236 		.data = NULL,
8237 	};
8238 	int err;
8239 
8240 	if (!new_dev)
8241 		return 0;
8242 
8243 	if (old_dev && new_dev != old_dev)
8244 		netdev_adjacent_dev_disable(dev, old_dev);
8245 	err = __netdev_upper_dev_link(new_dev, dev, false, NULL, NULL, &priv,
8246 				      extack);
8247 	if (err) {
8248 		if (old_dev && new_dev != old_dev)
8249 			netdev_adjacent_dev_enable(dev, old_dev);
8250 		return err;
8251 	}
8252 
8253 	return 0;
8254 }
8255 EXPORT_SYMBOL(netdev_adjacent_change_prepare);
8256 
8257 void netdev_adjacent_change_commit(struct net_device *old_dev,
8258 				   struct net_device *new_dev,
8259 				   struct net_device *dev)
8260 {
8261 	struct netdev_nested_priv priv = {
8262 		.flags = NESTED_SYNC_IMM | NESTED_SYNC_TODO,
8263 		.data = NULL,
8264 	};
8265 
8266 	if (!new_dev || !old_dev)
8267 		return;
8268 
8269 	if (new_dev == old_dev)
8270 		return;
8271 
8272 	netdev_adjacent_dev_enable(dev, old_dev);
8273 	__netdev_upper_dev_unlink(old_dev, dev, &priv);
8274 }
8275 EXPORT_SYMBOL(netdev_adjacent_change_commit);
8276 
8277 void netdev_adjacent_change_abort(struct net_device *old_dev,
8278 				  struct net_device *new_dev,
8279 				  struct net_device *dev)
8280 {
8281 	struct netdev_nested_priv priv = {
8282 		.flags = 0,
8283 		.data = NULL,
8284 	};
8285 
8286 	if (!new_dev)
8287 		return;
8288 
8289 	if (old_dev && new_dev != old_dev)
8290 		netdev_adjacent_dev_enable(dev, old_dev);
8291 
8292 	__netdev_upper_dev_unlink(new_dev, dev, &priv);
8293 }
8294 EXPORT_SYMBOL(netdev_adjacent_change_abort);
8295 
8296 /**
8297  * netdev_bonding_info_change - Dispatch event about slave change
8298  * @dev: device
8299  * @bonding_info: info to dispatch
8300  *
8301  * Send NETDEV_BONDING_INFO to netdev notifiers with info.
8302  * The caller must hold the RTNL lock.
8303  */
8304 void netdev_bonding_info_change(struct net_device *dev,
8305 				struct netdev_bonding_info *bonding_info)
8306 {
8307 	struct netdev_notifier_bonding_info info = {
8308 		.info.dev = dev,
8309 	};
8310 
8311 	memcpy(&info.bonding_info, bonding_info,
8312 	       sizeof(struct netdev_bonding_info));
8313 	call_netdevice_notifiers_info(NETDEV_BONDING_INFO,
8314 				      &info.info);
8315 }
8316 EXPORT_SYMBOL(netdev_bonding_info_change);
8317 
8318 static int netdev_offload_xstats_enable_l3(struct net_device *dev,
8319 					   struct netlink_ext_ack *extack)
8320 {
8321 	struct netdev_notifier_offload_xstats_info info = {
8322 		.info.dev = dev,
8323 		.info.extack = extack,
8324 		.type = NETDEV_OFFLOAD_XSTATS_TYPE_L3,
8325 	};
8326 	int err;
8327 	int rc;
8328 
8329 	dev->offload_xstats_l3 = kzalloc(sizeof(*dev->offload_xstats_l3),
8330 					 GFP_KERNEL);
8331 	if (!dev->offload_xstats_l3)
8332 		return -ENOMEM;
8333 
8334 	rc = call_netdevice_notifiers_info_robust(NETDEV_OFFLOAD_XSTATS_ENABLE,
8335 						  NETDEV_OFFLOAD_XSTATS_DISABLE,
8336 						  &info.info);
8337 	err = notifier_to_errno(rc);
8338 	if (err)
8339 		goto free_stats;
8340 
8341 	return 0;
8342 
8343 free_stats:
8344 	kfree(dev->offload_xstats_l3);
8345 	dev->offload_xstats_l3 = NULL;
8346 	return err;
8347 }
8348 
8349 int netdev_offload_xstats_enable(struct net_device *dev,
8350 				 enum netdev_offload_xstats_type type,
8351 				 struct netlink_ext_ack *extack)
8352 {
8353 	ASSERT_RTNL();
8354 
8355 	if (netdev_offload_xstats_enabled(dev, type))
8356 		return -EALREADY;
8357 
8358 	switch (type) {
8359 	case NETDEV_OFFLOAD_XSTATS_TYPE_L3:
8360 		return netdev_offload_xstats_enable_l3(dev, extack);
8361 	}
8362 
8363 	WARN_ON(1);
8364 	return -EINVAL;
8365 }
8366 EXPORT_SYMBOL(netdev_offload_xstats_enable);
8367 
8368 static void netdev_offload_xstats_disable_l3(struct net_device *dev)
8369 {
8370 	struct netdev_notifier_offload_xstats_info info = {
8371 		.info.dev = dev,
8372 		.type = NETDEV_OFFLOAD_XSTATS_TYPE_L3,
8373 	};
8374 
8375 	call_netdevice_notifiers_info(NETDEV_OFFLOAD_XSTATS_DISABLE,
8376 				      &info.info);
8377 	kfree(dev->offload_xstats_l3);
8378 	dev->offload_xstats_l3 = NULL;
8379 }
8380 
8381 int netdev_offload_xstats_disable(struct net_device *dev,
8382 				  enum netdev_offload_xstats_type type)
8383 {
8384 	ASSERT_RTNL();
8385 
8386 	if (!netdev_offload_xstats_enabled(dev, type))
8387 		return -EALREADY;
8388 
8389 	switch (type) {
8390 	case NETDEV_OFFLOAD_XSTATS_TYPE_L3:
8391 		netdev_offload_xstats_disable_l3(dev);
8392 		return 0;
8393 	}
8394 
8395 	WARN_ON(1);
8396 	return -EINVAL;
8397 }
8398 EXPORT_SYMBOL(netdev_offload_xstats_disable);
8399 
8400 static void netdev_offload_xstats_disable_all(struct net_device *dev)
8401 {
8402 	netdev_offload_xstats_disable(dev, NETDEV_OFFLOAD_XSTATS_TYPE_L3);
8403 }
8404 
8405 static struct rtnl_hw_stats64 *
8406 netdev_offload_xstats_get_ptr(const struct net_device *dev,
8407 			      enum netdev_offload_xstats_type type)
8408 {
8409 	switch (type) {
8410 	case NETDEV_OFFLOAD_XSTATS_TYPE_L3:
8411 		return dev->offload_xstats_l3;
8412 	}
8413 
8414 	WARN_ON(1);
8415 	return NULL;
8416 }
8417 
8418 bool netdev_offload_xstats_enabled(const struct net_device *dev,
8419 				   enum netdev_offload_xstats_type type)
8420 {
8421 	ASSERT_RTNL();
8422 
8423 	return netdev_offload_xstats_get_ptr(dev, type);
8424 }
8425 EXPORT_SYMBOL(netdev_offload_xstats_enabled);
8426 
8427 struct netdev_notifier_offload_xstats_ru {
8428 	bool used;
8429 };
8430 
8431 struct netdev_notifier_offload_xstats_rd {
8432 	struct rtnl_hw_stats64 stats;
8433 	bool used;
8434 };
8435 
8436 static void netdev_hw_stats64_add(struct rtnl_hw_stats64 *dest,
8437 				  const struct rtnl_hw_stats64 *src)
8438 {
8439 	dest->rx_packets	  += src->rx_packets;
8440 	dest->tx_packets	  += src->tx_packets;
8441 	dest->rx_bytes		  += src->rx_bytes;
8442 	dest->tx_bytes		  += src->tx_bytes;
8443 	dest->rx_errors		  += src->rx_errors;
8444 	dest->tx_errors		  += src->tx_errors;
8445 	dest->rx_dropped	  += src->rx_dropped;
8446 	dest->tx_dropped	  += src->tx_dropped;
8447 	dest->multicast		  += src->multicast;
8448 }
8449 
8450 static int netdev_offload_xstats_get_used(struct net_device *dev,
8451 					  enum netdev_offload_xstats_type type,
8452 					  bool *p_used,
8453 					  struct netlink_ext_ack *extack)
8454 {
8455 	struct netdev_notifier_offload_xstats_ru report_used = {};
8456 	struct netdev_notifier_offload_xstats_info info = {
8457 		.info.dev = dev,
8458 		.info.extack = extack,
8459 		.type = type,
8460 		.report_used = &report_used,
8461 	};
8462 	int rc;
8463 
8464 	WARN_ON(!netdev_offload_xstats_enabled(dev, type));
8465 	rc = call_netdevice_notifiers_info(NETDEV_OFFLOAD_XSTATS_REPORT_USED,
8466 					   &info.info);
8467 	*p_used = report_used.used;
8468 	return notifier_to_errno(rc);
8469 }
8470 
8471 static int netdev_offload_xstats_get_stats(struct net_device *dev,
8472 					   enum netdev_offload_xstats_type type,
8473 					   struct rtnl_hw_stats64 *p_stats,
8474 					   bool *p_used,
8475 					   struct netlink_ext_ack *extack)
8476 {
8477 	struct netdev_notifier_offload_xstats_rd report_delta = {};
8478 	struct netdev_notifier_offload_xstats_info info = {
8479 		.info.dev = dev,
8480 		.info.extack = extack,
8481 		.type = type,
8482 		.report_delta = &report_delta,
8483 	};
8484 	struct rtnl_hw_stats64 *stats;
8485 	int rc;
8486 
8487 	stats = netdev_offload_xstats_get_ptr(dev, type);
8488 	if (WARN_ON(!stats))
8489 		return -EINVAL;
8490 
8491 	rc = call_netdevice_notifiers_info(NETDEV_OFFLOAD_XSTATS_REPORT_DELTA,
8492 					   &info.info);
8493 
8494 	/* Cache whatever we got, even if there was an error, otherwise the
8495 	 * successful stats retrievals would get lost.
8496 	 */
8497 	netdev_hw_stats64_add(stats, &report_delta.stats);
8498 
8499 	if (p_stats)
8500 		*p_stats = *stats;
8501 	*p_used = report_delta.used;
8502 
8503 	return notifier_to_errno(rc);
8504 }
8505 
8506 int netdev_offload_xstats_get(struct net_device *dev,
8507 			      enum netdev_offload_xstats_type type,
8508 			      struct rtnl_hw_stats64 *p_stats, bool *p_used,
8509 			      struct netlink_ext_ack *extack)
8510 {
8511 	ASSERT_RTNL();
8512 
8513 	if (p_stats)
8514 		return netdev_offload_xstats_get_stats(dev, type, p_stats,
8515 						       p_used, extack);
8516 	else
8517 		return netdev_offload_xstats_get_used(dev, type, p_used,
8518 						      extack);
8519 }
8520 EXPORT_SYMBOL(netdev_offload_xstats_get);
8521 
8522 void
8523 netdev_offload_xstats_report_delta(struct netdev_notifier_offload_xstats_rd *report_delta,
8524 				   const struct rtnl_hw_stats64 *stats)
8525 {
8526 	report_delta->used = true;
8527 	netdev_hw_stats64_add(&report_delta->stats, stats);
8528 }
8529 EXPORT_SYMBOL(netdev_offload_xstats_report_delta);
8530 
8531 void
8532 netdev_offload_xstats_report_used(struct netdev_notifier_offload_xstats_ru *report_used)
8533 {
8534 	report_used->used = true;
8535 }
8536 EXPORT_SYMBOL(netdev_offload_xstats_report_used);
8537 
8538 void netdev_offload_xstats_push_delta(struct net_device *dev,
8539 				      enum netdev_offload_xstats_type type,
8540 				      const struct rtnl_hw_stats64 *p_stats)
8541 {
8542 	struct rtnl_hw_stats64 *stats;
8543 
8544 	ASSERT_RTNL();
8545 
8546 	stats = netdev_offload_xstats_get_ptr(dev, type);
8547 	if (WARN_ON(!stats))
8548 		return;
8549 
8550 	netdev_hw_stats64_add(stats, p_stats);
8551 }
8552 EXPORT_SYMBOL(netdev_offload_xstats_push_delta);
8553 
8554 /**
8555  * netdev_get_xmit_slave - Get the xmit slave of master device
8556  * @dev: device
8557  * @skb: The packet
8558  * @all_slaves: assume all the slaves are active
8559  *
8560  * The reference counters are not incremented so the caller must be
8561  * careful with locks. The caller must hold RCU lock.
8562  * %NULL is returned if no slave is found.
8563  */
8564 
8565 struct net_device *netdev_get_xmit_slave(struct net_device *dev,
8566 					 struct sk_buff *skb,
8567 					 bool all_slaves)
8568 {
8569 	const struct net_device_ops *ops = dev->netdev_ops;
8570 
8571 	if (!ops->ndo_get_xmit_slave)
8572 		return NULL;
8573 	return ops->ndo_get_xmit_slave(dev, skb, all_slaves);
8574 }
8575 EXPORT_SYMBOL(netdev_get_xmit_slave);
8576 
8577 static struct net_device *netdev_sk_get_lower_dev(struct net_device *dev,
8578 						  struct sock *sk)
8579 {
8580 	const struct net_device_ops *ops = dev->netdev_ops;
8581 
8582 	if (!ops->ndo_sk_get_lower_dev)
8583 		return NULL;
8584 	return ops->ndo_sk_get_lower_dev(dev, sk);
8585 }
8586 
8587 /**
8588  * netdev_sk_get_lowest_dev - Get the lowest device in chain given device and socket
8589  * @dev: device
8590  * @sk: the socket
8591  *
8592  * %NULL is returned if no lower device is found.
8593  */
8594 
8595 struct net_device *netdev_sk_get_lowest_dev(struct net_device *dev,
8596 					    struct sock *sk)
8597 {
8598 	struct net_device *lower;
8599 
8600 	lower = netdev_sk_get_lower_dev(dev, sk);
8601 	while (lower) {
8602 		dev = lower;
8603 		lower = netdev_sk_get_lower_dev(dev, sk);
8604 	}
8605 
8606 	return dev;
8607 }
8608 EXPORT_SYMBOL(netdev_sk_get_lowest_dev);
8609 
8610 static void netdev_adjacent_add_links(struct net_device *dev)
8611 {
8612 	struct netdev_adjacent *iter;
8613 
8614 	struct net *net = dev_net(dev);
8615 
8616 	list_for_each_entry(iter, &dev->adj_list.upper, list) {
8617 		if (!net_eq(net, dev_net(iter->dev)))
8618 			continue;
8619 		netdev_adjacent_sysfs_add(iter->dev, dev,
8620 					  &iter->dev->adj_list.lower);
8621 		netdev_adjacent_sysfs_add(dev, iter->dev,
8622 					  &dev->adj_list.upper);
8623 	}
8624 
8625 	list_for_each_entry(iter, &dev->adj_list.lower, list) {
8626 		if (!net_eq(net, dev_net(iter->dev)))
8627 			continue;
8628 		netdev_adjacent_sysfs_add(iter->dev, dev,
8629 					  &iter->dev->adj_list.upper);
8630 		netdev_adjacent_sysfs_add(dev, iter->dev,
8631 					  &dev->adj_list.lower);
8632 	}
8633 }
8634 
8635 static void netdev_adjacent_del_links(struct net_device *dev)
8636 {
8637 	struct netdev_adjacent *iter;
8638 
8639 	struct net *net = dev_net(dev);
8640 
8641 	list_for_each_entry(iter, &dev->adj_list.upper, list) {
8642 		if (!net_eq(net, dev_net(iter->dev)))
8643 			continue;
8644 		netdev_adjacent_sysfs_del(iter->dev, dev->name,
8645 					  &iter->dev->adj_list.lower);
8646 		netdev_adjacent_sysfs_del(dev, iter->dev->name,
8647 					  &dev->adj_list.upper);
8648 	}
8649 
8650 	list_for_each_entry(iter, &dev->adj_list.lower, list) {
8651 		if (!net_eq(net, dev_net(iter->dev)))
8652 			continue;
8653 		netdev_adjacent_sysfs_del(iter->dev, dev->name,
8654 					  &iter->dev->adj_list.upper);
8655 		netdev_adjacent_sysfs_del(dev, iter->dev->name,
8656 					  &dev->adj_list.lower);
8657 	}
8658 }
8659 
8660 void netdev_adjacent_rename_links(struct net_device *dev, char *oldname)
8661 {
8662 	struct netdev_adjacent *iter;
8663 
8664 	struct net *net = dev_net(dev);
8665 
8666 	list_for_each_entry(iter, &dev->adj_list.upper, list) {
8667 		if (!net_eq(net, dev_net(iter->dev)))
8668 			continue;
8669 		netdev_adjacent_sysfs_del(iter->dev, oldname,
8670 					  &iter->dev->adj_list.lower);
8671 		netdev_adjacent_sysfs_add(iter->dev, dev,
8672 					  &iter->dev->adj_list.lower);
8673 	}
8674 
8675 	list_for_each_entry(iter, &dev->adj_list.lower, list) {
8676 		if (!net_eq(net, dev_net(iter->dev)))
8677 			continue;
8678 		netdev_adjacent_sysfs_del(iter->dev, oldname,
8679 					  &iter->dev->adj_list.upper);
8680 		netdev_adjacent_sysfs_add(iter->dev, dev,
8681 					  &iter->dev->adj_list.upper);
8682 	}
8683 }
8684 
8685 void *netdev_lower_dev_get_private(struct net_device *dev,
8686 				   struct net_device *lower_dev)
8687 {
8688 	struct netdev_adjacent *lower;
8689 
8690 	if (!lower_dev)
8691 		return NULL;
8692 	lower = __netdev_find_adj(lower_dev, &dev->adj_list.lower);
8693 	if (!lower)
8694 		return NULL;
8695 
8696 	return lower->private;
8697 }
8698 EXPORT_SYMBOL(netdev_lower_dev_get_private);
8699 
8700 
8701 /**
8702  * netdev_lower_state_changed - Dispatch event about lower device state change
8703  * @lower_dev: device
8704  * @lower_state_info: state to dispatch
8705  *
8706  * Send NETDEV_CHANGELOWERSTATE to netdev notifiers with info.
8707  * The caller must hold the RTNL lock.
8708  */
8709 void netdev_lower_state_changed(struct net_device *lower_dev,
8710 				void *lower_state_info)
8711 {
8712 	struct netdev_notifier_changelowerstate_info changelowerstate_info = {
8713 		.info.dev = lower_dev,
8714 	};
8715 
8716 	ASSERT_RTNL();
8717 	changelowerstate_info.lower_state_info = lower_state_info;
8718 	call_netdevice_notifiers_info(NETDEV_CHANGELOWERSTATE,
8719 				      &changelowerstate_info.info);
8720 }
8721 EXPORT_SYMBOL(netdev_lower_state_changed);
8722 
8723 static void dev_change_rx_flags(struct net_device *dev, int flags)
8724 {
8725 	const struct net_device_ops *ops = dev->netdev_ops;
8726 
8727 	if (ops->ndo_change_rx_flags)
8728 		ops->ndo_change_rx_flags(dev, flags);
8729 }
8730 
8731 static int __dev_set_promiscuity(struct net_device *dev, int inc, bool notify)
8732 {
8733 	unsigned int old_flags = dev->flags;
8734 	unsigned int promiscuity, flags;
8735 	kuid_t uid;
8736 	kgid_t gid;
8737 
8738 	ASSERT_RTNL();
8739 
8740 	promiscuity = dev->promiscuity + inc;
8741 	if (promiscuity == 0) {
8742 		/*
8743 		 * Avoid overflow.
8744 		 * If inc causes overflow, untouch promisc and return error.
8745 		 */
8746 		if (unlikely(inc > 0)) {
8747 			netdev_warn(dev, "promiscuity touches roof, set promiscuity failed. promiscuity feature of device might be broken.\n");
8748 			return -EOVERFLOW;
8749 		}
8750 		flags = old_flags & ~IFF_PROMISC;
8751 	} else {
8752 		flags = old_flags | IFF_PROMISC;
8753 	}
8754 	WRITE_ONCE(dev->promiscuity, promiscuity);
8755 	if (flags != old_flags) {
8756 		WRITE_ONCE(dev->flags, flags);
8757 		netdev_info(dev, "%s promiscuous mode\n",
8758 			    dev->flags & IFF_PROMISC ? "entered" : "left");
8759 		if (audit_enabled) {
8760 			current_uid_gid(&uid, &gid);
8761 			audit_log(audit_context(), GFP_ATOMIC,
8762 				  AUDIT_ANOM_PROMISCUOUS,
8763 				  "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u",
8764 				  dev->name, (dev->flags & IFF_PROMISC),
8765 				  (old_flags & IFF_PROMISC),
8766 				  from_kuid(&init_user_ns, audit_get_loginuid(current)),
8767 				  from_kuid(&init_user_ns, uid),
8768 				  from_kgid(&init_user_ns, gid),
8769 				  audit_get_sessionid(current));
8770 		}
8771 
8772 		dev_change_rx_flags(dev, IFF_PROMISC);
8773 	}
8774 	if (notify)
8775 		__dev_notify_flags(dev, old_flags, IFF_PROMISC, 0, NULL);
8776 	return 0;
8777 }
8778 
8779 /**
8780  *	dev_set_promiscuity	- update promiscuity count on a device
8781  *	@dev: device
8782  *	@inc: modifier
8783  *
8784  *	Add or remove promiscuity from a device. While the count in the device
8785  *	remains above zero the interface remains promiscuous. Once it hits zero
8786  *	the device reverts back to normal filtering operation. A negative inc
8787  *	value is used to drop promiscuity on the device.
8788  *	Return 0 if successful or a negative errno code on error.
8789  */
8790 int dev_set_promiscuity(struct net_device *dev, int inc)
8791 {
8792 	unsigned int old_flags = dev->flags;
8793 	int err;
8794 
8795 	err = __dev_set_promiscuity(dev, inc, true);
8796 	if (err < 0)
8797 		return err;
8798 	if (dev->flags != old_flags)
8799 		dev_set_rx_mode(dev);
8800 	return err;
8801 }
8802 EXPORT_SYMBOL(dev_set_promiscuity);
8803 
8804 static int __dev_set_allmulti(struct net_device *dev, int inc, bool notify)
8805 {
8806 	unsigned int old_flags = dev->flags, old_gflags = dev->gflags;
8807 	unsigned int allmulti, flags;
8808 
8809 	ASSERT_RTNL();
8810 
8811 	allmulti = dev->allmulti + inc;
8812 	if (allmulti == 0) {
8813 		/*
8814 		 * Avoid overflow.
8815 		 * If inc causes overflow, untouch allmulti and return error.
8816 		 */
8817 		if (unlikely(inc > 0)) {
8818 			netdev_warn(dev, "allmulti touches roof, set allmulti failed. allmulti feature of device might be broken.\n");
8819 			return -EOVERFLOW;
8820 		}
8821 		flags = old_flags & ~IFF_ALLMULTI;
8822 	} else {
8823 		flags = old_flags | IFF_ALLMULTI;
8824 	}
8825 	WRITE_ONCE(dev->allmulti, allmulti);
8826 	if (flags != old_flags) {
8827 		WRITE_ONCE(dev->flags, flags);
8828 		netdev_info(dev, "%s allmulticast mode\n",
8829 			    dev->flags & IFF_ALLMULTI ? "entered" : "left");
8830 		dev_change_rx_flags(dev, IFF_ALLMULTI);
8831 		dev_set_rx_mode(dev);
8832 		if (notify)
8833 			__dev_notify_flags(dev, old_flags,
8834 					   dev->gflags ^ old_gflags, 0, NULL);
8835 	}
8836 	return 0;
8837 }
8838 
8839 /**
8840  *	dev_set_allmulti	- update allmulti count on a device
8841  *	@dev: device
8842  *	@inc: modifier
8843  *
8844  *	Add or remove reception of all multicast frames to a device. While the
8845  *	count in the device remains above zero the interface remains listening
8846  *	to all interfaces. Once it hits zero the device reverts back to normal
8847  *	filtering operation. A negative @inc value is used to drop the counter
8848  *	when releasing a resource needing all multicasts.
8849  *	Return 0 if successful or a negative errno code on error.
8850  */
8851 
8852 int dev_set_allmulti(struct net_device *dev, int inc)
8853 {
8854 	return __dev_set_allmulti(dev, inc, true);
8855 }
8856 EXPORT_SYMBOL(dev_set_allmulti);
8857 
8858 /*
8859  *	Upload unicast and multicast address lists to device and
8860  *	configure RX filtering. When the device doesn't support unicast
8861  *	filtering it is put in promiscuous mode while unicast addresses
8862  *	are present.
8863  */
8864 void __dev_set_rx_mode(struct net_device *dev)
8865 {
8866 	const struct net_device_ops *ops = dev->netdev_ops;
8867 
8868 	/* dev_open will call this function so the list will stay sane. */
8869 	if (!(dev->flags&IFF_UP))
8870 		return;
8871 
8872 	if (!netif_device_present(dev))
8873 		return;
8874 
8875 	if (!(dev->priv_flags & IFF_UNICAST_FLT)) {
8876 		/* Unicast addresses changes may only happen under the rtnl,
8877 		 * therefore calling __dev_set_promiscuity here is safe.
8878 		 */
8879 		if (!netdev_uc_empty(dev) && !dev->uc_promisc) {
8880 			__dev_set_promiscuity(dev, 1, false);
8881 			dev->uc_promisc = true;
8882 		} else if (netdev_uc_empty(dev) && dev->uc_promisc) {
8883 			__dev_set_promiscuity(dev, -1, false);
8884 			dev->uc_promisc = false;
8885 		}
8886 	}
8887 
8888 	if (ops->ndo_set_rx_mode)
8889 		ops->ndo_set_rx_mode(dev);
8890 }
8891 
8892 void dev_set_rx_mode(struct net_device *dev)
8893 {
8894 	netif_addr_lock_bh(dev);
8895 	__dev_set_rx_mode(dev);
8896 	netif_addr_unlock_bh(dev);
8897 }
8898 
8899 /**
8900  *	dev_get_flags - get flags reported to userspace
8901  *	@dev: device
8902  *
8903  *	Get the combination of flag bits exported through APIs to userspace.
8904  */
8905 unsigned int dev_get_flags(const struct net_device *dev)
8906 {
8907 	unsigned int flags;
8908 
8909 	flags = (READ_ONCE(dev->flags) & ~(IFF_PROMISC |
8910 				IFF_ALLMULTI |
8911 				IFF_RUNNING |
8912 				IFF_LOWER_UP |
8913 				IFF_DORMANT)) |
8914 		(READ_ONCE(dev->gflags) & (IFF_PROMISC |
8915 				IFF_ALLMULTI));
8916 
8917 	if (netif_running(dev)) {
8918 		if (netif_oper_up(dev))
8919 			flags |= IFF_RUNNING;
8920 		if (netif_carrier_ok(dev))
8921 			flags |= IFF_LOWER_UP;
8922 		if (netif_dormant(dev))
8923 			flags |= IFF_DORMANT;
8924 	}
8925 
8926 	return flags;
8927 }
8928 EXPORT_SYMBOL(dev_get_flags);
8929 
8930 int __dev_change_flags(struct net_device *dev, unsigned int flags,
8931 		       struct netlink_ext_ack *extack)
8932 {
8933 	unsigned int old_flags = dev->flags;
8934 	int ret;
8935 
8936 	ASSERT_RTNL();
8937 
8938 	/*
8939 	 *	Set the flags on our device.
8940 	 */
8941 
8942 	dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
8943 			       IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
8944 			       IFF_AUTOMEDIA)) |
8945 		     (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
8946 				    IFF_ALLMULTI));
8947 
8948 	/*
8949 	 *	Load in the correct multicast list now the flags have changed.
8950 	 */
8951 
8952 	if ((old_flags ^ flags) & IFF_MULTICAST)
8953 		dev_change_rx_flags(dev, IFF_MULTICAST);
8954 
8955 	dev_set_rx_mode(dev);
8956 
8957 	/*
8958 	 *	Have we downed the interface. We handle IFF_UP ourselves
8959 	 *	according to user attempts to set it, rather than blindly
8960 	 *	setting it.
8961 	 */
8962 
8963 	ret = 0;
8964 	if ((old_flags ^ flags) & IFF_UP) {
8965 		if (old_flags & IFF_UP)
8966 			__dev_close(dev);
8967 		else
8968 			ret = __dev_open(dev, extack);
8969 	}
8970 
8971 	if ((flags ^ dev->gflags) & IFF_PROMISC) {
8972 		int inc = (flags & IFF_PROMISC) ? 1 : -1;
8973 		unsigned int old_flags = dev->flags;
8974 
8975 		dev->gflags ^= IFF_PROMISC;
8976 
8977 		if (__dev_set_promiscuity(dev, inc, false) >= 0)
8978 			if (dev->flags != old_flags)
8979 				dev_set_rx_mode(dev);
8980 	}
8981 
8982 	/* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
8983 	 * is important. Some (broken) drivers set IFF_PROMISC, when
8984 	 * IFF_ALLMULTI is requested not asking us and not reporting.
8985 	 */
8986 	if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
8987 		int inc = (flags & IFF_ALLMULTI) ? 1 : -1;
8988 
8989 		dev->gflags ^= IFF_ALLMULTI;
8990 		__dev_set_allmulti(dev, inc, false);
8991 	}
8992 
8993 	return ret;
8994 }
8995 
8996 void __dev_notify_flags(struct net_device *dev, unsigned int old_flags,
8997 			unsigned int gchanges, u32 portid,
8998 			const struct nlmsghdr *nlh)
8999 {
9000 	unsigned int changes = dev->flags ^ old_flags;
9001 
9002 	if (gchanges)
9003 		rtmsg_ifinfo(RTM_NEWLINK, dev, gchanges, GFP_ATOMIC, portid, nlh);
9004 
9005 	if (changes & IFF_UP) {
9006 		if (dev->flags & IFF_UP)
9007 			call_netdevice_notifiers(NETDEV_UP, dev);
9008 		else
9009 			call_netdevice_notifiers(NETDEV_DOWN, dev);
9010 	}
9011 
9012 	if (dev->flags & IFF_UP &&
9013 	    (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE))) {
9014 		struct netdev_notifier_change_info change_info = {
9015 			.info = {
9016 				.dev = dev,
9017 			},
9018 			.flags_changed = changes,
9019 		};
9020 
9021 		call_netdevice_notifiers_info(NETDEV_CHANGE, &change_info.info);
9022 	}
9023 }
9024 
9025 /**
9026  *	dev_change_flags - change device settings
9027  *	@dev: device
9028  *	@flags: device state flags
9029  *	@extack: netlink extended ack
9030  *
9031  *	Change settings on device based state flags. The flags are
9032  *	in the userspace exported format.
9033  */
9034 int dev_change_flags(struct net_device *dev, unsigned int flags,
9035 		     struct netlink_ext_ack *extack)
9036 {
9037 	int ret;
9038 	unsigned int changes, old_flags = dev->flags, old_gflags = dev->gflags;
9039 
9040 	ret = __dev_change_flags(dev, flags, extack);
9041 	if (ret < 0)
9042 		return ret;
9043 
9044 	changes = (old_flags ^ dev->flags) | (old_gflags ^ dev->gflags);
9045 	__dev_notify_flags(dev, old_flags, changes, 0, NULL);
9046 	return ret;
9047 }
9048 EXPORT_SYMBOL(dev_change_flags);
9049 
9050 int __dev_set_mtu(struct net_device *dev, int new_mtu)
9051 {
9052 	const struct net_device_ops *ops = dev->netdev_ops;
9053 
9054 	if (ops->ndo_change_mtu)
9055 		return ops->ndo_change_mtu(dev, new_mtu);
9056 
9057 	/* Pairs with all the lockless reads of dev->mtu in the stack */
9058 	WRITE_ONCE(dev->mtu, new_mtu);
9059 	return 0;
9060 }
9061 EXPORT_SYMBOL(__dev_set_mtu);
9062 
9063 int dev_validate_mtu(struct net_device *dev, int new_mtu,
9064 		     struct netlink_ext_ack *extack)
9065 {
9066 	/* MTU must be positive, and in range */
9067 	if (new_mtu < 0 || new_mtu < dev->min_mtu) {
9068 		NL_SET_ERR_MSG(extack, "mtu less than device minimum");
9069 		return -EINVAL;
9070 	}
9071 
9072 	if (dev->max_mtu > 0 && new_mtu > dev->max_mtu) {
9073 		NL_SET_ERR_MSG(extack, "mtu greater than device maximum");
9074 		return -EINVAL;
9075 	}
9076 	return 0;
9077 }
9078 
9079 /**
9080  *	dev_set_mtu_ext - Change maximum transfer unit
9081  *	@dev: device
9082  *	@new_mtu: new transfer unit
9083  *	@extack: netlink extended ack
9084  *
9085  *	Change the maximum transfer size of the network device.
9086  */
9087 int dev_set_mtu_ext(struct net_device *dev, int new_mtu,
9088 		    struct netlink_ext_ack *extack)
9089 {
9090 	int err, orig_mtu;
9091 
9092 	if (new_mtu == dev->mtu)
9093 		return 0;
9094 
9095 	err = dev_validate_mtu(dev, new_mtu, extack);
9096 	if (err)
9097 		return err;
9098 
9099 	if (!netif_device_present(dev))
9100 		return -ENODEV;
9101 
9102 	err = call_netdevice_notifiers(NETDEV_PRECHANGEMTU, dev);
9103 	err = notifier_to_errno(err);
9104 	if (err)
9105 		return err;
9106 
9107 	orig_mtu = dev->mtu;
9108 	err = __dev_set_mtu(dev, new_mtu);
9109 
9110 	if (!err) {
9111 		err = call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev,
9112 						   orig_mtu);
9113 		err = notifier_to_errno(err);
9114 		if (err) {
9115 			/* setting mtu back and notifying everyone again,
9116 			 * so that they have a chance to revert changes.
9117 			 */
9118 			__dev_set_mtu(dev, orig_mtu);
9119 			call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev,
9120 						     new_mtu);
9121 		}
9122 	}
9123 	return err;
9124 }
9125 
9126 int dev_set_mtu(struct net_device *dev, int new_mtu)
9127 {
9128 	struct netlink_ext_ack extack;
9129 	int err;
9130 
9131 	memset(&extack, 0, sizeof(extack));
9132 	err = dev_set_mtu_ext(dev, new_mtu, &extack);
9133 	if (err && extack._msg)
9134 		net_err_ratelimited("%s: %s\n", dev->name, extack._msg);
9135 	return err;
9136 }
9137 EXPORT_SYMBOL(dev_set_mtu);
9138 
9139 /**
9140  *	dev_change_tx_queue_len - Change TX queue length of a netdevice
9141  *	@dev: device
9142  *	@new_len: new tx queue length
9143  */
9144 int dev_change_tx_queue_len(struct net_device *dev, unsigned long new_len)
9145 {
9146 	unsigned int orig_len = dev->tx_queue_len;
9147 	int res;
9148 
9149 	if (new_len != (unsigned int)new_len)
9150 		return -ERANGE;
9151 
9152 	if (new_len != orig_len) {
9153 		WRITE_ONCE(dev->tx_queue_len, new_len);
9154 		res = call_netdevice_notifiers(NETDEV_CHANGE_TX_QUEUE_LEN, dev);
9155 		res = notifier_to_errno(res);
9156 		if (res)
9157 			goto err_rollback;
9158 		res = dev_qdisc_change_tx_queue_len(dev);
9159 		if (res)
9160 			goto err_rollback;
9161 	}
9162 
9163 	return 0;
9164 
9165 err_rollback:
9166 	netdev_err(dev, "refused to change device tx_queue_len\n");
9167 	WRITE_ONCE(dev->tx_queue_len, orig_len);
9168 	return res;
9169 }
9170 
9171 /**
9172  *	dev_set_group - Change group this device belongs to
9173  *	@dev: device
9174  *	@new_group: group this device should belong to
9175  */
9176 void dev_set_group(struct net_device *dev, int new_group)
9177 {
9178 	dev->group = new_group;
9179 }
9180 
9181 /**
9182  *	dev_pre_changeaddr_notify - Call NETDEV_PRE_CHANGEADDR.
9183  *	@dev: device
9184  *	@addr: new address
9185  *	@extack: netlink extended ack
9186  */
9187 int dev_pre_changeaddr_notify(struct net_device *dev, const char *addr,
9188 			      struct netlink_ext_ack *extack)
9189 {
9190 	struct netdev_notifier_pre_changeaddr_info info = {
9191 		.info.dev = dev,
9192 		.info.extack = extack,
9193 		.dev_addr = addr,
9194 	};
9195 	int rc;
9196 
9197 	rc = call_netdevice_notifiers_info(NETDEV_PRE_CHANGEADDR, &info.info);
9198 	return notifier_to_errno(rc);
9199 }
9200 EXPORT_SYMBOL(dev_pre_changeaddr_notify);
9201 
9202 /**
9203  *	dev_set_mac_address - Change Media Access Control Address
9204  *	@dev: device
9205  *	@sa: new address
9206  *	@extack: netlink extended ack
9207  *
9208  *	Change the hardware (MAC) address of the device
9209  */
9210 int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa,
9211 			struct netlink_ext_ack *extack)
9212 {
9213 	const struct net_device_ops *ops = dev->netdev_ops;
9214 	int err;
9215 
9216 	if (!ops->ndo_set_mac_address)
9217 		return -EOPNOTSUPP;
9218 	if (sa->sa_family != dev->type)
9219 		return -EINVAL;
9220 	if (!netif_device_present(dev))
9221 		return -ENODEV;
9222 	err = dev_pre_changeaddr_notify(dev, sa->sa_data, extack);
9223 	if (err)
9224 		return err;
9225 	if (memcmp(dev->dev_addr, sa->sa_data, dev->addr_len)) {
9226 		err = ops->ndo_set_mac_address(dev, sa);
9227 		if (err)
9228 			return err;
9229 	}
9230 	dev->addr_assign_type = NET_ADDR_SET;
9231 	call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
9232 	add_device_randomness(dev->dev_addr, dev->addr_len);
9233 	return 0;
9234 }
9235 EXPORT_SYMBOL(dev_set_mac_address);
9236 
9237 DECLARE_RWSEM(dev_addr_sem);
9238 
9239 int dev_set_mac_address_user(struct net_device *dev, struct sockaddr *sa,
9240 			     struct netlink_ext_ack *extack)
9241 {
9242 	int ret;
9243 
9244 	down_write(&dev_addr_sem);
9245 	ret = dev_set_mac_address(dev, sa, extack);
9246 	up_write(&dev_addr_sem);
9247 	return ret;
9248 }
9249 EXPORT_SYMBOL(dev_set_mac_address_user);
9250 
9251 int dev_get_mac_address(struct sockaddr *sa, struct net *net, char *dev_name)
9252 {
9253 	size_t size = sizeof(sa->sa_data_min);
9254 	struct net_device *dev;
9255 	int ret = 0;
9256 
9257 	down_read(&dev_addr_sem);
9258 	rcu_read_lock();
9259 
9260 	dev = dev_get_by_name_rcu(net, dev_name);
9261 	if (!dev) {
9262 		ret = -ENODEV;
9263 		goto unlock;
9264 	}
9265 	if (!dev->addr_len)
9266 		memset(sa->sa_data, 0, size);
9267 	else
9268 		memcpy(sa->sa_data, dev->dev_addr,
9269 		       min_t(size_t, size, dev->addr_len));
9270 	sa->sa_family = dev->type;
9271 
9272 unlock:
9273 	rcu_read_unlock();
9274 	up_read(&dev_addr_sem);
9275 	return ret;
9276 }
9277 EXPORT_SYMBOL(dev_get_mac_address);
9278 
9279 /**
9280  *	dev_change_carrier - Change device carrier
9281  *	@dev: device
9282  *	@new_carrier: new value
9283  *
9284  *	Change device carrier
9285  */
9286 int dev_change_carrier(struct net_device *dev, bool new_carrier)
9287 {
9288 	const struct net_device_ops *ops = dev->netdev_ops;
9289 
9290 	if (!ops->ndo_change_carrier)
9291 		return -EOPNOTSUPP;
9292 	if (!netif_device_present(dev))
9293 		return -ENODEV;
9294 	return ops->ndo_change_carrier(dev, new_carrier);
9295 }
9296 
9297 /**
9298  *	dev_get_phys_port_id - Get device physical port ID
9299  *	@dev: device
9300  *	@ppid: port ID
9301  *
9302  *	Get device physical port ID
9303  */
9304 int dev_get_phys_port_id(struct net_device *dev,
9305 			 struct netdev_phys_item_id *ppid)
9306 {
9307 	const struct net_device_ops *ops = dev->netdev_ops;
9308 
9309 	if (!ops->ndo_get_phys_port_id)
9310 		return -EOPNOTSUPP;
9311 	return ops->ndo_get_phys_port_id(dev, ppid);
9312 }
9313 
9314 /**
9315  *	dev_get_phys_port_name - Get device physical port name
9316  *	@dev: device
9317  *	@name: port name
9318  *	@len: limit of bytes to copy to name
9319  *
9320  *	Get device physical port name
9321  */
9322 int dev_get_phys_port_name(struct net_device *dev,
9323 			   char *name, size_t len)
9324 {
9325 	const struct net_device_ops *ops = dev->netdev_ops;
9326 	int err;
9327 
9328 	if (ops->ndo_get_phys_port_name) {
9329 		err = ops->ndo_get_phys_port_name(dev, name, len);
9330 		if (err != -EOPNOTSUPP)
9331 			return err;
9332 	}
9333 	return devlink_compat_phys_port_name_get(dev, name, len);
9334 }
9335 
9336 /**
9337  *	dev_get_port_parent_id - Get the device's port parent identifier
9338  *	@dev: network device
9339  *	@ppid: pointer to a storage for the port's parent identifier
9340  *	@recurse: allow/disallow recursion to lower devices
9341  *
9342  *	Get the devices's port parent identifier
9343  */
9344 int dev_get_port_parent_id(struct net_device *dev,
9345 			   struct netdev_phys_item_id *ppid,
9346 			   bool recurse)
9347 {
9348 	const struct net_device_ops *ops = dev->netdev_ops;
9349 	struct netdev_phys_item_id first = { };
9350 	struct net_device *lower_dev;
9351 	struct list_head *iter;
9352 	int err;
9353 
9354 	if (ops->ndo_get_port_parent_id) {
9355 		err = ops->ndo_get_port_parent_id(dev, ppid);
9356 		if (err != -EOPNOTSUPP)
9357 			return err;
9358 	}
9359 
9360 	err = devlink_compat_switch_id_get(dev, ppid);
9361 	if (!recurse || err != -EOPNOTSUPP)
9362 		return err;
9363 
9364 	netdev_for_each_lower_dev(dev, lower_dev, iter) {
9365 		err = dev_get_port_parent_id(lower_dev, ppid, true);
9366 		if (err)
9367 			break;
9368 		if (!first.id_len)
9369 			first = *ppid;
9370 		else if (memcmp(&first, ppid, sizeof(*ppid)))
9371 			return -EOPNOTSUPP;
9372 	}
9373 
9374 	return err;
9375 }
9376 EXPORT_SYMBOL(dev_get_port_parent_id);
9377 
9378 /**
9379  *	netdev_port_same_parent_id - Indicate if two network devices have
9380  *	the same port parent identifier
9381  *	@a: first network device
9382  *	@b: second network device
9383  */
9384 bool netdev_port_same_parent_id(struct net_device *a, struct net_device *b)
9385 {
9386 	struct netdev_phys_item_id a_id = { };
9387 	struct netdev_phys_item_id b_id = { };
9388 
9389 	if (dev_get_port_parent_id(a, &a_id, true) ||
9390 	    dev_get_port_parent_id(b, &b_id, true))
9391 		return false;
9392 
9393 	return netdev_phys_item_id_same(&a_id, &b_id);
9394 }
9395 EXPORT_SYMBOL(netdev_port_same_parent_id);
9396 
9397 /**
9398  *	dev_change_proto_down - set carrier according to proto_down.
9399  *
9400  *	@dev: device
9401  *	@proto_down: new value
9402  */
9403 int dev_change_proto_down(struct net_device *dev, bool proto_down)
9404 {
9405 	if (!dev->change_proto_down)
9406 		return -EOPNOTSUPP;
9407 	if (!netif_device_present(dev))
9408 		return -ENODEV;
9409 	if (proto_down)
9410 		netif_carrier_off(dev);
9411 	else
9412 		netif_carrier_on(dev);
9413 	WRITE_ONCE(dev->proto_down, proto_down);
9414 	return 0;
9415 }
9416 
9417 /**
9418  *	dev_change_proto_down_reason - proto down reason
9419  *
9420  *	@dev: device
9421  *	@mask: proto down mask
9422  *	@value: proto down value
9423  */
9424 void dev_change_proto_down_reason(struct net_device *dev, unsigned long mask,
9425 				  u32 value)
9426 {
9427 	u32 proto_down_reason;
9428 	int b;
9429 
9430 	if (!mask) {
9431 		proto_down_reason = value;
9432 	} else {
9433 		proto_down_reason = dev->proto_down_reason;
9434 		for_each_set_bit(b, &mask, 32) {
9435 			if (value & (1 << b))
9436 				proto_down_reason |= BIT(b);
9437 			else
9438 				proto_down_reason &= ~BIT(b);
9439 		}
9440 	}
9441 	WRITE_ONCE(dev->proto_down_reason, proto_down_reason);
9442 }
9443 
9444 struct bpf_xdp_link {
9445 	struct bpf_link link;
9446 	struct net_device *dev; /* protected by rtnl_lock, no refcnt held */
9447 	int flags;
9448 };
9449 
9450 static enum bpf_xdp_mode dev_xdp_mode(struct net_device *dev, u32 flags)
9451 {
9452 	if (flags & XDP_FLAGS_HW_MODE)
9453 		return XDP_MODE_HW;
9454 	if (flags & XDP_FLAGS_DRV_MODE)
9455 		return XDP_MODE_DRV;
9456 	if (flags & XDP_FLAGS_SKB_MODE)
9457 		return XDP_MODE_SKB;
9458 	return dev->netdev_ops->ndo_bpf ? XDP_MODE_DRV : XDP_MODE_SKB;
9459 }
9460 
9461 static bpf_op_t dev_xdp_bpf_op(struct net_device *dev, enum bpf_xdp_mode mode)
9462 {
9463 	switch (mode) {
9464 	case XDP_MODE_SKB:
9465 		return generic_xdp_install;
9466 	case XDP_MODE_DRV:
9467 	case XDP_MODE_HW:
9468 		return dev->netdev_ops->ndo_bpf;
9469 	default:
9470 		return NULL;
9471 	}
9472 }
9473 
9474 static struct bpf_xdp_link *dev_xdp_link(struct net_device *dev,
9475 					 enum bpf_xdp_mode mode)
9476 {
9477 	return dev->xdp_state[mode].link;
9478 }
9479 
9480 static struct bpf_prog *dev_xdp_prog(struct net_device *dev,
9481 				     enum bpf_xdp_mode mode)
9482 {
9483 	struct bpf_xdp_link *link = dev_xdp_link(dev, mode);
9484 
9485 	if (link)
9486 		return link->link.prog;
9487 	return dev->xdp_state[mode].prog;
9488 }
9489 
9490 u8 dev_xdp_prog_count(struct net_device *dev)
9491 {
9492 	u8 count = 0;
9493 	int i;
9494 
9495 	for (i = 0; i < __MAX_XDP_MODE; i++)
9496 		if (dev->xdp_state[i].prog || dev->xdp_state[i].link)
9497 			count++;
9498 	return count;
9499 }
9500 EXPORT_SYMBOL_GPL(dev_xdp_prog_count);
9501 
9502 int dev_xdp_propagate(struct net_device *dev, struct netdev_bpf *bpf)
9503 {
9504 	if (!dev->netdev_ops->ndo_bpf)
9505 		return -EOPNOTSUPP;
9506 
9507 	if (dev_get_min_mp_channel_count(dev)) {
9508 		NL_SET_ERR_MSG(bpf->extack, "unable to propagate XDP to device using memory provider");
9509 		return -EBUSY;
9510 	}
9511 
9512 	return dev->netdev_ops->ndo_bpf(dev, bpf);
9513 }
9514 EXPORT_SYMBOL_GPL(dev_xdp_propagate);
9515 
9516 u32 dev_xdp_prog_id(struct net_device *dev, enum bpf_xdp_mode mode)
9517 {
9518 	struct bpf_prog *prog = dev_xdp_prog(dev, mode);
9519 
9520 	return prog ? prog->aux->id : 0;
9521 }
9522 
9523 static void dev_xdp_set_link(struct net_device *dev, enum bpf_xdp_mode mode,
9524 			     struct bpf_xdp_link *link)
9525 {
9526 	dev->xdp_state[mode].link = link;
9527 	dev->xdp_state[mode].prog = NULL;
9528 }
9529 
9530 static void dev_xdp_set_prog(struct net_device *dev, enum bpf_xdp_mode mode,
9531 			     struct bpf_prog *prog)
9532 {
9533 	dev->xdp_state[mode].link = NULL;
9534 	dev->xdp_state[mode].prog = prog;
9535 }
9536 
9537 static int dev_xdp_install(struct net_device *dev, enum bpf_xdp_mode mode,
9538 			   bpf_op_t bpf_op, struct netlink_ext_ack *extack,
9539 			   u32 flags, struct bpf_prog *prog)
9540 {
9541 	struct netdev_bpf xdp;
9542 	int err;
9543 
9544 	if (dev_get_min_mp_channel_count(dev)) {
9545 		NL_SET_ERR_MSG(extack, "unable to install XDP to device using memory provider");
9546 		return -EBUSY;
9547 	}
9548 
9549 	memset(&xdp, 0, sizeof(xdp));
9550 	xdp.command = mode == XDP_MODE_HW ? XDP_SETUP_PROG_HW : XDP_SETUP_PROG;
9551 	xdp.extack = extack;
9552 	xdp.flags = flags;
9553 	xdp.prog = prog;
9554 
9555 	/* Drivers assume refcnt is already incremented (i.e, prog pointer is
9556 	 * "moved" into driver), so they don't increment it on their own, but
9557 	 * they do decrement refcnt when program is detached or replaced.
9558 	 * Given net_device also owns link/prog, we need to bump refcnt here
9559 	 * to prevent drivers from underflowing it.
9560 	 */
9561 	if (prog)
9562 		bpf_prog_inc(prog);
9563 	err = bpf_op(dev, &xdp);
9564 	if (err) {
9565 		if (prog)
9566 			bpf_prog_put(prog);
9567 		return err;
9568 	}
9569 
9570 	if (mode != XDP_MODE_HW)
9571 		bpf_prog_change_xdp(dev_xdp_prog(dev, mode), prog);
9572 
9573 	return 0;
9574 }
9575 
9576 static void dev_xdp_uninstall(struct net_device *dev)
9577 {
9578 	struct bpf_xdp_link *link;
9579 	struct bpf_prog *prog;
9580 	enum bpf_xdp_mode mode;
9581 	bpf_op_t bpf_op;
9582 
9583 	ASSERT_RTNL();
9584 
9585 	for (mode = XDP_MODE_SKB; mode < __MAX_XDP_MODE; mode++) {
9586 		prog = dev_xdp_prog(dev, mode);
9587 		if (!prog)
9588 			continue;
9589 
9590 		bpf_op = dev_xdp_bpf_op(dev, mode);
9591 		if (!bpf_op)
9592 			continue;
9593 
9594 		WARN_ON(dev_xdp_install(dev, mode, bpf_op, NULL, 0, NULL));
9595 
9596 		/* auto-detach link from net device */
9597 		link = dev_xdp_link(dev, mode);
9598 		if (link)
9599 			link->dev = NULL;
9600 		else
9601 			bpf_prog_put(prog);
9602 
9603 		dev_xdp_set_link(dev, mode, NULL);
9604 	}
9605 }
9606 
9607 static int dev_xdp_attach(struct net_device *dev, struct netlink_ext_ack *extack,
9608 			  struct bpf_xdp_link *link, struct bpf_prog *new_prog,
9609 			  struct bpf_prog *old_prog, u32 flags)
9610 {
9611 	unsigned int num_modes = hweight32(flags & XDP_FLAGS_MODES);
9612 	struct bpf_prog *cur_prog;
9613 	struct net_device *upper;
9614 	struct list_head *iter;
9615 	enum bpf_xdp_mode mode;
9616 	bpf_op_t bpf_op;
9617 	int err;
9618 
9619 	ASSERT_RTNL();
9620 
9621 	/* either link or prog attachment, never both */
9622 	if (link && (new_prog || old_prog))
9623 		return -EINVAL;
9624 	/* link supports only XDP mode flags */
9625 	if (link && (flags & ~XDP_FLAGS_MODES)) {
9626 		NL_SET_ERR_MSG(extack, "Invalid XDP flags for BPF link attachment");
9627 		return -EINVAL;
9628 	}
9629 	/* just one XDP mode bit should be set, zero defaults to drv/skb mode */
9630 	if (num_modes > 1) {
9631 		NL_SET_ERR_MSG(extack, "Only one XDP mode flag can be set");
9632 		return -EINVAL;
9633 	}
9634 	/* avoid ambiguity if offload + drv/skb mode progs are both loaded */
9635 	if (!num_modes && dev_xdp_prog_count(dev) > 1) {
9636 		NL_SET_ERR_MSG(extack,
9637 			       "More than one program loaded, unset mode is ambiguous");
9638 		return -EINVAL;
9639 	}
9640 	/* old_prog != NULL implies XDP_FLAGS_REPLACE is set */
9641 	if (old_prog && !(flags & XDP_FLAGS_REPLACE)) {
9642 		NL_SET_ERR_MSG(extack, "XDP_FLAGS_REPLACE is not specified");
9643 		return -EINVAL;
9644 	}
9645 
9646 	mode = dev_xdp_mode(dev, flags);
9647 	/* can't replace attached link */
9648 	if (dev_xdp_link(dev, mode)) {
9649 		NL_SET_ERR_MSG(extack, "Can't replace active BPF XDP link");
9650 		return -EBUSY;
9651 	}
9652 
9653 	/* don't allow if an upper device already has a program */
9654 	netdev_for_each_upper_dev_rcu(dev, upper, iter) {
9655 		if (dev_xdp_prog_count(upper) > 0) {
9656 			NL_SET_ERR_MSG(extack, "Cannot attach when an upper device already has a program");
9657 			return -EEXIST;
9658 		}
9659 	}
9660 
9661 	cur_prog = dev_xdp_prog(dev, mode);
9662 	/* can't replace attached prog with link */
9663 	if (link && cur_prog) {
9664 		NL_SET_ERR_MSG(extack, "Can't replace active XDP program with BPF link");
9665 		return -EBUSY;
9666 	}
9667 	if ((flags & XDP_FLAGS_REPLACE) && cur_prog != old_prog) {
9668 		NL_SET_ERR_MSG(extack, "Active program does not match expected");
9669 		return -EEXIST;
9670 	}
9671 
9672 	/* put effective new program into new_prog */
9673 	if (link)
9674 		new_prog = link->link.prog;
9675 
9676 	if (new_prog) {
9677 		bool offload = mode == XDP_MODE_HW;
9678 		enum bpf_xdp_mode other_mode = mode == XDP_MODE_SKB
9679 					       ? XDP_MODE_DRV : XDP_MODE_SKB;
9680 
9681 		if ((flags & XDP_FLAGS_UPDATE_IF_NOEXIST) && cur_prog) {
9682 			NL_SET_ERR_MSG(extack, "XDP program already attached");
9683 			return -EBUSY;
9684 		}
9685 		if (!offload && dev_xdp_prog(dev, other_mode)) {
9686 			NL_SET_ERR_MSG(extack, "Native and generic XDP can't be active at the same time");
9687 			return -EEXIST;
9688 		}
9689 		if (!offload && bpf_prog_is_offloaded(new_prog->aux)) {
9690 			NL_SET_ERR_MSG(extack, "Using offloaded program without HW_MODE flag is not supported");
9691 			return -EINVAL;
9692 		}
9693 		if (bpf_prog_is_dev_bound(new_prog->aux) && !bpf_offload_dev_match(new_prog, dev)) {
9694 			NL_SET_ERR_MSG(extack, "Program bound to different device");
9695 			return -EINVAL;
9696 		}
9697 		if (new_prog->expected_attach_type == BPF_XDP_DEVMAP) {
9698 			NL_SET_ERR_MSG(extack, "BPF_XDP_DEVMAP programs can not be attached to a device");
9699 			return -EINVAL;
9700 		}
9701 		if (new_prog->expected_attach_type == BPF_XDP_CPUMAP) {
9702 			NL_SET_ERR_MSG(extack, "BPF_XDP_CPUMAP programs can not be attached to a device");
9703 			return -EINVAL;
9704 		}
9705 	}
9706 
9707 	/* don't call drivers if the effective program didn't change */
9708 	if (new_prog != cur_prog) {
9709 		bpf_op = dev_xdp_bpf_op(dev, mode);
9710 		if (!bpf_op) {
9711 			NL_SET_ERR_MSG(extack, "Underlying driver does not support XDP in native mode");
9712 			return -EOPNOTSUPP;
9713 		}
9714 
9715 		err = dev_xdp_install(dev, mode, bpf_op, extack, flags, new_prog);
9716 		if (err)
9717 			return err;
9718 	}
9719 
9720 	if (link)
9721 		dev_xdp_set_link(dev, mode, link);
9722 	else
9723 		dev_xdp_set_prog(dev, mode, new_prog);
9724 	if (cur_prog)
9725 		bpf_prog_put(cur_prog);
9726 
9727 	return 0;
9728 }
9729 
9730 static int dev_xdp_attach_link(struct net_device *dev,
9731 			       struct netlink_ext_ack *extack,
9732 			       struct bpf_xdp_link *link)
9733 {
9734 	return dev_xdp_attach(dev, extack, link, NULL, NULL, link->flags);
9735 }
9736 
9737 static int dev_xdp_detach_link(struct net_device *dev,
9738 			       struct netlink_ext_ack *extack,
9739 			       struct bpf_xdp_link *link)
9740 {
9741 	enum bpf_xdp_mode mode;
9742 	bpf_op_t bpf_op;
9743 
9744 	ASSERT_RTNL();
9745 
9746 	mode = dev_xdp_mode(dev, link->flags);
9747 	if (dev_xdp_link(dev, mode) != link)
9748 		return -EINVAL;
9749 
9750 	bpf_op = dev_xdp_bpf_op(dev, mode);
9751 	WARN_ON(dev_xdp_install(dev, mode, bpf_op, NULL, 0, NULL));
9752 	dev_xdp_set_link(dev, mode, NULL);
9753 	return 0;
9754 }
9755 
9756 static void bpf_xdp_link_release(struct bpf_link *link)
9757 {
9758 	struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
9759 
9760 	rtnl_lock();
9761 
9762 	/* if racing with net_device's tear down, xdp_link->dev might be
9763 	 * already NULL, in which case link was already auto-detached
9764 	 */
9765 	if (xdp_link->dev) {
9766 		WARN_ON(dev_xdp_detach_link(xdp_link->dev, NULL, xdp_link));
9767 		xdp_link->dev = NULL;
9768 	}
9769 
9770 	rtnl_unlock();
9771 }
9772 
9773 static int bpf_xdp_link_detach(struct bpf_link *link)
9774 {
9775 	bpf_xdp_link_release(link);
9776 	return 0;
9777 }
9778 
9779 static void bpf_xdp_link_dealloc(struct bpf_link *link)
9780 {
9781 	struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
9782 
9783 	kfree(xdp_link);
9784 }
9785 
9786 static void bpf_xdp_link_show_fdinfo(const struct bpf_link *link,
9787 				     struct seq_file *seq)
9788 {
9789 	struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
9790 	u32 ifindex = 0;
9791 
9792 	rtnl_lock();
9793 	if (xdp_link->dev)
9794 		ifindex = xdp_link->dev->ifindex;
9795 	rtnl_unlock();
9796 
9797 	seq_printf(seq, "ifindex:\t%u\n", ifindex);
9798 }
9799 
9800 static int bpf_xdp_link_fill_link_info(const struct bpf_link *link,
9801 				       struct bpf_link_info *info)
9802 {
9803 	struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
9804 	u32 ifindex = 0;
9805 
9806 	rtnl_lock();
9807 	if (xdp_link->dev)
9808 		ifindex = xdp_link->dev->ifindex;
9809 	rtnl_unlock();
9810 
9811 	info->xdp.ifindex = ifindex;
9812 	return 0;
9813 }
9814 
9815 static int bpf_xdp_link_update(struct bpf_link *link, struct bpf_prog *new_prog,
9816 			       struct bpf_prog *old_prog)
9817 {
9818 	struct bpf_xdp_link *xdp_link = container_of(link, struct bpf_xdp_link, link);
9819 	enum bpf_xdp_mode mode;
9820 	bpf_op_t bpf_op;
9821 	int err = 0;
9822 
9823 	rtnl_lock();
9824 
9825 	/* link might have been auto-released already, so fail */
9826 	if (!xdp_link->dev) {
9827 		err = -ENOLINK;
9828 		goto out_unlock;
9829 	}
9830 
9831 	if (old_prog && link->prog != old_prog) {
9832 		err = -EPERM;
9833 		goto out_unlock;
9834 	}
9835 	old_prog = link->prog;
9836 	if (old_prog->type != new_prog->type ||
9837 	    old_prog->expected_attach_type != new_prog->expected_attach_type) {
9838 		err = -EINVAL;
9839 		goto out_unlock;
9840 	}
9841 
9842 	if (old_prog == new_prog) {
9843 		/* no-op, don't disturb drivers */
9844 		bpf_prog_put(new_prog);
9845 		goto out_unlock;
9846 	}
9847 
9848 	mode = dev_xdp_mode(xdp_link->dev, xdp_link->flags);
9849 	bpf_op = dev_xdp_bpf_op(xdp_link->dev, mode);
9850 	err = dev_xdp_install(xdp_link->dev, mode, bpf_op, NULL,
9851 			      xdp_link->flags, new_prog);
9852 	if (err)
9853 		goto out_unlock;
9854 
9855 	old_prog = xchg(&link->prog, new_prog);
9856 	bpf_prog_put(old_prog);
9857 
9858 out_unlock:
9859 	rtnl_unlock();
9860 	return err;
9861 }
9862 
9863 static const struct bpf_link_ops bpf_xdp_link_lops = {
9864 	.release = bpf_xdp_link_release,
9865 	.dealloc = bpf_xdp_link_dealloc,
9866 	.detach = bpf_xdp_link_detach,
9867 	.show_fdinfo = bpf_xdp_link_show_fdinfo,
9868 	.fill_link_info = bpf_xdp_link_fill_link_info,
9869 	.update_prog = bpf_xdp_link_update,
9870 };
9871 
9872 int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
9873 {
9874 	struct net *net = current->nsproxy->net_ns;
9875 	struct bpf_link_primer link_primer;
9876 	struct netlink_ext_ack extack = {};
9877 	struct bpf_xdp_link *link;
9878 	struct net_device *dev;
9879 	int err, fd;
9880 
9881 	rtnl_lock();
9882 	dev = dev_get_by_index(net, attr->link_create.target_ifindex);
9883 	if (!dev) {
9884 		rtnl_unlock();
9885 		return -EINVAL;
9886 	}
9887 
9888 	link = kzalloc(sizeof(*link), GFP_USER);
9889 	if (!link) {
9890 		err = -ENOMEM;
9891 		goto unlock;
9892 	}
9893 
9894 	bpf_link_init(&link->link, BPF_LINK_TYPE_XDP, &bpf_xdp_link_lops, prog);
9895 	link->dev = dev;
9896 	link->flags = attr->link_create.flags;
9897 
9898 	err = bpf_link_prime(&link->link, &link_primer);
9899 	if (err) {
9900 		kfree(link);
9901 		goto unlock;
9902 	}
9903 
9904 	err = dev_xdp_attach_link(dev, &extack, link);
9905 	rtnl_unlock();
9906 
9907 	if (err) {
9908 		link->dev = NULL;
9909 		bpf_link_cleanup(&link_primer);
9910 		trace_bpf_xdp_link_attach_failed(extack._msg);
9911 		goto out_put_dev;
9912 	}
9913 
9914 	fd = bpf_link_settle(&link_primer);
9915 	/* link itself doesn't hold dev's refcnt to not complicate shutdown */
9916 	dev_put(dev);
9917 	return fd;
9918 
9919 unlock:
9920 	rtnl_unlock();
9921 
9922 out_put_dev:
9923 	dev_put(dev);
9924 	return err;
9925 }
9926 
9927 /**
9928  *	dev_change_xdp_fd - set or clear a bpf program for a device rx path
9929  *	@dev: device
9930  *	@extack: netlink extended ack
9931  *	@fd: new program fd or negative value to clear
9932  *	@expected_fd: old program fd that userspace expects to replace or clear
9933  *	@flags: xdp-related flags
9934  *
9935  *	Set or clear a bpf program for a device
9936  */
9937 int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,
9938 		      int fd, int expected_fd, u32 flags)
9939 {
9940 	enum bpf_xdp_mode mode = dev_xdp_mode(dev, flags);
9941 	struct bpf_prog *new_prog = NULL, *old_prog = NULL;
9942 	int err;
9943 
9944 	ASSERT_RTNL();
9945 
9946 	if (fd >= 0) {
9947 		new_prog = bpf_prog_get_type_dev(fd, BPF_PROG_TYPE_XDP,
9948 						 mode != XDP_MODE_SKB);
9949 		if (IS_ERR(new_prog))
9950 			return PTR_ERR(new_prog);
9951 	}
9952 
9953 	if (expected_fd >= 0) {
9954 		old_prog = bpf_prog_get_type_dev(expected_fd, BPF_PROG_TYPE_XDP,
9955 						 mode != XDP_MODE_SKB);
9956 		if (IS_ERR(old_prog)) {
9957 			err = PTR_ERR(old_prog);
9958 			old_prog = NULL;
9959 			goto err_out;
9960 		}
9961 	}
9962 
9963 	err = dev_xdp_attach(dev, extack, NULL, new_prog, old_prog, flags);
9964 
9965 err_out:
9966 	if (err && new_prog)
9967 		bpf_prog_put(new_prog);
9968 	if (old_prog)
9969 		bpf_prog_put(old_prog);
9970 	return err;
9971 }
9972 
9973 u32 dev_get_min_mp_channel_count(const struct net_device *dev)
9974 {
9975 	int i;
9976 
9977 	ASSERT_RTNL();
9978 
9979 	for (i = dev->real_num_rx_queues - 1; i >= 0; i--)
9980 		if (dev->_rx[i].mp_params.mp_priv)
9981 			/* The channel count is the idx plus 1. */
9982 			return i + 1;
9983 
9984 	return 0;
9985 }
9986 
9987 /**
9988  * dev_index_reserve() - allocate an ifindex in a namespace
9989  * @net: the applicable net namespace
9990  * @ifindex: requested ifindex, pass %0 to get one allocated
9991  *
9992  * Allocate a ifindex for a new device. Caller must either use the ifindex
9993  * to store the device (via list_netdevice()) or call dev_index_release()
9994  * to give the index up.
9995  *
9996  * Return: a suitable unique value for a new device interface number or -errno.
9997  */
9998 static int dev_index_reserve(struct net *net, u32 ifindex)
9999 {
10000 	int err;
10001 
10002 	if (ifindex > INT_MAX) {
10003 		DEBUG_NET_WARN_ON_ONCE(1);
10004 		return -EINVAL;
10005 	}
10006 
10007 	if (!ifindex)
10008 		err = xa_alloc_cyclic(&net->dev_by_index, &ifindex, NULL,
10009 				      xa_limit_31b, &net->ifindex, GFP_KERNEL);
10010 	else
10011 		err = xa_insert(&net->dev_by_index, ifindex, NULL, GFP_KERNEL);
10012 	if (err < 0)
10013 		return err;
10014 
10015 	return ifindex;
10016 }
10017 
10018 static void dev_index_release(struct net *net, int ifindex)
10019 {
10020 	/* Expect only unused indexes, unlist_netdevice() removes the used */
10021 	WARN_ON(xa_erase(&net->dev_by_index, ifindex));
10022 }
10023 
10024 /* Delayed registration/unregisteration */
10025 LIST_HEAD(net_todo_list);
10026 DECLARE_WAIT_QUEUE_HEAD(netdev_unregistering_wq);
10027 atomic_t dev_unreg_count = ATOMIC_INIT(0);
10028 
10029 static void net_set_todo(struct net_device *dev)
10030 {
10031 	list_add_tail(&dev->todo_list, &net_todo_list);
10032 }
10033 
10034 static netdev_features_t netdev_sync_upper_features(struct net_device *lower,
10035 	struct net_device *upper, netdev_features_t features)
10036 {
10037 	netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
10038 	netdev_features_t feature;
10039 	int feature_bit;
10040 
10041 	for_each_netdev_feature(upper_disables, feature_bit) {
10042 		feature = __NETIF_F_BIT(feature_bit);
10043 		if (!(upper->wanted_features & feature)
10044 		    && (features & feature)) {
10045 			netdev_dbg(lower, "Dropping feature %pNF, upper dev %s has it off.\n",
10046 				   &feature, upper->name);
10047 			features &= ~feature;
10048 		}
10049 	}
10050 
10051 	return features;
10052 }
10053 
10054 static void netdev_sync_lower_features(struct net_device *upper,
10055 	struct net_device *lower, netdev_features_t features)
10056 {
10057 	netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
10058 	netdev_features_t feature;
10059 	int feature_bit;
10060 
10061 	for_each_netdev_feature(upper_disables, feature_bit) {
10062 		feature = __NETIF_F_BIT(feature_bit);
10063 		if (!(features & feature) && (lower->features & feature)) {
10064 			netdev_dbg(upper, "Disabling feature %pNF on lower dev %s.\n",
10065 				   &feature, lower->name);
10066 			lower->wanted_features &= ~feature;
10067 			__netdev_update_features(lower);
10068 
10069 			if (unlikely(lower->features & feature))
10070 				netdev_WARN(upper, "failed to disable %pNF on %s!\n",
10071 					    &feature, lower->name);
10072 			else
10073 				netdev_features_change(lower);
10074 		}
10075 	}
10076 }
10077 
10078 static bool netdev_has_ip_or_hw_csum(netdev_features_t features)
10079 {
10080 	netdev_features_t ip_csum_mask = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
10081 	bool ip_csum = (features & ip_csum_mask) == ip_csum_mask;
10082 	bool hw_csum = features & NETIF_F_HW_CSUM;
10083 
10084 	return ip_csum || hw_csum;
10085 }
10086 
10087 static netdev_features_t netdev_fix_features(struct net_device *dev,
10088 	netdev_features_t features)
10089 {
10090 	/* Fix illegal checksum combinations */
10091 	if ((features & NETIF_F_HW_CSUM) &&
10092 	    (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
10093 		netdev_warn(dev, "mixed HW and IP checksum settings.\n");
10094 		features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
10095 	}
10096 
10097 	/* TSO requires that SG is present as well. */
10098 	if ((features & NETIF_F_ALL_TSO) && !(features & NETIF_F_SG)) {
10099 		netdev_dbg(dev, "Dropping TSO features since no SG feature.\n");
10100 		features &= ~NETIF_F_ALL_TSO;
10101 	}
10102 
10103 	if ((features & NETIF_F_TSO) && !(features & NETIF_F_HW_CSUM) &&
10104 					!(features & NETIF_F_IP_CSUM)) {
10105 		netdev_dbg(dev, "Dropping TSO features since no CSUM feature.\n");
10106 		features &= ~NETIF_F_TSO;
10107 		features &= ~NETIF_F_TSO_ECN;
10108 	}
10109 
10110 	if ((features & NETIF_F_TSO6) && !(features & NETIF_F_HW_CSUM) &&
10111 					 !(features & NETIF_F_IPV6_CSUM)) {
10112 		netdev_dbg(dev, "Dropping TSO6 features since no CSUM feature.\n");
10113 		features &= ~NETIF_F_TSO6;
10114 	}
10115 
10116 	/* TSO with IPv4 ID mangling requires IPv4 TSO be enabled */
10117 	if ((features & NETIF_F_TSO_MANGLEID) && !(features & NETIF_F_TSO))
10118 		features &= ~NETIF_F_TSO_MANGLEID;
10119 
10120 	/* TSO ECN requires that TSO is present as well. */
10121 	if ((features & NETIF_F_ALL_TSO) == NETIF_F_TSO_ECN)
10122 		features &= ~NETIF_F_TSO_ECN;
10123 
10124 	/* Software GSO depends on SG. */
10125 	if ((features & NETIF_F_GSO) && !(features & NETIF_F_SG)) {
10126 		netdev_dbg(dev, "Dropping NETIF_F_GSO since no SG feature.\n");
10127 		features &= ~NETIF_F_GSO;
10128 	}
10129 
10130 	/* GSO partial features require GSO partial be set */
10131 	if ((features & dev->gso_partial_features) &&
10132 	    !(features & NETIF_F_GSO_PARTIAL)) {
10133 		netdev_dbg(dev,
10134 			   "Dropping partially supported GSO features since no GSO partial.\n");
10135 		features &= ~dev->gso_partial_features;
10136 	}
10137 
10138 	if (!(features & NETIF_F_RXCSUM)) {
10139 		/* NETIF_F_GRO_HW implies doing RXCSUM since every packet
10140 		 * successfully merged by hardware must also have the
10141 		 * checksum verified by hardware.  If the user does not
10142 		 * want to enable RXCSUM, logically, we should disable GRO_HW.
10143 		 */
10144 		if (features & NETIF_F_GRO_HW) {
10145 			netdev_dbg(dev, "Dropping NETIF_F_GRO_HW since no RXCSUM feature.\n");
10146 			features &= ~NETIF_F_GRO_HW;
10147 		}
10148 	}
10149 
10150 	/* LRO/HW-GRO features cannot be combined with RX-FCS */
10151 	if (features & NETIF_F_RXFCS) {
10152 		if (features & NETIF_F_LRO) {
10153 			netdev_dbg(dev, "Dropping LRO feature since RX-FCS is requested.\n");
10154 			features &= ~NETIF_F_LRO;
10155 		}
10156 
10157 		if (features & NETIF_F_GRO_HW) {
10158 			netdev_dbg(dev, "Dropping HW-GRO feature since RX-FCS is requested.\n");
10159 			features &= ~NETIF_F_GRO_HW;
10160 		}
10161 	}
10162 
10163 	if ((features & NETIF_F_GRO_HW) && (features & NETIF_F_LRO)) {
10164 		netdev_dbg(dev, "Dropping LRO feature since HW-GRO is requested.\n");
10165 		features &= ~NETIF_F_LRO;
10166 	}
10167 
10168 	if ((features & NETIF_F_HW_TLS_TX) && !netdev_has_ip_or_hw_csum(features)) {
10169 		netdev_dbg(dev, "Dropping TLS TX HW offload feature since no CSUM feature.\n");
10170 		features &= ~NETIF_F_HW_TLS_TX;
10171 	}
10172 
10173 	if ((features & NETIF_F_HW_TLS_RX) && !(features & NETIF_F_RXCSUM)) {
10174 		netdev_dbg(dev, "Dropping TLS RX HW offload feature since no RXCSUM feature.\n");
10175 		features &= ~NETIF_F_HW_TLS_RX;
10176 	}
10177 
10178 	if ((features & NETIF_F_GSO_UDP_L4) && !netdev_has_ip_or_hw_csum(features)) {
10179 		netdev_dbg(dev, "Dropping USO feature since no CSUM feature.\n");
10180 		features &= ~NETIF_F_GSO_UDP_L4;
10181 	}
10182 
10183 	return features;
10184 }
10185 
10186 int __netdev_update_features(struct net_device *dev)
10187 {
10188 	struct net_device *upper, *lower;
10189 	netdev_features_t features;
10190 	struct list_head *iter;
10191 	int err = -1;
10192 
10193 	ASSERT_RTNL();
10194 
10195 	features = netdev_get_wanted_features(dev);
10196 
10197 	if (dev->netdev_ops->ndo_fix_features)
10198 		features = dev->netdev_ops->ndo_fix_features(dev, features);
10199 
10200 	/* driver might be less strict about feature dependencies */
10201 	features = netdev_fix_features(dev, features);
10202 
10203 	/* some features can't be enabled if they're off on an upper device */
10204 	netdev_for_each_upper_dev_rcu(dev, upper, iter)
10205 		features = netdev_sync_upper_features(dev, upper, features);
10206 
10207 	if (dev->features == features)
10208 		goto sync_lower;
10209 
10210 	netdev_dbg(dev, "Features changed: %pNF -> %pNF\n",
10211 		&dev->features, &features);
10212 
10213 	if (dev->netdev_ops->ndo_set_features)
10214 		err = dev->netdev_ops->ndo_set_features(dev, features);
10215 	else
10216 		err = 0;
10217 
10218 	if (unlikely(err < 0)) {
10219 		netdev_err(dev,
10220 			"set_features() failed (%d); wanted %pNF, left %pNF\n",
10221 			err, &features, &dev->features);
10222 		/* return non-0 since some features might have changed and
10223 		 * it's better to fire a spurious notification than miss it
10224 		 */
10225 		return -1;
10226 	}
10227 
10228 sync_lower:
10229 	/* some features must be disabled on lower devices when disabled
10230 	 * on an upper device (think: bonding master or bridge)
10231 	 */
10232 	netdev_for_each_lower_dev(dev, lower, iter)
10233 		netdev_sync_lower_features(dev, lower, features);
10234 
10235 	if (!err) {
10236 		netdev_features_t diff = features ^ dev->features;
10237 
10238 		if (diff & NETIF_F_RX_UDP_TUNNEL_PORT) {
10239 			/* udp_tunnel_{get,drop}_rx_info both need
10240 			 * NETIF_F_RX_UDP_TUNNEL_PORT enabled on the
10241 			 * device, or they won't do anything.
10242 			 * Thus we need to update dev->features
10243 			 * *before* calling udp_tunnel_get_rx_info,
10244 			 * but *after* calling udp_tunnel_drop_rx_info.
10245 			 */
10246 			if (features & NETIF_F_RX_UDP_TUNNEL_PORT) {
10247 				dev->features = features;
10248 				udp_tunnel_get_rx_info(dev);
10249 			} else {
10250 				udp_tunnel_drop_rx_info(dev);
10251 			}
10252 		}
10253 
10254 		if (diff & NETIF_F_HW_VLAN_CTAG_FILTER) {
10255 			if (features & NETIF_F_HW_VLAN_CTAG_FILTER) {
10256 				dev->features = features;
10257 				err |= vlan_get_rx_ctag_filter_info(dev);
10258 			} else {
10259 				vlan_drop_rx_ctag_filter_info(dev);
10260 			}
10261 		}
10262 
10263 		if (diff & NETIF_F_HW_VLAN_STAG_FILTER) {
10264 			if (features & NETIF_F_HW_VLAN_STAG_FILTER) {
10265 				dev->features = features;
10266 				err |= vlan_get_rx_stag_filter_info(dev);
10267 			} else {
10268 				vlan_drop_rx_stag_filter_info(dev);
10269 			}
10270 		}
10271 
10272 		dev->features = features;
10273 	}
10274 
10275 	return err < 0 ? 0 : 1;
10276 }
10277 
10278 /**
10279  *	netdev_update_features - recalculate device features
10280  *	@dev: the device to check
10281  *
10282  *	Recalculate dev->features set and send notifications if it
10283  *	has changed. Should be called after driver or hardware dependent
10284  *	conditions might have changed that influence the features.
10285  */
10286 void netdev_update_features(struct net_device *dev)
10287 {
10288 	if (__netdev_update_features(dev))
10289 		netdev_features_change(dev);
10290 }
10291 EXPORT_SYMBOL(netdev_update_features);
10292 
10293 /**
10294  *	netdev_change_features - recalculate device features
10295  *	@dev: the device to check
10296  *
10297  *	Recalculate dev->features set and send notifications even
10298  *	if they have not changed. Should be called instead of
10299  *	netdev_update_features() if also dev->vlan_features might
10300  *	have changed to allow the changes to be propagated to stacked
10301  *	VLAN devices.
10302  */
10303 void netdev_change_features(struct net_device *dev)
10304 {
10305 	__netdev_update_features(dev);
10306 	netdev_features_change(dev);
10307 }
10308 EXPORT_SYMBOL(netdev_change_features);
10309 
10310 /**
10311  *	netif_stacked_transfer_operstate -	transfer operstate
10312  *	@rootdev: the root or lower level device to transfer state from
10313  *	@dev: the device to transfer operstate to
10314  *
10315  *	Transfer operational state from root to device. This is normally
10316  *	called when a stacking relationship exists between the root
10317  *	device and the device(a leaf device).
10318  */
10319 void netif_stacked_transfer_operstate(const struct net_device *rootdev,
10320 					struct net_device *dev)
10321 {
10322 	if (rootdev->operstate == IF_OPER_DORMANT)
10323 		netif_dormant_on(dev);
10324 	else
10325 		netif_dormant_off(dev);
10326 
10327 	if (rootdev->operstate == IF_OPER_TESTING)
10328 		netif_testing_on(dev);
10329 	else
10330 		netif_testing_off(dev);
10331 
10332 	if (netif_carrier_ok(rootdev))
10333 		netif_carrier_on(dev);
10334 	else
10335 		netif_carrier_off(dev);
10336 }
10337 EXPORT_SYMBOL(netif_stacked_transfer_operstate);
10338 
10339 static int netif_alloc_rx_queues(struct net_device *dev)
10340 {
10341 	unsigned int i, count = dev->num_rx_queues;
10342 	struct netdev_rx_queue *rx;
10343 	size_t sz = count * sizeof(*rx);
10344 	int err = 0;
10345 
10346 	BUG_ON(count < 1);
10347 
10348 	rx = kvzalloc(sz, GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL);
10349 	if (!rx)
10350 		return -ENOMEM;
10351 
10352 	dev->_rx = rx;
10353 
10354 	for (i = 0; i < count; i++) {
10355 		rx[i].dev = dev;
10356 
10357 		/* XDP RX-queue setup */
10358 		err = xdp_rxq_info_reg(&rx[i].xdp_rxq, dev, i, 0);
10359 		if (err < 0)
10360 			goto err_rxq_info;
10361 	}
10362 	return 0;
10363 
10364 err_rxq_info:
10365 	/* Rollback successful reg's and free other resources */
10366 	while (i--)
10367 		xdp_rxq_info_unreg(&rx[i].xdp_rxq);
10368 	kvfree(dev->_rx);
10369 	dev->_rx = NULL;
10370 	return err;
10371 }
10372 
10373 static void netif_free_rx_queues(struct net_device *dev)
10374 {
10375 	unsigned int i, count = dev->num_rx_queues;
10376 
10377 	/* netif_alloc_rx_queues alloc failed, resources have been unreg'ed */
10378 	if (!dev->_rx)
10379 		return;
10380 
10381 	for (i = 0; i < count; i++)
10382 		xdp_rxq_info_unreg(&dev->_rx[i].xdp_rxq);
10383 
10384 	kvfree(dev->_rx);
10385 }
10386 
10387 static void netdev_init_one_queue(struct net_device *dev,
10388 				  struct netdev_queue *queue, void *_unused)
10389 {
10390 	/* Initialize queue lock */
10391 	spin_lock_init(&queue->_xmit_lock);
10392 	netdev_set_xmit_lockdep_class(&queue->_xmit_lock, dev->type);
10393 	queue->xmit_lock_owner = -1;
10394 	netdev_queue_numa_node_write(queue, NUMA_NO_NODE);
10395 	queue->dev = dev;
10396 #ifdef CONFIG_BQL
10397 	dql_init(&queue->dql, HZ);
10398 #endif
10399 }
10400 
10401 static void netif_free_tx_queues(struct net_device *dev)
10402 {
10403 	kvfree(dev->_tx);
10404 }
10405 
10406 static int netif_alloc_netdev_queues(struct net_device *dev)
10407 {
10408 	unsigned int count = dev->num_tx_queues;
10409 	struct netdev_queue *tx;
10410 	size_t sz = count * sizeof(*tx);
10411 
10412 	if (count < 1 || count > 0xffff)
10413 		return -EINVAL;
10414 
10415 	tx = kvzalloc(sz, GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL);
10416 	if (!tx)
10417 		return -ENOMEM;
10418 
10419 	dev->_tx = tx;
10420 
10421 	netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL);
10422 	spin_lock_init(&dev->tx_global_lock);
10423 
10424 	return 0;
10425 }
10426 
10427 void netif_tx_stop_all_queues(struct net_device *dev)
10428 {
10429 	unsigned int i;
10430 
10431 	for (i = 0; i < dev->num_tx_queues; i++) {
10432 		struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
10433 
10434 		netif_tx_stop_queue(txq);
10435 	}
10436 }
10437 EXPORT_SYMBOL(netif_tx_stop_all_queues);
10438 
10439 static int netdev_do_alloc_pcpu_stats(struct net_device *dev)
10440 {
10441 	void __percpu *v;
10442 
10443 	/* Drivers implementing ndo_get_peer_dev must support tstat
10444 	 * accounting, so that skb_do_redirect() can bump the dev's
10445 	 * RX stats upon network namespace switch.
10446 	 */
10447 	if (dev->netdev_ops->ndo_get_peer_dev &&
10448 	    dev->pcpu_stat_type != NETDEV_PCPU_STAT_TSTATS)
10449 		return -EOPNOTSUPP;
10450 
10451 	switch (dev->pcpu_stat_type) {
10452 	case NETDEV_PCPU_STAT_NONE:
10453 		return 0;
10454 	case NETDEV_PCPU_STAT_LSTATS:
10455 		v = dev->lstats = netdev_alloc_pcpu_stats(struct pcpu_lstats);
10456 		break;
10457 	case NETDEV_PCPU_STAT_TSTATS:
10458 		v = dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
10459 		break;
10460 	case NETDEV_PCPU_STAT_DSTATS:
10461 		v = dev->dstats = netdev_alloc_pcpu_stats(struct pcpu_dstats);
10462 		break;
10463 	default:
10464 		return -EINVAL;
10465 	}
10466 
10467 	return v ? 0 : -ENOMEM;
10468 }
10469 
10470 static void netdev_do_free_pcpu_stats(struct net_device *dev)
10471 {
10472 	switch (dev->pcpu_stat_type) {
10473 	case NETDEV_PCPU_STAT_NONE:
10474 		return;
10475 	case NETDEV_PCPU_STAT_LSTATS:
10476 		free_percpu(dev->lstats);
10477 		break;
10478 	case NETDEV_PCPU_STAT_TSTATS:
10479 		free_percpu(dev->tstats);
10480 		break;
10481 	case NETDEV_PCPU_STAT_DSTATS:
10482 		free_percpu(dev->dstats);
10483 		break;
10484 	}
10485 }
10486 
10487 static void netdev_free_phy_link_topology(struct net_device *dev)
10488 {
10489 	struct phy_link_topology *topo = dev->link_topo;
10490 
10491 	if (IS_ENABLED(CONFIG_PHYLIB) && topo) {
10492 		xa_destroy(&topo->phys);
10493 		kfree(topo);
10494 		dev->link_topo = NULL;
10495 	}
10496 }
10497 
10498 /**
10499  * register_netdevice() - register a network device
10500  * @dev: device to register
10501  *
10502  * Take a prepared network device structure and make it externally accessible.
10503  * A %NETDEV_REGISTER message is sent to the netdev notifier chain.
10504  * Callers must hold the rtnl lock - you may want register_netdev()
10505  * instead of this.
10506  */
10507 int register_netdevice(struct net_device *dev)
10508 {
10509 	int ret;
10510 	struct net *net = dev_net(dev);
10511 
10512 	BUILD_BUG_ON(sizeof(netdev_features_t) * BITS_PER_BYTE <
10513 		     NETDEV_FEATURE_COUNT);
10514 	BUG_ON(dev_boot_phase);
10515 	ASSERT_RTNL();
10516 
10517 	might_sleep();
10518 
10519 	/* When net_device's are persistent, this will be fatal. */
10520 	BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
10521 	BUG_ON(!net);
10522 
10523 	ret = ethtool_check_ops(dev->ethtool_ops);
10524 	if (ret)
10525 		return ret;
10526 
10527 	/* rss ctx ID 0 is reserved for the default context, start from 1 */
10528 	xa_init_flags(&dev->ethtool->rss_ctx, XA_FLAGS_ALLOC1);
10529 	mutex_init(&dev->ethtool->rss_lock);
10530 
10531 	spin_lock_init(&dev->addr_list_lock);
10532 	netdev_set_addr_lockdep_class(dev);
10533 
10534 	ret = dev_get_valid_name(net, dev, dev->name);
10535 	if (ret < 0)
10536 		goto out;
10537 
10538 	ret = -ENOMEM;
10539 	dev->name_node = netdev_name_node_head_alloc(dev);
10540 	if (!dev->name_node)
10541 		goto out;
10542 
10543 	/* Init, if this function is available */
10544 	if (dev->netdev_ops->ndo_init) {
10545 		ret = dev->netdev_ops->ndo_init(dev);
10546 		if (ret) {
10547 			if (ret > 0)
10548 				ret = -EIO;
10549 			goto err_free_name;
10550 		}
10551 	}
10552 
10553 	if (((dev->hw_features | dev->features) &
10554 	     NETIF_F_HW_VLAN_CTAG_FILTER) &&
10555 	    (!dev->netdev_ops->ndo_vlan_rx_add_vid ||
10556 	     !dev->netdev_ops->ndo_vlan_rx_kill_vid)) {
10557 		netdev_WARN(dev, "Buggy VLAN acceleration in driver!\n");
10558 		ret = -EINVAL;
10559 		goto err_uninit;
10560 	}
10561 
10562 	ret = netdev_do_alloc_pcpu_stats(dev);
10563 	if (ret)
10564 		goto err_uninit;
10565 
10566 	ret = dev_index_reserve(net, dev->ifindex);
10567 	if (ret < 0)
10568 		goto err_free_pcpu;
10569 	dev->ifindex = ret;
10570 
10571 	/* Transfer changeable features to wanted_features and enable
10572 	 * software offloads (GSO and GRO).
10573 	 */
10574 	dev->hw_features |= (NETIF_F_SOFT_FEATURES | NETIF_F_SOFT_FEATURES_OFF);
10575 	dev->features |= NETIF_F_SOFT_FEATURES;
10576 
10577 	if (dev->udp_tunnel_nic_info) {
10578 		dev->features |= NETIF_F_RX_UDP_TUNNEL_PORT;
10579 		dev->hw_features |= NETIF_F_RX_UDP_TUNNEL_PORT;
10580 	}
10581 
10582 	dev->wanted_features = dev->features & dev->hw_features;
10583 
10584 	if (!(dev->flags & IFF_LOOPBACK))
10585 		dev->hw_features |= NETIF_F_NOCACHE_COPY;
10586 
10587 	/* If IPv4 TCP segmentation offload is supported we should also
10588 	 * allow the device to enable segmenting the frame with the option
10589 	 * of ignoring a static IP ID value.  This doesn't enable the
10590 	 * feature itself but allows the user to enable it later.
10591 	 */
10592 	if (dev->hw_features & NETIF_F_TSO)
10593 		dev->hw_features |= NETIF_F_TSO_MANGLEID;
10594 	if (dev->vlan_features & NETIF_F_TSO)
10595 		dev->vlan_features |= NETIF_F_TSO_MANGLEID;
10596 	if (dev->mpls_features & NETIF_F_TSO)
10597 		dev->mpls_features |= NETIF_F_TSO_MANGLEID;
10598 	if (dev->hw_enc_features & NETIF_F_TSO)
10599 		dev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
10600 
10601 	/* Make NETIF_F_HIGHDMA inheritable to VLAN devices.
10602 	 */
10603 	dev->vlan_features |= NETIF_F_HIGHDMA;
10604 
10605 	/* Make NETIF_F_SG inheritable to tunnel devices.
10606 	 */
10607 	dev->hw_enc_features |= NETIF_F_SG | NETIF_F_GSO_PARTIAL;
10608 
10609 	/* Make NETIF_F_SG inheritable to MPLS.
10610 	 */
10611 	dev->mpls_features |= NETIF_F_SG;
10612 
10613 	ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev);
10614 	ret = notifier_to_errno(ret);
10615 	if (ret)
10616 		goto err_ifindex_release;
10617 
10618 	ret = netdev_register_kobject(dev);
10619 
10620 	WRITE_ONCE(dev->reg_state, ret ? NETREG_UNREGISTERED : NETREG_REGISTERED);
10621 
10622 	if (ret)
10623 		goto err_uninit_notify;
10624 
10625 	__netdev_update_features(dev);
10626 
10627 	/*
10628 	 *	Default initial state at registry is that the
10629 	 *	device is present.
10630 	 */
10631 
10632 	set_bit(__LINK_STATE_PRESENT, &dev->state);
10633 
10634 	linkwatch_init_dev(dev);
10635 
10636 	dev_init_scheduler(dev);
10637 
10638 	netdev_hold(dev, &dev->dev_registered_tracker, GFP_KERNEL);
10639 	list_netdevice(dev);
10640 
10641 	add_device_randomness(dev->dev_addr, dev->addr_len);
10642 
10643 	/* If the device has permanent device address, driver should
10644 	 * set dev_addr and also addr_assign_type should be set to
10645 	 * NET_ADDR_PERM (default value).
10646 	 */
10647 	if (dev->addr_assign_type == NET_ADDR_PERM)
10648 		memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
10649 
10650 	/* Notify protocols, that a new device appeared. */
10651 	ret = call_netdevice_notifiers(NETDEV_REGISTER, dev);
10652 	ret = notifier_to_errno(ret);
10653 	if (ret) {
10654 		/* Expect explicit free_netdev() on failure */
10655 		dev->needs_free_netdev = false;
10656 		unregister_netdevice_queue(dev, NULL);
10657 		goto out;
10658 	}
10659 	/*
10660 	 *	Prevent userspace races by waiting until the network
10661 	 *	device is fully setup before sending notifications.
10662 	 */
10663 	if (!dev->rtnl_link_ops ||
10664 	    dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
10665 		rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL, 0, NULL);
10666 
10667 out:
10668 	return ret;
10669 
10670 err_uninit_notify:
10671 	call_netdevice_notifiers(NETDEV_PRE_UNINIT, dev);
10672 err_ifindex_release:
10673 	dev_index_release(net, dev->ifindex);
10674 err_free_pcpu:
10675 	netdev_do_free_pcpu_stats(dev);
10676 err_uninit:
10677 	if (dev->netdev_ops->ndo_uninit)
10678 		dev->netdev_ops->ndo_uninit(dev);
10679 	if (dev->priv_destructor)
10680 		dev->priv_destructor(dev);
10681 err_free_name:
10682 	netdev_name_node_free(dev->name_node);
10683 	goto out;
10684 }
10685 EXPORT_SYMBOL(register_netdevice);
10686 
10687 /* Initialize the core of a dummy net device.
10688  * This is useful if you are calling this function after alloc_netdev(),
10689  * since it does not memset the net_device fields.
10690  */
10691 static void init_dummy_netdev_core(struct net_device *dev)
10692 {
10693 	/* make sure we BUG if trying to hit standard
10694 	 * register/unregister code path
10695 	 */
10696 	dev->reg_state = NETREG_DUMMY;
10697 
10698 	/* NAPI wants this */
10699 	INIT_LIST_HEAD(&dev->napi_list);
10700 
10701 	/* a dummy interface is started by default */
10702 	set_bit(__LINK_STATE_PRESENT, &dev->state);
10703 	set_bit(__LINK_STATE_START, &dev->state);
10704 
10705 	/* napi_busy_loop stats accounting wants this */
10706 	dev_net_set(dev, &init_net);
10707 
10708 	/* Note : We dont allocate pcpu_refcnt for dummy devices,
10709 	 * because users of this 'device' dont need to change
10710 	 * its refcount.
10711 	 */
10712 }
10713 
10714 /**
10715  *	init_dummy_netdev	- init a dummy network device for NAPI
10716  *	@dev: device to init
10717  *
10718  *	This takes a network device structure and initializes the minimum
10719  *	amount of fields so it can be used to schedule NAPI polls without
10720  *	registering a full blown interface. This is to be used by drivers
10721  *	that need to tie several hardware interfaces to a single NAPI
10722  *	poll scheduler due to HW limitations.
10723  */
10724 void init_dummy_netdev(struct net_device *dev)
10725 {
10726 	/* Clear everything. Note we don't initialize spinlocks
10727 	 * as they aren't supposed to be taken by any of the
10728 	 * NAPI code and this dummy netdev is supposed to be
10729 	 * only ever used for NAPI polls
10730 	 */
10731 	memset(dev, 0, sizeof(struct net_device));
10732 	init_dummy_netdev_core(dev);
10733 }
10734 EXPORT_SYMBOL_GPL(init_dummy_netdev);
10735 
10736 /**
10737  *	register_netdev	- register a network device
10738  *	@dev: device to register
10739  *
10740  *	Take a completed network device structure and add it to the kernel
10741  *	interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
10742  *	chain. 0 is returned on success. A negative errno code is returned
10743  *	on a failure to set up the device, or if the name is a duplicate.
10744  *
10745  *	This is a wrapper around register_netdevice that takes the rtnl semaphore
10746  *	and expands the device name if you passed a format string to
10747  *	alloc_netdev.
10748  */
10749 int register_netdev(struct net_device *dev)
10750 {
10751 	struct net *net = dev_net(dev);
10752 	int err;
10753 
10754 	if (rtnl_net_lock_killable(net))
10755 		return -EINTR;
10756 
10757 	err = register_netdevice(dev);
10758 
10759 	rtnl_net_unlock(net);
10760 
10761 	return err;
10762 }
10763 EXPORT_SYMBOL(register_netdev);
10764 
10765 int netdev_refcnt_read(const struct net_device *dev)
10766 {
10767 #ifdef CONFIG_PCPU_DEV_REFCNT
10768 	int i, refcnt = 0;
10769 
10770 	for_each_possible_cpu(i)
10771 		refcnt += *per_cpu_ptr(dev->pcpu_refcnt, i);
10772 	return refcnt;
10773 #else
10774 	return refcount_read(&dev->dev_refcnt);
10775 #endif
10776 }
10777 EXPORT_SYMBOL(netdev_refcnt_read);
10778 
10779 int netdev_unregister_timeout_secs __read_mostly = 10;
10780 
10781 #define WAIT_REFS_MIN_MSECS 1
10782 #define WAIT_REFS_MAX_MSECS 250
10783 /**
10784  * netdev_wait_allrefs_any - wait until all references are gone.
10785  * @list: list of net_devices to wait on
10786  *
10787  * This is called when unregistering network devices.
10788  *
10789  * Any protocol or device that holds a reference should register
10790  * for netdevice notification, and cleanup and put back the
10791  * reference if they receive an UNREGISTER event.
10792  * We can get stuck here if buggy protocols don't correctly
10793  * call dev_put.
10794  */
10795 static struct net_device *netdev_wait_allrefs_any(struct list_head *list)
10796 {
10797 	unsigned long rebroadcast_time, warning_time;
10798 	struct net_device *dev;
10799 	int wait = 0;
10800 
10801 	rebroadcast_time = warning_time = jiffies;
10802 
10803 	list_for_each_entry(dev, list, todo_list)
10804 		if (netdev_refcnt_read(dev) == 1)
10805 			return dev;
10806 
10807 	while (true) {
10808 		if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
10809 			rtnl_lock();
10810 
10811 			/* Rebroadcast unregister notification */
10812 			list_for_each_entry(dev, list, todo_list)
10813 				call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
10814 
10815 			__rtnl_unlock();
10816 			rcu_barrier();
10817 			rtnl_lock();
10818 
10819 			list_for_each_entry(dev, list, todo_list)
10820 				if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
10821 					     &dev->state)) {
10822 					/* We must not have linkwatch events
10823 					 * pending on unregister. If this
10824 					 * happens, we simply run the queue
10825 					 * unscheduled, resulting in a noop
10826 					 * for this device.
10827 					 */
10828 					linkwatch_run_queue();
10829 					break;
10830 				}
10831 
10832 			__rtnl_unlock();
10833 
10834 			rebroadcast_time = jiffies;
10835 		}
10836 
10837 		rcu_barrier();
10838 
10839 		if (!wait) {
10840 			wait = WAIT_REFS_MIN_MSECS;
10841 		} else {
10842 			msleep(wait);
10843 			wait = min(wait << 1, WAIT_REFS_MAX_MSECS);
10844 		}
10845 
10846 		list_for_each_entry(dev, list, todo_list)
10847 			if (netdev_refcnt_read(dev) == 1)
10848 				return dev;
10849 
10850 		if (time_after(jiffies, warning_time +
10851 			       READ_ONCE(netdev_unregister_timeout_secs) * HZ)) {
10852 			list_for_each_entry(dev, list, todo_list) {
10853 				pr_emerg("unregister_netdevice: waiting for %s to become free. Usage count = %d\n",
10854 					 dev->name, netdev_refcnt_read(dev));
10855 				ref_tracker_dir_print(&dev->refcnt_tracker, 10);
10856 			}
10857 
10858 			warning_time = jiffies;
10859 		}
10860 	}
10861 }
10862 
10863 /* The sequence is:
10864  *
10865  *	rtnl_lock();
10866  *	...
10867  *	register_netdevice(x1);
10868  *	register_netdevice(x2);
10869  *	...
10870  *	unregister_netdevice(y1);
10871  *	unregister_netdevice(y2);
10872  *      ...
10873  *	rtnl_unlock();
10874  *	free_netdev(y1);
10875  *	free_netdev(y2);
10876  *
10877  * We are invoked by rtnl_unlock().
10878  * This allows us to deal with problems:
10879  * 1) We can delete sysfs objects which invoke hotplug
10880  *    without deadlocking with linkwatch via keventd.
10881  * 2) Since we run with the RTNL semaphore not held, we can sleep
10882  *    safely in order to wait for the netdev refcnt to drop to zero.
10883  *
10884  * We must not return until all unregister events added during
10885  * the interval the lock was held have been completed.
10886  */
10887 void netdev_run_todo(void)
10888 {
10889 	struct net_device *dev, *tmp;
10890 	struct list_head list;
10891 	int cnt;
10892 #ifdef CONFIG_LOCKDEP
10893 	struct list_head unlink_list;
10894 
10895 	list_replace_init(&net_unlink_list, &unlink_list);
10896 
10897 	while (!list_empty(&unlink_list)) {
10898 		struct net_device *dev = list_first_entry(&unlink_list,
10899 							  struct net_device,
10900 							  unlink_list);
10901 		list_del_init(&dev->unlink_list);
10902 		dev->nested_level = dev->lower_level - 1;
10903 	}
10904 #endif
10905 
10906 	/* Snapshot list, allow later requests */
10907 	list_replace_init(&net_todo_list, &list);
10908 
10909 	__rtnl_unlock();
10910 
10911 	/* Wait for rcu callbacks to finish before next phase */
10912 	if (!list_empty(&list))
10913 		rcu_barrier();
10914 
10915 	list_for_each_entry_safe(dev, tmp, &list, todo_list) {
10916 		if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
10917 			netdev_WARN(dev, "run_todo but not unregistering\n");
10918 			list_del(&dev->todo_list);
10919 			continue;
10920 		}
10921 
10922 		WRITE_ONCE(dev->reg_state, NETREG_UNREGISTERED);
10923 		linkwatch_sync_dev(dev);
10924 	}
10925 
10926 	cnt = 0;
10927 	while (!list_empty(&list)) {
10928 		dev = netdev_wait_allrefs_any(&list);
10929 		list_del(&dev->todo_list);
10930 
10931 		/* paranoia */
10932 		BUG_ON(netdev_refcnt_read(dev) != 1);
10933 		BUG_ON(!list_empty(&dev->ptype_all));
10934 		BUG_ON(!list_empty(&dev->ptype_specific));
10935 		WARN_ON(rcu_access_pointer(dev->ip_ptr));
10936 		WARN_ON(rcu_access_pointer(dev->ip6_ptr));
10937 
10938 		netdev_do_free_pcpu_stats(dev);
10939 		if (dev->priv_destructor)
10940 			dev->priv_destructor(dev);
10941 		if (dev->needs_free_netdev)
10942 			free_netdev(dev);
10943 
10944 		cnt++;
10945 
10946 		/* Free network device */
10947 		kobject_put(&dev->dev.kobj);
10948 	}
10949 	if (cnt && atomic_sub_and_test(cnt, &dev_unreg_count))
10950 		wake_up(&netdev_unregistering_wq);
10951 }
10952 
10953 /* Collate per-cpu network dstats statistics
10954  *
10955  * Read per-cpu network statistics from dev->dstats and populate the related
10956  * fields in @s.
10957  */
10958 static void dev_fetch_dstats(struct rtnl_link_stats64 *s,
10959 			     const struct pcpu_dstats __percpu *dstats)
10960 {
10961 	int cpu;
10962 
10963 	for_each_possible_cpu(cpu) {
10964 		u64 rx_packets, rx_bytes, rx_drops;
10965 		u64 tx_packets, tx_bytes, tx_drops;
10966 		const struct pcpu_dstats *stats;
10967 		unsigned int start;
10968 
10969 		stats = per_cpu_ptr(dstats, cpu);
10970 		do {
10971 			start = u64_stats_fetch_begin(&stats->syncp);
10972 			rx_packets = u64_stats_read(&stats->rx_packets);
10973 			rx_bytes   = u64_stats_read(&stats->rx_bytes);
10974 			rx_drops   = u64_stats_read(&stats->rx_drops);
10975 			tx_packets = u64_stats_read(&stats->tx_packets);
10976 			tx_bytes   = u64_stats_read(&stats->tx_bytes);
10977 			tx_drops   = u64_stats_read(&stats->tx_drops);
10978 		} while (u64_stats_fetch_retry(&stats->syncp, start));
10979 
10980 		s->rx_packets += rx_packets;
10981 		s->rx_bytes   += rx_bytes;
10982 		s->rx_dropped += rx_drops;
10983 		s->tx_packets += tx_packets;
10984 		s->tx_bytes   += tx_bytes;
10985 		s->tx_dropped += tx_drops;
10986 	}
10987 }
10988 
10989 /* ndo_get_stats64 implementation for dtstats-based accounting.
10990  *
10991  * Populate @s from dev->stats and dev->dstats. This is used internally by the
10992  * core for NETDEV_PCPU_STAT_DSTAT-type stats collection.
10993  */
10994 static void dev_get_dstats64(const struct net_device *dev,
10995 			     struct rtnl_link_stats64 *s)
10996 {
10997 	netdev_stats_to_stats64(s, &dev->stats);
10998 	dev_fetch_dstats(s, dev->dstats);
10999 }
11000 
11001 /* Convert net_device_stats to rtnl_link_stats64. rtnl_link_stats64 has
11002  * all the same fields in the same order as net_device_stats, with only
11003  * the type differing, but rtnl_link_stats64 may have additional fields
11004  * at the end for newer counters.
11005  */
11006 void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
11007 			     const struct net_device_stats *netdev_stats)
11008 {
11009 	size_t i, n = sizeof(*netdev_stats) / sizeof(atomic_long_t);
11010 	const atomic_long_t *src = (atomic_long_t *)netdev_stats;
11011 	u64 *dst = (u64 *)stats64;
11012 
11013 	BUILD_BUG_ON(n > sizeof(*stats64) / sizeof(u64));
11014 	for (i = 0; i < n; i++)
11015 		dst[i] = (unsigned long)atomic_long_read(&src[i]);
11016 	/* zero out counters that only exist in rtnl_link_stats64 */
11017 	memset((char *)stats64 + n * sizeof(u64), 0,
11018 	       sizeof(*stats64) - n * sizeof(u64));
11019 }
11020 EXPORT_SYMBOL(netdev_stats_to_stats64);
11021 
11022 static __cold struct net_device_core_stats __percpu *netdev_core_stats_alloc(
11023 		struct net_device *dev)
11024 {
11025 	struct net_device_core_stats __percpu *p;
11026 
11027 	p = alloc_percpu_gfp(struct net_device_core_stats,
11028 			     GFP_ATOMIC | __GFP_NOWARN);
11029 
11030 	if (p && cmpxchg(&dev->core_stats, NULL, p))
11031 		free_percpu(p);
11032 
11033 	/* This READ_ONCE() pairs with the cmpxchg() above */
11034 	return READ_ONCE(dev->core_stats);
11035 }
11036 
11037 noinline void netdev_core_stats_inc(struct net_device *dev, u32 offset)
11038 {
11039 	/* This READ_ONCE() pairs with the write in netdev_core_stats_alloc() */
11040 	struct net_device_core_stats __percpu *p = READ_ONCE(dev->core_stats);
11041 	unsigned long __percpu *field;
11042 
11043 	if (unlikely(!p)) {
11044 		p = netdev_core_stats_alloc(dev);
11045 		if (!p)
11046 			return;
11047 	}
11048 
11049 	field = (unsigned long __percpu *)((void __percpu *)p + offset);
11050 	this_cpu_inc(*field);
11051 }
11052 EXPORT_SYMBOL_GPL(netdev_core_stats_inc);
11053 
11054 /**
11055  *	dev_get_stats	- get network device statistics
11056  *	@dev: device to get statistics from
11057  *	@storage: place to store stats
11058  *
11059  *	Get network statistics from device. Return @storage.
11060  *	The device driver may provide its own method by setting
11061  *	dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats;
11062  *	otherwise the internal statistics structure is used.
11063  */
11064 struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev,
11065 					struct rtnl_link_stats64 *storage)
11066 {
11067 	const struct net_device_ops *ops = dev->netdev_ops;
11068 	const struct net_device_core_stats __percpu *p;
11069 
11070 	if (ops->ndo_get_stats64) {
11071 		memset(storage, 0, sizeof(*storage));
11072 		ops->ndo_get_stats64(dev, storage);
11073 	} else if (ops->ndo_get_stats) {
11074 		netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev));
11075 	} else if (dev->pcpu_stat_type == NETDEV_PCPU_STAT_TSTATS) {
11076 		dev_get_tstats64(dev, storage);
11077 	} else if (dev->pcpu_stat_type == NETDEV_PCPU_STAT_DSTATS) {
11078 		dev_get_dstats64(dev, storage);
11079 	} else {
11080 		netdev_stats_to_stats64(storage, &dev->stats);
11081 	}
11082 
11083 	/* This READ_ONCE() pairs with the write in netdev_core_stats_alloc() */
11084 	p = READ_ONCE(dev->core_stats);
11085 	if (p) {
11086 		const struct net_device_core_stats *core_stats;
11087 		int i;
11088 
11089 		for_each_possible_cpu(i) {
11090 			core_stats = per_cpu_ptr(p, i);
11091 			storage->rx_dropped += READ_ONCE(core_stats->rx_dropped);
11092 			storage->tx_dropped += READ_ONCE(core_stats->tx_dropped);
11093 			storage->rx_nohandler += READ_ONCE(core_stats->rx_nohandler);
11094 			storage->rx_otherhost_dropped += READ_ONCE(core_stats->rx_otherhost_dropped);
11095 		}
11096 	}
11097 	return storage;
11098 }
11099 EXPORT_SYMBOL(dev_get_stats);
11100 
11101 /**
11102  *	dev_fetch_sw_netstats - get per-cpu network device statistics
11103  *	@s: place to store stats
11104  *	@netstats: per-cpu network stats to read from
11105  *
11106  *	Read per-cpu network statistics and populate the related fields in @s.
11107  */
11108 void dev_fetch_sw_netstats(struct rtnl_link_stats64 *s,
11109 			   const struct pcpu_sw_netstats __percpu *netstats)
11110 {
11111 	int cpu;
11112 
11113 	for_each_possible_cpu(cpu) {
11114 		u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
11115 		const struct pcpu_sw_netstats *stats;
11116 		unsigned int start;
11117 
11118 		stats = per_cpu_ptr(netstats, cpu);
11119 		do {
11120 			start = u64_stats_fetch_begin(&stats->syncp);
11121 			rx_packets = u64_stats_read(&stats->rx_packets);
11122 			rx_bytes   = u64_stats_read(&stats->rx_bytes);
11123 			tx_packets = u64_stats_read(&stats->tx_packets);
11124 			tx_bytes   = u64_stats_read(&stats->tx_bytes);
11125 		} while (u64_stats_fetch_retry(&stats->syncp, start));
11126 
11127 		s->rx_packets += rx_packets;
11128 		s->rx_bytes   += rx_bytes;
11129 		s->tx_packets += tx_packets;
11130 		s->tx_bytes   += tx_bytes;
11131 	}
11132 }
11133 EXPORT_SYMBOL_GPL(dev_fetch_sw_netstats);
11134 
11135 /**
11136  *	dev_get_tstats64 - ndo_get_stats64 implementation
11137  *	@dev: device to get statistics from
11138  *	@s: place to store stats
11139  *
11140  *	Populate @s from dev->stats and dev->tstats. Can be used as
11141  *	ndo_get_stats64() callback.
11142  */
11143 void dev_get_tstats64(struct net_device *dev, struct rtnl_link_stats64 *s)
11144 {
11145 	netdev_stats_to_stats64(s, &dev->stats);
11146 	dev_fetch_sw_netstats(s, dev->tstats);
11147 }
11148 EXPORT_SYMBOL_GPL(dev_get_tstats64);
11149 
11150 struct netdev_queue *dev_ingress_queue_create(struct net_device *dev)
11151 {
11152 	struct netdev_queue *queue = dev_ingress_queue(dev);
11153 
11154 #ifdef CONFIG_NET_CLS_ACT
11155 	if (queue)
11156 		return queue;
11157 	queue = kzalloc(sizeof(*queue), GFP_KERNEL);
11158 	if (!queue)
11159 		return NULL;
11160 	netdev_init_one_queue(dev, queue, NULL);
11161 	RCU_INIT_POINTER(queue->qdisc, &noop_qdisc);
11162 	RCU_INIT_POINTER(queue->qdisc_sleeping, &noop_qdisc);
11163 	rcu_assign_pointer(dev->ingress_queue, queue);
11164 #endif
11165 	return queue;
11166 }
11167 
11168 static const struct ethtool_ops default_ethtool_ops;
11169 
11170 void netdev_set_default_ethtool_ops(struct net_device *dev,
11171 				    const struct ethtool_ops *ops)
11172 {
11173 	if (dev->ethtool_ops == &default_ethtool_ops)
11174 		dev->ethtool_ops = ops;
11175 }
11176 EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops);
11177 
11178 /**
11179  * netdev_sw_irq_coalesce_default_on() - enable SW IRQ coalescing by default
11180  * @dev: netdev to enable the IRQ coalescing on
11181  *
11182  * Sets a conservative default for SW IRQ coalescing. Users can use
11183  * sysfs attributes to override the default values.
11184  */
11185 void netdev_sw_irq_coalesce_default_on(struct net_device *dev)
11186 {
11187 	WARN_ON(dev->reg_state == NETREG_REGISTERED);
11188 
11189 	if (!IS_ENABLED(CONFIG_PREEMPT_RT)) {
11190 		netdev_set_gro_flush_timeout(dev, 20000);
11191 		netdev_set_defer_hard_irqs(dev, 1);
11192 	}
11193 }
11194 EXPORT_SYMBOL_GPL(netdev_sw_irq_coalesce_default_on);
11195 
11196 /**
11197  * alloc_netdev_mqs - allocate network device
11198  * @sizeof_priv: size of private data to allocate space for
11199  * @name: device name format string
11200  * @name_assign_type: origin of device name
11201  * @setup: callback to initialize device
11202  * @txqs: the number of TX subqueues to allocate
11203  * @rxqs: the number of RX subqueues to allocate
11204  *
11205  * Allocates a struct net_device with private data area for driver use
11206  * and performs basic initialization.  Also allocates subqueue structs
11207  * for each queue on the device.
11208  */
11209 struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
11210 		unsigned char name_assign_type,
11211 		void (*setup)(struct net_device *),
11212 		unsigned int txqs, unsigned int rxqs)
11213 {
11214 	struct net_device *dev;
11215 	size_t napi_config_sz;
11216 	unsigned int maxqs;
11217 
11218 	BUG_ON(strlen(name) >= sizeof(dev->name));
11219 
11220 	if (txqs < 1) {
11221 		pr_err("alloc_netdev: Unable to allocate device with zero queues\n");
11222 		return NULL;
11223 	}
11224 
11225 	if (rxqs < 1) {
11226 		pr_err("alloc_netdev: Unable to allocate device with zero RX queues\n");
11227 		return NULL;
11228 	}
11229 
11230 	maxqs = max(txqs, rxqs);
11231 
11232 	dev = kvzalloc(struct_size(dev, priv, sizeof_priv),
11233 		       GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL);
11234 	if (!dev)
11235 		return NULL;
11236 
11237 	dev->priv_len = sizeof_priv;
11238 
11239 	ref_tracker_dir_init(&dev->refcnt_tracker, 128, name);
11240 #ifdef CONFIG_PCPU_DEV_REFCNT
11241 	dev->pcpu_refcnt = alloc_percpu(int);
11242 	if (!dev->pcpu_refcnt)
11243 		goto free_dev;
11244 	__dev_hold(dev);
11245 #else
11246 	refcount_set(&dev->dev_refcnt, 1);
11247 #endif
11248 
11249 	if (dev_addr_init(dev))
11250 		goto free_pcpu;
11251 
11252 	dev_mc_init(dev);
11253 	dev_uc_init(dev);
11254 
11255 	dev_net_set(dev, &init_net);
11256 
11257 	dev->gso_max_size = GSO_LEGACY_MAX_SIZE;
11258 	dev->xdp_zc_max_segs = 1;
11259 	dev->gso_max_segs = GSO_MAX_SEGS;
11260 	dev->gro_max_size = GRO_LEGACY_MAX_SIZE;
11261 	dev->gso_ipv4_max_size = GSO_LEGACY_MAX_SIZE;
11262 	dev->gro_ipv4_max_size = GRO_LEGACY_MAX_SIZE;
11263 	dev->tso_max_size = TSO_LEGACY_MAX_SIZE;
11264 	dev->tso_max_segs = TSO_MAX_SEGS;
11265 	dev->upper_level = 1;
11266 	dev->lower_level = 1;
11267 #ifdef CONFIG_LOCKDEP
11268 	dev->nested_level = 0;
11269 	INIT_LIST_HEAD(&dev->unlink_list);
11270 #endif
11271 
11272 	INIT_LIST_HEAD(&dev->napi_list);
11273 	INIT_LIST_HEAD(&dev->unreg_list);
11274 	INIT_LIST_HEAD(&dev->close_list);
11275 	INIT_LIST_HEAD(&dev->link_watch_list);
11276 	INIT_LIST_HEAD(&dev->adj_list.upper);
11277 	INIT_LIST_HEAD(&dev->adj_list.lower);
11278 	INIT_LIST_HEAD(&dev->ptype_all);
11279 	INIT_LIST_HEAD(&dev->ptype_specific);
11280 	INIT_LIST_HEAD(&dev->net_notifier_list);
11281 #ifdef CONFIG_NET_SCHED
11282 	hash_init(dev->qdisc_hash);
11283 #endif
11284 
11285 	mutex_init(&dev->lock);
11286 
11287 	dev->priv_flags = IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM;
11288 	setup(dev);
11289 
11290 	if (!dev->tx_queue_len) {
11291 		dev->priv_flags |= IFF_NO_QUEUE;
11292 		dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;
11293 	}
11294 
11295 	dev->num_tx_queues = txqs;
11296 	dev->real_num_tx_queues = txqs;
11297 	if (netif_alloc_netdev_queues(dev))
11298 		goto free_all;
11299 
11300 	dev->num_rx_queues = rxqs;
11301 	dev->real_num_rx_queues = rxqs;
11302 	if (netif_alloc_rx_queues(dev))
11303 		goto free_all;
11304 	dev->ethtool = kzalloc(sizeof(*dev->ethtool), GFP_KERNEL_ACCOUNT);
11305 	if (!dev->ethtool)
11306 		goto free_all;
11307 
11308 	napi_config_sz = array_size(maxqs, sizeof(*dev->napi_config));
11309 	dev->napi_config = kvzalloc(napi_config_sz, GFP_KERNEL_ACCOUNT);
11310 	if (!dev->napi_config)
11311 		goto free_all;
11312 
11313 	strscpy(dev->name, name);
11314 	dev->name_assign_type = name_assign_type;
11315 	dev->group = INIT_NETDEV_GROUP;
11316 	if (!dev->ethtool_ops)
11317 		dev->ethtool_ops = &default_ethtool_ops;
11318 
11319 	nf_hook_netdev_init(dev);
11320 
11321 	return dev;
11322 
11323 free_all:
11324 	free_netdev(dev);
11325 	return NULL;
11326 
11327 free_pcpu:
11328 #ifdef CONFIG_PCPU_DEV_REFCNT
11329 	free_percpu(dev->pcpu_refcnt);
11330 free_dev:
11331 #endif
11332 	kvfree(dev);
11333 	return NULL;
11334 }
11335 EXPORT_SYMBOL(alloc_netdev_mqs);
11336 
11337 /**
11338  * free_netdev - free network device
11339  * @dev: device
11340  *
11341  * This function does the last stage of destroying an allocated device
11342  * interface. The reference to the device object is released. If this
11343  * is the last reference then it will be freed.Must be called in process
11344  * context.
11345  */
11346 void free_netdev(struct net_device *dev)
11347 {
11348 	struct napi_struct *p, *n;
11349 
11350 	might_sleep();
11351 
11352 	/* When called immediately after register_netdevice() failed the unwind
11353 	 * handling may still be dismantling the device. Handle that case by
11354 	 * deferring the free.
11355 	 */
11356 	if (dev->reg_state == NETREG_UNREGISTERING) {
11357 		ASSERT_RTNL();
11358 		dev->needs_free_netdev = true;
11359 		return;
11360 	}
11361 
11362 	mutex_destroy(&dev->lock);
11363 
11364 	kfree(dev->ethtool);
11365 	netif_free_tx_queues(dev);
11366 	netif_free_rx_queues(dev);
11367 
11368 	kfree(rcu_dereference_protected(dev->ingress_queue, 1));
11369 
11370 	/* Flush device addresses */
11371 	dev_addr_flush(dev);
11372 
11373 	list_for_each_entry_safe(p, n, &dev->napi_list, dev_list)
11374 		netif_napi_del(p);
11375 
11376 	kvfree(dev->napi_config);
11377 
11378 	ref_tracker_dir_exit(&dev->refcnt_tracker);
11379 #ifdef CONFIG_PCPU_DEV_REFCNT
11380 	free_percpu(dev->pcpu_refcnt);
11381 	dev->pcpu_refcnt = NULL;
11382 #endif
11383 	free_percpu(dev->core_stats);
11384 	dev->core_stats = NULL;
11385 	free_percpu(dev->xdp_bulkq);
11386 	dev->xdp_bulkq = NULL;
11387 
11388 	netdev_free_phy_link_topology(dev);
11389 
11390 	/*  Compatibility with error handling in drivers */
11391 	if (dev->reg_state == NETREG_UNINITIALIZED ||
11392 	    dev->reg_state == NETREG_DUMMY) {
11393 		kvfree(dev);
11394 		return;
11395 	}
11396 
11397 	BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
11398 	WRITE_ONCE(dev->reg_state, NETREG_RELEASED);
11399 
11400 	/* will free via device release */
11401 	put_device(&dev->dev);
11402 }
11403 EXPORT_SYMBOL(free_netdev);
11404 
11405 /**
11406  * alloc_netdev_dummy - Allocate and initialize a dummy net device.
11407  * @sizeof_priv: size of private data to allocate space for
11408  *
11409  * Return: the allocated net_device on success, NULL otherwise
11410  */
11411 struct net_device *alloc_netdev_dummy(int sizeof_priv)
11412 {
11413 	return alloc_netdev(sizeof_priv, "dummy#", NET_NAME_UNKNOWN,
11414 			    init_dummy_netdev_core);
11415 }
11416 EXPORT_SYMBOL_GPL(alloc_netdev_dummy);
11417 
11418 /**
11419  *	synchronize_net -  Synchronize with packet receive processing
11420  *
11421  *	Wait for packets currently being received to be done.
11422  *	Does not block later packets from starting.
11423  */
11424 void synchronize_net(void)
11425 {
11426 	might_sleep();
11427 	if (rtnl_is_locked())
11428 		synchronize_rcu_expedited();
11429 	else
11430 		synchronize_rcu();
11431 }
11432 EXPORT_SYMBOL(synchronize_net);
11433 
11434 static void netdev_rss_contexts_free(struct net_device *dev)
11435 {
11436 	struct ethtool_rxfh_context *ctx;
11437 	unsigned long context;
11438 
11439 	mutex_lock(&dev->ethtool->rss_lock);
11440 	xa_for_each(&dev->ethtool->rss_ctx, context, ctx) {
11441 		struct ethtool_rxfh_param rxfh;
11442 
11443 		rxfh.indir = ethtool_rxfh_context_indir(ctx);
11444 		rxfh.key = ethtool_rxfh_context_key(ctx);
11445 		rxfh.hfunc = ctx->hfunc;
11446 		rxfh.input_xfrm = ctx->input_xfrm;
11447 		rxfh.rss_context = context;
11448 		rxfh.rss_delete = true;
11449 
11450 		xa_erase(&dev->ethtool->rss_ctx, context);
11451 		if (dev->ethtool_ops->create_rxfh_context)
11452 			dev->ethtool_ops->remove_rxfh_context(dev, ctx,
11453 							      context, NULL);
11454 		else
11455 			dev->ethtool_ops->set_rxfh(dev, &rxfh, NULL);
11456 		kfree(ctx);
11457 	}
11458 	xa_destroy(&dev->ethtool->rss_ctx);
11459 	mutex_unlock(&dev->ethtool->rss_lock);
11460 }
11461 
11462 /**
11463  *	unregister_netdevice_queue - remove device from the kernel
11464  *	@dev: device
11465  *	@head: list
11466  *
11467  *	This function shuts down a device interface and removes it
11468  *	from the kernel tables.
11469  *	If head not NULL, device is queued to be unregistered later.
11470  *
11471  *	Callers must hold the rtnl semaphore.  You may want
11472  *	unregister_netdev() instead of this.
11473  */
11474 
11475 void unregister_netdevice_queue(struct net_device *dev, struct list_head *head)
11476 {
11477 	ASSERT_RTNL();
11478 
11479 	if (head) {
11480 		list_move_tail(&dev->unreg_list, head);
11481 	} else {
11482 		LIST_HEAD(single);
11483 
11484 		list_add(&dev->unreg_list, &single);
11485 		unregister_netdevice_many(&single);
11486 	}
11487 }
11488 EXPORT_SYMBOL(unregister_netdevice_queue);
11489 
11490 void unregister_netdevice_many_notify(struct list_head *head,
11491 				      u32 portid, const struct nlmsghdr *nlh)
11492 {
11493 	struct net_device *dev, *tmp;
11494 	LIST_HEAD(close_head);
11495 	int cnt = 0;
11496 
11497 	BUG_ON(dev_boot_phase);
11498 	ASSERT_RTNL();
11499 
11500 	if (list_empty(head))
11501 		return;
11502 
11503 	list_for_each_entry_safe(dev, tmp, head, unreg_list) {
11504 		/* Some devices call without registering
11505 		 * for initialization unwind. Remove those
11506 		 * devices and proceed with the remaining.
11507 		 */
11508 		if (dev->reg_state == NETREG_UNINITIALIZED) {
11509 			pr_debug("unregister_netdevice: device %s/%p never was registered\n",
11510 				 dev->name, dev);
11511 
11512 			WARN_ON(1);
11513 			list_del(&dev->unreg_list);
11514 			continue;
11515 		}
11516 		dev->dismantle = true;
11517 		BUG_ON(dev->reg_state != NETREG_REGISTERED);
11518 	}
11519 
11520 	/* If device is running, close it first. */
11521 	list_for_each_entry(dev, head, unreg_list)
11522 		list_add_tail(&dev->close_list, &close_head);
11523 	dev_close_many(&close_head, true);
11524 
11525 	list_for_each_entry(dev, head, unreg_list) {
11526 		/* And unlink it from device chain. */
11527 		unlist_netdevice(dev);
11528 		WRITE_ONCE(dev->reg_state, NETREG_UNREGISTERING);
11529 	}
11530 	flush_all_backlogs();
11531 
11532 	synchronize_net();
11533 
11534 	list_for_each_entry(dev, head, unreg_list) {
11535 		struct sk_buff *skb = NULL;
11536 
11537 		/* Shutdown queueing discipline. */
11538 		dev_shutdown(dev);
11539 		dev_tcx_uninstall(dev);
11540 		dev_xdp_uninstall(dev);
11541 		bpf_dev_bound_netdev_unregister(dev);
11542 		dev_dmabuf_uninstall(dev);
11543 
11544 		netdev_offload_xstats_disable_all(dev);
11545 
11546 		/* Notify protocols, that we are about to destroy
11547 		 * this device. They should clean all the things.
11548 		 */
11549 		call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
11550 
11551 		if (!dev->rtnl_link_ops ||
11552 		    dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
11553 			skb = rtmsg_ifinfo_build_skb(RTM_DELLINK, dev, ~0U, 0,
11554 						     GFP_KERNEL, NULL, 0,
11555 						     portid, nlh);
11556 
11557 		/*
11558 		 *	Flush the unicast and multicast chains
11559 		 */
11560 		dev_uc_flush(dev);
11561 		dev_mc_flush(dev);
11562 
11563 		netdev_name_node_alt_flush(dev);
11564 		netdev_name_node_free(dev->name_node);
11565 
11566 		netdev_rss_contexts_free(dev);
11567 
11568 		call_netdevice_notifiers(NETDEV_PRE_UNINIT, dev);
11569 
11570 		if (dev->netdev_ops->ndo_uninit)
11571 			dev->netdev_ops->ndo_uninit(dev);
11572 
11573 		mutex_destroy(&dev->ethtool->rss_lock);
11574 
11575 		net_shaper_flush_netdev(dev);
11576 
11577 		if (skb)
11578 			rtmsg_ifinfo_send(skb, dev, GFP_KERNEL, portid, nlh);
11579 
11580 		/* Notifier chain MUST detach us all upper devices. */
11581 		WARN_ON(netdev_has_any_upper_dev(dev));
11582 		WARN_ON(netdev_has_any_lower_dev(dev));
11583 
11584 		/* Remove entries from kobject tree */
11585 		netdev_unregister_kobject(dev);
11586 #ifdef CONFIG_XPS
11587 		/* Remove XPS queueing entries */
11588 		netif_reset_xps_queues_gt(dev, 0);
11589 #endif
11590 	}
11591 
11592 	synchronize_net();
11593 
11594 	list_for_each_entry(dev, head, unreg_list) {
11595 		netdev_put(dev, &dev->dev_registered_tracker);
11596 		net_set_todo(dev);
11597 		cnt++;
11598 	}
11599 	atomic_add(cnt, &dev_unreg_count);
11600 
11601 	list_del(head);
11602 }
11603 
11604 /**
11605  *	unregister_netdevice_many - unregister many devices
11606  *	@head: list of devices
11607  *
11608  *  Note: As most callers use a stack allocated list_head,
11609  *  we force a list_del() to make sure stack won't be corrupted later.
11610  */
11611 void unregister_netdevice_many(struct list_head *head)
11612 {
11613 	unregister_netdevice_many_notify(head, 0, NULL);
11614 }
11615 EXPORT_SYMBOL(unregister_netdevice_many);
11616 
11617 /**
11618  *	unregister_netdev - remove device from the kernel
11619  *	@dev: device
11620  *
11621  *	This function shuts down a device interface and removes it
11622  *	from the kernel tables.
11623  *
11624  *	This is just a wrapper for unregister_netdevice that takes
11625  *	the rtnl semaphore.  In general you want to use this and not
11626  *	unregister_netdevice.
11627  */
11628 void unregister_netdev(struct net_device *dev)
11629 {
11630 	struct net *net = dev_net(dev);
11631 
11632 	rtnl_net_lock(net);
11633 	unregister_netdevice(dev);
11634 	rtnl_net_unlock(net);
11635 }
11636 EXPORT_SYMBOL(unregister_netdev);
11637 
11638 /**
11639  *	__dev_change_net_namespace - move device to different nethost namespace
11640  *	@dev: device
11641  *	@net: network namespace
11642  *	@pat: If not NULL name pattern to try if the current device name
11643  *	      is already taken in the destination network namespace.
11644  *	@new_ifindex: If not zero, specifies device index in the target
11645  *	              namespace.
11646  *
11647  *	This function shuts down a device interface and moves it
11648  *	to a new network namespace. On success 0 is returned, on
11649  *	a failure a netagive errno code is returned.
11650  *
11651  *	Callers must hold the rtnl semaphore.
11652  */
11653 
11654 int __dev_change_net_namespace(struct net_device *dev, struct net *net,
11655 			       const char *pat, int new_ifindex)
11656 {
11657 	struct netdev_name_node *name_node;
11658 	struct net *net_old = dev_net(dev);
11659 	char new_name[IFNAMSIZ] = {};
11660 	int err, new_nsid;
11661 
11662 	ASSERT_RTNL();
11663 
11664 	/* Don't allow namespace local devices to be moved. */
11665 	err = -EINVAL;
11666 	if (dev->netns_local)
11667 		goto out;
11668 
11669 	/* Ensure the device has been registered */
11670 	if (dev->reg_state != NETREG_REGISTERED)
11671 		goto out;
11672 
11673 	/* Get out if there is nothing todo */
11674 	err = 0;
11675 	if (net_eq(net_old, net))
11676 		goto out;
11677 
11678 	/* Pick the destination device name, and ensure
11679 	 * we can use it in the destination network namespace.
11680 	 */
11681 	err = -EEXIST;
11682 	if (netdev_name_in_use(net, dev->name)) {
11683 		/* We get here if we can't use the current device name */
11684 		if (!pat)
11685 			goto out;
11686 		err = dev_prep_valid_name(net, dev, pat, new_name, EEXIST);
11687 		if (err < 0)
11688 			goto out;
11689 	}
11690 	/* Check that none of the altnames conflicts. */
11691 	err = -EEXIST;
11692 	netdev_for_each_altname(dev, name_node)
11693 		if (netdev_name_in_use(net, name_node->name))
11694 			goto out;
11695 
11696 	/* Check that new_ifindex isn't used yet. */
11697 	if (new_ifindex) {
11698 		err = dev_index_reserve(net, new_ifindex);
11699 		if (err < 0)
11700 			goto out;
11701 	} else {
11702 		/* If there is an ifindex conflict assign a new one */
11703 		err = dev_index_reserve(net, dev->ifindex);
11704 		if (err == -EBUSY)
11705 			err = dev_index_reserve(net, 0);
11706 		if (err < 0)
11707 			goto out;
11708 		new_ifindex = err;
11709 	}
11710 
11711 	/*
11712 	 * And now a mini version of register_netdevice unregister_netdevice.
11713 	 */
11714 
11715 	/* If device is running close it first. */
11716 	dev_close(dev);
11717 
11718 	/* And unlink it from device chain */
11719 	unlist_netdevice(dev);
11720 
11721 	synchronize_net();
11722 
11723 	/* Shutdown queueing discipline. */
11724 	dev_shutdown(dev);
11725 
11726 	/* Notify protocols, that we are about to destroy
11727 	 * this device. They should clean all the things.
11728 	 *
11729 	 * Note that dev->reg_state stays at NETREG_REGISTERED.
11730 	 * This is wanted because this way 8021q and macvlan know
11731 	 * the device is just moving and can keep their slaves up.
11732 	 */
11733 	call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
11734 	rcu_barrier();
11735 
11736 	new_nsid = peernet2id_alloc(dev_net(dev), net, GFP_KERNEL);
11737 
11738 	rtmsg_ifinfo_newnet(RTM_DELLINK, dev, ~0U, GFP_KERNEL, &new_nsid,
11739 			    new_ifindex);
11740 
11741 	/*
11742 	 *	Flush the unicast and multicast chains
11743 	 */
11744 	dev_uc_flush(dev);
11745 	dev_mc_flush(dev);
11746 
11747 	/* Send a netdev-removed uevent to the old namespace */
11748 	kobject_uevent(&dev->dev.kobj, KOBJ_REMOVE);
11749 	netdev_adjacent_del_links(dev);
11750 
11751 	/* Move per-net netdevice notifiers that are following the netdevice */
11752 	move_netdevice_notifiers_dev_net(dev, net);
11753 
11754 	/* Actually switch the network namespace */
11755 	dev_net_set(dev, net);
11756 	dev->ifindex = new_ifindex;
11757 
11758 	if (new_name[0]) {
11759 		/* Rename the netdev to prepared name */
11760 		write_seqlock_bh(&netdev_rename_lock);
11761 		strscpy(dev->name, new_name, IFNAMSIZ);
11762 		write_sequnlock_bh(&netdev_rename_lock);
11763 	}
11764 
11765 	/* Fixup kobjects */
11766 	dev_set_uevent_suppress(&dev->dev, 1);
11767 	err = device_rename(&dev->dev, dev->name);
11768 	dev_set_uevent_suppress(&dev->dev, 0);
11769 	WARN_ON(err);
11770 
11771 	/* Send a netdev-add uevent to the new namespace */
11772 	kobject_uevent(&dev->dev.kobj, KOBJ_ADD);
11773 	netdev_adjacent_add_links(dev);
11774 
11775 	/* Adapt owner in case owning user namespace of target network
11776 	 * namespace is different from the original one.
11777 	 */
11778 	err = netdev_change_owner(dev, net_old, net);
11779 	WARN_ON(err);
11780 
11781 	/* Add the device back in the hashes */
11782 	list_netdevice(dev);
11783 
11784 	/* Notify protocols, that a new device appeared. */
11785 	call_netdevice_notifiers(NETDEV_REGISTER, dev);
11786 
11787 	/*
11788 	 *	Prevent userspace races by waiting until the network
11789 	 *	device is fully setup before sending notifications.
11790 	 */
11791 	rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL, 0, NULL);
11792 
11793 	synchronize_net();
11794 	err = 0;
11795 out:
11796 	return err;
11797 }
11798 EXPORT_SYMBOL_GPL(__dev_change_net_namespace);
11799 
11800 static int dev_cpu_dead(unsigned int oldcpu)
11801 {
11802 	struct sk_buff **list_skb;
11803 	struct sk_buff *skb;
11804 	unsigned int cpu;
11805 	struct softnet_data *sd, *oldsd, *remsd = NULL;
11806 
11807 	local_irq_disable();
11808 	cpu = smp_processor_id();
11809 	sd = &per_cpu(softnet_data, cpu);
11810 	oldsd = &per_cpu(softnet_data, oldcpu);
11811 
11812 	/* Find end of our completion_queue. */
11813 	list_skb = &sd->completion_queue;
11814 	while (*list_skb)
11815 		list_skb = &(*list_skb)->next;
11816 	/* Append completion queue from offline CPU. */
11817 	*list_skb = oldsd->completion_queue;
11818 	oldsd->completion_queue = NULL;
11819 
11820 	/* Append output queue from offline CPU. */
11821 	if (oldsd->output_queue) {
11822 		*sd->output_queue_tailp = oldsd->output_queue;
11823 		sd->output_queue_tailp = oldsd->output_queue_tailp;
11824 		oldsd->output_queue = NULL;
11825 		oldsd->output_queue_tailp = &oldsd->output_queue;
11826 	}
11827 	/* Append NAPI poll list from offline CPU, with one exception :
11828 	 * process_backlog() must be called by cpu owning percpu backlog.
11829 	 * We properly handle process_queue & input_pkt_queue later.
11830 	 */
11831 	while (!list_empty(&oldsd->poll_list)) {
11832 		struct napi_struct *napi = list_first_entry(&oldsd->poll_list,
11833 							    struct napi_struct,
11834 							    poll_list);
11835 
11836 		list_del_init(&napi->poll_list);
11837 		if (napi->poll == process_backlog)
11838 			napi->state &= NAPIF_STATE_THREADED;
11839 		else
11840 			____napi_schedule(sd, napi);
11841 	}
11842 
11843 	raise_softirq_irqoff(NET_TX_SOFTIRQ);
11844 	local_irq_enable();
11845 
11846 	if (!use_backlog_threads()) {
11847 #ifdef CONFIG_RPS
11848 		remsd = oldsd->rps_ipi_list;
11849 		oldsd->rps_ipi_list = NULL;
11850 #endif
11851 		/* send out pending IPI's on offline CPU */
11852 		net_rps_send_ipi(remsd);
11853 	}
11854 
11855 	/* Process offline CPU's input_pkt_queue */
11856 	while ((skb = __skb_dequeue(&oldsd->process_queue))) {
11857 		netif_rx(skb);
11858 		rps_input_queue_head_incr(oldsd);
11859 	}
11860 	while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) {
11861 		netif_rx(skb);
11862 		rps_input_queue_head_incr(oldsd);
11863 	}
11864 
11865 	return 0;
11866 }
11867 
11868 /**
11869  *	netdev_increment_features - increment feature set by one
11870  *	@all: current feature set
11871  *	@one: new feature set
11872  *	@mask: mask feature set
11873  *
11874  *	Computes a new feature set after adding a device with feature set
11875  *	@one to the master device with current feature set @all.  Will not
11876  *	enable anything that is off in @mask. Returns the new feature set.
11877  */
11878 netdev_features_t netdev_increment_features(netdev_features_t all,
11879 	netdev_features_t one, netdev_features_t mask)
11880 {
11881 	if (mask & NETIF_F_HW_CSUM)
11882 		mask |= NETIF_F_CSUM_MASK;
11883 	mask |= NETIF_F_VLAN_CHALLENGED;
11884 
11885 	all |= one & (NETIF_F_ONE_FOR_ALL | NETIF_F_CSUM_MASK) & mask;
11886 	all &= one | ~NETIF_F_ALL_FOR_ALL;
11887 
11888 	/* If one device supports hw checksumming, set for all. */
11889 	if (all & NETIF_F_HW_CSUM)
11890 		all &= ~(NETIF_F_CSUM_MASK & ~NETIF_F_HW_CSUM);
11891 
11892 	return all;
11893 }
11894 EXPORT_SYMBOL(netdev_increment_features);
11895 
11896 static struct hlist_head * __net_init netdev_create_hash(void)
11897 {
11898 	int i;
11899 	struct hlist_head *hash;
11900 
11901 	hash = kmalloc_array(NETDEV_HASHENTRIES, sizeof(*hash), GFP_KERNEL);
11902 	if (hash != NULL)
11903 		for (i = 0; i < NETDEV_HASHENTRIES; i++)
11904 			INIT_HLIST_HEAD(&hash[i]);
11905 
11906 	return hash;
11907 }
11908 
11909 /* Initialize per network namespace state */
11910 static int __net_init netdev_init(struct net *net)
11911 {
11912 	BUILD_BUG_ON(GRO_HASH_BUCKETS >
11913 		     8 * sizeof_field(struct napi_struct, gro_bitmask));
11914 
11915 	INIT_LIST_HEAD(&net->dev_base_head);
11916 
11917 	net->dev_name_head = netdev_create_hash();
11918 	if (net->dev_name_head == NULL)
11919 		goto err_name;
11920 
11921 	net->dev_index_head = netdev_create_hash();
11922 	if (net->dev_index_head == NULL)
11923 		goto err_idx;
11924 
11925 	xa_init_flags(&net->dev_by_index, XA_FLAGS_ALLOC1);
11926 
11927 	RAW_INIT_NOTIFIER_HEAD(&net->netdev_chain);
11928 
11929 	return 0;
11930 
11931 err_idx:
11932 	kfree(net->dev_name_head);
11933 err_name:
11934 	return -ENOMEM;
11935 }
11936 
11937 /**
11938  *	netdev_drivername - network driver for the device
11939  *	@dev: network device
11940  *
11941  *	Determine network driver for device.
11942  */
11943 const char *netdev_drivername(const struct net_device *dev)
11944 {
11945 	const struct device_driver *driver;
11946 	const struct device *parent;
11947 	const char *empty = "";
11948 
11949 	parent = dev->dev.parent;
11950 	if (!parent)
11951 		return empty;
11952 
11953 	driver = parent->driver;
11954 	if (driver && driver->name)
11955 		return driver->name;
11956 	return empty;
11957 }
11958 
11959 static void __netdev_printk(const char *level, const struct net_device *dev,
11960 			    struct va_format *vaf)
11961 {
11962 	if (dev && dev->dev.parent) {
11963 		dev_printk_emit(level[1] - '0',
11964 				dev->dev.parent,
11965 				"%s %s %s%s: %pV",
11966 				dev_driver_string(dev->dev.parent),
11967 				dev_name(dev->dev.parent),
11968 				netdev_name(dev), netdev_reg_state(dev),
11969 				vaf);
11970 	} else if (dev) {
11971 		printk("%s%s%s: %pV",
11972 		       level, netdev_name(dev), netdev_reg_state(dev), vaf);
11973 	} else {
11974 		printk("%s(NULL net_device): %pV", level, vaf);
11975 	}
11976 }
11977 
11978 void netdev_printk(const char *level, const struct net_device *dev,
11979 		   const char *format, ...)
11980 {
11981 	struct va_format vaf;
11982 	va_list args;
11983 
11984 	va_start(args, format);
11985 
11986 	vaf.fmt = format;
11987 	vaf.va = &args;
11988 
11989 	__netdev_printk(level, dev, &vaf);
11990 
11991 	va_end(args);
11992 }
11993 EXPORT_SYMBOL(netdev_printk);
11994 
11995 #define define_netdev_printk_level(func, level)			\
11996 void func(const struct net_device *dev, const char *fmt, ...)	\
11997 {								\
11998 	struct va_format vaf;					\
11999 	va_list args;						\
12000 								\
12001 	va_start(args, fmt);					\
12002 								\
12003 	vaf.fmt = fmt;						\
12004 	vaf.va = &args;						\
12005 								\
12006 	__netdev_printk(level, dev, &vaf);			\
12007 								\
12008 	va_end(args);						\
12009 }								\
12010 EXPORT_SYMBOL(func);
12011 
12012 define_netdev_printk_level(netdev_emerg, KERN_EMERG);
12013 define_netdev_printk_level(netdev_alert, KERN_ALERT);
12014 define_netdev_printk_level(netdev_crit, KERN_CRIT);
12015 define_netdev_printk_level(netdev_err, KERN_ERR);
12016 define_netdev_printk_level(netdev_warn, KERN_WARNING);
12017 define_netdev_printk_level(netdev_notice, KERN_NOTICE);
12018 define_netdev_printk_level(netdev_info, KERN_INFO);
12019 
12020 static void __net_exit netdev_exit(struct net *net)
12021 {
12022 	kfree(net->dev_name_head);
12023 	kfree(net->dev_index_head);
12024 	xa_destroy(&net->dev_by_index);
12025 	if (net != &init_net)
12026 		WARN_ON_ONCE(!list_empty(&net->dev_base_head));
12027 }
12028 
12029 static struct pernet_operations __net_initdata netdev_net_ops = {
12030 	.init = netdev_init,
12031 	.exit = netdev_exit,
12032 };
12033 
12034 static void __net_exit default_device_exit_net(struct net *net)
12035 {
12036 	struct netdev_name_node *name_node, *tmp;
12037 	struct net_device *dev, *aux;
12038 	/*
12039 	 * Push all migratable network devices back to the
12040 	 * initial network namespace
12041 	 */
12042 	ASSERT_RTNL();
12043 	for_each_netdev_safe(net, dev, aux) {
12044 		int err;
12045 		char fb_name[IFNAMSIZ];
12046 
12047 		/* Ignore unmoveable devices (i.e. loopback) */
12048 		if (dev->netns_local)
12049 			continue;
12050 
12051 		/* Leave virtual devices for the generic cleanup */
12052 		if (dev->rtnl_link_ops && !dev->rtnl_link_ops->netns_refund)
12053 			continue;
12054 
12055 		/* Push remaining network devices to init_net */
12056 		snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex);
12057 		if (netdev_name_in_use(&init_net, fb_name))
12058 			snprintf(fb_name, IFNAMSIZ, "dev%%d");
12059 
12060 		netdev_for_each_altname_safe(dev, name_node, tmp)
12061 			if (netdev_name_in_use(&init_net, name_node->name))
12062 				__netdev_name_node_alt_destroy(name_node);
12063 
12064 		err = dev_change_net_namespace(dev, &init_net, fb_name);
12065 		if (err) {
12066 			pr_emerg("%s: failed to move %s to init_net: %d\n",
12067 				 __func__, dev->name, err);
12068 			BUG();
12069 		}
12070 	}
12071 }
12072 
12073 static void __net_exit default_device_exit_batch(struct list_head *net_list)
12074 {
12075 	/* At exit all network devices most be removed from a network
12076 	 * namespace.  Do this in the reverse order of registration.
12077 	 * Do this across as many network namespaces as possible to
12078 	 * improve batching efficiency.
12079 	 */
12080 	struct net_device *dev;
12081 	struct net *net;
12082 	LIST_HEAD(dev_kill_list);
12083 
12084 	rtnl_lock();
12085 	list_for_each_entry(net, net_list, exit_list) {
12086 		default_device_exit_net(net);
12087 		cond_resched();
12088 	}
12089 
12090 	list_for_each_entry(net, net_list, exit_list) {
12091 		for_each_netdev_reverse(net, dev) {
12092 			if (dev->rtnl_link_ops && dev->rtnl_link_ops->dellink)
12093 				dev->rtnl_link_ops->dellink(dev, &dev_kill_list);
12094 			else
12095 				unregister_netdevice_queue(dev, &dev_kill_list);
12096 		}
12097 	}
12098 	unregister_netdevice_many(&dev_kill_list);
12099 	rtnl_unlock();
12100 }
12101 
12102 static struct pernet_operations __net_initdata default_device_ops = {
12103 	.exit_batch = default_device_exit_batch,
12104 };
12105 
12106 static void __init net_dev_struct_check(void)
12107 {
12108 	/* TX read-mostly hotpath */
12109 	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, priv_flags_fast);
12110 	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, netdev_ops);
12111 	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, header_ops);
12112 	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, _tx);
12113 	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, real_num_tx_queues);
12114 	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, gso_max_size);
12115 	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, gso_ipv4_max_size);
12116 	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, gso_max_segs);
12117 	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, gso_partial_features);
12118 	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, num_tc);
12119 	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, mtu);
12120 	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, needed_headroom);
12121 	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, tc_to_txq);
12122 #ifdef CONFIG_XPS
12123 	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, xps_maps);
12124 #endif
12125 #ifdef CONFIG_NETFILTER_EGRESS
12126 	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, nf_hooks_egress);
12127 #endif
12128 #ifdef CONFIG_NET_XGRESS
12129 	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_tx, tcx_egress);
12130 #endif
12131 	CACHELINE_ASSERT_GROUP_SIZE(struct net_device, net_device_read_tx, 160);
12132 
12133 	/* TXRX read-mostly hotpath */
12134 	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_txrx, lstats);
12135 	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_txrx, state);
12136 	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_txrx, flags);
12137 	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_txrx, hard_header_len);
12138 	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_txrx, features);
12139 	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_txrx, ip6_ptr);
12140 	CACHELINE_ASSERT_GROUP_SIZE(struct net_device, net_device_read_txrx, 46);
12141 
12142 	/* RX read-mostly hotpath */
12143 	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, ptype_specific);
12144 	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, ifindex);
12145 	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, real_num_rx_queues);
12146 	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, _rx);
12147 	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, gro_max_size);
12148 	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, gro_ipv4_max_size);
12149 	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, rx_handler);
12150 	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, rx_handler_data);
12151 	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, nd_net);
12152 #ifdef CONFIG_NETPOLL
12153 	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, npinfo);
12154 #endif
12155 #ifdef CONFIG_NET_XGRESS
12156 	CACHELINE_ASSERT_GROUP_MEMBER(struct net_device, net_device_read_rx, tcx_ingress);
12157 #endif
12158 	CACHELINE_ASSERT_GROUP_SIZE(struct net_device, net_device_read_rx, 92);
12159 }
12160 
12161 /*
12162  *	Initialize the DEV module. At boot time this walks the device list and
12163  *	unhooks any devices that fail to initialise (normally hardware not
12164  *	present) and leaves us with a valid list of present and active devices.
12165  *
12166  */
12167 
12168 /* We allocate 256 pages for each CPU if PAGE_SHIFT is 12 */
12169 #define SYSTEM_PERCPU_PAGE_POOL_SIZE	((1 << 20) / PAGE_SIZE)
12170 
12171 static int net_page_pool_create(int cpuid)
12172 {
12173 #if IS_ENABLED(CONFIG_PAGE_POOL)
12174 	struct page_pool_params page_pool_params = {
12175 		.pool_size = SYSTEM_PERCPU_PAGE_POOL_SIZE,
12176 		.flags = PP_FLAG_SYSTEM_POOL,
12177 		.nid = cpu_to_mem(cpuid),
12178 	};
12179 	struct page_pool *pp_ptr;
12180 	int err;
12181 
12182 	pp_ptr = page_pool_create_percpu(&page_pool_params, cpuid);
12183 	if (IS_ERR(pp_ptr))
12184 		return -ENOMEM;
12185 
12186 	err = xdp_reg_page_pool(pp_ptr);
12187 	if (err) {
12188 		page_pool_destroy(pp_ptr);
12189 		return err;
12190 	}
12191 
12192 	per_cpu(system_page_pool, cpuid) = pp_ptr;
12193 #endif
12194 	return 0;
12195 }
12196 
12197 static int backlog_napi_should_run(unsigned int cpu)
12198 {
12199 	struct softnet_data *sd = per_cpu_ptr(&softnet_data, cpu);
12200 	struct napi_struct *napi = &sd->backlog;
12201 
12202 	return test_bit(NAPI_STATE_SCHED_THREADED, &napi->state);
12203 }
12204 
12205 static void run_backlog_napi(unsigned int cpu)
12206 {
12207 	struct softnet_data *sd = per_cpu_ptr(&softnet_data, cpu);
12208 
12209 	napi_threaded_poll_loop(&sd->backlog);
12210 }
12211 
12212 static void backlog_napi_setup(unsigned int cpu)
12213 {
12214 	struct softnet_data *sd = per_cpu_ptr(&softnet_data, cpu);
12215 	struct napi_struct *napi = &sd->backlog;
12216 
12217 	napi->thread = this_cpu_read(backlog_napi);
12218 	set_bit(NAPI_STATE_THREADED, &napi->state);
12219 }
12220 
12221 static struct smp_hotplug_thread backlog_threads = {
12222 	.store			= &backlog_napi,
12223 	.thread_should_run	= backlog_napi_should_run,
12224 	.thread_fn		= run_backlog_napi,
12225 	.thread_comm		= "backlog_napi/%u",
12226 	.setup			= backlog_napi_setup,
12227 };
12228 
12229 /*
12230  *       This is called single threaded during boot, so no need
12231  *       to take the rtnl semaphore.
12232  */
12233 static int __init net_dev_init(void)
12234 {
12235 	int i, rc = -ENOMEM;
12236 
12237 	BUG_ON(!dev_boot_phase);
12238 
12239 	net_dev_struct_check();
12240 
12241 	if (dev_proc_init())
12242 		goto out;
12243 
12244 	if (netdev_kobject_init())
12245 		goto out;
12246 
12247 	for (i = 0; i < PTYPE_HASH_SIZE; i++)
12248 		INIT_LIST_HEAD(&ptype_base[i]);
12249 
12250 	if (register_pernet_subsys(&netdev_net_ops))
12251 		goto out;
12252 
12253 	/*
12254 	 *	Initialise the packet receive queues.
12255 	 */
12256 
12257 	for_each_possible_cpu(i) {
12258 		struct work_struct *flush = per_cpu_ptr(&flush_works, i);
12259 		struct softnet_data *sd = &per_cpu(softnet_data, i);
12260 
12261 		INIT_WORK(flush, flush_backlog);
12262 
12263 		skb_queue_head_init(&sd->input_pkt_queue);
12264 		skb_queue_head_init(&sd->process_queue);
12265 #ifdef CONFIG_XFRM_OFFLOAD
12266 		skb_queue_head_init(&sd->xfrm_backlog);
12267 #endif
12268 		INIT_LIST_HEAD(&sd->poll_list);
12269 		sd->output_queue_tailp = &sd->output_queue;
12270 #ifdef CONFIG_RPS
12271 		INIT_CSD(&sd->csd, rps_trigger_softirq, sd);
12272 		sd->cpu = i;
12273 #endif
12274 		INIT_CSD(&sd->defer_csd, trigger_rx_softirq, sd);
12275 		spin_lock_init(&sd->defer_lock);
12276 
12277 		init_gro_hash(&sd->backlog);
12278 		sd->backlog.poll = process_backlog;
12279 		sd->backlog.weight = weight_p;
12280 		INIT_LIST_HEAD(&sd->backlog.poll_list);
12281 
12282 		if (net_page_pool_create(i))
12283 			goto out;
12284 	}
12285 	if (use_backlog_threads())
12286 		smpboot_register_percpu_thread(&backlog_threads);
12287 
12288 	dev_boot_phase = 0;
12289 
12290 	/* The loopback device is special if any other network devices
12291 	 * is present in a network namespace the loopback device must
12292 	 * be present. Since we now dynamically allocate and free the
12293 	 * loopback device ensure this invariant is maintained by
12294 	 * keeping the loopback device as the first device on the
12295 	 * list of network devices.  Ensuring the loopback devices
12296 	 * is the first device that appears and the last network device
12297 	 * that disappears.
12298 	 */
12299 	if (register_pernet_device(&loopback_net_ops))
12300 		goto out;
12301 
12302 	if (register_pernet_device(&default_device_ops))
12303 		goto out;
12304 
12305 	open_softirq(NET_TX_SOFTIRQ, net_tx_action);
12306 	open_softirq(NET_RX_SOFTIRQ, net_rx_action);
12307 
12308 	rc = cpuhp_setup_state_nocalls(CPUHP_NET_DEV_DEAD, "net/dev:dead",
12309 				       NULL, dev_cpu_dead);
12310 	WARN_ON(rc < 0);
12311 	rc = 0;
12312 
12313 	/* avoid static key IPIs to isolated CPUs */
12314 	if (housekeeping_enabled(HK_TYPE_MISC))
12315 		net_enable_timestamp();
12316 out:
12317 	if (rc < 0) {
12318 		for_each_possible_cpu(i) {
12319 			struct page_pool *pp_ptr;
12320 
12321 			pp_ptr = per_cpu(system_page_pool, i);
12322 			if (!pp_ptr)
12323 				continue;
12324 
12325 			xdp_unreg_page_pool(pp_ptr);
12326 			page_pool_destroy(pp_ptr);
12327 			per_cpu(system_page_pool, i) = NULL;
12328 		}
12329 	}
12330 
12331 	return rc;
12332 }
12333 
12334 subsys_initcall(net_dev_init);
12335