1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
11 #include <rte_errno.h>
15 static struct rte_bpf *
16 bpf_load(const struct rte_bpf_prm *prm)
20 size_t sz, bsz, insz, xsz;
22 xsz = prm->nb_xsym * sizeof(prm->xsym[0]);
23 insz = prm->nb_ins * sizeof(prm->ins[0]);
25 sz = insz + xsz + bsz;
27 buf = mmap(NULL, sz, PROT_READ | PROT_WRITE,
28 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
29 if (buf == MAP_FAILED)
35 memcpy(&bpf->prm, prm, sizeof(bpf->prm));
37 memcpy(buf + bsz, prm->xsym, xsz);
38 memcpy(buf + bsz + xsz, prm->ins, insz);
40 bpf->prm.xsym = (void *)(buf + bsz);
41 bpf->prm.ins = (void *)(buf + bsz + xsz);
47 * Check that user provided external symbol.
50 bpf_check_xsym(const struct rte_bpf_xsym *xsym)
54 if (xsym->name == NULL)
57 if (xsym->type == RTE_BPF_XTYPE_VAR) {
58 if (xsym->var.desc.type == RTE_BPF_ARG_UNDEF)
60 } else if (xsym->type == RTE_BPF_XTYPE_FUNC) {
62 if (xsym->func.nb_args > EBPF_FUNC_MAX_ARGS)
65 /* check function arguments */
66 for (i = 0; i != xsym->func.nb_args; i++) {
67 if (xsym->func.args[i].type == RTE_BPF_ARG_UNDEF)
71 /* check return value info */
72 if (xsym->func.ret.type != RTE_BPF_ARG_UNDEF &&
73 xsym->func.ret.size == 0)
82 rte_bpf_load(const struct rte_bpf_prm *prm)
88 if (prm == NULL || prm->ins == NULL ||
89 (prm->nb_xsym != 0 && prm->xsym == NULL)) {
95 for (i = 0; i != prm->nb_xsym && rc == 0; i++)
96 rc = bpf_check_xsym(prm->xsym + i);
100 RTE_BPF_LOG(ERR, "%s: %d-th xsym is invalid\n", __func__, i);
110 rc = bpf_validate(bpf);
113 if (mprotect(bpf, bpf->sz, PROT_READ) != 0)
118 rte_bpf_destroy(bpf);