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