* Copyright(c) 2020 Intel Corporation.
*/
-#include <bpf/bpf.h>
+#ifdef RTE_NET_AF_XDP_LIBXDP
+#include <xdp/xsk.h>
+#else
#include <bpf/xsk.h>
+#endif
+#include <bpf/bpf.h>
#include <linux/version.h>
#include <poll.h>
#if KERNEL_VERSION(5, 10, 0) <= LINUX_VERSION_CODE && \
- defined(RTE_LIBRTE_AF_XDP_PMD_SHARED_UMEM)
+ defined(RTE_NET_AF_XDP_SHARED_UMEM)
#define ETH_AF_XDP_SHARED_UMEM 1
#endif
}
#endif
-#ifdef RTE_LIBRTE_AF_XDP_PMD_BPF_LINK
-static int link_lookup(int ifindex, int *link_fd)
-{
- struct bpf_link_info link_info;
- __u32 link_len;
- __u32 id = 0;
- int err;
- int fd;
-
- while (true) {
- err = bpf_link_get_next_id(id, &id);
- if (err) {
- if (errno == ENOENT) {
- err = 0;
- break;
- }
- break;
- }
-
- fd = bpf_link_get_fd_by_id(id);
- if (fd < 0) {
- if (errno == ENOENT)
- continue;
- err = -errno;
- break;
- }
-
- link_len = sizeof(struct bpf_link_info);
- memset(&link_info, 0, link_len);
- err = bpf_obj_get_info_by_fd(fd, &link_info, &link_len);
- if (err) {
- close(fd);
- break;
- }
- if (link_info.type == BPF_LINK_TYPE_XDP) {
- if ((int)link_info.xdp.ifindex == ifindex) {
- *link_fd = fd;
- break;
- }
- }
- close(fd);
- }
-
- return err;
-}
-
-static bool probe_bpf_link(void)
+#ifdef RTE_NET_AF_XDP_LIBBPF_OBJ_OPEN
+static int load_program(const char *prog_path, struct bpf_object **obj)
{
- DECLARE_LIBBPF_OPTS(bpf_link_create_opts, opts,
- .flags = XDP_FLAGS_SKB_MODE);
- struct bpf_load_program_attr prog_attr;
- struct bpf_insn insns[2];
- int prog_fd, link_fd = -1;
- int ifindex_lo = 1;
- bool ret = false;
+ struct bpf_program *prog;
int err;
- err = link_lookup(ifindex_lo, &link_fd);
+ *obj = bpf_object__open_file(prog_path, NULL);
+ err = libbpf_get_error(*obj);
if (err)
- return ret;
-
- if (link_fd >= 0)
- return true;
-
- /* BPF_MOV64_IMM(BPF_REG_0, XDP_PASS), */
- insns[0].code = BPF_ALU64 | BPF_MOV | BPF_K;
- insns[0].dst_reg = BPF_REG_0;
- insns[0].imm = XDP_PASS;
+ return -1;
- /* BPF_EXIT_INSN() */
- insns[1].code = BPF_JMP | BPF_EXIT;
-
- memset(&prog_attr, 0, sizeof(prog_attr));
- prog_attr.prog_type = BPF_PROG_TYPE_XDP;
- prog_attr.insns = insns;
- prog_attr.insns_cnt = RTE_DIM(insns);
- prog_attr.license = "GPL";
-
- prog_fd = bpf_load_program_xattr(&prog_attr, NULL, 0);
- if (prog_fd < 0)
- return ret;
-
- link_fd = bpf_link_create(prog_fd, ifindex_lo, BPF_XDP, &opts);
- close(prog_fd);
-
- if (link_fd >= 0) {
- ret = true;
- close(link_fd);
- }
-
- return ret;
-}
-
-static int link_xdp_program(int if_index, int prog_fd, bool use_bpf_link)
-{
- DECLARE_LIBBPF_OPTS(bpf_link_create_opts, opts);
- int link_fd, ret = 0;
+ err = bpf_object__load(*obj);
+ if (err)
+ goto out;
- if (!use_bpf_link)
- return bpf_set_link_xdp_fd(if_index, prog_fd,
- XDP_FLAGS_UPDATE_IF_NOEXIST);
+ prog = bpf_object__next_program(*obj, NULL);
+ if (!prog)
+ goto out;
- opts.flags = 0;
- link_fd = bpf_link_create(prog_fd, if_index, BPF_XDP, &opts);
- if (link_fd < 0)
- ret = -1;
+ return bpf_program__fd(prog);
- return ret;
+out:
+ bpf_object__close(*obj);
+ return -1;
}
#else
-static bool probe_bpf_link(void)
+static int load_program(const char *prog_path, struct bpf_object **obj)
{
- return false;
-}
+ int ret, prog_fd;
-static int link_xdp_program(int if_index, int prog_fd,
- bool use_bpf_link __rte_unused)
-{
- return bpf_set_link_xdp_fd(if_index, prog_fd,
- XDP_FLAGS_UPDATE_IF_NOEXIST);
+ ret = bpf_prog_load(prog_path, BPF_PROG_TYPE_XDP, obj, &prog_fd);
+ if (ret)
+ return -1;
+
+ return prog_fd;
}
#endif