1 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
4 * AF_XDP user-space access library.
6 * Copyright(c) 2018 - 2019 Intel Corporation.
11 #ifndef __LIBBPF_XSK_H
12 #define __LIBBPF_XSK_H
16 #include <linux/if_xdp.h>
19 #include "libbpf_util.h"
25 /* Do not access these members directly. Use the functions below. */
26 #define DEFINE_XSK_RING(name) \
37 DEFINE_XSK_RING(xsk_ring_prod);
38 DEFINE_XSK_RING(xsk_ring_cons);
40 /* For a detailed explanation on the memory barriers associated with the
41 * ring, please take a look at net/xdp/xsk_queue.h.
47 static inline __u64 *xsk_ring_prod__fill_addr(struct xsk_ring_prod *fill,
50 __u64 *addrs = (__u64 *)fill->ring;
52 return &addrs[idx & fill->mask];
55 static inline const __u64 *
56 xsk_ring_cons__comp_addr(const struct xsk_ring_cons *comp, __u32 idx)
58 const __u64 *addrs = (const __u64 *)comp->ring;
60 return &addrs[idx & comp->mask];
63 static inline struct xdp_desc *xsk_ring_prod__tx_desc(struct xsk_ring_prod *tx,
66 struct xdp_desc *descs = (struct xdp_desc *)tx->ring;
68 return &descs[idx & tx->mask];
71 static inline const struct xdp_desc *
72 xsk_ring_cons__rx_desc(const struct xsk_ring_cons *rx, __u32 idx)
74 const struct xdp_desc *descs = (const struct xdp_desc *)rx->ring;
76 return &descs[idx & rx->mask];
79 static inline __u32 xsk_prod_nb_free(struct xsk_ring_prod *r, __u32 nb)
81 __u32 free_entries = r->cached_cons - r->cached_prod;
83 if (free_entries >= nb)
86 /* Refresh the local tail pointer.
87 * cached_cons is r->size bigger than the real consumer pointer so
88 * that this addition can be avoided in the more frequently
89 * executed code that computs free_entries in the beginning of
90 * this function. Without this optimization it whould have been
91 * free_entries = r->cached_prod - r->cached_cons + r->size.
93 r->cached_cons = *r->consumer + r->size;
95 return r->cached_cons - r->cached_prod;
98 static inline __u32 xsk_cons_nb_avail(struct xsk_ring_cons *r, __u32 nb)
100 __u32 entries = r->cached_prod - r->cached_cons;
103 r->cached_prod = *r->producer;
104 entries = r->cached_prod - r->cached_cons;
107 return (entries > nb) ? nb : entries;
110 static inline size_t xsk_ring_prod__reserve(struct xsk_ring_prod *prod,
111 size_t nb, __u32 *idx)
113 if (xsk_prod_nb_free(prod, nb) < nb)
116 *idx = prod->cached_prod;
117 prod->cached_prod += nb;
122 static inline void xsk_ring_prod__submit(struct xsk_ring_prod *prod, size_t nb)
124 /* Make sure everything has been written to the ring before indicating
125 * this to the kernel by writing the producer pointer.
129 *prod->producer += nb;
132 static inline size_t xsk_ring_cons__peek(struct xsk_ring_cons *cons,
133 size_t nb, __u32 *idx)
135 size_t entries = xsk_cons_nb_avail(cons, nb);
138 /* Make sure we do not speculatively read the data before
139 * we have received the packet buffers from the ring.
143 *idx = cons->cached_cons;
144 cons->cached_cons += entries;
150 static inline void xsk_ring_cons__release(struct xsk_ring_cons *cons, size_t nb)
152 /* Make sure data has been read before indicating we are done
153 * with the entries by updating the consumer pointer.
157 *cons->consumer += nb;
160 static inline void *xsk_umem__get_data(void *umem_area, __u64 addr)
162 return &((char *)umem_area)[addr];
165 LIBBPF_API int xsk_umem__fd(const struct xsk_umem *umem);
166 LIBBPF_API int xsk_socket__fd(const struct xsk_socket *xsk);
168 #define XSK_RING_CONS__DEFAULT_NUM_DESCS 2048
169 #define XSK_RING_PROD__DEFAULT_NUM_DESCS 2048
170 #define XSK_UMEM__DEFAULT_FRAME_SHIFT 12 /* 4096 bytes */
171 #define XSK_UMEM__DEFAULT_FRAME_SIZE (1 << XSK_UMEM__DEFAULT_FRAME_SHIFT)
172 #define XSK_UMEM__DEFAULT_FRAME_HEADROOM 0
174 struct xsk_umem_config {
178 __u32 frame_headroom;
181 /* Flags for the libbpf_flags field. */
182 #define XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD (1 << 0)
184 struct xsk_socket_config {
192 /* Set config to NULL to get the default configuration. */
193 LIBBPF_API int xsk_umem__create(struct xsk_umem **umem,
194 void *umem_area, __u64 size,
195 struct xsk_ring_prod *fill,
196 struct xsk_ring_cons *comp,
197 const struct xsk_umem_config *config);
198 LIBBPF_API int xsk_socket__create(struct xsk_socket **xsk,
199 const char *ifname, __u32 queue_id,
200 struct xsk_umem *umem,
201 struct xsk_ring_cons *rx,
202 struct xsk_ring_prod *tx,
203 const struct xsk_socket_config *config);
205 /* Returns 0 for success and -EBUSY if the umem is still in use. */
206 LIBBPF_API int xsk_umem__delete(struct xsk_umem *umem);
207 LIBBPF_API void xsk_socket__delete(struct xsk_socket *xsk);
213 #endif /* __LIBBPF_XSK_H */