]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/dpdk/lib/librte_bpf/bpf_load.c
import 15.2.0 Octopus source
[ceph.git] / ceph / src / spdk / dpdk / lib / librte_bpf / bpf_load.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
3 */
4
5 #include <stdarg.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <errno.h>
9 #include <stdint.h>
10 #include <unistd.h>
11 #include <inttypes.h>
12
13 #include <rte_common.h>
14 #include <rte_log.h>
15 #include <rte_debug.h>
16 #include <rte_memory.h>
17 #include <rte_eal.h>
18 #include <rte_byteorder.h>
19 #include <rte_errno.h>
20
21 #include "bpf_impl.h"
22
23 static struct rte_bpf *
24 bpf_load(const struct rte_bpf_prm *prm)
25 {
26 uint8_t *buf;
27 struct rte_bpf *bpf;
28 size_t sz, bsz, insz, xsz;
29
30 xsz = prm->nb_xsym * sizeof(prm->xsym[0]);
31 insz = prm->nb_ins * sizeof(prm->ins[0]);
32 bsz = sizeof(bpf[0]);
33 sz = insz + xsz + bsz;
34
35 buf = mmap(NULL, sz, PROT_READ | PROT_WRITE,
36 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
37 if (buf == MAP_FAILED)
38 return NULL;
39
40 bpf = (void *)buf;
41 bpf->sz = sz;
42
43 memcpy(&bpf->prm, prm, sizeof(bpf->prm));
44
45 memcpy(buf + bsz, prm->xsym, xsz);
46 memcpy(buf + bsz + xsz, prm->ins, insz);
47
48 bpf->prm.xsym = (void *)(buf + bsz);
49 bpf->prm.ins = (void *)(buf + bsz + xsz);
50
51 return bpf;
52 }
53
54 /*
55 * Check that user provided external symbol.
56 */
57 static int
58 bpf_check_xsym(const struct rte_bpf_xsym *xsym)
59 {
60 uint32_t i;
61
62 if (xsym->name == NULL)
63 return -EINVAL;
64
65 if (xsym->type == RTE_BPF_XTYPE_VAR) {
66 if (xsym->var.desc.type == RTE_BPF_ARG_UNDEF)
67 return -EINVAL;
68 } else if (xsym->type == RTE_BPF_XTYPE_FUNC) {
69
70 if (xsym->func.nb_args > EBPF_FUNC_MAX_ARGS)
71 return -EINVAL;
72
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)
76 return -EINVAL;
77 }
78
79 /* check return value info */
80 if (xsym->func.ret.type != RTE_BPF_ARG_UNDEF &&
81 xsym->func.ret.size == 0)
82 return -EINVAL;
83 } else
84 return -EINVAL;
85
86 return 0;
87 }
88
89 __rte_experimental struct rte_bpf *
90 rte_bpf_load(const struct rte_bpf_prm *prm)
91 {
92 struct rte_bpf *bpf;
93 int32_t rc;
94 uint32_t i;
95
96 if (prm == NULL || prm->ins == NULL ||
97 (prm->nb_xsym != 0 && prm->xsym == NULL)) {
98 rte_errno = EINVAL;
99 return NULL;
100 }
101
102 rc = 0;
103 for (i = 0; i != prm->nb_xsym && rc == 0; i++)
104 rc = bpf_check_xsym(prm->xsym + i);
105
106 if (rc != 0) {
107 rte_errno = -rc;
108 RTE_BPF_LOG(ERR, "%s: %d-th xsym is invalid\n", __func__, i);
109 return NULL;
110 }
111
112 bpf = bpf_load(prm);
113 if (bpf == NULL) {
114 rte_errno = ENOMEM;
115 return NULL;
116 }
117
118 rc = bpf_validate(bpf);
119 if (rc == 0) {
120 bpf_jit(bpf);
121 if (mprotect(bpf, bpf->sz, PROT_READ) != 0)
122 rc = -ENOMEM;
123 }
124
125 if (rc != 0) {
126 rte_bpf_destroy(bpf);
127 rte_errno = -rc;
128 return NULL;
129 }
130
131 return bpf;
132 }
133
134 __rte_experimental __rte_weak struct rte_bpf *
135 rte_bpf_elf_load(const struct rte_bpf_prm *prm, const char *fname,
136 const char *sname)
137 {
138 if (prm == NULL || fname == NULL || sname == NULL) {
139 rte_errno = EINVAL;
140 return NULL;
141 }
142
143 RTE_BPF_LOG(ERR, "%s() is not supported with current config\n"
144 "rebuild with libelf installed\n",
145 __func__);
146 rte_errno = ENOTSUP;
147 return NULL;
148 }