Branch data Line data Source code
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 <eal_export.h>
11 : : #include "rte_bpf.h"
12 : : #include "bpf_def.h"
13 : :
14 : : #define BPF_OP_INDEX(x) (BPF_OP(x) >> 4)
15 : : #define BPF_SIZE_INDEX(x) (BPF_SIZE(x) >> 3)
16 : :
17 : : static const char *const class_tbl[] = {
18 : : [BPF_LD] = "ld", [BPF_LDX] = "ldx", [BPF_ST] = "st",
19 : : [BPF_STX] = "stx", [BPF_ALU] = "alu", [BPF_JMP] = "jmp",
20 : : [BPF_RET] = "ret", [BPF_MISC] = "alu64",
21 : : };
22 : :
23 : : static const char *const alu_op_tbl[16] = {
24 : : [BPF_ADD >> 4] = "add", [BPF_SUB >> 4] = "sub",
25 : : [BPF_MUL >> 4] = "mul", [BPF_DIV >> 4] = "div",
26 : : [BPF_OR >> 4] = "or", [BPF_AND >> 4] = "and",
27 : : [BPF_LSH >> 4] = "lsh", [BPF_RSH >> 4] = "rsh",
28 : : [BPF_NEG >> 4] = "neg", [BPF_MOD >> 4] = "mod",
29 : : [BPF_XOR >> 4] = "xor", [EBPF_MOV >> 4] = "mov",
30 : : [EBPF_ARSH >> 4] = "arsh", [EBPF_END >> 4] = "endian",
31 : : };
32 : :
33 : : static const char *const size_tbl[] = {
34 : : [BPF_W >> 3] = "w",
35 : : [BPF_H >> 3] = "h",
36 : : [BPF_B >> 3] = "b",
37 : : [EBPF_DW >> 3] = "dw",
38 : : };
39 : :
40 : : static const char *const jump_tbl[16] = {
41 : : [BPF_JA >> 4] = "ja", [BPF_JEQ >> 4] = "jeq",
42 : : [BPF_JGT >> 4] = "jgt", [BPF_JGE >> 4] = "jge",
43 : : [BPF_JSET >> 4] = "jset", [EBPF_JNE >> 4] = "jne",
44 : : [EBPF_JSGT >> 4] = "jsgt", [EBPF_JSGE >> 4] = "jsge",
45 : : [EBPF_CALL >> 4] = "call", [EBPF_EXIT >> 4] = "exit",
46 : : [EBPF_JLT >> 4] = "jlt", [EBPF_JLE >> 4] = "jle",
47 : : [EBPF_JSLT >> 4] = "jslt", [EBPF_JSLE >> 4] = "jsle",
48 : : };
49 : :
50 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_bpf_insn_is_wide, 26.07)
51 : : bool
52 : 0 : rte_bpf_insn_is_wide(const struct rte_ebpf_insn *ins)
53 : : {
54 : 0 : return ins->code == (BPF_LD | BPF_IMM | EBPF_DW);
55 : : }
56 : :
57 : :
58 : : /* Format one (possibly wide) eBPF command as hexadecimal in objdump format. */
59 : : static int
60 : 0 : format_hexadecimal(char *buffer, size_t bufsz, const struct rte_ebpf_insn *ins,
61 : : uint32_t flags)
62 : : {
63 : : const char *const b = (const char *)ins;
64 : :
65 : : RTE_ASSERT((flags & RTE_BPF_FORMAT_FLAG_HEXADECIMAL) != 0);
66 : :
67 : : RTE_BUILD_BUG_ON(sizeof(*ins) != 8);
68 : :
69 [ # # # # ]: 0 : if ((flags & RTE_BPF_FORMAT_FLAG_NEVER_WIDE) == 0 && rte_bpf_insn_is_wide(ins))
70 : 0 : return snprintf(buffer, bufsz,
71 : : "%02hhx %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx "
72 : : "%02hhx %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx",
73 : 0 : b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7],
74 : 0 : b[8], b[9], b[10], b[11], b[12], b[13], b[14], b[15]);
75 : : else
76 : 0 : return snprintf(buffer, bufsz,
77 : : "%02hhx %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx %02hhx",
78 : 0 : b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]);
79 : : }
80 : :
81 : : /* Return atomic subcommand mnemonic based on BPF_STX immediate. */
82 : : static inline const char *
83 : : atomic_op(int32_t imm)
84 : : {
85 : 0 : switch (imm) {
86 : : case BPF_ATOMIC_ADD:
87 : : return "xadd";
88 : 0 : case BPF_ATOMIC_XCHG:
89 : 0 : return "xchg";
90 : : default:
91 : : return NULL;
92 : : }
93 : : }
94 : :
95 : : /* Format one (possibly wide) eBPF command as assembler. */
96 : : static int
97 : 0 : format_disassembly(char *buffer, size_t bufsz, const struct rte_ebpf_insn *ins,
98 : : uint32_t pc, uint32_t flags)
99 : : {
100 : 0 : uint8_t cls = BPF_CLASS(ins->code);
101 : : const char *op, *postfix = "", *warning = "";
102 : : char jump[16];
103 : :
104 : : RTE_ASSERT((flags & RTE_BPF_FORMAT_FLAG_HEXADECIMAL) == 0);
105 : :
106 [ # # # # : 0 : switch (cls) {
# # # # ]
107 : : default:
108 : : return snprintf(buffer, bufsz, "unimp 0x%x // class: %s",
109 : : ins->code, class_tbl[cls]);
110 : 0 : case BPF_ALU:
111 : : postfix = "32";
112 : : /* fall through */
113 : 0 : case EBPF_ALU64:
114 : 0 : op = alu_op_tbl[BPF_OP_INDEX(ins->code)];
115 [ # # ]: 0 : if (ins->off != 0)
116 : : /* Not yet supported variation with non-zero offset. */
117 : : warning = ", off != 0";
118 [ # # ]: 0 : if (BPF_SRC(ins->code) == BPF_X)
119 : 0 : return snprintf(buffer, bufsz, "%s%s r%u, r%u%s", op, postfix, ins->dst_reg,
120 : 0 : ins->src_reg, warning);
121 : : else
122 : 0 : return snprintf(buffer, bufsz, "%s%s r%u, #0x%x%s", op, postfix,
123 : 0 : ins->dst_reg, ins->imm, warning);
124 : 0 : case BPF_LD:
125 : : op = "ld";
126 : 0 : postfix = size_tbl[BPF_SIZE_INDEX(ins->code)];
127 [ # # ]: 0 : if (ins->code == (BPF_LD | BPF_IMM | EBPF_DW)) {
128 : : uint64_t val;
129 : :
130 [ # # ]: 0 : if (ins->src_reg != 0)
131 : : /* Not yet supported variation with non-zero src. */
132 : : warning = ", src != 0";
133 : 0 : val = (uint32_t)ins[0].imm |
134 : 0 : (uint64_t)(uint32_t)ins[1].imm << 32;
135 : 0 : return snprintf(buffer, bufsz, "%s%s r%d, #0x%"PRIx64"%s",
136 : 0 : op, postfix, ins->dst_reg, val, warning);
137 : : }
138 [ # # # # ]: 0 : switch (BPF_MODE(ins->code)) {
139 : 0 : case BPF_IMM:
140 : 0 : return snprintf(buffer, bufsz, "%s%s r%d, #0x%x", op, postfix,
141 : 0 : ins->dst_reg, ins->imm);
142 : 0 : case BPF_ABS:
143 : 0 : return snprintf(buffer, bufsz, "%s%s r%d, [%d]", op, postfix,
144 : 0 : ins->dst_reg, ins->imm);
145 : 0 : case BPF_IND:
146 : 0 : return snprintf(buffer, bufsz, "%s%s r%d, [r%u + %d]", op, postfix,
147 : 0 : ins->dst_reg, ins->src_reg, ins->imm);
148 : 0 : default:
149 : 0 : return snprintf(buffer, bufsz, "// BUG: LD opcode 0x%02x in eBPF insns",
150 : : ins->code);
151 : : }
152 : 0 : case BPF_LDX:
153 : : op = "ldx";
154 : 0 : postfix = size_tbl[BPF_SIZE_INDEX(ins->code)];
155 [ # # ]: 0 : if (BPF_MODE(ins->code) == BPF_MEM)
156 : 0 : return snprintf(buffer, bufsz, "%s%s r%d, [r%u + %d]", op, postfix,
157 : 0 : ins->dst_reg, ins->src_reg, ins->off);
158 : : else
159 : 0 : return snprintf(buffer, bufsz, "// BUG: LDX opcode 0x%02x in eBPF insns",
160 : : ins->code);
161 : 0 : case BPF_ST:
162 : : op = "st";
163 : 0 : postfix = size_tbl[BPF_SIZE_INDEX(ins->code)];
164 [ # # ]: 0 : if (BPF_MODE(ins->code) == BPF_MEM)
165 : 0 : return snprintf(buffer, bufsz, "%s%s [r%d + %d], #0x%x", op, postfix,
166 : 0 : ins->dst_reg, ins->off, ins->imm);
167 : : else
168 : 0 : return snprintf(buffer, bufsz, "// BUG: ST opcode 0x%02x in eBPF insns",
169 : : ins->code);
170 : 0 : case BPF_STX:
171 [ # # # ]: 0 : switch (BPF_MODE(ins->code)) {
172 : : case BPF_MEM:
173 : : op = "stx";
174 : : break;
175 : 0 : case EBPF_ATOMIC:
176 [ # # # ]: 0 : op = atomic_op(ins->imm);
177 : : if (op == NULL)
178 : 0 : return snprintf(buffer, bufsz,
179 : : "// BUG: ATOMIC operation 0x%x in eBPF insns", ins->imm);
180 : : break;
181 : 0 : default:
182 : 0 : return snprintf(buffer, bufsz, "// BUG: STX opcode 0x%02x in eBPF insns",
183 : : ins->code);
184 : : }
185 : 0 : postfix = size_tbl[BPF_SIZE_INDEX(ins->code)];
186 : 0 : return snprintf(buffer, bufsz, "%s%s [r%d + %d], r%u", op, postfix,
187 : 0 : ins->dst_reg, ins->off, ins->src_reg);
188 : 0 : case BPF_JMP:
189 : 0 : op = jump_tbl[BPF_OP_INDEX(ins->code)];
190 [ # # ]: 0 : if (op == NULL)
191 : 0 : return snprintf(buffer, bufsz, "invalid jump opcode: %#x", ins->code);
192 : :
193 [ # # ]: 0 : if ((flags & RTE_BPF_FORMAT_FLAG_ABSOLUTE_JUMPS) != 0)
194 : 0 : snprintf(jump, sizeof(jump), "L%d", pc + 1 + ins->off);
195 : : else
196 : 0 : snprintf(jump, sizeof(jump), "%+d", (int)ins->off);
197 : :
198 [ # # ]: 0 : if (ins->src_reg != 0)
199 : : /* Not yet supported variation with non-zero src w/o condition. */
200 : : warning = ", src != 0";
201 [ # # # # ]: 0 : switch (BPF_OP(ins->code)) {
202 : : case BPF_JA:
203 : 0 : return snprintf(buffer, bufsz, "%s %s%s", op, jump, warning);
204 : 0 : case EBPF_CALL:
205 : : /* Call of helper function with index in immediate. */
206 : 0 : return snprintf(buffer, bufsz, "%s #%u%s", op, ins->imm, warning);
207 : : case EBPF_EXIT:
208 : 0 : return snprintf(buffer, bufsz, "%s%s", op, warning);
209 : : }
210 : :
211 [ # # ]: 0 : if (BPF_SRC(ins->code) == BPF_X)
212 : 0 : return snprintf(buffer, bufsz, "%s r%u, r%u, %s", op, ins->dst_reg,
213 : 0 : ins->src_reg, jump);
214 : : else
215 : 0 : return snprintf(buffer, bufsz, "%s r%u, #0x%x, %s", op, ins->dst_reg,
216 : 0 : ins->imm, jump);
217 : 0 : case BPF_RET:
218 : 0 : return snprintf(buffer, bufsz, "// BUG: RET opcode 0x%02x in eBPF insns",
219 : : ins->code);
220 : : }
221 : : }
222 : :
223 : : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_bpf_format, 26.07)
224 : : int
225 : 0 : rte_bpf_format(char *buffer, size_t bufsz, const struct rte_ebpf_insn *ins,
226 : : uint32_t pc, uint32_t flags)
227 : : {
228 [ # # ]: 0 : if ((flags & RTE_BPF_FORMAT_FLAG_HEXADECIMAL) != 0)
229 : 0 : return format_hexadecimal(buffer, bufsz, ins, flags);
230 : : else
231 : 0 : return format_disassembly(buffer, bufsz, ins, pc, flags);
232 : : }
233 : :
234 : : RTE_EXPORT_SYMBOL(rte_bpf_dump)
235 : 0 : void rte_bpf_dump(FILE *f, const struct rte_ebpf_insn *buf, uint32_t len)
236 : : {
237 : : uint32_t i;
238 : : char buffer[256];
239 : :
240 [ # # ]: 0 : for (i = 0; i < len; ++i) {
241 : 0 : const struct rte_ebpf_insn *ins = buf + i;
242 : :
243 : 0 : format_disassembly(buffer, sizeof(buffer), ins, i,
244 : : RTE_BPF_FORMAT_FLAG_DISASSEMBLY |
245 : : RTE_BPF_FORMAT_FLAG_ABSOLUTE_JUMPS);
246 : : fprintf(f, " L%u:\t%s\n", i, buffer);
247 : 0 : i += rte_bpf_insn_is_wide(ins);
248 : : }
249 : 0 : }
|