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