bpf: add BPF loading and execution framework
[dpdk.git] / lib / librte_bpf / bpf_validate.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 /*
18  * dummy one for now, need more work.
19  */
20 int
21 bpf_validate(struct rte_bpf *bpf)
22 {
23         int32_t rc, ofs, stack_sz;
24         uint32_t i, op, dr;
25         const struct ebpf_insn *ins;
26
27         rc = 0;
28         stack_sz = 0;
29         for (i = 0; i != bpf->prm.nb_ins; i++) {
30
31                 ins = bpf->prm.ins + i;
32                 op = ins->code;
33                 dr = ins->dst_reg;
34                 ofs = ins->off;
35
36                 if ((BPF_CLASS(op) == BPF_STX || BPF_CLASS(op) == BPF_ST) &&
37                                 dr == EBPF_REG_10) {
38                         ofs -= sizeof(uint64_t);
39                         stack_sz = RTE_MIN(ofs, stack_sz);
40                 }
41         }
42
43         if (stack_sz != 0) {
44                 stack_sz = -stack_sz;
45                 if (stack_sz > MAX_BPF_STACK_SIZE)
46                         rc = -ERANGE;
47                 else
48                         bpf->stack_sz = stack_sz;
49         }
50
51         if (rc != 0)
52                 RTE_BPF_LOG(ERR, "%s(%p) failed, error code: %d;\n",
53                         __func__, bpf, rc);
54         return rc;
55 }