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