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