xref: /linux-6.15/include/linux/netdevice.h (revision bd7c0060)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * INET		An implementation of the TCP/IP protocol suite for the LINUX
4  *		operating system.  INET is implemented using the  BSD Socket
5  *		interface as the means of communication with the user level.
6  *
7  *		Definitions for the Interfaces handler.
8  *
9  * Version:	@(#)dev.h	1.0.10	08/12/93
10  *
11  * Authors:	Ross Biro
12  *		Fred N. van Kempen, <[email protected]>
13  *		Corey Minyard <[email protected]>
14  *		Donald J. Becker, <[email protected]>
15  *		Alan Cox, <[email protected]>
16  *		Bjorn Ekwall. <[email protected]>
17  *              Pekka Riikonen <[email protected]>
18  *
19  *		Moved to /usr/include/linux for NET3
20  */
21 #ifndef _LINUX_NETDEVICE_H
22 #define _LINUX_NETDEVICE_H
23 
24 #include <linux/timer.h>
25 #include <linux/bug.h>
26 #include <linux/delay.h>
27 #include <linux/atomic.h>
28 #include <linux/prefetch.h>
29 #include <asm/cache.h>
30 #include <asm/byteorder.h>
31 #include <asm/local.h>
32 
33 #include <linux/percpu.h>
34 #include <linux/rculist.h>
35 #include <linux/workqueue.h>
36 #include <linux/dynamic_queue_limits.h>
37 
38 #include <net/net_namespace.h>
39 #ifdef CONFIG_DCB
40 #include <net/dcbnl.h>
41 #endif
42 #include <net/netprio_cgroup.h>
43 #include <linux/netdev_features.h>
44 #include <linux/neighbour.h>
45 #include <linux/netdevice_xmit.h>
46 #include <uapi/linux/netdevice.h>
47 #include <uapi/linux/if_bonding.h>
48 #include <uapi/linux/pkt_cls.h>
49 #include <uapi/linux/netdev.h>
50 #include <linux/hashtable.h>
51 #include <linux/rbtree.h>
52 #include <net/net_trackers.h>
53 #include <net/net_debug.h>
54 #include <net/dropreason-core.h>
55 #include <net/neighbour_tables.h>
56 
57 struct netpoll_info;
58 struct device;
59 struct ethtool_ops;
60 struct kernel_hwtstamp_config;
61 struct phy_device;
62 struct dsa_port;
63 struct ip_tunnel_parm_kern;
64 struct macsec_context;
65 struct macsec_ops;
66 struct netdev_config;
67 struct netdev_name_node;
68 struct sd_flow_limit;
69 struct sfp_bus;
70 /* 802.11 specific */
71 struct wireless_dev;
72 /* 802.15.4 specific */
73 struct wpan_dev;
74 struct mpls_dev;
75 /* UDP Tunnel offloads */
76 struct udp_tunnel_info;
77 struct udp_tunnel_nic_info;
78 struct udp_tunnel_nic;
79 struct bpf_prog;
80 struct xdp_buff;
81 struct xdp_frame;
82 struct xdp_metadata_ops;
83 struct xdp_md;
84 struct ethtool_netdev_state;
85 struct phy_link_topology;
86 struct hwtstamp_provider;
87 
88 typedef u32 xdp_features_t;
89 
90 void synchronize_net(void);
91 void netdev_set_default_ethtool_ops(struct net_device *dev,
92 				    const struct ethtool_ops *ops);
93 void netdev_sw_irq_coalesce_default_on(struct net_device *dev);
94 
95 /* Backlog congestion levels */
96 #define NET_RX_SUCCESS		0	/* keep 'em coming, baby */
97 #define NET_RX_DROP		1	/* packet dropped */
98 
99 #define MAX_NEST_DEV 8
100 
101 /*
102  * Transmit return codes: transmit return codes originate from three different
103  * namespaces:
104  *
105  * - qdisc return codes
106  * - driver transmit return codes
107  * - errno values
108  *
109  * Drivers are allowed to return any one of those in their hard_start_xmit()
110  * function. Real network devices commonly used with qdiscs should only return
111  * the driver transmit return codes though - when qdiscs are used, the actual
112  * transmission happens asynchronously, so the value is not propagated to
113  * higher layers. Virtual network devices transmit synchronously; in this case
114  * the driver transmit return codes are consumed by dev_queue_xmit(), and all
115  * others are propagated to higher layers.
116  */
117 
118 /* qdisc ->enqueue() return codes. */
119 #define NET_XMIT_SUCCESS	0x00
120 #define NET_XMIT_DROP		0x01	/* skb dropped			*/
121 #define NET_XMIT_CN		0x02	/* congestion notification	*/
122 #define NET_XMIT_MASK		0x0f	/* qdisc flags in net/sch_generic.h */
123 
124 /* NET_XMIT_CN is special. It does not guarantee that this packet is lost. It
125  * indicates that the device will soon be dropping packets, or already drops
126  * some packets of the same priority; prompting us to send less aggressively. */
127 #define net_xmit_eval(e)	((e) == NET_XMIT_CN ? 0 : (e))
128 #define net_xmit_errno(e)	((e) != NET_XMIT_CN ? -ENOBUFS : 0)
129 
130 /* Driver transmit return codes */
131 #define NETDEV_TX_MASK		0xf0
132 
133 enum netdev_tx {
134 	__NETDEV_TX_MIN	 = INT_MIN,	/* make sure enum is signed */
135 	NETDEV_TX_OK	 = 0x00,	/* driver took care of packet */
136 	NETDEV_TX_BUSY	 = 0x10,	/* driver tx path was busy*/
137 };
138 typedef enum netdev_tx netdev_tx_t;
139 
140 /*
141  * Current order: NETDEV_TX_MASK > NET_XMIT_MASK >= 0 is significant;
142  * hard_start_xmit() return < NET_XMIT_MASK means skb was consumed.
143  */
144 static inline bool dev_xmit_complete(int rc)
145 {
146 	/*
147 	 * Positive cases with an skb consumed by a driver:
148 	 * - successful transmission (rc == NETDEV_TX_OK)
149 	 * - error while transmitting (rc < 0)
150 	 * - error while queueing to a different device (rc & NET_XMIT_MASK)
151 	 */
152 	if (likely(rc < NET_XMIT_MASK))
153 		return true;
154 
155 	return false;
156 }
157 
158 /*
159  *	Compute the worst-case header length according to the protocols
160  *	used.
161  */
162 
163 #if defined(CONFIG_HYPERV_NET)
164 # define LL_MAX_HEADER 128
165 #elif defined(CONFIG_WLAN) || IS_ENABLED(CONFIG_AX25)
166 # if defined(CONFIG_MAC80211_MESH)
167 #  define LL_MAX_HEADER 128
168 # else
169 #  define LL_MAX_HEADER 96
170 # endif
171 #else
172 # define LL_MAX_HEADER 32
173 #endif
174 
175 #if !IS_ENABLED(CONFIG_NET_IPIP) && !IS_ENABLED(CONFIG_NET_IPGRE) && \
176     !IS_ENABLED(CONFIG_IPV6_SIT) && !IS_ENABLED(CONFIG_IPV6_TUNNEL)
177 #define MAX_HEADER LL_MAX_HEADER
178 #else
179 #define MAX_HEADER (LL_MAX_HEADER + 48)
180 #endif
181 
182 /*
183  *	Old network device statistics. Fields are native words
184  *	(unsigned long) so they can be read and written atomically.
185  */
186 
187 #define NET_DEV_STAT(FIELD)			\
188 	union {					\
189 		unsigned long FIELD;		\
190 		atomic_long_t __##FIELD;	\
191 	}
192 
193 struct net_device_stats {
194 	NET_DEV_STAT(rx_packets);
195 	NET_DEV_STAT(tx_packets);
196 	NET_DEV_STAT(rx_bytes);
197 	NET_DEV_STAT(tx_bytes);
198 	NET_DEV_STAT(rx_errors);
199 	NET_DEV_STAT(tx_errors);
200 	NET_DEV_STAT(rx_dropped);
201 	NET_DEV_STAT(tx_dropped);
202 	NET_DEV_STAT(multicast);
203 	NET_DEV_STAT(collisions);
204 	NET_DEV_STAT(rx_length_errors);
205 	NET_DEV_STAT(rx_over_errors);
206 	NET_DEV_STAT(rx_crc_errors);
207 	NET_DEV_STAT(rx_frame_errors);
208 	NET_DEV_STAT(rx_fifo_errors);
209 	NET_DEV_STAT(rx_missed_errors);
210 	NET_DEV_STAT(tx_aborted_errors);
211 	NET_DEV_STAT(tx_carrier_errors);
212 	NET_DEV_STAT(tx_fifo_errors);
213 	NET_DEV_STAT(tx_heartbeat_errors);
214 	NET_DEV_STAT(tx_window_errors);
215 	NET_DEV_STAT(rx_compressed);
216 	NET_DEV_STAT(tx_compressed);
217 };
218 #undef NET_DEV_STAT
219 
220 /* per-cpu stats, allocated on demand.
221  * Try to fit them in a single cache line, for dev_get_stats() sake.
222  */
223 struct net_device_core_stats {
224 	unsigned long	rx_dropped;
225 	unsigned long	tx_dropped;
226 	unsigned long	rx_nohandler;
227 	unsigned long	rx_otherhost_dropped;
228 } __aligned(4 * sizeof(unsigned long));
229 
230 #include <linux/cache.h>
231 #include <linux/skbuff.h>
232 
233 struct neighbour;
234 struct neigh_parms;
235 struct sk_buff;
236 
237 struct netdev_hw_addr {
238 	struct list_head	list;
239 	struct rb_node		node;
240 	unsigned char		addr[MAX_ADDR_LEN];
241 	unsigned char		type;
242 #define NETDEV_HW_ADDR_T_LAN		1
243 #define NETDEV_HW_ADDR_T_SAN		2
244 #define NETDEV_HW_ADDR_T_UNICAST	3
245 #define NETDEV_HW_ADDR_T_MULTICAST	4
246 	bool			global_use;
247 	int			sync_cnt;
248 	int			refcount;
249 	int			synced;
250 	struct rcu_head		rcu_head;
251 };
252 
253 struct netdev_hw_addr_list {
254 	struct list_head	list;
255 	int			count;
256 
257 	/* Auxiliary tree for faster lookup on addition and deletion */
258 	struct rb_root		tree;
259 };
260 
261 #define netdev_hw_addr_list_count(l) ((l)->count)
262 #define netdev_hw_addr_list_empty(l) (netdev_hw_addr_list_count(l) == 0)
263 #define netdev_hw_addr_list_for_each(ha, l) \
264 	list_for_each_entry(ha, &(l)->list, list)
265 
266 #define netdev_uc_count(dev) netdev_hw_addr_list_count(&(dev)->uc)
267 #define netdev_uc_empty(dev) netdev_hw_addr_list_empty(&(dev)->uc)
268 #define netdev_for_each_uc_addr(ha, dev) \
269 	netdev_hw_addr_list_for_each(ha, &(dev)->uc)
270 #define netdev_for_each_synced_uc_addr(_ha, _dev) \
271 	netdev_for_each_uc_addr((_ha), (_dev)) \
272 		if ((_ha)->sync_cnt)
273 
274 #define netdev_mc_count(dev) netdev_hw_addr_list_count(&(dev)->mc)
275 #define netdev_mc_empty(dev) netdev_hw_addr_list_empty(&(dev)->mc)
276 #define netdev_for_each_mc_addr(ha, dev) \
277 	netdev_hw_addr_list_for_each(ha, &(dev)->mc)
278 #define netdev_for_each_synced_mc_addr(_ha, _dev) \
279 	netdev_for_each_mc_addr((_ha), (_dev)) \
280 		if ((_ha)->sync_cnt)
281 
282 struct hh_cache {
283 	unsigned int	hh_len;
284 	seqlock_t	hh_lock;
285 
286 	/* cached hardware header; allow for machine alignment needs.        */
287 #define HH_DATA_MOD	16
288 #define HH_DATA_OFF(__len) \
289 	(HH_DATA_MOD - (((__len - 1) & (HH_DATA_MOD - 1)) + 1))
290 #define HH_DATA_ALIGN(__len) \
291 	(((__len)+(HH_DATA_MOD-1))&~(HH_DATA_MOD - 1))
292 	unsigned long	hh_data[HH_DATA_ALIGN(LL_MAX_HEADER) / sizeof(long)];
293 };
294 
295 /* Reserve HH_DATA_MOD byte-aligned hard_header_len, but at least that much.
296  * Alternative is:
297  *   dev->hard_header_len ? (dev->hard_header_len +
298  *                           (HH_DATA_MOD - 1)) & ~(HH_DATA_MOD - 1) : 0
299  *
300  * We could use other alignment values, but we must maintain the
301  * relationship HH alignment <= LL alignment.
302  */
303 #define LL_RESERVED_SPACE(dev) \
304 	((((dev)->hard_header_len + READ_ONCE((dev)->needed_headroom)) \
305 	  & ~(HH_DATA_MOD - 1)) + HH_DATA_MOD)
306 #define LL_RESERVED_SPACE_EXTRA(dev,extra) \
307 	((((dev)->hard_header_len + READ_ONCE((dev)->needed_headroom) + (extra)) \
308 	  & ~(HH_DATA_MOD - 1)) + HH_DATA_MOD)
309 
310 struct header_ops {
311 	int	(*create) (struct sk_buff *skb, struct net_device *dev,
312 			   unsigned short type, const void *daddr,
313 			   const void *saddr, unsigned int len);
314 	int	(*parse)(const struct sk_buff *skb, unsigned char *haddr);
315 	int	(*cache)(const struct neighbour *neigh, struct hh_cache *hh, __be16 type);
316 	void	(*cache_update)(struct hh_cache *hh,
317 				const struct net_device *dev,
318 				const unsigned char *haddr);
319 	bool	(*validate)(const char *ll_header, unsigned int len);
320 	__be16	(*parse_protocol)(const struct sk_buff *skb);
321 };
322 
323 /* These flag bits are private to the generic network queueing
324  * layer; they may not be explicitly referenced by any other
325  * code.
326  */
327 
328 enum netdev_state_t {
329 	__LINK_STATE_START,
330 	__LINK_STATE_PRESENT,
331 	__LINK_STATE_NOCARRIER,
332 	__LINK_STATE_LINKWATCH_PENDING,
333 	__LINK_STATE_DORMANT,
334 	__LINK_STATE_TESTING,
335 };
336 
337 struct gro_list {
338 	struct list_head	list;
339 	int			count;
340 };
341 
342 /*
343  * size of gro hash buckets, must less than bit number of
344  * napi_struct::gro_bitmask
345  */
346 #define GRO_HASH_BUCKETS	8
347 
348 /*
349  * Structure for per-NAPI config
350  */
351 struct napi_config {
352 	u64 gro_flush_timeout;
353 	u64 irq_suspend_timeout;
354 	u32 defer_hard_irqs;
355 	cpumask_t affinity_mask;
356 	unsigned int napi_id;
357 };
358 
359 /*
360  * Structure for NAPI scheduling similar to tasklet but with weighting
361  */
362 struct napi_struct {
363 	/* The poll_list must only be managed by the entity which
364 	 * changes the state of the NAPI_STATE_SCHED bit.  This means
365 	 * whoever atomically sets that bit can add this napi_struct
366 	 * to the per-CPU poll_list, and whoever clears that bit
367 	 * can remove from the list right before clearing the bit.
368 	 */
369 	struct list_head	poll_list;
370 
371 	unsigned long		state;
372 	int			weight;
373 	u32			defer_hard_irqs_count;
374 	unsigned long		gro_bitmask;
375 	int			(*poll)(struct napi_struct *, int);
376 #ifdef CONFIG_NETPOLL
377 	/* CPU actively polling if netpoll is configured */
378 	int			poll_owner;
379 #endif
380 	/* CPU on which NAPI has been scheduled for processing */
381 	int			list_owner;
382 	struct net_device	*dev;
383 	struct gro_list		gro_hash[GRO_HASH_BUCKETS];
384 	struct sk_buff		*skb;
385 	struct list_head	rx_list; /* Pending GRO_NORMAL skbs */
386 	int			rx_count; /* length of rx_list */
387 	unsigned int		napi_id; /* protected by netdev_lock */
388 	struct hrtimer		timer;
389 	/* all fields past this point are write-protected by netdev_lock */
390 	struct task_struct	*thread;
391 	unsigned long		gro_flush_timeout;
392 	unsigned long		irq_suspend_timeout;
393 	u32			defer_hard_irqs;
394 	/* control-path-only fields follow */
395 	struct list_head	dev_list;
396 	struct hlist_node	napi_hash_node;
397 	int			irq;
398 	struct irq_affinity_notify notify;
399 	int			napi_rmap_idx;
400 	int			index;
401 	struct napi_config	*config;
402 };
403 
404 enum {
405 	NAPI_STATE_SCHED,		/* Poll is scheduled */
406 	NAPI_STATE_MISSED,		/* reschedule a napi */
407 	NAPI_STATE_DISABLE,		/* Disable pending */
408 	NAPI_STATE_NPSVC,		/* Netpoll - don't dequeue from poll_list */
409 	NAPI_STATE_LISTED,		/* NAPI added to system lists */
410 	NAPI_STATE_NO_BUSY_POLL,	/* Do not add in napi_hash, no busy polling */
411 	NAPI_STATE_IN_BUSY_POLL,	/* sk_busy_loop() owns this NAPI */
412 	NAPI_STATE_PREFER_BUSY_POLL,	/* prefer busy-polling over softirq processing*/
413 	NAPI_STATE_THREADED,		/* The poll is performed inside its own thread*/
414 	NAPI_STATE_SCHED_THREADED,	/* Napi is currently scheduled in threaded mode */
415 	NAPI_STATE_HAS_NOTIFIER,	/* Napi has an IRQ notifier */
416 };
417 
418 enum {
419 	NAPIF_STATE_SCHED		= BIT(NAPI_STATE_SCHED),
420 	NAPIF_STATE_MISSED		= BIT(NAPI_STATE_MISSED),
421 	NAPIF_STATE_DISABLE		= BIT(NAPI_STATE_DISABLE),
422 	NAPIF_STATE_NPSVC		= BIT(NAPI_STATE_NPSVC),
423 	NAPIF_STATE_LISTED		= BIT(NAPI_STATE_LISTED),
424 	NAPIF_STATE_NO_BUSY_POLL	= BIT(NAPI_STATE_NO_BUSY_POLL),
425 	NAPIF_STATE_IN_BUSY_POLL	= BIT(NAPI_STATE_IN_BUSY_POLL),
426 	NAPIF_STATE_PREFER_BUSY_POLL	= BIT(NAPI_STATE_PREFER_BUSY_POLL),
427 	NAPIF_STATE_THREADED		= BIT(NAPI_STATE_THREADED),
428 	NAPIF_STATE_SCHED_THREADED	= BIT(NAPI_STATE_SCHED_THREADED),
429 	NAPIF_STATE_HAS_NOTIFIER	= BIT(NAPI_STATE_HAS_NOTIFIER),
430 };
431 
432 enum gro_result {
433 	GRO_MERGED,
434 	GRO_MERGED_FREE,
435 	GRO_HELD,
436 	GRO_NORMAL,
437 	GRO_CONSUMED,
438 };
439 typedef enum gro_result gro_result_t;
440 
441 /*
442  * enum rx_handler_result - Possible return values for rx_handlers.
443  * @RX_HANDLER_CONSUMED: skb was consumed by rx_handler, do not process it
444  * further.
445  * @RX_HANDLER_ANOTHER: Do another round in receive path. This is indicated in
446  * case skb->dev was changed by rx_handler.
447  * @RX_HANDLER_EXACT: Force exact delivery, no wildcard.
448  * @RX_HANDLER_PASS: Do nothing, pass the skb as if no rx_handler was called.
449  *
450  * rx_handlers are functions called from inside __netif_receive_skb(), to do
451  * special processing of the skb, prior to delivery to protocol handlers.
452  *
453  * Currently, a net_device can only have a single rx_handler registered. Trying
454  * to register a second rx_handler will return -EBUSY.
455  *
456  * To register a rx_handler on a net_device, use netdev_rx_handler_register().
457  * To unregister a rx_handler on a net_device, use
458  * netdev_rx_handler_unregister().
459  *
460  * Upon return, rx_handler is expected to tell __netif_receive_skb() what to
461  * do with the skb.
462  *
463  * If the rx_handler consumed the skb in some way, it should return
464  * RX_HANDLER_CONSUMED. This is appropriate when the rx_handler arranged for
465  * the skb to be delivered in some other way.
466  *
467  * If the rx_handler changed skb->dev, to divert the skb to another
468  * net_device, it should return RX_HANDLER_ANOTHER. The rx_handler for the
469  * new device will be called if it exists.
470  *
471  * If the rx_handler decides the skb should be ignored, it should return
472  * RX_HANDLER_EXACT. The skb will only be delivered to protocol handlers that
473  * are registered on exact device (ptype->dev == skb->dev).
474  *
475  * If the rx_handler didn't change skb->dev, but wants the skb to be normally
476  * delivered, it should return RX_HANDLER_PASS.
477  *
478  * A device without a registered rx_handler will behave as if rx_handler
479  * returned RX_HANDLER_PASS.
480  */
481 
482 enum rx_handler_result {
483 	RX_HANDLER_CONSUMED,
484 	RX_HANDLER_ANOTHER,
485 	RX_HANDLER_EXACT,
486 	RX_HANDLER_PASS,
487 };
488 typedef enum rx_handler_result rx_handler_result_t;
489 typedef rx_handler_result_t rx_handler_func_t(struct sk_buff **pskb);
490 
491 void __napi_schedule(struct napi_struct *n);
492 void __napi_schedule_irqoff(struct napi_struct *n);
493 
494 static inline bool napi_disable_pending(struct napi_struct *n)
495 {
496 	return test_bit(NAPI_STATE_DISABLE, &n->state);
497 }
498 
499 static inline bool napi_prefer_busy_poll(struct napi_struct *n)
500 {
501 	return test_bit(NAPI_STATE_PREFER_BUSY_POLL, &n->state);
502 }
503 
504 /**
505  * napi_is_scheduled - test if NAPI is scheduled
506  * @n: NAPI context
507  *
508  * This check is "best-effort". With no locking implemented,
509  * a NAPI can be scheduled or terminate right after this check
510  * and produce not precise results.
511  *
512  * NAPI_STATE_SCHED is an internal state, napi_is_scheduled
513  * should not be used normally and napi_schedule should be
514  * used instead.
515  *
516  * Use only if the driver really needs to check if a NAPI
517  * is scheduled for example in the context of delayed timer
518  * that can be skipped if a NAPI is already scheduled.
519  *
520  * Return: True if NAPI is scheduled, False otherwise.
521  */
522 static inline bool napi_is_scheduled(struct napi_struct *n)
523 {
524 	return test_bit(NAPI_STATE_SCHED, &n->state);
525 }
526 
527 bool napi_schedule_prep(struct napi_struct *n);
528 
529 /**
530  *	napi_schedule - schedule NAPI poll
531  *	@n: NAPI context
532  *
533  * Schedule NAPI poll routine to be called if it is not already
534  * running.
535  * Return: true if we schedule a NAPI or false if not.
536  * Refer to napi_schedule_prep() for additional reason on why
537  * a NAPI might not be scheduled.
538  */
539 static inline bool napi_schedule(struct napi_struct *n)
540 {
541 	if (napi_schedule_prep(n)) {
542 		__napi_schedule(n);
543 		return true;
544 	}
545 
546 	return false;
547 }
548 
549 /**
550  *	napi_schedule_irqoff - schedule NAPI poll
551  *	@n: NAPI context
552  *
553  * Variant of napi_schedule(), assuming hard irqs are masked.
554  */
555 static inline void napi_schedule_irqoff(struct napi_struct *n)
556 {
557 	if (napi_schedule_prep(n))
558 		__napi_schedule_irqoff(n);
559 }
560 
561 /**
562  * napi_complete_done - NAPI processing complete
563  * @n: NAPI context
564  * @work_done: number of packets processed
565  *
566  * Mark NAPI processing as complete. Should only be called if poll budget
567  * has not been completely consumed.
568  * Prefer over napi_complete().
569  * Return: false if device should avoid rearming interrupts.
570  */
571 bool napi_complete_done(struct napi_struct *n, int work_done);
572 
573 static inline bool napi_complete(struct napi_struct *n)
574 {
575 	return napi_complete_done(n, 0);
576 }
577 
578 int dev_set_threaded(struct net_device *dev, bool threaded);
579 
580 void napi_disable(struct napi_struct *n);
581 void napi_disable_locked(struct napi_struct *n);
582 
583 void napi_enable(struct napi_struct *n);
584 void napi_enable_locked(struct napi_struct *n);
585 
586 /**
587  *	napi_synchronize - wait until NAPI is not running
588  *	@n: NAPI context
589  *
590  * Wait until NAPI is done being scheduled on this context.
591  * Waits till any outstanding processing completes but
592  * does not disable future activations.
593  */
594 static inline void napi_synchronize(const struct napi_struct *n)
595 {
596 	if (IS_ENABLED(CONFIG_SMP))
597 		while (test_bit(NAPI_STATE_SCHED, &n->state))
598 			msleep(1);
599 	else
600 		barrier();
601 }
602 
603 /**
604  *	napi_if_scheduled_mark_missed - if napi is running, set the
605  *	NAPIF_STATE_MISSED
606  *	@n: NAPI context
607  *
608  * If napi is running, set the NAPIF_STATE_MISSED, and return true if
609  * NAPI is scheduled.
610  **/
611 static inline bool napi_if_scheduled_mark_missed(struct napi_struct *n)
612 {
613 	unsigned long val, new;
614 
615 	val = READ_ONCE(n->state);
616 	do {
617 		if (val & NAPIF_STATE_DISABLE)
618 			return true;
619 
620 		if (!(val & NAPIF_STATE_SCHED))
621 			return false;
622 
623 		new = val | NAPIF_STATE_MISSED;
624 	} while (!try_cmpxchg(&n->state, &val, new));
625 
626 	return true;
627 }
628 
629 enum netdev_queue_state_t {
630 	__QUEUE_STATE_DRV_XOFF,
631 	__QUEUE_STATE_STACK_XOFF,
632 	__QUEUE_STATE_FROZEN,
633 };
634 
635 #define QUEUE_STATE_DRV_XOFF	(1 << __QUEUE_STATE_DRV_XOFF)
636 #define QUEUE_STATE_STACK_XOFF	(1 << __QUEUE_STATE_STACK_XOFF)
637 #define QUEUE_STATE_FROZEN	(1 << __QUEUE_STATE_FROZEN)
638 
639 #define QUEUE_STATE_ANY_XOFF	(QUEUE_STATE_DRV_XOFF | QUEUE_STATE_STACK_XOFF)
640 #define QUEUE_STATE_ANY_XOFF_OR_FROZEN (QUEUE_STATE_ANY_XOFF | \
641 					QUEUE_STATE_FROZEN)
642 #define QUEUE_STATE_DRV_XOFF_OR_FROZEN (QUEUE_STATE_DRV_XOFF | \
643 					QUEUE_STATE_FROZEN)
644 
645 /*
646  * __QUEUE_STATE_DRV_XOFF is used by drivers to stop the transmit queue.  The
647  * netif_tx_* functions below are used to manipulate this flag.  The
648  * __QUEUE_STATE_STACK_XOFF flag is used by the stack to stop the transmit
649  * queue independently.  The netif_xmit_*stopped functions below are called
650  * to check if the queue has been stopped by the driver or stack (either
651  * of the XOFF bits are set in the state).  Drivers should not need to call
652  * netif_xmit*stopped functions, they should only be using netif_tx_*.
653  */
654 
655 struct netdev_queue {
656 /*
657  * read-mostly part
658  */
659 	struct net_device	*dev;
660 	netdevice_tracker	dev_tracker;
661 
662 	struct Qdisc __rcu	*qdisc;
663 	struct Qdisc __rcu	*qdisc_sleeping;
664 #ifdef CONFIG_SYSFS
665 	struct kobject		kobj;
666 	const struct attribute_group	**groups;
667 #endif
668 	unsigned long		tx_maxrate;
669 	/*
670 	 * Number of TX timeouts for this queue
671 	 * (/sys/class/net/DEV/Q/trans_timeout)
672 	 */
673 	atomic_long_t		trans_timeout;
674 
675 	/* Subordinate device that the queue has been assigned to */
676 	struct net_device	*sb_dev;
677 #ifdef CONFIG_XDP_SOCKETS
678 	struct xsk_buff_pool    *pool;
679 #endif
680 
681 /*
682  * write-mostly part
683  */
684 #ifdef CONFIG_BQL
685 	struct dql		dql;
686 #endif
687 	spinlock_t		_xmit_lock ____cacheline_aligned_in_smp;
688 	int			xmit_lock_owner;
689 	/*
690 	 * Time (in jiffies) of last Tx
691 	 */
692 	unsigned long		trans_start;
693 
694 	unsigned long		state;
695 
696 /*
697  * slow- / control-path part
698  */
699 	/* NAPI instance for the queue
700 	 * Readers and writers must hold RTNL
701 	 */
702 	struct napi_struct	*napi;
703 
704 #if defined(CONFIG_XPS) && defined(CONFIG_NUMA)
705 	int			numa_node;
706 #endif
707 } ____cacheline_aligned_in_smp;
708 
709 extern int sysctl_fb_tunnels_only_for_init_net;
710 extern int sysctl_devconf_inherit_init_net;
711 
712 /*
713  * sysctl_fb_tunnels_only_for_init_net == 0 : For all netns
714  *                                     == 1 : For initns only
715  *                                     == 2 : For none.
716  */
717 static inline bool net_has_fallback_tunnels(const struct net *net)
718 {
719 #if IS_ENABLED(CONFIG_SYSCTL)
720 	int fb_tunnels_only_for_init_net = READ_ONCE(sysctl_fb_tunnels_only_for_init_net);
721 
722 	return !fb_tunnels_only_for_init_net ||
723 		(net_eq(net, &init_net) && fb_tunnels_only_for_init_net == 1);
724 #else
725 	return true;
726 #endif
727 }
728 
729 static inline int net_inherit_devconf(void)
730 {
731 #if IS_ENABLED(CONFIG_SYSCTL)
732 	return READ_ONCE(sysctl_devconf_inherit_init_net);
733 #else
734 	return 0;
735 #endif
736 }
737 
738 static inline int netdev_queue_numa_node_read(const struct netdev_queue *q)
739 {
740 #if defined(CONFIG_XPS) && defined(CONFIG_NUMA)
741 	return q->numa_node;
742 #else
743 	return NUMA_NO_NODE;
744 #endif
745 }
746 
747 static inline void netdev_queue_numa_node_write(struct netdev_queue *q, int node)
748 {
749 #if defined(CONFIG_XPS) && defined(CONFIG_NUMA)
750 	q->numa_node = node;
751 #endif
752 }
753 
754 #ifdef CONFIG_RFS_ACCEL
755 bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index, u32 flow_id,
756 			 u16 filter_id);
757 #endif
758 
759 /* XPS map type and offset of the xps map within net_device->xps_maps[]. */
760 enum xps_map_type {
761 	XPS_CPUS = 0,
762 	XPS_RXQS,
763 	XPS_MAPS_MAX,
764 };
765 
766 #ifdef CONFIG_XPS
767 /*
768  * This structure holds an XPS map which can be of variable length.  The
769  * map is an array of queues.
770  */
771 struct xps_map {
772 	unsigned int len;
773 	unsigned int alloc_len;
774 	struct rcu_head rcu;
775 	u16 queues[];
776 };
777 #define XPS_MAP_SIZE(_num) (sizeof(struct xps_map) + ((_num) * sizeof(u16)))
778 #define XPS_MIN_MAP_ALLOC ((L1_CACHE_ALIGN(offsetof(struct xps_map, queues[1])) \
779        - sizeof(struct xps_map)) / sizeof(u16))
780 
781 /*
782  * This structure holds all XPS maps for device.  Maps are indexed by CPU.
783  *
784  * We keep track of the number of cpus/rxqs used when the struct is allocated,
785  * in nr_ids. This will help not accessing out-of-bound memory.
786  *
787  * We keep track of the number of traffic classes used when the struct is
788  * allocated, in num_tc. This will be used to navigate the maps, to ensure we're
789  * not crossing its upper bound, as the original dev->num_tc can be updated in
790  * the meantime.
791  */
792 struct xps_dev_maps {
793 	struct rcu_head rcu;
794 	unsigned int nr_ids;
795 	s16 num_tc;
796 	struct xps_map __rcu *attr_map[]; /* Either CPUs map or RXQs map */
797 };
798 
799 #define XPS_CPU_DEV_MAPS_SIZE(_tcs) (sizeof(struct xps_dev_maps) +	\
800 	(nr_cpu_ids * (_tcs) * sizeof(struct xps_map *)))
801 
802 #define XPS_RXQ_DEV_MAPS_SIZE(_tcs, _rxqs) (sizeof(struct xps_dev_maps) +\
803 	(_rxqs * (_tcs) * sizeof(struct xps_map *)))
804 
805 #endif /* CONFIG_XPS */
806 
807 #define TC_MAX_QUEUE	16
808 #define TC_BITMASK	15
809 /* HW offloaded queuing disciplines txq count and offset maps */
810 struct netdev_tc_txq {
811 	u16 count;
812 	u16 offset;
813 };
814 
815 #if defined(CONFIG_FCOE) || defined(CONFIG_FCOE_MODULE)
816 /*
817  * This structure is to hold information about the device
818  * configured to run FCoE protocol stack.
819  */
820 struct netdev_fcoe_hbainfo {
821 	char	manufacturer[64];
822 	char	serial_number[64];
823 	char	hardware_version[64];
824 	char	driver_version[64];
825 	char	optionrom_version[64];
826 	char	firmware_version[64];
827 	char	model[256];
828 	char	model_description[256];
829 };
830 #endif
831 
832 #define MAX_PHYS_ITEM_ID_LEN 32
833 
834 /* This structure holds a unique identifier to identify some
835  * physical item (port for example) used by a netdevice.
836  */
837 struct netdev_phys_item_id {
838 	unsigned char id[MAX_PHYS_ITEM_ID_LEN];
839 	unsigned char id_len;
840 };
841 
842 static inline bool netdev_phys_item_id_same(struct netdev_phys_item_id *a,
843 					    struct netdev_phys_item_id *b)
844 {
845 	return a->id_len == b->id_len &&
846 	       memcmp(a->id, b->id, a->id_len) == 0;
847 }
848 
849 typedef u16 (*select_queue_fallback_t)(struct net_device *dev,
850 				       struct sk_buff *skb,
851 				       struct net_device *sb_dev);
852 
853 enum net_device_path_type {
854 	DEV_PATH_ETHERNET = 0,
855 	DEV_PATH_VLAN,
856 	DEV_PATH_BRIDGE,
857 	DEV_PATH_PPPOE,
858 	DEV_PATH_DSA,
859 	DEV_PATH_MTK_WDMA,
860 };
861 
862 struct net_device_path {
863 	enum net_device_path_type	type;
864 	const struct net_device		*dev;
865 	union {
866 		struct {
867 			u16		id;
868 			__be16		proto;
869 			u8		h_dest[ETH_ALEN];
870 		} encap;
871 		struct {
872 			enum {
873 				DEV_PATH_BR_VLAN_KEEP,
874 				DEV_PATH_BR_VLAN_TAG,
875 				DEV_PATH_BR_VLAN_UNTAG,
876 				DEV_PATH_BR_VLAN_UNTAG_HW,
877 			}		vlan_mode;
878 			u16		vlan_id;
879 			__be16		vlan_proto;
880 		} bridge;
881 		struct {
882 			int port;
883 			u16 proto;
884 		} dsa;
885 		struct {
886 			u8 wdma_idx;
887 			u8 queue;
888 			u16 wcid;
889 			u8 bss;
890 			u8 amsdu;
891 		} mtk_wdma;
892 	};
893 };
894 
895 #define NET_DEVICE_PATH_STACK_MAX	5
896 #define NET_DEVICE_PATH_VLAN_MAX	2
897 
898 struct net_device_path_stack {
899 	int			num_paths;
900 	struct net_device_path	path[NET_DEVICE_PATH_STACK_MAX];
901 };
902 
903 struct net_device_path_ctx {
904 	const struct net_device *dev;
905 	u8			daddr[ETH_ALEN];
906 
907 	int			num_vlans;
908 	struct {
909 		u16		id;
910 		__be16		proto;
911 	} vlan[NET_DEVICE_PATH_VLAN_MAX];
912 };
913 
914 enum tc_setup_type {
915 	TC_QUERY_CAPS,
916 	TC_SETUP_QDISC_MQPRIO,
917 	TC_SETUP_CLSU32,
918 	TC_SETUP_CLSFLOWER,
919 	TC_SETUP_CLSMATCHALL,
920 	TC_SETUP_CLSBPF,
921 	TC_SETUP_BLOCK,
922 	TC_SETUP_QDISC_CBS,
923 	TC_SETUP_QDISC_RED,
924 	TC_SETUP_QDISC_PRIO,
925 	TC_SETUP_QDISC_MQ,
926 	TC_SETUP_QDISC_ETF,
927 	TC_SETUP_ROOT_QDISC,
928 	TC_SETUP_QDISC_GRED,
929 	TC_SETUP_QDISC_TAPRIO,
930 	TC_SETUP_FT,
931 	TC_SETUP_QDISC_ETS,
932 	TC_SETUP_QDISC_TBF,
933 	TC_SETUP_QDISC_FIFO,
934 	TC_SETUP_QDISC_HTB,
935 	TC_SETUP_ACT,
936 };
937 
938 /* These structures hold the attributes of bpf state that are being passed
939  * to the netdevice through the bpf op.
940  */
941 enum bpf_netdev_command {
942 	/* Set or clear a bpf program used in the earliest stages of packet
943 	 * rx. The prog will have been loaded as BPF_PROG_TYPE_XDP. The callee
944 	 * is responsible for calling bpf_prog_put on any old progs that are
945 	 * stored. In case of error, the callee need not release the new prog
946 	 * reference, but on success it takes ownership and must bpf_prog_put
947 	 * when it is no longer used.
948 	 */
949 	XDP_SETUP_PROG,
950 	XDP_SETUP_PROG_HW,
951 	/* BPF program for offload callbacks, invoked at program load time. */
952 	BPF_OFFLOAD_MAP_ALLOC,
953 	BPF_OFFLOAD_MAP_FREE,
954 	XDP_SETUP_XSK_POOL,
955 };
956 
957 struct bpf_prog_offload_ops;
958 struct netlink_ext_ack;
959 struct xdp_umem;
960 struct xdp_dev_bulk_queue;
961 struct bpf_xdp_link;
962 
963 enum bpf_xdp_mode {
964 	XDP_MODE_SKB = 0,
965 	XDP_MODE_DRV = 1,
966 	XDP_MODE_HW = 2,
967 	__MAX_XDP_MODE
968 };
969 
970 struct bpf_xdp_entity {
971 	struct bpf_prog *prog;
972 	struct bpf_xdp_link *link;
973 };
974 
975 struct netdev_bpf {
976 	enum bpf_netdev_command command;
977 	union {
978 		/* XDP_SETUP_PROG */
979 		struct {
980 			u32 flags;
981 			struct bpf_prog *prog;
982 			struct netlink_ext_ack *extack;
983 		};
984 		/* BPF_OFFLOAD_MAP_ALLOC, BPF_OFFLOAD_MAP_FREE */
985 		struct {
986 			struct bpf_offloaded_map *offmap;
987 		};
988 		/* XDP_SETUP_XSK_POOL */
989 		struct {
990 			struct xsk_buff_pool *pool;
991 			u16 queue_id;
992 		} xsk;
993 	};
994 };
995 
996 /* Flags for ndo_xsk_wakeup. */
997 #define XDP_WAKEUP_RX (1 << 0)
998 #define XDP_WAKEUP_TX (1 << 1)
999 
1000 #ifdef CONFIG_XFRM_OFFLOAD
1001 struct xfrmdev_ops {
1002 	int	(*xdo_dev_state_add) (struct xfrm_state *x, struct netlink_ext_ack *extack);
1003 	void	(*xdo_dev_state_delete) (struct xfrm_state *x);
1004 	void	(*xdo_dev_state_free) (struct xfrm_state *x);
1005 	bool	(*xdo_dev_offload_ok) (struct sk_buff *skb,
1006 				       struct xfrm_state *x);
1007 	void	(*xdo_dev_state_advance_esn) (struct xfrm_state *x);
1008 	void	(*xdo_dev_state_update_stats) (struct xfrm_state *x);
1009 	int	(*xdo_dev_policy_add) (struct xfrm_policy *x, struct netlink_ext_ack *extack);
1010 	void	(*xdo_dev_policy_delete) (struct xfrm_policy *x);
1011 	void	(*xdo_dev_policy_free) (struct xfrm_policy *x);
1012 };
1013 #endif
1014 
1015 struct dev_ifalias {
1016 	struct rcu_head rcuhead;
1017 	char ifalias[];
1018 };
1019 
1020 struct devlink;
1021 struct tlsdev_ops;
1022 
1023 struct netdev_net_notifier {
1024 	struct list_head list;
1025 	struct notifier_block *nb;
1026 };
1027 
1028 /*
1029  * This structure defines the management hooks for network devices.
1030  * The following hooks can be defined; unless noted otherwise, they are
1031  * optional and can be filled with a null pointer.
1032  *
1033  * int (*ndo_init)(struct net_device *dev);
1034  *     This function is called once when a network device is registered.
1035  *     The network device can use this for any late stage initialization
1036  *     or semantic validation. It can fail with an error code which will
1037  *     be propagated back to register_netdev.
1038  *
1039  * void (*ndo_uninit)(struct net_device *dev);
1040  *     This function is called when device is unregistered or when registration
1041  *     fails. It is not called if init fails.
1042  *
1043  * int (*ndo_open)(struct net_device *dev);
1044  *     This function is called when a network device transitions to the up
1045  *     state.
1046  *
1047  * int (*ndo_stop)(struct net_device *dev);
1048  *     This function is called when a network device transitions to the down
1049  *     state.
1050  *
1051  * netdev_tx_t (*ndo_start_xmit)(struct sk_buff *skb,
1052  *                               struct net_device *dev);
1053  *	Called when a packet needs to be transmitted.
1054  *	Returns NETDEV_TX_OK.  Can return NETDEV_TX_BUSY, but you should stop
1055  *	the queue before that can happen; it's for obsolete devices and weird
1056  *	corner cases, but the stack really does a non-trivial amount
1057  *	of useless work if you return NETDEV_TX_BUSY.
1058  *	Required; cannot be NULL.
1059  *
1060  * netdev_features_t (*ndo_features_check)(struct sk_buff *skb,
1061  *					   struct net_device *dev
1062  *					   netdev_features_t features);
1063  *	Called by core transmit path to determine if device is capable of
1064  *	performing offload operations on a given packet. This is to give
1065  *	the device an opportunity to implement any restrictions that cannot
1066  *	be otherwise expressed by feature flags. The check is called with
1067  *	the set of features that the stack has calculated and it returns
1068  *	those the driver believes to be appropriate.
1069  *
1070  * u16 (*ndo_select_queue)(struct net_device *dev, struct sk_buff *skb,
1071  *                         struct net_device *sb_dev);
1072  *	Called to decide which queue to use when device supports multiple
1073  *	transmit queues.
1074  *
1075  * void (*ndo_change_rx_flags)(struct net_device *dev, int flags);
1076  *	This function is called to allow device receiver to make
1077  *	changes to configuration when multicast or promiscuous is enabled.
1078  *
1079  * void (*ndo_set_rx_mode)(struct net_device *dev);
1080  *	This function is called device changes address list filtering.
1081  *	If driver handles unicast address filtering, it should set
1082  *	IFF_UNICAST_FLT in its priv_flags.
1083  *
1084  * int (*ndo_set_mac_address)(struct net_device *dev, void *addr);
1085  *	This function  is called when the Media Access Control address
1086  *	needs to be changed. If this interface is not defined, the
1087  *	MAC address can not be changed.
1088  *
1089  * int (*ndo_validate_addr)(struct net_device *dev);
1090  *	Test if Media Access Control address is valid for the device.
1091  *
1092  * int (*ndo_do_ioctl)(struct net_device *dev, struct ifreq *ifr, int cmd);
1093  *	Old-style ioctl entry point. This is used internally by the
1094  *	ieee802154 subsystem but is no longer called by the device
1095  *	ioctl handler.
1096  *
1097  * int (*ndo_siocbond)(struct net_device *dev, struct ifreq *ifr, int cmd);
1098  *	Used by the bonding driver for its device specific ioctls:
1099  *	SIOCBONDENSLAVE, SIOCBONDRELEASE, SIOCBONDSETHWADDR, SIOCBONDCHANGEACTIVE,
1100  *	SIOCBONDSLAVEINFOQUERY, and SIOCBONDINFOQUERY
1101  *
1102  * * int (*ndo_eth_ioctl)(struct net_device *dev, struct ifreq *ifr, int cmd);
1103  *	Called for ethernet specific ioctls: SIOCGMIIPHY, SIOCGMIIREG,
1104  *	SIOCSMIIREG, SIOCSHWTSTAMP and SIOCGHWTSTAMP.
1105  *
1106  * int (*ndo_set_config)(struct net_device *dev, struct ifmap *map);
1107  *	Used to set network devices bus interface parameters. This interface
1108  *	is retained for legacy reasons; new devices should use the bus
1109  *	interface (PCI) for low level management.
1110  *
1111  * int (*ndo_change_mtu)(struct net_device *dev, int new_mtu);
1112  *	Called when a user wants to change the Maximum Transfer Unit
1113  *	of a device.
1114  *
1115  * void (*ndo_tx_timeout)(struct net_device *dev, unsigned int txqueue);
1116  *	Callback used when the transmitter has not made any progress
1117  *	for dev->watchdog ticks.
1118  *
1119  * void (*ndo_get_stats64)(struct net_device *dev,
1120  *                         struct rtnl_link_stats64 *storage);
1121  * struct net_device_stats* (*ndo_get_stats)(struct net_device *dev);
1122  *	Called when a user wants to get the network device usage
1123  *	statistics. Drivers must do one of the following:
1124  *	1. Define @ndo_get_stats64 to fill in a zero-initialised
1125  *	   rtnl_link_stats64 structure passed by the caller.
1126  *	2. Define @ndo_get_stats to update a net_device_stats structure
1127  *	   (which should normally be dev->stats) and return a pointer to
1128  *	   it. The structure may be changed asynchronously only if each
1129  *	   field is written atomically.
1130  *	3. Update dev->stats asynchronously and atomically, and define
1131  *	   neither operation.
1132  *
1133  * bool (*ndo_has_offload_stats)(const struct net_device *dev, int attr_id)
1134  *	Return true if this device supports offload stats of this attr_id.
1135  *
1136  * int (*ndo_get_offload_stats)(int attr_id, const struct net_device *dev,
1137  *	void *attr_data)
1138  *	Get statistics for offload operations by attr_id. Write it into the
1139  *	attr_data pointer.
1140  *
1141  * int (*ndo_vlan_rx_add_vid)(struct net_device *dev, __be16 proto, u16 vid);
1142  *	If device supports VLAN filtering this function is called when a
1143  *	VLAN id is registered.
1144  *
1145  * int (*ndo_vlan_rx_kill_vid)(struct net_device *dev, __be16 proto, u16 vid);
1146  *	If device supports VLAN filtering this function is called when a
1147  *	VLAN id is unregistered.
1148  *
1149  * void (*ndo_poll_controller)(struct net_device *dev);
1150  *
1151  *	SR-IOV management functions.
1152  * int (*ndo_set_vf_mac)(struct net_device *dev, int vf, u8* mac);
1153  * int (*ndo_set_vf_vlan)(struct net_device *dev, int vf, u16 vlan,
1154  *			  u8 qos, __be16 proto);
1155  * int (*ndo_set_vf_rate)(struct net_device *dev, int vf, int min_tx_rate,
1156  *			  int max_tx_rate);
1157  * int (*ndo_set_vf_spoofchk)(struct net_device *dev, int vf, bool setting);
1158  * int (*ndo_set_vf_trust)(struct net_device *dev, int vf, bool setting);
1159  * int (*ndo_get_vf_config)(struct net_device *dev,
1160  *			    int vf, struct ifla_vf_info *ivf);
1161  * int (*ndo_set_vf_link_state)(struct net_device *dev, int vf, int link_state);
1162  * int (*ndo_set_vf_port)(struct net_device *dev, int vf,
1163  *			  struct nlattr *port[]);
1164  *
1165  *      Enable or disable the VF ability to query its RSS Redirection Table and
1166  *      Hash Key. This is needed since on some devices VF share this information
1167  *      with PF and querying it may introduce a theoretical security risk.
1168  * int (*ndo_set_vf_rss_query_en)(struct net_device *dev, int vf, bool setting);
1169  * int (*ndo_get_vf_port)(struct net_device *dev, int vf, struct sk_buff *skb);
1170  * int (*ndo_setup_tc)(struct net_device *dev, enum tc_setup_type type,
1171  *		       void *type_data);
1172  *	Called to setup any 'tc' scheduler, classifier or action on @dev.
1173  *	This is always called from the stack with the rtnl lock held and netif
1174  *	tx queues stopped. This allows the netdevice to perform queue
1175  *	management safely.
1176  *
1177  *	Fiber Channel over Ethernet (FCoE) offload functions.
1178  * int (*ndo_fcoe_enable)(struct net_device *dev);
1179  *	Called when the FCoE protocol stack wants to start using LLD for FCoE
1180  *	so the underlying device can perform whatever needed configuration or
1181  *	initialization to support acceleration of FCoE traffic.
1182  *
1183  * int (*ndo_fcoe_disable)(struct net_device *dev);
1184  *	Called when the FCoE protocol stack wants to stop using LLD for FCoE
1185  *	so the underlying device can perform whatever needed clean-ups to
1186  *	stop supporting acceleration of FCoE traffic.
1187  *
1188  * int (*ndo_fcoe_ddp_setup)(struct net_device *dev, u16 xid,
1189  *			     struct scatterlist *sgl, unsigned int sgc);
1190  *	Called when the FCoE Initiator wants to initialize an I/O that
1191  *	is a possible candidate for Direct Data Placement (DDP). The LLD can
1192  *	perform necessary setup and returns 1 to indicate the device is set up
1193  *	successfully to perform DDP on this I/O, otherwise this returns 0.
1194  *
1195  * int (*ndo_fcoe_ddp_done)(struct net_device *dev,  u16 xid);
1196  *	Called when the FCoE Initiator/Target is done with the DDPed I/O as
1197  *	indicated by the FC exchange id 'xid', so the underlying device can
1198  *	clean up and reuse resources for later DDP requests.
1199  *
1200  * int (*ndo_fcoe_ddp_target)(struct net_device *dev, u16 xid,
1201  *			      struct scatterlist *sgl, unsigned int sgc);
1202  *	Called when the FCoE Target wants to initialize an I/O that
1203  *	is a possible candidate for Direct Data Placement (DDP). The LLD can
1204  *	perform necessary setup and returns 1 to indicate the device is set up
1205  *	successfully to perform DDP on this I/O, otherwise this returns 0.
1206  *
1207  * int (*ndo_fcoe_get_hbainfo)(struct net_device *dev,
1208  *			       struct netdev_fcoe_hbainfo *hbainfo);
1209  *	Called when the FCoE Protocol stack wants information on the underlying
1210  *	device. This information is utilized by the FCoE protocol stack to
1211  *	register attributes with Fiber Channel management service as per the
1212  *	FC-GS Fabric Device Management Information(FDMI) specification.
1213  *
1214  * int (*ndo_fcoe_get_wwn)(struct net_device *dev, u64 *wwn, int type);
1215  *	Called when the underlying device wants to override default World Wide
1216  *	Name (WWN) generation mechanism in FCoE protocol stack to pass its own
1217  *	World Wide Port Name (WWPN) or World Wide Node Name (WWNN) to the FCoE
1218  *	protocol stack to use.
1219  *
1220  *	RFS acceleration.
1221  * int (*ndo_rx_flow_steer)(struct net_device *dev, const struct sk_buff *skb,
1222  *			    u16 rxq_index, u32 flow_id);
1223  *	Set hardware filter for RFS.  rxq_index is the target queue index;
1224  *	flow_id is a flow ID to be passed to rps_may_expire_flow() later.
1225  *	Return the filter ID on success, or a negative error code.
1226  *
1227  *	Slave management functions (for bridge, bonding, etc).
1228  * int (*ndo_add_slave)(struct net_device *dev, struct net_device *slave_dev);
1229  *	Called to make another netdev an underling.
1230  *
1231  * int (*ndo_del_slave)(struct net_device *dev, struct net_device *slave_dev);
1232  *	Called to release previously enslaved netdev.
1233  *
1234  * struct net_device *(*ndo_get_xmit_slave)(struct net_device *dev,
1235  *					    struct sk_buff *skb,
1236  *					    bool all_slaves);
1237  *	Get the xmit slave of master device. If all_slaves is true, function
1238  *	assume all the slaves can transmit.
1239  *
1240  *      Feature/offload setting functions.
1241  * netdev_features_t (*ndo_fix_features)(struct net_device *dev,
1242  *		netdev_features_t features);
1243  *	Adjusts the requested feature flags according to device-specific
1244  *	constraints, and returns the resulting flags. Must not modify
1245  *	the device state.
1246  *
1247  * int (*ndo_set_features)(struct net_device *dev, netdev_features_t features);
1248  *	Called to update device configuration to new features. Passed
1249  *	feature set might be less than what was returned by ndo_fix_features()).
1250  *	Must return >0 or -errno if it changed dev->features itself.
1251  *
1252  * int (*ndo_fdb_add)(struct ndmsg *ndm, struct nlattr *tb[],
1253  *		      struct net_device *dev,
1254  *		      const unsigned char *addr, u16 vid, u16 flags,
1255  *		      bool *notified, struct netlink_ext_ack *extack);
1256  *	Adds an FDB entry to dev for addr.
1257  *	Callee shall set *notified to true if it sent any appropriate
1258  *	notification(s). Otherwise core will send a generic one.
1259  * int (*ndo_fdb_del)(struct ndmsg *ndm, struct nlattr *tb[],
1260  *		      struct net_device *dev,
1261  *		      const unsigned char *addr, u16 vid
1262  *		      bool *notified, struct netlink_ext_ack *extack);
1263  *	Deletes the FDB entry from dev corresponding to addr.
1264  *	Callee shall set *notified to true if it sent any appropriate
1265  *	notification(s). Otherwise core will send a generic one.
1266  * int (*ndo_fdb_del_bulk)(struct nlmsghdr *nlh, struct net_device *dev,
1267  *			   struct netlink_ext_ack *extack);
1268  * int (*ndo_fdb_dump)(struct sk_buff *skb, struct netlink_callback *cb,
1269  *		       struct net_device *dev, struct net_device *filter_dev,
1270  *		       int *idx)
1271  *	Used to add FDB entries to dump requests. Implementers should add
1272  *	entries to skb and update idx with the number of entries.
1273  *
1274  * int (*ndo_mdb_add)(struct net_device *dev, struct nlattr *tb[],
1275  *		      u16 nlmsg_flags, struct netlink_ext_ack *extack);
1276  *	Adds an MDB entry to dev.
1277  * int (*ndo_mdb_del)(struct net_device *dev, struct nlattr *tb[],
1278  *		      struct netlink_ext_ack *extack);
1279  *	Deletes the MDB entry from dev.
1280  * int (*ndo_mdb_del_bulk)(struct net_device *dev, struct nlattr *tb[],
1281  *			   struct netlink_ext_ack *extack);
1282  *	Bulk deletes MDB entries from dev.
1283  * int (*ndo_mdb_dump)(struct net_device *dev, struct sk_buff *skb,
1284  *		       struct netlink_callback *cb);
1285  *	Dumps MDB entries from dev. The first argument (marker) in the netlink
1286  *	callback is used by core rtnetlink code.
1287  *
1288  * int (*ndo_bridge_setlink)(struct net_device *dev, struct nlmsghdr *nlh,
1289  *			     u16 flags, struct netlink_ext_ack *extack)
1290  * int (*ndo_bridge_getlink)(struct sk_buff *skb, u32 pid, u32 seq,
1291  *			     struct net_device *dev, u32 filter_mask,
1292  *			     int nlflags)
1293  * int (*ndo_bridge_dellink)(struct net_device *dev, struct nlmsghdr *nlh,
1294  *			     u16 flags);
1295  *
1296  * int (*ndo_change_carrier)(struct net_device *dev, bool new_carrier);
1297  *	Called to change device carrier. Soft-devices (like dummy, team, etc)
1298  *	which do not represent real hardware may define this to allow their
1299  *	userspace components to manage their virtual carrier state. Devices
1300  *	that determine carrier state from physical hardware properties (eg
1301  *	network cables) or protocol-dependent mechanisms (eg
1302  *	USB_CDC_NOTIFY_NETWORK_CONNECTION) should NOT implement this function.
1303  *
1304  * int (*ndo_get_phys_port_id)(struct net_device *dev,
1305  *			       struct netdev_phys_item_id *ppid);
1306  *	Called to get ID of physical port of this device. If driver does
1307  *	not implement this, it is assumed that the hw is not able to have
1308  *	multiple net devices on single physical port.
1309  *
1310  * int (*ndo_get_port_parent_id)(struct net_device *dev,
1311  *				 struct netdev_phys_item_id *ppid)
1312  *	Called to get the parent ID of the physical port of this device.
1313  *
1314  * void* (*ndo_dfwd_add_station)(struct net_device *pdev,
1315  *				 struct net_device *dev)
1316  *	Called by upper layer devices to accelerate switching or other
1317  *	station functionality into hardware. 'pdev is the lowerdev
1318  *	to use for the offload and 'dev' is the net device that will
1319  *	back the offload. Returns a pointer to the private structure
1320  *	the upper layer will maintain.
1321  * void (*ndo_dfwd_del_station)(struct net_device *pdev, void *priv)
1322  *	Called by upper layer device to delete the station created
1323  *	by 'ndo_dfwd_add_station'. 'pdev' is the net device backing
1324  *	the station and priv is the structure returned by the add
1325  *	operation.
1326  * int (*ndo_set_tx_maxrate)(struct net_device *dev,
1327  *			     int queue_index, u32 maxrate);
1328  *	Called when a user wants to set a max-rate limitation of specific
1329  *	TX queue.
1330  * int (*ndo_get_iflink)(const struct net_device *dev);
1331  *	Called to get the iflink value of this device.
1332  * int (*ndo_fill_metadata_dst)(struct net_device *dev, struct sk_buff *skb);
1333  *	This function is used to get egress tunnel information for given skb.
1334  *	This is useful for retrieving outer tunnel header parameters while
1335  *	sampling packet.
1336  * void (*ndo_set_rx_headroom)(struct net_device *dev, int needed_headroom);
1337  *	This function is used to specify the headroom that the skb must
1338  *	consider when allocation skb during packet reception. Setting
1339  *	appropriate rx headroom value allows avoiding skb head copy on
1340  *	forward. Setting a negative value resets the rx headroom to the
1341  *	default value.
1342  * int (*ndo_bpf)(struct net_device *dev, struct netdev_bpf *bpf);
1343  *	This function is used to set or query state related to XDP on the
1344  *	netdevice and manage BPF offload. See definition of
1345  *	enum bpf_netdev_command for details.
1346  * int (*ndo_xdp_xmit)(struct net_device *dev, int n, struct xdp_frame **xdp,
1347  *			u32 flags);
1348  *	This function is used to submit @n XDP packets for transmit on a
1349  *	netdevice. Returns number of frames successfully transmitted, frames
1350  *	that got dropped are freed/returned via xdp_return_frame().
1351  *	Returns negative number, means general error invoking ndo, meaning
1352  *	no frames were xmit'ed and core-caller will free all frames.
1353  * struct net_device *(*ndo_xdp_get_xmit_slave)(struct net_device *dev,
1354  *					        struct xdp_buff *xdp);
1355  *      Get the xmit slave of master device based on the xdp_buff.
1356  * int (*ndo_xsk_wakeup)(struct net_device *dev, u32 queue_id, u32 flags);
1357  *      This function is used to wake up the softirq, ksoftirqd or kthread
1358  *	responsible for sending and/or receiving packets on a specific
1359  *	queue id bound to an AF_XDP socket. The flags field specifies if
1360  *	only RX, only Tx, or both should be woken up using the flags
1361  *	XDP_WAKEUP_RX and XDP_WAKEUP_TX.
1362  * int (*ndo_tunnel_ctl)(struct net_device *dev, struct ip_tunnel_parm_kern *p,
1363  *			 int cmd);
1364  *	Add, change, delete or get information on an IPv4 tunnel.
1365  * struct net_device *(*ndo_get_peer_dev)(struct net_device *dev);
1366  *	If a device is paired with a peer device, return the peer instance.
1367  *	The caller must be under RCU read context.
1368  * int (*ndo_fill_forward_path)(struct net_device_path_ctx *ctx, struct net_device_path *path);
1369  *     Get the forwarding path to reach the real device from the HW destination address
1370  * ktime_t (*ndo_get_tstamp)(struct net_device *dev,
1371  *			     const struct skb_shared_hwtstamps *hwtstamps,
1372  *			     bool cycles);
1373  *	Get hardware timestamp based on normal/adjustable time or free running
1374  *	cycle counter. This function is required if physical clock supports a
1375  *	free running cycle counter.
1376  *
1377  * int (*ndo_hwtstamp_get)(struct net_device *dev,
1378  *			   struct kernel_hwtstamp_config *kernel_config);
1379  *	Get the currently configured hardware timestamping parameters for the
1380  *	NIC device.
1381  *
1382  * int (*ndo_hwtstamp_set)(struct net_device *dev,
1383  *			   struct kernel_hwtstamp_config *kernel_config,
1384  *			   struct netlink_ext_ack *extack);
1385  *	Change the hardware timestamping parameters for NIC device.
1386  */
1387 struct net_device_ops {
1388 	int			(*ndo_init)(struct net_device *dev);
1389 	void			(*ndo_uninit)(struct net_device *dev);
1390 	int			(*ndo_open)(struct net_device *dev);
1391 	int			(*ndo_stop)(struct net_device *dev);
1392 	netdev_tx_t		(*ndo_start_xmit)(struct sk_buff *skb,
1393 						  struct net_device *dev);
1394 	netdev_features_t	(*ndo_features_check)(struct sk_buff *skb,
1395 						      struct net_device *dev,
1396 						      netdev_features_t features);
1397 	u16			(*ndo_select_queue)(struct net_device *dev,
1398 						    struct sk_buff *skb,
1399 						    struct net_device *sb_dev);
1400 	void			(*ndo_change_rx_flags)(struct net_device *dev,
1401 						       int flags);
1402 	void			(*ndo_set_rx_mode)(struct net_device *dev);
1403 	int			(*ndo_set_mac_address)(struct net_device *dev,
1404 						       void *addr);
1405 	int			(*ndo_validate_addr)(struct net_device *dev);
1406 	int			(*ndo_do_ioctl)(struct net_device *dev,
1407 					        struct ifreq *ifr, int cmd);
1408 	int			(*ndo_eth_ioctl)(struct net_device *dev,
1409 						 struct ifreq *ifr, int cmd);
1410 	int			(*ndo_siocbond)(struct net_device *dev,
1411 						struct ifreq *ifr, int cmd);
1412 	int			(*ndo_siocwandev)(struct net_device *dev,
1413 						  struct if_settings *ifs);
1414 	int			(*ndo_siocdevprivate)(struct net_device *dev,
1415 						      struct ifreq *ifr,
1416 						      void __user *data, int cmd);
1417 	int			(*ndo_set_config)(struct net_device *dev,
1418 					          struct ifmap *map);
1419 	int			(*ndo_change_mtu)(struct net_device *dev,
1420 						  int new_mtu);
1421 	int			(*ndo_neigh_setup)(struct net_device *dev,
1422 						   struct neigh_parms *);
1423 	void			(*ndo_tx_timeout) (struct net_device *dev,
1424 						   unsigned int txqueue);
1425 
1426 	void			(*ndo_get_stats64)(struct net_device *dev,
1427 						   struct rtnl_link_stats64 *storage);
1428 	bool			(*ndo_has_offload_stats)(const struct net_device *dev, int attr_id);
1429 	int			(*ndo_get_offload_stats)(int attr_id,
1430 							 const struct net_device *dev,
1431 							 void *attr_data);
1432 	struct net_device_stats* (*ndo_get_stats)(struct net_device *dev);
1433 
1434 	int			(*ndo_vlan_rx_add_vid)(struct net_device *dev,
1435 						       __be16 proto, u16 vid);
1436 	int			(*ndo_vlan_rx_kill_vid)(struct net_device *dev,
1437 						        __be16 proto, u16 vid);
1438 #ifdef CONFIG_NET_POLL_CONTROLLER
1439 	void                    (*ndo_poll_controller)(struct net_device *dev);
1440 	int			(*ndo_netpoll_setup)(struct net_device *dev);
1441 	void			(*ndo_netpoll_cleanup)(struct net_device *dev);
1442 #endif
1443 	int			(*ndo_set_vf_mac)(struct net_device *dev,
1444 						  int queue, u8 *mac);
1445 	int			(*ndo_set_vf_vlan)(struct net_device *dev,
1446 						   int queue, u16 vlan,
1447 						   u8 qos, __be16 proto);
1448 	int			(*ndo_set_vf_rate)(struct net_device *dev,
1449 						   int vf, int min_tx_rate,
1450 						   int max_tx_rate);
1451 	int			(*ndo_set_vf_spoofchk)(struct net_device *dev,
1452 						       int vf, bool setting);
1453 	int			(*ndo_set_vf_trust)(struct net_device *dev,
1454 						    int vf, bool setting);
1455 	int			(*ndo_get_vf_config)(struct net_device *dev,
1456 						     int vf,
1457 						     struct ifla_vf_info *ivf);
1458 	int			(*ndo_set_vf_link_state)(struct net_device *dev,
1459 							 int vf, int link_state);
1460 	int			(*ndo_get_vf_stats)(struct net_device *dev,
1461 						    int vf,
1462 						    struct ifla_vf_stats
1463 						    *vf_stats);
1464 	int			(*ndo_set_vf_port)(struct net_device *dev,
1465 						   int vf,
1466 						   struct nlattr *port[]);
1467 	int			(*ndo_get_vf_port)(struct net_device *dev,
1468 						   int vf, struct sk_buff *skb);
1469 	int			(*ndo_get_vf_guid)(struct net_device *dev,
1470 						   int vf,
1471 						   struct ifla_vf_guid *node_guid,
1472 						   struct ifla_vf_guid *port_guid);
1473 	int			(*ndo_set_vf_guid)(struct net_device *dev,
1474 						   int vf, u64 guid,
1475 						   int guid_type);
1476 	int			(*ndo_set_vf_rss_query_en)(
1477 						   struct net_device *dev,
1478 						   int vf, bool setting);
1479 	int			(*ndo_setup_tc)(struct net_device *dev,
1480 						enum tc_setup_type type,
1481 						void *type_data);
1482 #if IS_ENABLED(CONFIG_FCOE)
1483 	int			(*ndo_fcoe_enable)(struct net_device *dev);
1484 	int			(*ndo_fcoe_disable)(struct net_device *dev);
1485 	int			(*ndo_fcoe_ddp_setup)(struct net_device *dev,
1486 						      u16 xid,
1487 						      struct scatterlist *sgl,
1488 						      unsigned int sgc);
1489 	int			(*ndo_fcoe_ddp_done)(struct net_device *dev,
1490 						     u16 xid);
1491 	int			(*ndo_fcoe_ddp_target)(struct net_device *dev,
1492 						       u16 xid,
1493 						       struct scatterlist *sgl,
1494 						       unsigned int sgc);
1495 	int			(*ndo_fcoe_get_hbainfo)(struct net_device *dev,
1496 							struct netdev_fcoe_hbainfo *hbainfo);
1497 #endif
1498 
1499 #if IS_ENABLED(CONFIG_LIBFCOE)
1500 #define NETDEV_FCOE_WWNN 0
1501 #define NETDEV_FCOE_WWPN 1
1502 	int			(*ndo_fcoe_get_wwn)(struct net_device *dev,
1503 						    u64 *wwn, int type);
1504 #endif
1505 
1506 #ifdef CONFIG_RFS_ACCEL
1507 	int			(*ndo_rx_flow_steer)(struct net_device *dev,
1508 						     const struct sk_buff *skb,
1509 						     u16 rxq_index,
1510 						     u32 flow_id);
1511 #endif
1512 	int			(*ndo_add_slave)(struct net_device *dev,
1513 						 struct net_device *slave_dev,
1514 						 struct netlink_ext_ack *extack);
1515 	int			(*ndo_del_slave)(struct net_device *dev,
1516 						 struct net_device *slave_dev);
1517 	struct net_device*	(*ndo_get_xmit_slave)(struct net_device *dev,
1518 						      struct sk_buff *skb,
1519 						      bool all_slaves);
1520 	struct net_device*	(*ndo_sk_get_lower_dev)(struct net_device *dev,
1521 							struct sock *sk);
1522 	netdev_features_t	(*ndo_fix_features)(struct net_device *dev,
1523 						    netdev_features_t features);
1524 	int			(*ndo_set_features)(struct net_device *dev,
1525 						    netdev_features_t features);
1526 	int			(*ndo_neigh_construct)(struct net_device *dev,
1527 						       struct neighbour *n);
1528 	void			(*ndo_neigh_destroy)(struct net_device *dev,
1529 						     struct neighbour *n);
1530 
1531 	int			(*ndo_fdb_add)(struct ndmsg *ndm,
1532 					       struct nlattr *tb[],
1533 					       struct net_device *dev,
1534 					       const unsigned char *addr,
1535 					       u16 vid,
1536 					       u16 flags,
1537 					       bool *notified,
1538 					       struct netlink_ext_ack *extack);
1539 	int			(*ndo_fdb_del)(struct ndmsg *ndm,
1540 					       struct nlattr *tb[],
1541 					       struct net_device *dev,
1542 					       const unsigned char *addr,
1543 					       u16 vid,
1544 					       bool *notified,
1545 					       struct netlink_ext_ack *extack);
1546 	int			(*ndo_fdb_del_bulk)(struct nlmsghdr *nlh,
1547 						    struct net_device *dev,
1548 						    struct netlink_ext_ack *extack);
1549 	int			(*ndo_fdb_dump)(struct sk_buff *skb,
1550 						struct netlink_callback *cb,
1551 						struct net_device *dev,
1552 						struct net_device *filter_dev,
1553 						int *idx);
1554 	int			(*ndo_fdb_get)(struct sk_buff *skb,
1555 					       struct nlattr *tb[],
1556 					       struct net_device *dev,
1557 					       const unsigned char *addr,
1558 					       u16 vid, u32 portid, u32 seq,
1559 					       struct netlink_ext_ack *extack);
1560 	int			(*ndo_mdb_add)(struct net_device *dev,
1561 					       struct nlattr *tb[],
1562 					       u16 nlmsg_flags,
1563 					       struct netlink_ext_ack *extack);
1564 	int			(*ndo_mdb_del)(struct net_device *dev,
1565 					       struct nlattr *tb[],
1566 					       struct netlink_ext_ack *extack);
1567 	int			(*ndo_mdb_del_bulk)(struct net_device *dev,
1568 						    struct nlattr *tb[],
1569 						    struct netlink_ext_ack *extack);
1570 	int			(*ndo_mdb_dump)(struct net_device *dev,
1571 						struct sk_buff *skb,
1572 						struct netlink_callback *cb);
1573 	int			(*ndo_mdb_get)(struct net_device *dev,
1574 					       struct nlattr *tb[], u32 portid,
1575 					       u32 seq,
1576 					       struct netlink_ext_ack *extack);
1577 	int			(*ndo_bridge_setlink)(struct net_device *dev,
1578 						      struct nlmsghdr *nlh,
1579 						      u16 flags,
1580 						      struct netlink_ext_ack *extack);
1581 	int			(*ndo_bridge_getlink)(struct sk_buff *skb,
1582 						      u32 pid, u32 seq,
1583 						      struct net_device *dev,
1584 						      u32 filter_mask,
1585 						      int nlflags);
1586 	int			(*ndo_bridge_dellink)(struct net_device *dev,
1587 						      struct nlmsghdr *nlh,
1588 						      u16 flags);
1589 	int			(*ndo_change_carrier)(struct net_device *dev,
1590 						      bool new_carrier);
1591 	int			(*ndo_get_phys_port_id)(struct net_device *dev,
1592 							struct netdev_phys_item_id *ppid);
1593 	int			(*ndo_get_port_parent_id)(struct net_device *dev,
1594 							  struct netdev_phys_item_id *ppid);
1595 	int			(*ndo_get_phys_port_name)(struct net_device *dev,
1596 							  char *name, size_t len);
1597 	void*			(*ndo_dfwd_add_station)(struct net_device *pdev,
1598 							struct net_device *dev);
1599 	void			(*ndo_dfwd_del_station)(struct net_device *pdev,
1600 							void *priv);
1601 
1602 	int			(*ndo_set_tx_maxrate)(struct net_device *dev,
1603 						      int queue_index,
1604 						      u32 maxrate);
1605 	int			(*ndo_get_iflink)(const struct net_device *dev);
1606 	int			(*ndo_fill_metadata_dst)(struct net_device *dev,
1607 						       struct sk_buff *skb);
1608 	void			(*ndo_set_rx_headroom)(struct net_device *dev,
1609 						       int needed_headroom);
1610 	int			(*ndo_bpf)(struct net_device *dev,
1611 					   struct netdev_bpf *bpf);
1612 	int			(*ndo_xdp_xmit)(struct net_device *dev, int n,
1613 						struct xdp_frame **xdp,
1614 						u32 flags);
1615 	struct net_device *	(*ndo_xdp_get_xmit_slave)(struct net_device *dev,
1616 							  struct xdp_buff *xdp);
1617 	int			(*ndo_xsk_wakeup)(struct net_device *dev,
1618 						  u32 queue_id, u32 flags);
1619 	int			(*ndo_tunnel_ctl)(struct net_device *dev,
1620 						  struct ip_tunnel_parm_kern *p,
1621 						  int cmd);
1622 	struct net_device *	(*ndo_get_peer_dev)(struct net_device *dev);
1623 	int                     (*ndo_fill_forward_path)(struct net_device_path_ctx *ctx,
1624                                                          struct net_device_path *path);
1625 	ktime_t			(*ndo_get_tstamp)(struct net_device *dev,
1626 						  const struct skb_shared_hwtstamps *hwtstamps,
1627 						  bool cycles);
1628 	int			(*ndo_hwtstamp_get)(struct net_device *dev,
1629 						    struct kernel_hwtstamp_config *kernel_config);
1630 	int			(*ndo_hwtstamp_set)(struct net_device *dev,
1631 						    struct kernel_hwtstamp_config *kernel_config,
1632 						    struct netlink_ext_ack *extack);
1633 
1634 #if IS_ENABLED(CONFIG_NET_SHAPER)
1635 	/**
1636 	 * @net_shaper_ops: Device shaping offload operations
1637 	 * see include/net/net_shapers.h
1638 	 */
1639 	const struct net_shaper_ops *net_shaper_ops;
1640 #endif
1641 };
1642 
1643 /**
1644  * enum netdev_priv_flags - &struct net_device priv_flags
1645  *
1646  * These are the &struct net_device, they are only set internally
1647  * by drivers and used in the kernel. These flags are invisible to
1648  * userspace; this means that the order of these flags can change
1649  * during any kernel release.
1650  *
1651  * You should add bitfield booleans after either net_device::priv_flags
1652  * (hotpath) or ::threaded (slowpath) instead of extending these flags.
1653  *
1654  * @IFF_802_1Q_VLAN: 802.1Q VLAN device
1655  * @IFF_EBRIDGE: Ethernet bridging device
1656  * @IFF_BONDING: bonding master or slave
1657  * @IFF_ISATAP: ISATAP interface (RFC4214)
1658  * @IFF_WAN_HDLC: WAN HDLC device
1659  * @IFF_XMIT_DST_RELEASE: dev_hard_start_xmit() is allowed to
1660  *	release skb->dst
1661  * @IFF_DONT_BRIDGE: disallow bridging this ether dev
1662  * @IFF_DISABLE_NETPOLL: disable netpoll at run-time
1663  * @IFF_MACVLAN_PORT: device used as macvlan port
1664  * @IFF_BRIDGE_PORT: device used as bridge port
1665  * @IFF_OVS_DATAPATH: device used as Open vSwitch datapath port
1666  * @IFF_TX_SKB_SHARING: The interface supports sharing skbs on transmit
1667  * @IFF_UNICAST_FLT: Supports unicast filtering
1668  * @IFF_TEAM_PORT: device used as team port
1669  * @IFF_SUPP_NOFCS: device supports sending custom FCS
1670  * @IFF_LIVE_ADDR_CHANGE: device supports hardware address
1671  *	change when it's running
1672  * @IFF_MACVLAN: Macvlan device
1673  * @IFF_XMIT_DST_RELEASE_PERM: IFF_XMIT_DST_RELEASE not taking into account
1674  *	underlying stacked devices
1675  * @IFF_L3MDEV_MASTER: device is an L3 master device
1676  * @IFF_NO_QUEUE: device can run without qdisc attached
1677  * @IFF_OPENVSWITCH: device is a Open vSwitch master
1678  * @IFF_L3MDEV_SLAVE: device is enslaved to an L3 master device
1679  * @IFF_TEAM: device is a team device
1680  * @IFF_RXFH_CONFIGURED: device has had Rx Flow indirection table configured
1681  * @IFF_PHONY_HEADROOM: the headroom value is controlled by an external
1682  *	entity (i.e. the master device for bridged veth)
1683  * @IFF_MACSEC: device is a MACsec device
1684  * @IFF_NO_RX_HANDLER: device doesn't support the rx_handler hook
1685  * @IFF_FAILOVER: device is a failover master device
1686  * @IFF_FAILOVER_SLAVE: device is lower dev of a failover master device
1687  * @IFF_L3MDEV_RX_HANDLER: only invoke the rx handler of L3 master device
1688  * @IFF_NO_ADDRCONF: prevent ipv6 addrconf
1689  * @IFF_TX_SKB_NO_LINEAR: device/driver is capable of xmitting frames with
1690  *	skb_headlen(skb) == 0 (data starts from frag0)
1691  */
1692 enum netdev_priv_flags {
1693 	IFF_802_1Q_VLAN			= 1<<0,
1694 	IFF_EBRIDGE			= 1<<1,
1695 	IFF_BONDING			= 1<<2,
1696 	IFF_ISATAP			= 1<<3,
1697 	IFF_WAN_HDLC			= 1<<4,
1698 	IFF_XMIT_DST_RELEASE		= 1<<5,
1699 	IFF_DONT_BRIDGE			= 1<<6,
1700 	IFF_DISABLE_NETPOLL		= 1<<7,
1701 	IFF_MACVLAN_PORT		= 1<<8,
1702 	IFF_BRIDGE_PORT			= 1<<9,
1703 	IFF_OVS_DATAPATH		= 1<<10,
1704 	IFF_TX_SKB_SHARING		= 1<<11,
1705 	IFF_UNICAST_FLT			= 1<<12,
1706 	IFF_TEAM_PORT			= 1<<13,
1707 	IFF_SUPP_NOFCS			= 1<<14,
1708 	IFF_LIVE_ADDR_CHANGE		= 1<<15,
1709 	IFF_MACVLAN			= 1<<16,
1710 	IFF_XMIT_DST_RELEASE_PERM	= 1<<17,
1711 	IFF_L3MDEV_MASTER		= 1<<18,
1712 	IFF_NO_QUEUE			= 1<<19,
1713 	IFF_OPENVSWITCH			= 1<<20,
1714 	IFF_L3MDEV_SLAVE		= 1<<21,
1715 	IFF_TEAM			= 1<<22,
1716 	IFF_RXFH_CONFIGURED		= 1<<23,
1717 	IFF_PHONY_HEADROOM		= 1<<24,
1718 	IFF_MACSEC			= 1<<25,
1719 	IFF_NO_RX_HANDLER		= 1<<26,
1720 	IFF_FAILOVER			= 1<<27,
1721 	IFF_FAILOVER_SLAVE		= 1<<28,
1722 	IFF_L3MDEV_RX_HANDLER		= 1<<29,
1723 	IFF_NO_ADDRCONF			= BIT_ULL(30),
1724 	IFF_TX_SKB_NO_LINEAR		= BIT_ULL(31),
1725 };
1726 
1727 /* Specifies the type of the struct net_device::ml_priv pointer */
1728 enum netdev_ml_priv_type {
1729 	ML_PRIV_NONE,
1730 	ML_PRIV_CAN,
1731 };
1732 
1733 enum netdev_stat_type {
1734 	NETDEV_PCPU_STAT_NONE,
1735 	NETDEV_PCPU_STAT_LSTATS, /* struct pcpu_lstats */
1736 	NETDEV_PCPU_STAT_TSTATS, /* struct pcpu_sw_netstats */
1737 	NETDEV_PCPU_STAT_DSTATS, /* struct pcpu_dstats */
1738 };
1739 
1740 enum netdev_reg_state {
1741 	NETREG_UNINITIALIZED = 0,
1742 	NETREG_REGISTERED,	/* completed register_netdevice */
1743 	NETREG_UNREGISTERING,	/* called unregister_netdevice */
1744 	NETREG_UNREGISTERED,	/* completed unregister todo */
1745 	NETREG_RELEASED,	/* called free_netdev */
1746 	NETREG_DUMMY,		/* dummy device for NAPI poll */
1747 };
1748 
1749 /**
1750  *	struct net_device - The DEVICE structure.
1751  *
1752  *	Actually, this whole structure is a big mistake.  It mixes I/O
1753  *	data with strictly "high-level" data, and it has to know about
1754  *	almost every data structure used in the INET module.
1755  *
1756  *	@priv_flags:	flags invisible to userspace defined as bits, see
1757  *			enum netdev_priv_flags for the definitions
1758  *	@lltx:		device supports lockless Tx. Deprecated for real HW
1759  *			drivers. Mainly used by logical interfaces, such as
1760  *			bonding and tunnels
1761  *
1762  *	@name:	This is the first field of the "visible" part of this structure
1763  *		(i.e. as seen by users in the "Space.c" file).  It is the name
1764  *		of the interface.
1765  *
1766  *	@name_node:	Name hashlist node
1767  *	@ifalias:	SNMP alias
1768  *	@mem_end:	Shared memory end
1769  *	@mem_start:	Shared memory start
1770  *	@base_addr:	Device I/O address
1771  *	@irq:		Device IRQ number
1772  *
1773  *	@state:		Generic network queuing layer state, see netdev_state_t
1774  *	@dev_list:	The global list of network devices
1775  *	@napi_list:	List entry used for polling NAPI devices
1776  *	@unreg_list:	List entry  when we are unregistering the
1777  *			device; see the function unregister_netdev
1778  *	@close_list:	List entry used when we are closing the device
1779  *	@ptype_all:     Device-specific packet handlers for all protocols
1780  *	@ptype_specific: Device-specific, protocol-specific packet handlers
1781  *
1782  *	@adj_list:	Directly linked devices, like slaves for bonding
1783  *	@features:	Currently active device features
1784  *	@hw_features:	User-changeable features
1785  *
1786  *	@wanted_features:	User-requested features
1787  *	@vlan_features:		Mask of features inheritable by VLAN devices
1788  *
1789  *	@hw_enc_features:	Mask of features inherited by encapsulating devices
1790  *				This field indicates what encapsulation
1791  *				offloads the hardware is capable of doing,
1792  *				and drivers will need to set them appropriately.
1793  *
1794  *	@mpls_features:	Mask of features inheritable by MPLS
1795  *	@gso_partial_features: value(s) from NETIF_F_GSO\*
1796  *
1797  *	@ifindex:	interface index
1798  *	@group:		The group the device belongs to
1799  *
1800  *	@stats:		Statistics struct, which was left as a legacy, use
1801  *			rtnl_link_stats64 instead
1802  *
1803  *	@core_stats:	core networking counters,
1804  *			do not use this in drivers
1805  *	@carrier_up_count:	Number of times the carrier has been up
1806  *	@carrier_down_count:	Number of times the carrier has been down
1807  *
1808  *	@wireless_handlers:	List of functions to handle Wireless Extensions,
1809  *				instead of ioctl,
1810  *				see <net/iw_handler.h> for details.
1811  *
1812  *	@netdev_ops:	Includes several pointers to callbacks,
1813  *			if one wants to override the ndo_*() functions
1814  *	@xdp_metadata_ops:	Includes pointers to XDP metadata callbacks.
1815  *	@xsk_tx_metadata_ops:	Includes pointers to AF_XDP TX metadata callbacks.
1816  *	@ethtool_ops:	Management operations
1817  *	@l3mdev_ops:	Layer 3 master device operations
1818  *	@ndisc_ops:	Includes callbacks for different IPv6 neighbour
1819  *			discovery handling. Necessary for e.g. 6LoWPAN.
1820  *	@xfrmdev_ops:	Transformation offload operations
1821  *	@tlsdev_ops:	Transport Layer Security offload operations
1822  *	@header_ops:	Includes callbacks for creating,parsing,caching,etc
1823  *			of Layer 2 headers.
1824  *
1825  *	@flags:		Interface flags (a la BSD)
1826  *	@xdp_features:	XDP capability supported by the device
1827  *	@gflags:	Global flags ( kept as legacy )
1828  *	@priv_len:	Size of the ->priv flexible array
1829  *	@priv:		Flexible array containing private data
1830  *	@operstate:	RFC2863 operstate
1831  *	@link_mode:	Mapping policy to operstate
1832  *	@if_port:	Selectable AUI, TP, ...
1833  *	@dma:		DMA channel
1834  *	@mtu:		Interface MTU value
1835  *	@min_mtu:	Interface Minimum MTU value
1836  *	@max_mtu:	Interface Maximum MTU value
1837  *	@type:		Interface hardware type
1838  *	@hard_header_len: Maximum hardware header length.
1839  *	@min_header_len:  Minimum hardware header length
1840  *
1841  *	@needed_headroom: Extra headroom the hardware may need, but not in all
1842  *			  cases can this be guaranteed
1843  *	@needed_tailroom: Extra tailroom the hardware may need, but not in all
1844  *			  cases can this be guaranteed. Some cases also use
1845  *			  LL_MAX_HEADER instead to allocate the skb
1846  *
1847  *	interface address info:
1848  *
1849  * 	@perm_addr:		Permanent hw address
1850  * 	@addr_assign_type:	Hw address assignment type
1851  * 	@addr_len:		Hardware address length
1852  *	@upper_level:		Maximum depth level of upper devices.
1853  *	@lower_level:		Maximum depth level of lower devices.
1854  *	@neigh_priv_len:	Used in neigh_alloc()
1855  * 	@dev_id:		Used to differentiate devices that share
1856  * 				the same link layer address
1857  * 	@dev_port:		Used to differentiate devices that share
1858  * 				the same function
1859  *	@addr_list_lock:	XXX: need comments on this one
1860  *	@name_assign_type:	network interface name assignment type
1861  *	@uc_promisc:		Counter that indicates promiscuous mode
1862  *				has been enabled due to the need to listen to
1863  *				additional unicast addresses in a device that
1864  *				does not implement ndo_set_rx_mode()
1865  *	@uc:			unicast mac addresses
1866  *	@mc:			multicast mac addresses
1867  *	@dev_addrs:		list of device hw addresses
1868  *	@queues_kset:		Group of all Kobjects in the Tx and RX queues
1869  *	@promiscuity:		Number of times the NIC is told to work in
1870  *				promiscuous mode; if it becomes 0 the NIC will
1871  *				exit promiscuous mode
1872  *	@allmulti:		Counter, enables or disables allmulticast mode
1873  *
1874  *	@vlan_info:	VLAN info
1875  *	@dsa_ptr:	dsa specific data
1876  *	@tipc_ptr:	TIPC specific data
1877  *	@atalk_ptr:	AppleTalk link
1878  *	@ip_ptr:	IPv4 specific data
1879  *	@ip6_ptr:	IPv6 specific data
1880  *	@ax25_ptr:	AX.25 specific data
1881  *	@ieee80211_ptr:	IEEE 802.11 specific data, assign before registering
1882  *	@ieee802154_ptr: IEEE 802.15.4 low-rate Wireless Personal Area Network
1883  *			 device struct
1884  *	@mpls_ptr:	mpls_dev struct pointer
1885  *	@mctp_ptr:	MCTP specific data
1886  *
1887  *	@dev_addr:	Hw address (before bcast,
1888  *			because most packets are unicast)
1889  *
1890  *	@_rx:			Array of RX queues
1891  *	@num_rx_queues:		Number of RX queues
1892  *				allocated at register_netdev() time
1893  *	@real_num_rx_queues: 	Number of RX queues currently active in device
1894  *	@xdp_prog:		XDP sockets filter program pointer
1895  *
1896  *	@rx_handler:		handler for received packets
1897  *	@rx_handler_data: 	XXX: need comments on this one
1898  *	@tcx_ingress:		BPF & clsact qdisc specific data for ingress processing
1899  *	@ingress_queue:		XXX: need comments on this one
1900  *	@nf_hooks_ingress:	netfilter hooks executed for ingress packets
1901  *	@broadcast:		hw bcast address
1902  *
1903  *	@rx_cpu_rmap:	CPU reverse-mapping for RX completion interrupts,
1904  *			indexed by RX queue number. Assigned by driver.
1905  *			This must only be set if the ndo_rx_flow_steer
1906  *			operation is defined
1907  *	@index_hlist:		Device index hash chain
1908  *
1909  *	@_tx:			Array of TX queues
1910  *	@num_tx_queues:		Number of TX queues allocated at alloc_netdev_mq() time
1911  *	@real_num_tx_queues: 	Number of TX queues currently active in device
1912  *	@qdisc:			Root qdisc from userspace point of view
1913  *	@tx_queue_len:		Max frames per queue allowed
1914  *	@tx_global_lock: 	XXX: need comments on this one
1915  *	@xdp_bulkq:		XDP device bulk queue
1916  *	@xps_maps:		all CPUs/RXQs maps for XPS device
1917  *
1918  *	@xps_maps:	XXX: need comments on this one
1919  *	@tcx_egress:		BPF & clsact qdisc specific data for egress processing
1920  *	@nf_hooks_egress:	netfilter hooks executed for egress packets
1921  *	@qdisc_hash:		qdisc hash table
1922  *	@watchdog_timeo:	Represents the timeout that is used by
1923  *				the watchdog (see dev_watchdog())
1924  *	@watchdog_timer:	List of timers
1925  *
1926  *	@proto_down_reason:	reason a netdev interface is held down
1927  *	@pcpu_refcnt:		Number of references to this device
1928  *	@dev_refcnt:		Number of references to this device
1929  *	@refcnt_tracker:	Tracker directory for tracked references to this device
1930  *	@todo_list:		Delayed register/unregister
1931  *	@link_watch_list:	XXX: need comments on this one
1932  *
1933  *	@reg_state:		Register/unregister state machine
1934  *	@dismantle:		Device is going to be freed
1935  *	@rtnl_link_state:	This enum represents the phases of creating
1936  *				a new link
1937  *
1938  *	@needs_free_netdev:	Should unregister perform free_netdev?
1939  *	@priv_destructor:	Called from unregister
1940  *	@npinfo:		XXX: need comments on this one
1941  * 	@nd_net:		Network namespace this network device is inside
1942  *
1943  * 	@ml_priv:	Mid-layer private
1944  *	@ml_priv_type:  Mid-layer private type
1945  *
1946  *	@pcpu_stat_type:	Type of device statistics which the core should
1947  *				allocate/free: none, lstats, tstats, dstats. none
1948  *				means the driver is handling statistics allocation/
1949  *				freeing internally.
1950  *	@lstats:		Loopback statistics: packets, bytes
1951  *	@tstats:		Tunnel statistics: RX/TX packets, RX/TX bytes
1952  *	@dstats:		Dummy statistics: RX/TX/drop packets, RX/TX bytes
1953  *
1954  *	@garp_port:	GARP
1955  *	@mrp_port:	MRP
1956  *
1957  *	@dm_private:	Drop monitor private
1958  *
1959  *	@dev:		Class/net/name entry
1960  *	@sysfs_groups:	Space for optional device, statistics and wireless
1961  *			sysfs groups
1962  *
1963  *	@sysfs_rx_queue_group:	Space for optional per-rx queue attributes
1964  *	@rtnl_link_ops:	Rtnl_link_ops
1965  *	@stat_ops:	Optional ops for queue-aware statistics
1966  *	@queue_mgmt_ops:	Optional ops for queue management
1967  *
1968  *	@gso_max_size:	Maximum size of generic segmentation offload
1969  *	@tso_max_size:	Device (as in HW) limit on the max TSO request size
1970  *	@gso_max_segs:	Maximum number of segments that can be passed to the
1971  *			NIC for GSO
1972  *	@tso_max_segs:	Device (as in HW) limit on the max TSO segment count
1973  * 	@gso_ipv4_max_size:	Maximum size of generic segmentation offload,
1974  * 				for IPv4.
1975  *
1976  *	@dcbnl_ops:	Data Center Bridging netlink ops
1977  *	@num_tc:	Number of traffic classes in the net device
1978  *	@tc_to_txq:	XXX: need comments on this one
1979  *	@prio_tc_map:	XXX: need comments on this one
1980  *
1981  *	@fcoe_ddp_xid:	Max exchange id for FCoE LRO by ddp
1982  *
1983  *	@priomap:	XXX: need comments on this one
1984  *	@link_topo:	Physical link topology tracking attached PHYs
1985  *	@phydev:	Physical device may attach itself
1986  *			for hardware timestamping
1987  *	@sfp_bus:	attached &struct sfp_bus structure.
1988  *
1989  *	@qdisc_tx_busylock: lockdep class annotating Qdisc->busylock spinlock
1990  *
1991  *	@proto_down:	protocol port state information can be sent to the
1992  *			switch driver and used to set the phys state of the
1993  *			switch port.
1994  *
1995  *	@threaded:	napi threaded mode is enabled
1996  *
1997  *	@irq_affinity_auto: driver wants the core to store and re-assign the IRQ
1998  *			    affinity. Set by netif_enable_irq_affinity(), then
1999  *			    the driver must create a persistent napi by
2000  *			    netif_napi_add_config() and finally bind the napi to
2001  *			    IRQ (via netif_napi_set_irq()).
2002  *
2003  *	@rx_cpu_rmap_auto: driver wants the core to manage the ARFS rmap.
2004  *	                   Set by calling netif_enable_cpu_rmap().
2005  *
2006  *	@see_all_hwtstamp_requests: device wants to see calls to
2007  *			ndo_hwtstamp_set() for all timestamp requests
2008  *			regardless of source, even if those aren't
2009  *			HWTSTAMP_SOURCE_NETDEV
2010  *	@change_proto_down: device supports setting carrier via IFLA_PROTO_DOWN
2011  *	@netns_local: interface can't change network namespaces
2012  *	@fcoe_mtu:	device supports maximum FCoE MTU, 2158 bytes
2013  *
2014  *	@net_notifier_list:	List of per-net netdev notifier block
2015  *				that follow this device when it is moved
2016  *				to another network namespace.
2017  *
2018  *	@macsec_ops:    MACsec offloading ops
2019  *
2020  *	@udp_tunnel_nic_info:	static structure describing the UDP tunnel
2021  *				offload capabilities of the device
2022  *	@udp_tunnel_nic:	UDP tunnel offload state
2023  *	@ethtool:	ethtool related state
2024  *	@xdp_state:		stores info on attached XDP BPF programs
2025  *
2026  *	@nested_level:	Used as a parameter of spin_lock_nested() of
2027  *			dev->addr_list_lock.
2028  *	@unlink_list:	As netif_addr_lock() can be called recursively,
2029  *			keep a list of interfaces to be deleted.
2030  *	@gro_max_size:	Maximum size of aggregated packet in generic
2031  *			receive offload (GRO)
2032  * 	@gro_ipv4_max_size:	Maximum size of aggregated packet in generic
2033  * 				receive offload (GRO), for IPv4.
2034  *	@xdp_zc_max_segs:	Maximum number of segments supported by AF_XDP
2035  *				zero copy driver
2036  *
2037  *	@dev_addr_shadow:	Copy of @dev_addr to catch direct writes.
2038  *	@linkwatch_dev_tracker:	refcount tracker used by linkwatch.
2039  *	@watchdog_dev_tracker:	refcount tracker used by watchdog.
2040  *	@dev_registered_tracker:	tracker for reference held while
2041  *					registered
2042  *	@offload_xstats_l3:	L3 HW stats for this netdevice.
2043  *
2044  *	@devlink_port:	Pointer to related devlink port structure.
2045  *			Assigned by a driver before netdev registration using
2046  *			SET_NETDEV_DEVLINK_PORT macro. This pointer is static
2047  *			during the time netdevice is registered.
2048  *
2049  *	@dpll_pin: Pointer to the SyncE source pin of a DPLL subsystem,
2050  *		   where the clock is recovered.
2051  *
2052  *	@max_pacing_offload_horizon: max EDT offload horizon in nsec.
2053  *	@napi_config: An array of napi_config structures containing per-NAPI
2054  *		      settings.
2055  *	@gro_flush_timeout:	timeout for GRO layer in NAPI
2056  *	@napi_defer_hard_irqs:	If not zero, provides a counter that would
2057  *				allow to avoid NIC hard IRQ, on busy queues.
2058  *
2059  *	@neighbours:	List heads pointing to this device's neighbours'
2060  *			dev_list, one per address-family.
2061  *	@hwprov: Tracks which PTP performs hardware packet time stamping.
2062  *
2063  *	FIXME: cleanup struct net_device such that network protocol info
2064  *	moves out.
2065  */
2066 
2067 struct net_device {
2068 	/* Cacheline organization can be found documented in
2069 	 * Documentation/networking/net_cachelines/net_device.rst.
2070 	 * Please update the document when adding new fields.
2071 	 */
2072 
2073 	/* TX read-mostly hotpath */
2074 	__cacheline_group_begin(net_device_read_tx);
2075 	struct_group(priv_flags_fast,
2076 		unsigned long		priv_flags:32;
2077 		unsigned long		lltx:1;
2078 	);
2079 	const struct net_device_ops *netdev_ops;
2080 	const struct header_ops *header_ops;
2081 	struct netdev_queue	*_tx;
2082 	netdev_features_t	gso_partial_features;
2083 	unsigned int		real_num_tx_queues;
2084 	unsigned int		gso_max_size;
2085 	unsigned int		gso_ipv4_max_size;
2086 	u16			gso_max_segs;
2087 	s16			num_tc;
2088 	/* Note : dev->mtu is often read without holding a lock.
2089 	 * Writers usually hold RTNL.
2090 	 * It is recommended to use READ_ONCE() to annotate the reads,
2091 	 * and to use WRITE_ONCE() to annotate the writes.
2092 	 */
2093 	unsigned int		mtu;
2094 	unsigned short		needed_headroom;
2095 	struct netdev_tc_txq	tc_to_txq[TC_MAX_QUEUE];
2096 #ifdef CONFIG_XPS
2097 	struct xps_dev_maps __rcu *xps_maps[XPS_MAPS_MAX];
2098 #endif
2099 #ifdef CONFIG_NETFILTER_EGRESS
2100 	struct nf_hook_entries __rcu *nf_hooks_egress;
2101 #endif
2102 #ifdef CONFIG_NET_XGRESS
2103 	struct bpf_mprog_entry __rcu *tcx_egress;
2104 #endif
2105 	__cacheline_group_end(net_device_read_tx);
2106 
2107 	/* TXRX read-mostly hotpath */
2108 	__cacheline_group_begin(net_device_read_txrx);
2109 	union {
2110 		struct pcpu_lstats __percpu		*lstats;
2111 		struct pcpu_sw_netstats __percpu	*tstats;
2112 		struct pcpu_dstats __percpu		*dstats;
2113 	};
2114 	unsigned long		state;
2115 	unsigned int		flags;
2116 	unsigned short		hard_header_len;
2117 	netdev_features_t	features;
2118 	struct inet6_dev __rcu	*ip6_ptr;
2119 	__cacheline_group_end(net_device_read_txrx);
2120 
2121 	/* RX read-mostly hotpath */
2122 	__cacheline_group_begin(net_device_read_rx);
2123 	struct bpf_prog __rcu	*xdp_prog;
2124 	struct list_head	ptype_specific;
2125 	int			ifindex;
2126 	unsigned int		real_num_rx_queues;
2127 	struct netdev_rx_queue	*_rx;
2128 	unsigned int		gro_max_size;
2129 	unsigned int		gro_ipv4_max_size;
2130 	rx_handler_func_t __rcu	*rx_handler;
2131 	void __rcu		*rx_handler_data;
2132 	possible_net_t			nd_net;
2133 #ifdef CONFIG_NETPOLL
2134 	struct netpoll_info __rcu	*npinfo;
2135 #endif
2136 #ifdef CONFIG_NET_XGRESS
2137 	struct bpf_mprog_entry __rcu *tcx_ingress;
2138 #endif
2139 	__cacheline_group_end(net_device_read_rx);
2140 
2141 	char			name[IFNAMSIZ];
2142 	struct netdev_name_node	*name_node;
2143 	struct dev_ifalias	__rcu *ifalias;
2144 	/*
2145 	 *	I/O specific fields
2146 	 *	FIXME: Merge these and struct ifmap into one
2147 	 */
2148 	unsigned long		mem_end;
2149 	unsigned long		mem_start;
2150 	unsigned long		base_addr;
2151 
2152 	/*
2153 	 *	Some hardware also needs these fields (state,dev_list,
2154 	 *	napi_list,unreg_list,close_list) but they are not
2155 	 *	part of the usual set specified in Space.c.
2156 	 */
2157 
2158 
2159 	struct list_head	dev_list;
2160 	struct list_head	napi_list;
2161 	struct list_head	unreg_list;
2162 	struct list_head	close_list;
2163 	struct list_head	ptype_all;
2164 
2165 	struct {
2166 		struct list_head upper;
2167 		struct list_head lower;
2168 	} adj_list;
2169 
2170 	/* Read-mostly cache-line for fast-path access */
2171 	xdp_features_t		xdp_features;
2172 	const struct xdp_metadata_ops *xdp_metadata_ops;
2173 	const struct xsk_tx_metadata_ops *xsk_tx_metadata_ops;
2174 	unsigned short		gflags;
2175 
2176 	unsigned short		needed_tailroom;
2177 
2178 	netdev_features_t	hw_features;
2179 	netdev_features_t	wanted_features;
2180 	netdev_features_t	vlan_features;
2181 	netdev_features_t	hw_enc_features;
2182 	netdev_features_t	mpls_features;
2183 
2184 	unsigned int		min_mtu;
2185 	unsigned int		max_mtu;
2186 	unsigned short		type;
2187 	unsigned char		min_header_len;
2188 	unsigned char		name_assign_type;
2189 
2190 	int			group;
2191 
2192 	struct net_device_stats	stats; /* not used by modern drivers */
2193 
2194 	struct net_device_core_stats __percpu *core_stats;
2195 
2196 	/* Stats to monitor link on/off, flapping */
2197 	atomic_t		carrier_up_count;
2198 	atomic_t		carrier_down_count;
2199 
2200 #ifdef CONFIG_WIRELESS_EXT
2201 	const struct iw_handler_def *wireless_handlers;
2202 #endif
2203 	const struct ethtool_ops *ethtool_ops;
2204 #ifdef CONFIG_NET_L3_MASTER_DEV
2205 	const struct l3mdev_ops	*l3mdev_ops;
2206 #endif
2207 #if IS_ENABLED(CONFIG_IPV6)
2208 	const struct ndisc_ops *ndisc_ops;
2209 #endif
2210 
2211 #ifdef CONFIG_XFRM_OFFLOAD
2212 	const struct xfrmdev_ops *xfrmdev_ops;
2213 #endif
2214 
2215 #if IS_ENABLED(CONFIG_TLS_DEVICE)
2216 	const struct tlsdev_ops *tlsdev_ops;
2217 #endif
2218 
2219 	unsigned int		operstate;
2220 	unsigned char		link_mode;
2221 
2222 	unsigned char		if_port;
2223 	unsigned char		dma;
2224 
2225 	/* Interface address info. */
2226 	unsigned char		perm_addr[MAX_ADDR_LEN];
2227 	unsigned char		addr_assign_type;
2228 	unsigned char		addr_len;
2229 	unsigned char		upper_level;
2230 	unsigned char		lower_level;
2231 
2232 	unsigned short		neigh_priv_len;
2233 	unsigned short          dev_id;
2234 	unsigned short          dev_port;
2235 	int			irq;
2236 	u32			priv_len;
2237 
2238 	spinlock_t		addr_list_lock;
2239 
2240 	struct netdev_hw_addr_list	uc;
2241 	struct netdev_hw_addr_list	mc;
2242 	struct netdev_hw_addr_list	dev_addrs;
2243 
2244 #ifdef CONFIG_SYSFS
2245 	struct kset		*queues_kset;
2246 #endif
2247 #ifdef CONFIG_LOCKDEP
2248 	struct list_head	unlink_list;
2249 #endif
2250 	unsigned int		promiscuity;
2251 	unsigned int		allmulti;
2252 	bool			uc_promisc;
2253 #ifdef CONFIG_LOCKDEP
2254 	unsigned char		nested_level;
2255 #endif
2256 
2257 
2258 	/* Protocol-specific pointers */
2259 	struct in_device __rcu	*ip_ptr;
2260 	/** @fib_nh_head: nexthops associated with this netdev */
2261 	struct hlist_head	fib_nh_head;
2262 
2263 #if IS_ENABLED(CONFIG_VLAN_8021Q)
2264 	struct vlan_info __rcu	*vlan_info;
2265 #endif
2266 #if IS_ENABLED(CONFIG_NET_DSA)
2267 	struct dsa_port		*dsa_ptr;
2268 #endif
2269 #if IS_ENABLED(CONFIG_TIPC)
2270 	struct tipc_bearer __rcu *tipc_ptr;
2271 #endif
2272 #if IS_ENABLED(CONFIG_ATALK)
2273 	void 			*atalk_ptr;
2274 #endif
2275 #if IS_ENABLED(CONFIG_AX25)
2276 	struct ax25_dev	__rcu	*ax25_ptr;
2277 #endif
2278 #if IS_ENABLED(CONFIG_CFG80211)
2279 	struct wireless_dev	*ieee80211_ptr;
2280 #endif
2281 #if IS_ENABLED(CONFIG_IEEE802154) || IS_ENABLED(CONFIG_6LOWPAN)
2282 	struct wpan_dev		*ieee802154_ptr;
2283 #endif
2284 #if IS_ENABLED(CONFIG_MPLS_ROUTING)
2285 	struct mpls_dev __rcu	*mpls_ptr;
2286 #endif
2287 #if IS_ENABLED(CONFIG_MCTP)
2288 	struct mctp_dev __rcu	*mctp_ptr;
2289 #endif
2290 
2291 /*
2292  * Cache lines mostly used on receive path (including eth_type_trans())
2293  */
2294 	/* Interface address info used in eth_type_trans() */
2295 	const unsigned char	*dev_addr;
2296 
2297 	unsigned int		num_rx_queues;
2298 #define GRO_LEGACY_MAX_SIZE	65536u
2299 /* TCP minimal MSS is 8 (TCP_MIN_GSO_SIZE),
2300  * and shinfo->gso_segs is a 16bit field.
2301  */
2302 #define GRO_MAX_SIZE		(8 * 65535u)
2303 	unsigned int		xdp_zc_max_segs;
2304 	struct netdev_queue __rcu *ingress_queue;
2305 #ifdef CONFIG_NETFILTER_INGRESS
2306 	struct nf_hook_entries __rcu *nf_hooks_ingress;
2307 #endif
2308 
2309 	unsigned char		broadcast[MAX_ADDR_LEN];
2310 #ifdef CONFIG_RFS_ACCEL
2311 	struct cpu_rmap		*rx_cpu_rmap;
2312 #endif
2313 	struct hlist_node	index_hlist;
2314 
2315 /*
2316  * Cache lines mostly used on transmit path
2317  */
2318 	unsigned int		num_tx_queues;
2319 	struct Qdisc __rcu	*qdisc;
2320 	unsigned int		tx_queue_len;
2321 	spinlock_t		tx_global_lock;
2322 
2323 	struct xdp_dev_bulk_queue __percpu *xdp_bulkq;
2324 
2325 #ifdef CONFIG_NET_SCHED
2326 	DECLARE_HASHTABLE	(qdisc_hash, 4);
2327 #endif
2328 	/* These may be needed for future network-power-down code. */
2329 	struct timer_list	watchdog_timer;
2330 	int			watchdog_timeo;
2331 
2332 	u32                     proto_down_reason;
2333 
2334 	struct list_head	todo_list;
2335 
2336 #ifdef CONFIG_PCPU_DEV_REFCNT
2337 	int __percpu		*pcpu_refcnt;
2338 #else
2339 	refcount_t		dev_refcnt;
2340 #endif
2341 	struct ref_tracker_dir	refcnt_tracker;
2342 
2343 	struct list_head	link_watch_list;
2344 
2345 	u8 reg_state;
2346 
2347 	bool dismantle;
2348 
2349 	enum {
2350 		RTNL_LINK_INITIALIZED,
2351 		RTNL_LINK_INITIALIZING,
2352 	} rtnl_link_state:16;
2353 
2354 	bool needs_free_netdev;
2355 	void (*priv_destructor)(struct net_device *dev);
2356 
2357 	/* mid-layer private */
2358 	void				*ml_priv;
2359 	enum netdev_ml_priv_type	ml_priv_type;
2360 
2361 	enum netdev_stat_type		pcpu_stat_type:8;
2362 
2363 #if IS_ENABLED(CONFIG_GARP)
2364 	struct garp_port __rcu	*garp_port;
2365 #endif
2366 #if IS_ENABLED(CONFIG_MRP)
2367 	struct mrp_port __rcu	*mrp_port;
2368 #endif
2369 #if IS_ENABLED(CONFIG_NET_DROP_MONITOR)
2370 	struct dm_hw_stat_delta __rcu *dm_private;
2371 #endif
2372 	struct device		dev;
2373 	const struct attribute_group *sysfs_groups[4];
2374 	const struct attribute_group *sysfs_rx_queue_group;
2375 
2376 	const struct rtnl_link_ops *rtnl_link_ops;
2377 
2378 	const struct netdev_stat_ops *stat_ops;
2379 
2380 	const struct netdev_queue_mgmt_ops *queue_mgmt_ops;
2381 
2382 	/* for setting kernel sock attribute on TCP connection setup */
2383 #define GSO_MAX_SEGS		65535u
2384 #define GSO_LEGACY_MAX_SIZE	65536u
2385 /* TCP minimal MSS is 8 (TCP_MIN_GSO_SIZE),
2386  * and shinfo->gso_segs is a 16bit field.
2387  */
2388 #define GSO_MAX_SIZE		(8 * GSO_MAX_SEGS)
2389 
2390 #define TSO_LEGACY_MAX_SIZE	65536
2391 #define TSO_MAX_SIZE		UINT_MAX
2392 	unsigned int		tso_max_size;
2393 #define TSO_MAX_SEGS		U16_MAX
2394 	u16			tso_max_segs;
2395 
2396 #ifdef CONFIG_DCB
2397 	const struct dcbnl_rtnl_ops *dcbnl_ops;
2398 #endif
2399 	u8			prio_tc_map[TC_BITMASK + 1];
2400 
2401 #if IS_ENABLED(CONFIG_FCOE)
2402 	unsigned int		fcoe_ddp_xid;
2403 #endif
2404 #if IS_ENABLED(CONFIG_CGROUP_NET_PRIO)
2405 	struct netprio_map __rcu *priomap;
2406 #endif
2407 	struct phy_link_topology	*link_topo;
2408 	struct phy_device	*phydev;
2409 	struct sfp_bus		*sfp_bus;
2410 	struct lock_class_key	*qdisc_tx_busylock;
2411 	bool			proto_down;
2412 	bool			threaded;
2413 	bool			irq_affinity_auto;
2414 	bool			rx_cpu_rmap_auto;
2415 
2416 	/* priv_flags_slow, ungrouped to save space */
2417 	unsigned long		see_all_hwtstamp_requests:1;
2418 	unsigned long		change_proto_down:1;
2419 	unsigned long		netns_local:1;
2420 	unsigned long		fcoe_mtu:1;
2421 
2422 	struct list_head	net_notifier_list;
2423 
2424 #if IS_ENABLED(CONFIG_MACSEC)
2425 	/* MACsec management functions */
2426 	const struct macsec_ops *macsec_ops;
2427 #endif
2428 	const struct udp_tunnel_nic_info	*udp_tunnel_nic_info;
2429 	struct udp_tunnel_nic	*udp_tunnel_nic;
2430 
2431 	/** @cfg: net_device queue-related configuration */
2432 	struct netdev_config	*cfg;
2433 	/**
2434 	 * @cfg_pending: same as @cfg but when device is being actively
2435 	 *	reconfigured includes any changes to the configuration
2436 	 *	requested by the user, but which may or may not be rejected.
2437 	 */
2438 	struct netdev_config	*cfg_pending;
2439 	struct ethtool_netdev_state *ethtool;
2440 
2441 	/* protected by rtnl_lock */
2442 	struct bpf_xdp_entity	xdp_state[__MAX_XDP_MODE];
2443 
2444 	u8 dev_addr_shadow[MAX_ADDR_LEN];
2445 	netdevice_tracker	linkwatch_dev_tracker;
2446 	netdevice_tracker	watchdog_dev_tracker;
2447 	netdevice_tracker	dev_registered_tracker;
2448 	struct rtnl_hw_stats64	*offload_xstats_l3;
2449 
2450 	struct devlink_port	*devlink_port;
2451 
2452 #if IS_ENABLED(CONFIG_DPLL)
2453 	struct dpll_pin	__rcu	*dpll_pin;
2454 #endif
2455 #if IS_ENABLED(CONFIG_PAGE_POOL)
2456 	/** @page_pools: page pools created for this netdevice */
2457 	struct hlist_head	page_pools;
2458 #endif
2459 
2460 	/** @irq_moder: dim parameters used if IS_ENABLED(CONFIG_DIMLIB). */
2461 	struct dim_irq_moder	*irq_moder;
2462 
2463 	u64			max_pacing_offload_horizon;
2464 	struct napi_config	*napi_config;
2465 	unsigned long		gro_flush_timeout;
2466 	u32			napi_defer_hard_irqs;
2467 
2468 	/**
2469 	 * @up: copy of @state's IFF_UP, but safe to read with just @lock.
2470 	 *	May report false negatives while the device is being opened
2471 	 *	or closed (@lock does not protect .ndo_open, or .ndo_close).
2472 	 */
2473 	bool			up;
2474 
2475 	/**
2476 	 * @lock: netdev-scope lock, protects a small selection of fields.
2477 	 * Should always be taken using netdev_lock() / netdev_unlock() helpers.
2478 	 * Drivers are free to use it for other protection.
2479 	 *
2480 	 * Protects:
2481 	 *	@gro_flush_timeout, @napi_defer_hard_irqs, @napi_list,
2482 	 *	@net_shaper_hierarchy, @reg_state, @threaded
2483 	 *
2484 	 * Partially protects (writers must hold both @lock and rtnl_lock):
2485 	 *	@up
2486 	 *
2487 	 * Also protects some fields in struct napi_struct.
2488 	 *
2489 	 * Ordering: take after rtnl_lock.
2490 	 */
2491 	struct mutex		lock;
2492 
2493 #if IS_ENABLED(CONFIG_NET_SHAPER)
2494 	/**
2495 	 * @net_shaper_hierarchy: data tracking the current shaper status
2496 	 *  see include/net/net_shapers.h
2497 	 */
2498 	struct net_shaper_hierarchy *net_shaper_hierarchy;
2499 #endif
2500 
2501 	struct hlist_head neighbours[NEIGH_NR_TABLES];
2502 
2503 	struct hwtstamp_provider __rcu	*hwprov;
2504 
2505 	u8			priv[] ____cacheline_aligned
2506 				       __counted_by(priv_len);
2507 } ____cacheline_aligned;
2508 #define to_net_dev(d) container_of(d, struct net_device, dev)
2509 
2510 /*
2511  * Driver should use this to assign devlink port instance to a netdevice
2512  * before it registers the netdevice. Therefore devlink_port is static
2513  * during the netdev lifetime after it is registered.
2514  */
2515 #define SET_NETDEV_DEVLINK_PORT(dev, port)			\
2516 ({								\
2517 	WARN_ON((dev)->reg_state != NETREG_UNINITIALIZED);	\
2518 	((dev)->devlink_port = (port));				\
2519 })
2520 
2521 static inline bool netif_elide_gro(const struct net_device *dev)
2522 {
2523 	if (!(dev->features & NETIF_F_GRO) || dev->xdp_prog)
2524 		return true;
2525 	return false;
2526 }
2527 
2528 #define	NETDEV_ALIGN		32
2529 
2530 static inline
2531 int netdev_get_prio_tc_map(const struct net_device *dev, u32 prio)
2532 {
2533 	return dev->prio_tc_map[prio & TC_BITMASK];
2534 }
2535 
2536 static inline
2537 int netdev_set_prio_tc_map(struct net_device *dev, u8 prio, u8 tc)
2538 {
2539 	if (tc >= dev->num_tc)
2540 		return -EINVAL;
2541 
2542 	dev->prio_tc_map[prio & TC_BITMASK] = tc & TC_BITMASK;
2543 	return 0;
2544 }
2545 
2546 int netdev_txq_to_tc(struct net_device *dev, unsigned int txq);
2547 void netdev_reset_tc(struct net_device *dev);
2548 int netdev_set_tc_queue(struct net_device *dev, u8 tc, u16 count, u16 offset);
2549 int netdev_set_num_tc(struct net_device *dev, u8 num_tc);
2550 
2551 static inline
2552 int netdev_get_num_tc(struct net_device *dev)
2553 {
2554 	return dev->num_tc;
2555 }
2556 
2557 static inline void net_prefetch(void *p)
2558 {
2559 	prefetch(p);
2560 #if L1_CACHE_BYTES < 128
2561 	prefetch((u8 *)p + L1_CACHE_BYTES);
2562 #endif
2563 }
2564 
2565 static inline void net_prefetchw(void *p)
2566 {
2567 	prefetchw(p);
2568 #if L1_CACHE_BYTES < 128
2569 	prefetchw((u8 *)p + L1_CACHE_BYTES);
2570 #endif
2571 }
2572 
2573 void netdev_unbind_sb_channel(struct net_device *dev,
2574 			      struct net_device *sb_dev);
2575 int netdev_bind_sb_channel_queue(struct net_device *dev,
2576 				 struct net_device *sb_dev,
2577 				 u8 tc, u16 count, u16 offset);
2578 int netdev_set_sb_channel(struct net_device *dev, u16 channel);
2579 static inline int netdev_get_sb_channel(struct net_device *dev)
2580 {
2581 	return max_t(int, -dev->num_tc, 0);
2582 }
2583 
2584 static inline
2585 struct netdev_queue *netdev_get_tx_queue(const struct net_device *dev,
2586 					 unsigned int index)
2587 {
2588 	DEBUG_NET_WARN_ON_ONCE(index >= dev->num_tx_queues);
2589 	return &dev->_tx[index];
2590 }
2591 
2592 static inline struct netdev_queue *skb_get_tx_queue(const struct net_device *dev,
2593 						    const struct sk_buff *skb)
2594 {
2595 	return netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
2596 }
2597 
2598 static inline void netdev_for_each_tx_queue(struct net_device *dev,
2599 					    void (*f)(struct net_device *,
2600 						      struct netdev_queue *,
2601 						      void *),
2602 					    void *arg)
2603 {
2604 	unsigned int i;
2605 
2606 	for (i = 0; i < dev->num_tx_queues; i++)
2607 		f(dev, &dev->_tx[i], arg);
2608 }
2609 
2610 #define netdev_lockdep_set_classes(dev)				\
2611 {								\
2612 	static struct lock_class_key qdisc_tx_busylock_key;	\
2613 	static struct lock_class_key qdisc_xmit_lock_key;	\
2614 	static struct lock_class_key dev_addr_list_lock_key;	\
2615 	unsigned int i;						\
2616 								\
2617 	(dev)->qdisc_tx_busylock = &qdisc_tx_busylock_key;	\
2618 	lockdep_set_class(&(dev)->addr_list_lock,		\
2619 			  &dev_addr_list_lock_key);		\
2620 	for (i = 0; i < (dev)->num_tx_queues; i++)		\
2621 		lockdep_set_class(&(dev)->_tx[i]._xmit_lock,	\
2622 				  &qdisc_xmit_lock_key);	\
2623 }
2624 
2625 u16 netdev_pick_tx(struct net_device *dev, struct sk_buff *skb,
2626 		     struct net_device *sb_dev);
2627 struct netdev_queue *netdev_core_pick_tx(struct net_device *dev,
2628 					 struct sk_buff *skb,
2629 					 struct net_device *sb_dev);
2630 
2631 /* returns the headroom that the master device needs to take in account
2632  * when forwarding to this dev
2633  */
2634 static inline unsigned netdev_get_fwd_headroom(struct net_device *dev)
2635 {
2636 	return dev->priv_flags & IFF_PHONY_HEADROOM ? 0 : dev->needed_headroom;
2637 }
2638 
2639 static inline void netdev_set_rx_headroom(struct net_device *dev, int new_hr)
2640 {
2641 	if (dev->netdev_ops->ndo_set_rx_headroom)
2642 		dev->netdev_ops->ndo_set_rx_headroom(dev, new_hr);
2643 }
2644 
2645 /* set the device rx headroom to the dev's default */
2646 static inline void netdev_reset_rx_headroom(struct net_device *dev)
2647 {
2648 	netdev_set_rx_headroom(dev, -1);
2649 }
2650 
2651 static inline void *netdev_get_ml_priv(struct net_device *dev,
2652 				       enum netdev_ml_priv_type type)
2653 {
2654 	if (dev->ml_priv_type != type)
2655 		return NULL;
2656 
2657 	return dev->ml_priv;
2658 }
2659 
2660 static inline void netdev_set_ml_priv(struct net_device *dev,
2661 				      void *ml_priv,
2662 				      enum netdev_ml_priv_type type)
2663 {
2664 	WARN(dev->ml_priv_type && dev->ml_priv_type != type,
2665 	     "Overwriting already set ml_priv_type (%u) with different ml_priv_type (%u)!\n",
2666 	     dev->ml_priv_type, type);
2667 	WARN(!dev->ml_priv_type && dev->ml_priv,
2668 	     "Overwriting already set ml_priv and ml_priv_type is ML_PRIV_NONE!\n");
2669 
2670 	dev->ml_priv = ml_priv;
2671 	dev->ml_priv_type = type;
2672 }
2673 
2674 /*
2675  * Net namespace inlines
2676  */
2677 static inline
2678 struct net *dev_net(const struct net_device *dev)
2679 {
2680 	return read_pnet(&dev->nd_net);
2681 }
2682 
2683 static inline
2684 struct net *dev_net_rcu(const struct net_device *dev)
2685 {
2686 	return read_pnet_rcu(&dev->nd_net);
2687 }
2688 
2689 static inline
2690 void dev_net_set(struct net_device *dev, struct net *net)
2691 {
2692 	write_pnet(&dev->nd_net, net);
2693 }
2694 
2695 /**
2696  *	netdev_priv - access network device private data
2697  *	@dev: network device
2698  *
2699  * Get network device private data
2700  */
2701 static inline void *netdev_priv(const struct net_device *dev)
2702 {
2703 	return (void *)dev->priv;
2704 }
2705 
2706 /* Set the sysfs physical device reference for the network logical device
2707  * if set prior to registration will cause a symlink during initialization.
2708  */
2709 #define SET_NETDEV_DEV(net, pdev)	((net)->dev.parent = (pdev))
2710 
2711 /* Set the sysfs device type for the network logical device to allow
2712  * fine-grained identification of different network device types. For
2713  * example Ethernet, Wireless LAN, Bluetooth, WiMAX etc.
2714  */
2715 #define SET_NETDEV_DEVTYPE(net, devtype)	((net)->dev.type = (devtype))
2716 
2717 void netif_queue_set_napi(struct net_device *dev, unsigned int queue_index,
2718 			  enum netdev_queue_type type,
2719 			  struct napi_struct *napi);
2720 
2721 static inline void netdev_lock(struct net_device *dev)
2722 {
2723 	mutex_lock(&dev->lock);
2724 }
2725 
2726 static inline void netdev_unlock(struct net_device *dev)
2727 {
2728 	mutex_unlock(&dev->lock);
2729 }
2730 
2731 static inline void netdev_assert_locked(struct net_device *dev)
2732 {
2733 	lockdep_assert_held(&dev->lock);
2734 }
2735 
2736 static inline void netdev_assert_locked_or_invisible(struct net_device *dev)
2737 {
2738 	if (dev->reg_state == NETREG_REGISTERED ||
2739 	    dev->reg_state == NETREG_UNREGISTERING)
2740 		netdev_assert_locked(dev);
2741 }
2742 
2743 void netif_napi_set_irq_locked(struct napi_struct *napi, int irq);
2744 
2745 static inline void netif_napi_set_irq(struct napi_struct *napi, int irq)
2746 {
2747 	netdev_lock(napi->dev);
2748 	netif_napi_set_irq_locked(napi, irq);
2749 	netdev_unlock(napi->dev);
2750 }
2751 
2752 /* Default NAPI poll() weight
2753  * Device drivers are strongly advised to not use bigger value
2754  */
2755 #define NAPI_POLL_WEIGHT 64
2756 
2757 void netif_napi_add_weight_locked(struct net_device *dev,
2758 				  struct napi_struct *napi,
2759 				  int (*poll)(struct napi_struct *, int),
2760 				  int weight);
2761 
2762 static inline void
2763 netif_napi_add_weight(struct net_device *dev, struct napi_struct *napi,
2764 		      int (*poll)(struct napi_struct *, int), int weight)
2765 {
2766 	netdev_lock(dev);
2767 	netif_napi_add_weight_locked(dev, napi, poll, weight);
2768 	netdev_unlock(dev);
2769 }
2770 
2771 /**
2772  * netif_napi_add() - initialize a NAPI context
2773  * @dev:  network device
2774  * @napi: NAPI context
2775  * @poll: polling function
2776  *
2777  * netif_napi_add() must be used to initialize a NAPI context prior to calling
2778  * *any* of the other NAPI-related functions.
2779  */
2780 static inline void
2781 netif_napi_add(struct net_device *dev, struct napi_struct *napi,
2782 	       int (*poll)(struct napi_struct *, int))
2783 {
2784 	netif_napi_add_weight(dev, napi, poll, NAPI_POLL_WEIGHT);
2785 }
2786 
2787 static inline void
2788 netif_napi_add_locked(struct net_device *dev, struct napi_struct *napi,
2789 		      int (*poll)(struct napi_struct *, int))
2790 {
2791 	netif_napi_add_weight_locked(dev, napi, poll, NAPI_POLL_WEIGHT);
2792 }
2793 
2794 static inline void
2795 netif_napi_add_tx_weight(struct net_device *dev,
2796 			 struct napi_struct *napi,
2797 			 int (*poll)(struct napi_struct *, int),
2798 			 int weight)
2799 {
2800 	set_bit(NAPI_STATE_NO_BUSY_POLL, &napi->state);
2801 	netif_napi_add_weight(dev, napi, poll, weight);
2802 }
2803 
2804 static inline void
2805 netif_napi_add_config_locked(struct net_device *dev, struct napi_struct *napi,
2806 			     int (*poll)(struct napi_struct *, int), int index)
2807 {
2808 	napi->index = index;
2809 	napi->config = &dev->napi_config[index];
2810 	netif_napi_add_weight_locked(dev, napi, poll, NAPI_POLL_WEIGHT);
2811 }
2812 
2813 /**
2814  * netif_napi_add_config - initialize a NAPI context with persistent config
2815  * @dev: network device
2816  * @napi: NAPI context
2817  * @poll: polling function
2818  * @index: the NAPI index
2819  */
2820 static inline void
2821 netif_napi_add_config(struct net_device *dev, struct napi_struct *napi,
2822 		      int (*poll)(struct napi_struct *, int), int index)
2823 {
2824 	netdev_lock(dev);
2825 	netif_napi_add_config_locked(dev, napi, poll, index);
2826 	netdev_unlock(dev);
2827 }
2828 
2829 /**
2830  * netif_napi_add_tx() - initialize a NAPI context to be used for Tx only
2831  * @dev:  network device
2832  * @napi: NAPI context
2833  * @poll: polling function
2834  *
2835  * This variant of netif_napi_add() should be used from drivers using NAPI
2836  * to exclusively poll a TX queue.
2837  * This will avoid we add it into napi_hash[], thus polluting this hash table.
2838  */
2839 static inline void netif_napi_add_tx(struct net_device *dev,
2840 				     struct napi_struct *napi,
2841 				     int (*poll)(struct napi_struct *, int))
2842 {
2843 	netif_napi_add_tx_weight(dev, napi, poll, NAPI_POLL_WEIGHT);
2844 }
2845 
2846 void __netif_napi_del_locked(struct napi_struct *napi);
2847 
2848 /**
2849  *  __netif_napi_del - remove a NAPI context
2850  *  @napi: NAPI context
2851  *
2852  * Warning: caller must observe RCU grace period before freeing memory
2853  * containing @napi. Drivers might want to call this helper to combine
2854  * all the needed RCU grace periods into a single one.
2855  */
2856 static inline void __netif_napi_del(struct napi_struct *napi)
2857 {
2858 	netdev_lock(napi->dev);
2859 	__netif_napi_del_locked(napi);
2860 	netdev_unlock(napi->dev);
2861 }
2862 
2863 static inline void netif_napi_del_locked(struct napi_struct *napi)
2864 {
2865 	__netif_napi_del_locked(napi);
2866 	synchronize_net();
2867 }
2868 
2869 /**
2870  *  netif_napi_del - remove a NAPI context
2871  *  @napi: NAPI context
2872  *
2873  *  netif_napi_del() removes a NAPI context from the network device NAPI list
2874  */
2875 static inline void netif_napi_del(struct napi_struct *napi)
2876 {
2877 	__netif_napi_del(napi);
2878 	synchronize_net();
2879 }
2880 
2881 int netif_enable_cpu_rmap(struct net_device *dev, unsigned int num_irqs);
2882 void netif_set_affinity_auto(struct net_device *dev);
2883 
2884 struct packet_type {
2885 	__be16			type;	/* This is really htons(ether_type). */
2886 	bool			ignore_outgoing;
2887 	struct net_device	*dev;	/* NULL is wildcarded here	     */
2888 	netdevice_tracker	dev_tracker;
2889 	int			(*func) (struct sk_buff *,
2890 					 struct net_device *,
2891 					 struct packet_type *,
2892 					 struct net_device *);
2893 	void			(*list_func) (struct list_head *,
2894 					      struct packet_type *,
2895 					      struct net_device *);
2896 	bool			(*id_match)(struct packet_type *ptype,
2897 					    struct sock *sk);
2898 	struct net		*af_packet_net;
2899 	void			*af_packet_priv;
2900 	struct list_head	list;
2901 };
2902 
2903 struct offload_callbacks {
2904 	struct sk_buff		*(*gso_segment)(struct sk_buff *skb,
2905 						netdev_features_t features);
2906 	struct sk_buff		*(*gro_receive)(struct list_head *head,
2907 						struct sk_buff *skb);
2908 	int			(*gro_complete)(struct sk_buff *skb, int nhoff);
2909 };
2910 
2911 struct packet_offload {
2912 	__be16			 type;	/* This is really htons(ether_type). */
2913 	u16			 priority;
2914 	struct offload_callbacks callbacks;
2915 	struct list_head	 list;
2916 };
2917 
2918 /* often modified stats are per-CPU, other are shared (netdev->stats) */
2919 struct pcpu_sw_netstats {
2920 	u64_stats_t		rx_packets;
2921 	u64_stats_t		rx_bytes;
2922 	u64_stats_t		tx_packets;
2923 	u64_stats_t		tx_bytes;
2924 	struct u64_stats_sync   syncp;
2925 } __aligned(4 * sizeof(u64));
2926 
2927 struct pcpu_dstats {
2928 	u64_stats_t		rx_packets;
2929 	u64_stats_t		rx_bytes;
2930 	u64_stats_t		tx_packets;
2931 	u64_stats_t		tx_bytes;
2932 	u64_stats_t		rx_drops;
2933 	u64_stats_t		tx_drops;
2934 	struct u64_stats_sync	syncp;
2935 } __aligned(8 * sizeof(u64));
2936 
2937 struct pcpu_lstats {
2938 	u64_stats_t packets;
2939 	u64_stats_t bytes;
2940 	struct u64_stats_sync syncp;
2941 } __aligned(2 * sizeof(u64));
2942 
2943 void dev_lstats_read(struct net_device *dev, u64 *packets, u64 *bytes);
2944 
2945 static inline void dev_sw_netstats_rx_add(struct net_device *dev, unsigned int len)
2946 {
2947 	struct pcpu_sw_netstats *tstats = this_cpu_ptr(dev->tstats);
2948 
2949 	u64_stats_update_begin(&tstats->syncp);
2950 	u64_stats_add(&tstats->rx_bytes, len);
2951 	u64_stats_inc(&tstats->rx_packets);
2952 	u64_stats_update_end(&tstats->syncp);
2953 }
2954 
2955 static inline void dev_sw_netstats_tx_add(struct net_device *dev,
2956 					  unsigned int packets,
2957 					  unsigned int len)
2958 {
2959 	struct pcpu_sw_netstats *tstats = this_cpu_ptr(dev->tstats);
2960 
2961 	u64_stats_update_begin(&tstats->syncp);
2962 	u64_stats_add(&tstats->tx_bytes, len);
2963 	u64_stats_add(&tstats->tx_packets, packets);
2964 	u64_stats_update_end(&tstats->syncp);
2965 }
2966 
2967 static inline void dev_lstats_add(struct net_device *dev, unsigned int len)
2968 {
2969 	struct pcpu_lstats *lstats = this_cpu_ptr(dev->lstats);
2970 
2971 	u64_stats_update_begin(&lstats->syncp);
2972 	u64_stats_add(&lstats->bytes, len);
2973 	u64_stats_inc(&lstats->packets);
2974 	u64_stats_update_end(&lstats->syncp);
2975 }
2976 
2977 static inline void dev_dstats_rx_add(struct net_device *dev,
2978 				     unsigned int len)
2979 {
2980 	struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);
2981 
2982 	u64_stats_update_begin(&dstats->syncp);
2983 	u64_stats_inc(&dstats->rx_packets);
2984 	u64_stats_add(&dstats->rx_bytes, len);
2985 	u64_stats_update_end(&dstats->syncp);
2986 }
2987 
2988 static inline void dev_dstats_rx_dropped(struct net_device *dev)
2989 {
2990 	struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);
2991 
2992 	u64_stats_update_begin(&dstats->syncp);
2993 	u64_stats_inc(&dstats->rx_drops);
2994 	u64_stats_update_end(&dstats->syncp);
2995 }
2996 
2997 static inline void dev_dstats_tx_add(struct net_device *dev,
2998 				     unsigned int len)
2999 {
3000 	struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);
3001 
3002 	u64_stats_update_begin(&dstats->syncp);
3003 	u64_stats_inc(&dstats->tx_packets);
3004 	u64_stats_add(&dstats->tx_bytes, len);
3005 	u64_stats_update_end(&dstats->syncp);
3006 }
3007 
3008 static inline void dev_dstats_tx_dropped(struct net_device *dev)
3009 {
3010 	struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);
3011 
3012 	u64_stats_update_begin(&dstats->syncp);
3013 	u64_stats_inc(&dstats->tx_drops);
3014 	u64_stats_update_end(&dstats->syncp);
3015 }
3016 
3017 #define __netdev_alloc_pcpu_stats(type, gfp)				\
3018 ({									\
3019 	typeof(type) __percpu *pcpu_stats = alloc_percpu_gfp(type, gfp);\
3020 	if (pcpu_stats)	{						\
3021 		int __cpu;						\
3022 		for_each_possible_cpu(__cpu) {				\
3023 			typeof(type) *stat;				\
3024 			stat = per_cpu_ptr(pcpu_stats, __cpu);		\
3025 			u64_stats_init(&stat->syncp);			\
3026 		}							\
3027 	}								\
3028 	pcpu_stats;							\
3029 })
3030 
3031 #define netdev_alloc_pcpu_stats(type)					\
3032 	__netdev_alloc_pcpu_stats(type, GFP_KERNEL)
3033 
3034 #define devm_netdev_alloc_pcpu_stats(dev, type)				\
3035 ({									\
3036 	typeof(type) __percpu *pcpu_stats = devm_alloc_percpu(dev, type);\
3037 	if (pcpu_stats) {						\
3038 		int __cpu;						\
3039 		for_each_possible_cpu(__cpu) {				\
3040 			typeof(type) *stat;				\
3041 			stat = per_cpu_ptr(pcpu_stats, __cpu);		\
3042 			u64_stats_init(&stat->syncp);			\
3043 		}							\
3044 	}								\
3045 	pcpu_stats;							\
3046 })
3047 
3048 enum netdev_lag_tx_type {
3049 	NETDEV_LAG_TX_TYPE_UNKNOWN,
3050 	NETDEV_LAG_TX_TYPE_RANDOM,
3051 	NETDEV_LAG_TX_TYPE_BROADCAST,
3052 	NETDEV_LAG_TX_TYPE_ROUNDROBIN,
3053 	NETDEV_LAG_TX_TYPE_ACTIVEBACKUP,
3054 	NETDEV_LAG_TX_TYPE_HASH,
3055 };
3056 
3057 enum netdev_lag_hash {
3058 	NETDEV_LAG_HASH_NONE,
3059 	NETDEV_LAG_HASH_L2,
3060 	NETDEV_LAG_HASH_L34,
3061 	NETDEV_LAG_HASH_L23,
3062 	NETDEV_LAG_HASH_E23,
3063 	NETDEV_LAG_HASH_E34,
3064 	NETDEV_LAG_HASH_VLAN_SRCMAC,
3065 	NETDEV_LAG_HASH_UNKNOWN,
3066 };
3067 
3068 struct netdev_lag_upper_info {
3069 	enum netdev_lag_tx_type tx_type;
3070 	enum netdev_lag_hash hash_type;
3071 };
3072 
3073 struct netdev_lag_lower_state_info {
3074 	u8 link_up : 1,
3075 	   tx_enabled : 1;
3076 };
3077 
3078 #include <linux/notifier.h>
3079 
3080 /* netdevice notifier chain. Please remember to update netdev_cmd_to_name()
3081  * and the rtnetlink notification exclusion list in rtnetlink_event() when
3082  * adding new types.
3083  */
3084 enum netdev_cmd {
3085 	NETDEV_UP	= 1,	/* For now you can't veto a device up/down */
3086 	NETDEV_DOWN,
3087 	NETDEV_REBOOT,		/* Tell a protocol stack a network interface
3088 				   detected a hardware crash and restarted
3089 				   - we can use this eg to kick tcp sessions
3090 				   once done */
3091 	NETDEV_CHANGE,		/* Notify device state change */
3092 	NETDEV_REGISTER,
3093 	NETDEV_UNREGISTER,
3094 	NETDEV_CHANGEMTU,	/* notify after mtu change happened */
3095 	NETDEV_CHANGEADDR,	/* notify after the address change */
3096 	NETDEV_PRE_CHANGEADDR,	/* notify before the address change */
3097 	NETDEV_GOING_DOWN,
3098 	NETDEV_CHANGENAME,
3099 	NETDEV_FEAT_CHANGE,
3100 	NETDEV_BONDING_FAILOVER,
3101 	NETDEV_PRE_UP,
3102 	NETDEV_PRE_TYPE_CHANGE,
3103 	NETDEV_POST_TYPE_CHANGE,
3104 	NETDEV_POST_INIT,
3105 	NETDEV_PRE_UNINIT,
3106 	NETDEV_RELEASE,
3107 	NETDEV_NOTIFY_PEERS,
3108 	NETDEV_JOIN,
3109 	NETDEV_CHANGEUPPER,
3110 	NETDEV_RESEND_IGMP,
3111 	NETDEV_PRECHANGEMTU,	/* notify before mtu change happened */
3112 	NETDEV_CHANGEINFODATA,
3113 	NETDEV_BONDING_INFO,
3114 	NETDEV_PRECHANGEUPPER,
3115 	NETDEV_CHANGELOWERSTATE,
3116 	NETDEV_UDP_TUNNEL_PUSH_INFO,
3117 	NETDEV_UDP_TUNNEL_DROP_INFO,
3118 	NETDEV_CHANGE_TX_QUEUE_LEN,
3119 	NETDEV_CVLAN_FILTER_PUSH_INFO,
3120 	NETDEV_CVLAN_FILTER_DROP_INFO,
3121 	NETDEV_SVLAN_FILTER_PUSH_INFO,
3122 	NETDEV_SVLAN_FILTER_DROP_INFO,
3123 	NETDEV_OFFLOAD_XSTATS_ENABLE,
3124 	NETDEV_OFFLOAD_XSTATS_DISABLE,
3125 	NETDEV_OFFLOAD_XSTATS_REPORT_USED,
3126 	NETDEV_OFFLOAD_XSTATS_REPORT_DELTA,
3127 	NETDEV_XDP_FEAT_CHANGE,
3128 };
3129 const char *netdev_cmd_to_name(enum netdev_cmd cmd);
3130 
3131 int register_netdevice_notifier(struct notifier_block *nb);
3132 int unregister_netdevice_notifier(struct notifier_block *nb);
3133 int register_netdevice_notifier_net(struct net *net, struct notifier_block *nb);
3134 int unregister_netdevice_notifier_net(struct net *net,
3135 				      struct notifier_block *nb);
3136 int register_netdevice_notifier_dev_net(struct net_device *dev,
3137 					struct notifier_block *nb,
3138 					struct netdev_net_notifier *nn);
3139 int unregister_netdevice_notifier_dev_net(struct net_device *dev,
3140 					  struct notifier_block *nb,
3141 					  struct netdev_net_notifier *nn);
3142 
3143 struct netdev_notifier_info {
3144 	struct net_device	*dev;
3145 	struct netlink_ext_ack	*extack;
3146 };
3147 
3148 struct netdev_notifier_info_ext {
3149 	struct netdev_notifier_info info; /* must be first */
3150 	union {
3151 		u32 mtu;
3152 	} ext;
3153 };
3154 
3155 struct netdev_notifier_change_info {
3156 	struct netdev_notifier_info info; /* must be first */
3157 	unsigned int flags_changed;
3158 };
3159 
3160 struct netdev_notifier_changeupper_info {
3161 	struct netdev_notifier_info info; /* must be first */
3162 	struct net_device *upper_dev; /* new upper dev */
3163 	bool master; /* is upper dev master */
3164 	bool linking; /* is the notification for link or unlink */
3165 	void *upper_info; /* upper dev info */
3166 };
3167 
3168 struct netdev_notifier_changelowerstate_info {
3169 	struct netdev_notifier_info info; /* must be first */
3170 	void *lower_state_info; /* is lower dev state */
3171 };
3172 
3173 struct netdev_notifier_pre_changeaddr_info {
3174 	struct netdev_notifier_info info; /* must be first */
3175 	const unsigned char *dev_addr;
3176 };
3177 
3178 enum netdev_offload_xstats_type {
3179 	NETDEV_OFFLOAD_XSTATS_TYPE_L3 = 1,
3180 };
3181 
3182 struct netdev_notifier_offload_xstats_info {
3183 	struct netdev_notifier_info info; /* must be first */
3184 	enum netdev_offload_xstats_type type;
3185 
3186 	union {
3187 		/* NETDEV_OFFLOAD_XSTATS_REPORT_DELTA */
3188 		struct netdev_notifier_offload_xstats_rd *report_delta;
3189 		/* NETDEV_OFFLOAD_XSTATS_REPORT_USED */
3190 		struct netdev_notifier_offload_xstats_ru *report_used;
3191 	};
3192 };
3193 
3194 int netdev_offload_xstats_enable(struct net_device *dev,
3195 				 enum netdev_offload_xstats_type type,
3196 				 struct netlink_ext_ack *extack);
3197 int netdev_offload_xstats_disable(struct net_device *dev,
3198 				  enum netdev_offload_xstats_type type);
3199 bool netdev_offload_xstats_enabled(const struct net_device *dev,
3200 				   enum netdev_offload_xstats_type type);
3201 int netdev_offload_xstats_get(struct net_device *dev,
3202 			      enum netdev_offload_xstats_type type,
3203 			      struct rtnl_hw_stats64 *stats, bool *used,
3204 			      struct netlink_ext_ack *extack);
3205 void
3206 netdev_offload_xstats_report_delta(struct netdev_notifier_offload_xstats_rd *rd,
3207 				   const struct rtnl_hw_stats64 *stats);
3208 void
3209 netdev_offload_xstats_report_used(struct netdev_notifier_offload_xstats_ru *ru);
3210 void netdev_offload_xstats_push_delta(struct net_device *dev,
3211 				      enum netdev_offload_xstats_type type,
3212 				      const struct rtnl_hw_stats64 *stats);
3213 
3214 static inline void netdev_notifier_info_init(struct netdev_notifier_info *info,
3215 					     struct net_device *dev)
3216 {
3217 	info->dev = dev;
3218 	info->extack = NULL;
3219 }
3220 
3221 static inline struct net_device *
3222 netdev_notifier_info_to_dev(const struct netdev_notifier_info *info)
3223 {
3224 	return info->dev;
3225 }
3226 
3227 static inline struct netlink_ext_ack *
3228 netdev_notifier_info_to_extack(const struct netdev_notifier_info *info)
3229 {
3230 	return info->extack;
3231 }
3232 
3233 int call_netdevice_notifiers(unsigned long val, struct net_device *dev);
3234 int call_netdevice_notifiers_info(unsigned long val,
3235 				  struct netdev_notifier_info *info);
3236 
3237 #define for_each_netdev(net, d)		\
3238 		list_for_each_entry(d, &(net)->dev_base_head, dev_list)
3239 #define for_each_netdev_reverse(net, d)	\
3240 		list_for_each_entry_reverse(d, &(net)->dev_base_head, dev_list)
3241 #define for_each_netdev_rcu(net, d)		\
3242 		list_for_each_entry_rcu(d, &(net)->dev_base_head, dev_list)
3243 #define for_each_netdev_safe(net, d, n)	\
3244 		list_for_each_entry_safe(d, n, &(net)->dev_base_head, dev_list)
3245 #define for_each_netdev_continue(net, d)		\
3246 		list_for_each_entry_continue(d, &(net)->dev_base_head, dev_list)
3247 #define for_each_netdev_continue_reverse(net, d)		\
3248 		list_for_each_entry_continue_reverse(d, &(net)->dev_base_head, \
3249 						     dev_list)
3250 #define for_each_netdev_continue_rcu(net, d)		\
3251 	list_for_each_entry_continue_rcu(d, &(net)->dev_base_head, dev_list)
3252 #define for_each_netdev_in_bond_rcu(bond, slave)	\
3253 		for_each_netdev_rcu(&init_net, slave)	\
3254 			if (netdev_master_upper_dev_get_rcu(slave) == (bond))
3255 #define net_device_entry(lh)	list_entry(lh, struct net_device, dev_list)
3256 
3257 #define for_each_netdev_dump(net, d, ifindex)				\
3258 	for (; (d = xa_find(&(net)->dev_by_index, &ifindex,		\
3259 			    ULONG_MAX, XA_PRESENT)); ifindex++)
3260 
3261 static inline struct net_device *next_net_device(struct net_device *dev)
3262 {
3263 	struct list_head *lh;
3264 	struct net *net;
3265 
3266 	net = dev_net(dev);
3267 	lh = dev->dev_list.next;
3268 	return lh == &net->dev_base_head ? NULL : net_device_entry(lh);
3269 }
3270 
3271 static inline struct net_device *next_net_device_rcu(struct net_device *dev)
3272 {
3273 	struct list_head *lh;
3274 	struct net *net;
3275 
3276 	net = dev_net(dev);
3277 	lh = rcu_dereference(list_next_rcu(&dev->dev_list));
3278 	return lh == &net->dev_base_head ? NULL : net_device_entry(lh);
3279 }
3280 
3281 static inline struct net_device *first_net_device(struct net *net)
3282 {
3283 	return list_empty(&net->dev_base_head) ? NULL :
3284 		net_device_entry(net->dev_base_head.next);
3285 }
3286 
3287 static inline struct net_device *first_net_device_rcu(struct net *net)
3288 {
3289 	struct list_head *lh = rcu_dereference(list_next_rcu(&net->dev_base_head));
3290 
3291 	return lh == &net->dev_base_head ? NULL : net_device_entry(lh);
3292 }
3293 
3294 int netdev_boot_setup_check(struct net_device *dev);
3295 struct net_device *dev_getbyhwaddr(struct net *net, unsigned short type,
3296 				   const char *hwaddr);
3297 struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type,
3298 				       const char *hwaddr);
3299 struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type);
3300 void dev_add_pack(struct packet_type *pt);
3301 void dev_remove_pack(struct packet_type *pt);
3302 void __dev_remove_pack(struct packet_type *pt);
3303 void dev_add_offload(struct packet_offload *po);
3304 void dev_remove_offload(struct packet_offload *po);
3305 
3306 int dev_get_iflink(const struct net_device *dev);
3307 int dev_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb);
3308 int dev_fill_forward_path(const struct net_device *dev, const u8 *daddr,
3309 			  struct net_device_path_stack *stack);
3310 struct net_device *__dev_get_by_flags(struct net *net, unsigned short flags,
3311 				      unsigned short mask);
3312 struct net_device *dev_get_by_name(struct net *net, const char *name);
3313 struct net_device *dev_get_by_name_rcu(struct net *net, const char *name);
3314 struct net_device *__dev_get_by_name(struct net *net, const char *name);
3315 bool netdev_name_in_use(struct net *net, const char *name);
3316 int dev_alloc_name(struct net_device *dev, const char *name);
3317 int dev_open(struct net_device *dev, struct netlink_ext_ack *extack);
3318 void dev_close(struct net_device *dev);
3319 void dev_close_many(struct list_head *head, bool unlink);
3320 void dev_disable_lro(struct net_device *dev);
3321 int dev_loopback_xmit(struct net *net, struct sock *sk, struct sk_buff *newskb);
3322 u16 dev_pick_tx_zero(struct net_device *dev, struct sk_buff *skb,
3323 		     struct net_device *sb_dev);
3324 
3325 int __dev_queue_xmit(struct sk_buff *skb, struct net_device *sb_dev);
3326 int __dev_direct_xmit(struct sk_buff *skb, u16 queue_id);
3327 
3328 static inline int dev_queue_xmit(struct sk_buff *skb)
3329 {
3330 	return __dev_queue_xmit(skb, NULL);
3331 }
3332 
3333 static inline int dev_queue_xmit_accel(struct sk_buff *skb,
3334 				       struct net_device *sb_dev)
3335 {
3336 	return __dev_queue_xmit(skb, sb_dev);
3337 }
3338 
3339 static inline int dev_direct_xmit(struct sk_buff *skb, u16 queue_id)
3340 {
3341 	int ret;
3342 
3343 	ret = __dev_direct_xmit(skb, queue_id);
3344 	if (!dev_xmit_complete(ret))
3345 		kfree_skb(skb);
3346 	return ret;
3347 }
3348 
3349 int register_netdevice(struct net_device *dev);
3350 void unregister_netdevice_queue(struct net_device *dev, struct list_head *head);
3351 void unregister_netdevice_many(struct list_head *head);
3352 static inline void unregister_netdevice(struct net_device *dev)
3353 {
3354 	unregister_netdevice_queue(dev, NULL);
3355 }
3356 
3357 int netdev_refcnt_read(const struct net_device *dev);
3358 void free_netdev(struct net_device *dev);
3359 
3360 struct net_device *netdev_get_xmit_slave(struct net_device *dev,
3361 					 struct sk_buff *skb,
3362 					 bool all_slaves);
3363 struct net_device *netdev_sk_get_lowest_dev(struct net_device *dev,
3364 					    struct sock *sk);
3365 struct net_device *dev_get_by_index(struct net *net, int ifindex);
3366 struct net_device *__dev_get_by_index(struct net *net, int ifindex);
3367 struct net_device *netdev_get_by_index(struct net *net, int ifindex,
3368 				       netdevice_tracker *tracker, gfp_t gfp);
3369 struct net_device *netdev_get_by_name(struct net *net, const char *name,
3370 				      netdevice_tracker *tracker, gfp_t gfp);
3371 struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex);
3372 void netdev_copy_name(struct net_device *dev, char *name);
3373 
3374 static inline int dev_hard_header(struct sk_buff *skb, struct net_device *dev,
3375 				  unsigned short type,
3376 				  const void *daddr, const void *saddr,
3377 				  unsigned int len)
3378 {
3379 	if (!dev->header_ops || !dev->header_ops->create)
3380 		return 0;
3381 
3382 	return dev->header_ops->create(skb, dev, type, daddr, saddr, len);
3383 }
3384 
3385 static inline int dev_parse_header(const struct sk_buff *skb,
3386 				   unsigned char *haddr)
3387 {
3388 	const struct net_device *dev = skb->dev;
3389 
3390 	if (!dev->header_ops || !dev->header_ops->parse)
3391 		return 0;
3392 	return dev->header_ops->parse(skb, haddr);
3393 }
3394 
3395 static inline __be16 dev_parse_header_protocol(const struct sk_buff *skb)
3396 {
3397 	const struct net_device *dev = skb->dev;
3398 
3399 	if (!dev->header_ops || !dev->header_ops->parse_protocol)
3400 		return 0;
3401 	return dev->header_ops->parse_protocol(skb);
3402 }
3403 
3404 /* ll_header must have at least hard_header_len allocated */
3405 static inline bool dev_validate_header(const struct net_device *dev,
3406 				       char *ll_header, int len)
3407 {
3408 	if (likely(len >= dev->hard_header_len))
3409 		return true;
3410 	if (len < dev->min_header_len)
3411 		return false;
3412 
3413 	if (capable(CAP_SYS_RAWIO)) {
3414 		memset(ll_header + len, 0, dev->hard_header_len - len);
3415 		return true;
3416 	}
3417 
3418 	if (dev->header_ops && dev->header_ops->validate)
3419 		return dev->header_ops->validate(ll_header, len);
3420 
3421 	return false;
3422 }
3423 
3424 static inline bool dev_has_header(const struct net_device *dev)
3425 {
3426 	return dev->header_ops && dev->header_ops->create;
3427 }
3428 
3429 /*
3430  * Incoming packets are placed on per-CPU queues
3431  */
3432 struct softnet_data {
3433 	struct list_head	poll_list;
3434 	struct sk_buff_head	process_queue;
3435 	local_lock_t		process_queue_bh_lock;
3436 
3437 	/* stats */
3438 	unsigned int		processed;
3439 	unsigned int		time_squeeze;
3440 #ifdef CONFIG_RPS
3441 	struct softnet_data	*rps_ipi_list;
3442 #endif
3443 
3444 	unsigned int		received_rps;
3445 	bool			in_net_rx_action;
3446 	bool			in_napi_threaded_poll;
3447 
3448 #ifdef CONFIG_NET_FLOW_LIMIT
3449 	struct sd_flow_limit __rcu *flow_limit;
3450 #endif
3451 	struct Qdisc		*output_queue;
3452 	struct Qdisc		**output_queue_tailp;
3453 	struct sk_buff		*completion_queue;
3454 #ifdef CONFIG_XFRM_OFFLOAD
3455 	struct sk_buff_head	xfrm_backlog;
3456 #endif
3457 	/* written and read only by owning cpu: */
3458 	struct netdev_xmit xmit;
3459 #ifdef CONFIG_RPS
3460 	/* input_queue_head should be written by cpu owning this struct,
3461 	 * and only read by other cpus. Worth using a cache line.
3462 	 */
3463 	unsigned int		input_queue_head ____cacheline_aligned_in_smp;
3464 
3465 	/* Elements below can be accessed between CPUs for RPS/RFS */
3466 	call_single_data_t	csd ____cacheline_aligned_in_smp;
3467 	struct softnet_data	*rps_ipi_next;
3468 	unsigned int		cpu;
3469 	unsigned int		input_queue_tail;
3470 #endif
3471 	struct sk_buff_head	input_pkt_queue;
3472 	struct napi_struct	backlog;
3473 
3474 	atomic_t		dropped ____cacheline_aligned_in_smp;
3475 
3476 	/* Another possibly contended cache line */
3477 	spinlock_t		defer_lock ____cacheline_aligned_in_smp;
3478 	int			defer_count;
3479 	int			defer_ipi_scheduled;
3480 	struct sk_buff		*defer_list;
3481 	call_single_data_t	defer_csd;
3482 };
3483 
3484 DECLARE_PER_CPU_ALIGNED(struct softnet_data, softnet_data);
3485 DECLARE_PER_CPU(struct page_pool *, system_page_pool);
3486 
3487 #ifndef CONFIG_PREEMPT_RT
3488 static inline int dev_recursion_level(void)
3489 {
3490 	return this_cpu_read(softnet_data.xmit.recursion);
3491 }
3492 #else
3493 static inline int dev_recursion_level(void)
3494 {
3495 	return current->net_xmit.recursion;
3496 }
3497 
3498 #endif
3499 
3500 void __netif_schedule(struct Qdisc *q);
3501 void netif_schedule_queue(struct netdev_queue *txq);
3502 
3503 static inline void netif_tx_schedule_all(struct net_device *dev)
3504 {
3505 	unsigned int i;
3506 
3507 	for (i = 0; i < dev->num_tx_queues; i++)
3508 		netif_schedule_queue(netdev_get_tx_queue(dev, i));
3509 }
3510 
3511 static __always_inline void netif_tx_start_queue(struct netdev_queue *dev_queue)
3512 {
3513 	clear_bit(__QUEUE_STATE_DRV_XOFF, &dev_queue->state);
3514 }
3515 
3516 /**
3517  *	netif_start_queue - allow transmit
3518  *	@dev: network device
3519  *
3520  *	Allow upper layers to call the device hard_start_xmit routine.
3521  */
3522 static inline void netif_start_queue(struct net_device *dev)
3523 {
3524 	netif_tx_start_queue(netdev_get_tx_queue(dev, 0));
3525 }
3526 
3527 static inline void netif_tx_start_all_queues(struct net_device *dev)
3528 {
3529 	unsigned int i;
3530 
3531 	for (i = 0; i < dev->num_tx_queues; i++) {
3532 		struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
3533 		netif_tx_start_queue(txq);
3534 	}
3535 }
3536 
3537 void netif_tx_wake_queue(struct netdev_queue *dev_queue);
3538 
3539 /**
3540  *	netif_wake_queue - restart transmit
3541  *	@dev: network device
3542  *
3543  *	Allow upper layers to call the device hard_start_xmit routine.
3544  *	Used for flow control when transmit resources are available.
3545  */
3546 static inline void netif_wake_queue(struct net_device *dev)
3547 {
3548 	netif_tx_wake_queue(netdev_get_tx_queue(dev, 0));
3549 }
3550 
3551 static inline void netif_tx_wake_all_queues(struct net_device *dev)
3552 {
3553 	unsigned int i;
3554 
3555 	for (i = 0; i < dev->num_tx_queues; i++) {
3556 		struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
3557 		netif_tx_wake_queue(txq);
3558 	}
3559 }
3560 
3561 static __always_inline void netif_tx_stop_queue(struct netdev_queue *dev_queue)
3562 {
3563 	/* Paired with READ_ONCE() from dev_watchdog() */
3564 	WRITE_ONCE(dev_queue->trans_start, jiffies);
3565 
3566 	/* This barrier is paired with smp_mb() from dev_watchdog() */
3567 	smp_mb__before_atomic();
3568 
3569 	/* Must be an atomic op see netif_txq_try_stop() */
3570 	set_bit(__QUEUE_STATE_DRV_XOFF, &dev_queue->state);
3571 }
3572 
3573 /**
3574  *	netif_stop_queue - stop transmitted packets
3575  *	@dev: network device
3576  *
3577  *	Stop upper layers calling the device hard_start_xmit routine.
3578  *	Used for flow control when transmit resources are unavailable.
3579  */
3580 static inline void netif_stop_queue(struct net_device *dev)
3581 {
3582 	netif_tx_stop_queue(netdev_get_tx_queue(dev, 0));
3583 }
3584 
3585 void netif_tx_stop_all_queues(struct net_device *dev);
3586 
3587 static inline bool netif_tx_queue_stopped(const struct netdev_queue *dev_queue)
3588 {
3589 	return test_bit(__QUEUE_STATE_DRV_XOFF, &dev_queue->state);
3590 }
3591 
3592 /**
3593  *	netif_queue_stopped - test if transmit queue is flowblocked
3594  *	@dev: network device
3595  *
3596  *	Test if transmit queue on device is currently unable to send.
3597  */
3598 static inline bool netif_queue_stopped(const struct net_device *dev)
3599 {
3600 	return netif_tx_queue_stopped(netdev_get_tx_queue(dev, 0));
3601 }
3602 
3603 static inline bool netif_xmit_stopped(const struct netdev_queue *dev_queue)
3604 {
3605 	return dev_queue->state & QUEUE_STATE_ANY_XOFF;
3606 }
3607 
3608 static inline bool
3609 netif_xmit_frozen_or_stopped(const struct netdev_queue *dev_queue)
3610 {
3611 	return dev_queue->state & QUEUE_STATE_ANY_XOFF_OR_FROZEN;
3612 }
3613 
3614 static inline bool
3615 netif_xmit_frozen_or_drv_stopped(const struct netdev_queue *dev_queue)
3616 {
3617 	return dev_queue->state & QUEUE_STATE_DRV_XOFF_OR_FROZEN;
3618 }
3619 
3620 /**
3621  *	netdev_queue_set_dql_min_limit - set dql minimum limit
3622  *	@dev_queue: pointer to transmit queue
3623  *	@min_limit: dql minimum limit
3624  *
3625  * Forces xmit_more() to return true until the minimum threshold
3626  * defined by @min_limit is reached (or until the tx queue is
3627  * empty). Warning: to be use with care, misuse will impact the
3628  * latency.
3629  */
3630 static inline void netdev_queue_set_dql_min_limit(struct netdev_queue *dev_queue,
3631 						  unsigned int min_limit)
3632 {
3633 #ifdef CONFIG_BQL
3634 	dev_queue->dql.min_limit = min_limit;
3635 #endif
3636 }
3637 
3638 static inline int netdev_queue_dql_avail(const struct netdev_queue *txq)
3639 {
3640 #ifdef CONFIG_BQL
3641 	/* Non-BQL migrated drivers will return 0, too. */
3642 	return dql_avail(&txq->dql);
3643 #else
3644 	return 0;
3645 #endif
3646 }
3647 
3648 /**
3649  *	netdev_txq_bql_enqueue_prefetchw - prefetch bql data for write
3650  *	@dev_queue: pointer to transmit queue
3651  *
3652  * BQL enabled drivers might use this helper in their ndo_start_xmit(),
3653  * to give appropriate hint to the CPU.
3654  */
3655 static inline void netdev_txq_bql_enqueue_prefetchw(struct netdev_queue *dev_queue)
3656 {
3657 #ifdef CONFIG_BQL
3658 	prefetchw(&dev_queue->dql.num_queued);
3659 #endif
3660 }
3661 
3662 /**
3663  *	netdev_txq_bql_complete_prefetchw - prefetch bql data for write
3664  *	@dev_queue: pointer to transmit queue
3665  *
3666  * BQL enabled drivers might use this helper in their TX completion path,
3667  * to give appropriate hint to the CPU.
3668  */
3669 static inline void netdev_txq_bql_complete_prefetchw(struct netdev_queue *dev_queue)
3670 {
3671 #ifdef CONFIG_BQL
3672 	prefetchw(&dev_queue->dql.limit);
3673 #endif
3674 }
3675 
3676 /**
3677  *	netdev_tx_sent_queue - report the number of bytes queued to a given tx queue
3678  *	@dev_queue: network device queue
3679  *	@bytes: number of bytes queued to the device queue
3680  *
3681  *	Report the number of bytes queued for sending/completion to the network
3682  *	device hardware queue. @bytes should be a good approximation and should
3683  *	exactly match netdev_completed_queue() @bytes.
3684  *	This is typically called once per packet, from ndo_start_xmit().
3685  */
3686 static inline void netdev_tx_sent_queue(struct netdev_queue *dev_queue,
3687 					unsigned int bytes)
3688 {
3689 #ifdef CONFIG_BQL
3690 	dql_queued(&dev_queue->dql, bytes);
3691 
3692 	if (likely(dql_avail(&dev_queue->dql) >= 0))
3693 		return;
3694 
3695 	/* Paired with READ_ONCE() from dev_watchdog() */
3696 	WRITE_ONCE(dev_queue->trans_start, jiffies);
3697 
3698 	/* This barrier is paired with smp_mb() from dev_watchdog() */
3699 	smp_mb__before_atomic();
3700 
3701 	set_bit(__QUEUE_STATE_STACK_XOFF, &dev_queue->state);
3702 
3703 	/*
3704 	 * The XOFF flag must be set before checking the dql_avail below,
3705 	 * because in netdev_tx_completed_queue we update the dql_completed
3706 	 * before checking the XOFF flag.
3707 	 */
3708 	smp_mb__after_atomic();
3709 
3710 	/* check again in case another CPU has just made room avail */
3711 	if (unlikely(dql_avail(&dev_queue->dql) >= 0))
3712 		clear_bit(__QUEUE_STATE_STACK_XOFF, &dev_queue->state);
3713 #endif
3714 }
3715 
3716 /* Variant of netdev_tx_sent_queue() for drivers that are aware
3717  * that they should not test BQL status themselves.
3718  * We do want to change __QUEUE_STATE_STACK_XOFF only for the last
3719  * skb of a batch.
3720  * Returns true if the doorbell must be used to kick the NIC.
3721  */
3722 static inline bool __netdev_tx_sent_queue(struct netdev_queue *dev_queue,
3723 					  unsigned int bytes,
3724 					  bool xmit_more)
3725 {
3726 	if (xmit_more) {
3727 #ifdef CONFIG_BQL
3728 		dql_queued(&dev_queue->dql, bytes);
3729 #endif
3730 		return netif_tx_queue_stopped(dev_queue);
3731 	}
3732 	netdev_tx_sent_queue(dev_queue, bytes);
3733 	return true;
3734 }
3735 
3736 /**
3737  *	netdev_sent_queue - report the number of bytes queued to hardware
3738  *	@dev: network device
3739  *	@bytes: number of bytes queued to the hardware device queue
3740  *
3741  *	Report the number of bytes queued for sending/completion to the network
3742  *	device hardware queue#0. @bytes should be a good approximation and should
3743  *	exactly match netdev_completed_queue() @bytes.
3744  *	This is typically called once per packet, from ndo_start_xmit().
3745  */
3746 static inline void netdev_sent_queue(struct net_device *dev, unsigned int bytes)
3747 {
3748 	netdev_tx_sent_queue(netdev_get_tx_queue(dev, 0), bytes);
3749 }
3750 
3751 static inline bool __netdev_sent_queue(struct net_device *dev,
3752 				       unsigned int bytes,
3753 				       bool xmit_more)
3754 {
3755 	return __netdev_tx_sent_queue(netdev_get_tx_queue(dev, 0), bytes,
3756 				      xmit_more);
3757 }
3758 
3759 /**
3760  *	netdev_tx_completed_queue - report number of packets/bytes at TX completion.
3761  *	@dev_queue: network device queue
3762  *	@pkts: number of packets (currently ignored)
3763  *	@bytes: number of bytes dequeued from the device queue
3764  *
3765  *	Must be called at most once per TX completion round (and not per
3766  *	individual packet), so that BQL can adjust its limits appropriately.
3767  */
3768 static inline void netdev_tx_completed_queue(struct netdev_queue *dev_queue,
3769 					     unsigned int pkts, unsigned int bytes)
3770 {
3771 #ifdef CONFIG_BQL
3772 	if (unlikely(!bytes))
3773 		return;
3774 
3775 	dql_completed(&dev_queue->dql, bytes);
3776 
3777 	/*
3778 	 * Without the memory barrier there is a small possibility that
3779 	 * netdev_tx_sent_queue will miss the update and cause the queue to
3780 	 * be stopped forever
3781 	 */
3782 	smp_mb(); /* NOTE: netdev_txq_completed_mb() assumes this exists */
3783 
3784 	if (unlikely(dql_avail(&dev_queue->dql) < 0))
3785 		return;
3786 
3787 	if (test_and_clear_bit(__QUEUE_STATE_STACK_XOFF, &dev_queue->state))
3788 		netif_schedule_queue(dev_queue);
3789 #endif
3790 }
3791 
3792 /**
3793  * 	netdev_completed_queue - report bytes and packets completed by device
3794  * 	@dev: network device
3795  * 	@pkts: actual number of packets sent over the medium
3796  * 	@bytes: actual number of bytes sent over the medium
3797  *
3798  * 	Report the number of bytes and packets transmitted by the network device
3799  * 	hardware queue over the physical medium, @bytes must exactly match the
3800  * 	@bytes amount passed to netdev_sent_queue()
3801  */
3802 static inline void netdev_completed_queue(struct net_device *dev,
3803 					  unsigned int pkts, unsigned int bytes)
3804 {
3805 	netdev_tx_completed_queue(netdev_get_tx_queue(dev, 0), pkts, bytes);
3806 }
3807 
3808 static inline void netdev_tx_reset_queue(struct netdev_queue *q)
3809 {
3810 #ifdef CONFIG_BQL
3811 	clear_bit(__QUEUE_STATE_STACK_XOFF, &q->state);
3812 	dql_reset(&q->dql);
3813 #endif
3814 }
3815 
3816 /**
3817  * netdev_tx_reset_subqueue - reset the BQL stats and state of a netdev queue
3818  * @dev: network device
3819  * @qid: stack index of the queue to reset
3820  */
3821 static inline void netdev_tx_reset_subqueue(const struct net_device *dev,
3822 					    u32 qid)
3823 {
3824 	netdev_tx_reset_queue(netdev_get_tx_queue(dev, qid));
3825 }
3826 
3827 /**
3828  * 	netdev_reset_queue - reset the packets and bytes count of a network device
3829  * 	@dev_queue: network device
3830  *
3831  * 	Reset the bytes and packet count of a network device and clear the
3832  * 	software flow control OFF bit for this network device
3833  */
3834 static inline void netdev_reset_queue(struct net_device *dev_queue)
3835 {
3836 	netdev_tx_reset_subqueue(dev_queue, 0);
3837 }
3838 
3839 /**
3840  * 	netdev_cap_txqueue - check if selected tx queue exceeds device queues
3841  * 	@dev: network device
3842  * 	@queue_index: given tx queue index
3843  *
3844  * 	Returns 0 if given tx queue index >= number of device tx queues,
3845  * 	otherwise returns the originally passed tx queue index.
3846  */
3847 static inline u16 netdev_cap_txqueue(struct net_device *dev, u16 queue_index)
3848 {
3849 	if (unlikely(queue_index >= dev->real_num_tx_queues)) {
3850 		net_warn_ratelimited("%s selects TX queue %d, but real number of TX queues is %d\n",
3851 				     dev->name, queue_index,
3852 				     dev->real_num_tx_queues);
3853 		return 0;
3854 	}
3855 
3856 	return queue_index;
3857 }
3858 
3859 /**
3860  *	netif_running - test if up
3861  *	@dev: network device
3862  *
3863  *	Test if the device has been brought up.
3864  */
3865 static inline bool netif_running(const struct net_device *dev)
3866 {
3867 	return test_bit(__LINK_STATE_START, &dev->state);
3868 }
3869 
3870 /*
3871  * Routines to manage the subqueues on a device.  We only need start,
3872  * stop, and a check if it's stopped.  All other device management is
3873  * done at the overall netdevice level.
3874  * Also test the device if we're multiqueue.
3875  */
3876 
3877 /**
3878  *	netif_start_subqueue - allow sending packets on subqueue
3879  *	@dev: network device
3880  *	@queue_index: sub queue index
3881  *
3882  * Start individual transmit queue of a device with multiple transmit queues.
3883  */
3884 static inline void netif_start_subqueue(struct net_device *dev, u16 queue_index)
3885 {
3886 	struct netdev_queue *txq = netdev_get_tx_queue(dev, queue_index);
3887 
3888 	netif_tx_start_queue(txq);
3889 }
3890 
3891 /**
3892  *	netif_stop_subqueue - stop sending packets on subqueue
3893  *	@dev: network device
3894  *	@queue_index: sub queue index
3895  *
3896  * Stop individual transmit queue of a device with multiple transmit queues.
3897  */
3898 static inline void netif_stop_subqueue(struct net_device *dev, u16 queue_index)
3899 {
3900 	struct netdev_queue *txq = netdev_get_tx_queue(dev, queue_index);
3901 	netif_tx_stop_queue(txq);
3902 }
3903 
3904 /**
3905  *	__netif_subqueue_stopped - test status of subqueue
3906  *	@dev: network device
3907  *	@queue_index: sub queue index
3908  *
3909  * Check individual transmit queue of a device with multiple transmit queues.
3910  */
3911 static inline bool __netif_subqueue_stopped(const struct net_device *dev,
3912 					    u16 queue_index)
3913 {
3914 	struct netdev_queue *txq = netdev_get_tx_queue(dev, queue_index);
3915 
3916 	return netif_tx_queue_stopped(txq);
3917 }
3918 
3919 /**
3920  *	netif_subqueue_stopped - test status of subqueue
3921  *	@dev: network device
3922  *	@skb: sub queue buffer pointer
3923  *
3924  * Check individual transmit queue of a device with multiple transmit queues.
3925  */
3926 static inline bool netif_subqueue_stopped(const struct net_device *dev,
3927 					  struct sk_buff *skb)
3928 {
3929 	return __netif_subqueue_stopped(dev, skb_get_queue_mapping(skb));
3930 }
3931 
3932 /**
3933  *	netif_wake_subqueue - allow sending packets on subqueue
3934  *	@dev: network device
3935  *	@queue_index: sub queue index
3936  *
3937  * Resume individual transmit queue of a device with multiple transmit queues.
3938  */
3939 static inline void netif_wake_subqueue(struct net_device *dev, u16 queue_index)
3940 {
3941 	struct netdev_queue *txq = netdev_get_tx_queue(dev, queue_index);
3942 
3943 	netif_tx_wake_queue(txq);
3944 }
3945 
3946 #ifdef CONFIG_XPS
3947 int netif_set_xps_queue(struct net_device *dev, const struct cpumask *mask,
3948 			u16 index);
3949 int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask,
3950 			  u16 index, enum xps_map_type type);
3951 
3952 /**
3953  *	netif_attr_test_mask - Test a CPU or Rx queue set in a mask
3954  *	@j: CPU/Rx queue index
3955  *	@mask: bitmask of all cpus/rx queues
3956  *	@nr_bits: number of bits in the bitmask
3957  *
3958  * Test if a CPU or Rx queue index is set in a mask of all CPU/Rx queues.
3959  */
3960 static inline bool netif_attr_test_mask(unsigned long j,
3961 					const unsigned long *mask,
3962 					unsigned int nr_bits)
3963 {
3964 	cpu_max_bits_warn(j, nr_bits);
3965 	return test_bit(j, mask);
3966 }
3967 
3968 /**
3969  *	netif_attr_test_online - Test for online CPU/Rx queue
3970  *	@j: CPU/Rx queue index
3971  *	@online_mask: bitmask for CPUs/Rx queues that are online
3972  *	@nr_bits: number of bits in the bitmask
3973  *
3974  * Returns: true if a CPU/Rx queue is online.
3975  */
3976 static inline bool netif_attr_test_online(unsigned long j,
3977 					  const unsigned long *online_mask,
3978 					  unsigned int nr_bits)
3979 {
3980 	cpu_max_bits_warn(j, nr_bits);
3981 
3982 	if (online_mask)
3983 		return test_bit(j, online_mask);
3984 
3985 	return (j < nr_bits);
3986 }
3987 
3988 /**
3989  *	netif_attrmask_next - get the next CPU/Rx queue in a cpu/Rx queues mask
3990  *	@n: CPU/Rx queue index
3991  *	@srcp: the cpumask/Rx queue mask pointer
3992  *	@nr_bits: number of bits in the bitmask
3993  *
3994  * Returns: next (after n) CPU/Rx queue index in the mask;
3995  * >= nr_bits if no further CPUs/Rx queues set.
3996  */
3997 static inline unsigned int netif_attrmask_next(int n, const unsigned long *srcp,
3998 					       unsigned int nr_bits)
3999 {
4000 	/* -1 is a legal arg here. */
4001 	if (n != -1)
4002 		cpu_max_bits_warn(n, nr_bits);
4003 
4004 	if (srcp)
4005 		return find_next_bit(srcp, nr_bits, n + 1);
4006 
4007 	return n + 1;
4008 }
4009 
4010 /**
4011  *	netif_attrmask_next_and - get the next CPU/Rx queue in \*src1p & \*src2p
4012  *	@n: CPU/Rx queue index
4013  *	@src1p: the first CPUs/Rx queues mask pointer
4014  *	@src2p: the second CPUs/Rx queues mask pointer
4015  *	@nr_bits: number of bits in the bitmask
4016  *
4017  * Returns: next (after n) CPU/Rx queue index set in both masks;
4018  * >= nr_bits if no further CPUs/Rx queues set in both.
4019  */
4020 static inline int netif_attrmask_next_and(int n, const unsigned long *src1p,
4021 					  const unsigned long *src2p,
4022 					  unsigned int nr_bits)
4023 {
4024 	/* -1 is a legal arg here. */
4025 	if (n != -1)
4026 		cpu_max_bits_warn(n, nr_bits);
4027 
4028 	if (src1p && src2p)
4029 		return find_next_and_bit(src1p, src2p, nr_bits, n + 1);
4030 	else if (src1p)
4031 		return find_next_bit(src1p, nr_bits, n + 1);
4032 	else if (src2p)
4033 		return find_next_bit(src2p, nr_bits, n + 1);
4034 
4035 	return n + 1;
4036 }
4037 #else
4038 static inline int netif_set_xps_queue(struct net_device *dev,
4039 				      const struct cpumask *mask,
4040 				      u16 index)
4041 {
4042 	return 0;
4043 }
4044 
4045 static inline int __netif_set_xps_queue(struct net_device *dev,
4046 					const unsigned long *mask,
4047 					u16 index, enum xps_map_type type)
4048 {
4049 	return 0;
4050 }
4051 #endif
4052 
4053 /**
4054  *	netif_is_multiqueue - test if device has multiple transmit queues
4055  *	@dev: network device
4056  *
4057  * Check if device has multiple transmit queues
4058  */
4059 static inline bool netif_is_multiqueue(const struct net_device *dev)
4060 {
4061 	return dev->num_tx_queues > 1;
4062 }
4063 
4064 int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq);
4065 
4066 #ifdef CONFIG_SYSFS
4067 int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq);
4068 #else
4069 static inline int netif_set_real_num_rx_queues(struct net_device *dev,
4070 						unsigned int rxqs)
4071 {
4072 	dev->real_num_rx_queues = rxqs;
4073 	return 0;
4074 }
4075 #endif
4076 int netif_set_real_num_queues(struct net_device *dev,
4077 			      unsigned int txq, unsigned int rxq);
4078 
4079 int netif_get_num_default_rss_queues(void);
4080 
4081 void dev_kfree_skb_irq_reason(struct sk_buff *skb, enum skb_drop_reason reason);
4082 void dev_kfree_skb_any_reason(struct sk_buff *skb, enum skb_drop_reason reason);
4083 
4084 /*
4085  * It is not allowed to call kfree_skb() or consume_skb() from hardware
4086  * interrupt context or with hardware interrupts being disabled.
4087  * (in_hardirq() || irqs_disabled())
4088  *
4089  * We provide four helpers that can be used in following contexts :
4090  *
4091  * dev_kfree_skb_irq(skb) when caller drops a packet from irq context,
4092  *  replacing kfree_skb(skb)
4093  *
4094  * dev_consume_skb_irq(skb) when caller consumes a packet from irq context.
4095  *  Typically used in place of consume_skb(skb) in TX completion path
4096  *
4097  * dev_kfree_skb_any(skb) when caller doesn't know its current irq context,
4098  *  replacing kfree_skb(skb)
4099  *
4100  * dev_consume_skb_any(skb) when caller doesn't know its current irq context,
4101  *  and consumed a packet. Used in place of consume_skb(skb)
4102  */
4103 static inline void dev_kfree_skb_irq(struct sk_buff *skb)
4104 {
4105 	dev_kfree_skb_irq_reason(skb, SKB_DROP_REASON_NOT_SPECIFIED);
4106 }
4107 
4108 static inline void dev_consume_skb_irq(struct sk_buff *skb)
4109 {
4110 	dev_kfree_skb_irq_reason(skb, SKB_CONSUMED);
4111 }
4112 
4113 static inline void dev_kfree_skb_any(struct sk_buff *skb)
4114 {
4115 	dev_kfree_skb_any_reason(skb, SKB_DROP_REASON_NOT_SPECIFIED);
4116 }
4117 
4118 static inline void dev_consume_skb_any(struct sk_buff *skb)
4119 {
4120 	dev_kfree_skb_any_reason(skb, SKB_CONSUMED);
4121 }
4122 
4123 u32 bpf_prog_run_generic_xdp(struct sk_buff *skb, struct xdp_buff *xdp,
4124 			     const struct bpf_prog *xdp_prog);
4125 void generic_xdp_tx(struct sk_buff *skb, const struct bpf_prog *xdp_prog);
4126 int do_xdp_generic(const struct bpf_prog *xdp_prog, struct sk_buff **pskb);
4127 int netif_rx(struct sk_buff *skb);
4128 int __netif_rx(struct sk_buff *skb);
4129 
4130 int netif_receive_skb(struct sk_buff *skb);
4131 int netif_receive_skb_core(struct sk_buff *skb);
4132 void netif_receive_skb_list_internal(struct list_head *head);
4133 void netif_receive_skb_list(struct list_head *head);
4134 gro_result_t napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb);
4135 void napi_gro_flush(struct napi_struct *napi, bool flush_old);
4136 struct sk_buff *napi_get_frags(struct napi_struct *napi);
4137 gro_result_t napi_gro_frags(struct napi_struct *napi);
4138 
4139 static inline void napi_free_frags(struct napi_struct *napi)
4140 {
4141 	kfree_skb(napi->skb);
4142 	napi->skb = NULL;
4143 }
4144 
4145 bool netdev_is_rx_handler_busy(struct net_device *dev);
4146 int netdev_rx_handler_register(struct net_device *dev,
4147 			       rx_handler_func_t *rx_handler,
4148 			       void *rx_handler_data);
4149 void netdev_rx_handler_unregister(struct net_device *dev);
4150 
4151 bool dev_valid_name(const char *name);
4152 static inline bool is_socket_ioctl_cmd(unsigned int cmd)
4153 {
4154 	return _IOC_TYPE(cmd) == SOCK_IOC_TYPE;
4155 }
4156 int get_user_ifreq(struct ifreq *ifr, void __user **ifrdata, void __user *arg);
4157 int put_user_ifreq(struct ifreq *ifr, void __user *arg);
4158 int dev_ioctl(struct net *net, unsigned int cmd, struct ifreq *ifr,
4159 		void __user *data, bool *need_copyout);
4160 int dev_ifconf(struct net *net, struct ifconf __user *ifc);
4161 int generic_hwtstamp_get_lower(struct net_device *dev,
4162 			       struct kernel_hwtstamp_config *kernel_cfg);
4163 int generic_hwtstamp_set_lower(struct net_device *dev,
4164 			       struct kernel_hwtstamp_config *kernel_cfg,
4165 			       struct netlink_ext_ack *extack);
4166 int dev_ethtool(struct net *net, struct ifreq *ifr, void __user *userdata);
4167 unsigned int dev_get_flags(const struct net_device *);
4168 int __dev_change_flags(struct net_device *dev, unsigned int flags,
4169 		       struct netlink_ext_ack *extack);
4170 int dev_change_flags(struct net_device *dev, unsigned int flags,
4171 		     struct netlink_ext_ack *extack);
4172 int dev_set_alias(struct net_device *, const char *, size_t);
4173 int dev_get_alias(const struct net_device *, char *, size_t);
4174 int __dev_change_net_namespace(struct net_device *dev, struct net *net,
4175 			       const char *pat, int new_ifindex);
4176 static inline
4177 int dev_change_net_namespace(struct net_device *dev, struct net *net,
4178 			     const char *pat)
4179 {
4180 	return __dev_change_net_namespace(dev, net, pat, 0);
4181 }
4182 int __dev_set_mtu(struct net_device *, int);
4183 int dev_set_mtu(struct net_device *, int);
4184 int dev_pre_changeaddr_notify(struct net_device *dev, const char *addr,
4185 			      struct netlink_ext_ack *extack);
4186 int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa,
4187 			struct netlink_ext_ack *extack);
4188 int dev_set_mac_address_user(struct net_device *dev, struct sockaddr *sa,
4189 			     struct netlink_ext_ack *extack);
4190 int dev_get_mac_address(struct sockaddr *sa, struct net *net, char *dev_name);
4191 int dev_get_port_parent_id(struct net_device *dev,
4192 			   struct netdev_phys_item_id *ppid, bool recurse);
4193 bool netdev_port_same_parent_id(struct net_device *a, struct net_device *b);
4194 
4195 struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *dev, bool *again);
4196 struct sk_buff *dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev,
4197 				    struct netdev_queue *txq, int *ret);
4198 
4199 int bpf_xdp_link_attach(const union bpf_attr *attr, struct bpf_prog *prog);
4200 u8 dev_xdp_prog_count(struct net_device *dev);
4201 int dev_xdp_propagate(struct net_device *dev, struct netdev_bpf *bpf);
4202 u8 dev_xdp_sb_prog_count(struct net_device *dev);
4203 u32 dev_xdp_prog_id(struct net_device *dev, enum bpf_xdp_mode mode);
4204 
4205 u32 dev_get_min_mp_channel_count(const struct net_device *dev);
4206 
4207 int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb);
4208 int dev_forward_skb(struct net_device *dev, struct sk_buff *skb);
4209 int dev_forward_skb_nomtu(struct net_device *dev, struct sk_buff *skb);
4210 bool is_skb_forwardable(const struct net_device *dev,
4211 			const struct sk_buff *skb);
4212 
4213 static __always_inline bool __is_skb_forwardable(const struct net_device *dev,
4214 						 const struct sk_buff *skb,
4215 						 const bool check_mtu)
4216 {
4217 	const u32 vlan_hdr_len = 4; /* VLAN_HLEN */
4218 	unsigned int len;
4219 
4220 	if (!(dev->flags & IFF_UP))
4221 		return false;
4222 
4223 	if (!check_mtu)
4224 		return true;
4225 
4226 	len = dev->mtu + dev->hard_header_len + vlan_hdr_len;
4227 	if (skb->len <= len)
4228 		return true;
4229 
4230 	/* if TSO is enabled, we don't care about the length as the packet
4231 	 * could be forwarded without being segmented before
4232 	 */
4233 	if (skb_is_gso(skb))
4234 		return true;
4235 
4236 	return false;
4237 }
4238 
4239 void netdev_core_stats_inc(struct net_device *dev, u32 offset);
4240 
4241 #define DEV_CORE_STATS_INC(FIELD)						\
4242 static inline void dev_core_stats_##FIELD##_inc(struct net_device *dev)		\
4243 {										\
4244 	netdev_core_stats_inc(dev,						\
4245 			offsetof(struct net_device_core_stats, FIELD));		\
4246 }
4247 DEV_CORE_STATS_INC(rx_dropped)
4248 DEV_CORE_STATS_INC(tx_dropped)
4249 DEV_CORE_STATS_INC(rx_nohandler)
4250 DEV_CORE_STATS_INC(rx_otherhost_dropped)
4251 #undef DEV_CORE_STATS_INC
4252 
4253 static __always_inline int ____dev_forward_skb(struct net_device *dev,
4254 					       struct sk_buff *skb,
4255 					       const bool check_mtu)
4256 {
4257 	if (skb_orphan_frags(skb, GFP_ATOMIC) ||
4258 	    unlikely(!__is_skb_forwardable(dev, skb, check_mtu))) {
4259 		dev_core_stats_rx_dropped_inc(dev);
4260 		kfree_skb(skb);
4261 		return NET_RX_DROP;
4262 	}
4263 
4264 	skb_scrub_packet(skb, !net_eq(dev_net(dev), dev_net(skb->dev)));
4265 	skb->priority = 0;
4266 	return 0;
4267 }
4268 
4269 bool dev_nit_active(struct net_device *dev);
4270 void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev);
4271 
4272 static inline void __dev_put(struct net_device *dev)
4273 {
4274 	if (dev) {
4275 #ifdef CONFIG_PCPU_DEV_REFCNT
4276 		this_cpu_dec(*dev->pcpu_refcnt);
4277 #else
4278 		refcount_dec(&dev->dev_refcnt);
4279 #endif
4280 	}
4281 }
4282 
4283 static inline void __dev_hold(struct net_device *dev)
4284 {
4285 	if (dev) {
4286 #ifdef CONFIG_PCPU_DEV_REFCNT
4287 		this_cpu_inc(*dev->pcpu_refcnt);
4288 #else
4289 		refcount_inc(&dev->dev_refcnt);
4290 #endif
4291 	}
4292 }
4293 
4294 static inline void __netdev_tracker_alloc(struct net_device *dev,
4295 					  netdevice_tracker *tracker,
4296 					  gfp_t gfp)
4297 {
4298 #ifdef CONFIG_NET_DEV_REFCNT_TRACKER
4299 	ref_tracker_alloc(&dev->refcnt_tracker, tracker, gfp);
4300 #endif
4301 }
4302 
4303 /* netdev_tracker_alloc() can upgrade a prior untracked reference
4304  * taken by dev_get_by_name()/dev_get_by_index() to a tracked one.
4305  */
4306 static inline void netdev_tracker_alloc(struct net_device *dev,
4307 					netdevice_tracker *tracker, gfp_t gfp)
4308 {
4309 #ifdef CONFIG_NET_DEV_REFCNT_TRACKER
4310 	refcount_dec(&dev->refcnt_tracker.no_tracker);
4311 	__netdev_tracker_alloc(dev, tracker, gfp);
4312 #endif
4313 }
4314 
4315 static inline void netdev_tracker_free(struct net_device *dev,
4316 				       netdevice_tracker *tracker)
4317 {
4318 #ifdef CONFIG_NET_DEV_REFCNT_TRACKER
4319 	ref_tracker_free(&dev->refcnt_tracker, tracker);
4320 #endif
4321 }
4322 
4323 static inline void netdev_hold(struct net_device *dev,
4324 			       netdevice_tracker *tracker, gfp_t gfp)
4325 {
4326 	if (dev) {
4327 		__dev_hold(dev);
4328 		__netdev_tracker_alloc(dev, tracker, gfp);
4329 	}
4330 }
4331 
4332 static inline void netdev_put(struct net_device *dev,
4333 			      netdevice_tracker *tracker)
4334 {
4335 	if (dev) {
4336 		netdev_tracker_free(dev, tracker);
4337 		__dev_put(dev);
4338 	}
4339 }
4340 
4341 /**
4342  *	dev_hold - get reference to device
4343  *	@dev: network device
4344  *
4345  * Hold reference to device to keep it from being freed.
4346  * Try using netdev_hold() instead.
4347  */
4348 static inline void dev_hold(struct net_device *dev)
4349 {
4350 	netdev_hold(dev, NULL, GFP_ATOMIC);
4351 }
4352 
4353 /**
4354  *	dev_put - release reference to device
4355  *	@dev: network device
4356  *
4357  * Release reference to device to allow it to be freed.
4358  * Try using netdev_put() instead.
4359  */
4360 static inline void dev_put(struct net_device *dev)
4361 {
4362 	netdev_put(dev, NULL);
4363 }
4364 
4365 DEFINE_FREE(dev_put, struct net_device *, if (_T) dev_put(_T))
4366 
4367 static inline void netdev_ref_replace(struct net_device *odev,
4368 				      struct net_device *ndev,
4369 				      netdevice_tracker *tracker,
4370 				      gfp_t gfp)
4371 {
4372 	if (odev)
4373 		netdev_tracker_free(odev, tracker);
4374 
4375 	__dev_hold(ndev);
4376 	__dev_put(odev);
4377 
4378 	if (ndev)
4379 		__netdev_tracker_alloc(ndev, tracker, gfp);
4380 }
4381 
4382 /* Carrier loss detection, dial on demand. The functions netif_carrier_on
4383  * and _off may be called from IRQ context, but it is caller
4384  * who is responsible for serialization of these calls.
4385  *
4386  * The name carrier is inappropriate, these functions should really be
4387  * called netif_lowerlayer_*() because they represent the state of any
4388  * kind of lower layer not just hardware media.
4389  */
4390 void linkwatch_fire_event(struct net_device *dev);
4391 
4392 /**
4393  * linkwatch_sync_dev - sync linkwatch for the given device
4394  * @dev: network device to sync linkwatch for
4395  *
4396  * Sync linkwatch for the given device, removing it from the
4397  * pending work list (if queued).
4398  */
4399 void linkwatch_sync_dev(struct net_device *dev);
4400 
4401 /**
4402  *	netif_carrier_ok - test if carrier present
4403  *	@dev: network device
4404  *
4405  * Check if carrier is present on device
4406  */
4407 static inline bool netif_carrier_ok(const struct net_device *dev)
4408 {
4409 	return !test_bit(__LINK_STATE_NOCARRIER, &dev->state);
4410 }
4411 
4412 unsigned long dev_trans_start(struct net_device *dev);
4413 
4414 void netdev_watchdog_up(struct net_device *dev);
4415 
4416 void netif_carrier_on(struct net_device *dev);
4417 void netif_carrier_off(struct net_device *dev);
4418 void netif_carrier_event(struct net_device *dev);
4419 
4420 /**
4421  *	netif_dormant_on - mark device as dormant.
4422  *	@dev: network device
4423  *
4424  * Mark device as dormant (as per RFC2863).
4425  *
4426  * The dormant state indicates that the relevant interface is not
4427  * actually in a condition to pass packets (i.e., it is not 'up') but is
4428  * in a "pending" state, waiting for some external event.  For "on-
4429  * demand" interfaces, this new state identifies the situation where the
4430  * interface is waiting for events to place it in the up state.
4431  */
4432 static inline void netif_dormant_on(struct net_device *dev)
4433 {
4434 	if (!test_and_set_bit(__LINK_STATE_DORMANT, &dev->state))
4435 		linkwatch_fire_event(dev);
4436 }
4437 
4438 /**
4439  *	netif_dormant_off - set device as not dormant.
4440  *	@dev: network device
4441  *
4442  * Device is not in dormant state.
4443  */
4444 static inline void netif_dormant_off(struct net_device *dev)
4445 {
4446 	if (test_and_clear_bit(__LINK_STATE_DORMANT, &dev->state))
4447 		linkwatch_fire_event(dev);
4448 }
4449 
4450 /**
4451  *	netif_dormant - test if device is dormant
4452  *	@dev: network device
4453  *
4454  * Check if device is dormant.
4455  */
4456 static inline bool netif_dormant(const struct net_device *dev)
4457 {
4458 	return test_bit(__LINK_STATE_DORMANT, &dev->state);
4459 }
4460 
4461 
4462 /**
4463  *	netif_testing_on - mark device as under test.
4464  *	@dev: network device
4465  *
4466  * Mark device as under test (as per RFC2863).
4467  *
4468  * The testing state indicates that some test(s) must be performed on
4469  * the interface. After completion, of the test, the interface state
4470  * will change to up, dormant, or down, as appropriate.
4471  */
4472 static inline void netif_testing_on(struct net_device *dev)
4473 {
4474 	if (!test_and_set_bit(__LINK_STATE_TESTING, &dev->state))
4475 		linkwatch_fire_event(dev);
4476 }
4477 
4478 /**
4479  *	netif_testing_off - set device as not under test.
4480  *	@dev: network device
4481  *
4482  * Device is not in testing state.
4483  */
4484 static inline void netif_testing_off(struct net_device *dev)
4485 {
4486 	if (test_and_clear_bit(__LINK_STATE_TESTING, &dev->state))
4487 		linkwatch_fire_event(dev);
4488 }
4489 
4490 /**
4491  *	netif_testing - test if device is under test
4492  *	@dev: network device
4493  *
4494  * Check if device is under test
4495  */
4496 static inline bool netif_testing(const struct net_device *dev)
4497 {
4498 	return test_bit(__LINK_STATE_TESTING, &dev->state);
4499 }
4500 
4501 
4502 /**
4503  *	netif_oper_up - test if device is operational
4504  *	@dev: network device
4505  *
4506  * Check if carrier is operational
4507  */
4508 static inline bool netif_oper_up(const struct net_device *dev)
4509 {
4510 	unsigned int operstate = READ_ONCE(dev->operstate);
4511 
4512 	return	operstate == IF_OPER_UP ||
4513 		operstate == IF_OPER_UNKNOWN /* backward compat */;
4514 }
4515 
4516 /**
4517  *	netif_device_present - is device available or removed
4518  *	@dev: network device
4519  *
4520  * Check if device has not been removed from system.
4521  */
4522 static inline bool netif_device_present(const struct net_device *dev)
4523 {
4524 	return test_bit(__LINK_STATE_PRESENT, &dev->state);
4525 }
4526 
4527 void netif_device_detach(struct net_device *dev);
4528 
4529 void netif_device_attach(struct net_device *dev);
4530 
4531 /*
4532  * Network interface message level settings
4533  */
4534 
4535 enum {
4536 	NETIF_MSG_DRV_BIT,
4537 	NETIF_MSG_PROBE_BIT,
4538 	NETIF_MSG_LINK_BIT,
4539 	NETIF_MSG_TIMER_BIT,
4540 	NETIF_MSG_IFDOWN_BIT,
4541 	NETIF_MSG_IFUP_BIT,
4542 	NETIF_MSG_RX_ERR_BIT,
4543 	NETIF_MSG_TX_ERR_BIT,
4544 	NETIF_MSG_TX_QUEUED_BIT,
4545 	NETIF_MSG_INTR_BIT,
4546 	NETIF_MSG_TX_DONE_BIT,
4547 	NETIF_MSG_RX_STATUS_BIT,
4548 	NETIF_MSG_PKTDATA_BIT,
4549 	NETIF_MSG_HW_BIT,
4550 	NETIF_MSG_WOL_BIT,
4551 
4552 	/* When you add a new bit above, update netif_msg_class_names array
4553 	 * in net/ethtool/common.c
4554 	 */
4555 	NETIF_MSG_CLASS_COUNT,
4556 };
4557 /* Both ethtool_ops interface and internal driver implementation use u32 */
4558 static_assert(NETIF_MSG_CLASS_COUNT <= 32);
4559 
4560 #define __NETIF_MSG_BIT(bit)	((u32)1 << (bit))
4561 #define __NETIF_MSG(name)	__NETIF_MSG_BIT(NETIF_MSG_ ## name ## _BIT)
4562 
4563 #define NETIF_MSG_DRV		__NETIF_MSG(DRV)
4564 #define NETIF_MSG_PROBE		__NETIF_MSG(PROBE)
4565 #define NETIF_MSG_LINK		__NETIF_MSG(LINK)
4566 #define NETIF_MSG_TIMER		__NETIF_MSG(TIMER)
4567 #define NETIF_MSG_IFDOWN	__NETIF_MSG(IFDOWN)
4568 #define NETIF_MSG_IFUP		__NETIF_MSG(IFUP)
4569 #define NETIF_MSG_RX_ERR	__NETIF_MSG(RX_ERR)
4570 #define NETIF_MSG_TX_ERR	__NETIF_MSG(TX_ERR)
4571 #define NETIF_MSG_TX_QUEUED	__NETIF_MSG(TX_QUEUED)
4572 #define NETIF_MSG_INTR		__NETIF_MSG(INTR)
4573 #define NETIF_MSG_TX_DONE	__NETIF_MSG(TX_DONE)
4574 #define NETIF_MSG_RX_STATUS	__NETIF_MSG(RX_STATUS)
4575 #define NETIF_MSG_PKTDATA	__NETIF_MSG(PKTDATA)
4576 #define NETIF_MSG_HW		__NETIF_MSG(HW)
4577 #define NETIF_MSG_WOL		__NETIF_MSG(WOL)
4578 
4579 #define netif_msg_drv(p)	((p)->msg_enable & NETIF_MSG_DRV)
4580 #define netif_msg_probe(p)	((p)->msg_enable & NETIF_MSG_PROBE)
4581 #define netif_msg_link(p)	((p)->msg_enable & NETIF_MSG_LINK)
4582 #define netif_msg_timer(p)	((p)->msg_enable & NETIF_MSG_TIMER)
4583 #define netif_msg_ifdown(p)	((p)->msg_enable & NETIF_MSG_IFDOWN)
4584 #define netif_msg_ifup(p)	((p)->msg_enable & NETIF_MSG_IFUP)
4585 #define netif_msg_rx_err(p)	((p)->msg_enable & NETIF_MSG_RX_ERR)
4586 #define netif_msg_tx_err(p)	((p)->msg_enable & NETIF_MSG_TX_ERR)
4587 #define netif_msg_tx_queued(p)	((p)->msg_enable & NETIF_MSG_TX_QUEUED)
4588 #define netif_msg_intr(p)	((p)->msg_enable & NETIF_MSG_INTR)
4589 #define netif_msg_tx_done(p)	((p)->msg_enable & NETIF_MSG_TX_DONE)
4590 #define netif_msg_rx_status(p)	((p)->msg_enable & NETIF_MSG_RX_STATUS)
4591 #define netif_msg_pktdata(p)	((p)->msg_enable & NETIF_MSG_PKTDATA)
4592 #define netif_msg_hw(p)		((p)->msg_enable & NETIF_MSG_HW)
4593 #define netif_msg_wol(p)	((p)->msg_enable & NETIF_MSG_WOL)
4594 
4595 static inline u32 netif_msg_init(int debug_value, int default_msg_enable_bits)
4596 {
4597 	/* use default */
4598 	if (debug_value < 0 || debug_value >= (sizeof(u32) * 8))
4599 		return default_msg_enable_bits;
4600 	if (debug_value == 0)	/* no output */
4601 		return 0;
4602 	/* set low N bits */
4603 	return (1U << debug_value) - 1;
4604 }
4605 
4606 static inline void __netif_tx_lock(struct netdev_queue *txq, int cpu)
4607 {
4608 	spin_lock(&txq->_xmit_lock);
4609 	/* Pairs with READ_ONCE() in __dev_queue_xmit() */
4610 	WRITE_ONCE(txq->xmit_lock_owner, cpu);
4611 }
4612 
4613 static inline bool __netif_tx_acquire(struct netdev_queue *txq)
4614 {
4615 	__acquire(&txq->_xmit_lock);
4616 	return true;
4617 }
4618 
4619 static inline void __netif_tx_release(struct netdev_queue *txq)
4620 {
4621 	__release(&txq->_xmit_lock);
4622 }
4623 
4624 static inline void __netif_tx_lock_bh(struct netdev_queue *txq)
4625 {
4626 	spin_lock_bh(&txq->_xmit_lock);
4627 	/* Pairs with READ_ONCE() in __dev_queue_xmit() */
4628 	WRITE_ONCE(txq->xmit_lock_owner, smp_processor_id());
4629 }
4630 
4631 static inline bool __netif_tx_trylock(struct netdev_queue *txq)
4632 {
4633 	bool ok = spin_trylock(&txq->_xmit_lock);
4634 
4635 	if (likely(ok)) {
4636 		/* Pairs with READ_ONCE() in __dev_queue_xmit() */
4637 		WRITE_ONCE(txq->xmit_lock_owner, smp_processor_id());
4638 	}
4639 	return ok;
4640 }
4641 
4642 static inline void __netif_tx_unlock(struct netdev_queue *txq)
4643 {
4644 	/* Pairs with READ_ONCE() in __dev_queue_xmit() */
4645 	WRITE_ONCE(txq->xmit_lock_owner, -1);
4646 	spin_unlock(&txq->_xmit_lock);
4647 }
4648 
4649 static inline void __netif_tx_unlock_bh(struct netdev_queue *txq)
4650 {
4651 	/* Pairs with READ_ONCE() in __dev_queue_xmit() */
4652 	WRITE_ONCE(txq->xmit_lock_owner, -1);
4653 	spin_unlock_bh(&txq->_xmit_lock);
4654 }
4655 
4656 /*
4657  * txq->trans_start can be read locklessly from dev_watchdog()
4658  */
4659 static inline void txq_trans_update(struct netdev_queue *txq)
4660 {
4661 	if (txq->xmit_lock_owner != -1)
4662 		WRITE_ONCE(txq->trans_start, jiffies);
4663 }
4664 
4665 static inline void txq_trans_cond_update(struct netdev_queue *txq)
4666 {
4667 	unsigned long now = jiffies;
4668 
4669 	if (READ_ONCE(txq->trans_start) != now)
4670 		WRITE_ONCE(txq->trans_start, now);
4671 }
4672 
4673 /* legacy drivers only, netdev_start_xmit() sets txq->trans_start */
4674 static inline void netif_trans_update(struct net_device *dev)
4675 {
4676 	struct netdev_queue *txq = netdev_get_tx_queue(dev, 0);
4677 
4678 	txq_trans_cond_update(txq);
4679 }
4680 
4681 /**
4682  *	netif_tx_lock - grab network device transmit lock
4683  *	@dev: network device
4684  *
4685  * Get network device transmit lock
4686  */
4687 void netif_tx_lock(struct net_device *dev);
4688 
4689 static inline void netif_tx_lock_bh(struct net_device *dev)
4690 {
4691 	local_bh_disable();
4692 	netif_tx_lock(dev);
4693 }
4694 
4695 void netif_tx_unlock(struct net_device *dev);
4696 
4697 static inline void netif_tx_unlock_bh(struct net_device *dev)
4698 {
4699 	netif_tx_unlock(dev);
4700 	local_bh_enable();
4701 }
4702 
4703 #define HARD_TX_LOCK(dev, txq, cpu) {			\
4704 	if (!(dev)->lltx) {				\
4705 		__netif_tx_lock(txq, cpu);		\
4706 	} else {					\
4707 		__netif_tx_acquire(txq);		\
4708 	}						\
4709 }
4710 
4711 #define HARD_TX_TRYLOCK(dev, txq)			\
4712 	(!(dev)->lltx ?					\
4713 		__netif_tx_trylock(txq) :		\
4714 		__netif_tx_acquire(txq))
4715 
4716 #define HARD_TX_UNLOCK(dev, txq) {			\
4717 	if (!(dev)->lltx) {				\
4718 		__netif_tx_unlock(txq);			\
4719 	} else {					\
4720 		__netif_tx_release(txq);		\
4721 	}						\
4722 }
4723 
4724 static inline void netif_tx_disable(struct net_device *dev)
4725 {
4726 	unsigned int i;
4727 	int cpu;
4728 
4729 	local_bh_disable();
4730 	cpu = smp_processor_id();
4731 	spin_lock(&dev->tx_global_lock);
4732 	for (i = 0; i < dev->num_tx_queues; i++) {
4733 		struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
4734 
4735 		__netif_tx_lock(txq, cpu);
4736 		netif_tx_stop_queue(txq);
4737 		__netif_tx_unlock(txq);
4738 	}
4739 	spin_unlock(&dev->tx_global_lock);
4740 	local_bh_enable();
4741 }
4742 
4743 static inline void netif_addr_lock(struct net_device *dev)
4744 {
4745 	unsigned char nest_level = 0;
4746 
4747 #ifdef CONFIG_LOCKDEP
4748 	nest_level = dev->nested_level;
4749 #endif
4750 	spin_lock_nested(&dev->addr_list_lock, nest_level);
4751 }
4752 
4753 static inline void netif_addr_lock_bh(struct net_device *dev)
4754 {
4755 	unsigned char nest_level = 0;
4756 
4757 #ifdef CONFIG_LOCKDEP
4758 	nest_level = dev->nested_level;
4759 #endif
4760 	local_bh_disable();
4761 	spin_lock_nested(&dev->addr_list_lock, nest_level);
4762 }
4763 
4764 static inline void netif_addr_unlock(struct net_device *dev)
4765 {
4766 	spin_unlock(&dev->addr_list_lock);
4767 }
4768 
4769 static inline void netif_addr_unlock_bh(struct net_device *dev)
4770 {
4771 	spin_unlock_bh(&dev->addr_list_lock);
4772 }
4773 
4774 /*
4775  * dev_addrs walker. Should be used only for read access. Call with
4776  * rcu_read_lock held.
4777  */
4778 #define for_each_dev_addr(dev, ha) \
4779 		list_for_each_entry_rcu(ha, &dev->dev_addrs.list, list)
4780 
4781 /* These functions live elsewhere (drivers/net/net_init.c, but related) */
4782 
4783 void ether_setup(struct net_device *dev);
4784 
4785 /* Allocate dummy net_device */
4786 struct net_device *alloc_netdev_dummy(int sizeof_priv);
4787 
4788 /* Support for loadable net-drivers */
4789 struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
4790 				    unsigned char name_assign_type,
4791 				    void (*setup)(struct net_device *),
4792 				    unsigned int txqs, unsigned int rxqs);
4793 #define alloc_netdev(sizeof_priv, name, name_assign_type, setup) \
4794 	alloc_netdev_mqs(sizeof_priv, name, name_assign_type, setup, 1, 1)
4795 
4796 #define alloc_netdev_mq(sizeof_priv, name, name_assign_type, setup, count) \
4797 	alloc_netdev_mqs(sizeof_priv, name, name_assign_type, setup, count, \
4798 			 count)
4799 
4800 int register_netdev(struct net_device *dev);
4801 void unregister_netdev(struct net_device *dev);
4802 
4803 int devm_register_netdev(struct device *dev, struct net_device *ndev);
4804 
4805 /* General hardware address lists handling functions */
4806 int __hw_addr_sync(struct netdev_hw_addr_list *to_list,
4807 		   struct netdev_hw_addr_list *from_list, int addr_len);
4808 int __hw_addr_sync_multiple(struct netdev_hw_addr_list *to_list,
4809 			    struct netdev_hw_addr_list *from_list,
4810 			    int addr_len);
4811 void __hw_addr_unsync(struct netdev_hw_addr_list *to_list,
4812 		      struct netdev_hw_addr_list *from_list, int addr_len);
4813 int __hw_addr_sync_dev(struct netdev_hw_addr_list *list,
4814 		       struct net_device *dev,
4815 		       int (*sync)(struct net_device *, const unsigned char *),
4816 		       int (*unsync)(struct net_device *,
4817 				     const unsigned char *));
4818 int __hw_addr_ref_sync_dev(struct netdev_hw_addr_list *list,
4819 			   struct net_device *dev,
4820 			   int (*sync)(struct net_device *,
4821 				       const unsigned char *, int),
4822 			   int (*unsync)(struct net_device *,
4823 					 const unsigned char *, int));
4824 void __hw_addr_ref_unsync_dev(struct netdev_hw_addr_list *list,
4825 			      struct net_device *dev,
4826 			      int (*unsync)(struct net_device *,
4827 					    const unsigned char *, int));
4828 void __hw_addr_unsync_dev(struct netdev_hw_addr_list *list,
4829 			  struct net_device *dev,
4830 			  int (*unsync)(struct net_device *,
4831 					const unsigned char *));
4832 void __hw_addr_init(struct netdev_hw_addr_list *list);
4833 
4834 /* Functions used for device addresses handling */
4835 void dev_addr_mod(struct net_device *dev, unsigned int offset,
4836 		  const void *addr, size_t len);
4837 
4838 static inline void
4839 __dev_addr_set(struct net_device *dev, const void *addr, size_t len)
4840 {
4841 	dev_addr_mod(dev, 0, addr, len);
4842 }
4843 
4844 static inline void dev_addr_set(struct net_device *dev, const u8 *addr)
4845 {
4846 	__dev_addr_set(dev, addr, dev->addr_len);
4847 }
4848 
4849 int dev_addr_add(struct net_device *dev, const unsigned char *addr,
4850 		 unsigned char addr_type);
4851 int dev_addr_del(struct net_device *dev, const unsigned char *addr,
4852 		 unsigned char addr_type);
4853 
4854 /* Functions used for unicast addresses handling */
4855 int dev_uc_add(struct net_device *dev, const unsigned char *addr);
4856 int dev_uc_add_excl(struct net_device *dev, const unsigned char *addr);
4857 int dev_uc_del(struct net_device *dev, const unsigned char *addr);
4858 int dev_uc_sync(struct net_device *to, struct net_device *from);
4859 int dev_uc_sync_multiple(struct net_device *to, struct net_device *from);
4860 void dev_uc_unsync(struct net_device *to, struct net_device *from);
4861 void dev_uc_flush(struct net_device *dev);
4862 void dev_uc_init(struct net_device *dev);
4863 
4864 /**
4865  *  __dev_uc_sync - Synchronize device's unicast list
4866  *  @dev:  device to sync
4867  *  @sync: function to call if address should be added
4868  *  @unsync: function to call if address should be removed
4869  *
4870  *  Add newly added addresses to the interface, and release
4871  *  addresses that have been deleted.
4872  */
4873 static inline int __dev_uc_sync(struct net_device *dev,
4874 				int (*sync)(struct net_device *,
4875 					    const unsigned char *),
4876 				int (*unsync)(struct net_device *,
4877 					      const unsigned char *))
4878 {
4879 	return __hw_addr_sync_dev(&dev->uc, dev, sync, unsync);
4880 }
4881 
4882 /**
4883  *  __dev_uc_unsync - Remove synchronized addresses from device
4884  *  @dev:  device to sync
4885  *  @unsync: function to call if address should be removed
4886  *
4887  *  Remove all addresses that were added to the device by dev_uc_sync().
4888  */
4889 static inline void __dev_uc_unsync(struct net_device *dev,
4890 				   int (*unsync)(struct net_device *,
4891 						 const unsigned char *))
4892 {
4893 	__hw_addr_unsync_dev(&dev->uc, dev, unsync);
4894 }
4895 
4896 /* Functions used for multicast addresses handling */
4897 int dev_mc_add(struct net_device *dev, const unsigned char *addr);
4898 int dev_mc_add_global(struct net_device *dev, const unsigned char *addr);
4899 int dev_mc_add_excl(struct net_device *dev, const unsigned char *addr);
4900 int dev_mc_del(struct net_device *dev, const unsigned char *addr);
4901 int dev_mc_del_global(struct net_device *dev, const unsigned char *addr);
4902 int dev_mc_sync(struct net_device *to, struct net_device *from);
4903 int dev_mc_sync_multiple(struct net_device *to, struct net_device *from);
4904 void dev_mc_unsync(struct net_device *to, struct net_device *from);
4905 void dev_mc_flush(struct net_device *dev);
4906 void dev_mc_init(struct net_device *dev);
4907 
4908 /**
4909  *  __dev_mc_sync - Synchronize device's multicast list
4910  *  @dev:  device to sync
4911  *  @sync: function to call if address should be added
4912  *  @unsync: function to call if address should be removed
4913  *
4914  *  Add newly added addresses to the interface, and release
4915  *  addresses that have been deleted.
4916  */
4917 static inline int __dev_mc_sync(struct net_device *dev,
4918 				int (*sync)(struct net_device *,
4919 					    const unsigned char *),
4920 				int (*unsync)(struct net_device *,
4921 					      const unsigned char *))
4922 {
4923 	return __hw_addr_sync_dev(&dev->mc, dev, sync, unsync);
4924 }
4925 
4926 /**
4927  *  __dev_mc_unsync - Remove synchronized addresses from device
4928  *  @dev:  device to sync
4929  *  @unsync: function to call if address should be removed
4930  *
4931  *  Remove all addresses that were added to the device by dev_mc_sync().
4932  */
4933 static inline void __dev_mc_unsync(struct net_device *dev,
4934 				   int (*unsync)(struct net_device *,
4935 						 const unsigned char *))
4936 {
4937 	__hw_addr_unsync_dev(&dev->mc, dev, unsync);
4938 }
4939 
4940 /* Functions used for secondary unicast and multicast support */
4941 void dev_set_rx_mode(struct net_device *dev);
4942 int dev_set_promiscuity(struct net_device *dev, int inc);
4943 int dev_set_allmulti(struct net_device *dev, int inc);
4944 void netdev_state_change(struct net_device *dev);
4945 void __netdev_notify_peers(struct net_device *dev);
4946 void netdev_notify_peers(struct net_device *dev);
4947 void netdev_features_change(struct net_device *dev);
4948 /* Load a device via the kmod */
4949 void dev_load(struct net *net, const char *name);
4950 struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev,
4951 					struct rtnl_link_stats64 *storage);
4952 void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
4953 			     const struct net_device_stats *netdev_stats);
4954 void dev_fetch_sw_netstats(struct rtnl_link_stats64 *s,
4955 			   const struct pcpu_sw_netstats __percpu *netstats);
4956 void dev_get_tstats64(struct net_device *dev, struct rtnl_link_stats64 *s);
4957 
4958 enum {
4959 	NESTED_SYNC_IMM_BIT,
4960 	NESTED_SYNC_TODO_BIT,
4961 };
4962 
4963 #define __NESTED_SYNC_BIT(bit)	((u32)1 << (bit))
4964 #define __NESTED_SYNC(name)	__NESTED_SYNC_BIT(NESTED_SYNC_ ## name ## _BIT)
4965 
4966 #define NESTED_SYNC_IMM		__NESTED_SYNC(IMM)
4967 #define NESTED_SYNC_TODO	__NESTED_SYNC(TODO)
4968 
4969 struct netdev_nested_priv {
4970 	unsigned char flags;
4971 	void *data;
4972 };
4973 
4974 bool netdev_has_upper_dev(struct net_device *dev, struct net_device *upper_dev);
4975 struct net_device *netdev_upper_get_next_dev_rcu(struct net_device *dev,
4976 						     struct list_head **iter);
4977 
4978 /* iterate through upper list, must be called under RCU read lock */
4979 #define netdev_for_each_upper_dev_rcu(dev, updev, iter) \
4980 	for (iter = &(dev)->adj_list.upper, \
4981 	     updev = netdev_upper_get_next_dev_rcu(dev, &(iter)); \
4982 	     updev; \
4983 	     updev = netdev_upper_get_next_dev_rcu(dev, &(iter)))
4984 
4985 int netdev_walk_all_upper_dev_rcu(struct net_device *dev,
4986 				  int (*fn)(struct net_device *upper_dev,
4987 					    struct netdev_nested_priv *priv),
4988 				  struct netdev_nested_priv *priv);
4989 
4990 bool netdev_has_upper_dev_all_rcu(struct net_device *dev,
4991 				  struct net_device *upper_dev);
4992 
4993 bool netdev_has_any_upper_dev(struct net_device *dev);
4994 
4995 void *netdev_lower_get_next_private(struct net_device *dev,
4996 				    struct list_head **iter);
4997 void *netdev_lower_get_next_private_rcu(struct net_device *dev,
4998 					struct list_head **iter);
4999 
5000 #define netdev_for_each_lower_private(dev, priv, iter) \
5001 	for (iter = (dev)->adj_list.lower.next, \
5002 	     priv = netdev_lower_get_next_private(dev, &(iter)); \
5003 	     priv; \
5004 	     priv = netdev_lower_get_next_private(dev, &(iter)))
5005 
5006 #define netdev_for_each_lower_private_rcu(dev, priv, iter) \
5007 	for (iter = &(dev)->adj_list.lower, \
5008 	     priv = netdev_lower_get_next_private_rcu(dev, &(iter)); \
5009 	     priv; \
5010 	     priv = netdev_lower_get_next_private_rcu(dev, &(iter)))
5011 
5012 void *netdev_lower_get_next(struct net_device *dev,
5013 				struct list_head **iter);
5014 
5015 #define netdev_for_each_lower_dev(dev, ldev, iter) \
5016 	for (iter = (dev)->adj_list.lower.next, \
5017 	     ldev = netdev_lower_get_next(dev, &(iter)); \
5018 	     ldev; \
5019 	     ldev = netdev_lower_get_next(dev, &(iter)))
5020 
5021 struct net_device *netdev_next_lower_dev_rcu(struct net_device *dev,
5022 					     struct list_head **iter);
5023 int netdev_walk_all_lower_dev(struct net_device *dev,
5024 			      int (*fn)(struct net_device *lower_dev,
5025 					struct netdev_nested_priv *priv),
5026 			      struct netdev_nested_priv *priv);
5027 int netdev_walk_all_lower_dev_rcu(struct net_device *dev,
5028 				  int (*fn)(struct net_device *lower_dev,
5029 					    struct netdev_nested_priv *priv),
5030 				  struct netdev_nested_priv *priv);
5031 
5032 void *netdev_adjacent_get_private(struct list_head *adj_list);
5033 void *netdev_lower_get_first_private_rcu(struct net_device *dev);
5034 struct net_device *netdev_master_upper_dev_get(struct net_device *dev);
5035 struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev);
5036 int netdev_upper_dev_link(struct net_device *dev, struct net_device *upper_dev,
5037 			  struct netlink_ext_ack *extack);
5038 int netdev_master_upper_dev_link(struct net_device *dev,
5039 				 struct net_device *upper_dev,
5040 				 void *upper_priv, void *upper_info,
5041 				 struct netlink_ext_ack *extack);
5042 void netdev_upper_dev_unlink(struct net_device *dev,
5043 			     struct net_device *upper_dev);
5044 int netdev_adjacent_change_prepare(struct net_device *old_dev,
5045 				   struct net_device *new_dev,
5046 				   struct net_device *dev,
5047 				   struct netlink_ext_ack *extack);
5048 void netdev_adjacent_change_commit(struct net_device *old_dev,
5049 				   struct net_device *new_dev,
5050 				   struct net_device *dev);
5051 void netdev_adjacent_change_abort(struct net_device *old_dev,
5052 				  struct net_device *new_dev,
5053 				  struct net_device *dev);
5054 void netdev_adjacent_rename_links(struct net_device *dev, char *oldname);
5055 void *netdev_lower_dev_get_private(struct net_device *dev,
5056 				   struct net_device *lower_dev);
5057 void netdev_lower_state_changed(struct net_device *lower_dev,
5058 				void *lower_state_info);
5059 
5060 /* RSS keys are 40 or 52 bytes long */
5061 #define NETDEV_RSS_KEY_LEN 52
5062 extern u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
5063 void netdev_rss_key_fill(void *buffer, size_t len);
5064 
5065 int skb_checksum_help(struct sk_buff *skb);
5066 int skb_crc32c_csum_help(struct sk_buff *skb);
5067 int skb_csum_hwoffload_help(struct sk_buff *skb,
5068 			    const netdev_features_t features);
5069 
5070 struct netdev_bonding_info {
5071 	ifslave	slave;
5072 	ifbond	master;
5073 };
5074 
5075 struct netdev_notifier_bonding_info {
5076 	struct netdev_notifier_info info; /* must be first */
5077 	struct netdev_bonding_info  bonding_info;
5078 };
5079 
5080 void netdev_bonding_info_change(struct net_device *dev,
5081 				struct netdev_bonding_info *bonding_info);
5082 
5083 #if IS_ENABLED(CONFIG_ETHTOOL_NETLINK)
5084 void ethtool_notify(struct net_device *dev, unsigned int cmd, const void *data);
5085 #else
5086 static inline void ethtool_notify(struct net_device *dev, unsigned int cmd,
5087 				  const void *data)
5088 {
5089 }
5090 #endif
5091 
5092 __be16 skb_network_protocol(struct sk_buff *skb, int *depth);
5093 
5094 static inline bool can_checksum_protocol(netdev_features_t features,
5095 					 __be16 protocol)
5096 {
5097 	if (protocol == htons(ETH_P_FCOE))
5098 		return !!(features & NETIF_F_FCOE_CRC);
5099 
5100 	/* Assume this is an IP checksum (not SCTP CRC) */
5101 
5102 	if (features & NETIF_F_HW_CSUM) {
5103 		/* Can checksum everything */
5104 		return true;
5105 	}
5106 
5107 	switch (protocol) {
5108 	case htons(ETH_P_IP):
5109 		return !!(features & NETIF_F_IP_CSUM);
5110 	case htons(ETH_P_IPV6):
5111 		return !!(features & NETIF_F_IPV6_CSUM);
5112 	default:
5113 		return false;
5114 	}
5115 }
5116 
5117 #ifdef CONFIG_BUG
5118 void netdev_rx_csum_fault(struct net_device *dev, struct sk_buff *skb);
5119 #else
5120 static inline void netdev_rx_csum_fault(struct net_device *dev,
5121 					struct sk_buff *skb)
5122 {
5123 }
5124 #endif
5125 /* rx skb timestamps */
5126 void net_enable_timestamp(void);
5127 void net_disable_timestamp(void);
5128 
5129 static inline ktime_t netdev_get_tstamp(struct net_device *dev,
5130 					const struct skb_shared_hwtstamps *hwtstamps,
5131 					bool cycles)
5132 {
5133 	const struct net_device_ops *ops = dev->netdev_ops;
5134 
5135 	if (ops->ndo_get_tstamp)
5136 		return ops->ndo_get_tstamp(dev, hwtstamps, cycles);
5137 
5138 	return hwtstamps->hwtstamp;
5139 }
5140 
5141 #ifndef CONFIG_PREEMPT_RT
5142 static inline void netdev_xmit_set_more(bool more)
5143 {
5144 	__this_cpu_write(softnet_data.xmit.more, more);
5145 }
5146 
5147 static inline bool netdev_xmit_more(void)
5148 {
5149 	return __this_cpu_read(softnet_data.xmit.more);
5150 }
5151 #else
5152 static inline void netdev_xmit_set_more(bool more)
5153 {
5154 	current->net_xmit.more = more;
5155 }
5156 
5157 static inline bool netdev_xmit_more(void)
5158 {
5159 	return current->net_xmit.more;
5160 }
5161 #endif
5162 
5163 static inline netdev_tx_t __netdev_start_xmit(const struct net_device_ops *ops,
5164 					      struct sk_buff *skb, struct net_device *dev,
5165 					      bool more)
5166 {
5167 	netdev_xmit_set_more(more);
5168 	return ops->ndo_start_xmit(skb, dev);
5169 }
5170 
5171 static inline netdev_tx_t netdev_start_xmit(struct sk_buff *skb, struct net_device *dev,
5172 					    struct netdev_queue *txq, bool more)
5173 {
5174 	const struct net_device_ops *ops = dev->netdev_ops;
5175 	netdev_tx_t rc;
5176 
5177 	rc = __netdev_start_xmit(ops, skb, dev, more);
5178 	if (rc == NETDEV_TX_OK)
5179 		txq_trans_update(txq);
5180 
5181 	return rc;
5182 }
5183 
5184 int netdev_class_create_file_ns(const struct class_attribute *class_attr,
5185 				const void *ns);
5186 void netdev_class_remove_file_ns(const struct class_attribute *class_attr,
5187 				 const void *ns);
5188 
5189 extern const struct kobj_ns_type_operations net_ns_type_operations;
5190 
5191 const char *netdev_drivername(const struct net_device *dev);
5192 
5193 static inline netdev_features_t netdev_intersect_features(netdev_features_t f1,
5194 							  netdev_features_t f2)
5195 {
5196 	if ((f1 ^ f2) & NETIF_F_HW_CSUM) {
5197 		if (f1 & NETIF_F_HW_CSUM)
5198 			f1 |= (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
5199 		else
5200 			f2 |= (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
5201 	}
5202 
5203 	return f1 & f2;
5204 }
5205 
5206 static inline netdev_features_t netdev_get_wanted_features(
5207 	struct net_device *dev)
5208 {
5209 	return (dev->features & ~dev->hw_features) | dev->wanted_features;
5210 }
5211 netdev_features_t netdev_increment_features(netdev_features_t all,
5212 	netdev_features_t one, netdev_features_t mask);
5213 
5214 /* Allow TSO being used on stacked device :
5215  * Performing the GSO segmentation before last device
5216  * is a performance improvement.
5217  */
5218 static inline netdev_features_t netdev_add_tso_features(netdev_features_t features,
5219 							netdev_features_t mask)
5220 {
5221 	return netdev_increment_features(features, NETIF_F_ALL_TSO, mask);
5222 }
5223 
5224 int __netdev_update_features(struct net_device *dev);
5225 void netdev_update_features(struct net_device *dev);
5226 void netdev_change_features(struct net_device *dev);
5227 
5228 void netif_stacked_transfer_operstate(const struct net_device *rootdev,
5229 					struct net_device *dev);
5230 
5231 netdev_features_t passthru_features_check(struct sk_buff *skb,
5232 					  struct net_device *dev,
5233 					  netdev_features_t features);
5234 netdev_features_t netif_skb_features(struct sk_buff *skb);
5235 void skb_warn_bad_offload(const struct sk_buff *skb);
5236 
5237 static inline bool net_gso_ok(netdev_features_t features, int gso_type)
5238 {
5239 	netdev_features_t feature = (netdev_features_t)gso_type << NETIF_F_GSO_SHIFT;
5240 
5241 	/* check flags correspondence */
5242 	BUILD_BUG_ON(SKB_GSO_TCPV4   != (NETIF_F_TSO >> NETIF_F_GSO_SHIFT));
5243 	BUILD_BUG_ON(SKB_GSO_DODGY   != (NETIF_F_GSO_ROBUST >> NETIF_F_GSO_SHIFT));
5244 	BUILD_BUG_ON(SKB_GSO_TCP_ECN != (NETIF_F_TSO_ECN >> NETIF_F_GSO_SHIFT));
5245 	BUILD_BUG_ON(SKB_GSO_TCP_FIXEDID != (NETIF_F_TSO_MANGLEID >> NETIF_F_GSO_SHIFT));
5246 	BUILD_BUG_ON(SKB_GSO_TCPV6   != (NETIF_F_TSO6 >> NETIF_F_GSO_SHIFT));
5247 	BUILD_BUG_ON(SKB_GSO_FCOE    != (NETIF_F_FSO >> NETIF_F_GSO_SHIFT));
5248 	BUILD_BUG_ON(SKB_GSO_GRE     != (NETIF_F_GSO_GRE >> NETIF_F_GSO_SHIFT));
5249 	BUILD_BUG_ON(SKB_GSO_GRE_CSUM != (NETIF_F_GSO_GRE_CSUM >> NETIF_F_GSO_SHIFT));
5250 	BUILD_BUG_ON(SKB_GSO_IPXIP4  != (NETIF_F_GSO_IPXIP4 >> NETIF_F_GSO_SHIFT));
5251 	BUILD_BUG_ON(SKB_GSO_IPXIP6  != (NETIF_F_GSO_IPXIP6 >> NETIF_F_GSO_SHIFT));
5252 	BUILD_BUG_ON(SKB_GSO_UDP_TUNNEL != (NETIF_F_GSO_UDP_TUNNEL >> NETIF_F_GSO_SHIFT));
5253 	BUILD_BUG_ON(SKB_GSO_UDP_TUNNEL_CSUM != (NETIF_F_GSO_UDP_TUNNEL_CSUM >> NETIF_F_GSO_SHIFT));
5254 	BUILD_BUG_ON(SKB_GSO_PARTIAL != (NETIF_F_GSO_PARTIAL >> NETIF_F_GSO_SHIFT));
5255 	BUILD_BUG_ON(SKB_GSO_TUNNEL_REMCSUM != (NETIF_F_GSO_TUNNEL_REMCSUM >> NETIF_F_GSO_SHIFT));
5256 	BUILD_BUG_ON(SKB_GSO_SCTP    != (NETIF_F_GSO_SCTP >> NETIF_F_GSO_SHIFT));
5257 	BUILD_BUG_ON(SKB_GSO_ESP != (NETIF_F_GSO_ESP >> NETIF_F_GSO_SHIFT));
5258 	BUILD_BUG_ON(SKB_GSO_UDP != (NETIF_F_GSO_UDP >> NETIF_F_GSO_SHIFT));
5259 	BUILD_BUG_ON(SKB_GSO_UDP_L4 != (NETIF_F_GSO_UDP_L4 >> NETIF_F_GSO_SHIFT));
5260 	BUILD_BUG_ON(SKB_GSO_FRAGLIST != (NETIF_F_GSO_FRAGLIST >> NETIF_F_GSO_SHIFT));
5261 
5262 	return (features & feature) == feature;
5263 }
5264 
5265 static inline bool skb_gso_ok(struct sk_buff *skb, netdev_features_t features)
5266 {
5267 	return net_gso_ok(features, skb_shinfo(skb)->gso_type) &&
5268 	       (!skb_has_frag_list(skb) || (features & NETIF_F_FRAGLIST));
5269 }
5270 
5271 static inline bool netif_needs_gso(struct sk_buff *skb,
5272 				   netdev_features_t features)
5273 {
5274 	return skb_is_gso(skb) && (!skb_gso_ok(skb, features) ||
5275 		unlikely((skb->ip_summed != CHECKSUM_PARTIAL) &&
5276 			 (skb->ip_summed != CHECKSUM_UNNECESSARY)));
5277 }
5278 
5279 void netif_set_tso_max_size(struct net_device *dev, unsigned int size);
5280 void netif_set_tso_max_segs(struct net_device *dev, unsigned int segs);
5281 void netif_inherit_tso_max(struct net_device *to,
5282 			   const struct net_device *from);
5283 
5284 static inline unsigned int
5285 netif_get_gro_max_size(const struct net_device *dev, const struct sk_buff *skb)
5286 {
5287 	/* pairs with WRITE_ONCE() in netif_set_gro(_ipv4)_max_size() */
5288 	return skb->protocol == htons(ETH_P_IPV6) ?
5289 	       READ_ONCE(dev->gro_max_size) :
5290 	       READ_ONCE(dev->gro_ipv4_max_size);
5291 }
5292 
5293 static inline unsigned int
5294 netif_get_gso_max_size(const struct net_device *dev, const struct sk_buff *skb)
5295 {
5296 	/* pairs with WRITE_ONCE() in netif_set_gso(_ipv4)_max_size() */
5297 	return skb->protocol == htons(ETH_P_IPV6) ?
5298 	       READ_ONCE(dev->gso_max_size) :
5299 	       READ_ONCE(dev->gso_ipv4_max_size);
5300 }
5301 
5302 static inline bool netif_is_macsec(const struct net_device *dev)
5303 {
5304 	return dev->priv_flags & IFF_MACSEC;
5305 }
5306 
5307 static inline bool netif_is_macvlan(const struct net_device *dev)
5308 {
5309 	return dev->priv_flags & IFF_MACVLAN;
5310 }
5311 
5312 static inline bool netif_is_macvlan_port(const struct net_device *dev)
5313 {
5314 	return dev->priv_flags & IFF_MACVLAN_PORT;
5315 }
5316 
5317 static inline bool netif_is_bond_master(const struct net_device *dev)
5318 {
5319 	return dev->flags & IFF_MASTER && dev->priv_flags & IFF_BONDING;
5320 }
5321 
5322 static inline bool netif_is_bond_slave(const struct net_device *dev)
5323 {
5324 	return dev->flags & IFF_SLAVE && dev->priv_flags & IFF_BONDING;
5325 }
5326 
5327 static inline bool netif_supports_nofcs(struct net_device *dev)
5328 {
5329 	return dev->priv_flags & IFF_SUPP_NOFCS;
5330 }
5331 
5332 static inline bool netif_has_l3_rx_handler(const struct net_device *dev)
5333 {
5334 	return dev->priv_flags & IFF_L3MDEV_RX_HANDLER;
5335 }
5336 
5337 static inline bool netif_is_l3_master(const struct net_device *dev)
5338 {
5339 	return dev->priv_flags & IFF_L3MDEV_MASTER;
5340 }
5341 
5342 static inline bool netif_is_l3_slave(const struct net_device *dev)
5343 {
5344 	return dev->priv_flags & IFF_L3MDEV_SLAVE;
5345 }
5346 
5347 static inline int dev_sdif(const struct net_device *dev)
5348 {
5349 #ifdef CONFIG_NET_L3_MASTER_DEV
5350 	if (netif_is_l3_slave(dev))
5351 		return dev->ifindex;
5352 #endif
5353 	return 0;
5354 }
5355 
5356 static inline bool netif_is_bridge_master(const struct net_device *dev)
5357 {
5358 	return dev->priv_flags & IFF_EBRIDGE;
5359 }
5360 
5361 static inline bool netif_is_bridge_port(const struct net_device *dev)
5362 {
5363 	return dev->priv_flags & IFF_BRIDGE_PORT;
5364 }
5365 
5366 static inline bool netif_is_ovs_master(const struct net_device *dev)
5367 {
5368 	return dev->priv_flags & IFF_OPENVSWITCH;
5369 }
5370 
5371 static inline bool netif_is_ovs_port(const struct net_device *dev)
5372 {
5373 	return dev->priv_flags & IFF_OVS_DATAPATH;
5374 }
5375 
5376 static inline bool netif_is_any_bridge_master(const struct net_device *dev)
5377 {
5378 	return netif_is_bridge_master(dev) || netif_is_ovs_master(dev);
5379 }
5380 
5381 static inline bool netif_is_any_bridge_port(const struct net_device *dev)
5382 {
5383 	return netif_is_bridge_port(dev) || netif_is_ovs_port(dev);
5384 }
5385 
5386 static inline bool netif_is_team_master(const struct net_device *dev)
5387 {
5388 	return dev->priv_flags & IFF_TEAM;
5389 }
5390 
5391 static inline bool netif_is_team_port(const struct net_device *dev)
5392 {
5393 	return dev->priv_flags & IFF_TEAM_PORT;
5394 }
5395 
5396 static inline bool netif_is_lag_master(const struct net_device *dev)
5397 {
5398 	return netif_is_bond_master(dev) || netif_is_team_master(dev);
5399 }
5400 
5401 static inline bool netif_is_lag_port(const struct net_device *dev)
5402 {
5403 	return netif_is_bond_slave(dev) || netif_is_team_port(dev);
5404 }
5405 
5406 static inline bool netif_is_rxfh_configured(const struct net_device *dev)
5407 {
5408 	return dev->priv_flags & IFF_RXFH_CONFIGURED;
5409 }
5410 
5411 static inline bool netif_is_failover(const struct net_device *dev)
5412 {
5413 	return dev->priv_flags & IFF_FAILOVER;
5414 }
5415 
5416 static inline bool netif_is_failover_slave(const struct net_device *dev)
5417 {
5418 	return dev->priv_flags & IFF_FAILOVER_SLAVE;
5419 }
5420 
5421 /* This device needs to keep skb dst for qdisc enqueue or ndo_start_xmit() */
5422 static inline void netif_keep_dst(struct net_device *dev)
5423 {
5424 	dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM);
5425 }
5426 
5427 /* return true if dev can't cope with mtu frames that need vlan tag insertion */
5428 static inline bool netif_reduces_vlan_mtu(struct net_device *dev)
5429 {
5430 	/* TODO: reserve and use an additional IFF bit, if we get more users */
5431 	return netif_is_macsec(dev);
5432 }
5433 
5434 extern struct pernet_operations __net_initdata loopback_net_ops;
5435 
5436 /* Logging, debugging and troubleshooting/diagnostic helpers. */
5437 
5438 /* netdev_printk helpers, similar to dev_printk */
5439 
5440 static inline const char *netdev_name(const struct net_device *dev)
5441 {
5442 	if (!dev->name[0] || strchr(dev->name, '%'))
5443 		return "(unnamed net_device)";
5444 	return dev->name;
5445 }
5446 
5447 static inline const char *netdev_reg_state(const struct net_device *dev)
5448 {
5449 	u8 reg_state = READ_ONCE(dev->reg_state);
5450 
5451 	switch (reg_state) {
5452 	case NETREG_UNINITIALIZED: return " (uninitialized)";
5453 	case NETREG_REGISTERED: return "";
5454 	case NETREG_UNREGISTERING: return " (unregistering)";
5455 	case NETREG_UNREGISTERED: return " (unregistered)";
5456 	case NETREG_RELEASED: return " (released)";
5457 	case NETREG_DUMMY: return " (dummy)";
5458 	}
5459 
5460 	WARN_ONCE(1, "%s: unknown reg_state %d\n", dev->name, reg_state);
5461 	return " (unknown)";
5462 }
5463 
5464 #define MODULE_ALIAS_NETDEV(device) \
5465 	MODULE_ALIAS("netdev-" device)
5466 
5467 /*
5468  * netdev_WARN() acts like dev_printk(), but with the key difference
5469  * of using a WARN/WARN_ON to get the message out, including the
5470  * file/line information and a backtrace.
5471  */
5472 #define netdev_WARN(dev, format, args...)			\
5473 	WARN(1, "netdevice: %s%s: " format, netdev_name(dev),	\
5474 	     netdev_reg_state(dev), ##args)
5475 
5476 #define netdev_WARN_ONCE(dev, format, args...)				\
5477 	WARN_ONCE(1, "netdevice: %s%s: " format, netdev_name(dev),	\
5478 		  netdev_reg_state(dev), ##args)
5479 
5480 /*
5481  *	The list of packet types we will receive (as opposed to discard)
5482  *	and the routines to invoke.
5483  *
5484  *	Why 16. Because with 16 the only overlap we get on a hash of the
5485  *	low nibble of the protocol value is RARP/SNAP/X.25.
5486  *
5487  *		0800	IP
5488  *		0001	802.3
5489  *		0002	AX.25
5490  *		0004	802.2
5491  *		8035	RARP
5492  *		0005	SNAP
5493  *		0805	X.25
5494  *		0806	ARP
5495  *		8137	IPX
5496  *		0009	Localtalk
5497  *		86DD	IPv6
5498  */
5499 #define PTYPE_HASH_SIZE	(16)
5500 #define PTYPE_HASH_MASK	(PTYPE_HASH_SIZE - 1)
5501 
5502 extern struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
5503 
5504 extern struct net_device *blackhole_netdev;
5505 
5506 /* Note: Avoid these macros in fast path, prefer per-cpu or per-queue counters. */
5507 #define DEV_STATS_INC(DEV, FIELD) atomic_long_inc(&(DEV)->stats.__##FIELD)
5508 #define DEV_STATS_ADD(DEV, FIELD, VAL) 	\
5509 		atomic_long_add((VAL), &(DEV)->stats.__##FIELD)
5510 #define DEV_STATS_READ(DEV, FIELD) atomic_long_read(&(DEV)->stats.__##FIELD)
5511 
5512 #endif	/* _LINUX_NETDEVICE_H */
5513