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