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