xref: /dpdk/drivers/net/af_xdp/compat.h (revision 9c1323736cf91aa46d43def8e8d2349f7498a203)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2020 Intel Corporation.
3  */
4 
5 #ifdef RTE_NET_AF_XDP_LIBXDP
6 #include <xdp/xsk.h>
7 #else
8 #include <bpf/xsk.h>
9 #endif
10 #include <bpf/bpf.h>
11 #include <linux/version.h>
12 #include <poll.h>
13 
14 #if KERNEL_VERSION(5, 10, 0) <= LINUX_VERSION_CODE && \
15 	defined(RTE_NET_AF_XDP_SHARED_UMEM)
16 #define ETH_AF_XDP_SHARED_UMEM 1
17 #endif
18 
19 #ifdef ETH_AF_XDP_SHARED_UMEM
20 static __rte_always_inline int
create_shared_socket(struct xsk_socket ** xsk_ptr,const char * ifname,__u32 queue_id,struct xsk_umem * umem,struct xsk_ring_cons * rx,struct xsk_ring_prod * tx,struct xsk_ring_prod * fill,struct xsk_ring_cons * comp,const struct xsk_socket_config * config)21 create_shared_socket(struct xsk_socket **xsk_ptr,
22 			  const char *ifname,
23 			  __u32 queue_id, struct xsk_umem *umem,
24 			  struct xsk_ring_cons *rx,
25 			  struct xsk_ring_prod *tx,
26 			  struct xsk_ring_prod *fill,
27 			  struct xsk_ring_cons *comp,
28 			  const struct xsk_socket_config *config)
29 {
30 	return xsk_socket__create_shared(xsk_ptr, ifname, queue_id, umem, rx,
31 						tx, fill, comp, config);
32 }
33 #else
34 static __rte_always_inline int
create_shared_socket(struct xsk_socket ** xsk_ptr __rte_unused,const char * ifname __rte_unused,__u32 queue_id __rte_unused,struct xsk_umem * umem __rte_unused,struct xsk_ring_cons * rx __rte_unused,struct xsk_ring_prod * tx __rte_unused,struct xsk_ring_prod * fill __rte_unused,struct xsk_ring_cons * comp __rte_unused,const struct xsk_socket_config * config __rte_unused)35 create_shared_socket(struct xsk_socket **xsk_ptr __rte_unused,
36 			  const char *ifname __rte_unused,
37 			  __u32 queue_id __rte_unused,
38 			  struct xsk_umem *umem __rte_unused,
39 			  struct xsk_ring_cons *rx __rte_unused,
40 			  struct xsk_ring_prod *tx __rte_unused,
41 			  struct xsk_ring_prod *fill __rte_unused,
42 			  struct xsk_ring_cons *comp __rte_unused,
43 			  const struct xsk_socket_config *config __rte_unused)
44 {
45 	return -1;
46 }
47 #endif
48 
49 #ifdef ETH_AF_XDP_UPDATE_XSKMAP
50 static __rte_always_inline int
update_xskmap(struct xsk_socket * xsk,int map_fd,int xsk_queue_idx __rte_unused)51 update_xskmap(struct xsk_socket *xsk, int map_fd, int xsk_queue_idx __rte_unused)
52 {
53 	return xsk_socket__update_xskmap(xsk, map_fd);
54 }
55 #else
56 static __rte_always_inline int
update_xskmap(struct xsk_socket * xsk,int map_fd,int xsk_queue_idx)57 update_xskmap(struct xsk_socket *xsk, int map_fd, int xsk_queue_idx)
58 {
59 	int fd = xsk_socket__fd(xsk);
60 	return bpf_map_update_elem(map_fd, &xsk_queue_idx, &fd, 0);
61 }
62 #endif
63 
64 #ifdef XDP_USE_NEED_WAKEUP
65 static int
tx_syscall_needed(struct xsk_ring_prod * q)66 tx_syscall_needed(struct xsk_ring_prod *q)
67 {
68 	return xsk_ring_prod__needs_wakeup(q);
69 }
70 #else
71 static int
tx_syscall_needed(struct xsk_ring_prod * q __rte_unused)72 tx_syscall_needed(struct xsk_ring_prod *q __rte_unused)
73 {
74 	return 1;
75 }
76 #endif
77 
78 #ifdef RTE_NET_AF_XDP_LIBBPF_OBJ_OPEN
load_program(const char * prog_path,struct bpf_object ** obj)79 static int load_program(const char *prog_path, struct bpf_object **obj)
80 {
81 	struct bpf_program *prog;
82 	int err;
83 
84 	*obj = bpf_object__open_file(prog_path, NULL);
85 	err = libbpf_get_error(*obj);
86 	if (err)
87 		return -1;
88 
89 	err = bpf_object__load(*obj);
90 	if (err)
91 		goto out;
92 
93 	prog = bpf_object__next_program(*obj, NULL);
94 	if (!prog)
95 		goto out;
96 
97 	return bpf_program__fd(prog);
98 
99 out:
100 	bpf_object__close(*obj);
101 	return -1;
102 }
103 #else
load_program(const char * prog_path,struct bpf_object ** obj)104 static int load_program(const char *prog_path, struct bpf_object **obj)
105 {
106 	int ret, prog_fd;
107 
108 	ret = bpf_prog_load(prog_path, BPF_PROG_TYPE_XDP, obj, &prog_fd);
109 	if (ret)
110 		return -1;
111 
112 	return prog_fd;
113 }
114 #endif
115