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