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