bpf: add BPF loading and execution framework
[dpdk.git] / lib / librte_bpf / bpf.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 <inttypes.h>
11
12 #include <rte_common.h>
13 #include <rte_eal.h>
14
15 #include "bpf_impl.h"
16
17 int rte_bpf_logtype;
18
19 __rte_experimental void
20 rte_bpf_destroy(struct rte_bpf *bpf)
21 {
22         if (bpf != NULL) {
23                 if (bpf->jit.func != NULL)
24                         munmap(bpf->jit.func, bpf->jit.sz);
25                 munmap(bpf, bpf->sz);
26         }
27 }
28
29 __rte_experimental int
30 rte_bpf_get_jit(const struct rte_bpf *bpf, struct rte_bpf_jit *jit)
31 {
32         if (bpf == NULL || jit == NULL)
33                 return -EINVAL;
34
35         jit[0] = bpf->jit;
36         return 0;
37 }
38
39 int
40 bpf_jit(struct rte_bpf *bpf)
41 {
42         int32_t rc;
43
44         rc = -ENOTSUP;
45         if (rc != 0)
46                 RTE_BPF_LOG(WARNING, "%s(%p) failed, error code: %d;\n",
47                         __func__, bpf, rc);
48         return rc;
49 }
50
51 RTE_INIT(rte_bpf_init_log);
52
53 static void
54 rte_bpf_init_log(void)
55 {
56         rte_bpf_logtype = rte_log_register("lib.bpf");
57         if (rte_bpf_logtype >= 0)
58                 rte_log_set_level(rte_bpf_logtype, RTE_LOG_INFO);
59 }