LCOV - code coverage report
Current view: top level - lib/bpf - bpf_exec.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 253 372 68.0 %
Date: 2026-11-01 19:11:22 Functions: 6 9 66.7 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 121 224 54.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2018 Intel Corporation
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <stdio.h>
       6                 :            : #include <stdint.h>
       7                 :            : #include <limits.h>
       8                 :            : 
       9                 :            : #include <eal_export.h>
      10                 :            : #include <rte_common.h>
      11                 :            : #include <rte_log.h>
      12                 :            : #include <rte_debug.h>
      13                 :            : #include <rte_byteorder.h>
      14                 :            : #include <rte_errno.h>
      15                 :            : #include <rte_stdatomic.h>
      16                 :            : 
      17                 :            : #include "bpf_impl.h"
      18                 :            : 
      19                 :            : #define BPF_JMP_UNC(ins)        ((ins) += (ins)->off)
      20                 :            : 
      21                 :            : #define BPF_JMP_CND_REG(reg, ins, op, type)     \
      22                 :            :         ((ins) += \
      23                 :            :                 ((type)(reg)[(ins)->dst_reg] op (type)(reg)[(ins)->src_reg]) ? \
      24                 :            :                 (ins)->off : 0)
      25                 :            : 
      26                 :            : #define BPF_JMP_CND_IMM(reg, ins, op, type)     \
      27                 :            :         ((ins) += \
      28                 :            :                 ((type)(reg)[(ins)->dst_reg] op (type)(ins)->imm) ? \
      29                 :            :                 (ins)->off : 0)
      30                 :            : 
      31                 :            : #define BPF_NEG_ALU(reg, ins, type)     \
      32                 :            :         ((reg)[(ins)->dst_reg] = (type)(-(reg)[(ins)->dst_reg]))
      33                 :            : 
      34                 :            : #define EBPF_MOV_ALU_REG(reg, ins, type)        \
      35                 :            :         ((reg)[(ins)->dst_reg] = (type)(reg)[(ins)->src_reg])
      36                 :            : 
      37                 :            : #define BPF_OP_ALU_REG(reg, ins, op, type)      \
      38                 :            :         ((reg)[(ins)->dst_reg] = \
      39                 :            :                 (type)(reg)[(ins)->dst_reg] op (type)(reg)[(ins)->src_reg])
      40                 :            : 
      41                 :            : #define EBPF_MOV_ALU_IMM(reg, ins, type)        \
      42                 :            :         ((reg)[(ins)->dst_reg] = (type)(ins)->imm)
      43                 :            : 
      44                 :            : #define BPF_OP_ALU_IMM(reg, ins, op, type)      \
      45                 :            :         ((reg)[(ins)->dst_reg] = \
      46                 :            :                 (type)(reg)[(ins)->dst_reg] op (type)(ins)->imm)
      47                 :            : 
      48                 :            : #define BPF_OP_SHIFT_IMM(reg, ins, op, type)    \
      49                 :            :         ((reg)[(ins)->dst_reg] =             \
      50                 :            :                 (type)(reg)[(ins)->dst_reg] op       \
      51                 :            :                 ((ins)->imm & (sizeof(type) * CHAR_BIT - 1)))
      52                 :            : 
      53                 :            : #define BPF_OP_SHIFT_REG(reg, ins, op, type)    \
      54                 :            :         ((reg)[(ins)->dst_reg] =             \
      55                 :            :                 (type)(reg)[(ins)->dst_reg] op       \
      56                 :            :                 ((reg)[(ins)->src_reg] & (sizeof(type) * CHAR_BIT - 1)))
      57                 :            : 
      58                 :            : #define BPF_DIV_ZERO_CHECK(bpf, reg, ins, type) do { \
      59                 :            :         if ((type)(reg)[(ins)->src_reg] == 0) { \
      60                 :            :                 RTE_BPF_LOG_LINE(ERR, \
      61                 :            :                         "%s(%p): division by 0 at pc: %#zx;", \
      62                 :            :                         __func__, bpf, \
      63                 :            :                         (uintptr_t)(ins) - (uintptr_t)(bpf)->prm.raw.ins); \
      64                 :            :                 return 0; \
      65                 :            :         } \
      66                 :            : } while (0)
      67                 :            : 
      68                 :            : #define BPF_LD_REG(reg, ins, type)      \
      69                 :            :         ((reg)[(ins)->dst_reg] = \
      70                 :            :                 *(type *)(uintptr_t)((reg)[(ins)->src_reg] + (ins)->off))
      71                 :            : 
      72                 :            : #define BPF_ST_IMM(reg, ins, type)      \
      73                 :            :         (*(type *)(uintptr_t)((reg)[(ins)->dst_reg] + (ins)->off) = \
      74                 :            :                 (type)(ins)->imm)
      75                 :            : 
      76                 :            : #define BPF_ST_REG(reg, ins, type)      \
      77                 :            :         (*(type *)(uintptr_t)((reg)[(ins)->dst_reg] + (ins)->off) = \
      78                 :            :                 (type)(reg)[(ins)->src_reg])
      79                 :            : 
      80                 :            : #define BPF_ST_ATOMIC_REG(reg, ins, tp) do { \
      81                 :            :         RTE_ATOMIC(uint##tp##_t) *dst = (RTE_ATOMIC(uint##tp##_t) *) \
      82                 :            :                 (uintptr_t)((reg)[(ins)->dst_reg] + (ins)->off); \
      83                 :            :         switch (ins->imm) { \
      84                 :            :         case BPF_ATOMIC_ADD: \
      85                 :            :                 rte_atomic_fetch_add_explicit(dst, \
      86                 :            :                         (reg)[(ins)->src_reg], rte_memory_order_seq_cst); \
      87                 :            :                 break; \
      88                 :            :         case BPF_ATOMIC_XCHG: \
      89                 :            :                 (reg)[(ins)->src_reg] = rte_atomic_exchange_explicit(dst, \
      90                 :            :                         (reg)[(ins)->src_reg], rte_memory_order_seq_cst); \
      91                 :            :                 break; \
      92                 :            :         default: \
      93                 :            :                 /* this should be caught by validator and never reach here */ \
      94                 :            :                 RTE_BPF_LOG_LINE(ERR, \
      95                 :            :                         "%s(%p): unsupported atomic operation at pc: %#zx;", \
      96                 :            :                         __func__, bpf, \
      97                 :            :                         (uintptr_t)(ins) - (uintptr_t)(bpf)->prm.raw.ins); \
      98                 :            :                 return 0; \
      99                 :            :         } \
     100                 :            : } while (0)
     101                 :            : 
     102                 :            : /* BPF_LD | BPF_ABS/BPF_IND */
     103                 :            : 
     104                 :            : #define NOP(x)  (x)
     105                 :            : 
     106                 :            : #define BPF_LD_ABS(bpf, reg, ins, type, op) do { \
     107                 :            :         const type *p = bpf_ld_mbuf(bpf, reg, ins, (ins)->imm, sizeof(type)); \
     108                 :            :         if (p == NULL)  \
     109                 :            :                 return 0; \
     110                 :            :         reg[EBPF_REG_0] = op(p[0]); \
     111                 :            : } while (0)
     112                 :            : 
     113                 :            : #define BPF_LD_IND(bpf, reg, ins, type, op) do { \
     114                 :            :         uint32_t ofs = reg[ins->src_reg] + (ins)->imm; \
     115                 :            :         const type *p = bpf_ld_mbuf(bpf, reg, ins, ofs, sizeof(type)); \
     116                 :            :         if (p == NULL)  \
     117                 :            :                 return 0; \
     118                 :            :         reg[EBPF_REG_0] = op(p[0]); \
     119                 :            : } while (0)
     120                 :            : 
     121                 :            : 
     122                 :            : static inline void
     123                 :          5 : bpf_alu_be(uint64_t reg[EBPF_REG_NUM], const struct rte_ebpf_insn *ins)
     124                 :            : {
     125                 :            :         uint64_t *v;
     126                 :            : 
     127                 :          5 :         v = reg + ins->dst_reg;
     128   [ +  +  +  - ]:          5 :         switch (ins->imm) {
     129                 :          1 :         case 16:
     130         [ -  + ]:          1 :                 *v = rte_cpu_to_be_16(*v);
     131                 :          1 :                 break;
     132                 :          3 :         case 32:
     133         [ -  + ]:          3 :                 *v = rte_cpu_to_be_32(*v);
     134                 :          3 :                 break;
     135                 :          1 :         case 64:
     136         [ -  + ]:          1 :                 *v = rte_cpu_to_be_64(*v);
     137                 :          1 :                 break;
     138                 :            :         }
     139                 :          5 : }
     140                 :            : 
     141                 :            : static inline void
     142                 :            : bpf_alu_le(uint64_t reg[EBPF_REG_NUM], const struct rte_ebpf_insn *ins)
     143                 :            : {
     144                 :            :         uint64_t *v;
     145                 :            : 
     146                 :            :         v = reg + ins->dst_reg;
     147                 :            :         switch (ins->imm) {
     148                 :            :         case 16:
     149                 :            :                 *v = rte_cpu_to_le_16(*v);
     150                 :            :                 break;
     151                 :            :         case 32:
     152                 :            :                 *v = rte_cpu_to_le_32(*v);
     153                 :            :                 break;
     154                 :            :         case 64:
     155                 :            :                 *v = rte_cpu_to_le_64(*v);
     156                 :            :                 break;
     157                 :            :         }
     158                 :            : }
     159                 :            : 
     160                 :            : static inline const void *
     161                 :        123 : bpf_ld_mbuf(const struct rte_bpf *bpf, uint64_t reg[EBPF_REG_NUM],
     162                 :            :         const struct rte_ebpf_insn *ins, uint32_t off, uint32_t len)
     163                 :            : {
     164                 :            :         const struct rte_mbuf *mb;
     165                 :            :         const void *p;
     166                 :            : 
     167         [ +  + ]:        123 :         mb = (const struct rte_mbuf *)(uintptr_t)reg[EBPF_REG_6];
     168                 :            :         p = rte_pktmbuf_read(mb, off, len, reg + EBPF_REG_0);
     169         [ +  + ]:        123 :         if (p == NULL)
     170                 :          1 :                 RTE_BPF_LOG_LINE(DEBUG, "%s(bpf=%p, mbuf=%p, ofs=%u, len=%u): "
     171                 :            :                         "load beyond packet boundary at pc: %#zx;",
     172                 :            :                         __func__, bpf, mb, off, len,
     173                 :            :                         (uintptr_t)(ins) - (uintptr_t)(bpf)->prm.raw.ins);
     174                 :        123 :         return p;
     175                 :            : }
     176                 :            : 
     177                 :            : static inline uint64_t
     178                 :         82 : bpf_exec(const struct rte_bpf *bpf, uint64_t reg[EBPF_REG_NUM])
     179                 :            : {
     180                 :            :         const struct rte_ebpf_insn *ins;
     181                 :            : 
     182                 :        956 :         for (ins = bpf->prm.raw.ins; ; ins++) {
     183   [ -  -  +  -  :        956 :                 switch (ins->code) {
          +  -  +  +  +  
          -  +  -  +  +  
          +  +  +  -  +  
          +  +  -  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          -  +  -  +  +  
          +  +  -  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          -  -  -  -  -  
          -  +  +  +  +  
          -  -  -  +  +  
          -  -  -  +  +  
                +  -  + ]
     184                 :            :                 /* 32 bit ALU IMM operations */
     185                 :          0 :                 case (BPF_ALU | BPF_ADD | BPF_K):
     186                 :          0 :                         BPF_OP_ALU_IMM(reg, ins, +, uint32_t);
     187                 :          0 :                         break;
     188                 :          0 :                 case (BPF_ALU | BPF_SUB | BPF_K):
     189                 :          0 :                         BPF_OP_ALU_IMM(reg, ins, -, uint32_t);
     190                 :          0 :                         break;
     191                 :         25 :                 case (BPF_ALU | BPF_AND | BPF_K):
     192                 :         25 :                         BPF_OP_ALU_IMM(reg, ins, &, uint32_t);
     193                 :         25 :                         break;
     194                 :          0 :                 case (BPF_ALU | BPF_OR | BPF_K):
     195                 :          0 :                         BPF_OP_ALU_IMM(reg, ins, |, uint32_t);
     196                 :          0 :                         break;
     197                 :          6 :                 case (BPF_ALU | BPF_LSH | BPF_K):
     198                 :          6 :                         BPF_OP_SHIFT_IMM(reg, ins, <<, uint32_t);
     199                 :          6 :                         break;
     200                 :          0 :                 case (BPF_ALU | BPF_RSH | BPF_K):
     201                 :          0 :                         BPF_OP_SHIFT_IMM(reg, ins, >>, uint32_t);
     202                 :          0 :                         break;
     203                 :          1 :                 case (BPF_ALU | BPF_XOR | BPF_K):
     204                 :          1 :                         BPF_OP_ALU_IMM(reg, ins, ^, uint32_t);
     205                 :          1 :                         break;
     206                 :          1 :                 case (BPF_ALU | BPF_MUL | BPF_K):
     207                 :          1 :                         BPF_OP_ALU_IMM(reg, ins, *, uint32_t);
     208                 :          1 :                         break;
     209                 :          1 :                 case (BPF_ALU | BPF_DIV | BPF_K):
     210                 :          1 :                         BPF_OP_ALU_IMM(reg, ins, /, uint32_t);
     211                 :          1 :                         break;
     212                 :          0 :                 case (BPF_ALU | BPF_MOD | BPF_K):
     213                 :          0 :                         BPF_OP_ALU_IMM(reg, ins, %, uint32_t);
     214                 :          0 :                         break;
     215                 :        101 :                 case (BPF_ALU | EBPF_MOV | BPF_K):
     216                 :        101 :                         EBPF_MOV_ALU_IMM(reg, ins, uint32_t);
     217                 :        101 :                         break;
     218                 :            :                 /* 32 bit ALU REG operations */
     219                 :          0 :                 case (BPF_ALU | BPF_ADD | BPF_X):
     220                 :          0 :                         BPF_OP_ALU_REG(reg, ins, +, uint32_t);
     221                 :          0 :                         break;
     222                 :          1 :                 case (BPF_ALU | BPF_SUB | BPF_X):
     223                 :          1 :                         BPF_OP_ALU_REG(reg, ins, -, uint32_t);
     224                 :          1 :                         break;
     225                 :          1 :                 case (BPF_ALU | BPF_AND | BPF_X):
     226                 :          1 :                         BPF_OP_ALU_REG(reg, ins, &, uint32_t);
     227                 :          1 :                         break;
     228                 :          1 :                 case (BPF_ALU | BPF_OR | BPF_X):
     229                 :          1 :                         BPF_OP_ALU_REG(reg, ins, |, uint32_t);
     230                 :          1 :                         break;
     231                 :          1 :                 case (BPF_ALU | BPF_LSH | BPF_X):
     232                 :          1 :                         BPF_OP_SHIFT_REG(reg, ins, <<, uint32_t);
     233                 :          1 :                         break;
     234                 :          1 :                 case (BPF_ALU | BPF_RSH | BPF_X):
     235                 :          1 :                         BPF_OP_SHIFT_REG(reg, ins, >>, uint32_t);
     236                 :          1 :                         break;
     237                 :          0 :                 case (BPF_ALU | BPF_XOR | BPF_X):
     238                 :          0 :                         BPF_OP_ALU_REG(reg, ins, ^, uint32_t);
     239                 :          0 :                         break;
     240                 :          1 :                 case (BPF_ALU | BPF_MUL | BPF_X):
     241                 :          1 :                         BPF_OP_ALU_REG(reg, ins, *, uint32_t);
     242                 :          1 :                         break;
     243                 :          1 :                 case (BPF_ALU | BPF_DIV | BPF_X):
     244         [ +  - ]:          1 :                         BPF_DIV_ZERO_CHECK(bpf, reg, ins, uint32_t);
     245                 :          0 :                         BPF_OP_ALU_REG(reg, ins, /, uint32_t);
     246                 :          0 :                         break;
     247                 :          1 :                 case (BPF_ALU | BPF_MOD | BPF_X):
     248         [ -  + ]:          1 :                         BPF_DIV_ZERO_CHECK(bpf, reg, ins, uint32_t);
     249                 :          1 :                         BPF_OP_ALU_REG(reg, ins, %, uint32_t);
     250                 :          1 :                         break;
     251                 :          0 :                 case (BPF_ALU | EBPF_MOV | BPF_X):
     252                 :          0 :                         EBPF_MOV_ALU_REG(reg, ins, uint32_t);
     253                 :          0 :                         break;
     254                 :          1 :                 case (BPF_ALU | BPF_NEG):
     255                 :          1 :                         BPF_NEG_ALU(reg, ins, uint32_t);
     256                 :          1 :                         break;
     257                 :          5 :                 case (BPF_ALU | EBPF_END | EBPF_TO_BE):
     258                 :          5 :                         bpf_alu_be(reg, ins);
     259                 :          5 :                         break;
     260                 :            :                 case (BPF_ALU | EBPF_END | EBPF_TO_LE):
     261                 :            :                         bpf_alu_le(reg, ins);
     262                 :            :                         break;
     263                 :            :                 /* 64 bit ALU IMM operations */
     264                 :          6 :                 case (EBPF_ALU64 | BPF_ADD | BPF_K):
     265                 :          6 :                         BPF_OP_ALU_IMM(reg, ins, +, uint64_t);
     266                 :          6 :                         break;
     267                 :          2 :                 case (EBPF_ALU64 | BPF_SUB | BPF_K):
     268                 :          2 :                         BPF_OP_ALU_IMM(reg, ins, -, uint64_t);
     269                 :          2 :                         break;
     270                 :          1 :                 case (EBPF_ALU64 | BPF_AND | BPF_K):
     271                 :          1 :                         BPF_OP_ALU_IMM(reg, ins, &, uint64_t);
     272                 :          1 :                         break;
     273                 :         10 :                 case (EBPF_ALU64 | BPF_OR | BPF_K):
     274                 :         10 :                         BPF_OP_ALU_IMM(reg, ins, |, uint64_t);
     275                 :         10 :                         break;
     276                 :          3 :                 case (EBPF_ALU64 | BPF_LSH | BPF_K):
     277                 :          3 :                         BPF_OP_SHIFT_IMM(reg, ins, <<, uint64_t);
     278                 :          3 :                         break;
     279                 :          5 :                 case (EBPF_ALU64 | BPF_RSH | BPF_K):
     280                 :          5 :                         BPF_OP_SHIFT_IMM(reg, ins, >>, uint64_t);
     281                 :          5 :                         break;
     282                 :          2 :                 case (EBPF_ALU64 | EBPF_ARSH | BPF_K):
     283                 :          2 :                         BPF_OP_SHIFT_IMM(reg, ins, >>, int64_t);
     284                 :          2 :                         break;
     285                 :          1 :                 case (EBPF_ALU64 | BPF_XOR | BPF_K):
     286                 :          1 :                         BPF_OP_ALU_IMM(reg, ins, ^, uint64_t);
     287                 :          1 :                         break;
     288                 :          1 :                 case (EBPF_ALU64 | BPF_MUL | BPF_K):
     289                 :          1 :                         BPF_OP_ALU_IMM(reg, ins, *, uint64_t);
     290                 :          1 :                         break;
     291                 :          1 :                 case (EBPF_ALU64 | BPF_DIV | BPF_K):
     292                 :          1 :                         BPF_OP_ALU_IMM(reg, ins, /, uint64_t);
     293                 :          1 :                         break;
     294                 :          2 :                 case (EBPF_ALU64 | BPF_MOD | BPF_K):
     295                 :          2 :                         BPF_OP_ALU_IMM(reg, ins, %, uint64_t);
     296                 :          2 :                         break;
     297                 :         17 :                 case (EBPF_ALU64 | EBPF_MOV | BPF_K):
     298                 :         17 :                         EBPF_MOV_ALU_IMM(reg, ins, uint64_t);
     299                 :         17 :                         break;
     300                 :            :                 /* 64 bit ALU REG operations */
     301                 :         24 :                 case (EBPF_ALU64 | BPF_ADD | BPF_X):
     302                 :         24 :                         BPF_OP_ALU_REG(reg, ins, +, uint64_t);
     303                 :         24 :                         break;
     304                 :          1 :                 case (EBPF_ALU64 | BPF_SUB | BPF_X):
     305                 :          1 :                         BPF_OP_ALU_REG(reg, ins, -, uint64_t);
     306                 :          1 :                         break;
     307                 :          1 :                 case (EBPF_ALU64 | BPF_AND | BPF_X):
     308                 :          1 :                         BPF_OP_ALU_REG(reg, ins, &, uint64_t);
     309                 :          1 :                         break;
     310                 :          0 :                 case (EBPF_ALU64 | BPF_OR | BPF_X):
     311                 :          0 :                         BPF_OP_ALU_REG(reg, ins, |, uint64_t);
     312                 :          0 :                         break;
     313                 :          1 :                 case (EBPF_ALU64 | BPF_LSH | BPF_X):
     314                 :          1 :                         BPF_OP_SHIFT_REG(reg, ins, <<, uint64_t);
     315                 :          1 :                         break;
     316                 :          0 :                 case (EBPF_ALU64 | BPF_RSH | BPF_X):
     317                 :          0 :                         BPF_OP_SHIFT_REG(reg, ins, >>, uint64_t);
     318                 :          0 :                         break;
     319                 :          1 :                 case (EBPF_ALU64 | EBPF_ARSH | BPF_X):
     320                 :          1 :                         BPF_OP_SHIFT_REG(reg, ins, >>, int64_t);
     321                 :          1 :                         break;
     322                 :        109 :                 case (EBPF_ALU64 | BPF_XOR | BPF_X):
     323                 :        109 :                         BPF_OP_ALU_REG(reg, ins, ^, uint64_t);
     324                 :        109 :                         break;
     325                 :          1 :                 case (EBPF_ALU64 | BPF_MUL | BPF_X):
     326                 :          1 :                         BPF_OP_ALU_REG(reg, ins, *, uint64_t);
     327                 :          1 :                         break;
     328                 :          1 :                 case (EBPF_ALU64 | BPF_DIV | BPF_X):
     329         [ -  + ]:          1 :                         BPF_DIV_ZERO_CHECK(bpf, reg, ins, uint64_t);
     330                 :          1 :                         BPF_OP_ALU_REG(reg, ins, /, uint64_t);
     331                 :          1 :                         break;
     332                 :          0 :                 case (EBPF_ALU64 | BPF_MOD | BPF_X):
     333         [ #  # ]:          0 :                         BPF_DIV_ZERO_CHECK(bpf, reg, ins, uint64_t);
     334                 :          0 :                         BPF_OP_ALU_REG(reg, ins, %, uint64_t);
     335                 :          0 :                         break;
     336                 :         85 :                 case (EBPF_ALU64 | EBPF_MOV | BPF_X):
     337                 :         85 :                         EBPF_MOV_ALU_REG(reg, ins, uint64_t);
     338                 :         85 :                         break;
     339                 :          3 :                 case (EBPF_ALU64 | BPF_NEG):
     340                 :          3 :                         BPF_NEG_ALU(reg, ins, uint64_t);
     341                 :          3 :                         break;
     342                 :            :                 /* load instructions */
     343                 :          9 :                 case (BPF_LDX | BPF_MEM | BPF_B):
     344                 :          9 :                         BPF_LD_REG(reg, ins, uint8_t);
     345                 :          9 :                         break;
     346                 :          7 :                 case (BPF_LDX | BPF_MEM | BPF_H):
     347                 :          7 :                         BPF_LD_REG(reg, ins, uint16_t);
     348                 :          7 :                         break;
     349                 :         24 :                 case (BPF_LDX | BPF_MEM | BPF_W):
     350                 :         24 :                         BPF_LD_REG(reg, ins, uint32_t);
     351                 :         24 :                         break;
     352                 :         16 :                 case (BPF_LDX | BPF_MEM | EBPF_DW):
     353                 :         16 :                         BPF_LD_REG(reg, ins, uint64_t);
     354                 :         16 :                         break;
     355                 :            :                 /* load 64 bit immediate value */
     356                 :         14 :                 case (BPF_LD | BPF_IMM | EBPF_DW):
     357                 :         14 :                         reg[ins->dst_reg] = (uint32_t)ins[0].imm |
     358                 :         14 :                                 (uint64_t)(uint32_t)ins[1].imm << 32;
     359                 :         14 :                         ins++;
     360                 :         14 :                         break;
     361                 :            :                 /* load absolute instructions */
     362                 :         27 :                 case (BPF_LD | BPF_ABS | BPF_B):
     363         [ +  - ]:         27 :                         BPF_LD_ABS(bpf, reg, ins, uint8_t, NOP);
     364                 :         27 :                         break;
     365                 :         53 :                 case (BPF_LD | BPF_ABS | BPF_H):
     366   [ +  -  -  + ]:        106 :                         BPF_LD_ABS(bpf, reg, ins, uint16_t, rte_be_to_cpu_16);
     367                 :         53 :                         break;
     368                 :         31 :                 case (BPF_LD | BPF_ABS | BPF_W):
     369   [ +  -  -  + ]:         62 :                         BPF_LD_ABS(bpf, reg, ins, uint32_t, rte_be_to_cpu_32);
     370                 :         31 :                         break;
     371                 :            :                 /* load indirect instructions */
     372                 :          3 :                 case (BPF_LD | BPF_IND | BPF_B):
     373         [ +  - ]:          3 :                         BPF_LD_IND(bpf, reg, ins, uint8_t, NOP);
     374                 :          3 :                         break;
     375                 :          6 :                 case (BPF_LD | BPF_IND | BPF_H):
     376   [ +  -  -  + ]:         12 :                         BPF_LD_IND(bpf, reg, ins, uint16_t, rte_be_to_cpu_16);
     377                 :          6 :                         break;
     378                 :          3 :                 case (BPF_LD | BPF_IND | BPF_W):
     379   [ +  +  -  + ]:          5 :                         BPF_LD_IND(bpf, reg, ins, uint32_t, rte_be_to_cpu_32);
     380                 :          2 :                         break;
     381                 :            :                 /* store instructions */
     382                 :          3 :                 case (BPF_STX | BPF_MEM | BPF_B):
     383                 :          3 :                         BPF_ST_REG(reg, ins, uint8_t);
     384                 :          3 :                         break;
     385                 :          1 :                 case (BPF_STX | BPF_MEM | BPF_H):
     386                 :          1 :                         BPF_ST_REG(reg, ins, uint16_t);
     387                 :          1 :                         break;
     388                 :          6 :                 case (BPF_STX | BPF_MEM | BPF_W):
     389                 :          6 :                         BPF_ST_REG(reg, ins, uint32_t);
     390                 :          6 :                         break;
     391                 :         29 :                 case (BPF_STX | BPF_MEM | EBPF_DW):
     392                 :         29 :                         BPF_ST_REG(reg, ins, uint64_t);
     393                 :         29 :                         break;
     394                 :          5 :                 case (BPF_ST | BPF_MEM | BPF_B):
     395                 :          5 :                         BPF_ST_IMM(reg, ins, uint8_t);
     396                 :          5 :                         break;
     397                 :          1 :                 case (BPF_ST | BPF_MEM | BPF_H):
     398                 :          1 :                         BPF_ST_IMM(reg, ins, uint16_t);
     399                 :          1 :                         break;
     400                 :          1 :                 case (BPF_ST | BPF_MEM | BPF_W):
     401                 :          1 :                         BPF_ST_IMM(reg, ins, uint32_t);
     402                 :          1 :                         break;
     403                 :          1 :                 case (BPF_ST | BPF_MEM | EBPF_DW):
     404                 :          1 :                         BPF_ST_IMM(reg, ins, uint64_t);
     405                 :          1 :                         break;
     406                 :            :                 /* atomic instructions */
     407                 :         12 :                 case (BPF_STX | EBPF_ATOMIC | BPF_W):
     408      [ +  +  - ]:         12 :                         BPF_ST_ATOMIC_REG(reg, ins, 32);
     409                 :            :                         break;
     410                 :         12 :                 case (BPF_STX | EBPF_ATOMIC | EBPF_DW):
     411      [ +  +  - ]:         12 :                         BPF_ST_ATOMIC_REG(reg, ins, 64);
     412                 :            :                         break;
     413                 :            :                 /* jump instructions */
     414                 :         21 :                 case (BPF_JMP | BPF_JA):
     415                 :         21 :                         BPF_JMP_UNC(ins);
     416                 :         21 :                         break;
     417                 :            :                 /* jump IMM instructions */
     418                 :         24 :                 case (BPF_JMP | BPF_JEQ | BPF_K):
     419         [ +  + ]:         24 :                         BPF_JMP_CND_IMM(reg, ins, ==, uint64_t);
     420                 :         24 :                         break;
     421                 :         77 :                 case (BPF_JMP | EBPF_JNE | BPF_K):
     422         [ +  + ]:         77 :                         BPF_JMP_CND_IMM(reg, ins, !=, uint64_t);
     423                 :         77 :                         break;
     424                 :          1 :                 case (BPF_JMP | BPF_JGT | BPF_K):
     425         [ +  - ]:          1 :                         BPF_JMP_CND_IMM(reg, ins, >, uint64_t);
     426                 :          1 :                         break;
     427                 :          0 :                 case (BPF_JMP | EBPF_JLT | BPF_K):
     428         [ #  # ]:          0 :                         BPF_JMP_CND_IMM(reg, ins, <, uint64_t);
     429                 :          0 :                         break;
     430                 :          0 :                 case (BPF_JMP | BPF_JGE | BPF_K):
     431         [ #  # ]:          0 :                         BPF_JMP_CND_IMM(reg, ins, >=, uint64_t);
     432                 :          0 :                         break;
     433                 :          0 :                 case (BPF_JMP | EBPF_JLE | BPF_K):
     434         [ #  # ]:          0 :                         BPF_JMP_CND_IMM(reg, ins, <=, uint64_t);
     435                 :          0 :                         break;
     436                 :          0 :                 case (BPF_JMP | EBPF_JSGT | BPF_K):
     437         [ #  # ]:          0 :                         BPF_JMP_CND_IMM(reg, ins, >, int64_t);
     438                 :          0 :                         break;
     439                 :          0 :                 case (BPF_JMP | EBPF_JSLT | BPF_K):
     440         [ #  # ]:          0 :                         BPF_JMP_CND_IMM(reg, ins, <, int64_t);
     441                 :          0 :                         break;
     442                 :          0 :                 case (BPF_JMP | EBPF_JSGE | BPF_K):
     443         [ #  # ]:          0 :                         BPF_JMP_CND_IMM(reg, ins, >=, int64_t);
     444                 :          0 :                         break;
     445                 :          1 :                 case (BPF_JMP | EBPF_JSLE | BPF_K):
     446         [ +  - ]:          1 :                         BPF_JMP_CND_IMM(reg, ins, <=, int64_t);
     447                 :          1 :                         break;
     448                 :          9 :                 case (BPF_JMP | BPF_JSET | BPF_K):
     449         [ +  + ]:          9 :                         BPF_JMP_CND_IMM(reg, ins, &, uint64_t);
     450                 :          9 :                         break;
     451                 :            :                 /* jump REG instructions */
     452                 :         30 :                 case (BPF_JMP | BPF_JEQ | BPF_X):
     453         [ +  + ]:         30 :                         BPF_JMP_CND_REG(reg, ins, ==, uint64_t);
     454                 :         30 :                         break;
     455                 :          9 :                 case (BPF_JMP | EBPF_JNE | BPF_X):
     456         [ +  + ]:          9 :                         BPF_JMP_CND_REG(reg, ins, !=, uint64_t);
     457                 :          9 :                         break;
     458                 :          0 :                 case (BPF_JMP | BPF_JGT | BPF_X):
     459         [ #  # ]:          0 :                         BPF_JMP_CND_REG(reg, ins, >, uint64_t);
     460                 :          0 :                         break;
     461                 :          0 :                 case (BPF_JMP | EBPF_JLT | BPF_X):
     462         [ #  # ]:          0 :                         BPF_JMP_CND_REG(reg, ins, <, uint64_t);
     463                 :          0 :                         break;
     464                 :          0 :                 case (BPF_JMP | BPF_JGE | BPF_X):
     465         [ #  # ]:          0 :                         BPF_JMP_CND_REG(reg, ins, >=, uint64_t);
     466                 :          0 :                         break;
     467                 :          1 :                 case (BPF_JMP | EBPF_JLE | BPF_X):
     468         [ +  - ]:          1 :                         BPF_JMP_CND_REG(reg, ins, <=, uint64_t);
     469                 :          1 :                         break;
     470                 :          1 :                 case (BPF_JMP | EBPF_JSGT | BPF_X):
     471         [ -  + ]:          1 :                         BPF_JMP_CND_REG(reg, ins, >, int64_t);
     472                 :          1 :                         break;
     473                 :          0 :                 case (BPF_JMP | EBPF_JSLT | BPF_X):
     474         [ #  # ]:          0 :                         BPF_JMP_CND_REG(reg, ins, <, int64_t);
     475                 :          0 :                         break;
     476                 :          0 :                 case (BPF_JMP | EBPF_JSGE | BPF_X):
     477         [ #  # ]:          0 :                         BPF_JMP_CND_REG(reg, ins, >=, int64_t);
     478                 :          0 :                         break;
     479                 :          0 :                 case (BPF_JMP | EBPF_JSLE | BPF_X):
     480         [ #  # ]:          0 :                         BPF_JMP_CND_REG(reg, ins, <=, int64_t);
     481                 :          0 :                         break;
     482                 :          1 :                 case (BPF_JMP | BPF_JSET | BPF_X):
     483         [ +  - ]:          1 :                         BPF_JMP_CND_REG(reg, ins, &, uint64_t);
     484                 :          1 :                         break;
     485                 :            :                 /* call instructions */
     486                 :          7 :                 case (BPF_JMP | EBPF_CALL):
     487                 :          7 :                         reg[EBPF_REG_0] = bpf->prm.xsym[ins->imm].func.val(
     488                 :            :                                 reg[EBPF_REG_1], reg[EBPF_REG_2],
     489                 :            :                                 reg[EBPF_REG_3], reg[EBPF_REG_4],
     490                 :            :                                 reg[EBPF_REG_5]);
     491                 :          7 :                         break;
     492                 :            :                 /* return instruction */
     493                 :         80 :                 case (BPF_JMP | EBPF_EXIT):
     494                 :         80 :                         return reg[EBPF_REG_0];
     495                 :          0 :                 default:
     496                 :          0 :                         RTE_BPF_LOG_LINE(ERR,
     497                 :            :                                 "%s(%p): invalid opcode %#x at pc: %#zx;",
     498                 :            :                                 __func__, bpf, ins->code,
     499                 :            :                                 (uintptr_t)ins - (uintptr_t)bpf->prm.raw.ins);
     500                 :          0 :                         return 0;
     501                 :            :                 }
     502                 :            :         }
     503                 :            : 
     504                 :            :         /* should never be reached */
     505                 :            :         RTE_VERIFY(0);
     506                 :            :         return 0;
     507                 :            : }
     508                 :            : 
     509                 :            : RTE_EXPORT_SYMBOL(rte_bpf_exec_burst)
     510                 :            : uint32_t
     511                 :         83 : rte_bpf_exec_burst(const struct rte_bpf *bpf, void *ctx[], uint64_t rc[],
     512                 :            :         uint32_t num)
     513                 :            : {
     514                 :            :         uint32_t i;
     515                 :            :         uint64_t reg[EBPF_REG_NUM];
     516                 :            :         uint64_t stack[MAX_BPF_STACK_SIZE / sizeof(uint64_t)];
     517                 :            : 
     518         [ +  + ]:         83 :         if (bpf->prm.nb_prog_arg != 1) {
     519                 :            :                 /* Use rte_bpf_exec_burst_ex with this program. */
     520                 :          1 :                 rte_errno = EINVAL;
     521                 :          1 :                 return 0;
     522                 :            :         }
     523                 :            : 
     524         [ +  + ]:        164 :         for (i = 0; i != num; i++) {
     525                 :            : 
     526                 :         82 :                 reg[EBPF_REG_1] = (uintptr_t)ctx[i];
     527                 :         82 :                 reg[EBPF_REG_10] = (uintptr_t)(stack + RTE_DIM(stack));
     528                 :            : 
     529                 :         82 :                 rc[i] = bpf_exec(bpf, reg);
     530                 :            :         }
     531                 :            : 
     532                 :            :         return i;
     533                 :            : }
     534                 :            : 
     535                 :            : static uint32_t
     536                 :          0 : exec_vm_burst_ex(const struct rte_bpf *bpf, const struct rte_bpf_prog_ctx *ctx,
     537                 :            :         uint64_t rc[], uint32_t num)
     538                 :            : {
     539                 :            :         uint32_t i;
     540                 :            :         uint64_t reg[EBPF_REG_NUM];
     541                 :            :         uint64_t stack[MAX_BPF_STACK_SIZE / sizeof(uint64_t)];
     542                 :            : 
     543         [ #  # ]:          0 :         for (i = 0; i != num; i++) {
     544                 :            : 
     545   [ #  #  #  #  :          0 :                 switch (bpf->prm.nb_prog_arg) {
                   #  # ]
     546                 :          0 :                 case 5:
     547                 :          0 :                         reg[EBPF_REG_5] = ctx[i].arg[4].u64;
     548                 :            :                         /* FALLTHROUGH */
     549                 :          0 :                 case 4:
     550                 :          0 :                         reg[EBPF_REG_4] = ctx[i].arg[3].u64;
     551                 :            :                         /* FALLTHROUGH */
     552                 :          0 :                 case 3:
     553                 :          0 :                         reg[EBPF_REG_3] = ctx[i].arg[2].u64;
     554                 :            :                         /* FALLTHROUGH */
     555                 :          0 :                 case 2:
     556                 :          0 :                         reg[EBPF_REG_2] = ctx[i].arg[1].u64;
     557                 :            :                         /* FALLTHROUGH */
     558                 :          0 :                 case 1:
     559                 :          0 :                         reg[EBPF_REG_1] = ctx[i].arg[0].u64;
     560                 :            :                         /* FALLTHROUGH */
     561                 :            :                 case 0:
     562                 :            :                         break;
     563                 :            :                 }
     564                 :            : 
     565                 :          0 :                 reg[EBPF_REG_10] = (uintptr_t)(stack + RTE_DIM(stack));
     566                 :            : 
     567                 :          0 :                 rc[i] = bpf_exec(bpf, reg);
     568                 :            :         }
     569                 :            : 
     570                 :          0 :         return i;
     571                 :            : }
     572                 :            : 
     573                 :            : static uint32_t
     574                 :          0 : exec_jit_burst_ex(const struct rte_bpf *bpf, const struct rte_bpf_prog_ctx *ctx,
     575                 :            :         uint64_t rc[], uint32_t num)
     576                 :            : {
     577                 :            :         uint32_t i = 0;
     578                 :          0 :         const struct rte_bpf_jit_ex jit = bpf->jit;
     579                 :            : 
     580                 :            :         /*
     581                 :            :          * Fast path: assumes application pre-validated RTE_BPF_EXEC_FLAG_JIT
     582                 :            :          * and successful JIT generation. No explicit NULL checks here.
     583                 :            :          */
     584   [ #  #  #  #  :          0 :         switch (bpf->prm.nb_prog_arg) {
                #  #  # ]
     585                 :            :         case 0:
     586         [ #  # ]:          0 :                 for (i = 0; i != num; i++)
     587                 :          0 :                         rc[i] = jit.func0();
     588                 :            :                 break;
     589                 :            :         case 1:
     590         [ #  # ]:          0 :                 for (i = 0; i != num; i++) {
     591                 :          0 :                         const union rte_bpf_func_arg *const arg = ctx[i].arg;
     592                 :          0 :                         rc[i] = jit.func1(arg[0]);
     593                 :            :                 }
     594                 :            :                 break;
     595                 :            :         case 2:
     596         [ #  # ]:          0 :                 for (i = 0; i != num; i++) {
     597                 :          0 :                         const union rte_bpf_func_arg *const arg = ctx[i].arg;
     598                 :          0 :                         rc[i] = jit.func2(arg[0], arg[1]);
     599                 :            :                 }
     600                 :            :                 break;
     601                 :            :         case 3:
     602         [ #  # ]:          0 :                 for (i = 0; i != num; i++) {
     603                 :          0 :                         const union rte_bpf_func_arg *const arg = ctx[i].arg;
     604                 :          0 :                         rc[i] = jit.func3(arg[0], arg[1], arg[2]);
     605                 :            :                 }
     606                 :            :                 break;
     607                 :            :         case 4:
     608         [ #  # ]:          0 :                 for (i = 0; i != num; i++) {
     609                 :          0 :                         const union rte_bpf_func_arg *const arg = ctx[i].arg;
     610                 :          0 :                         rc[i] = jit.func4(arg[0], arg[1], arg[2], arg[3]);
     611                 :            :                 }
     612                 :            :                 break;
     613                 :            :         case 5:
     614         [ #  # ]:          0 :                 for (i = 0; i != num; i++) {
     615                 :          0 :                         const union rte_bpf_func_arg *const arg = ctx[i].arg;
     616                 :          0 :                         rc[i] = jit.func5(arg[0], arg[1], arg[2], arg[3], arg[4]);
     617                 :            :                 }
     618                 :            :                 break;
     619                 :            :         }
     620                 :            : 
     621                 :          0 :         return i;
     622                 :            : }
     623                 :            : 
     624                 :            : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_bpf_exec_burst_ex, 26.07)
     625                 :            : uint32_t
     626                 :          1 : rte_bpf_exec_burst_ex(const struct rte_bpf *bpf, const struct rte_bpf_prog_ctx *ctx,
     627                 :            :         uint64_t rc[], uint32_t num, uint64_t flags)
     628                 :            : {
     629         [ +  - ]:          1 :         if ((flags & ~RTE_BPF_EXEC_FLAG_MASK) != 0) {
     630                 :          1 :                 rte_errno = EINVAL;
     631                 :          1 :                 return 0;
     632                 :            :         }
     633                 :            : 
     634                 :          0 :         return (flags & RTE_BPF_EXEC_FLAG_JIT) != 0 ?
     635         [ #  # ]:          0 :                 exec_jit_burst_ex(bpf, ctx, rc, num) :
     636                 :          0 :                 exec_vm_burst_ex(bpf, ctx, rc, num);
     637                 :            : }
     638                 :            : 
     639                 :            : RTE_EXPORT_SYMBOL(rte_bpf_exec)
     640                 :            : uint64_t
     641                 :         82 : rte_bpf_exec(const struct rte_bpf *bpf, void *ctx)
     642                 :            : {
     643                 :         82 :         uint64_t rc = UINT64_MAX;
     644                 :            : 
     645                 :         82 :         rte_bpf_exec_burst(bpf, &ctx, &rc, 1);
     646                 :         82 :         return rc;
     647                 :            : }
     648                 :            : 
     649                 :            : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_bpf_exec_ex, 26.07)
     650                 :            : uint64_t
     651                 :          0 : rte_bpf_exec_ex(const struct rte_bpf *bpf, const struct rte_bpf_prog_ctx *ctx,
     652                 :            :                 uint64_t flags)
     653                 :            : {
     654                 :          0 :         uint64_t rc = UINT64_MAX;
     655                 :            : 
     656                 :          0 :         rte_bpf_exec_burst_ex(bpf, ctx, &rc, 1, flags);
     657                 :          0 :         return rc;
     658                 :            : }

Generated by: LCOV version 1.14