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