1*f7c14bbaSAndroid Build Coastguard Worker /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ 2*f7c14bbaSAndroid Build Coastguard Worker /* 3*f7c14bbaSAndroid Build Coastguard Worker * if_xdp: XDP socket user-space interface 4*f7c14bbaSAndroid Build Coastguard Worker * Copyright(c) 2018 Intel Corporation. 5*f7c14bbaSAndroid Build Coastguard Worker * 6*f7c14bbaSAndroid Build Coastguard Worker * Author(s): Björn Töpel <[email protected]> 7*f7c14bbaSAndroid Build Coastguard Worker * Magnus Karlsson <[email protected]> 8*f7c14bbaSAndroid Build Coastguard Worker */ 9*f7c14bbaSAndroid Build Coastguard Worker 10*f7c14bbaSAndroid Build Coastguard Worker #ifndef _LINUX_IF_XDP_H 11*f7c14bbaSAndroid Build Coastguard Worker #define _LINUX_IF_XDP_H 12*f7c14bbaSAndroid Build Coastguard Worker 13*f7c14bbaSAndroid Build Coastguard Worker #include <linux/types.h> 14*f7c14bbaSAndroid Build Coastguard Worker 15*f7c14bbaSAndroid Build Coastguard Worker /* Options for the sxdp_flags field */ 16*f7c14bbaSAndroid Build Coastguard Worker #define XDP_SHARED_UMEM (1 << 0) 17*f7c14bbaSAndroid Build Coastguard Worker #define XDP_COPY (1 << 1) /* Force copy-mode */ 18*f7c14bbaSAndroid Build Coastguard Worker #define XDP_ZEROCOPY (1 << 2) /* Force zero-copy mode */ 19*f7c14bbaSAndroid Build Coastguard Worker /* If this option is set, the driver might go sleep and in that case 20*f7c14bbaSAndroid Build Coastguard Worker * the XDP_RING_NEED_WAKEUP flag in the fill and/or Tx rings will be 21*f7c14bbaSAndroid Build Coastguard Worker * set. If it is set, the application need to explicitly wake up the 22*f7c14bbaSAndroid Build Coastguard Worker * driver with a poll() (Rx and Tx) or sendto() (Tx only). If you are 23*f7c14bbaSAndroid Build Coastguard Worker * running the driver and the application on the same core, you should 24*f7c14bbaSAndroid Build Coastguard Worker * use this option so that the kernel will yield to the user space 25*f7c14bbaSAndroid Build Coastguard Worker * application. 26*f7c14bbaSAndroid Build Coastguard Worker */ 27*f7c14bbaSAndroid Build Coastguard Worker #define XDP_USE_NEED_WAKEUP (1 << 3) 28*f7c14bbaSAndroid Build Coastguard Worker /* By setting this option, userspace application indicates that it can 29*f7c14bbaSAndroid Build Coastguard Worker * handle multiple descriptors per packet thus enabling AF_XDP to split 30*f7c14bbaSAndroid Build Coastguard Worker * multi-buffer XDP frames into multiple Rx descriptors. Without this set 31*f7c14bbaSAndroid Build Coastguard Worker * such frames will be dropped. 32*f7c14bbaSAndroid Build Coastguard Worker */ 33*f7c14bbaSAndroid Build Coastguard Worker #define XDP_USE_SG (1 << 4) 34*f7c14bbaSAndroid Build Coastguard Worker 35*f7c14bbaSAndroid Build Coastguard Worker /* Flags for xsk_umem_config flags */ 36*f7c14bbaSAndroid Build Coastguard Worker #define XDP_UMEM_UNALIGNED_CHUNK_FLAG (1 << 0) 37*f7c14bbaSAndroid Build Coastguard Worker 38*f7c14bbaSAndroid Build Coastguard Worker /* Force checksum calculation in software. Can be used for testing or 39*f7c14bbaSAndroid Build Coastguard Worker * working around potential HW issues. This option causes performance 40*f7c14bbaSAndroid Build Coastguard Worker * degradation and only works in XDP_COPY mode. 41*f7c14bbaSAndroid Build Coastguard Worker */ 42*f7c14bbaSAndroid Build Coastguard Worker #define XDP_UMEM_TX_SW_CSUM (1 << 1) 43*f7c14bbaSAndroid Build Coastguard Worker 44*f7c14bbaSAndroid Build Coastguard Worker struct sockaddr_xdp { 45*f7c14bbaSAndroid Build Coastguard Worker __u16 sxdp_family; 46*f7c14bbaSAndroid Build Coastguard Worker __u16 sxdp_flags; 47*f7c14bbaSAndroid Build Coastguard Worker __u32 sxdp_ifindex; 48*f7c14bbaSAndroid Build Coastguard Worker __u32 sxdp_queue_id; 49*f7c14bbaSAndroid Build Coastguard Worker __u32 sxdp_shared_umem_fd; 50*f7c14bbaSAndroid Build Coastguard Worker }; 51*f7c14bbaSAndroid Build Coastguard Worker 52*f7c14bbaSAndroid Build Coastguard Worker /* XDP_RING flags */ 53*f7c14bbaSAndroid Build Coastguard Worker #define XDP_RING_NEED_WAKEUP (1 << 0) 54*f7c14bbaSAndroid Build Coastguard Worker 55*f7c14bbaSAndroid Build Coastguard Worker struct xdp_ring_offset { 56*f7c14bbaSAndroid Build Coastguard Worker __u64 producer; 57*f7c14bbaSAndroid Build Coastguard Worker __u64 consumer; 58*f7c14bbaSAndroid Build Coastguard Worker __u64 desc; 59*f7c14bbaSAndroid Build Coastguard Worker __u64 flags; 60*f7c14bbaSAndroid Build Coastguard Worker }; 61*f7c14bbaSAndroid Build Coastguard Worker 62*f7c14bbaSAndroid Build Coastguard Worker struct xdp_mmap_offsets { 63*f7c14bbaSAndroid Build Coastguard Worker struct xdp_ring_offset rx; 64*f7c14bbaSAndroid Build Coastguard Worker struct xdp_ring_offset tx; 65*f7c14bbaSAndroid Build Coastguard Worker struct xdp_ring_offset fr; /* Fill */ 66*f7c14bbaSAndroid Build Coastguard Worker struct xdp_ring_offset cr; /* Completion */ 67*f7c14bbaSAndroid Build Coastguard Worker }; 68*f7c14bbaSAndroid Build Coastguard Worker 69*f7c14bbaSAndroid Build Coastguard Worker /* XDP socket options */ 70*f7c14bbaSAndroid Build Coastguard Worker #define XDP_MMAP_OFFSETS 1 71*f7c14bbaSAndroid Build Coastguard Worker #define XDP_RX_RING 2 72*f7c14bbaSAndroid Build Coastguard Worker #define XDP_TX_RING 3 73*f7c14bbaSAndroid Build Coastguard Worker #define XDP_UMEM_REG 4 74*f7c14bbaSAndroid Build Coastguard Worker #define XDP_UMEM_FILL_RING 5 75*f7c14bbaSAndroid Build Coastguard Worker #define XDP_UMEM_COMPLETION_RING 6 76*f7c14bbaSAndroid Build Coastguard Worker #define XDP_STATISTICS 7 77*f7c14bbaSAndroid Build Coastguard Worker #define XDP_OPTIONS 8 78*f7c14bbaSAndroid Build Coastguard Worker 79*f7c14bbaSAndroid Build Coastguard Worker struct xdp_umem_reg { 80*f7c14bbaSAndroid Build Coastguard Worker __u64 addr; /* Start of packet data area */ 81*f7c14bbaSAndroid Build Coastguard Worker __u64 len; /* Length of packet data area */ 82*f7c14bbaSAndroid Build Coastguard Worker __u32 chunk_size; 83*f7c14bbaSAndroid Build Coastguard Worker __u32 headroom; 84*f7c14bbaSAndroid Build Coastguard Worker __u32 flags; 85*f7c14bbaSAndroid Build Coastguard Worker __u32 tx_metadata_len; 86*f7c14bbaSAndroid Build Coastguard Worker }; 87*f7c14bbaSAndroid Build Coastguard Worker 88*f7c14bbaSAndroid Build Coastguard Worker struct xdp_statistics { 89*f7c14bbaSAndroid Build Coastguard Worker __u64 rx_dropped; /* Dropped for other reasons */ 90*f7c14bbaSAndroid Build Coastguard Worker __u64 rx_invalid_descs; /* Dropped due to invalid descriptor */ 91*f7c14bbaSAndroid Build Coastguard Worker __u64 tx_invalid_descs; /* Dropped due to invalid descriptor */ 92*f7c14bbaSAndroid Build Coastguard Worker __u64 rx_ring_full; /* Dropped due to rx ring being full */ 93*f7c14bbaSAndroid Build Coastguard Worker __u64 rx_fill_ring_empty_descs; /* Failed to retrieve item from fill ring */ 94*f7c14bbaSAndroid Build Coastguard Worker __u64 tx_ring_empty_descs; /* Failed to retrieve item from tx ring */ 95*f7c14bbaSAndroid Build Coastguard Worker }; 96*f7c14bbaSAndroid Build Coastguard Worker 97*f7c14bbaSAndroid Build Coastguard Worker struct xdp_options { 98*f7c14bbaSAndroid Build Coastguard Worker __u32 flags; 99*f7c14bbaSAndroid Build Coastguard Worker }; 100*f7c14bbaSAndroid Build Coastguard Worker 101*f7c14bbaSAndroid Build Coastguard Worker /* Flags for the flags field of struct xdp_options */ 102*f7c14bbaSAndroid Build Coastguard Worker #define XDP_OPTIONS_ZEROCOPY (1 << 0) 103*f7c14bbaSAndroid Build Coastguard Worker 104*f7c14bbaSAndroid Build Coastguard Worker /* Pgoff for mmaping the rings */ 105*f7c14bbaSAndroid Build Coastguard Worker #define XDP_PGOFF_RX_RING 0 106*f7c14bbaSAndroid Build Coastguard Worker #define XDP_PGOFF_TX_RING 0x80000000 107*f7c14bbaSAndroid Build Coastguard Worker #define XDP_UMEM_PGOFF_FILL_RING 0x100000000ULL 108*f7c14bbaSAndroid Build Coastguard Worker #define XDP_UMEM_PGOFF_COMPLETION_RING 0x180000000ULL 109*f7c14bbaSAndroid Build Coastguard Worker 110*f7c14bbaSAndroid Build Coastguard Worker /* Masks for unaligned chunks mode */ 111*f7c14bbaSAndroid Build Coastguard Worker #define XSK_UNALIGNED_BUF_OFFSET_SHIFT 48 112*f7c14bbaSAndroid Build Coastguard Worker #define XSK_UNALIGNED_BUF_ADDR_MASK \ 113*f7c14bbaSAndroid Build Coastguard Worker ((1ULL << XSK_UNALIGNED_BUF_OFFSET_SHIFT) - 1) 114*f7c14bbaSAndroid Build Coastguard Worker 115*f7c14bbaSAndroid Build Coastguard Worker /* Request transmit timestamp. Upon completion, put it into tx_timestamp 116*f7c14bbaSAndroid Build Coastguard Worker * field of union xsk_tx_metadata. 117*f7c14bbaSAndroid Build Coastguard Worker */ 118*f7c14bbaSAndroid Build Coastguard Worker #define XDP_TXMD_FLAGS_TIMESTAMP (1 << 0) 119*f7c14bbaSAndroid Build Coastguard Worker 120*f7c14bbaSAndroid Build Coastguard Worker /* Request transmit checksum offload. Checksum start position and offset 121*f7c14bbaSAndroid Build Coastguard Worker * are communicated via csum_start and csum_offset fields of union 122*f7c14bbaSAndroid Build Coastguard Worker * xsk_tx_metadata. 123*f7c14bbaSAndroid Build Coastguard Worker */ 124*f7c14bbaSAndroid Build Coastguard Worker #define XDP_TXMD_FLAGS_CHECKSUM (1 << 1) 125*f7c14bbaSAndroid Build Coastguard Worker 126*f7c14bbaSAndroid Build Coastguard Worker /* AF_XDP offloads request. 'request' union member is consumed by the driver 127*f7c14bbaSAndroid Build Coastguard Worker * when the packet is being transmitted. 'completion' union member is 128*f7c14bbaSAndroid Build Coastguard Worker * filled by the driver when the transmit completion arrives. 129*f7c14bbaSAndroid Build Coastguard Worker */ 130*f7c14bbaSAndroid Build Coastguard Worker struct xsk_tx_metadata { 131*f7c14bbaSAndroid Build Coastguard Worker __u64 flags; 132*f7c14bbaSAndroid Build Coastguard Worker 133*f7c14bbaSAndroid Build Coastguard Worker union { 134*f7c14bbaSAndroid Build Coastguard Worker struct { 135*f7c14bbaSAndroid Build Coastguard Worker /* XDP_TXMD_FLAGS_CHECKSUM */ 136*f7c14bbaSAndroid Build Coastguard Worker 137*f7c14bbaSAndroid Build Coastguard Worker /* Offset from desc->addr where checksumming should start. */ 138*f7c14bbaSAndroid Build Coastguard Worker __u16 csum_start; 139*f7c14bbaSAndroid Build Coastguard Worker /* Offset from csum_start where checksum should be stored. */ 140*f7c14bbaSAndroid Build Coastguard Worker __u16 csum_offset; 141*f7c14bbaSAndroid Build Coastguard Worker } request; 142*f7c14bbaSAndroid Build Coastguard Worker 143*f7c14bbaSAndroid Build Coastguard Worker struct { 144*f7c14bbaSAndroid Build Coastguard Worker /* XDP_TXMD_FLAGS_TIMESTAMP */ 145*f7c14bbaSAndroid Build Coastguard Worker __u64 tx_timestamp; 146*f7c14bbaSAndroid Build Coastguard Worker } completion; 147*f7c14bbaSAndroid Build Coastguard Worker }; 148*f7c14bbaSAndroid Build Coastguard Worker }; 149*f7c14bbaSAndroid Build Coastguard Worker 150*f7c14bbaSAndroid Build Coastguard Worker /* Rx/Tx descriptor */ 151*f7c14bbaSAndroid Build Coastguard Worker struct xdp_desc { 152*f7c14bbaSAndroid Build Coastguard Worker __u64 addr; 153*f7c14bbaSAndroid Build Coastguard Worker __u32 len; 154*f7c14bbaSAndroid Build Coastguard Worker __u32 options; 155*f7c14bbaSAndroid Build Coastguard Worker }; 156*f7c14bbaSAndroid Build Coastguard Worker 157*f7c14bbaSAndroid Build Coastguard Worker /* UMEM descriptor is __u64 */ 158*f7c14bbaSAndroid Build Coastguard Worker 159*f7c14bbaSAndroid Build Coastguard Worker /* Flag indicating that the packet continues with the buffer pointed out by the 160*f7c14bbaSAndroid Build Coastguard Worker * next frame in the ring. The end of the packet is signalled by setting this 161*f7c14bbaSAndroid Build Coastguard Worker * bit to zero. For single buffer packets, every descriptor has 'options' set 162*f7c14bbaSAndroid Build Coastguard Worker * to 0 and this maintains backward compatibility. 163*f7c14bbaSAndroid Build Coastguard Worker */ 164*f7c14bbaSAndroid Build Coastguard Worker #define XDP_PKT_CONTD (1 << 0) 165*f7c14bbaSAndroid Build Coastguard Worker 166*f7c14bbaSAndroid Build Coastguard Worker /* TX packet carries valid metadata. */ 167*f7c14bbaSAndroid Build Coastguard Worker #define XDP_TX_METADATA (1 << 1) 168*f7c14bbaSAndroid Build Coastguard Worker 169*f7c14bbaSAndroid Build Coastguard Worker #endif /* _LINUX_IF_XDP_H */ 170