bpf: add JIT compilation for x86_64 ISA
[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 #ifdef RTE_ARCH_X86_64
45         rc = bpf_jit_x86(bpf);
46 #else
47         rc = -ENOTSUP;
48 #endif
49
50         if (rc != 0)
51                 RTE_BPF_LOG(WARNING, "%s(%p) failed, error code: %d;\n",
52                         __func__, bpf, rc);
53         return rc;
54 }
55
56 RTE_INIT(rte_bpf_init_log);
57
58 static void
59 rte_bpf_init_log(void)
60 {
61         rte_bpf_logtype = rte_log_register("lib.bpf");
62         if (rte_bpf_logtype >= 0)
63                 rte_log_set_level(rte_bpf_logtype, RTE_LOG_INFO);
64 }