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