1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
13 #include <rte_common.h>
15 #include <rte_debug.h>
16 #include <rte_memory.h>
18 #include <rte_byteorder.h>
19 #include <rte_errno.h>
23 static struct rte_bpf *
24 bpf_load(const struct rte_bpf_prm *prm)
28 size_t sz, bsz, insz, xsz;
30 xsz = prm->nb_xsym * sizeof(prm->xsym[0]);
31 insz = prm->nb_ins * sizeof(prm->ins[0]);
33 sz = insz + xsz + bsz;
35 buf = mmap(NULL, sz, PROT_READ | PROT_WRITE,
36 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
37 if (buf == MAP_FAILED)
43 memcpy(&bpf->prm, prm, sizeof(bpf->prm));
45 memcpy(buf + bsz, prm->xsym, xsz);
46 memcpy(buf + bsz + xsz, prm->ins, insz);
48 bpf->prm.xsym = (void *)(buf + bsz);
49 bpf->prm.ins = (void *)(buf + bsz + xsz);
55 * Check that user provided external symbol.
58 bpf_check_xsym(const struct rte_bpf_xsym *xsym)
62 if (xsym->name == NULL)
65 if (xsym->type == RTE_BPF_XTYPE_VAR) {
66 if (xsym->var.desc.type == RTE_BPF_ARG_UNDEF)
68 } else if (xsym->type == RTE_BPF_XTYPE_FUNC) {
70 if (xsym->func.nb_args > EBPF_FUNC_MAX_ARGS)
73 /* check function arguments */
74 for (i = 0; i != xsym->func.nb_args; i++) {
75 if (xsym->func.args[i].type == RTE_BPF_ARG_UNDEF)
79 /* check return value info */
80 if (xsym->func.ret.type != RTE_BPF_ARG_UNDEF &&
81 xsym->func.ret.size == 0)
90 rte_bpf_load(const struct rte_bpf_prm *prm)
96 if (prm == NULL || prm->ins == NULL ||
97 (prm->nb_xsym != 0 && prm->xsym == NULL)) {
103 for (i = 0; i != prm->nb_xsym && rc == 0; i++)
104 rc = bpf_check_xsym(prm->xsym + i);
108 RTE_BPF_LOG(ERR, "%s: %d-th xsym is invalid\n", __func__, i);
118 rc = bpf_validate(bpf);
121 if (mprotect(bpf, bpf->sz, PROT_READ) != 0)
126 rte_bpf_destroy(bpf);
134 #ifndef RTE_LIBRTE_BPF_ELF
136 rte_bpf_elf_load(const struct rte_bpf_prm *prm, const char *fname,
139 if (prm == NULL || fname == NULL || sname == NULL) {
144 RTE_BPF_LOG(ERR, "%s() is not supported with current config\n"
145 "rebuild with libelf installed\n",