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