bpf: add function to dump eBPF instructions
[dpdk.git] / lib / bpf / bpf_dump.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2021 Stephen Hemminger
3  * Based on filter2xdp
4  * Copyright (C) 2017 Tobias Klauser
5  */
6
7 #include <stdio.h>
8 #include <stdint.h>
9
10 #include "rte_bpf.h"
11
12 #define BPF_OP_INDEX(x) (BPF_OP(x) >> 4)
13 #define BPF_SIZE_INDEX(x) (BPF_SIZE(x) >> 3)
14
15 static const char *const class_tbl[] = {
16         [BPF_LD] = "ld",   [BPF_LDX] = "ldx",    [BPF_ST] = "st",
17         [BPF_STX] = "stx", [BPF_ALU] = "alu",    [BPF_JMP] = "jmp",
18         [BPF_RET] = "ret", [BPF_MISC] = "alu64",
19 };
20
21 static const char *const alu_op_tbl[16] = {
22         [BPF_ADD >> 4] = "add",    [BPF_SUB >> 4] = "sub",
23         [BPF_MUL >> 4] = "mul",    [BPF_DIV >> 4] = "div",
24         [BPF_OR >> 4] = "or",      [BPF_AND >> 4] = "and",
25         [BPF_LSH >> 4] = "lsh",    [BPF_RSH >> 4] = "rsh",
26         [BPF_NEG >> 4] = "neg",    [BPF_MOD >> 4] = "mod",
27         [BPF_XOR >> 4] = "xor",    [EBPF_MOV >> 4] = "mov",
28         [EBPF_ARSH >> 4] = "arsh", [EBPF_END >> 4] = "endian",
29 };
30
31 static const char *const size_tbl[] = {
32         [BPF_W >> 3] = "w",
33         [BPF_H >> 3] = "h",
34         [BPF_B >> 3] = "b",
35         [EBPF_DW >> 3] = "dw",
36 };
37
38 static const char *const jump_tbl[16] = {
39         [BPF_JA >> 4] = "ja",      [BPF_JEQ >> 4] = "jeq",
40         [BPF_JGT >> 4] = "jgt",    [BPF_JGE >> 4] = "jge",
41         [BPF_JSET >> 4] = "jset",  [EBPF_JNE >> 4] = "jne",
42         [EBPF_JSGT >> 4] = "jsgt", [EBPF_JSGE >> 4] = "jsge",
43         [EBPF_CALL >> 4] = "call", [EBPF_EXIT >> 4] = "exit",
44 };
45
46 void rte_bpf_dump(FILE *f, const struct ebpf_insn *buf, uint32_t len)
47 {
48         uint32_t i;
49
50         for (i = 0; i < len; ++i) {
51                 const struct ebpf_insn *ins = buf + i;
52                 uint8_t cls = BPF_CLASS(ins->code);
53                 const char *op, *postfix = "";
54
55                 fprintf(f, " L%u:\t", i);
56
57                 switch (cls) {
58                 default:
59                         fprintf(f, "unimp 0x%x // class: %s\n",
60                                 ins->code, class_tbl[cls]);
61                         break;
62                 case BPF_ALU:
63                         postfix = "32";
64                         /* fall through */
65                 case EBPF_ALU64:
66                         op = alu_op_tbl[BPF_OP_INDEX(ins->code)];
67                         if (BPF_SRC(ins->code) == BPF_X)
68                                 fprintf(f, "%s%s r%u, r%u\n", op, postfix, ins->dst_reg,
69                                         ins->src_reg);
70                         else
71                                 fprintf(f, "%s%s r%u, #0x%x\n", op, postfix,
72                                         ins->dst_reg, ins->imm);
73                         break;
74                 case BPF_LD:
75                         op = "ld";
76                         postfix = size_tbl[BPF_SIZE_INDEX(ins->code)];
77                         if (ins->code == (BPF_LD | BPF_IMM | EBPF_DW)) {
78                                 uint64_t val;
79
80                                 val = (uint32_t)ins[0].imm |
81                                         (uint64_t)(uint32_t)ins[1].imm << 32;
82                                 fprintf(f, "%s%s r%d, #0x%"PRIx64"\n",
83                                         op, postfix, ins->dst_reg, val);
84                                 i++;
85                         } else if (BPF_MODE(ins->code) == BPF_IMM)
86                                 fprintf(f, "%s%s r%d, #0x%x\n", op, postfix,
87                                         ins->dst_reg, ins->imm);
88                         else if (BPF_MODE(ins->code) == BPF_ABS)
89                                 fprintf(f, "%s%s r%d, [%d]\n", op, postfix,
90                                         ins->dst_reg, ins->imm);
91                         else if (BPF_MODE(ins->code) == BPF_IND)
92                                 fprintf(f, "%s%s r%d, [r%u + %d]\n", op, postfix,
93                                         ins->dst_reg, ins->src_reg, ins->imm);
94                         else
95                                 fprintf(f, "// BUG: LD opcode 0x%02x in eBPF insns\n",
96                                         ins->code);
97                         break;
98                 case BPF_LDX:
99                         op = "ldx";
100                         postfix = size_tbl[BPF_SIZE_INDEX(ins->code)];
101                         fprintf(f, "%s%s r%d, [r%u + %d]\n", op, postfix, ins->dst_reg,
102                                 ins->src_reg, ins->off);
103                         break;
104                 case BPF_ST:
105                         op = "st";
106                         postfix = size_tbl[BPF_SIZE_INDEX(ins->code)];
107                         if (BPF_MODE(ins->code) == BPF_MEM)
108                                 fprintf(f, "%s%s [r%d + %d], #0x%x\n", op, postfix,
109                                         ins->dst_reg, ins->off, ins->imm);
110                         else
111                                 fprintf(f, "// BUG: ST opcode 0x%02x in eBPF insns\n",
112                                         ins->code);
113                         break;
114                 case BPF_STX:
115                         op = "stx";
116                         postfix = size_tbl[BPF_SIZE_INDEX(ins->code)];
117                         fprintf(f, "%s%s [r%d + %d], r%u\n", op, postfix,
118                                 ins->dst_reg, ins->off, ins->src_reg);
119                         break;
120 #define L(pc, off) ((int)(pc) + 1 + (off))
121                 case BPF_JMP:
122                         op = jump_tbl[BPF_OP_INDEX(ins->code)];
123                         if (op == NULL)
124                                 fprintf(f, "invalid jump opcode: %#x\n", ins->code);
125                         else if (BPF_OP(ins->code) == BPF_JA)
126                                 fprintf(f, "%s L%d\n", op, L(i, ins->off));
127                         else if (BPF_OP(ins->code) == EBPF_EXIT)
128                                 fprintf(f, "%s\n", op);
129                         else
130                                 fprintf(f, "%s r%u, #0x%x, L%d\n", op, ins->dst_reg,
131                                         ins->imm, L(i, ins->off));
132                         break;
133                 case BPF_RET:
134                         fprintf(f, "// BUG: RET opcode 0x%02x in eBPF insns\n",
135                                 ins->code);
136                         break;
137                 }
138         }
139 }