LCOV - code coverage report
Current view: top level - lib/bpf - bpf_dump.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 48 71 67.6 %
Date: 2026-04-01 20:02:27 Functions: 1 1 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 28 47 59.6 %

           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                 :            : static inline const char *
      48                 :            : atomic_op(int32_t imm)
      49                 :            : {
      50                 :          0 :         switch (imm) {
      51                 :            :         case BPF_ATOMIC_ADD:
      52                 :            :                 return "xadd";
      53                 :          0 :         case BPF_ATOMIC_XCHG:
      54                 :          0 :                 return "xchg";
      55                 :            :         default:
      56                 :            :                 return NULL;
      57                 :            :         }
      58                 :            : }
      59                 :            : 
      60                 :            : RTE_EXPORT_SYMBOL(rte_bpf_dump)
      61                 :         26 : void rte_bpf_dump(FILE *f, const struct ebpf_insn *buf, uint32_t len)
      62                 :            : {
      63                 :            :         uint32_t i;
      64                 :            : 
      65         [ +  + ]:        887 :         for (i = 0; i < len; ++i) {
      66                 :        861 :                 const struct ebpf_insn *ins = buf + i;
      67                 :        861 :                 uint8_t cls = BPF_CLASS(ins->code);
      68                 :            :                 const char *op, *postfix = "", *warning = "";
      69                 :            : 
      70                 :            :                 fprintf(f, " L%u:\t", i);
      71                 :            : 
      72   [ +  +  +  +  :        861 :                 switch (cls) {
             -  +  +  - ]
      73                 :            :                 default:
      74                 :            :                         fprintf(f, "unimp 0x%x // class: %s\n",
      75                 :            :                                 ins->code, class_tbl[cls]);
      76                 :            :                         break;
      77                 :        154 :                 case BPF_ALU:
      78                 :            :                         postfix = "32";
      79                 :            :                         /* fall through */
      80                 :        282 :                 case EBPF_ALU64:
      81                 :        282 :                         op = alu_op_tbl[BPF_OP_INDEX(ins->code)];
      82         [ -  + ]:        282 :                         if (ins->off != 0)
      83                 :            :                                 /* Not yet supported variation with non-zero offset. */
      84                 :            :                                 warning = ", off != 0";
      85         [ +  + ]:        282 :                         if (BPF_SRC(ins->code) == BPF_X)
      86                 :        136 :                                 fprintf(f, "%s%s r%u, r%u%s\n", op, postfix, ins->dst_reg,
      87                 :        136 :                                         ins->src_reg, warning);
      88                 :            :                         else
      89                 :        146 :                                 fprintf(f, "%s%s r%u, #0x%x%s\n", op, postfix,
      90                 :        146 :                                         ins->dst_reg, ins->imm, warning);
      91                 :            :                         break;
      92                 :        233 :                 case BPF_LD:
      93                 :            :                         op = "ld";
      94                 :        233 :                         postfix = size_tbl[BPF_SIZE_INDEX(ins->code)];
      95         [ -  + ]:        233 :                         if (ins->code == (BPF_LD | BPF_IMM | EBPF_DW)) {
      96                 :            :                                 uint64_t val;
      97                 :            : 
      98         [ #  # ]:          0 :                                 if (ins->src_reg != 0)
      99                 :            :                                         /* Not yet supported variation with non-zero src. */
     100                 :            :                                         warning = ", src != 0";
     101                 :          0 :                                 val = (uint32_t)ins[0].imm |
     102                 :          0 :                                         (uint64_t)(uint32_t)ins[1].imm << 32;
     103                 :          0 :                                 fprintf(f, "%s%s r%d, #0x%"PRIx64"%s\n",
     104                 :          0 :                                         op, postfix, ins->dst_reg, val, warning);
     105                 :          0 :                                 i++;
     106         [ -  + ]:        233 :                         } else if (BPF_MODE(ins->code) == BPF_IMM)
     107                 :          0 :                                 fprintf(f, "%s%s r%d, #0x%x\n", op, postfix,
     108                 :          0 :                                         ins->dst_reg, ins->imm);
     109         [ +  + ]:        233 :                         else if (BPF_MODE(ins->code) == BPF_ABS)
     110                 :        206 :                                 fprintf(f, "%s%s r%d, [%d]\n", op, postfix,
     111                 :        206 :                                         ins->dst_reg, ins->imm);
     112         [ +  - ]:         27 :                         else if (BPF_MODE(ins->code) == BPF_IND)
     113                 :         27 :                                 fprintf(f, "%s%s r%d, [r%u + %d]\n", op, postfix,
     114                 :         27 :                                         ins->dst_reg, ins->src_reg, ins->imm);
     115                 :            :                         else
     116                 :            :                                 fprintf(f, "// BUG: LD opcode 0x%02x in eBPF insns\n",
     117                 :            :                                         ins->code);
     118                 :            :                         break;
     119                 :          3 :                 case BPF_LDX:
     120                 :            :                         op = "ldx";
     121                 :          3 :                         postfix = size_tbl[BPF_SIZE_INDEX(ins->code)];
     122         [ +  - ]:          3 :                         if (BPF_MODE(ins->code) == BPF_MEM)
     123                 :          3 :                                 fprintf(f, "%s%s r%d, [r%u + %d]\n", op, postfix, ins->dst_reg,
     124                 :          3 :                                         ins->src_reg, ins->off);
     125                 :            :                         else
     126                 :            :                                 fprintf(f, "// BUG: LDX opcode 0x%02x in eBPF insns\n",
     127                 :            :                                         ins->code);
     128                 :            :                         break;
     129                 :          0 :                 case BPF_ST:
     130                 :            :                         op = "st";
     131                 :          0 :                         postfix = size_tbl[BPF_SIZE_INDEX(ins->code)];
     132         [ #  # ]:          0 :                         if (BPF_MODE(ins->code) == BPF_MEM)
     133                 :          0 :                                 fprintf(f, "%s%s [r%d + %d], #0x%x\n", op, postfix,
     134                 :          0 :                                         ins->dst_reg, ins->off, ins->imm);
     135                 :            :                         else
     136                 :            :                                 fprintf(f, "// BUG: ST opcode 0x%02x in eBPF insns\n",
     137                 :            :                                         ins->code);
     138                 :            :                         break;
     139                 :          2 :                 case BPF_STX:
     140         [ -  + ]:          2 :                         if (BPF_MODE(ins->code) == BPF_MEM)
     141                 :            :                                 op = "stx";
     142         [ #  # ]:          0 :                         else if (BPF_MODE(ins->code) == EBPF_ATOMIC) {
     143      [ #  #  # ]:          0 :                                 op = atomic_op(ins->imm);
     144                 :            :                                 if (op == NULL) {
     145                 :            :                                         fprintf(f, "// BUG: ATOMIC operation 0x%x in eBPF insns\n",
     146                 :            :                                                 ins->imm);
     147                 :            :                                         break;
     148                 :            :                                 }
     149                 :            :                         } else {
     150                 :            :                                 fprintf(f, "// BUG: STX opcode 0x%02x in eBPF insns\n",
     151                 :            :                                         ins->code);
     152                 :            :                                 break;
     153                 :            :                         }
     154                 :          2 :                         postfix = size_tbl[BPF_SIZE_INDEX(ins->code)];
     155                 :          2 :                         fprintf(f, "%s%s [r%d + %d], r%u\n", op, postfix,
     156                 :          2 :                                 ins->dst_reg, ins->off, ins->src_reg);
     157                 :            :                         break;
     158                 :            : #define L(pc, off) ((int)(pc) + 1 + (off))
     159                 :        341 :                 case BPF_JMP:
     160                 :        341 :                         op = jump_tbl[BPF_OP_INDEX(ins->code)];
     161         [ +  + ]:        341 :                         if (ins->src_reg != 0)
     162                 :            :                                 /* Not yet supported variation with non-zero src w/o condition. */
     163                 :            :                                 warning = ", src != 0";
     164         [ -  + ]:        341 :                         if (op == NULL)
     165                 :          0 :                                 fprintf(f, "invalid jump opcode: %#x\n", ins->code);
     166         [ +  + ]:        341 :                         else if (BPF_OP(ins->code) == BPF_JA)
     167                 :         20 :                                 fprintf(f, "%s L%d%s\n", op, L(i, ins->off), warning);
     168         [ -  + ]:        321 :                         else if (BPF_OP(ins->code) == EBPF_CALL)
     169                 :            :                                 /* Call of helper function with index in immediate. */
     170                 :          0 :                                 fprintf(f, "%s #%u%s\n", op, ins->imm, warning);
     171         [ +  + ]:        321 :                         else if (BPF_OP(ins->code) == EBPF_EXIT)
     172                 :            :                                 fprintf(f, "%s%s\n", op, warning);
     173         [ +  + ]:        269 :                         else if (BPF_SRC(ins->code) == BPF_X)
     174                 :         35 :                                 fprintf(f, "%s r%u, r%u, L%d\n", op, ins->dst_reg,
     175                 :         35 :                                         ins->src_reg, L(i, ins->off));
     176                 :            :                         else
     177                 :        234 :                                 fprintf(f, "%s r%u, #0x%x, L%d\n", op, ins->dst_reg,
     178                 :        234 :                                         ins->imm, L(i, ins->off));
     179                 :            :                         break;
     180                 :          0 :                 case BPF_RET:
     181                 :          0 :                         fprintf(f, "// BUG: RET opcode 0x%02x in eBPF insns\n",
     182                 :          0 :                                 ins->code);
     183                 :            :                         break;
     184                 :            :                 }
     185                 :            :         }
     186                 :         26 : }

Generated by: LCOV version 1.14