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