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