LCOV - code coverage report
Current view: top level - lib/bpf - bpf_pkt.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 169 0.0 %
Date: 2026-11-01 19:11:22 Functions: 0 24 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 114 0.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 <string.h>
       7                 :            : #include <errno.h>
       8                 :            : #include <stdint.h>
       9                 :            : 
      10                 :            : #include <sys/queue.h>
      11                 :            : 
      12                 :            : #include <eal_export.h>
      13                 :            : #include <rte_common.h>
      14                 :            : #include <rte_malloc.h>
      15                 :            : #include <rte_log.h>
      16                 :            : #include <rte_atomic.h>
      17                 :            : #include <rte_mbuf.h>
      18                 :            : #include <rte_ethdev.h>
      19                 :            : 
      20                 :            : #include <rte_bpf_ethdev.h>
      21                 :            : #include "bpf_impl.h"
      22                 :            : 
      23                 :            : /*
      24                 :            :  * information about installed BPF rx/tx callback
      25                 :            :  */
      26                 :            : 
      27                 :            : struct __rte_cache_aligned bpf_eth_cbi {
      28                 :            :         /* used by both data & control path */
      29                 :            :         RTE_ATOMIC(uint32_t) use;    /*usage counter */
      30                 :            :         const struct rte_eth_rxtx_callback *cb;  /* callback handle */
      31                 :            :         struct rte_bpf *bpf;
      32                 :            :         struct rte_bpf_jit jit;
      33                 :            :         /* used by control path only */
      34                 :            :         LIST_ENTRY(bpf_eth_cbi) link;
      35                 :            :         uint16_t port;
      36                 :            :         uint16_t queue;
      37                 :            : };
      38                 :            : 
      39                 :            : /*
      40                 :            :  * Odd number means that callback is used by datapath.
      41                 :            :  * Even number means that callback is not used by datapath.
      42                 :            :  */
      43                 :            : #define BPF_ETH_CBI_INUSE  1
      44                 :            : 
      45                 :            : /*
      46                 :            :  * List to manage RX/TX installed callbacks.
      47                 :            :  */
      48                 :            : LIST_HEAD(bpf_eth_cbi_list, bpf_eth_cbi);
      49                 :            : 
      50                 :            : enum {
      51                 :            :         BPF_ETH_RX,
      52                 :            :         BPF_ETH_TX,
      53                 :            :         BPF_ETH_NUM,
      54                 :            : };
      55                 :            : 
      56                 :            : /*
      57                 :            :  * information about all installed BPF rx/tx callbacks
      58                 :            :  */
      59                 :            : struct bpf_eth_cbh {
      60                 :            :         rte_spinlock_t lock;
      61                 :            :         struct bpf_eth_cbi_list list;
      62                 :            :         uint32_t type;
      63                 :            : };
      64                 :            : 
      65                 :            : static struct bpf_eth_cbh rx_cbh = {
      66                 :            :         .lock = RTE_SPINLOCK_INITIALIZER,
      67                 :            :         .list = LIST_HEAD_INITIALIZER(list),
      68                 :            :         .type = BPF_ETH_RX,
      69                 :            : };
      70                 :            : 
      71                 :            : static struct bpf_eth_cbh tx_cbh = {
      72                 :            :         .lock = RTE_SPINLOCK_INITIALIZER,
      73                 :            :         .list = LIST_HEAD_INITIALIZER(list),
      74                 :            :         .type = BPF_ETH_TX,
      75                 :            : };
      76                 :            : 
      77                 :            : /*
      78                 :            :  * Marks given callback as used by datapath.
      79                 :            :  */
      80                 :            : static __rte_always_inline void
      81                 :            : bpf_eth_cbi_inuse(struct bpf_eth_cbi *cbi)
      82                 :            : {
      83                 :          0 :         uint32_t puse = rte_atomic_load_explicit(&cbi->use, rte_memory_order_relaxed);
      84                 :            : 
      85                 :            :         /* increment use count: does not need to be locked */
      86                 :          0 :         rte_atomic_store_explicit(&cbi->use, puse + 1, rte_memory_order_relaxed);
      87                 :            : 
      88                 :            :         /* full barrier: count must be visible before cb is read */
      89                 :            :         rte_atomic_thread_fence(rte_memory_order_seq_cst);
      90                 :            : }
      91                 :            : 
      92                 :            : /*
      93                 :            :  * Marks given callback list as not used by datapath.
      94                 :            :  */
      95                 :            : static __rte_always_inline void
      96                 :            : bpf_eth_cbi_unuse(struct bpf_eth_cbi *cbi)
      97                 :            : {
      98                 :          0 :         uint32_t puse = rte_atomic_load_explicit(&cbi->use, rte_memory_order_relaxed);
      99                 :            : 
     100                 :            :         /* release: pairs with the acquire bpf_eth_cbi_wait() */
     101                 :          0 :         rte_atomic_store_explicit(&cbi->use, puse + 1, rte_memory_order_release);
     102                 :            : }
     103                 :            : 
     104                 :            : /*
     105                 :            :  * Waits till datapath finished using given callback.
     106                 :            :  */
     107                 :            : static void
     108                 :          0 : bpf_eth_cbi_wait(const struct bpf_eth_cbi *cbi)
     109                 :            : {
     110                 :            :         uint32_t puse;
     111                 :            : 
     112                 :            :         /* full barrier: cleared cb must be visible before counter is read */
     113                 :            :         rte_atomic_thread_fence(rte_memory_order_seq_cst);
     114                 :            : 
     115                 :          0 :         puse = rte_atomic_load_explicit(&cbi->use, rte_memory_order_acquire);
     116                 :            : 
     117                 :            :         /* in use, busy wait till current RX/TX iteration is finished */
     118         [ #  # ]:          0 :         if ((puse & BPF_ETH_CBI_INUSE) != 0) {
     119         [ #  # ]:          0 :                 RTE_WAIT_UNTIL_MASKED((__rte_atomic uint32_t *)(uintptr_t)&cbi->use,
     120                 :            :                         UINT32_MAX, !=, puse, rte_memory_order_acquire);
     121                 :            :         }
     122                 :          0 : }
     123                 :            : 
     124                 :            : static void
     125                 :            : bpf_eth_cbi_cleanup(struct bpf_eth_cbi *bc)
     126                 :            : {
     127                 :          0 :         bc->bpf = NULL;
     128                 :          0 :         memset(&bc->jit, 0, sizeof(bc->jit));
     129                 :          0 : }
     130                 :            : 
     131                 :            : static struct bpf_eth_cbi *
     132                 :            : bpf_eth_cbh_find(struct bpf_eth_cbh *cbh, uint16_t port, uint16_t queue)
     133                 :            : {
     134                 :            :         struct bpf_eth_cbi *cbi;
     135                 :            : 
     136   [ #  #  #  # ]:          0 :         LIST_FOREACH(cbi, &cbh->list, link) {
     137   [ #  #  #  #  :          0 :                 if (cbi->port == port && cbi->queue == queue)
             #  #  #  # ]
     138                 :            :                         break;
     139                 :            :         }
     140                 :            :         return cbi;
     141                 :            : }
     142                 :            : 
     143                 :            : static struct bpf_eth_cbi *
     144                 :          0 : bpf_eth_cbh_add(struct bpf_eth_cbh *cbh, uint16_t port, uint16_t queue)
     145                 :            : {
     146                 :            :         struct bpf_eth_cbi *cbi;
     147                 :            : 
     148                 :            :         /* return an existing one */
     149                 :            :         cbi = bpf_eth_cbh_find(cbh, port, queue);
     150         [ #  # ]:          0 :         if (cbi != NULL)
     151                 :            :                 return cbi;
     152                 :            : 
     153                 :          0 :         cbi = rte_zmalloc(NULL, sizeof(*cbi), RTE_CACHE_LINE_SIZE);
     154         [ #  # ]:          0 :         if (cbi != NULL) {
     155                 :          0 :                 cbi->port = port;
     156                 :          0 :                 cbi->queue = queue;
     157         [ #  # ]:          0 :                 LIST_INSERT_HEAD(&cbh->list, cbi, link);
     158                 :            :         }
     159                 :            :         return cbi;
     160                 :            : }
     161                 :            : 
     162                 :            : /*
     163                 :            :  * BPF packet processing routines.
     164                 :            :  */
     165                 :            : 
     166                 :            : static inline uint32_t
     167                 :          0 : apply_filter(struct rte_mbuf *mb[], const uint64_t rc[], uint32_t num,
     168                 :            :         uint32_t drop)
     169                 :            : {
     170                 :            :         uint32_t i, j, k;
     171                 :          0 :         struct rte_mbuf **dr = alloca(num * sizeof(struct rte_mbuf *));
     172                 :            : 
     173         [ #  # ]:          0 :         for (i = 0, j = 0, k = 0; i != num; i++) {
     174                 :            : 
     175                 :            :                 /* filter matches */
     176         [ #  # ]:          0 :                 if (rc[i] != 0)
     177                 :          0 :                         mb[j++] = mb[i];
     178                 :            :                 /* no match */
     179                 :            :                 else
     180                 :          0 :                         dr[k++] = mb[i];
     181                 :            :         }
     182                 :            : 
     183         [ #  # ]:          0 :         if (drop != 0) {
     184                 :            :                 /* free filtered out mbufs */
     185                 :          0 :                 rte_pktmbuf_free_bulk(dr, k);
     186                 :            :         } else {
     187                 :            :                 /* copy filtered out mbufs beyond good ones */
     188         [ #  # ]:          0 :                 for (i = 0; i != k; i++)
     189                 :          0 :                         mb[j + i] = dr[i];
     190                 :            :         }
     191                 :            : 
     192                 :          0 :         return j;
     193                 :            : }
     194                 :            : 
     195                 :            : static inline uint32_t
     196                 :          0 : pkt_filter_vm(const struct rte_bpf *bpf, struct rte_mbuf *mb[], uint32_t num,
     197                 :            :         uint32_t drop)
     198                 :            : {
     199                 :            :         uint32_t i;
     200                 :          0 :         void **dp = alloca(num * sizeof(void *));
     201                 :          0 :         uint64_t *rc = alloca(num * sizeof(uint64_t));
     202                 :            : 
     203         [ #  # ]:          0 :         for (i = 0; i != num; i++)
     204                 :          0 :                 dp[i] = rte_pktmbuf_mtod(mb[i], void *);
     205                 :            : 
     206                 :          0 :         rte_bpf_exec_burst(bpf, dp, rc, num);
     207                 :          0 :         return apply_filter(mb, rc, num, drop);
     208                 :            : }
     209                 :            : 
     210                 :            : static inline uint32_t
     211                 :          0 : pkt_filter_jit(const struct rte_bpf_jit *jit, struct rte_mbuf *mb[],
     212                 :            :         uint32_t num, uint32_t drop)
     213                 :            : {
     214                 :            :         uint32_t i, n;
     215                 :            :         void *dp;
     216                 :          0 :         uint64_t *rc = alloca(num * sizeof(uint64_t));
     217                 :            : 
     218                 :            :         n = 0;
     219         [ #  # ]:          0 :         for (i = 0; i != num; i++) {
     220                 :          0 :                 dp = rte_pktmbuf_mtod(mb[i], void *);
     221                 :          0 :                 rc[i] = jit->func(dp);
     222                 :          0 :                 n += (rc[i] == 0);
     223                 :            :         }
     224                 :            : 
     225         [ #  # ]:          0 :         if (n != 0)
     226                 :          0 :                 num = apply_filter(mb, rc, num, drop);
     227                 :            : 
     228                 :          0 :         return num;
     229                 :            : }
     230                 :            : 
     231                 :            : static inline uint32_t
     232                 :          0 : pkt_filter_mb_vm(const struct rte_bpf *bpf, struct rte_mbuf *mb[], uint32_t num,
     233                 :            :         uint32_t drop)
     234                 :            : {
     235                 :          0 :         uint64_t *rc = alloca(num * sizeof(uint64_t));
     236                 :            : 
     237                 :          0 :         rte_bpf_exec_burst(bpf, (void **)mb, rc, num);
     238                 :          0 :         return apply_filter(mb, rc, num, drop);
     239                 :            : }
     240                 :            : 
     241                 :            : static inline uint32_t
     242                 :          0 : pkt_filter_mb_jit(const struct rte_bpf_jit *jit, struct rte_mbuf *mb[],
     243                 :            :         uint32_t num, uint32_t drop)
     244                 :            : {
     245                 :            :         uint32_t i, n;
     246                 :          0 :         uint64_t *rc = alloca(num * sizeof(uint64_t));
     247                 :            : 
     248                 :            :         n = 0;
     249         [ #  # ]:          0 :         for (i = 0; i != num; i++) {
     250                 :          0 :                 rc[i] = jit->func(mb[i]);
     251                 :          0 :                 n += (rc[i] == 0);
     252                 :            :         }
     253                 :            : 
     254         [ #  # ]:          0 :         if (n != 0)
     255                 :          0 :                 num = apply_filter(mb, rc, num, drop);
     256                 :            : 
     257                 :          0 :         return num;
     258                 :            : }
     259                 :            : 
     260                 :            : /*
     261                 :            :  * RX/TX callbacks for raw data bpf.
     262                 :            :  */
     263                 :            : 
     264                 :            : static uint16_t
     265                 :          0 : bpf_rx_callback_vm(__rte_unused uint16_t port, __rte_unused uint16_t queue,
     266                 :            :         struct rte_mbuf *pkt[], uint16_t nb_pkts,
     267                 :            :         __rte_unused uint16_t max_pkts, void *user_param)
     268                 :            : {
     269                 :            :         struct bpf_eth_cbi *cbi;
     270                 :            :         uint16_t rc;
     271                 :            : 
     272                 :            :         cbi = user_param;
     273                 :            : 
     274                 :            :         bpf_eth_cbi_inuse(cbi);
     275         [ #  # ]:          0 :         rc = (cbi->cb != NULL) ?
     276                 :          0 :                 pkt_filter_vm(cbi->bpf, pkt, nb_pkts, 1) :
     277                 :            :                 nb_pkts;
     278                 :            :         bpf_eth_cbi_unuse(cbi);
     279                 :          0 :         return rc;
     280                 :            : }
     281                 :            : 
     282                 :            : static uint16_t
     283                 :          0 : bpf_rx_callback_jit(__rte_unused uint16_t port, __rte_unused uint16_t queue,
     284                 :            :         struct rte_mbuf *pkt[], uint16_t nb_pkts,
     285                 :            :         __rte_unused uint16_t max_pkts, void *user_param)
     286                 :            : {
     287                 :            :         struct bpf_eth_cbi *cbi;
     288                 :            :         uint16_t rc;
     289                 :            : 
     290                 :            :         cbi = user_param;
     291                 :            :         bpf_eth_cbi_inuse(cbi);
     292         [ #  # ]:          0 :         rc = (cbi->cb != NULL) ?
     293                 :          0 :                 pkt_filter_jit(&cbi->jit, pkt, nb_pkts, 1) :
     294                 :            :                 nb_pkts;
     295                 :            :         bpf_eth_cbi_unuse(cbi);
     296                 :          0 :         return rc;
     297                 :            : }
     298                 :            : 
     299                 :            : static uint16_t
     300                 :          0 : bpf_tx_callback_vm(__rte_unused uint16_t port, __rte_unused uint16_t queue,
     301                 :            :         struct rte_mbuf *pkt[], uint16_t nb_pkts, void *user_param)
     302                 :            : {
     303                 :            :         struct bpf_eth_cbi *cbi;
     304                 :            :         uint16_t rc;
     305                 :            : 
     306                 :            :         cbi = user_param;
     307                 :            :         bpf_eth_cbi_inuse(cbi);
     308         [ #  # ]:          0 :         rc = (cbi->cb != NULL) ?
     309                 :          0 :                 pkt_filter_vm(cbi->bpf, pkt, nb_pkts, 0) :
     310                 :            :                 nb_pkts;
     311                 :            :         bpf_eth_cbi_unuse(cbi);
     312                 :          0 :         return rc;
     313                 :            : }
     314                 :            : 
     315                 :            : static uint16_t
     316                 :          0 : bpf_tx_callback_jit(__rte_unused uint16_t port, __rte_unused uint16_t queue,
     317                 :            :         struct rte_mbuf *pkt[], uint16_t nb_pkts, void *user_param)
     318                 :            : {
     319                 :            :         struct bpf_eth_cbi *cbi;
     320                 :            :         uint16_t rc;
     321                 :            : 
     322                 :            :         cbi = user_param;
     323                 :            :         bpf_eth_cbi_inuse(cbi);
     324         [ #  # ]:          0 :         rc = (cbi->cb != NULL) ?
     325                 :          0 :                 pkt_filter_jit(&cbi->jit, pkt, nb_pkts, 0) :
     326                 :            :                 nb_pkts;
     327                 :            :         bpf_eth_cbi_unuse(cbi);
     328                 :          0 :         return rc;
     329                 :            : }
     330                 :            : 
     331                 :            : /*
     332                 :            :  * RX/TX callbacks for mbuf.
     333                 :            :  */
     334                 :            : 
     335                 :            : static uint16_t
     336                 :          0 : bpf_rx_callback_mb_vm(__rte_unused uint16_t port, __rte_unused uint16_t queue,
     337                 :            :         struct rte_mbuf *pkt[], uint16_t nb_pkts,
     338                 :            :         __rte_unused uint16_t max_pkts, void *user_param)
     339                 :            : {
     340                 :            :         struct bpf_eth_cbi *cbi;
     341                 :            :         uint16_t rc;
     342                 :            : 
     343                 :            :         cbi = user_param;
     344                 :            :         bpf_eth_cbi_inuse(cbi);
     345         [ #  # ]:          0 :         rc = (cbi->cb != NULL) ?
     346                 :          0 :                 pkt_filter_mb_vm(cbi->bpf, pkt, nb_pkts, 1) :
     347                 :            :                 nb_pkts;
     348                 :            :         bpf_eth_cbi_unuse(cbi);
     349                 :          0 :         return rc;
     350                 :            : }
     351                 :            : 
     352                 :            : static uint16_t
     353                 :          0 : bpf_rx_callback_mb_jit(__rte_unused uint16_t port, __rte_unused uint16_t queue,
     354                 :            :         struct rte_mbuf *pkt[], uint16_t nb_pkts,
     355                 :            :         __rte_unused uint16_t max_pkts, void *user_param)
     356                 :            : {
     357                 :            :         struct bpf_eth_cbi *cbi;
     358                 :            :         uint16_t rc;
     359                 :            : 
     360                 :            :         cbi = user_param;
     361                 :            :         bpf_eth_cbi_inuse(cbi);
     362         [ #  # ]:          0 :         rc = (cbi->cb != NULL) ?
     363                 :          0 :                 pkt_filter_mb_jit(&cbi->jit, pkt, nb_pkts, 1) :
     364                 :            :                 nb_pkts;
     365                 :            :         bpf_eth_cbi_unuse(cbi);
     366                 :          0 :         return rc;
     367                 :            : }
     368                 :            : 
     369                 :            : static uint16_t
     370                 :          0 : bpf_tx_callback_mb_vm(__rte_unused uint16_t port, __rte_unused uint16_t queue,
     371                 :            :         struct rte_mbuf *pkt[], uint16_t nb_pkts, void *user_param)
     372                 :            : {
     373                 :            :         struct bpf_eth_cbi *cbi;
     374                 :            :         uint16_t rc;
     375                 :            : 
     376                 :            :         cbi = user_param;
     377                 :            :         bpf_eth_cbi_inuse(cbi);
     378         [ #  # ]:          0 :         rc = (cbi->cb != NULL) ?
     379                 :          0 :                 pkt_filter_mb_vm(cbi->bpf, pkt, nb_pkts, 0) :
     380                 :            :                 nb_pkts;
     381                 :            :         bpf_eth_cbi_unuse(cbi);
     382                 :          0 :         return rc;
     383                 :            : }
     384                 :            : 
     385                 :            : static uint16_t
     386                 :          0 : bpf_tx_callback_mb_jit(__rte_unused uint16_t port, __rte_unused uint16_t queue,
     387                 :            :         struct rte_mbuf *pkt[], uint16_t nb_pkts, void *user_param)
     388                 :            : {
     389                 :            :         struct bpf_eth_cbi *cbi;
     390                 :            :         uint16_t rc;
     391                 :            : 
     392                 :            :         cbi = user_param;
     393                 :            :         bpf_eth_cbi_inuse(cbi);
     394         [ #  # ]:          0 :         rc = (cbi->cb != NULL) ?
     395                 :          0 :                 pkt_filter_mb_jit(&cbi->jit, pkt, nb_pkts, 0) :
     396                 :            :                 nb_pkts;
     397                 :            :         bpf_eth_cbi_unuse(cbi);
     398                 :          0 :         return rc;
     399                 :            : }
     400                 :            : 
     401                 :            : static rte_rx_callback_fn
     402                 :            : select_rx_callback(enum rte_bpf_arg_type type, uint32_t flags)
     403                 :            : {
     404                 :          0 :         if (flags & RTE_BPF_ETH_F_JIT) {
     405         [ #  # ]:          0 :                 if (type == RTE_BPF_ARG_PTR)
     406                 :            :                         return bpf_rx_callback_jit;
     407         [ #  # ]:          0 :                 else if (type == RTE_BPF_ARG_PTR_MBUF)
     408                 :          0 :                         return bpf_rx_callback_mb_jit;
     409         [ #  # ]:          0 :         } else if (type == RTE_BPF_ARG_PTR)
     410                 :            :                 return bpf_rx_callback_vm;
     411         [ #  # ]:          0 :         else if (type == RTE_BPF_ARG_PTR_MBUF)
     412                 :          0 :                 return bpf_rx_callback_mb_vm;
     413                 :            : 
     414                 :            :         return NULL;
     415                 :            : }
     416                 :            : 
     417                 :            : static rte_tx_callback_fn
     418                 :            : select_tx_callback(enum rte_bpf_arg_type type, uint32_t flags)
     419                 :            : {
     420                 :          0 :         if (flags & RTE_BPF_ETH_F_JIT) {
     421         [ #  # ]:          0 :                 if (type == RTE_BPF_ARG_PTR)
     422                 :            :                         return bpf_tx_callback_jit;
     423         [ #  # ]:          0 :                 else if (type == RTE_BPF_ARG_PTR_MBUF)
     424                 :          0 :                         return bpf_tx_callback_mb_jit;
     425         [ #  # ]:          0 :         } else if (type == RTE_BPF_ARG_PTR)
     426                 :            :                 return bpf_tx_callback_vm;
     427         [ #  # ]:          0 :         else if (type == RTE_BPF_ARG_PTR_MBUF)
     428                 :          0 :                 return bpf_tx_callback_mb_vm;
     429                 :            : 
     430                 :            :         return NULL;
     431                 :            : }
     432                 :            : 
     433                 :            : /*
     434                 :            :  * helper function to perform BPF unload for given port/queue.
     435                 :            :  * have to introduce extra complexity (and possible slowdown) here,
     436                 :            :  * as right now there is no safe generic way to remove RX/TX callback
     437                 :            :  * while IO is active.
     438                 :            :  * Still don't free memory allocated for callback handle itself,
     439                 :            :  * again right now there is no safe way to do that without stopping RX/TX
     440                 :            :  * on given port/queue first.
     441                 :            :  */
     442                 :            : static void
     443                 :          0 : bpf_eth_cbi_unload(struct bpf_eth_cbi *bc)
     444                 :            : {
     445                 :            :         /* mark this cbi as empty */
     446                 :          0 :         bc->cb = NULL;
     447                 :            : 
     448                 :            :         /* make sure datapath doesn't use bpf anymore, then destroy bpf */
     449                 :          0 :         bpf_eth_cbi_wait(bc);
     450                 :          0 :         rte_bpf_destroy(bc->bpf);
     451                 :            :         bpf_eth_cbi_cleanup(bc);
     452                 :          0 : }
     453                 :            : 
     454                 :            : static void
     455                 :          0 : bpf_eth_unload(struct bpf_eth_cbh *cbh, uint16_t port, uint16_t queue)
     456                 :            : {
     457                 :            :         struct bpf_eth_cbi *bc;
     458                 :            : 
     459                 :          0 :         bc = bpf_eth_cbh_find(cbh, port, queue);
     460   [ #  #  #  # ]:          0 :         if (bc == NULL || bc->cb == NULL)
     461                 :            :                 return;
     462                 :            : 
     463         [ #  # ]:          0 :         if (cbh->type == BPF_ETH_RX)
     464                 :          0 :                 rte_eth_remove_rx_callback(port, queue, bc->cb);
     465                 :            :         else
     466                 :          0 :                 rte_eth_remove_tx_callback(port, queue, bc->cb);
     467                 :            : 
     468                 :          0 :         bpf_eth_cbi_unload(bc);
     469                 :            : }
     470                 :            : 
     471                 :            : 
     472                 :            : RTE_EXPORT_SYMBOL(rte_bpf_eth_rx_unload)
     473                 :            : void
     474                 :          0 : rte_bpf_eth_rx_unload(uint16_t port, uint16_t queue)
     475                 :            : {
     476                 :            :         struct bpf_eth_cbh *cbh;
     477                 :            : 
     478                 :            :         cbh = &rx_cbh;
     479                 :            :         rte_spinlock_lock(&cbh->lock);
     480                 :          0 :         bpf_eth_unload(cbh, port, queue);
     481                 :            :         rte_spinlock_unlock(&cbh->lock);
     482                 :          0 : }
     483                 :            : 
     484                 :            : RTE_EXPORT_SYMBOL(rte_bpf_eth_tx_unload)
     485                 :            : void
     486                 :          0 : rte_bpf_eth_tx_unload(uint16_t port, uint16_t queue)
     487                 :            : {
     488                 :            :         struct bpf_eth_cbh *cbh;
     489                 :            : 
     490                 :            :         cbh = &tx_cbh;
     491                 :            :         rte_spinlock_lock(&cbh->lock);
     492                 :          0 :         bpf_eth_unload(cbh, port, queue);
     493                 :            :         rte_spinlock_unlock(&cbh->lock);
     494                 :          0 : }
     495                 :            : 
     496                 :            : static int
     497                 :          0 : bpf_eth_elf_install(struct bpf_eth_cbh *cbh, uint16_t port, uint16_t queue,
     498                 :            :         struct rte_bpf *bpf, uint32_t flags)
     499                 :            : {
     500                 :            :         int32_t rc;
     501                 :            :         struct bpf_eth_cbi *bc;
     502                 :            :         rte_rx_callback_fn frx;
     503                 :            :         rte_tx_callback_fn ftx;
     504                 :            :         struct rte_bpf_jit jit;
     505                 :            : 
     506                 :            :         frx = NULL;
     507                 :            :         ftx = NULL;
     508                 :            : 
     509   [ #  #  #  #  :          0 :         if (bpf == NULL || rte_eth_dev_is_valid_port(port) == 0 ||
                   #  # ]
     510                 :            :                         queue >= RTE_MAX_QUEUES_PER_PORT)
     511                 :          0 :                 return -EINVAL;
     512                 :            : 
     513         [ #  # ]:          0 :         if (bpf->prm.nb_prog_arg != 1)
     514                 :            :                 return -EINVAL;
     515                 :            : 
     516         [ #  # ]:          0 :         if (cbh->type == BPF_ETH_RX)
     517         [ #  # ]:          0 :                 frx = select_rx_callback(bpf->prm.prog_arg[0].type, flags);
     518                 :            :         else
     519         [ #  # ]:          0 :                 ftx = select_tx_callback(bpf->prm.prog_arg[0].type, flags);
     520                 :            : 
     521         [ #  # ]:          0 :         if (frx == NULL && ftx == NULL) {
     522                 :          0 :                 RTE_BPF_LOG_LINE(ERR, "%s(%u, %u): no callback selected;",
     523                 :            :                         __func__, port, queue);
     524                 :          0 :                 return -EINVAL;
     525                 :            :         }
     526                 :            : 
     527                 :          0 :         rte_bpf_get_jit(bpf, &jit);
     528                 :            : 
     529   [ #  #  #  # ]:          0 :         if ((flags & RTE_BPF_ETH_F_JIT) != 0 && jit.func == NULL) {
     530                 :          0 :                 RTE_BPF_LOG_LINE(ERR, "%s(%u, %u): no JIT generated;",
     531                 :            :                         __func__, port, queue);
     532                 :          0 :                 return -ENOTSUP;
     533                 :            :         }
     534                 :            : 
     535                 :            :         /* setup/update global callback info */
     536                 :          0 :         bc = bpf_eth_cbh_add(cbh, port, queue);
     537         [ #  # ]:          0 :         if (bc == NULL)
     538                 :            :                 return -ENOMEM;
     539                 :            : 
     540                 :            :         /* remove old one, if any */
     541         [ #  # ]:          0 :         if (bc->cb != NULL)
     542                 :          0 :                 bpf_eth_unload(cbh, port, queue);
     543                 :            : 
     544                 :          0 :         bc->bpf = bpf;
     545                 :          0 :         bc->jit = jit;
     546                 :            : 
     547         [ #  # ]:          0 :         if (cbh->type == BPF_ETH_RX)
     548                 :          0 :                 bc->cb = rte_eth_add_rx_callback(port, queue, frx, bc);
     549                 :            :         else
     550                 :          0 :                 bc->cb = rte_eth_add_tx_callback(port, queue, ftx, bc);
     551                 :            : 
     552         [ #  # ]:          0 :         if (bc->cb == NULL) {
     553                 :          0 :                 rc = -rte_errno;
     554                 :            :                 bpf_eth_cbi_cleanup(bc);
     555                 :            :         } else
     556                 :            :                 rc = 0;
     557                 :            : 
     558                 :            :         return rc;
     559                 :            : }
     560                 :            : 
     561                 :            : RTE_EXPORT_SYMBOL(rte_bpf_eth_rx_elf_load)
     562                 :            : int
     563                 :          0 : rte_bpf_eth_rx_elf_load(uint16_t port, uint16_t queue,
     564                 :            :         const struct rte_bpf_prm *prm, const char *fname, const char *sname,
     565                 :            :         uint32_t flags)
     566                 :            : {
     567                 :            :         struct rte_bpf *bpf;
     568                 :            :         int32_t rc;
     569                 :            : 
     570                 :          0 :         bpf = rte_bpf_elf_load(prm, fname, sname);
     571         [ #  # ]:          0 :         if (bpf == NULL)
     572                 :          0 :                 return -rte_errno;
     573                 :            : 
     574                 :          0 :         rc = rte_bpf_eth_rx_install(port, queue, bpf, flags);
     575                 :            : 
     576         [ #  # ]:          0 :         if (rc < 0)
     577                 :          0 :                 rte_bpf_destroy(bpf);
     578                 :            : 
     579                 :            :         return rc;
     580                 :            : }
     581                 :            : 
     582                 :            : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_bpf_eth_rx_install, 26.07)
     583                 :            : int
     584                 :          0 : rte_bpf_eth_rx_install(uint16_t port, uint16_t queue, struct rte_bpf *bpf,
     585                 :            :         uint32_t flags)
     586                 :            : {
     587                 :            :         int32_t rc;
     588                 :            :         struct bpf_eth_cbh *cbh;
     589                 :            : 
     590                 :            :         cbh = &rx_cbh;
     591                 :            :         rte_spinlock_lock(&cbh->lock);
     592                 :          0 :         rc = bpf_eth_elf_install(cbh, port, queue, bpf, flags);
     593                 :            :         rte_spinlock_unlock(&cbh->lock);
     594                 :            : 
     595                 :          0 :         return rc;
     596                 :            : }
     597                 :            : 
     598                 :            : RTE_EXPORT_SYMBOL(rte_bpf_eth_tx_elf_load)
     599                 :            : int
     600                 :          0 : rte_bpf_eth_tx_elf_load(uint16_t port, uint16_t queue,
     601                 :            :         const struct rte_bpf_prm *prm, const char *fname, const char *sname,
     602                 :            :         uint32_t flags)
     603                 :            : {
     604                 :            :         struct rte_bpf *bpf;
     605                 :            :         int32_t rc;
     606                 :            : 
     607                 :          0 :         bpf = rte_bpf_elf_load(prm, fname, sname);
     608         [ #  # ]:          0 :         if (bpf == NULL)
     609                 :          0 :                 return -rte_errno;
     610                 :            : 
     611                 :          0 :         rc = rte_bpf_eth_tx_install(port, queue, bpf, flags);
     612                 :            : 
     613         [ #  # ]:          0 :         if (rc < 0)
     614                 :          0 :                 rte_bpf_destroy(bpf);
     615                 :            : 
     616                 :            :         return rc;
     617                 :            : }
     618                 :            : 
     619                 :            : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_bpf_eth_tx_install, 26.07)
     620                 :            : int
     621                 :          0 : rte_bpf_eth_tx_install(uint16_t port, uint16_t queue, struct rte_bpf *bpf,
     622                 :            :         uint32_t flags)
     623                 :            : {
     624                 :            :         int32_t rc;
     625                 :            :         struct bpf_eth_cbh *cbh;
     626                 :            : 
     627                 :            :         cbh = &tx_cbh;
     628                 :            :         rte_spinlock_lock(&cbh->lock);
     629                 :          0 :         rc = bpf_eth_elf_install(cbh, port, queue, bpf, flags);
     630                 :            :         rte_spinlock_unlock(&cbh->lock);
     631                 :            : 
     632                 :          0 :         return rc;
     633                 :            : }

Generated by: LCOV version 1.14