Re: [PATCH bpf-next 1/2] libbpf: add support for using AF_XDP sockets
From: Daniel Borkmann <daniel@iogearbox.net>
Date: 2018-12-12 00:57:15
On 12/10/2018 04:34 PM, Magnus Karlsson wrote: [...]
quoted hunk ↗ jump to hunk
diff --git a/tools/lib/bpf/README.rst b/tools/lib/bpf/README.rst index 056f383..5a4d644 100644 --- a/tools/lib/bpf/README.rst +++ b/tools/lib/bpf/README.rst@@ -9,7 +9,7 @@ described here. It's recommended to follow these conventions whenever a new function or type is added to keep libbpf API clean and consistent. All types and functions provided by libbpf API should have one of the -following prefixes: ``bpf_``, ``btf_``, ``libbpf_``. +following prefixes: ``bpf_``, ``btf_``, ``libbpf_``, ``xsk_``. System call wrappers --------------------@@ -62,6 +62,15 @@ Auxiliary functions and types that don't fit well in any of categories described above should have ``libbpf_`` prefix, e.g. ``libbpf_get_error`` or ``libbpf_prog_type_by_name``. +AF_XDP functions +------------------- + +AF_XDP functions should have ``xsk_`` prefix, e.g. ``xsk_get_data`` +or ``xsk_create_umem``. The interface consists of both low-level ring +access functions and high-level configuration functions. These can be +mixed and matched. Note that these functions are not reentrant for +performance reasons. + libbpf ABI ==========
[...]
+ +LIBBPF_API size_t xsk_peek_cons(struct xsk_cons_ring *ring, size_t nb, + __u32 *idx); +LIBBPF_API void xsk_release_cons(struct xsk_cons_ring *ring); +LIBBPF_API size_t xsk_reserve_prod(struct xsk_prod_ring *ring, size_t nb, + __u32 *idx); +LIBBPF_API void xsk_submit_prod(struct xsk_prod_ring *ring); + +LIBBPF_API void *xsk_get_data(void *umem_area, __u64 addr);
Nit (though exposed API related): Given existing naming conventions I think these API helpers should have xsk__ prefix (double '_') similarly as done with btf ones.
+#define XSK_DEFAULT_NUM_DESCS 2048
+#define XSK_DEFAULT_FRAME_SHIFT 11 /* 2048 bytes */
+#define XSK_DEFAULT_FRAME_SIZE (1 << XSK_DEFAULT_FRAME_SHIFT)
+#define XSK_DEFAULT_FRAME_HEADROOM 0
+
+struct xsk_umem_config {
+ __u32 fq_size;
+ __u32 cq_size;
+ __u32 frame_size;
+ __u32 frame_headroom;
+};
+
+struct xsk_xdp_socket_config {
+ __u32 rx_size;
+ __u32 tx_size;
+};
+
+/* Set config to XSK_DEFAULT_CONFIG to get the default configuration. */
+LIBBPF_API int xsk_create_umem(void *umem_area, __u64 size,
+ struct xsk_prod_ring *fq,
+ struct xsk_cons_ring *cq,
+ struct xsk_umem_config *config);
+LIBBPF_API int xsk_create_xdp_socket(int umem_fd, struct xsk_cons_ring *rx,
+ struct xsk_prod_ring *tx,
+ struct xsk_xdp_socket_config *config);
+/* Returns 0 for success. */
+LIBBPF_API int xsk_delete_umem(int fd);
+LIBBPF_API int xsk_delete_xdp_socket(int fd);
+
#ifdef __cplusplus