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 XDP_USE_NEED_WAKEUP
50 static int
tx_syscall_needed(struct xsk_ring_prod * q)51 tx_syscall_needed(struct xsk_ring_prod *q)
52 {
53 return xsk_ring_prod__needs_wakeup(q);
54 }
55 #else
56 static int
tx_syscall_needed(struct xsk_ring_prod * q __rte_unused)57 tx_syscall_needed(struct xsk_ring_prod *q __rte_unused)
58 {
59 return 1;
60 }
61 #endif
62
63 #ifdef RTE_NET_AF_XDP_LIBBPF_OBJ_OPEN
load_program(const char * prog_path,struct bpf_object ** obj)64 static int load_program(const char *prog_path, struct bpf_object **obj)
65 {
66 struct bpf_program *prog;
67 int err;
68
69 *obj = bpf_object__open_file(prog_path, NULL);
70 err = libbpf_get_error(*obj);
71 if (err)
72 return -1;
73
74 err = bpf_object__load(*obj);
75 if (err)
76 goto out;
77
78 prog = bpf_object__next_program(*obj, NULL);
79 if (!prog)
80 goto out;
81
82 return bpf_program__fd(prog);
83
84 out:
85 bpf_object__close(*obj);
86 return -1;
87 }
88 #else
load_program(const char * prog_path,struct bpf_object ** obj)89 static int load_program(const char *prog_path, struct bpf_object **obj)
90 {
91 int ret, prog_fd;
92
93 ret = bpf_prog_load(prog_path, BPF_PROG_TYPE_XDP, obj, &prog_fd);
94 if (ret)
95 return -1;
96
97 return prog_fd;
98 }
99 #endif
100