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