LCOV - code coverage report
Current view: top level - lib/bpf - bpf_dump.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 40 56 71.4 %
Date: 2025-05-01 17:49:45 Functions: 1 1 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 20 28 71.4 %

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

Generated by: LCOV version 1.14