LCOV - code coverage report
Current view: top level - lib/vhost - virtio_net.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 1475 0.0 %
Date: 2025-05-01 17:49:45 Functions: 0 29 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 3275 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2010-2016 Intel Corporation
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <stdint.h>
       6                 :            : #include <stdbool.h>
       7                 :            : #include <linux/virtio_net.h>
       8                 :            : 
       9                 :            : #include <eal_export.h>
      10                 :            : #include <rte_mbuf.h>
      11                 :            : #include <rte_memcpy.h>
      12                 :            : #include <rte_net.h>
      13                 :            : #include <rte_ether.h>
      14                 :            : #include <rte_ip.h>
      15                 :            : #include <rte_dmadev.h>
      16                 :            : #include <rte_vhost.h>
      17                 :            : #include <rte_tcp.h>
      18                 :            : #include <rte_udp.h>
      19                 :            : #include <rte_sctp.h>
      20                 :            : #include <rte_arp.h>
      21                 :            : #include <rte_spinlock.h>
      22                 :            : #include <rte_malloc.h>
      23                 :            : #include <rte_vhost_async.h>
      24                 :            : 
      25                 :            : #include "iotlb.h"
      26                 :            : #include "vhost.h"
      27                 :            : 
      28                 :            : #define MAX_BATCH_LEN 256
      29                 :            : 
      30                 :            : static __rte_always_inline uint16_t
      31                 :            : async_poll_dequeue_completed(struct virtio_net *dev, struct vhost_virtqueue *vq,
      32                 :            :                 struct rte_mbuf **pkts, uint16_t count, int16_t dma_id,
      33                 :            :                 uint16_t vchan_id, bool legacy_ol_flags);
      34                 :            : 
      35                 :            : /* DMA device copy operation tracking array. */
      36                 :            : struct async_dma_info dma_copy_track[RTE_DMADEV_DEFAULT_MAX];
      37                 :            : 
      38                 :            : static  __rte_always_inline bool
      39                 :            : rxvq_is_mergeable(struct virtio_net *dev)
      40                 :            : {
      41                 :          0 :         return dev->features & (1ULL << VIRTIO_NET_F_MRG_RXBUF);
      42                 :            : }
      43                 :            : 
      44                 :            : static  __rte_always_inline bool
      45                 :            : virtio_net_is_inorder(struct virtio_net *dev)
      46                 :            : {
      47                 :          0 :         return dev->features & (1ULL << VIRTIO_F_IN_ORDER);
      48                 :            : }
      49                 :            : 
      50                 :            : static bool
      51                 :            : is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t nr_vring)
      52                 :            : {
      53   [ #  #  #  #  :          0 :         return (is_tx ^ (idx & 1)) == 0 && idx < nr_vring;
          #  #  #  #  #  
                      # ]
      54                 :            : }
      55                 :            : 
      56                 :            : static inline void
      57                 :          0 : vhost_queue_stats_update(const struct virtio_net *dev, struct vhost_virtqueue *vq,
      58                 :            :                 struct rte_mbuf **pkts, uint16_t count)
      59                 :            :         __rte_requires_shared_capability(&vq->access_lock)
      60                 :            : {
      61                 :            :         struct virtqueue_stats *stats = &vq->stats;
      62                 :            :         int i;
      63                 :            : 
      64         [ #  # ]:          0 :         if (!(dev->flags & VIRTIO_DEV_STATS_ENABLED))
      65                 :            :                 return;
      66                 :            : 
      67         [ #  # ]:          0 :         for (i = 0; i < count; i++) {
      68                 :            :                 const struct rte_ether_addr *ea;
      69                 :          0 :                 const struct rte_mbuf *pkt = pkts[i];
      70                 :          0 :                 uint32_t pkt_len = rte_pktmbuf_pkt_len(pkt);
      71                 :            : 
      72                 :          0 :                 stats->packets++;
      73                 :          0 :                 stats->bytes += pkt_len;
      74                 :            : 
      75         [ #  # ]:          0 :                 if (pkt_len >= 1024)
      76         [ #  # ]:          0 :                         stats->size_bins[6 + (pkt_len > 1518)]++;
      77         [ #  # ]:          0 :                 else if (pkt_len <= 64)
      78                 :          0 :                         stats->size_bins[pkt_len >> 6]++;
      79                 :            :                 else
      80                 :          0 :                         stats->size_bins[32UL - rte_clz32(pkt_len) - 5]++;
      81                 :            : 
      82         [ #  # ]:          0 :                 ea = rte_pktmbuf_mtod(pkt, const struct rte_ether_addr *);
      83                 :            :                 RTE_BUILD_BUG_ON(offsetof(struct virtqueue_stats, broadcast) !=
      84                 :            :                                 offsetof(struct virtqueue_stats, multicast) + sizeof(uint64_t));
      85         [ #  # ]:          0 :                 if (unlikely(rte_is_multicast_ether_addr(ea)))
      86                 :          0 :                         (&stats->multicast)[rte_is_broadcast_ether_addr(ea)]++;
      87                 :            :         }
      88                 :            : }
      89                 :            : 
      90                 :            : static __rte_always_inline int64_t
      91                 :            : vhost_async_dma_transfer_one(struct virtio_net *dev, struct vhost_virtqueue *vq,
      92                 :            :                 int16_t dma_id, uint16_t vchan_id, uint16_t flag_idx,
      93                 :            :                 struct vhost_iov_iter *pkt)
      94                 :            :         __rte_requires_shared_capability(&vq->access_lock)
      95                 :            : {
      96                 :          0 :         struct async_dma_vchan_info *dma_info = &dma_copy_track[dma_id].vchans[vchan_id];
      97                 :          0 :         uint16_t ring_mask = dma_info->ring_mask;
      98                 :            :         static bool vhost_async_dma_copy_log;
      99                 :            : 
     100                 :            : 
     101                 :          0 :         struct vhost_iovec *iov = pkt->iov;
     102                 :            :         int copy_idx = 0;
     103                 :          0 :         uint32_t nr_segs = pkt->nr_segs;
     104                 :            :         uint16_t i;
     105                 :            : 
     106   [ #  #  #  #  :          0 :         if (rte_dma_burst_capacity(dma_id, vchan_id) < nr_segs)
          #  #  #  #  #  
                #  #  # ]
     107                 :            :                 return -1;
     108                 :            : 
     109   [ #  #  #  #  :          0 :         for (i = 0; i < nr_segs; i++) {
          #  #  #  #  #  
                #  #  # ]
     110                 :          0 :                 copy_idx = rte_dma_copy(dma_id, vchan_id, (rte_iova_t)iov[i].src_addr,
     111                 :          0 :                                 (rte_iova_t)iov[i].dst_addr, iov[i].len, RTE_DMA_OP_FLAG_LLC);
     112                 :            :                 /**
     113                 :            :                  * Since all memory is pinned and DMA vChannel
     114                 :            :                  * ring has enough space, failure should be a
     115                 :            :                  * rare case. If failure happens, it means DMA
     116                 :            :                  * device encounters serious errors; in this
     117                 :            :                  * case, please stop async data-path and check
     118                 :            :                  * what has happened to DMA device.
     119                 :            :                  */
     120   [ #  #  #  #  :          0 :                 if (unlikely(copy_idx < 0)) {
          #  #  #  #  #  
                #  #  # ]
     121   [ #  #  #  #  :          0 :                         if (!vhost_async_dma_copy_log) {
          #  #  #  #  #  
                #  #  # ]
     122                 :          0 :                                 VHOST_DATA_LOG(dev->ifname, ERR,
     123                 :            :                                         "DMA copy failed for channel %d:%u",
     124                 :            :                                         dma_id, vchan_id);
     125                 :          0 :                                 vhost_async_dma_copy_log = true;
     126                 :            :                         }
     127                 :            :                         return -1;
     128                 :            :                 }
     129                 :            :         }
     130                 :            : 
     131                 :            :         /**
     132                 :            :          * Only store packet completion flag address in the last copy's
     133                 :            :          * slot, and other slots are set to NULL.
     134                 :            :          */
     135                 :          0 :         dma_info->pkts_cmpl_flag_addr[copy_idx & ring_mask] = &vq->async->pkts_cmpl_flag[flag_idx];
     136                 :            : 
     137                 :          0 :         return nr_segs;
     138                 :            : }
     139                 :            : 
     140                 :            : static __rte_always_inline uint16_t
     141                 :            : vhost_async_dma_transfer(struct virtio_net *dev, struct vhost_virtqueue *vq,
     142                 :            :                 int16_t dma_id, uint16_t vchan_id, uint16_t head_idx,
     143                 :            :                 struct vhost_iov_iter *pkts, uint16_t nr_pkts)
     144                 :            :         __rte_requires_shared_capability(&vq->access_lock)
     145                 :            : {
     146                 :          0 :         struct async_dma_vchan_info *dma_info = &dma_copy_track[dma_id].vchans[vchan_id];
     147                 :            :         int64_t ret, nr_copies = 0;
     148                 :            :         uint16_t pkt_idx;
     149                 :            : 
     150                 :          0 :         rte_spinlock_lock(&dma_info->dma_lock);
     151                 :            : 
     152   [ #  #  #  #  :          0 :         for (pkt_idx = 0; pkt_idx < nr_pkts; pkt_idx++) {
          #  #  #  #  #  
                #  #  # ]
     153                 :          0 :                 ret = vhost_async_dma_transfer_one(dev, vq, dma_id, vchan_id, head_idx,
     154                 :          0 :                                 &pkts[pkt_idx]);
     155   [ #  #  #  #  :          0 :                 if (unlikely(ret < 0))
          #  #  #  #  #  
                #  #  # ]
     156                 :            :                         break;
     157                 :            : 
     158                 :          0 :                 nr_copies += ret;
     159                 :          0 :                 head_idx++;
     160   [ #  #  #  #  :          0 :                 if (head_idx >= vq->size)
          #  #  #  #  #  
                #  #  # ]
     161                 :          0 :                         head_idx -= vq->size;
     162                 :            :         }
     163                 :            : 
     164   [ #  #  #  #  :          0 :         if (likely(nr_copies > 0))
          #  #  #  #  #  
                #  #  # ]
     165                 :            :                 rte_dma_submit(dma_id, vchan_id);
     166                 :            : 
     167                 :            :         rte_spinlock_unlock(&dma_info->dma_lock);
     168                 :            : 
     169                 :            :         return pkt_idx;
     170                 :            : }
     171                 :            : 
     172                 :            : static __rte_always_inline uint16_t
     173                 :            : vhost_async_dma_check_completed(struct virtio_net *dev, int16_t dma_id, uint16_t vchan_id,
     174                 :            :                 uint16_t max_pkts)
     175                 :            : {
     176                 :          0 :         struct async_dma_vchan_info *dma_info = &dma_copy_track[dma_id].vchans[vchan_id];
     177                 :          0 :         uint16_t ring_mask = dma_info->ring_mask;
     178                 :          0 :         uint16_t last_idx = 0;
     179                 :            :         uint16_t nr_copies;
     180                 :            :         uint16_t copy_idx;
     181                 :            :         uint16_t i;
     182                 :          0 :         bool has_error = false;
     183                 :            :         static bool vhost_async_dma_complete_log;
     184                 :            : 
     185                 :          0 :         rte_spinlock_lock(&dma_info->dma_lock);
     186                 :            : 
     187                 :            :         /**
     188                 :            :          * Print error log for debugging, if DMA reports error during
     189                 :            :          * DMA transfer. We do not handle error in vhost level.
     190                 :            :          */
     191                 :            :         nr_copies = rte_dma_completed(dma_id, vchan_id, max_pkts, &last_idx, &has_error);
     192   [ #  #  #  #  :          0 :         if (unlikely(!vhost_async_dma_complete_log && has_error)) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
     193                 :          0 :                 VHOST_DATA_LOG(dev->ifname, ERR,
     194                 :            :                         "DMA completion failure on channel %d:%u",
     195                 :            :                         dma_id, vchan_id);
     196                 :          0 :                 vhost_async_dma_complete_log = true;
     197   [ #  #  #  #  :          0 :         } else if (nr_copies == 0) {
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     198                 :          0 :                 goto out;
     199                 :            :         }
     200                 :            : 
     201                 :          0 :         copy_idx = last_idx - nr_copies + 1;
     202   [ #  #  #  #  :          0 :         for (i = 0; i < nr_copies; i++) {
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     203                 :            :                 bool *flag;
     204                 :            : 
     205                 :          0 :                 flag = dma_info->pkts_cmpl_flag_addr[copy_idx & ring_mask];
     206   [ #  #  #  #  :          0 :                 if (flag) {
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     207                 :            :                         /**
     208                 :            :                          * Mark the packet flag as received. The flag
     209                 :            :                          * could belong to another virtqueue but write
     210                 :            :                          * is atomic.
     211                 :            :                          */
     212                 :          0 :                         *flag = true;
     213                 :          0 :                         dma_info->pkts_cmpl_flag_addr[copy_idx & ring_mask] = NULL;
     214                 :            :                 }
     215                 :          0 :                 copy_idx++;
     216                 :            :         }
     217                 :            : 
     218                 :          0 : out:
     219                 :            :         rte_spinlock_unlock(&dma_info->dma_lock);
     220                 :            :         return nr_copies;
     221                 :            : }
     222                 :            : 
     223                 :            : static inline void
     224                 :          0 : do_data_copy_enqueue(struct virtio_net *dev, struct vhost_virtqueue *vq)
     225                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
     226                 :            : {
     227                 :          0 :         struct batch_copy_elem *elem = vq->batch_copy_elems;
     228                 :          0 :         uint16_t count = vq->batch_copy_nb_elems;
     229                 :            :         int i;
     230                 :            : 
     231         [ #  # ]:          0 :         for (i = 0; i < count; i++) {
     232         [ #  # ]:          0 :                 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
     233                 :          0 :                 vhost_log_cache_write_iova(dev, vq, elem[i].log_addr,
     234         [ #  # ]:          0 :                                            elem[i].len);
     235                 :            :                 PRINT_PACKET(dev, (uintptr_t)elem[i].dst, elem[i].len, 0);
     236                 :            :         }
     237                 :            : 
     238                 :          0 :         vq->batch_copy_nb_elems = 0;
     239                 :          0 : }
     240                 :            : 
     241                 :            : static inline void
     242                 :          0 : do_data_copy_dequeue(struct vhost_virtqueue *vq)
     243                 :            : {
     244                 :          0 :         struct batch_copy_elem *elem = vq->batch_copy_elems;
     245                 :          0 :         uint16_t count = vq->batch_copy_nb_elems;
     246                 :            :         int i;
     247                 :            : 
     248         [ #  # ]:          0 :         for (i = 0; i < count; i++)
     249         [ #  # ]:          0 :                 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
     250                 :            : 
     251                 :          0 :         vq->batch_copy_nb_elems = 0;
     252                 :          0 : }
     253                 :            : 
     254                 :            : static __rte_always_inline void
     255                 :            : do_flush_shadow_used_ring_split(struct virtio_net *dev,
     256                 :            :                         struct vhost_virtqueue *vq,
     257                 :            :                         uint16_t to, uint16_t from, uint16_t size)
     258                 :            : {
     259                 :          0 :         rte_memcpy(&vq->used->ring[to],
     260   [ #  #  #  #  :          0 :                         &vq->shadow_used_split[from],
                   #  # ]
     261                 :            :                         size * sizeof(struct vring_used_elem));
     262   [ #  #  #  #  :          0 :         vhost_log_cache_used_vring(dev, vq,
          #  #  #  #  #  
                #  #  # ]
     263                 :            :                         offsetof(struct vring_used, ring[to]),
     264                 :            :                         size * sizeof(struct vring_used_elem));
     265                 :            : }
     266                 :            : 
     267                 :            : static __rte_always_inline void
     268                 :            : flush_shadow_used_ring_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
     269                 :            : {
     270                 :          0 :         uint16_t used_idx = vq->last_used_idx & (vq->size - 1);
     271                 :            : 
     272   [ #  #  #  #  :          0 :         if (used_idx + vq->shadow_used_idx <= vq->size) {
                   #  # ]
     273                 :            :                 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0,
     274                 :            :                                           vq->shadow_used_idx);
     275                 :            :         } else {
     276                 :            :                 uint16_t size;
     277                 :            : 
     278                 :            :                 /* update used ring interval [used_idx, vq->size] */
     279   [ #  #  #  #  :          0 :                 size = vq->size - used_idx;
                   #  # ]
     280                 :            :                 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0, size);
     281                 :            : 
     282                 :            :                 /* update the left half used ring interval [0, left_size] */
     283                 :            :                 do_flush_shadow_used_ring_split(dev, vq, 0, size,
     284   [ #  #  #  #  :          0 :                                           vq->shadow_used_idx - size);
                   #  # ]
     285                 :            :         }
     286   [ #  #  #  #  :          0 :         vq->last_used_idx += vq->shadow_used_idx;
                   #  # ]
     287                 :            : 
     288                 :            :         vhost_log_cache_sync(dev, vq);
     289                 :            : 
     290                 :          0 :         rte_atomic_fetch_add_explicit((unsigned short __rte_atomic *)&vq->used->idx,
     291                 :            :                 vq->shadow_used_idx, rte_memory_order_release);
     292   [ #  #  #  #  :          0 :         vq->shadow_used_idx = 0;
                   #  # ]
     293                 :            :         vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
     294                 :            :                 sizeof(vq->used->idx));
     295                 :            : }
     296                 :            : 
     297                 :            : static __rte_always_inline void
     298                 :            : update_shadow_used_ring_split(struct vhost_virtqueue *vq,
     299                 :            :                          uint16_t desc_idx, uint32_t len)
     300                 :            : {
     301                 :          0 :         uint16_t i = vq->shadow_used_idx++;
     302                 :            : 
     303                 :          0 :         vq->shadow_used_split[i].id  = desc_idx;
     304                 :          0 :         vq->shadow_used_split[i].len = len;
     305                 :            : }
     306                 :            : 
     307                 :            : static __rte_always_inline void
     308                 :            : vhost_flush_enqueue_shadow_packed(struct virtio_net *dev,
     309                 :            :                                   struct vhost_virtqueue *vq)
     310                 :            : {
     311                 :            :         int i;
     312                 :          0 :         uint16_t used_idx = vq->last_used_idx;
     313                 :            :         uint16_t head_idx = vq->last_used_idx;
     314                 :            :         uint16_t head_flags = 0;
     315                 :            : 
     316                 :            :         /* Split loop in two to save memory barriers */
     317   [ #  #  #  #  :          0 :         for (i = 0; i < vq->shadow_used_idx; i++) {
                   #  # ]
     318                 :          0 :                 vq->desc_packed[used_idx].id = vq->shadow_used_packed[i].id;
     319                 :          0 :                 vq->desc_packed[used_idx].len = vq->shadow_used_packed[i].len;
     320                 :            : 
     321                 :          0 :                 used_idx += vq->shadow_used_packed[i].count;
     322   [ #  #  #  #  :          0 :                 if (used_idx >= vq->size)
                   #  # ]
     323                 :          0 :                         used_idx -= vq->size;
     324                 :            :         }
     325                 :            : 
     326                 :            :         /* The ordering for storing desc flags needs to be enforced. */
     327                 :            :         rte_atomic_thread_fence(rte_memory_order_release);
     328                 :            : 
     329   [ #  #  #  #  :          0 :         for (i = 0; i < vq->shadow_used_idx; i++) {
                   #  # ]
     330                 :            :                 uint16_t flags;
     331                 :            : 
     332   [ #  #  #  #  :          0 :                 if (vq->shadow_used_packed[i].len)
                   #  # ]
     333                 :            :                         flags = VRING_DESC_F_WRITE;
     334                 :            :                 else
     335                 :            :                         flags = 0;
     336                 :            : 
     337   [ #  #  #  #  :          0 :                 if (vq->used_wrap_counter) {
                   #  # ]
     338                 :            :                         flags |= VRING_DESC_F_USED;
     339                 :          0 :                         flags |= VRING_DESC_F_AVAIL;
     340                 :            :                 } else {
     341                 :            :                         flags &= ~VRING_DESC_F_USED;
     342                 :            :                         flags &= ~VRING_DESC_F_AVAIL;
     343                 :            :                 }
     344                 :            : 
     345   [ #  #  #  #  :          0 :                 if (i > 0) {
                   #  # ]
     346                 :          0 :                         vq->desc_packed[vq->last_used_idx].flags = flags;
     347                 :            : 
     348                 :          0 :                         vhost_log_cache_used_vring(dev, vq,
     349   [ #  #  #  #  :          0 :                                         vq->last_used_idx *
                   #  # ]
     350                 :            :                                         sizeof(struct vring_packed_desc),
     351                 :            :                                         sizeof(struct vring_packed_desc));
     352                 :            :                 } else {
     353                 :          0 :                         head_idx = vq->last_used_idx;
     354                 :            :                         head_flags = flags;
     355                 :            :                 }
     356                 :            : 
     357   [ #  #  #  #  :          0 :                 vq_inc_last_used_packed(vq, vq->shadow_used_packed[i].count);
                   #  # ]
     358                 :            :         }
     359                 :            : 
     360   [ #  #  #  #  :          0 :         vq->desc_packed[head_idx].flags = head_flags;
                   #  # ]
     361                 :            : 
     362                 :            :         vhost_log_cache_used_vring(dev, vq,
     363                 :            :                                 head_idx *
     364                 :            :                                 sizeof(struct vring_packed_desc),
     365                 :            :                                 sizeof(struct vring_packed_desc));
     366                 :            : 
     367   [ #  #  #  #  :          0 :         vq->shadow_used_idx = 0;
                   #  # ]
     368                 :            :         vhost_log_cache_sync(dev, vq);
     369                 :            : }
     370                 :            : 
     371                 :            : static __rte_always_inline void
     372                 :            : vhost_flush_dequeue_shadow_packed(struct virtio_net *dev,
     373                 :            :                                   struct vhost_virtqueue *vq)
     374                 :            : {
     375                 :          0 :         struct vring_used_elem_packed *used_elem = &vq->shadow_used_packed[0];
     376                 :            : 
     377                 :          0 :         vq->desc_packed[vq->shadow_last_used_idx].id = used_elem->id;
     378                 :            :         /* desc flags is the synchronization point for virtio packed vring */
     379                 :          0 :         rte_atomic_store_explicit(
     380                 :            :                 (unsigned short __rte_atomic *)&vq->desc_packed[vq->shadow_last_used_idx].flags,
     381                 :            :                 used_elem->flags, rte_memory_order_release);
     382                 :            : 
     383   [ #  #  #  # ]:          0 :         vhost_log_cache_used_vring(dev, vq, vq->shadow_last_used_idx *
     384                 :            :                                    sizeof(struct vring_packed_desc),
     385                 :            :                                    sizeof(struct vring_packed_desc));
     386   [ #  #  #  # ]:          0 :         vq->shadow_used_idx = 0;
     387                 :            :         vhost_log_cache_sync(dev, vq);
     388                 :            : }
     389                 :            : 
     390                 :            : static __rte_always_inline void
     391                 :            : vhost_flush_enqueue_batch_packed(struct virtio_net *dev,
     392                 :            :                                  struct vhost_virtqueue *vq,
     393                 :            :                                  uint64_t *lens,
     394                 :            :                                  uint16_t *ids)
     395                 :            : {
     396                 :            :         uint16_t i;
     397                 :            :         uint16_t flags;
     398                 :            :         uint16_t last_used_idx;
     399                 :            :         struct vring_packed_desc *desc_base;
     400                 :            : 
     401                 :          0 :         last_used_idx = vq->last_used_idx;
     402                 :          0 :         desc_base = &vq->desc_packed[last_used_idx];
     403                 :            : 
     404         [ #  # ]:          0 :         flags = PACKED_DESC_ENQUEUE_USED_FLAG(vq->used_wrap_counter);
     405                 :            : 
     406         [ #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
     407                 :          0 :                 desc_base[i].id = ids[i];
     408                 :          0 :                 desc_base[i].len = lens[i];
     409                 :            :         }
     410                 :            : 
     411                 :            :         rte_atomic_thread_fence(rte_memory_order_release);
     412                 :            : 
     413         [ #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
     414                 :          0 :                 desc_base[i].flags = flags;
     415                 :            :         }
     416                 :            : 
     417                 :            :         vhost_log_cache_used_vring(dev, vq, last_used_idx *
     418                 :            :                                    sizeof(struct vring_packed_desc),
     419                 :            :                                    sizeof(struct vring_packed_desc) *
     420                 :            :                                    PACKED_BATCH_SIZE);
     421                 :            :         vhost_log_cache_sync(dev, vq);
     422                 :            : 
     423                 :            :         vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
     424                 :            : }
     425                 :            : 
     426                 :            : static __rte_always_inline void
     427                 :            : vhost_async_shadow_enqueue_packed_batch(struct vhost_virtqueue *vq,
     428                 :            :                                  uint64_t *lens,
     429                 :            :                                  uint16_t *ids)
     430                 :            :         __rte_requires_capability(&vq->access_lock)
     431                 :            : {
     432                 :            :         uint16_t i;
     433                 :          0 :         struct vhost_async *async = vq->async;
     434                 :            : 
     435         [ #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
     436                 :          0 :                 async->buffers_packed[async->buffer_idx_packed].id  = ids[i];
     437                 :          0 :                 async->buffers_packed[async->buffer_idx_packed].len = lens[i];
     438                 :          0 :                 async->buffers_packed[async->buffer_idx_packed].count = 1;
     439                 :          0 :                 async->buffer_idx_packed++;
     440         [ #  # ]:          0 :                 if (async->buffer_idx_packed >= vq->size)
     441                 :          0 :                         async->buffer_idx_packed -= vq->size;
     442                 :            :         }
     443                 :            : }
     444                 :            : 
     445                 :            : static __rte_always_inline void
     446                 :            : vhost_async_shadow_dequeue_packed_batch(struct vhost_virtqueue *vq, uint16_t *ids)
     447                 :            :         __rte_requires_shared_capability(&vq->access_lock)
     448                 :            : {
     449                 :            :         uint16_t i;
     450                 :          0 :         struct vhost_async *async = vq->async;
     451                 :            : 
     452   [ #  #  #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
     453                 :          0 :                 async->buffers_packed[async->buffer_idx_packed].id  = ids[i];
     454                 :          0 :                 async->buffers_packed[async->buffer_idx_packed].len = 0;
     455                 :          0 :                 async->buffers_packed[async->buffer_idx_packed].count = 1;
     456                 :            : 
     457                 :          0 :                 async->buffer_idx_packed++;
     458   [ #  #  #  # ]:          0 :                 if (async->buffer_idx_packed >= vq->size)
     459                 :          0 :                         async->buffer_idx_packed -= vq->size;
     460                 :            :         }
     461                 :            : }
     462                 :            : 
     463                 :            : static __rte_always_inline void
     464                 :            : vhost_shadow_dequeue_batch_packed_inorder(struct vhost_virtqueue *vq,
     465                 :            :                                           uint16_t id)
     466                 :            : {
     467                 :          0 :         vq->shadow_used_packed[0].id = id;
     468                 :            : 
     469                 :          0 :         if (!vq->shadow_used_idx) {
     470                 :          0 :                 vq->shadow_last_used_idx = vq->last_used_idx;
     471                 :          0 :                 vq->shadow_used_packed[0].flags =
     472   [ #  #  #  # ]:          0 :                         PACKED_DESC_DEQUEUE_USED_FLAG(vq->used_wrap_counter);
     473                 :          0 :                 vq->shadow_used_packed[0].len = 0;
     474                 :          0 :                 vq->shadow_used_packed[0].count = 1;
     475                 :          0 :                 vq->shadow_used_idx++;
     476                 :            :         }
     477                 :            : 
     478                 :            :         vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
     479                 :            : }
     480                 :            : 
     481                 :            : static __rte_always_inline void
     482                 :            : vhost_shadow_dequeue_batch_packed(struct virtio_net *dev,
     483                 :            :                                   struct vhost_virtqueue *vq,
     484                 :            :                                   uint16_t *ids)
     485                 :            : {
     486                 :            :         uint16_t flags;
     487                 :            :         uint16_t i;
     488                 :            :         uint16_t begin;
     489                 :            : 
     490   [ #  #  #  # ]:          0 :         flags = PACKED_DESC_DEQUEUE_USED_FLAG(vq->used_wrap_counter);
     491                 :            : 
     492   [ #  #  #  # ]:          0 :         if (!vq->shadow_used_idx) {
     493                 :          0 :                 vq->shadow_last_used_idx = vq->last_used_idx;
     494                 :          0 :                 vq->shadow_used_packed[0].id  = ids[0];
     495                 :          0 :                 vq->shadow_used_packed[0].len = 0;
     496                 :          0 :                 vq->shadow_used_packed[0].count = 1;
     497                 :          0 :                 vq->shadow_used_packed[0].flags = flags;
     498                 :          0 :                 vq->shadow_used_idx++;
     499                 :            :                 begin = 1;
     500                 :            :         } else
     501                 :            :                 begin = 0;
     502                 :            : 
     503   [ #  #  #  # ]:          0 :         vhost_for_each_try_unroll(i, begin, PACKED_BATCH_SIZE) {
     504                 :          0 :                 vq->desc_packed[vq->last_used_idx + i].id = ids[i];
     505                 :          0 :                 vq->desc_packed[vq->last_used_idx + i].len = 0;
     506                 :            :         }
     507                 :            : 
     508                 :            :         rte_atomic_thread_fence(rte_memory_order_release);
     509   [ #  #  #  # ]:          0 :         vhost_for_each_try_unroll(i, begin, PACKED_BATCH_SIZE)
     510                 :          0 :                 vq->desc_packed[vq->last_used_idx + i].flags = flags;
     511                 :            : 
     512   [ #  #  #  # ]:          0 :         vhost_log_cache_used_vring(dev, vq, vq->last_used_idx *
     513                 :            :                                    sizeof(struct vring_packed_desc),
     514                 :            :                                    sizeof(struct vring_packed_desc) *
     515                 :            :                                    PACKED_BATCH_SIZE);
     516                 :            :         vhost_log_cache_sync(dev, vq);
     517                 :            : 
     518                 :            :         vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
     519                 :            : }
     520                 :            : 
     521                 :            : static __rte_always_inline void
     522                 :            : vhost_shadow_dequeue_single_packed(struct vhost_virtqueue *vq,
     523                 :            :                                    uint16_t buf_id,
     524                 :            :                                    uint16_t count)
     525                 :            : {
     526                 :            :         uint16_t flags;
     527                 :            : 
     528                 :          0 :         flags = vq->desc_packed[vq->last_used_idx].flags;
     529   [ #  #  #  # ]:          0 :         if (vq->used_wrap_counter) {
     530                 :            :                 flags |= VRING_DESC_F_USED;
     531                 :          0 :                 flags |= VRING_DESC_F_AVAIL;
     532                 :            :         } else {
     533                 :            :                 flags &= ~VRING_DESC_F_USED;
     534                 :          0 :                 flags &= ~VRING_DESC_F_AVAIL;
     535                 :            :         }
     536                 :            : 
     537   [ #  #  #  # ]:          0 :         if (!vq->shadow_used_idx) {
     538                 :          0 :                 vq->shadow_last_used_idx = vq->last_used_idx;
     539                 :            : 
     540                 :          0 :                 vq->shadow_used_packed[0].id  = buf_id;
     541                 :          0 :                 vq->shadow_used_packed[0].len = 0;
     542                 :          0 :                 vq->shadow_used_packed[0].flags = flags;
     543                 :          0 :                 vq->shadow_used_idx++;
     544                 :            :         } else {
     545                 :          0 :                 vq->desc_packed[vq->last_used_idx].id = buf_id;
     546                 :          0 :                 vq->desc_packed[vq->last_used_idx].len = 0;
     547                 :          0 :                 vq->desc_packed[vq->last_used_idx].flags = flags;
     548                 :            :         }
     549                 :            : 
     550                 :            :         vq_inc_last_used_packed(vq, count);
     551                 :            : }
     552                 :            : 
     553                 :            : static __rte_always_inline void
     554                 :            : vhost_shadow_dequeue_single_packed_inorder(struct vhost_virtqueue *vq,
     555                 :            :                                            uint16_t buf_id,
     556                 :            :                                            uint16_t count)
     557                 :            : {
     558                 :            :         uint16_t flags;
     559                 :            : 
     560                 :          0 :         vq->shadow_used_packed[0].id = buf_id;
     561                 :            : 
     562                 :          0 :         flags = vq->desc_packed[vq->last_used_idx].flags;
     563   [ #  #  #  # ]:          0 :         if (vq->used_wrap_counter) {
     564                 :            :                 flags |= VRING_DESC_F_USED;
     565                 :          0 :                 flags |= VRING_DESC_F_AVAIL;
     566                 :            :         } else {
     567                 :            :                 flags &= ~VRING_DESC_F_USED;
     568                 :          0 :                 flags &= ~VRING_DESC_F_AVAIL;
     569                 :            :         }
     570                 :            : 
     571   [ #  #  #  # ]:          0 :         if (!vq->shadow_used_idx) {
     572                 :          0 :                 vq->shadow_last_used_idx = vq->last_used_idx;
     573                 :          0 :                 vq->shadow_used_packed[0].len = 0;
     574                 :          0 :                 vq->shadow_used_packed[0].flags = flags;
     575                 :          0 :                 vq->shadow_used_idx++;
     576                 :            :         }
     577                 :            : 
     578                 :            :         vq_inc_last_used_packed(vq, count);
     579                 :            : }
     580                 :            : 
     581                 :            : static __rte_always_inline void
     582                 :            : vhost_shadow_enqueue_packed(struct vhost_virtqueue *vq,
     583                 :            :                                    uint32_t *len,
     584                 :            :                                    uint16_t *id,
     585                 :            :                                    uint16_t *count,
     586                 :            :                                    uint16_t num_buffers)
     587                 :            : {
     588                 :            :         uint16_t i;
     589                 :            : 
     590         [ #  # ]:          0 :         for (i = 0; i < num_buffers; i++) {
     591                 :            :                 /* enqueue shadow flush action aligned with batch num */
     592         [ #  # ]:          0 :                 if (!vq->shadow_used_idx)
     593                 :          0 :                         vq->shadow_aligned_idx = vq->last_used_idx &
     594                 :            :                                 PACKED_BATCH_MASK;
     595                 :          0 :                 vq->shadow_used_packed[vq->shadow_used_idx].id  = id[i];
     596                 :          0 :                 vq->shadow_used_packed[vq->shadow_used_idx].len = len[i];
     597                 :          0 :                 vq->shadow_used_packed[vq->shadow_used_idx].count = count[i];
     598                 :          0 :                 vq->shadow_aligned_idx += count[i];
     599                 :          0 :                 vq->shadow_used_idx++;
     600                 :            :         }
     601                 :            : }
     602                 :            : 
     603                 :            : static __rte_always_inline void
     604                 :            : vhost_async_shadow_enqueue_packed(struct vhost_virtqueue *vq,
     605                 :            :                                    uint32_t *len,
     606                 :            :                                    uint16_t *id,
     607                 :            :                                    uint16_t *count,
     608                 :            :                                    uint16_t num_buffers)
     609                 :            :         __rte_requires_capability(&vq->access_lock)
     610                 :            : {
     611                 :            :         uint16_t i;
     612                 :          0 :         struct vhost_async *async = vq->async;
     613                 :            : 
     614         [ #  # ]:          0 :         for (i = 0; i < num_buffers; i++) {
     615                 :          0 :                 async->buffers_packed[async->buffer_idx_packed].id  = id[i];
     616                 :          0 :                 async->buffers_packed[async->buffer_idx_packed].len = len[i];
     617                 :          0 :                 async->buffers_packed[async->buffer_idx_packed].count = count[i];
     618                 :          0 :                 async->buffer_idx_packed++;
     619         [ #  # ]:          0 :                 if (async->buffer_idx_packed >= vq->size)
     620                 :          0 :                         async->buffer_idx_packed -= vq->size;
     621                 :            :         }
     622                 :            : }
     623                 :            : 
     624                 :            : static __rte_always_inline void
     625                 :            : vhost_shadow_enqueue_single_packed(struct virtio_net *dev,
     626                 :            :                                    struct vhost_virtqueue *vq,
     627                 :            :                                    uint32_t *len,
     628                 :            :                                    uint16_t *id,
     629                 :            :                                    uint16_t *count,
     630                 :            :                                    uint16_t num_buffers)
     631                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
     632                 :            : {
     633                 :            :         vhost_shadow_enqueue_packed(vq, len, id, count, num_buffers);
     634                 :            : 
     635         [ #  # ]:          0 :         if (vq->shadow_aligned_idx >= PACKED_BATCH_SIZE) {
     636                 :          0 :                 do_data_copy_enqueue(dev, vq);
     637                 :            :                 vhost_flush_enqueue_shadow_packed(dev, vq);
     638                 :            :         }
     639                 :            : }
     640                 :            : 
     641                 :            : /* avoid write operation when necessary, to lessen cache issues */
     642                 :            : #define ASSIGN_UNLESS_EQUAL(var, val) do {      \
     643                 :            :         if ((var) != (val))                     \
     644                 :            :                 (var) = (val);                  \
     645                 :            : } while (0)
     646                 :            : 
     647                 :            : static __rte_always_inline void
     648                 :            : virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
     649                 :            : {
     650                 :          0 :         uint64_t csum_l4 = m_buf->ol_flags & RTE_MBUF_F_TX_L4_MASK;
     651                 :            : 
     652   [ #  #  #  #  :          0 :         if (m_buf->ol_flags & RTE_MBUF_F_TX_TCP_SEG)
             #  #  #  # ]
     653                 :          0 :                 csum_l4 |= RTE_MBUF_F_TX_TCP_CKSUM;
     654                 :            : 
     655   [ #  #  #  #  :          0 :         if (csum_l4) {
          #  #  #  #  #  
                #  #  # ]
     656                 :            :                 /*
     657                 :            :                  * Pseudo-header checksum must be set as per Virtio spec.
     658                 :            :                  *
     659                 :            :                  * Note: We don't propagate rte_net_intel_cksum_prepare()
     660                 :            :                  * errors, as it would have an impact on performance, and an
     661                 :            :                  * error would mean the packet is dropped by the guest instead
     662                 :            :                  * of being dropped here.
     663                 :            :                  */
     664                 :          0 :                 rte_net_intel_cksum_prepare(m_buf);
     665                 :            : 
     666                 :          0 :                 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
     667                 :          0 :                 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
     668                 :            : 
     669   [ #  #  #  #  :          0 :                 switch (csum_l4) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     670                 :          0 :                 case RTE_MBUF_F_TX_TCP_CKSUM:
     671                 :          0 :                         net_hdr->csum_offset = (offsetof(struct rte_tcp_hdr,
     672                 :            :                                                 cksum));
     673                 :          0 :                         break;
     674                 :          0 :                 case RTE_MBUF_F_TX_UDP_CKSUM:
     675                 :          0 :                         net_hdr->csum_offset = (offsetof(struct rte_udp_hdr,
     676                 :            :                                                 dgram_cksum));
     677                 :          0 :                         break;
     678                 :          0 :                 case RTE_MBUF_F_TX_SCTP_CKSUM:
     679                 :          0 :                         net_hdr->csum_offset = (offsetof(struct rte_sctp_hdr,
     680                 :            :                                                 cksum));
     681                 :          0 :                         break;
     682                 :            :                 }
     683                 :            :         } else {
     684   [ #  #  #  #  :          0 :                 ASSIGN_UNLESS_EQUAL(net_hdr->csum_start, 0);
          #  #  #  #  #  
                #  #  # ]
     685   [ #  #  #  #  :          0 :                 ASSIGN_UNLESS_EQUAL(net_hdr->csum_offset, 0);
          #  #  #  #  #  
                #  #  # ]
     686   [ #  #  #  #  :          0 :                 ASSIGN_UNLESS_EQUAL(net_hdr->flags, 0);
          #  #  #  #  #  
                #  #  # ]
     687                 :            :         }
     688                 :            : 
     689                 :            :         /* IP cksum verification cannot be bypassed, then calculate here */
     690   [ #  #  #  #  :          0 :         if (m_buf->ol_flags & RTE_MBUF_F_TX_IP_CKSUM) {
          #  #  #  #  #  
                #  #  # ]
     691                 :            :                 struct rte_ipv4_hdr *ipv4_hdr;
     692                 :            : 
     693                 :          0 :                 ipv4_hdr = rte_pktmbuf_mtod_offset(m_buf, struct rte_ipv4_hdr *,
     694                 :            :                                                    m_buf->l2_len);
     695                 :          0 :                 ipv4_hdr->hdr_checksum = 0;
     696                 :          0 :                 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
     697                 :            :         }
     698                 :            : 
     699   [ #  #  #  #  :          0 :         if (m_buf->ol_flags & RTE_MBUF_F_TX_TCP_SEG) {
          #  #  #  #  #  
                #  #  # ]
     700   [ #  #  #  #  :          0 :                 if (m_buf->ol_flags & RTE_MBUF_F_TX_IPV4)
          #  #  #  #  #  
                #  #  # ]
     701                 :          0 :                         net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
     702                 :            :                 else
     703                 :          0 :                         net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
     704                 :          0 :                 net_hdr->gso_size = m_buf->tso_segsz;
     705                 :          0 :                 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
     706                 :          0 :                                         + m_buf->l4_len;
     707   [ #  #  #  #  :          0 :         } else if (m_buf->ol_flags & RTE_MBUF_F_TX_UDP_SEG) {
          #  #  #  #  #  
                #  #  # ]
     708                 :          0 :                 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
     709                 :          0 :                 net_hdr->gso_size = m_buf->tso_segsz;
     710                 :          0 :                 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len +
     711                 :          0 :                         m_buf->l4_len;
     712                 :            :         } else {
     713   [ #  #  #  #  :          0 :                 ASSIGN_UNLESS_EQUAL(net_hdr->gso_type, 0);
          #  #  #  #  #  
                #  #  # ]
     714   [ #  #  #  #  :          0 :                 ASSIGN_UNLESS_EQUAL(net_hdr->gso_size, 0);
          #  #  #  #  #  
                #  #  # ]
     715   [ #  #  #  #  :          0 :                 ASSIGN_UNLESS_EQUAL(net_hdr->hdr_len, 0);
          #  #  #  #  #  
                #  #  # ]
     716                 :            :         }
     717                 :            : }
     718                 :            : 
     719                 :            : static __rte_always_inline int
     720                 :            : map_one_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
     721                 :            :                 struct buf_vector *buf_vec, uint16_t *vec_idx,
     722                 :            :                 uint64_t desc_iova, uint64_t desc_len, uint8_t perm)
     723                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
     724                 :            : {
     725                 :            :         uint16_t vec_id = *vec_idx;
     726                 :            : 
     727   [ #  #  #  #  :          0 :         while (desc_len) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     728                 :            :                 uint64_t desc_addr;
     729                 :          0 :                 uint64_t desc_chunck_len = desc_len;
     730                 :            : 
     731   [ #  #  #  #  :          0 :                 if (unlikely(vec_id >= BUF_VECTOR_MAX))
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     732                 :          0 :                         return -1;
     733                 :            : 
     734                 :            :                 desc_addr = vhost_iova_to_vva(dev, vq,
     735                 :            :                                 desc_iova,
     736                 :            :                                 &desc_chunck_len,
     737                 :            :                                 perm);
     738   [ #  #  #  #  :          0 :                 if (unlikely(!desc_addr))
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     739                 :            :                         return -1;
     740                 :            : 
     741                 :          0 :                 rte_prefetch0((void *)(uintptr_t)desc_addr);
     742                 :            : 
     743                 :          0 :                 buf_vec[vec_id].buf_iova = desc_iova;
     744                 :          0 :                 buf_vec[vec_id].buf_addr = desc_addr;
     745                 :          0 :                 buf_vec[vec_id].buf_len  = desc_chunck_len;
     746                 :            : 
     747                 :          0 :                 desc_len -= desc_chunck_len;
     748                 :          0 :                 desc_iova += desc_chunck_len;
     749                 :          0 :                 vec_id++;
     750                 :            :         }
     751                 :            :         *vec_idx = vec_id;
     752                 :            : 
     753                 :            :         return 0;
     754                 :            : }
     755                 :            : 
     756                 :            : static __rte_always_inline int
     757                 :            : fill_vec_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
     758                 :            :                          uint32_t avail_idx, uint16_t *vec_idx,
     759                 :            :                          struct buf_vector *buf_vec, uint16_t *desc_chain_head,
     760                 :            :                          uint32_t *desc_chain_len, uint8_t perm)
     761                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
     762                 :            : {
     763                 :          0 :         uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
     764                 :            :         uint16_t vec_id = *vec_idx;
     765                 :            :         uint32_t len    = 0;
     766                 :            :         uint64_t dlen;
     767                 :          0 :         uint32_t nr_descs = vq->size;
     768                 :            :         uint32_t cnt    = 0;
     769                 :          0 :         struct vring_desc *descs = vq->desc;
     770                 :            :         struct vring_desc *idesc = NULL;
     771                 :            : 
     772                 :          0 :         if (unlikely(idx >= vq->size))
     773                 :            :                 return -1;
     774                 :            : 
     775                 :            :         *desc_chain_head = idx;
     776                 :            : 
     777   [ #  #  #  #  :          0 :         if (vq->desc[idx].flags & VRING_DESC_F_INDIRECT) {
          #  #  #  #  #  
                      # ]
     778                 :          0 :                 dlen = vq->desc[idx].len;
     779                 :          0 :                 nr_descs = dlen / sizeof(struct vring_desc);
     780   [ #  #  #  #  :          0 :                 if (unlikely(nr_descs > vq->size))
          #  #  #  #  #  
                      # ]
     781                 :            :                         return -1;
     782                 :            : 
     783                 :          0 :                 descs = (struct vring_desc *)(uintptr_t)
     784   [ #  #  #  #  :          0 :                         vhost_iova_to_vva(dev, vq, vq->desc[idx].addr,
          #  #  #  #  #  
                      # ]
     785                 :            :                                                 &dlen,
     786                 :            :                                                 VHOST_ACCESS_RO);
     787   [ #  #  #  #  :          0 :                 if (unlikely(!descs))
          #  #  #  #  #  
                      # ]
     788                 :            :                         return -1;
     789                 :            : 
     790   [ #  #  #  #  :          0 :                 if (unlikely(dlen < vq->desc[idx].len)) {
          #  #  #  #  #  
                      # ]
     791                 :            :                         /*
     792                 :            :                          * The indirect desc table is not contiguous
     793                 :            :                          * in process VA space, we have to copy it.
     794                 :            :                          */
     795                 :          0 :                         idesc = vhost_alloc_copy_ind_table(dev, vq,
     796                 :          0 :                                         vq->desc[idx].addr, vq->desc[idx].len);
     797   [ #  #  #  #  :          0 :                         if (unlikely(!idesc))
          #  #  #  #  #  
                      # ]
     798                 :            :                                 return -1;
     799                 :            : 
     800                 :            :                         descs = idesc;
     801                 :            :                 }
     802                 :            : 
     803                 :            :                 idx = 0;
     804                 :            :         }
     805                 :            : 
     806                 :            :         while (1) {
     807   [ #  #  #  #  :          0 :                 if (unlikely(idx >= nr_descs || cnt++ >= nr_descs)) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
     808                 :            :                         free_ind_table(idesc);
     809                 :          0 :                         return -1;
     810                 :            :                 }
     811                 :            : 
     812                 :          0 :                 dlen = descs[idx].len;
     813                 :          0 :                 len += dlen;
     814                 :            : 
     815   [ #  #  #  #  :          0 :                 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
          #  #  #  #  #  
                      # ]
     816                 :            :                                                 descs[idx].addr, dlen,
     817                 :            :                                                 perm))) {
     818                 :            :                         free_ind_table(idesc);
     819                 :          0 :                         return -1;
     820                 :            :                 }
     821                 :            : 
     822   [ #  #  #  #  :          0 :                 if ((descs[idx].flags & VRING_DESC_F_NEXT) == 0)
          #  #  #  #  #  
                      # ]
     823                 :            :                         break;
     824                 :            : 
     825                 :          0 :                 idx = descs[idx].next;
     826                 :            :         }
     827                 :            : 
     828                 :            :         *desc_chain_len = len;
     829                 :            :         *vec_idx = vec_id;
     830                 :            : 
     831   [ #  #  #  #  :          0 :         if (unlikely(!!idesc))
          #  #  #  #  #  
                      # ]
     832                 :            :                 free_ind_table(idesc);
     833                 :            : 
     834                 :            :         return 0;
     835                 :            : }
     836                 :            : 
     837                 :            : /*
     838                 :            :  * Returns -1 on fail, 0 on success
     839                 :            :  */
     840                 :            : static inline int
     841                 :          0 : reserve_avail_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
     842                 :            :                                 uint64_t size, struct buf_vector *buf_vec,
     843                 :            :                                 uint16_t *num_buffers, uint16_t avail_head,
     844                 :            :                                 uint16_t *nr_vec)
     845                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
     846                 :            : {
     847                 :            :         uint16_t cur_idx;
     848                 :            :         uint16_t vec_idx = 0;
     849                 :            :         uint16_t max_tries, tries = 0;
     850                 :            : 
     851                 :            :         uint16_t head_idx = 0;
     852                 :            :         uint32_t len = 0;
     853                 :            : 
     854                 :          0 :         *num_buffers = 0;
     855                 :          0 :         cur_idx  = vq->last_avail_idx;
     856                 :            : 
     857         [ #  # ]:          0 :         if (rxvq_is_mergeable(dev))
     858                 :          0 :                 max_tries = vq->size - 1;
     859                 :            :         else
     860                 :            :                 max_tries = 1;
     861                 :            : 
     862         [ #  # ]:          0 :         while (size > 0) {
     863         [ #  # ]:          0 :                 if (unlikely(cur_idx == avail_head))
     864                 :            :                         return -1;
     865                 :            :                 /*
     866                 :            :                  * if we tried all available ring items, and still
     867                 :            :                  * can't get enough buf, it means something abnormal
     868                 :            :                  * happened.
     869                 :            :                  */
     870         [ #  # ]:          0 :                 if (unlikely(++tries > max_tries))
     871                 :            :                         return -1;
     872                 :            : 
     873   [ #  #  #  # ]:          0 :                 if (unlikely(fill_vec_buf_split(dev, vq, cur_idx,
     874                 :            :                                                 &vec_idx, buf_vec,
     875                 :            :                                                 &head_idx, &len,
     876                 :            :                                                 VHOST_ACCESS_RW) < 0))
     877                 :            :                         return -1;
     878                 :          0 :                 len = RTE_MIN(len, size);
     879                 :            :                 update_shadow_used_ring_split(vq, head_idx, len);
     880                 :          0 :                 size -= len;
     881                 :            : 
     882                 :          0 :                 cur_idx++;
     883                 :          0 :                 *num_buffers += 1;
     884                 :            :         }
     885                 :            : 
     886                 :          0 :         *nr_vec = vec_idx;
     887                 :            : 
     888                 :          0 :         return 0;
     889                 :            : }
     890                 :            : 
     891                 :            : static __rte_always_inline int
     892                 :            : fill_vec_buf_packed_indirect(struct virtio_net *dev,
     893                 :            :                         struct vhost_virtqueue *vq,
     894                 :            :                         struct vring_packed_desc *desc, uint16_t *vec_idx,
     895                 :            :                         struct buf_vector *buf_vec, uint32_t *len, uint8_t perm)
     896                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
     897                 :            : {
     898                 :            :         uint16_t i;
     899                 :            :         uint32_t nr_descs;
     900                 :            :         uint16_t vec_id = *vec_idx;
     901                 :            :         uint64_t dlen;
     902                 :            :         struct vring_packed_desc *descs, *idescs = NULL;
     903                 :            : 
     904                 :          0 :         dlen = desc->len;
     905                 :          0 :         descs = (struct vring_packed_desc *)(uintptr_t)
     906   [ #  #  #  #  :          0 :                 vhost_iova_to_vva(dev, vq, desc->addr, &dlen, VHOST_ACCESS_RO);
          #  #  #  #  #  
                #  #  # ]
     907   [ #  #  #  #  :          0 :         if (unlikely(!descs))
          #  #  #  #  #  
                #  #  # ]
     908                 :            :                 return -1;
     909                 :            : 
     910   [ #  #  #  #  :          0 :         if (unlikely(dlen < desc->len)) {
          #  #  #  #  #  
                #  #  # ]
     911                 :            :                 /*
     912                 :            :                  * The indirect desc table is not contiguous
     913                 :            :                  * in process VA space, we have to copy it.
     914                 :            :                  */
     915                 :          0 :                 idescs = vhost_alloc_copy_ind_table(dev,
     916                 :          0 :                                 vq, desc->addr, desc->len);
     917   [ #  #  #  #  :          0 :                 if (unlikely(!idescs))
          #  #  #  #  #  
                #  #  # ]
     918                 :            :                         return -1;
     919                 :            : 
     920                 :            :                 descs = idescs;
     921                 :            :         }
     922                 :            : 
     923                 :          0 :         nr_descs =  desc->len / sizeof(struct vring_packed_desc);
     924   [ #  #  #  #  :          0 :         if (unlikely(nr_descs >= vq->size)) {
          #  #  #  #  #  
                #  #  # ]
     925                 :            :                 free_ind_table(idescs);
     926                 :          0 :                 return -1;
     927                 :            :         }
     928                 :            : 
     929   [ #  #  #  #  :          0 :         for (i = 0; i < nr_descs; i++) {
          #  #  #  #  #  
                #  #  # ]
     930   [ #  #  #  #  :          0 :                 if (unlikely(vec_id >= BUF_VECTOR_MAX)) {
          #  #  #  #  #  
                #  #  # ]
     931                 :            :                         free_ind_table(idescs);
     932                 :          0 :                         return -1;
     933                 :            :                 }
     934                 :            : 
     935                 :          0 :                 dlen = descs[i].len;
     936                 :          0 :                 *len += dlen;
     937   [ #  #  #  #  :          0 :                 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
          #  #  #  #  #  
                #  #  # ]
     938                 :            :                                                 descs[i].addr, dlen,
     939                 :            :                                                 perm)))
     940                 :            :                         return -1;
     941                 :            :         }
     942                 :            :         *vec_idx = vec_id;
     943                 :            : 
     944   [ #  #  #  #  :          0 :         if (unlikely(!!idescs))
          #  #  #  #  #  
                #  #  # ]
     945                 :            :                 free_ind_table(idescs);
     946                 :            : 
     947                 :            :         return 0;
     948                 :            : }
     949                 :            : 
     950                 :            : static __rte_always_inline int
     951                 :            : fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
     952                 :            :                                 uint16_t avail_idx, uint16_t *desc_count,
     953                 :            :                                 struct buf_vector *buf_vec, uint16_t *vec_idx,
     954                 :            :                                 uint16_t *buf_id, uint32_t *len, uint8_t perm)
     955                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
     956                 :            : {
     957                 :          0 :         bool wrap_counter = vq->avail_wrap_counter;
     958                 :          0 :         struct vring_packed_desc *descs = vq->desc_packed;
     959                 :            :         uint16_t vec_id = *vec_idx;
     960                 :            :         uint64_t dlen;
     961                 :            : 
     962   [ #  #  #  # ]:          0 :         if (avail_idx < vq->last_avail_idx)
     963                 :          0 :                 wrap_counter ^= 1;
     964                 :            : 
     965                 :            :         /*
     966                 :            :          * Perform a load-acquire barrier in desc_is_avail to
     967                 :            :          * enforce the ordering between desc flags and desc
     968                 :            :          * content.
     969                 :            :          */
     970   [ #  #  #  #  :          0 :         if (unlikely(!desc_is_avail(&descs[avail_idx], wrap_counter)))
          #  #  #  #  #  
                #  #  # ]
     971                 :            :                 return -1;
     972                 :            : 
     973                 :            :         *desc_count = 0;
     974                 :            :         *len = 0;
     975                 :            : 
     976                 :            :         while (1) {
     977   [ #  #  #  #  :          0 :                 if (unlikely(vec_id >= BUF_VECTOR_MAX))
          #  #  #  #  #  
                #  #  # ]
     978                 :            :                         return -1;
     979                 :            : 
     980   [ #  #  #  #  :          0 :                 if (unlikely(*desc_count >= vq->size))
          #  #  #  #  #  
                #  #  # ]
     981                 :            :                         return -1;
     982                 :            : 
     983                 :          0 :                 *desc_count += 1;
     984                 :          0 :                 *buf_id = descs[avail_idx].id;
     985                 :            : 
     986   [ #  #  #  #  :          0 :                 if (descs[avail_idx].flags & VRING_DESC_F_INDIRECT) {
          #  #  #  #  #  
                #  #  # ]
     987   [ #  #  #  #  :          0 :                         if (unlikely(fill_vec_buf_packed_indirect(dev, vq,
          #  #  #  #  #  
                #  #  # ]
     988                 :            :                                                         &descs[avail_idx],
     989                 :            :                                                         &vec_id, buf_vec,
     990                 :            :                                                         len, perm) < 0))
     991                 :            :                                 return -1;
     992                 :            :                 } else {
     993                 :          0 :                         dlen = descs[avail_idx].len;
     994                 :          0 :                         *len += dlen;
     995                 :            : 
     996   [ #  #  #  #  :          0 :                         if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
          #  #  #  #  #  
                #  #  # ]
     997                 :            :                                                         descs[avail_idx].addr,
     998                 :            :                                                         dlen,
     999                 :            :                                                         perm)))
    1000                 :            :                                 return -1;
    1001                 :            :                 }
    1002                 :            : 
    1003   [ #  #  #  #  :          0 :                 if ((descs[avail_idx].flags & VRING_DESC_F_NEXT) == 0)
          #  #  #  #  #  
                #  #  # ]
    1004                 :            :                         break;
    1005                 :            : 
    1006   [ #  #  #  #  :          0 :                 if (++avail_idx >= vq->size) {
          #  #  #  #  #  
                #  #  # ]
    1007                 :          0 :                         avail_idx -= vq->size;
    1008                 :            :                         wrap_counter ^= 1;
    1009                 :            :                 }
    1010                 :            :         }
    1011                 :            : 
    1012                 :            :         *vec_idx = vec_id;
    1013                 :            : 
    1014                 :            :         return 0;
    1015                 :            : }
    1016                 :            : 
    1017                 :            : static __rte_noinline void
    1018                 :          0 : copy_vnet_hdr_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
    1019                 :            :                 struct buf_vector *buf_vec,
    1020                 :            :                 struct virtio_net_hdr_mrg_rxbuf *hdr)
    1021                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    1022                 :            : {
    1023                 :            :         uint64_t len;
    1024                 :          0 :         uint64_t remain = dev->vhost_hlen;
    1025                 :          0 :         uint64_t src = (uint64_t)(uintptr_t)hdr, dst;
    1026                 :          0 :         uint64_t iova = buf_vec->buf_iova;
    1027                 :            : 
    1028         [ #  # ]:          0 :         while (remain) {
    1029                 :          0 :                 len = RTE_MIN(remain,
    1030                 :            :                                 buf_vec->buf_len);
    1031                 :          0 :                 dst = buf_vec->buf_addr;
    1032         [ #  # ]:          0 :                 rte_memcpy((void *)(uintptr_t)dst,
    1033                 :            :                                 (void *)(uintptr_t)src,
    1034                 :            :                                 len);
    1035                 :            : 
    1036                 :            :                 PRINT_PACKET(dev, (uintptr_t)dst,
    1037                 :            :                                 (uint32_t)len, 0);
    1038                 :            :                 vhost_log_cache_write_iova(dev, vq,
    1039                 :            :                                 iova, len);
    1040                 :            : 
    1041                 :          0 :                 remain -= len;
    1042                 :          0 :                 iova += len;
    1043                 :          0 :                 src += len;
    1044                 :          0 :                 buf_vec++;
    1045                 :            :         }
    1046                 :          0 : }
    1047                 :            : 
    1048                 :            : static __rte_always_inline int
    1049                 :            : async_iter_initialize(struct virtio_net *dev, struct vhost_async *async)
    1050                 :            : {
    1051                 :            :         struct vhost_iov_iter *iter;
    1052                 :            : 
    1053   [ #  #  #  #  :          0 :         if (unlikely(async->iovec_idx >= VHOST_MAX_ASYNC_VEC)) {
                   #  # ]
    1054                 :          0 :                 VHOST_DATA_LOG(dev->ifname, ERR, "no more async iovec available");
    1055                 :          0 :                 return -1;
    1056                 :            :         }
    1057                 :            : 
    1058                 :          0 :         iter = async->iov_iter + async->iter_idx;
    1059                 :          0 :         iter->iov = async->iovec + async->iovec_idx;
    1060                 :          0 :         iter->nr_segs = 0;
    1061                 :            : 
    1062                 :          0 :         return 0;
    1063                 :            : }
    1064                 :            : 
    1065                 :            : static __rte_always_inline int
    1066                 :            : async_iter_add_iovec(struct virtio_net *dev, struct vhost_async *async,
    1067                 :            :                 void *src, void *dst, size_t len)
    1068                 :            : {
    1069                 :            :         struct vhost_iov_iter *iter;
    1070                 :            :         struct vhost_iovec *iovec;
    1071                 :            : 
    1072   [ #  #  #  #  :          0 :         if (unlikely(async->iovec_idx >= VHOST_MAX_ASYNC_VEC)) {
                   #  # ]
    1073                 :            :                 static bool vhost_max_async_vec_log;
    1074                 :            : 
    1075   [ #  #  #  #  :          0 :                 if (!vhost_max_async_vec_log) {
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    1076                 :          0 :                         VHOST_DATA_LOG(dev->ifname, ERR, "no more async iovec available");
    1077                 :          0 :                         vhost_max_async_vec_log = true;
    1078                 :            :                 }
    1079                 :            : 
    1080                 :            :                 return -1;
    1081                 :            :         }
    1082                 :            : 
    1083                 :          0 :         iter = async->iov_iter + async->iter_idx;
    1084                 :          0 :         iovec = async->iovec + async->iovec_idx;
    1085                 :            : 
    1086                 :          0 :         iovec->src_addr = src;
    1087                 :          0 :         iovec->dst_addr = dst;
    1088                 :          0 :         iovec->len = len;
    1089                 :            : 
    1090                 :          0 :         iter->nr_segs++;
    1091                 :          0 :         async->iovec_idx++;
    1092                 :            : 
    1093                 :          0 :         return 0;
    1094                 :            : }
    1095                 :            : 
    1096                 :            : static __rte_always_inline void
    1097                 :            : async_iter_finalize(struct vhost_async *async)
    1098                 :            : {
    1099                 :          0 :         async->iter_idx++;
    1100                 :          0 : }
    1101                 :            : 
    1102                 :            : static __rte_always_inline void
    1103                 :            : async_iter_cancel(struct vhost_async *async)
    1104                 :            : {
    1105                 :            :         struct vhost_iov_iter *iter;
    1106                 :            : 
    1107                 :          0 :         iter = async->iov_iter + async->iter_idx;
    1108                 :          0 :         async->iovec_idx -= iter->nr_segs;
    1109                 :          0 :         iter->nr_segs = 0;
    1110                 :          0 :         iter->iov = NULL;
    1111                 :          0 : }
    1112                 :            : 
    1113                 :            : static __rte_always_inline void
    1114                 :            : async_iter_reset(struct vhost_async *async)
    1115                 :            : {
    1116                 :          0 :         async->iter_idx = 0;
    1117                 :          0 :         async->iovec_idx = 0;
    1118                 :            : }
    1119                 :            : 
    1120                 :            : static __rte_always_inline int
    1121                 :            : async_fill_seg(struct virtio_net *dev, struct vhost_virtqueue *vq,
    1122                 :            :                 struct rte_mbuf *m, uint32_t mbuf_offset,
    1123                 :            :                 uint64_t buf_iova, uint32_t cpy_len, bool to_desc)
    1124                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    1125                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    1126                 :            : {
    1127                 :          0 :         struct vhost_async *async = vq->async;
    1128                 :            :         uint64_t mapped_len;
    1129                 :            :         uint32_t buf_offset = 0;
    1130                 :            :         void *src, *dst;
    1131                 :            :         void *host_iova;
    1132                 :            : 
    1133   [ #  #  #  #  :          0 :         while (cpy_len) {
          #  #  #  #  #  
                #  #  # ]
    1134   [ #  #  #  #  :          0 :                 host_iova = (void *)(uintptr_t)gpa_to_first_hpa(dev,
          #  #  #  #  #  
                #  #  # ]
    1135                 :            :                                 buf_iova + buf_offset, cpy_len, &mapped_len);
    1136   [ #  #  #  #  :          0 :                 if (unlikely(!host_iova)) {
          #  #  #  #  #  
                #  #  # ]
    1137                 :          0 :                         VHOST_DATA_LOG(dev->ifname, ERR,
    1138                 :            :                                 "%s: failed to get host iova.",
    1139                 :            :                                 __func__);
    1140                 :            :                         return -1;
    1141                 :            :                 }
    1142                 :            : 
    1143                 :            :                 if (to_desc) {
    1144   [ #  #  #  # ]:          0 :                         src = (void *)(uintptr_t)rte_pktmbuf_iova_offset(m, mbuf_offset);
    1145                 :            :                         dst = host_iova;
    1146                 :            :                 } else {
    1147                 :            :                         src = host_iova;
    1148   [ #  #  #  #  :          0 :                         dst = (void *)(uintptr_t)rte_pktmbuf_iova_offset(m, mbuf_offset);
             #  #  #  # ]
    1149                 :            :                 }
    1150                 :            : 
    1151   [ #  #  #  #  :          0 :                 if (unlikely(async_iter_add_iovec(dev, async, src, dst, (size_t)mapped_len)))
          #  #  #  #  #  
                #  #  # ]
    1152                 :            :                         return -1;
    1153                 :            : 
    1154                 :          0 :                 cpy_len -= (uint32_t)mapped_len;
    1155                 :          0 :                 mbuf_offset += (uint32_t)mapped_len;
    1156                 :          0 :                 buf_offset += (uint32_t)mapped_len;
    1157                 :            :         }
    1158                 :            : 
    1159                 :            :         return 0;
    1160                 :            : }
    1161                 :            : 
    1162                 :            : static __rte_always_inline void
    1163                 :            : sync_fill_seg(struct virtio_net *dev, struct vhost_virtqueue *vq,
    1164                 :            :                 struct rte_mbuf *m, uint32_t mbuf_offset,
    1165                 :            :                 uint64_t buf_addr, uint64_t buf_iova, uint32_t cpy_len, bool to_desc)
    1166                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    1167                 :            : {
    1168                 :          0 :         struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
    1169                 :            : 
    1170   [ #  #  #  #  :          0 :         if (likely(cpy_len > MAX_BATCH_LEN || vq->batch_copy_nb_elems >= vq->size)) {
          #  #  #  #  #  
                #  #  # ]
    1171                 :            :                 if (to_desc) {
    1172                 :          0 :                         rte_memcpy((void *)((uintptr_t)(buf_addr)),
    1173   [ #  #  #  # ]:          0 :                                 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
    1174                 :            :                                 cpy_len);
    1175                 :            :                         vhost_log_cache_write_iova(dev, vq, buf_iova, cpy_len);
    1176                 :            :                         PRINT_PACKET(dev, (uintptr_t)(buf_addr), cpy_len, 0);
    1177                 :            :                 } else {
    1178   [ #  #  #  #  :          0 :                         rte_memcpy(rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
             #  #  #  # ]
    1179                 :            :                                 (void *)((uintptr_t)(buf_addr)),
    1180                 :            :                                 cpy_len);
    1181                 :            :                 }
    1182                 :            :         } else {
    1183                 :            :                 if (to_desc) {
    1184                 :          0 :                         batch_copy[vq->batch_copy_nb_elems].dst =
    1185                 :          0 :                                 (void *)((uintptr_t)(buf_addr));
    1186                 :          0 :                         batch_copy[vq->batch_copy_nb_elems].src =
    1187                 :          0 :                                 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
    1188                 :          0 :                         batch_copy[vq->batch_copy_nb_elems].log_addr = buf_iova;
    1189                 :            :                 } else {
    1190                 :          0 :                         batch_copy[vq->batch_copy_nb_elems].dst =
    1191                 :          0 :                                 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
    1192                 :          0 :                         batch_copy[vq->batch_copy_nb_elems].src =
    1193                 :          0 :                                 (void *)((uintptr_t)(buf_addr));
    1194                 :            :                 }
    1195                 :          0 :                 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
    1196                 :          0 :                 vq->batch_copy_nb_elems++;
    1197                 :            :         }
    1198                 :            : }
    1199                 :            : 
    1200                 :            : static __rte_always_inline int
    1201                 :            : mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
    1202                 :            :                 struct rte_mbuf *m, struct buf_vector *buf_vec,
    1203                 :            :                 uint16_t nr_vec, uint16_t num_buffers, bool is_async)
    1204                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    1205                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    1206                 :            : {
    1207                 :            :         uint32_t vec_idx = 0;
    1208                 :            :         uint32_t mbuf_offset, mbuf_avail;
    1209                 :            :         uint32_t buf_offset, buf_avail;
    1210                 :            :         uint64_t buf_addr, buf_iova, buf_len;
    1211                 :            :         uint32_t cpy_len;
    1212                 :            :         uint64_t hdr_addr;
    1213                 :            :         struct rte_mbuf *hdr_mbuf;
    1214                 :            :         struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
    1215                 :          0 :         struct vhost_async *async = vq->async;
    1216                 :            : 
    1217                 :          0 :         if (unlikely(m == NULL))
    1218                 :            :                 return -1;
    1219                 :            : 
    1220                 :          0 :         buf_addr = buf_vec[vec_idx].buf_addr;
    1221                 :          0 :         buf_iova = buf_vec[vec_idx].buf_iova;
    1222                 :          0 :         buf_len = buf_vec[vec_idx].buf_len;
    1223                 :            : 
    1224   [ #  #  #  #  :          0 :         if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1))
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    1225                 :            :                 return -1;
    1226                 :            : 
    1227                 :            :         hdr_mbuf = m;
    1228                 :            :         hdr_addr = buf_addr;
    1229   [ #  #  #  #  :          0 :         if (unlikely(buf_len < dev->vhost_hlen)) {
             #  #  #  # ]
    1230                 :            :                 memset(&tmp_hdr, 0, sizeof(struct virtio_net_hdr_mrg_rxbuf));
    1231                 :            :                 hdr = &tmp_hdr;
    1232                 :            :         } else
    1233                 :          0 :                 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
    1234                 :            : 
    1235                 :            :         VHOST_DATA_LOG(dev->ifname, DEBUG, "RX: num merge buffers %d", num_buffers);
    1236                 :            : 
    1237   [ #  #  #  #  :          0 :         if (unlikely(buf_len < dev->vhost_hlen)) {
             #  #  #  # ]
    1238                 :          0 :                 buf_offset = dev->vhost_hlen - buf_len;
    1239                 :            :                 vec_idx++;
    1240                 :          0 :                 buf_addr = buf_vec[vec_idx].buf_addr;
    1241                 :          0 :                 buf_iova = buf_vec[vec_idx].buf_iova;
    1242                 :          0 :                 buf_len = buf_vec[vec_idx].buf_len;
    1243                 :          0 :                 buf_avail = buf_len - buf_offset;
    1244                 :            :         } else {
    1245                 :            :                 buf_offset = dev->vhost_hlen;
    1246                 :          0 :                 buf_avail = buf_len - dev->vhost_hlen;
    1247                 :            :         }
    1248                 :            : 
    1249   [ #  #  #  # ]:          0 :         mbuf_avail  = rte_pktmbuf_data_len(m);
    1250                 :            :         mbuf_offset = 0;
    1251                 :            : 
    1252                 :            :         if (is_async) {
    1253                 :            :                 if (async_iter_initialize(dev, async))
    1254                 :          0 :                         return -1;
    1255                 :            :         }
    1256                 :            : 
    1257   [ #  #  #  #  :          0 :         while (mbuf_avail != 0 || m->next != NULL) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    1258                 :            :                 /* done with current buf, get the next one */
    1259   [ #  #  #  #  :          0 :                 if (buf_avail == 0) {
             #  #  #  # ]
    1260                 :          0 :                         vec_idx++;
    1261   [ #  #  #  #  :          0 :                         if (unlikely(vec_idx >= nr_vec))
             #  #  #  # ]
    1262                 :          0 :                                 goto error;
    1263                 :            : 
    1264                 :          0 :                         buf_addr = buf_vec[vec_idx].buf_addr;
    1265                 :          0 :                         buf_iova = buf_vec[vec_idx].buf_iova;
    1266                 :          0 :                         buf_len = buf_vec[vec_idx].buf_len;
    1267                 :            : 
    1268                 :            :                         buf_offset = 0;
    1269                 :            :                         buf_avail  = buf_len;
    1270                 :            :                 }
    1271                 :            : 
    1272                 :            :                 /* done with current mbuf, get the next one */
    1273   [ #  #  #  #  :          0 :                 if (mbuf_avail == 0) {
             #  #  #  # ]
    1274                 :          0 :                         m = m->next;
    1275                 :            : 
    1276                 :            :                         mbuf_offset = 0;
    1277                 :          0 :                         mbuf_avail  = rte_pktmbuf_data_len(m);
    1278                 :            :                 }
    1279                 :            : 
    1280   [ #  #  #  #  :          0 :                 if (hdr_addr) {
             #  #  #  # ]
    1281                 :            :                         virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
    1282   [ #  #  #  #  :          0 :                         if (rxvq_is_mergeable(dev))
             #  #  #  # ]
    1283   [ #  #  #  #  :          0 :                                 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
             #  #  #  # ]
    1284                 :            :                                                 num_buffers);
    1285                 :            : 
    1286   [ #  #  #  #  :          0 :                         if (unlikely(hdr == &tmp_hdr)) {
             #  #  #  # ]
    1287                 :          0 :                                 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
    1288                 :            :                         } else {
    1289                 :            :                                 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
    1290                 :            :                                                 dev->vhost_hlen, 0);
    1291                 :          0 :                                 vhost_log_cache_write_iova(dev, vq,
    1292                 :            :                                                 buf_vec[0].buf_iova,
    1293   [ #  #  #  #  :          0 :                                                 dev->vhost_hlen);
             #  #  #  # ]
    1294                 :            :                         }
    1295                 :            : 
    1296                 :            :                         hdr_addr = 0;
    1297                 :            :                 }
    1298                 :            : 
    1299                 :          0 :                 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
    1300                 :            : 
    1301                 :            :                 if (is_async) {
    1302   [ #  #  #  # ]:          0 :                         if (async_fill_seg(dev, vq, m, mbuf_offset,
    1303                 :            :                                            buf_iova + buf_offset, cpy_len, true) < 0)
    1304                 :          0 :                                 goto error;
    1305                 :            :                 } else {
    1306   [ #  #  #  # ]:          0 :                         sync_fill_seg(dev, vq, m, mbuf_offset,
    1307                 :            :                                       buf_addr + buf_offset,
    1308                 :            :                                       buf_iova + buf_offset, cpy_len, true);
    1309                 :            :                 }
    1310                 :            : 
    1311                 :          0 :                 mbuf_avail  -= cpy_len;
    1312                 :          0 :                 mbuf_offset += cpy_len;
    1313                 :          0 :                 buf_avail  -= cpy_len;
    1314                 :          0 :                 buf_offset += cpy_len;
    1315                 :            :         }
    1316                 :            : 
    1317                 :            :         if (is_async)
    1318                 :            :                 async_iter_finalize(async);
    1319                 :            : 
    1320                 :            :         return 0;
    1321                 :          0 : error:
    1322                 :            :         if (is_async)
    1323                 :            :                 async_iter_cancel(async);
    1324                 :            : 
    1325                 :            :         return -1;
    1326                 :            : }
    1327                 :            : 
    1328                 :            : static __rte_always_inline int
    1329                 :            : vhost_enqueue_single_packed(struct virtio_net *dev,
    1330                 :            :                             struct vhost_virtqueue *vq,
    1331                 :            :                             struct rte_mbuf *pkt,
    1332                 :            :                             struct buf_vector *buf_vec,
    1333                 :            :                             uint16_t *nr_descs)
    1334                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    1335                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    1336                 :          0 : {
    1337                 :            :         uint16_t nr_vec = 0;
    1338                 :          0 :         uint16_t avail_idx = vq->last_avail_idx;
    1339                 :            :         uint16_t max_tries, tries = 0;
    1340                 :            :         uint16_t buf_id = 0;
    1341                 :            :         uint32_t len = 0;
    1342                 :            :         uint16_t desc_count;
    1343                 :          0 :         uint64_t size = pkt->pkt_len + sizeof(struct virtio_net_hdr_mrg_rxbuf);
    1344                 :            :         uint16_t num_buffers = 0;
    1345                 :          0 :         uint32_t buffer_len[vq->size];
    1346                 :          0 :         uint16_t buffer_buf_id[vq->size];
    1347                 :          0 :         uint16_t buffer_desc_count[vq->size];
    1348                 :            : 
    1349                 :          0 :         if (rxvq_is_mergeable(dev))
    1350                 :          0 :                 max_tries = vq->size - 1;
    1351                 :            :         else
    1352                 :            :                 max_tries = 1;
    1353                 :            : 
    1354         [ #  # ]:          0 :         while (size > 0) {
    1355                 :            :                 /*
    1356                 :            :                  * if we tried all available ring items, and still
    1357                 :            :                  * can't get enough buf, it means something abnormal
    1358                 :            :                  * happened.
    1359                 :            :                  */
    1360         [ #  # ]:          0 :                 if (unlikely(++tries > max_tries))
    1361                 :            :                         return -1;
    1362                 :            : 
    1363         [ #  # ]:          0 :                 if (unlikely(fill_vec_buf_packed(dev, vq,
    1364                 :            :                                                 avail_idx, &desc_count,
    1365                 :            :                                                 buf_vec, &nr_vec,
    1366                 :            :                                                 &buf_id, &len,
    1367                 :            :                                                 VHOST_ACCESS_RW) < 0))
    1368                 :            :                         return -1;
    1369                 :            : 
    1370                 :          0 :                 len = RTE_MIN(len, size);
    1371                 :          0 :                 size -= len;
    1372                 :            : 
    1373                 :          0 :                 buffer_len[num_buffers] = len;
    1374                 :          0 :                 buffer_buf_id[num_buffers] = buf_id;
    1375                 :          0 :                 buffer_desc_count[num_buffers] = desc_count;
    1376                 :            :                 num_buffers += 1;
    1377                 :            : 
    1378                 :          0 :                 *nr_descs += desc_count;
    1379                 :          0 :                 avail_idx += desc_count;
    1380         [ #  # ]:          0 :                 if (avail_idx >= vq->size)
    1381                 :          0 :                         avail_idx -= vq->size;
    1382                 :            :         }
    1383                 :            : 
    1384                 :            :         if (mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec, num_buffers, false) < 0)
    1385                 :            :                 return -1;
    1386                 :            : 
    1387                 :            :         vhost_shadow_enqueue_single_packed(dev, vq, buffer_len, buffer_buf_id,
    1388                 :            :                                            buffer_desc_count, num_buffers);
    1389                 :            : 
    1390                 :            :         return 0;
    1391                 :            : }
    1392                 :            : 
    1393                 :            : static __rte_noinline uint32_t
    1394                 :          0 : virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
    1395                 :            :         struct rte_mbuf **pkts, uint32_t count)
    1396                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    1397                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    1398                 :            : {
    1399                 :            :         uint32_t pkt_idx = 0;
    1400                 :            :         uint16_t num_buffers;
    1401                 :            :         struct buf_vector buf_vec[BUF_VECTOR_MAX];
    1402                 :            :         uint16_t avail_head;
    1403                 :            : 
    1404                 :            :         /*
    1405                 :            :          * The ordering between avail index and
    1406                 :            :          * desc reads needs to be enforced.
    1407                 :            :          */
    1408                 :          0 :         avail_head = rte_atomic_load_explicit((unsigned short __rte_atomic *)&vq->avail->idx,
    1409                 :            :                 rte_memory_order_acquire);
    1410                 :            : 
    1411                 :          0 :         rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
    1412                 :            : 
    1413         [ #  # ]:          0 :         for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
    1414                 :          0 :                 uint64_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
    1415                 :          0 :                 uint16_t nr_vec = 0;
    1416                 :            : 
    1417         [ #  # ]:          0 :                 if (unlikely(reserve_avail_buf_split(dev, vq,
    1418                 :            :                                                 pkt_len, buf_vec, &num_buffers,
    1419                 :            :                                                 avail_head, &nr_vec) < 0)) {
    1420                 :            :                         VHOST_DATA_LOG(dev->ifname, DEBUG,
    1421                 :            :                                 "failed to get enough desc from vring");
    1422                 :          0 :                         vq->shadow_used_idx -= num_buffers;
    1423                 :          0 :                         break;
    1424                 :            :                 }
    1425                 :            : 
    1426                 :            :                 VHOST_DATA_LOG(dev->ifname, DEBUG,
    1427                 :            :                         "current index %d | end index %d",
    1428                 :            :                         vq->last_avail_idx, vq->last_avail_idx + num_buffers);
    1429                 :            : 
    1430         [ #  # ]:          0 :                 if (mbuf_to_desc(dev, vq, pkts[pkt_idx], buf_vec, nr_vec,
    1431                 :            :                                         num_buffers, false) < 0) {
    1432                 :          0 :                         vq->shadow_used_idx -= num_buffers;
    1433                 :          0 :                         break;
    1434                 :            :                 }
    1435                 :            : 
    1436         [ #  # ]:          0 :                 vq->last_avail_idx += num_buffers;
    1437                 :            :                 vhost_virtqueue_reconnect_log_split(vq);
    1438                 :            :         }
    1439                 :            : 
    1440                 :          0 :         do_data_copy_enqueue(dev, vq);
    1441                 :            : 
    1442         [ #  # ]:          0 :         if (likely(vq->shadow_used_idx)) {
    1443                 :            :                 flush_shadow_used_ring_split(dev, vq);
    1444                 :            :                 vhost_vring_call_split(dev, vq);
    1445                 :            :         }
    1446                 :            : 
    1447                 :          0 :         return pkt_idx;
    1448                 :            : }
    1449                 :            : 
    1450                 :            : static __rte_always_inline int
    1451                 :            : virtio_dev_rx_sync_batch_check(struct virtio_net *dev,
    1452                 :            :                            struct vhost_virtqueue *vq,
    1453                 :            :                            struct rte_mbuf **pkts,
    1454                 :            :                            uint64_t *desc_addrs,
    1455                 :            :                            uint64_t *lens)
    1456                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    1457                 :            : {
    1458                 :          0 :         bool wrap_counter = vq->avail_wrap_counter;
    1459                 :            :         struct vring_packed_desc *descs = vq->desc_packed;
    1460                 :            :         uint16_t avail_idx = vq->last_avail_idx;
    1461                 :            :         uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
    1462                 :            :         uint16_t i;
    1463                 :            : 
    1464                 :          0 :         if (unlikely(avail_idx & PACKED_BATCH_MASK))
    1465                 :            :                 return -1;
    1466                 :            : 
    1467         [ #  # ]:          0 :         if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
    1468                 :            :                 return -1;
    1469                 :            : 
    1470         [ #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    1471         [ #  # ]:          0 :                 if (unlikely(pkts[i]->next != NULL))
    1472                 :            :                         return -1;
    1473         [ #  # ]:          0 :                 if (unlikely(!desc_is_avail(&descs[avail_idx + i],
    1474                 :            :                                             wrap_counter)))
    1475                 :            :                         return -1;
    1476                 :            :         }
    1477                 :            : 
    1478         [ #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
    1479                 :          0 :                 lens[i] = descs[avail_idx + i].len;
    1480                 :            : 
    1481         [ #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    1482         [ #  # ]:          0 :                 if (unlikely(pkts[i]->pkt_len > (lens[i] - buf_offset)))
    1483                 :            :                         return -1;
    1484                 :            :         }
    1485                 :            : 
    1486         [ #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
    1487                 :          0 :                 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
    1488                 :          0 :                                                   descs[avail_idx + i].addr,
    1489         [ #  # ]:          0 :                                                   &lens[i],
    1490                 :            :                                                   VHOST_ACCESS_RW);
    1491                 :            : 
    1492         [ #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    1493         [ #  # ]:          0 :                 if (unlikely(!desc_addrs[i]))
    1494                 :            :                         return -1;
    1495         [ #  # ]:          0 :                 if (unlikely(lens[i] != descs[avail_idx + i].len))
    1496                 :            :                         return -1;
    1497                 :            :         }
    1498                 :            : 
    1499                 :            :         return 0;
    1500                 :            : }
    1501                 :            : 
    1502                 :            : static __rte_always_inline int
    1503                 :            : virtio_dev_rx_async_batch_check(struct vhost_virtqueue *vq,
    1504                 :            :                            struct rte_mbuf **pkts,
    1505                 :            :                            uint64_t *desc_addrs,
    1506                 :            :                            uint64_t *lens,
    1507                 :            :                            int16_t dma_id,
    1508                 :            :                            uint16_t vchan_id)
    1509                 :            : {
    1510                 :          0 :         bool wrap_counter = vq->avail_wrap_counter;
    1511                 :            :         struct vring_packed_desc *descs = vq->desc_packed;
    1512                 :            :         uint16_t avail_idx = vq->last_avail_idx;
    1513                 :            :         uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
    1514                 :            :         uint16_t i;
    1515                 :            : 
    1516                 :          0 :         if (unlikely(avail_idx & PACKED_BATCH_MASK))
    1517                 :            :                 return -1;
    1518                 :            : 
    1519         [ #  # ]:          0 :         if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
    1520                 :            :                 return -1;
    1521                 :            : 
    1522         [ #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    1523         [ #  # ]:          0 :                 if (unlikely(pkts[i]->next != NULL))
    1524                 :            :                         return -1;
    1525         [ #  # ]:          0 :                 if (unlikely(!desc_is_avail(&descs[avail_idx + i],
    1526                 :            :                                             wrap_counter)))
    1527                 :            :                         return -1;
    1528                 :            :         }
    1529                 :            : 
    1530         [ #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
    1531                 :          0 :                 lens[i] = descs[avail_idx + i].len;
    1532                 :            : 
    1533         [ #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    1534         [ #  # ]:          0 :                 if (unlikely(pkts[i]->pkt_len > (lens[i] - buf_offset)))
    1535                 :            :                         return -1;
    1536                 :            :         }
    1537                 :            : 
    1538         [ #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
    1539                 :          0 :                 desc_addrs[i] =  descs[avail_idx + i].addr;
    1540                 :            : 
    1541         [ #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    1542         [ #  # ]:          0 :                 if (unlikely(!desc_addrs[i]))
    1543                 :            :                         return -1;
    1544         [ #  # ]:          0 :                 if (unlikely(lens[i] != descs[avail_idx + i].len))
    1545                 :            :                         return -1;
    1546                 :            :         }
    1547                 :            : 
    1548         [ #  # ]:          0 :         if (rte_dma_burst_capacity(dma_id, vchan_id) < PACKED_BATCH_SIZE)
    1549                 :            :                 return -1;
    1550                 :            : 
    1551                 :            :         return 0;
    1552                 :            : }
    1553                 :            : 
    1554                 :            : static __rte_always_inline void
    1555                 :            : virtio_dev_rx_batch_packed_copy(struct virtio_net *dev,
    1556                 :            :                            struct vhost_virtqueue *vq,
    1557                 :            :                            struct rte_mbuf **pkts,
    1558                 :            :                            uint64_t *desc_addrs,
    1559                 :            :                            uint64_t *lens)
    1560                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    1561                 :            : {
    1562                 :            :         uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
    1563                 :            :         struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE];
    1564                 :          0 :         struct vring_packed_desc *descs = vq->desc_packed;
    1565                 :          0 :         uint16_t avail_idx = vq->last_avail_idx;
    1566                 :            :         uint16_t ids[PACKED_BATCH_SIZE];
    1567                 :            :         uint16_t i;
    1568                 :            : 
    1569         [ #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    1570                 :          0 :                 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
    1571                 :          0 :                 hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *)
    1572                 :            :                                         (uintptr_t)desc_addrs[i];
    1573                 :          0 :                 lens[i] = pkts[i]->pkt_len +
    1574                 :            :                         sizeof(struct virtio_net_hdr_mrg_rxbuf);
    1575                 :            :         }
    1576                 :            : 
    1577         [ #  # ]:          0 :         if (rxvq_is_mergeable(dev)) {
    1578         [ #  # ]:          0 :                 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    1579         [ #  # ]:          0 :                         ASSIGN_UNLESS_EQUAL(hdrs[i]->num_buffers, 1);
    1580                 :            :                 }
    1581                 :            :         }
    1582                 :            : 
    1583         [ #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
    1584         [ #  # ]:          0 :                 virtio_enqueue_offload(pkts[i], &hdrs[i]->hdr);
    1585                 :            : 
    1586                 :            :         vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
    1587                 :            : 
    1588         [ #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    1589                 :          0 :                 rte_memcpy((void *)(uintptr_t)(desc_addrs[i] + buf_offset),
    1590                 :          0 :                            rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
    1591         [ #  # ]:          0 :                            pkts[i]->pkt_len);
    1592                 :            :         }
    1593                 :            : 
    1594         [ #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
    1595                 :          0 :                 vhost_log_cache_write_iova(dev, vq, descs[avail_idx + i].addr,
    1596         [ #  # ]:          0 :                                            lens[i]);
    1597                 :            : 
    1598         [ #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
    1599                 :          0 :                 ids[i] = descs[avail_idx + i].id;
    1600                 :            : 
    1601                 :            :         vhost_flush_enqueue_batch_packed(dev, vq, lens, ids);
    1602                 :            : }
    1603                 :            : 
    1604                 :            : static __rte_always_inline int
    1605                 :            : virtio_dev_rx_sync_batch_packed(struct virtio_net *dev,
    1606                 :            :                            struct vhost_virtqueue *vq,
    1607                 :            :                            struct rte_mbuf **pkts)
    1608                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    1609                 :            : {
    1610                 :            :         uint64_t desc_addrs[PACKED_BATCH_SIZE];
    1611                 :            :         uint64_t lens[PACKED_BATCH_SIZE];
    1612                 :            : 
    1613         [ #  # ]:          0 :         if (virtio_dev_rx_sync_batch_check(dev, vq, pkts, desc_addrs, lens) == -1)
    1614                 :            :                 return -1;
    1615                 :            : 
    1616         [ #  # ]:          0 :         if (vq->shadow_used_idx) {
    1617                 :          0 :                 do_data_copy_enqueue(dev, vq);
    1618                 :            :                 vhost_flush_enqueue_shadow_packed(dev, vq);
    1619                 :            :         }
    1620                 :            : 
    1621                 :            :         virtio_dev_rx_batch_packed_copy(dev, vq, pkts, desc_addrs, lens);
    1622                 :            : 
    1623                 :            :         return 0;
    1624                 :            : }
    1625                 :            : 
    1626                 :            : static __rte_always_inline int16_t
    1627                 :            : virtio_dev_rx_single_packed(struct virtio_net *dev,
    1628                 :            :                             struct vhost_virtqueue *vq,
    1629                 :            :                             struct rte_mbuf *pkt)
    1630                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    1631                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    1632                 :            : {
    1633                 :            :         struct buf_vector buf_vec[BUF_VECTOR_MAX];
    1634                 :            :         uint16_t nr_descs = 0;
    1635                 :            : 
    1636         [ #  # ]:          0 :         if (unlikely(vhost_enqueue_single_packed(dev, vq, pkt, buf_vec,
    1637                 :            :                                                  &nr_descs) < 0)) {
    1638                 :            :                 VHOST_DATA_LOG(dev->ifname, DEBUG, "failed to get enough desc from vring");
    1639                 :            :                 return -1;
    1640                 :            :         }
    1641                 :            : 
    1642                 :            :         VHOST_DATA_LOG(dev->ifname, DEBUG,
    1643                 :            :                 "current index %d | end index %d",
    1644                 :            :                 vq->last_avail_idx, vq->last_avail_idx + nr_descs);
    1645                 :            : 
    1646                 :            :         vq_inc_last_avail_packed(vq, nr_descs);
    1647                 :            : 
    1648                 :            :         return 0;
    1649                 :            : }
    1650                 :            : 
    1651                 :            : static __rte_noinline uint32_t
    1652                 :          0 : virtio_dev_rx_packed(struct virtio_net *dev,
    1653                 :            :                      struct vhost_virtqueue *__rte_restrict vq,
    1654                 :            :                      struct rte_mbuf **__rte_restrict pkts,
    1655                 :            :                      uint32_t count)
    1656                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    1657                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    1658                 :            : {
    1659                 :            :         uint32_t pkt_idx = 0;
    1660                 :            : 
    1661                 :            :         do {
    1662                 :          0 :                 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
    1663                 :            : 
    1664         [ #  # ]:          0 :                 if (count - pkt_idx >= PACKED_BATCH_SIZE) {
    1665                 :          0 :                         if (!virtio_dev_rx_sync_batch_packed(dev, vq,
    1666         [ #  # ]:          0 :                                                         &pkts[pkt_idx])) {
    1667                 :          0 :                                 pkt_idx += PACKED_BATCH_SIZE;
    1668                 :          0 :                                 continue;
    1669                 :            :                         }
    1670                 :            :                 }
    1671                 :            : 
    1672         [ #  # ]:          0 :                 if (virtio_dev_rx_single_packed(dev, vq, pkts[pkt_idx]))
    1673                 :            :                         break;
    1674                 :          0 :                 pkt_idx++;
    1675                 :            : 
    1676         [ #  # ]:          0 :         } while (pkt_idx < count);
    1677                 :            : 
    1678         [ #  # ]:          0 :         if (vq->shadow_used_idx) {
    1679                 :          0 :                 do_data_copy_enqueue(dev, vq);
    1680                 :            :                 vhost_flush_enqueue_shadow_packed(dev, vq);
    1681                 :            :         }
    1682                 :            : 
    1683         [ #  # ]:          0 :         if (pkt_idx)
    1684                 :            :                 vhost_vring_call_packed(dev, vq);
    1685                 :            : 
    1686                 :          0 :         return pkt_idx;
    1687                 :            : }
    1688                 :            : 
    1689                 :            : static void
    1690                 :          0 : virtio_dev_vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
    1691                 :            : {
    1692                 :          0 :         rte_rwlock_write_lock(&vq->access_lock);
    1693                 :            :         vhost_user_iotlb_rd_lock(vq);
    1694         [ #  # ]:          0 :         if (!vq->access_ok)
    1695                 :          0 :                 vring_translate(dev, vq);
    1696                 :            :         vhost_user_iotlb_rd_unlock(vq);
    1697                 :            :         rte_rwlock_write_unlock(&vq->access_lock);
    1698                 :          0 : }
    1699                 :            : 
    1700                 :            : static __rte_always_inline uint32_t
    1701                 :            : virtio_dev_rx(struct virtio_net *dev, struct vhost_virtqueue *vq,
    1702                 :            :         struct rte_mbuf **pkts, uint32_t count)
    1703                 :            : {
    1704                 :            :         uint32_t nb_tx = 0;
    1705                 :            : 
    1706                 :            :         VHOST_DATA_LOG(dev->ifname, DEBUG, "%s", __func__);
    1707                 :          0 :         rte_rwlock_read_lock(&vq->access_lock);
    1708                 :            : 
    1709         [ #  # ]:          0 :         if (unlikely(!vq->enabled))
    1710                 :          0 :                 goto out_access_unlock;
    1711                 :            : 
    1712                 :            :         vhost_user_iotlb_rd_lock(vq);
    1713                 :            : 
    1714         [ #  # ]:          0 :         if (unlikely(!vq->access_ok)) {
    1715                 :            :                 vhost_user_iotlb_rd_unlock(vq);
    1716                 :            :                 rte_rwlock_read_unlock(&vq->access_lock);
    1717                 :            : 
    1718                 :          0 :                 virtio_dev_vring_translate(dev, vq);
    1719                 :          0 :                 goto out_no_unlock;
    1720                 :            :         }
    1721                 :            : 
    1722                 :          0 :         count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
    1723         [ #  # ]:          0 :         if (count == 0)
    1724                 :          0 :                 goto out;
    1725                 :            : 
    1726         [ #  # ]:          0 :         if (vq_is_packed(dev))
    1727                 :          0 :                 nb_tx = virtio_dev_rx_packed(dev, vq, pkts, count);
    1728                 :            :         else
    1729                 :          0 :                 nb_tx = virtio_dev_rx_split(dev, vq, pkts, count);
    1730                 :            : 
    1731                 :          0 :         vhost_queue_stats_update(dev, vq, pkts, nb_tx);
    1732                 :            : 
    1733                 :          0 : out:
    1734                 :            :         vhost_user_iotlb_rd_unlock(vq);
    1735                 :            : 
    1736                 :          0 : out_access_unlock:
    1737                 :            :         rte_rwlock_read_unlock(&vq->access_lock);
    1738                 :            : 
    1739                 :          0 : out_no_unlock:
    1740                 :            :         return nb_tx;
    1741                 :            : }
    1742                 :            : 
    1743                 :            : RTE_EXPORT_SYMBOL(rte_vhost_enqueue_burst)
    1744                 :            : uint16_t
    1745         [ #  # ]:          0 : rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
    1746                 :            :         struct rte_mbuf **__rte_restrict pkts, uint16_t count)
    1747                 :            : {
    1748                 :            :         struct virtio_net *dev = get_device(vid);
    1749                 :            : 
    1750         [ #  # ]:          0 :         if (!dev)
    1751                 :            :                 return 0;
    1752                 :            : 
    1753         [ #  # ]:          0 :         if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
    1754                 :          0 :                 VHOST_DATA_LOG(dev->ifname, ERR,
    1755                 :            :                         "%s: built-in vhost net backend is disabled.",
    1756                 :            :                         __func__);
    1757                 :          0 :                 return 0;
    1758                 :            :         }
    1759                 :            : 
    1760   [ #  #  #  # ]:          0 :         if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
    1761                 :          0 :                 VHOST_DATA_LOG(dev->ifname, ERR,
    1762                 :            :                         "%s: invalid virtqueue idx %d.",
    1763                 :            :                         __func__, queue_id);
    1764                 :          0 :                 return 0;
    1765                 :            :         }
    1766                 :            : 
    1767                 :          0 :         return virtio_dev_rx(dev, dev->virtqueue[queue_id], pkts, count);
    1768                 :            : }
    1769                 :            : 
    1770                 :            : static __rte_always_inline uint16_t
    1771                 :            : async_get_first_inflight_pkt_idx(struct vhost_virtqueue *vq)
    1772                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    1773                 :            : {
    1774                 :          0 :         struct vhost_async *async = vq->async;
    1775                 :            : 
    1776   [ #  #  #  #  :          0 :         if (async->pkts_idx >= async->pkts_inflight_n)
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    1777                 :          0 :                 return async->pkts_idx - async->pkts_inflight_n;
    1778                 :            :         else
    1779                 :          0 :                 return vq->size - async->pkts_inflight_n + async->pkts_idx;
    1780                 :            : }
    1781                 :            : 
    1782                 :            : static __rte_always_inline void
    1783                 :            : store_dma_desc_info_split(struct vring_used_elem *s_ring, struct vring_used_elem *d_ring,
    1784                 :            :                 uint16_t ring_size, uint16_t s_idx, uint16_t d_idx, uint16_t count)
    1785                 :            : {
    1786                 :            :         size_t elem_size = sizeof(struct vring_used_elem);
    1787                 :            : 
    1788                 :          0 :         if (d_idx + count <= ring_size) {
    1789         [ #  # ]:          0 :                 rte_memcpy(d_ring + d_idx, s_ring + s_idx, count * elem_size);
    1790                 :            :         } else {
    1791                 :          0 :                 uint16_t size = ring_size - d_idx;
    1792                 :            : 
    1793         [ #  # ]:          0 :                 rte_memcpy(d_ring + d_idx, s_ring + s_idx, size * elem_size);
    1794         [ #  # ]:          0 :                 rte_memcpy(d_ring, s_ring + s_idx + size, (count - size) * elem_size);
    1795                 :            :         }
    1796                 :            : }
    1797                 :            : 
    1798                 :            : static __rte_noinline uint32_t
    1799                 :          0 : virtio_dev_rx_async_submit_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
    1800                 :            :         struct rte_mbuf **pkts, uint32_t count, int16_t dma_id, uint16_t vchan_id)
    1801                 :            :         __rte_requires_capability(&vq->access_lock)
    1802                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    1803                 :            : {
    1804                 :            :         struct buf_vector buf_vec[BUF_VECTOR_MAX];
    1805                 :            :         uint32_t pkt_idx = 0;
    1806                 :            :         uint16_t num_buffers;
    1807                 :            :         uint16_t avail_head;
    1808                 :            : 
    1809                 :          0 :         struct vhost_async *async = vq->async;
    1810                 :          0 :         struct async_inflight_info *pkts_info = async->pkts_info;
    1811                 :            :         uint32_t pkt_err = 0;
    1812                 :            :         uint16_t n_xfer;
    1813                 :            :         uint16_t slot_idx = 0;
    1814                 :            : 
    1815                 :            :         /*
    1816                 :            :          * The ordering between avail index and desc reads need to be enforced.
    1817                 :            :          */
    1818                 :          0 :         avail_head = rte_atomic_load_explicit((unsigned short __rte_atomic *)&vq->avail->idx,
    1819                 :            :                 rte_memory_order_acquire);
    1820                 :            : 
    1821                 :          0 :         rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
    1822                 :            : 
    1823                 :            :         async_iter_reset(async);
    1824                 :            : 
    1825         [ #  # ]:          0 :         for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
    1826                 :          0 :                 uint64_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
    1827                 :          0 :                 uint16_t nr_vec = 0;
    1828                 :            : 
    1829         [ #  # ]:          0 :                 if (unlikely(reserve_avail_buf_split(dev, vq, pkt_len, buf_vec,
    1830                 :            :                                                 &num_buffers, avail_head, &nr_vec) < 0)) {
    1831                 :            :                         VHOST_DATA_LOG(dev->ifname, DEBUG,
    1832                 :            :                                 "failed to get enough desc from vring");
    1833                 :          0 :                         vq->shadow_used_idx -= num_buffers;
    1834                 :          0 :                         break;
    1835                 :            :                 }
    1836                 :            : 
    1837                 :            :                 VHOST_DATA_LOG(dev->ifname, DEBUG,
    1838                 :            :                         "current index %d | end index %d",
    1839                 :            :                         vq->last_avail_idx, vq->last_avail_idx + num_buffers);
    1840                 :            : 
    1841         [ #  # ]:          0 :                 if (mbuf_to_desc(dev, vq, pkts[pkt_idx], buf_vec, nr_vec, num_buffers, true) < 0) {
    1842                 :          0 :                         vq->shadow_used_idx -= num_buffers;
    1843                 :          0 :                         break;
    1844                 :            :                 }
    1845                 :            : 
    1846                 :          0 :                 slot_idx = (async->pkts_idx + pkt_idx) & (vq->size - 1);
    1847                 :          0 :                 pkts_info[slot_idx].descs = num_buffers;
    1848                 :          0 :                 pkts_info[slot_idx].mbuf = pkts[pkt_idx];
    1849                 :            : 
    1850         [ #  # ]:          0 :                 vq->last_avail_idx += num_buffers;
    1851                 :            :                 vhost_virtqueue_reconnect_log_split(vq);
    1852                 :            :         }
    1853                 :            : 
    1854         [ #  # ]:          0 :         if (unlikely(pkt_idx == 0))
    1855                 :            :                 return 0;
    1856                 :            : 
    1857                 :          0 :         n_xfer = vhost_async_dma_transfer(dev, vq, dma_id, vchan_id, async->pkts_idx,
    1858                 :          0 :                         async->iov_iter, pkt_idx);
    1859                 :            : 
    1860                 :          0 :         pkt_err = pkt_idx - n_xfer;
    1861         [ #  # ]:          0 :         if (unlikely(pkt_err)) {
    1862                 :            :                 uint16_t num_descs = 0;
    1863                 :            : 
    1864                 :            :                 VHOST_DATA_LOG(dev->ifname, DEBUG,
    1865                 :            :                         "%s: failed to transfer %u packets for queue %u.",
    1866                 :            :                         __func__, pkt_err, vq->index);
    1867                 :            : 
    1868                 :            :                 /* update number of completed packets */
    1869                 :            :                 pkt_idx = n_xfer;
    1870                 :            : 
    1871                 :            :                 /* calculate the sum of descriptors to revert */
    1872         [ #  # ]:          0 :                 while (pkt_err-- > 0) {
    1873                 :          0 :                         num_descs += pkts_info[slot_idx & (vq->size - 1)].descs;
    1874                 :          0 :                         slot_idx--;
    1875                 :            :                 }
    1876                 :            : 
    1877                 :            :                 /* recover shadow used ring and available ring */
    1878                 :          0 :                 vq->shadow_used_idx -= num_descs;
    1879         [ #  # ]:          0 :                 vq->last_avail_idx -= num_descs;
    1880                 :            :                 vhost_virtqueue_reconnect_log_split(vq);
    1881                 :            :         }
    1882                 :            : 
    1883                 :            :         /* keep used descriptors */
    1884         [ #  # ]:          0 :         if (likely(vq->shadow_used_idx)) {
    1885                 :          0 :                 uint16_t to = async->desc_idx_split & (vq->size - 1);
    1886                 :            : 
    1887         [ #  # ]:          0 :                 store_dma_desc_info_split(vq->shadow_used_split,
    1888                 :            :                                 async->descs_split, vq->size, 0, to,
    1889                 :            :                                 vq->shadow_used_idx);
    1890                 :            : 
    1891                 :          0 :                 async->desc_idx_split += vq->shadow_used_idx;
    1892                 :            : 
    1893                 :          0 :                 async->pkts_idx += pkt_idx;
    1894         [ #  # ]:          0 :                 if (async->pkts_idx >= vq->size)
    1895                 :          0 :                         async->pkts_idx -= vq->size;
    1896                 :            : 
    1897                 :          0 :                 async->pkts_inflight_n += pkt_idx;
    1898                 :          0 :                 vq->shadow_used_idx = 0;
    1899                 :            :         }
    1900                 :            : 
    1901                 :            :         return pkt_idx;
    1902                 :            : }
    1903                 :            : 
    1904                 :            : 
    1905                 :            : static __rte_always_inline int
    1906                 :            : vhost_enqueue_async_packed(struct virtio_net *dev,
    1907                 :            :                             struct vhost_virtqueue *vq,
    1908                 :            :                             struct rte_mbuf *pkt,
    1909                 :            :                             struct buf_vector *buf_vec,
    1910                 :            :                             uint16_t *nr_descs,
    1911                 :            :                             uint16_t *nr_buffers)
    1912                 :            :         __rte_requires_capability(&vq->access_lock)
    1913                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    1914                 :          0 : {
    1915                 :            :         uint16_t nr_vec = 0;
    1916                 :          0 :         uint16_t avail_idx = vq->last_avail_idx;
    1917                 :            :         uint16_t max_tries, tries = 0;
    1918                 :            :         uint16_t buf_id = 0;
    1919                 :            :         uint32_t len = 0;
    1920                 :            :         uint16_t desc_count = 0;
    1921                 :          0 :         uint64_t size = pkt->pkt_len + sizeof(struct virtio_net_hdr_mrg_rxbuf);
    1922                 :          0 :         uint32_t buffer_len[vq->size];
    1923                 :          0 :         uint16_t buffer_buf_id[vq->size];
    1924                 :          0 :         uint16_t buffer_desc_count[vq->size];
    1925                 :            : 
    1926                 :          0 :         if (rxvq_is_mergeable(dev))
    1927                 :          0 :                 max_tries = vq->size - 1;
    1928                 :            :         else
    1929                 :            :                 max_tries = 1;
    1930                 :            : 
    1931                 :            :         do {
    1932                 :            :                 /*
    1933                 :            :                  * if we tried all available ring items, and still
    1934                 :            :                  * can't get enough buf, it means something abnormal
    1935                 :            :                  * happened.
    1936                 :            :                  */
    1937         [ #  # ]:          0 :                 if (unlikely(++tries > max_tries))
    1938                 :            :                         return -1;
    1939                 :            : 
    1940         [ #  # ]:          0 :                 if (unlikely(fill_vec_buf_packed(dev, vq,
    1941                 :            :                                                 avail_idx, &desc_count,
    1942                 :            :                                                 buf_vec, &nr_vec,
    1943                 :            :                                                 &buf_id, &len,
    1944                 :            :                                                 VHOST_ACCESS_RW) < 0))
    1945                 :            :                         return -1;
    1946                 :            : 
    1947                 :          0 :                 len = RTE_MIN(len, size);
    1948                 :          0 :                 size -= len;
    1949                 :            : 
    1950                 :          0 :                 buffer_len[*nr_buffers] = len;
    1951                 :          0 :                 buffer_buf_id[*nr_buffers] = buf_id;
    1952                 :          0 :                 buffer_desc_count[*nr_buffers] = desc_count;
    1953                 :            :                 *nr_buffers += 1;
    1954                 :          0 :                 *nr_descs += desc_count;
    1955                 :          0 :                 avail_idx += desc_count;
    1956         [ #  # ]:          0 :                 if (avail_idx >= vq->size)
    1957                 :          0 :                         avail_idx -= vq->size;
    1958         [ #  # ]:          0 :         } while (size > 0);
    1959                 :            : 
    1960         [ #  # ]:          0 :         if (unlikely(mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec, *nr_buffers, true) < 0))
    1961                 :            :                 return -1;
    1962                 :            : 
    1963                 :            :         vhost_async_shadow_enqueue_packed(vq, buffer_len, buffer_buf_id,
    1964                 :            :                                         buffer_desc_count, *nr_buffers);
    1965                 :            : 
    1966                 :            :         return 0;
    1967                 :            : }
    1968                 :            : 
    1969                 :            : static __rte_always_inline int16_t
    1970                 :            : virtio_dev_rx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
    1971                 :            :                             struct rte_mbuf *pkt, uint16_t *nr_descs, uint16_t *nr_buffers)
    1972                 :            :         __rte_requires_capability(&vq->access_lock)
    1973                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    1974                 :            : {
    1975                 :            :         struct buf_vector buf_vec[BUF_VECTOR_MAX];
    1976                 :            : 
    1977         [ #  # ]:          0 :         if (unlikely(vhost_enqueue_async_packed(dev, vq, pkt, buf_vec,
    1978                 :            :                                         nr_descs, nr_buffers) < 0)) {
    1979                 :            :                 VHOST_DATA_LOG(dev->ifname, DEBUG, "failed to get enough desc from vring");
    1980                 :          0 :                 return -1;
    1981                 :            :         }
    1982                 :            : 
    1983                 :            :         VHOST_DATA_LOG(dev->ifname, DEBUG,
    1984                 :            :                 "current index %d | end index %d",
    1985                 :            :                 vq->last_avail_idx, vq->last_avail_idx + *nr_descs);
    1986                 :            : 
    1987                 :            :         return 0;
    1988                 :            : }
    1989                 :            : 
    1990                 :            : static __rte_always_inline void
    1991                 :            : virtio_dev_rx_async_packed_batch_enqueue(struct virtio_net *dev,
    1992                 :            :                            struct vhost_virtqueue *vq,
    1993                 :            :                            struct rte_mbuf **pkts,
    1994                 :            :                            uint64_t *desc_addrs,
    1995                 :            :                            uint64_t *lens)
    1996                 :            :         __rte_requires_capability(&vq->access_lock)
    1997                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    1998                 :            : {
    1999                 :            :         uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
    2000                 :            :         struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE];
    2001                 :          0 :         struct vring_packed_desc *descs = vq->desc_packed;
    2002                 :          0 :         struct vhost_async *async = vq->async;
    2003                 :          0 :         uint16_t avail_idx = vq->last_avail_idx;
    2004                 :            :         uint32_t mbuf_offset = 0;
    2005                 :            :         uint16_t ids[PACKED_BATCH_SIZE];
    2006                 :            :         uint64_t mapped_len[PACKED_BATCH_SIZE];
    2007                 :            :         void *host_iova[PACKED_BATCH_SIZE];
    2008                 :            :         uintptr_t desc;
    2009                 :            :         uint16_t i;
    2010                 :            : 
    2011         [ #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    2012                 :          0 :                 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
    2013         [ #  # ]:          0 :                 desc = vhost_iova_to_vva(dev, vq, desc_addrs[i], &lens[i], VHOST_ACCESS_RW);
    2014                 :          0 :                 hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)desc;
    2015                 :          0 :                 lens[i] = pkts[i]->pkt_len +
    2016                 :            :                         sizeof(struct virtio_net_hdr_mrg_rxbuf);
    2017                 :            :         }
    2018                 :            : 
    2019         [ #  # ]:          0 :         if (rxvq_is_mergeable(dev)) {
    2020         [ #  # ]:          0 :                 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    2021         [ #  # ]:          0 :                         ASSIGN_UNLESS_EQUAL(hdrs[i]->num_buffers, 1);
    2022                 :            :                 }
    2023                 :            :         }
    2024                 :            : 
    2025         [ #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
    2026         [ #  # ]:          0 :                 virtio_enqueue_offload(pkts[i], &hdrs[i]->hdr);
    2027                 :            : 
    2028                 :            :         vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
    2029                 :            : 
    2030         [ #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    2031                 :          0 :                 host_iova[i] = (void *)(uintptr_t)gpa_to_first_hpa(dev,
    2032         [ #  # ]:          0 :                         desc_addrs[i] + buf_offset, lens[i], &mapped_len[i]);
    2033                 :            :         }
    2034                 :            : 
    2035         [ #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    2036                 :            :                 async_iter_initialize(dev, async);
    2037                 :          0 :                 async_iter_add_iovec(dev, async,
    2038         [ #  # ]:          0 :                                 (void *)(uintptr_t)rte_pktmbuf_iova_offset(pkts[i], mbuf_offset),
    2039                 :            :                                 host_iova[i],
    2040                 :            :                                 mapped_len[i]);
    2041                 :          0 :                 async->iter_idx++;
    2042                 :            :         }
    2043                 :            : 
    2044         [ #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
    2045         [ #  # ]:          0 :                 vhost_log_cache_write_iova(dev, vq, descs[avail_idx + i].addr, lens[i]);
    2046                 :            : 
    2047         [ #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
    2048                 :          0 :                 ids[i] = descs[avail_idx + i].id;
    2049                 :            : 
    2050                 :            :         vhost_async_shadow_enqueue_packed_batch(vq, lens, ids);
    2051                 :            : }
    2052                 :            : 
    2053                 :            : static __rte_always_inline int
    2054                 :            : virtio_dev_rx_async_packed_batch(struct virtio_net *dev,
    2055                 :            :                            struct vhost_virtqueue *vq,
    2056                 :            :                            struct rte_mbuf **pkts,
    2057                 :            :                            int16_t dma_id, uint16_t vchan_id)
    2058                 :            :         __rte_requires_capability(&vq->access_lock)
    2059                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    2060                 :            : {
    2061                 :            :         uint64_t desc_addrs[PACKED_BATCH_SIZE];
    2062                 :            :         uint64_t lens[PACKED_BATCH_SIZE];
    2063                 :            : 
    2064                 :            :         if (virtio_dev_rx_async_batch_check(vq, pkts, desc_addrs, lens, dma_id, vchan_id) == -1)
    2065                 :            :                 return -1;
    2066                 :            : 
    2067                 :            :         virtio_dev_rx_async_packed_batch_enqueue(dev, vq, pkts, desc_addrs, lens);
    2068                 :            : 
    2069                 :            :         return 0;
    2070                 :            : }
    2071                 :            : 
    2072                 :            : static __rte_always_inline void
    2073                 :            : dma_error_handler_packed(struct vhost_virtqueue *vq, uint16_t slot_idx,
    2074                 :            :                         uint32_t nr_err, uint32_t *pkt_idx)
    2075                 :            :         __rte_requires_capability(&vq->access_lock)
    2076                 :            : {
    2077                 :            :         uint16_t descs_err = 0;
    2078                 :            :         uint16_t buffers_err = 0;
    2079                 :          0 :         struct vhost_async *async = vq->async;
    2080                 :          0 :         struct async_inflight_info *pkts_info = vq->async->pkts_info;
    2081                 :            : 
    2082                 :            :         *pkt_idx -= nr_err;
    2083                 :            :         /* calculate the sum of buffers and descs of DMA-error packets. */
    2084         [ #  # ]:          0 :         while (nr_err-- > 0) {
    2085                 :          0 :                 descs_err += pkts_info[slot_idx % vq->size].descs;
    2086                 :          0 :                 buffers_err += pkts_info[slot_idx % vq->size].nr_buffers;
    2087                 :          0 :                 slot_idx--;
    2088                 :            :         }
    2089                 :            : 
    2090         [ #  # ]:          0 :         if (vq->last_avail_idx >= descs_err) {
    2091                 :          0 :                 vq->last_avail_idx -= descs_err;
    2092                 :            :         } else {
    2093                 :          0 :                 vq->last_avail_idx = vq->last_avail_idx + vq->size - descs_err;
    2094                 :          0 :                 vq->avail_wrap_counter ^= 1;
    2095                 :            :         }
    2096                 :            :         vhost_virtqueue_reconnect_log_packed(vq);
    2097                 :            : 
    2098         [ #  # ]:          0 :         if (async->buffer_idx_packed >= buffers_err)
    2099                 :          0 :                 async->buffer_idx_packed -= buffers_err;
    2100                 :            :         else
    2101                 :          0 :                 async->buffer_idx_packed = async->buffer_idx_packed + vq->size - buffers_err;
    2102                 :            : }
    2103                 :            : 
    2104                 :            : static __rte_noinline uint32_t
    2105                 :          0 : virtio_dev_rx_async_submit_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
    2106                 :            :         struct rte_mbuf **pkts, uint32_t count, int16_t dma_id, uint16_t vchan_id)
    2107                 :            :         __rte_requires_capability(&vq->access_lock)
    2108                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    2109                 :            : {
    2110                 :            :         uint32_t pkt_idx = 0;
    2111                 :            :         uint16_t n_xfer;
    2112                 :            :         uint16_t num_buffers;
    2113                 :            :         uint16_t num_descs;
    2114                 :            : 
    2115                 :          0 :         struct vhost_async *async = vq->async;
    2116                 :          0 :         struct async_inflight_info *pkts_info = async->pkts_info;
    2117                 :            :         uint32_t pkt_err = 0;
    2118                 :            :         uint16_t slot_idx = 0;
    2119                 :            :         uint16_t i;
    2120                 :            : 
    2121                 :            :         do {
    2122                 :          0 :                 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
    2123                 :            : 
    2124         [ #  # ]:          0 :                 if (count - pkt_idx >= PACKED_BATCH_SIZE) {
    2125         [ #  # ]:          0 :                         if (!virtio_dev_rx_async_packed_batch(dev, vq, &pkts[pkt_idx],
    2126                 :            :                                         dma_id, vchan_id)) {
    2127         [ #  # ]:          0 :                                 for (i = 0; i < PACKED_BATCH_SIZE; i++) {
    2128                 :          0 :                                         slot_idx = (async->pkts_idx + pkt_idx) % vq->size;
    2129                 :          0 :                                         pkts_info[slot_idx].descs = 1;
    2130                 :          0 :                                         pkts_info[slot_idx].nr_buffers = 1;
    2131                 :          0 :                                         pkts_info[slot_idx].mbuf = pkts[pkt_idx];
    2132                 :          0 :                                         pkt_idx++;
    2133                 :            :                                 }
    2134                 :          0 :                                 continue;
    2135                 :            :                         }
    2136                 :            :                 }
    2137                 :            : 
    2138                 :            :                 num_buffers = 0;
    2139                 :            :                 num_descs = 0;
    2140   [ #  #  #  # ]:          0 :                 if (unlikely(virtio_dev_rx_async_packed(dev, vq, pkts[pkt_idx],
    2141                 :            :                                                 &num_descs, &num_buffers) < 0))
    2142                 :            :                         break;
    2143                 :            : 
    2144                 :          0 :                 slot_idx = (async->pkts_idx + pkt_idx) % vq->size;
    2145                 :            : 
    2146                 :          0 :                 pkts_info[slot_idx].descs = num_descs;
    2147                 :          0 :                 pkts_info[slot_idx].nr_buffers = num_buffers;
    2148                 :          0 :                 pkts_info[slot_idx].mbuf = pkts[pkt_idx];
    2149                 :            : 
    2150         [ #  # ]:          0 :                 pkt_idx++;
    2151                 :            :                 vq_inc_last_avail_packed(vq, num_descs);
    2152         [ #  # ]:          0 :         } while (pkt_idx < count);
    2153                 :            : 
    2154         [ #  # ]:          0 :         if (unlikely(pkt_idx == 0))
    2155                 :            :                 return 0;
    2156                 :            : 
    2157                 :          0 :         n_xfer = vhost_async_dma_transfer(dev, vq, dma_id, vchan_id, async->pkts_idx,
    2158                 :          0 :                         async->iov_iter, pkt_idx);
    2159                 :            : 
    2160                 :            :         async_iter_reset(async);
    2161                 :            : 
    2162                 :          0 :         pkt_err = pkt_idx - n_xfer;
    2163         [ #  # ]:          0 :         if (unlikely(pkt_err)) {
    2164                 :            :                 VHOST_DATA_LOG(dev->ifname, DEBUG,
    2165                 :            :                         "%s: failed to transfer %u packets for queue %u.",
    2166                 :            :                         __func__, pkt_err, vq->index);
    2167                 :            :                 dma_error_handler_packed(vq, slot_idx, pkt_err, &pkt_idx);
    2168                 :            :         }
    2169                 :            : 
    2170                 :          0 :         async->pkts_idx += pkt_idx;
    2171         [ #  # ]:          0 :         if (async->pkts_idx >= vq->size)
    2172                 :          0 :                 async->pkts_idx -= vq->size;
    2173                 :            : 
    2174                 :          0 :         async->pkts_inflight_n += pkt_idx;
    2175                 :            : 
    2176                 :          0 :         return pkt_idx;
    2177                 :            : }
    2178                 :            : 
    2179                 :            : static __rte_always_inline void
    2180                 :            : write_back_completed_descs_split(struct vhost_virtqueue *vq, uint16_t n_descs)
    2181                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    2182                 :            : {
    2183                 :          0 :         struct vhost_async *async = vq->async;
    2184                 :            :         uint16_t nr_left = n_descs;
    2185                 :            :         uint16_t nr_copy;
    2186                 :            :         uint16_t to, from;
    2187                 :            : 
    2188                 :            :         do {
    2189                 :          0 :                 from = async->last_desc_idx_split & (vq->size - 1);
    2190   [ #  #  #  #  :          0 :                 nr_copy = nr_left + from <= vq->size ? nr_left : vq->size - from;
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2191                 :          0 :                 to = vq->last_used_idx & (vq->size - 1);
    2192                 :            : 
    2193   [ #  #  #  #  :          0 :                 if (to + nr_copy <= vq->size) {
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2194   [ #  #  #  #  :          0 :                         rte_memcpy(&vq->used->ring[to], &async->descs_split[from],
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2195                 :            :                                         nr_copy * sizeof(struct vring_used_elem));
    2196                 :            :                 } else {
    2197                 :          0 :                         uint16_t size = vq->size - to;
    2198                 :            : 
    2199   [ #  #  #  #  :          0 :                         rte_memcpy(&vq->used->ring[to], &async->descs_split[from],
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2200                 :            :                                         size * sizeof(struct vring_used_elem));
    2201                 :          0 :                         rte_memcpy(&vq->used->ring[0], &async->descs_split[from + size],
    2202   [ #  #  #  #  :          0 :                                         (nr_copy - size) * sizeof(struct vring_used_elem));
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2203                 :            :                 }
    2204                 :            : 
    2205                 :          0 :                 async->last_desc_idx_split += nr_copy;
    2206                 :          0 :                 vq->last_used_idx += nr_copy;
    2207                 :          0 :                 nr_left -= nr_copy;
    2208   [ #  #  #  #  :          0 :         } while (nr_left > 0);
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2209                 :            : }
    2210                 :            : 
    2211                 :            : static __rte_always_inline void
    2212                 :            : write_back_completed_descs_packed(struct vhost_virtqueue *vq,
    2213                 :            :                                 uint16_t n_buffers)
    2214                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    2215                 :            : {
    2216                 :          0 :         struct vhost_async *async = vq->async;
    2217                 :          0 :         uint16_t from = async->last_buffer_idx_packed;
    2218                 :          0 :         uint16_t used_idx = vq->last_used_idx;
    2219                 :            :         uint16_t head_idx = vq->last_used_idx;
    2220                 :            :         uint16_t head_flags = 0;
    2221                 :            :         uint16_t i;
    2222                 :            : 
    2223                 :            :         /* Split loop in two to save memory barriers */
    2224   [ #  #  #  #  :          0 :         for (i = 0; i < n_buffers; i++) {
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2225                 :          0 :                 vq->desc_packed[used_idx].id = async->buffers_packed[from].id;
    2226                 :          0 :                 vq->desc_packed[used_idx].len = async->buffers_packed[from].len;
    2227                 :            : 
    2228                 :          0 :                 used_idx += async->buffers_packed[from].count;
    2229   [ #  #  #  #  :          0 :                 if (used_idx >= vq->size)
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2230                 :          0 :                         used_idx -= vq->size;
    2231                 :            : 
    2232                 :          0 :                 from++;
    2233   [ #  #  #  #  :          0 :                 if (from >= vq->size)
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2234                 :            :                         from = 0;
    2235                 :            :         }
    2236                 :            : 
    2237                 :            :         /* The ordering for storing desc flags needs to be enforced. */
    2238                 :            :         rte_atomic_thread_fence(rte_memory_order_release);
    2239                 :            : 
    2240                 :          0 :         from = async->last_buffer_idx_packed;
    2241                 :            : 
    2242   [ #  #  #  #  :          0 :         for (i = 0; i < n_buffers; i++) {
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2243                 :            :                 uint16_t flags;
    2244                 :            : 
    2245   [ #  #  #  #  :          0 :                 if (async->buffers_packed[from].len)
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2246                 :            :                         flags = VRING_DESC_F_WRITE;
    2247                 :            :                 else
    2248                 :            :                         flags = 0;
    2249                 :            : 
    2250   [ #  #  #  #  :          0 :                 if (vq->used_wrap_counter) {
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2251                 :            :                         flags |= VRING_DESC_F_USED;
    2252                 :          0 :                         flags |= VRING_DESC_F_AVAIL;
    2253                 :            :                 } else {
    2254                 :            :                         flags &= ~VRING_DESC_F_USED;
    2255                 :            :                         flags &= ~VRING_DESC_F_AVAIL;
    2256                 :            :                 }
    2257                 :            : 
    2258   [ #  #  #  #  :          0 :                 if (i > 0) {
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2259                 :          0 :                         vq->desc_packed[vq->last_used_idx].flags = flags;
    2260                 :            :                 } else {
    2261                 :          0 :                         head_idx = vq->last_used_idx;
    2262                 :            :                         head_flags = flags;
    2263                 :            :                 }
    2264                 :            : 
    2265   [ #  #  #  #  :          0 :                 vq_inc_last_used_packed(vq, async->buffers_packed[from].count);
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2266                 :            : 
    2267                 :          0 :                 from++;
    2268   [ #  #  #  #  :          0 :                 if (from == vq->size)
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2269                 :            :                         from = 0;
    2270                 :            :         }
    2271                 :            : 
    2272                 :          0 :         vq->desc_packed[head_idx].flags = head_flags;
    2273                 :          0 :         async->last_buffer_idx_packed = from;
    2274                 :            : }
    2275                 :            : 
    2276                 :            : static __rte_always_inline uint16_t
    2277                 :            : vhost_poll_enqueue_completed(struct virtio_net *dev, struct vhost_virtqueue *vq,
    2278                 :            :         struct rte_mbuf **pkts, uint16_t count, int16_t dma_id, uint16_t vchan_id)
    2279                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    2280                 :            : {
    2281                 :            :         struct vhost_async *async = vq->async;
    2282                 :          0 :         struct async_inflight_info *pkts_info = async->pkts_info;
    2283                 :            :         uint16_t nr_cpl_pkts = 0;
    2284                 :            :         uint16_t n_descs = 0, n_buffers = 0;
    2285                 :            :         uint16_t start_idx, from, i;
    2286                 :            : 
    2287                 :            :         /* Check completed copies for the given DMA vChannel */
    2288                 :            :         vhost_async_dma_check_completed(dev, dma_id, vchan_id, VHOST_DMA_MAX_COPY_COMPLETE);
    2289                 :            : 
    2290                 :            :         start_idx = async_get_first_inflight_pkt_idx(vq);
    2291                 :            :         /**
    2292                 :            :          * Calculate the number of copy completed packets.
    2293                 :            :          * Note that there may be completed packets even if
    2294                 :            :          * no copies are reported done by the given DMA vChannel,
    2295                 :            :          * as it's possible that a virtqueue uses multiple DMA
    2296                 :            :          * vChannels.
    2297                 :            :          */
    2298                 :            :         from = start_idx;
    2299   [ #  #  #  #  :          0 :         while (vq->async->pkts_cmpl_flag[from] && count--) {
          #  #  #  #  #  
                #  #  # ]
    2300                 :          0 :                 vq->async->pkts_cmpl_flag[from] = false;
    2301                 :          0 :                 from++;
    2302   [ #  #  #  #  :          0 :                 if (from >= vq->size)
                   #  # ]
    2303                 :          0 :                         from -= vq->size;
    2304                 :          0 :                 nr_cpl_pkts++;
    2305                 :            :         }
    2306                 :            : 
    2307   [ #  #  #  #  :          0 :         if (nr_cpl_pkts == 0)
                   #  # ]
    2308                 :            :                 return 0;
    2309                 :            : 
    2310   [ #  #  #  #  :          0 :         for (i = 0; i < nr_cpl_pkts; i++) {
                   #  # ]
    2311                 :          0 :                 from = (start_idx + i) % vq->size;
    2312                 :            :                 /* Only used with packed ring */
    2313                 :          0 :                 n_buffers += pkts_info[from].nr_buffers;
    2314                 :            :                 /* Only used with split ring */
    2315                 :          0 :                 n_descs += pkts_info[from].descs;
    2316                 :          0 :                 pkts[i] = pkts_info[from].mbuf;
    2317                 :            :         }
    2318                 :            : 
    2319                 :          0 :         async->pkts_inflight_n -= nr_cpl_pkts;
    2320                 :            : 
    2321   [ #  #  #  #  :          0 :         if (likely(vq->enabled && vq->access_ok)) {
          #  #  #  #  #  
                #  #  # ]
    2322   [ #  #  #  #  :          0 :                 if (vq_is_packed(dev)) {
                   #  # ]
    2323                 :            :                         write_back_completed_descs_packed(vq, n_buffers);
    2324                 :            :                         vhost_vring_call_packed(dev, vq);
    2325                 :            :                 } else {
    2326                 :          0 :                         write_back_completed_descs_split(vq, n_descs);
    2327                 :          0 :                         rte_atomic_fetch_add_explicit(
    2328                 :            :                                 (unsigned short __rte_atomic *)&vq->used->idx,
    2329                 :            :                                 n_descs, rte_memory_order_release);
    2330                 :            :                         vhost_vring_call_split(dev, vq);
    2331                 :            :                 }
    2332                 :            :         } else {
    2333   [ #  #  #  #  :          0 :                 if (vq_is_packed(dev)) {
                   #  # ]
    2334                 :          0 :                         async->last_buffer_idx_packed += n_buffers;
    2335   [ #  #  #  #  :          0 :                         if (async->last_buffer_idx_packed >= vq->size)
                   #  # ]
    2336                 :          0 :                                 async->last_buffer_idx_packed -= vq->size;
    2337                 :            :                 } else {
    2338                 :          0 :                         async->last_desc_idx_split += n_descs;
    2339                 :            :                 }
    2340                 :            :         }
    2341                 :            : 
    2342                 :            :         return nr_cpl_pkts;
    2343                 :            : }
    2344                 :            : 
    2345                 :            : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_vhost_poll_enqueue_completed, 20.08)
    2346                 :            : uint16_t
    2347         [ #  # ]:          0 : rte_vhost_poll_enqueue_completed(int vid, uint16_t queue_id,
    2348                 :            :                 struct rte_mbuf **pkts, uint16_t count, int16_t dma_id,
    2349                 :            :                 uint16_t vchan_id)
    2350                 :            : {
    2351                 :            :         struct virtio_net *dev = get_device(vid);
    2352                 :            :         struct vhost_virtqueue *vq;
    2353                 :            :         uint16_t n_pkts_cpl = 0;
    2354                 :            : 
    2355         [ #  # ]:          0 :         if (unlikely(!dev))
    2356                 :            :                 return 0;
    2357                 :            : 
    2358                 :            :         VHOST_DATA_LOG(dev->ifname, DEBUG, "%s", __func__);
    2359   [ #  #  #  # ]:          0 :         if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
    2360                 :          0 :                 VHOST_DATA_LOG(dev->ifname, ERR,
    2361                 :            :                         "%s: invalid virtqueue idx %d.",
    2362                 :            :                         __func__, queue_id);
    2363                 :          0 :                 return 0;
    2364                 :            :         }
    2365                 :            : 
    2366   [ #  #  #  # ]:          0 :         if (unlikely(!dma_copy_track[dma_id].vchans ||
    2367                 :            :                                 !dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr)) {
    2368                 :          0 :                 VHOST_DATA_LOG(dev->ifname, ERR,
    2369                 :            :                         "%s: invalid channel %d:%u.",
    2370                 :            :                         __func__, dma_id, vchan_id);
    2371                 :          0 :                 return 0;
    2372                 :            :         }
    2373                 :            : 
    2374                 :          0 :         vq = dev->virtqueue[queue_id];
    2375                 :            : 
    2376         [ #  # ]:          0 :         if (rte_rwlock_read_trylock(&vq->access_lock)) {
    2377                 :            :                 VHOST_DATA_LOG(dev->ifname, DEBUG,
    2378                 :            :                         "%s: virtqueue %u is busy.",
    2379                 :            :                         __func__, queue_id);
    2380                 :            :                 return 0;
    2381                 :            :         }
    2382                 :            : 
    2383         [ #  # ]:          0 :         if (unlikely(!vq->async)) {
    2384                 :          0 :                 VHOST_DATA_LOG(dev->ifname, ERR,
    2385                 :            :                         "%s: async not registered for virtqueue %d.",
    2386                 :            :                         __func__, queue_id);
    2387                 :          0 :                 goto out;
    2388                 :            :         }
    2389                 :            : 
    2390                 :          0 :         n_pkts_cpl = vhost_poll_enqueue_completed(dev, vq, pkts, count, dma_id, vchan_id);
    2391                 :            : 
    2392                 :          0 :         vhost_queue_stats_update(dev, vq, pkts, n_pkts_cpl);
    2393                 :          0 :         vq->stats.inflight_completed += n_pkts_cpl;
    2394                 :            : 
    2395                 :          0 : out:
    2396                 :            :         rte_rwlock_read_unlock(&vq->access_lock);
    2397                 :            : 
    2398                 :          0 :         return n_pkts_cpl;
    2399                 :            : }
    2400                 :            : 
    2401                 :            : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_vhost_clear_queue_thread_unsafe, 21.08)
    2402                 :            : uint16_t
    2403         [ #  # ]:          0 : rte_vhost_clear_queue_thread_unsafe(int vid, uint16_t queue_id,
    2404                 :            :                 struct rte_mbuf **pkts, uint16_t count, int16_t dma_id,
    2405                 :            :                 uint16_t vchan_id)
    2406                 :            : {
    2407                 :            :         struct virtio_net *dev = get_device(vid);
    2408                 :            :         struct vhost_virtqueue *vq;
    2409                 :            :         uint16_t n_pkts_cpl = 0;
    2410                 :            : 
    2411         [ #  # ]:          0 :         if (!dev)
    2412                 :            :                 return 0;
    2413                 :            : 
    2414                 :            :         VHOST_DATA_LOG(dev->ifname, DEBUG, "%s", __func__);
    2415         [ #  # ]:          0 :         if (unlikely(queue_id >= dev->nr_vring)) {
    2416                 :          0 :                 VHOST_DATA_LOG(dev->ifname, ERR, "%s: invalid virtqueue idx %d.",
    2417                 :            :                         __func__, queue_id);
    2418                 :          0 :                 return 0;
    2419                 :            :         }
    2420                 :            : 
    2421         [ #  # ]:          0 :         if (unlikely(dma_id < 0 || dma_id >= RTE_DMADEV_DEFAULT_MAX)) {
    2422                 :          0 :                 VHOST_DATA_LOG(dev->ifname, ERR, "%s: invalid dma id %d.",
    2423                 :            :                         __func__, dma_id);
    2424                 :          0 :                 return 0;
    2425                 :            :         }
    2426                 :            : 
    2427                 :          0 :         vq = dev->virtqueue[queue_id];
    2428                 :            : 
    2429                 :          0 :         vq_assert_lock(dev, vq);
    2430                 :            : 
    2431         [ #  # ]:          0 :         if (unlikely(!vq->async)) {
    2432                 :          0 :                 VHOST_DATA_LOG(dev->ifname, ERR,
    2433                 :            :                         "%s: async not registered for virtqueue %d.",
    2434                 :            :                         __func__, queue_id);
    2435                 :          0 :                 return 0;
    2436                 :            :         }
    2437                 :            : 
    2438   [ #  #  #  # ]:          0 :         if (unlikely(!dma_copy_track[dma_id].vchans ||
    2439                 :            :                                 !dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr)) {
    2440                 :          0 :                 VHOST_DATA_LOG(dev->ifname, ERR,
    2441                 :            :                         "%s: invalid channel %d:%u.",
    2442                 :            :                         __func__, dma_id, vchan_id);
    2443                 :          0 :                 return 0;
    2444                 :            :         }
    2445                 :            : 
    2446         [ #  # ]:          0 :         if ((queue_id & 1) == 0)
    2447                 :          0 :                 n_pkts_cpl = vhost_poll_enqueue_completed(dev, vq, pkts, count,
    2448                 :            :                         dma_id, vchan_id);
    2449                 :            :         else
    2450                 :          0 :                 n_pkts_cpl = async_poll_dequeue_completed(dev, vq, pkts, count,
    2451                 :          0 :                         dma_id, vchan_id, dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS);
    2452                 :            : 
    2453                 :          0 :         vhost_queue_stats_update(dev, vq, pkts, n_pkts_cpl);
    2454                 :          0 :         vq->stats.inflight_completed += n_pkts_cpl;
    2455                 :            : 
    2456                 :          0 :         return n_pkts_cpl;
    2457                 :            : }
    2458                 :            : 
    2459                 :            : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_vhost_clear_queue, 22.07)
    2460                 :            : uint16_t
    2461         [ #  # ]:          0 : rte_vhost_clear_queue(int vid, uint16_t queue_id, struct rte_mbuf **pkts,
    2462                 :            :                 uint16_t count, int16_t dma_id, uint16_t vchan_id)
    2463                 :            : {
    2464                 :            :         struct virtio_net *dev = get_device(vid);
    2465                 :            :         struct vhost_virtqueue *vq;
    2466                 :            :         uint16_t n_pkts_cpl = 0;
    2467                 :            : 
    2468         [ #  # ]:          0 :         if (!dev)
    2469                 :            :                 return 0;
    2470                 :            : 
    2471                 :            :         VHOST_DATA_LOG(dev->ifname, DEBUG, "%s", __func__);
    2472         [ #  # ]:          0 :         if (unlikely(queue_id >= dev->nr_vring)) {
    2473                 :          0 :                 VHOST_DATA_LOG(dev->ifname, ERR, "%s: invalid virtqueue idx %u.",
    2474                 :            :                         __func__, queue_id);
    2475                 :          0 :                 return 0;
    2476                 :            :         }
    2477                 :            : 
    2478         [ #  # ]:          0 :         if (unlikely(dma_id < 0 || dma_id >= RTE_DMADEV_DEFAULT_MAX)) {
    2479                 :          0 :                 VHOST_DATA_LOG(dev->ifname, ERR, "%s: invalid dma id %d.",
    2480                 :            :                         __func__, dma_id);
    2481                 :          0 :                 return 0;
    2482                 :            :         }
    2483                 :            : 
    2484                 :          0 :         vq = dev->virtqueue[queue_id];
    2485                 :            : 
    2486         [ #  # ]:          0 :         if (rte_rwlock_read_trylock(&vq->access_lock)) {
    2487                 :            :                 VHOST_DATA_LOG(dev->ifname, DEBUG, "%s: virtqueue %u is busy.",
    2488                 :            :                         __func__, queue_id);
    2489                 :            :                 return 0;
    2490                 :            :         }
    2491                 :            : 
    2492         [ #  # ]:          0 :         if (unlikely(!vq->async)) {
    2493                 :          0 :                 VHOST_DATA_LOG(dev->ifname, ERR, "%s: async not registered for queue id %u.",
    2494                 :            :                         __func__, queue_id);
    2495                 :          0 :                 goto out_access_unlock;
    2496                 :            :         }
    2497                 :            : 
    2498   [ #  #  #  # ]:          0 :         if (unlikely(!dma_copy_track[dma_id].vchans ||
    2499                 :            :                                 !dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr)) {
    2500                 :          0 :                 VHOST_DATA_LOG(dev->ifname, ERR, "%s: invalid channel %d:%u.",
    2501                 :            :                         __func__, dma_id, vchan_id);
    2502                 :          0 :                 goto out_access_unlock;
    2503                 :            :         }
    2504                 :            : 
    2505         [ #  # ]:          0 :         if ((queue_id & 1) == 0)
    2506                 :          0 :                 n_pkts_cpl = vhost_poll_enqueue_completed(dev, vq, pkts, count,
    2507                 :            :                         dma_id, vchan_id);
    2508                 :            :         else
    2509                 :          0 :                 n_pkts_cpl = async_poll_dequeue_completed(dev, vq, pkts, count,
    2510                 :          0 :                         dma_id, vchan_id, dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS);
    2511                 :            : 
    2512                 :          0 :         vhost_queue_stats_update(dev, vq, pkts, n_pkts_cpl);
    2513                 :          0 :         vq->stats.inflight_completed += n_pkts_cpl;
    2514                 :            : 
    2515                 :          0 : out_access_unlock:
    2516                 :            :         rte_rwlock_read_unlock(&vq->access_lock);
    2517                 :            : 
    2518                 :          0 :         return n_pkts_cpl;
    2519                 :            : }
    2520                 :            : 
    2521                 :            : static __rte_always_inline uint32_t
    2522                 :            : virtio_dev_rx_async_submit(struct virtio_net *dev, struct vhost_virtqueue *vq,
    2523                 :            :         struct rte_mbuf **pkts, uint32_t count, int16_t dma_id, uint16_t vchan_id)
    2524                 :            : {
    2525                 :            :         uint32_t nb_tx = 0;
    2526                 :            : 
    2527                 :            :         VHOST_DATA_LOG(dev->ifname, DEBUG, "%s", __func__);
    2528                 :            : 
    2529         [ #  # ]:          0 :         if (unlikely(!dma_copy_track[dma_id].vchans ||
    2530                 :            :                                 !dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr)) {
    2531                 :          0 :                 VHOST_DATA_LOG(dev->ifname, ERR,
    2532                 :            :                         "%s: invalid channel %d:%u.",
    2533                 :            :                          __func__, dma_id, vchan_id);
    2534                 :          0 :                 return 0;
    2535                 :            :         }
    2536                 :            : 
    2537                 :          0 :         rte_rwlock_write_lock(&vq->access_lock);
    2538                 :            : 
    2539   [ #  #  #  # ]:          0 :         if (unlikely(!vq->enabled || !vq->async))
    2540                 :          0 :                 goto out_access_unlock;
    2541                 :            : 
    2542                 :            :         vhost_user_iotlb_rd_lock(vq);
    2543                 :            : 
    2544         [ #  # ]:          0 :         if (unlikely(!vq->access_ok)) {
    2545                 :            :                 vhost_user_iotlb_rd_unlock(vq);
    2546                 :            :                 rte_rwlock_write_unlock(&vq->access_lock);
    2547                 :            : 
    2548                 :          0 :                 virtio_dev_vring_translate(dev, vq);
    2549                 :          0 :                 goto out_no_unlock;
    2550                 :            :         }
    2551                 :            : 
    2552                 :          0 :         count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
    2553         [ #  # ]:          0 :         if (count == 0)
    2554                 :          0 :                 goto out;
    2555                 :            : 
    2556         [ #  # ]:          0 :         if (vq_is_packed(dev))
    2557                 :          0 :                 nb_tx = virtio_dev_rx_async_submit_packed(dev, vq, pkts, count,
    2558                 :            :                         dma_id, vchan_id);
    2559                 :            :         else
    2560                 :          0 :                 nb_tx = virtio_dev_rx_async_submit_split(dev, vq, pkts, count,
    2561                 :            :                         dma_id, vchan_id);
    2562                 :            : 
    2563                 :          0 :         vq->stats.inflight_submitted += nb_tx;
    2564                 :            : 
    2565                 :          0 : out:
    2566                 :            :         vhost_user_iotlb_rd_unlock(vq);
    2567                 :            : 
    2568                 :          0 : out_access_unlock:
    2569                 :            :         rte_rwlock_write_unlock(&vq->access_lock);
    2570                 :            : 
    2571                 :            : out_no_unlock:
    2572                 :            :         return nb_tx;
    2573                 :            : }
    2574                 :            : 
    2575                 :            : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_vhost_submit_enqueue_burst, 20.08)
    2576                 :            : uint16_t
    2577         [ #  # ]:          0 : rte_vhost_submit_enqueue_burst(int vid, uint16_t queue_id,
    2578                 :            :                 struct rte_mbuf **pkts, uint16_t count, int16_t dma_id,
    2579                 :            :                 uint16_t vchan_id)
    2580                 :            : {
    2581                 :            :         struct virtio_net *dev = get_device(vid);
    2582                 :            : 
    2583         [ #  # ]:          0 :         if (!dev)
    2584                 :            :                 return 0;
    2585                 :            : 
    2586         [ #  # ]:          0 :         if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
    2587                 :          0 :                 VHOST_DATA_LOG(dev->ifname, ERR,
    2588                 :            :                         "%s: built-in vhost net backend is disabled.",
    2589                 :            :                         __func__);
    2590                 :          0 :                 return 0;
    2591                 :            :         }
    2592                 :            : 
    2593   [ #  #  #  # ]:          0 :         if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
    2594                 :          0 :                 VHOST_DATA_LOG(dev->ifname, ERR,
    2595                 :            :                         "%s: invalid virtqueue idx %d.",
    2596                 :            :                         __func__, queue_id);
    2597                 :          0 :                 return 0;
    2598                 :            :         }
    2599                 :            : 
    2600         [ #  # ]:          0 :         return virtio_dev_rx_async_submit(dev, dev->virtqueue[queue_id], pkts, count,
    2601                 :            :                 dma_id, vchan_id);
    2602                 :            : }
    2603                 :            : 
    2604                 :            : static inline bool
    2605                 :            : virtio_net_with_host_offload(struct virtio_net *dev)
    2606                 :            : {
    2607   [ #  #  #  #  :          0 :         if (dev->features &
             #  #  #  # ]
    2608                 :            :                         ((1ULL << VIRTIO_NET_F_CSUM) |
    2609                 :            :                          (1ULL << VIRTIO_NET_F_HOST_ECN) |
    2610                 :            :                          (1ULL << VIRTIO_NET_F_HOST_TSO4) |
    2611                 :            :                          (1ULL << VIRTIO_NET_F_HOST_TSO6) |
    2612                 :            :                          (1ULL << VIRTIO_NET_F_HOST_UFO)))
    2613                 :            :                 return true;
    2614                 :            : 
    2615                 :            :         return false;
    2616                 :            : }
    2617                 :            : 
    2618                 :            : static int
    2619                 :          0 : parse_headers(struct rte_mbuf *m, uint8_t *l4_proto)
    2620                 :            : {
    2621                 :            :         struct rte_ipv4_hdr *ipv4_hdr;
    2622                 :            :         struct rte_ipv6_hdr *ipv6_hdr;
    2623                 :            :         struct rte_ether_hdr *eth_hdr;
    2624                 :            :         uint16_t ethertype;
    2625                 :          0 :         uint16_t data_len = rte_pktmbuf_data_len(m);
    2626                 :            : 
    2627         [ #  # ]:          0 :         if (data_len < sizeof(struct rte_ether_hdr))
    2628                 :            :                 return -EINVAL;
    2629                 :            : 
    2630                 :          0 :         eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
    2631                 :            : 
    2632                 :          0 :         m->l2_len = sizeof(struct rte_ether_hdr);
    2633         [ #  # ]:          0 :         ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
    2634                 :            : 
    2635         [ #  # ]:          0 :         if (ethertype == RTE_ETHER_TYPE_VLAN) {
    2636         [ #  # ]:          0 :                 if (data_len < sizeof(struct rte_ether_hdr) +
    2637                 :            :                                 sizeof(struct rte_vlan_hdr))
    2638                 :          0 :                         goto error;
    2639                 :            : 
    2640                 :            :                 struct rte_vlan_hdr *vlan_hdr =
    2641                 :            :                         (struct rte_vlan_hdr *)(eth_hdr + 1);
    2642                 :            : 
    2643                 :          0 :                 m->l2_len += sizeof(struct rte_vlan_hdr);
    2644         [ #  # ]:          0 :                 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
    2645                 :            :         }
    2646                 :            : 
    2647      [ #  #  # ]:          0 :         switch (ethertype) {
    2648                 :          0 :         case RTE_ETHER_TYPE_IPV4:
    2649         [ #  # ]:          0 :                 if (data_len < m->l2_len + sizeof(struct rte_ipv4_hdr))
    2650                 :          0 :                         goto error;
    2651         [ #  # ]:          0 :                 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *,
    2652                 :            :                                 m->l2_len);
    2653                 :          0 :                 m->l3_len = rte_ipv4_hdr_len(ipv4_hdr);
    2654         [ #  # ]:          0 :                 if (data_len < m->l2_len + m->l3_len)
    2655                 :          0 :                         goto error;
    2656                 :          0 :                 m->ol_flags |= RTE_MBUF_F_TX_IPV4;
    2657                 :          0 :                 *l4_proto = ipv4_hdr->next_proto_id;
    2658                 :          0 :                 break;
    2659                 :          0 :         case RTE_ETHER_TYPE_IPV6:
    2660         [ #  # ]:          0 :                 if (data_len < m->l2_len + sizeof(struct rte_ipv6_hdr))
    2661                 :          0 :                         goto error;
    2662                 :          0 :                 ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv6_hdr *,
    2663                 :            :                                 m->l2_len);
    2664                 :          0 :                 m->l3_len = sizeof(struct rte_ipv6_hdr);
    2665                 :          0 :                 m->ol_flags |= RTE_MBUF_F_TX_IPV6;
    2666                 :          0 :                 *l4_proto = ipv6_hdr->proto;
    2667                 :          0 :                 break;
    2668                 :          0 :         default:
    2669                 :            :                 /* a valid L3 header is needed for further L4 parsing */
    2670                 :          0 :                 goto error;
    2671                 :            :         }
    2672                 :            : 
    2673                 :            :         /* both CSUM and GSO need a valid L4 header */
    2674   [ #  #  #  # ]:          0 :         switch (*l4_proto) {
    2675                 :          0 :         case IPPROTO_TCP:
    2676         [ #  # ]:          0 :                 if (data_len < m->l2_len + m->l3_len +
    2677                 :            :                                 sizeof(struct rte_tcp_hdr))
    2678                 :          0 :                         goto error;
    2679                 :            :                 break;
    2680                 :          0 :         case IPPROTO_UDP:
    2681         [ #  # ]:          0 :                 if (data_len < m->l2_len + m->l3_len +
    2682                 :            :                                 sizeof(struct rte_udp_hdr))
    2683                 :          0 :                         goto error;
    2684                 :            :                 break;
    2685                 :          0 :         case IPPROTO_SCTP:
    2686         [ #  # ]:          0 :                 if (data_len < m->l2_len + m->l3_len +
    2687                 :            :                                 sizeof(struct rte_sctp_hdr))
    2688                 :          0 :                         goto error;
    2689                 :            :                 break;
    2690                 :          0 :         default:
    2691                 :          0 :                 goto error;
    2692                 :            :         }
    2693                 :            : 
    2694                 :            :         return 0;
    2695                 :            : 
    2696                 :          0 : error:
    2697                 :          0 :         m->l2_len = 0;
    2698                 :          0 :         m->l3_len = 0;
    2699                 :          0 :         m->ol_flags = 0;
    2700                 :          0 :         return -EINVAL;
    2701                 :            : }
    2702                 :            : 
    2703                 :            : static __rte_always_inline void
    2704                 :            : vhost_dequeue_offload_legacy(struct virtio_net *dev, struct virtio_net_hdr *hdr,
    2705                 :            :                 struct rte_mbuf *m)
    2706                 :            : {
    2707                 :          0 :         uint8_t l4_proto = 0;
    2708                 :            :         struct rte_tcp_hdr *tcp_hdr = NULL;
    2709                 :            :         uint16_t tcp_len;
    2710                 :          0 :         uint16_t data_len = rte_pktmbuf_data_len(m);
    2711                 :            : 
    2712   [ #  #  #  #  :          0 :         if (parse_headers(m, &l4_proto) < 0)
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    2713                 :          0 :                 return;
    2714                 :            : 
    2715   [ #  #  #  #  :          0 :         if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    2716   [ #  #  #  #  :          0 :                 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    2717   [ #  #  #  #  :          0 :                         switch (hdr->csum_offset) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2718                 :          0 :                         case (offsetof(struct rte_tcp_hdr, cksum)):
    2719   [ #  #  #  #  :          0 :                                 if (l4_proto != IPPROTO_TCP)
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    2720                 :          0 :                                         goto error;
    2721                 :          0 :                                 m->ol_flags |= RTE_MBUF_F_TX_TCP_CKSUM;
    2722                 :          0 :                                 break;
    2723                 :          0 :                         case (offsetof(struct rte_udp_hdr, dgram_cksum)):
    2724   [ #  #  #  #  :          0 :                                 if (l4_proto != IPPROTO_UDP)
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    2725                 :          0 :                                         goto error;
    2726                 :          0 :                                 m->ol_flags |= RTE_MBUF_F_TX_UDP_CKSUM;
    2727                 :          0 :                                 break;
    2728                 :          0 :                         case (offsetof(struct rte_sctp_hdr, cksum)):
    2729   [ #  #  #  #  :          0 :                                 if (l4_proto != IPPROTO_SCTP)
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    2730                 :          0 :                                         goto error;
    2731                 :          0 :                                 m->ol_flags |= RTE_MBUF_F_TX_SCTP_CKSUM;
    2732                 :          0 :                                 break;
    2733                 :          0 :                         default:
    2734                 :          0 :                                 goto error;
    2735                 :            :                         }
    2736                 :            :                 } else {
    2737                 :          0 :                         goto error;
    2738                 :            :                 }
    2739                 :            :         }
    2740                 :            : 
    2741   [ #  #  #  #  :          0 :         if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    2742   [ #  #  #  #  :          0 :                 if (hdr->gso_size == 0)
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    2743                 :          0 :                         goto error;
    2744                 :            : 
    2745   [ #  #  #  #  :          0 :                 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    2746                 :          0 :                 case VIRTIO_NET_HDR_GSO_TCPV4:
    2747                 :            :                 case VIRTIO_NET_HDR_GSO_TCPV6:
    2748   [ #  #  #  #  :          0 :                         if (l4_proto != IPPROTO_TCP)
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    2749                 :          0 :                                 goto error;
    2750                 :          0 :                         tcp_hdr = rte_pktmbuf_mtod_offset(m,
    2751                 :            :                                         struct rte_tcp_hdr *,
    2752                 :            :                                         m->l2_len + m->l3_len);
    2753                 :          0 :                         tcp_len = (tcp_hdr->data_off & 0xf0) >> 2;
    2754   [ #  #  #  #  :          0 :                         if (data_len < m->l2_len + m->l3_len + tcp_len)
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    2755                 :          0 :                                 goto error;
    2756                 :          0 :                         m->ol_flags |= RTE_MBUF_F_TX_TCP_SEG;
    2757                 :          0 :                         m->tso_segsz = hdr->gso_size;
    2758                 :          0 :                         m->l4_len = tcp_len;
    2759                 :          0 :                         break;
    2760                 :          0 :                 case VIRTIO_NET_HDR_GSO_UDP:
    2761   [ #  #  #  #  :          0 :                         if (l4_proto != IPPROTO_UDP)
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    2762                 :          0 :                                 goto error;
    2763                 :          0 :                         m->ol_flags |= RTE_MBUF_F_TX_UDP_SEG;
    2764                 :          0 :                         m->tso_segsz = hdr->gso_size;
    2765                 :          0 :                         m->l4_len = sizeof(struct rte_udp_hdr);
    2766                 :          0 :                         break;
    2767                 :          0 :                 default:
    2768                 :          0 :                         VHOST_DATA_LOG(dev->ifname, WARNING,
    2769                 :            :                                 "unsupported gso type %u.",
    2770                 :            :                                 hdr->gso_type);
    2771                 :          0 :                         goto error;
    2772                 :            :                 }
    2773                 :            :         }
    2774                 :            :         return;
    2775                 :            : 
    2776                 :          0 : error:
    2777                 :          0 :         m->l2_len = 0;
    2778                 :          0 :         m->l3_len = 0;
    2779                 :          0 :         m->ol_flags = 0;
    2780                 :            : }
    2781                 :            : 
    2782                 :            : static __rte_always_inline void
    2783                 :            : vhost_dequeue_offload(struct virtio_net *dev, struct virtio_net_hdr *hdr,
    2784                 :            :                 struct rte_mbuf *m, bool legacy_ol_flags)
    2785                 :            : {
    2786                 :            :         struct rte_net_hdr_lens hdr_lens;
    2787                 :            :         int l4_supported = 0;
    2788                 :            :         uint32_t ptype;
    2789                 :            : 
    2790   [ #  #  #  #  :          0 :         if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
    2791                 :          0 :                 return;
    2792                 :            : 
    2793   [ #  #  #  # ]:          0 :         if (legacy_ol_flags) {
    2794                 :            :                 vhost_dequeue_offload_legacy(dev, hdr, m);
    2795                 :            :                 return;
    2796                 :            :         }
    2797                 :            : 
    2798                 :            :         m->ol_flags |= RTE_MBUF_F_RX_IP_CKSUM_UNKNOWN;
    2799                 :            : 
    2800                 :          0 :         ptype = rte_net_get_ptype(m, &hdr_lens, RTE_PTYPE_ALL_MASK);
    2801                 :          0 :         m->packet_type = ptype;
    2802   [ #  #  #  #  :          0 :         if ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP ||
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    2803   [ #  #  #  #  :          0 :             (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP ||
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    2804                 :            :             (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_SCTP)
    2805                 :            :                 l4_supported = 1;
    2806                 :            : 
    2807                 :            :         /* According to Virtio 1.1 spec, the device only needs to look at
    2808                 :            :          * VIRTIO_NET_HDR_F_NEEDS_CSUM in the packet transmission path.
    2809                 :            :          * This differs from the processing incoming packets path where the
    2810                 :            :          * driver could rely on VIRTIO_NET_HDR_F_DATA_VALID flag set by the
    2811                 :            :          * device.
    2812                 :            :          *
    2813                 :            :          * 5.1.6.2.1 Driver Requirements: Packet Transmission
    2814                 :            :          * The driver MUST NOT set the VIRTIO_NET_HDR_F_DATA_VALID and
    2815                 :            :          * VIRTIO_NET_HDR_F_RSC_INFO bits in flags.
    2816                 :            :          *
    2817                 :            :          * 5.1.6.2.2 Device Requirements: Packet Transmission
    2818                 :            :          * The device MUST ignore flag bits that it does not recognize.
    2819                 :            :          */
    2820   [ #  #  #  #  :          0 :         if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    2821                 :            :                 uint32_t hdrlen;
    2822                 :            : 
    2823                 :          0 :                 hdrlen = hdr_lens.l2_len + hdr_lens.l3_len + hdr_lens.l4_len;
    2824   [ #  #  #  #  :          0 :                 if (hdr->csum_start <= hdrlen && l4_supported != 0) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    2825                 :          0 :                         m->ol_flags |= RTE_MBUF_F_RX_L4_CKSUM_NONE;
    2826                 :            :                 } else {
    2827                 :            :                         /* Unknown proto or tunnel, do sw cksum. We can assume
    2828                 :            :                          * the cksum field is in the first segment since the
    2829                 :            :                          * buffers we provided to the host are large enough.
    2830                 :            :                          * In case of SCTP, this will be wrong since it's a CRC
    2831                 :            :                          * but there's nothing we can do.
    2832                 :            :                          */
    2833                 :          0 :                         uint16_t csum = 0, off;
    2834                 :            : 
    2835   [ #  #  #  #  :          0 :                         if (hdr->csum_start >= rte_pktmbuf_pkt_len(m))
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    2836                 :          0 :                                 return;
    2837                 :            : 
    2838   [ #  #  #  #  :          0 :                         if (rte_raw_cksum_mbuf(m, hdr->csum_start,
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    2839                 :            :                                         rte_pktmbuf_pkt_len(m) - hdr->csum_start, &csum) < 0)
    2840                 :            :                                 return;
    2841   [ #  #  #  #  :          0 :                         if (likely(csum != 0xffff))
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    2842                 :          0 :                                 csum = ~csum;
    2843                 :          0 :                         off = hdr->csum_offset + hdr->csum_start;
    2844   [ #  #  #  #  :          0 :                         if (rte_pktmbuf_data_len(m) >= off + 1)
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    2845                 :          0 :                                 *rte_pktmbuf_mtod_offset(m, uint16_t *, off) = csum;
    2846                 :            :                 }
    2847                 :            :         }
    2848                 :            : 
    2849   [ #  #  #  #  :          0 :         if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    2850   [ #  #  #  #  :          0 :                 if (hdr->gso_size == 0)
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    2851                 :            :                         return;
    2852                 :            : 
    2853   [ #  #  #  #  :          0 :                 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    2854                 :          0 :                 case VIRTIO_NET_HDR_GSO_TCPV4:
    2855                 :            :                 case VIRTIO_NET_HDR_GSO_TCPV6:
    2856   [ #  #  #  #  :          0 :                         if ((ptype & RTE_PTYPE_L4_MASK) != RTE_PTYPE_L4_TCP)
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    2857                 :            :                                 break;
    2858                 :          0 :                         m->ol_flags |= RTE_MBUF_F_RX_LRO | RTE_MBUF_F_RX_L4_CKSUM_NONE;
    2859                 :          0 :                         m->tso_segsz = hdr->gso_size;
    2860                 :          0 :                         break;
    2861                 :          0 :                 case VIRTIO_NET_HDR_GSO_UDP:
    2862   [ #  #  #  #  :          0 :                         if ((ptype & RTE_PTYPE_L4_MASK) != RTE_PTYPE_L4_UDP)
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    2863                 :            :                                 break;
    2864                 :          0 :                         m->ol_flags |= RTE_MBUF_F_RX_LRO | RTE_MBUF_F_RX_L4_CKSUM_NONE;
    2865                 :          0 :                         m->tso_segsz = hdr->gso_size;
    2866                 :          0 :                         break;
    2867                 :            :                 default:
    2868                 :            :                         break;
    2869                 :            :                 }
    2870                 :            :         }
    2871                 :            : }
    2872                 :            : 
    2873                 :            : static __rte_noinline void
    2874                 :          0 : copy_vnet_hdr_from_desc(struct virtio_net_hdr *hdr,
    2875                 :            :                 struct buf_vector *buf_vec)
    2876                 :            : {
    2877                 :            :         uint64_t len;
    2878                 :            :         uint64_t remain = sizeof(struct virtio_net_hdr);
    2879                 :            :         uint64_t src;
    2880                 :          0 :         uint64_t dst = (uint64_t)(uintptr_t)hdr;
    2881                 :            : 
    2882         [ #  # ]:          0 :         while (remain) {
    2883                 :          0 :                 len = RTE_MIN(remain, buf_vec->buf_len);
    2884                 :          0 :                 src = buf_vec->buf_addr;
    2885         [ #  # ]:          0 :                 rte_memcpy((void *)(uintptr_t)dst,
    2886                 :            :                                 (void *)(uintptr_t)src, len);
    2887                 :            : 
    2888                 :          0 :                 remain -= len;
    2889                 :          0 :                 dst += len;
    2890                 :          0 :                 buf_vec++;
    2891                 :            :         }
    2892                 :          0 : }
    2893                 :            : 
    2894                 :            : static __rte_always_inline int
    2895                 :            : desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
    2896                 :            :                   struct buf_vector *buf_vec, uint16_t nr_vec,
    2897                 :            :                   struct rte_mbuf *m, struct rte_mempool *mbuf_pool,
    2898                 :            :                   bool legacy_ol_flags, uint16_t slot_idx, bool is_async)
    2899                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    2900                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    2901                 :            : {
    2902                 :            :         uint32_t buf_avail, buf_offset, buf_len;
    2903                 :            :         uint64_t buf_addr, buf_iova;
    2904                 :            :         uint32_t mbuf_avail, mbuf_offset;
    2905   [ #  #  #  # ]:          0 :         uint32_t hdr_remain = dev->vhost_hlen;
    2906                 :            :         uint32_t cpy_len;
    2907                 :            :         struct rte_mbuf *cur = m, *prev = m;
    2908                 :            :         struct virtio_net_hdr tmp_hdr;
    2909                 :            :         struct virtio_net_hdr *hdr = NULL;
    2910                 :            :         uint16_t vec_idx;
    2911   [ #  #  #  # ]:          0 :         struct vhost_async *async = vq->async;
    2912                 :            :         struct async_inflight_info *pkts_info;
    2913                 :            : 
    2914                 :            :         /*
    2915                 :            :          * The caller has checked the descriptors chain is larger than the
    2916                 :            :          * header size.
    2917                 :            :          */
    2918                 :            : 
    2919                 :            :         if (virtio_net_with_host_offload(dev)) {
    2920   [ #  #  #  #  :          0 :                 if (unlikely(buf_vec[0].buf_len < sizeof(struct virtio_net_hdr))) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    2921                 :            :                         /*
    2922                 :            :                          * No luck, the virtio-net header doesn't fit
    2923                 :            :                          * in a contiguous virtual area.
    2924                 :            :                          */
    2925                 :          0 :                         copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
    2926                 :            :                         hdr = &tmp_hdr;
    2927                 :            :                 } else {
    2928                 :          0 :                         hdr = (struct virtio_net_hdr *)((uintptr_t)buf_vec[0].buf_addr);
    2929                 :            :                 }
    2930                 :            :         }
    2931                 :            : 
    2932   [ #  #  #  #  :          0 :         for (vec_idx = 0; vec_idx < nr_vec; vec_idx++) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    2933   [ #  #  #  #  :          0 :                 if (buf_vec[vec_idx].buf_len > hdr_remain)
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    2934                 :            :                         break;
    2935                 :            : 
    2936                 :          0 :                 hdr_remain -= buf_vec[vec_idx].buf_len;
    2937                 :            :         }
    2938                 :            : 
    2939                 :          0 :         buf_addr = buf_vec[vec_idx].buf_addr;
    2940                 :          0 :         buf_iova = buf_vec[vec_idx].buf_iova;
    2941                 :          0 :         buf_len = buf_vec[vec_idx].buf_len;
    2942                 :            :         buf_offset = hdr_remain;
    2943                 :          0 :         buf_avail = buf_vec[vec_idx].buf_len - hdr_remain;
    2944                 :            : 
    2945                 :            :         PRINT_PACKET(dev,
    2946                 :            :                         (uintptr_t)(buf_addr + buf_offset),
    2947                 :            :                         (uint32_t)buf_avail, 0);
    2948                 :            : 
    2949                 :            :         mbuf_offset = 0;
    2950                 :          0 :         mbuf_avail  = m->buf_len - RTE_PKTMBUF_HEADROOM;
    2951                 :            : 
    2952                 :            :         if (is_async) {
    2953   [ #  #  #  #  :          0 :                 pkts_info = async->pkts_info;
             #  #  #  # ]
    2954                 :            :                 if (async_iter_initialize(dev, async))
    2955                 :          0 :                         return -1;
    2956                 :            :         }
    2957                 :            : 
    2958                 :            :         while (1) {
    2959                 :          0 :                 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
    2960                 :            : 
    2961                 :            :                 if (is_async) {
    2962   [ #  #  #  #  :          0 :                         if (async_fill_seg(dev, vq, cur, mbuf_offset,
             #  #  #  # ]
    2963                 :            :                                            buf_iova + buf_offset, cpy_len, false) < 0)
    2964                 :          0 :                                 goto error;
    2965   [ #  #  #  #  :          0 :                 } else if (likely(hdr && cur == m)) {
             #  #  #  # ]
    2966                 :          0 :                         rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *, mbuf_offset),
    2967   [ #  #  #  #  :          0 :                                 (void *)((uintptr_t)(buf_addr + buf_offset)),
             #  #  #  # ]
    2968                 :            :                                 cpy_len);
    2969                 :            :                 } else {
    2970   [ #  #  #  #  :          0 :                         sync_fill_seg(dev, vq, cur, mbuf_offset,
             #  #  #  # ]
    2971                 :            :                                       buf_addr + buf_offset,
    2972                 :            :                                       buf_iova + buf_offset, cpy_len, false);
    2973                 :            :                 }
    2974                 :            : 
    2975                 :          0 :                 mbuf_avail  -= cpy_len;
    2976                 :          0 :                 mbuf_offset += cpy_len;
    2977                 :          0 :                 buf_avail -= cpy_len;
    2978                 :          0 :                 buf_offset += cpy_len;
    2979                 :            : 
    2980                 :            :                 /* This buf reaches to its end, get the next one */
    2981   [ #  #  #  #  :          0 :                 if (buf_avail == 0) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    2982   [ #  #  #  #  :          0 :                         if (++vec_idx >= nr_vec)
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    2983                 :            :                                 break;
    2984                 :            : 
    2985                 :          0 :                         buf_addr = buf_vec[vec_idx].buf_addr;
    2986                 :          0 :                         buf_iova = buf_vec[vec_idx].buf_iova;
    2987                 :          0 :                         buf_len = buf_vec[vec_idx].buf_len;
    2988                 :            : 
    2989                 :            :                         buf_offset = 0;
    2990                 :            :                         buf_avail  = buf_len;
    2991                 :            : 
    2992                 :            :                         PRINT_PACKET(dev, (uintptr_t)buf_addr,
    2993                 :            :                                         (uint32_t)buf_avail, 0);
    2994                 :            :                 }
    2995                 :            : 
    2996                 :            :                 /*
    2997                 :            :                  * This mbuf reaches to its end, get a new one
    2998                 :            :                  * to hold more data.
    2999                 :            :                  */
    3000   [ #  #  #  #  :          0 :                 if (mbuf_avail == 0) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    3001                 :          0 :                         cur = rte_pktmbuf_alloc(mbuf_pool);
    3002   [ #  #  #  #  :          0 :                         if (unlikely(cur == NULL)) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    3003                 :          0 :                                 vq->stats.mbuf_alloc_failed++;
    3004                 :          0 :                                 VHOST_DATA_LOG(dev->ifname, ERR,
    3005                 :            :                                         "failed to allocate memory for mbuf.");
    3006                 :          0 :                                 goto error;
    3007                 :            :                         }
    3008                 :            : 
    3009                 :          0 :                         prev->next = cur;
    3010                 :          0 :                         prev->data_len = mbuf_offset;
    3011                 :          0 :                         m->nb_segs += 1;
    3012                 :          0 :                         m->pkt_len += mbuf_offset;
    3013                 :            :                         prev = cur;
    3014                 :            : 
    3015                 :            :                         mbuf_offset = 0;
    3016                 :          0 :                         mbuf_avail  = cur->buf_len - RTE_PKTMBUF_HEADROOM;
    3017                 :            :                 }
    3018                 :            :         }
    3019                 :            : 
    3020                 :          0 :         prev->data_len = mbuf_offset;
    3021                 :          0 :         m->pkt_len    += mbuf_offset;
    3022                 :            : 
    3023                 :            :         if (is_async) {
    3024                 :            :                 async_iter_finalize(async);
    3025   [ #  #  #  #  :          0 :                 if (hdr)
             #  #  #  # ]
    3026                 :          0 :                         pkts_info[slot_idx].nethdr = *hdr;
    3027   [ #  #  #  #  :          0 :         } else if (hdr) {
             #  #  #  # ]
    3028                 :            :                 vhost_dequeue_offload(dev, hdr, m, legacy_ol_flags);
    3029                 :            :         }
    3030                 :            : 
    3031                 :            :         return 0;
    3032                 :          0 : error:
    3033                 :            :         if (is_async)
    3034                 :            :                 async_iter_cancel(async);
    3035                 :            : 
    3036                 :            :         return -1;
    3037                 :            : }
    3038                 :            : 
    3039                 :            : static void
    3040                 :          0 : virtio_dev_extbuf_free(void *addr __rte_unused, void *opaque)
    3041                 :            : {
    3042                 :          0 :         rte_free(opaque);
    3043                 :          0 : }
    3044                 :            : 
    3045                 :            : static int
    3046                 :          0 : virtio_dev_extbuf_alloc(struct virtio_net *dev, struct rte_mbuf *pkt, uint32_t size)
    3047                 :            : {
    3048                 :            :         struct rte_mbuf_ext_shared_info *shinfo = NULL;
    3049                 :            :         uint32_t total_len = RTE_PKTMBUF_HEADROOM + size;
    3050                 :            :         uint16_t buf_len;
    3051                 :            :         rte_iova_t iova;
    3052                 :            :         void *buf;
    3053                 :            : 
    3054                 :            :         total_len += sizeof(*shinfo) + sizeof(uintptr_t);
    3055                 :          0 :         total_len = RTE_ALIGN_CEIL(total_len, sizeof(uintptr_t));
    3056                 :            : 
    3057         [ #  # ]:          0 :         if (unlikely(total_len > UINT16_MAX))
    3058                 :            :                 return -ENOSPC;
    3059                 :            : 
    3060                 :          0 :         buf_len = total_len;
    3061                 :          0 :         buf = rte_malloc(NULL, buf_len, RTE_CACHE_LINE_SIZE);
    3062         [ #  # ]:          0 :         if (unlikely(buf == NULL))
    3063                 :            :                 return -ENOMEM;
    3064                 :            : 
    3065                 :            :         /* Initialize shinfo */
    3066                 :            :         shinfo = rte_pktmbuf_ext_shinfo_init_helper(buf, &buf_len,
    3067                 :            :                                                 virtio_dev_extbuf_free, buf);
    3068         [ #  # ]:          0 :         if (unlikely(shinfo == NULL)) {
    3069                 :          0 :                 rte_free(buf);
    3070                 :          0 :                 VHOST_DATA_LOG(dev->ifname, ERR, "failed to init shinfo");
    3071                 :          0 :                 return -1;
    3072                 :            :         }
    3073                 :            : 
    3074                 :          0 :         iova = rte_malloc_virt2iova(buf);
    3075                 :            :         rte_pktmbuf_attach_extbuf(pkt, buf, iova, buf_len, shinfo);
    3076                 :            :         rte_pktmbuf_reset_headroom(pkt);
    3077                 :            : 
    3078                 :          0 :         return 0;
    3079                 :            : }
    3080                 :            : 
    3081                 :            : /*
    3082                 :            :  * Prepare a host supported pktmbuf.
    3083                 :            :  */
    3084                 :            : static __rte_always_inline int
    3085                 :            : virtio_dev_pktmbuf_prep(struct virtio_net *dev, struct rte_mbuf *pkt,
    3086                 :            :                          uint32_t data_len)
    3087                 :            : {
    3088   [ #  #  #  #  :          0 :         if (rte_pktmbuf_tailroom(pkt) >= data_len)
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    3089                 :            :                 return 0;
    3090                 :            : 
    3091                 :            :         /* attach an external buffer if supported */
    3092   [ #  #  #  #  :          0 :         if (dev->extbuf && !virtio_dev_extbuf_alloc(dev, pkt, data_len))
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
    3093                 :            :                 return 0;
    3094                 :            : 
    3095                 :            :         /* check if chained buffers are allowed */
    3096   [ #  #  #  #  :          0 :         if (!dev->linearbuf)
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    3097                 :          0 :                 return 0;
    3098                 :            : 
    3099                 :            :         return -1;
    3100                 :            : }
    3101                 :            : 
    3102                 :            : __rte_always_inline
    3103                 :            : static uint16_t
    3104                 :            : virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
    3105                 :            :         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count,
    3106                 :            :         bool legacy_ol_flags)
    3107                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    3108                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    3109                 :            : {
    3110                 :            :         uint16_t i;
    3111                 :            :         uint16_t avail_entries;
    3112                 :            :         static bool allocerr_warned;
    3113                 :            : 
    3114                 :            :         /*
    3115                 :            :          * The ordering between avail index and
    3116                 :            :          * desc reads needs to be enforced.
    3117                 :            :          */
    3118                 :          0 :         avail_entries = rte_atomic_load_explicit((unsigned short __rte_atomic *)&vq->avail->idx,
    3119                 :          0 :                 rte_memory_order_acquire) - vq->last_avail_idx;
    3120                 :          0 :         if (avail_entries == 0)
    3121                 :            :                 return 0;
    3122                 :            : 
    3123                 :          0 :         rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
    3124                 :            : 
    3125                 :            :         VHOST_DATA_LOG(dev->ifname, DEBUG, "%s", __func__);
    3126                 :            : 
    3127                 :          0 :         count = RTE_MIN(count, MAX_PKT_BURST);
    3128                 :          0 :         count = RTE_MIN(count, avail_entries);
    3129                 :            :         VHOST_DATA_LOG(dev->ifname, DEBUG, "about to dequeue %u buffers", count);
    3130                 :            : 
    3131   [ #  #  #  # ]:          0 :         if (rte_pktmbuf_alloc_bulk(mbuf_pool, pkts, count)) {
    3132                 :          0 :                 vq->stats.mbuf_alloc_failed += count;
    3133                 :          0 :                 return 0;
    3134                 :            :         }
    3135                 :            : 
    3136   [ #  #  #  # ]:          0 :         for (i = 0; i < count; i++) {
    3137                 :            :                 struct buf_vector buf_vec[BUF_VECTOR_MAX];
    3138                 :            :                 uint16_t head_idx;
    3139                 :            :                 uint32_t buf_len;
    3140                 :            :                 uint16_t nr_vec = 0;
    3141                 :            :                 int err;
    3142                 :            : 
    3143   [ #  #  #  #  :          0 :                 if (unlikely(fill_vec_buf_split(dev, vq,
             #  #  #  # ]
    3144                 :            :                                                 vq->last_avail_idx + i,
    3145                 :            :                                                 &nr_vec, buf_vec,
    3146                 :            :                                                 &head_idx, &buf_len,
    3147                 :            :                                                 VHOST_ACCESS_RO) < 0))
    3148                 :            :                         break;
    3149                 :            : 
    3150                 :            :                 update_shadow_used_ring_split(vq, head_idx, 0);
    3151                 :            : 
    3152   [ #  #  #  # ]:          0 :                 if (unlikely(buf_len <= dev->vhost_hlen))
    3153                 :            :                         break;
    3154                 :            : 
    3155                 :          0 :                 buf_len -= dev->vhost_hlen;
    3156                 :            : 
    3157   [ #  #  #  # ]:          0 :                 err = virtio_dev_pktmbuf_prep(dev, pkts[i], buf_len);
    3158   [ #  #  #  # ]:          0 :                 if (unlikely(err)) {
    3159                 :            :                         /*
    3160                 :            :                          * mbuf allocation fails for jumbo packets when external
    3161                 :            :                          * buffer allocation is not allowed and linear buffer
    3162                 :            :                          * is required. Drop this packet.
    3163                 :            :                          */
    3164   [ #  #  #  # ]:          0 :                         if (!allocerr_warned) {
    3165                 :          0 :                                 VHOST_DATA_LOG(dev->ifname, ERR,
    3166                 :            :                                         "failed mbuf alloc of size %d from %s.",
    3167                 :            :                                         buf_len, mbuf_pool->name);
    3168                 :          0 :                                 allocerr_warned = true;
    3169                 :            :                         }
    3170                 :            :                         break;
    3171                 :            :                 }
    3172                 :            : 
    3173   [ #  #  #  # ]:          0 :                 err = desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
    3174                 :            :                                    mbuf_pool, legacy_ol_flags, 0, false);
    3175   [ #  #  #  # ]:          0 :                 if (unlikely(err)) {
    3176   [ #  #  #  # ]:          0 :                         if (!allocerr_warned) {
    3177                 :          0 :                                 VHOST_DATA_LOG(dev->ifname, ERR, "failed to copy desc to mbuf.");
    3178                 :          0 :                                 allocerr_warned = true;
    3179                 :            :                         }
    3180                 :            :                         break;
    3181                 :            :                 }
    3182                 :            :         }
    3183                 :            : 
    3184   [ #  #  #  # ]:          0 :         if (unlikely(count != i))
    3185                 :          0 :                 rte_pktmbuf_free_bulk(&pkts[i], count - i);
    3186                 :            : 
    3187   [ #  #  #  # ]:          0 :         if (likely(vq->shadow_used_idx)) {
    3188   [ #  #  #  # ]:          0 :                 vq->last_avail_idx += vq->shadow_used_idx;
    3189                 :            :                 vhost_virtqueue_reconnect_log_split(vq);
    3190                 :          0 :                 do_data_copy_dequeue(vq);
    3191                 :            :                 flush_shadow_used_ring_split(dev, vq);
    3192                 :            :                 vhost_vring_call_split(dev, vq);
    3193                 :            :         }
    3194                 :            : 
    3195                 :            :         return i;
    3196                 :            : }
    3197                 :            : 
    3198                 :            : __rte_noinline
    3199                 :            : static uint16_t
    3200                 :          0 : virtio_dev_tx_split_legacy(struct virtio_net *dev,
    3201                 :            :         struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
    3202                 :            :         struct rte_mbuf **pkts, uint16_t count)
    3203                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    3204                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    3205                 :            : {
    3206         [ #  # ]:          0 :         return virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count, true);
    3207                 :            : }
    3208                 :            : 
    3209                 :            : __rte_noinline
    3210                 :            : static uint16_t
    3211                 :          0 : virtio_dev_tx_split_compliant(struct virtio_net *dev,
    3212                 :            :         struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
    3213                 :            :         struct rte_mbuf **pkts, uint16_t count)
    3214                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    3215                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    3216                 :            : {
    3217         [ #  # ]:          0 :         return virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count, false);
    3218                 :            : }
    3219                 :            : 
    3220                 :            : static __rte_always_inline int
    3221                 :            : vhost_reserve_avail_batch_packed(struct virtio_net *dev,
    3222                 :            :                                  struct vhost_virtqueue *vq,
    3223                 :            :                                  struct rte_mbuf **pkts,
    3224                 :            :                                  uint16_t avail_idx,
    3225                 :            :                                  uintptr_t *desc_addrs,
    3226                 :            :                                  uint16_t *ids)
    3227                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    3228                 :            : {
    3229                 :          0 :         bool wrap = vq->avail_wrap_counter;
    3230                 :            :         struct vring_packed_desc *descs = vq->desc_packed;
    3231                 :            :         uint64_t lens[PACKED_BATCH_SIZE];
    3232                 :            :         uint64_t buf_lens[PACKED_BATCH_SIZE];
    3233                 :            :         uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
    3234                 :            :         uint16_t flags, i;
    3235                 :            : 
    3236                 :          0 :         if (unlikely(avail_idx & PACKED_BATCH_MASK))
    3237                 :            :                 return -1;
    3238   [ #  #  #  # ]:          0 :         if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
    3239                 :            :                 return -1;
    3240                 :            : 
    3241   [ #  #  #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    3242                 :          0 :                 flags = descs[avail_idx + i].flags;
    3243   [ #  #  #  #  :          0 :                 if (unlikely((wrap != !!(flags & VRING_DESC_F_AVAIL)) ||
          #  #  #  #  #  
                #  #  # ]
    3244                 :            :                              (wrap == !!(flags & VRING_DESC_F_USED))  ||
    3245                 :            :                              (flags & PACKED_DESC_SINGLE_DEQUEUE_FLAG)))
    3246                 :            :                         return -1;
    3247                 :            :         }
    3248                 :            : 
    3249                 :            :         rte_atomic_thread_fence(rte_memory_order_acquire);
    3250                 :            : 
    3251   [ #  #  #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
    3252                 :          0 :                 lens[i] = descs[avail_idx + i].len;
    3253                 :            : 
    3254   [ #  #  #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    3255                 :          0 :                 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
    3256                 :          0 :                                                   descs[avail_idx + i].addr,
    3257   [ #  #  #  # ]:          0 :                                                   &lens[i], VHOST_ACCESS_RW);
    3258                 :            :         }
    3259                 :            : 
    3260   [ #  #  #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    3261   [ #  #  #  # ]:          0 :                 if (unlikely(!desc_addrs[i]))
    3262                 :            :                         return -1;
    3263   [ #  #  #  # ]:          0 :                 if (unlikely((lens[i] != descs[avail_idx + i].len)))
    3264                 :            :                         return -1;
    3265                 :            :         }
    3266                 :            : 
    3267   [ #  #  #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    3268   [ #  #  #  # ]:          0 :                 if (virtio_dev_pktmbuf_prep(dev, pkts[i], lens[i]))
    3269                 :          0 :                         goto err;
    3270                 :            :         }
    3271                 :            : 
    3272   [ #  #  #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
    3273                 :          0 :                 buf_lens[i] = pkts[i]->buf_len - pkts[i]->data_off;
    3274                 :            : 
    3275   [ #  #  #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    3276   [ #  #  #  # ]:          0 :                 if (unlikely(buf_lens[i] < (lens[i] - buf_offset)))
    3277                 :          0 :                         goto err;
    3278                 :            :         }
    3279                 :            : 
    3280   [ #  #  #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    3281                 :          0 :                 pkts[i]->pkt_len = lens[i] - buf_offset;
    3282                 :          0 :                 pkts[i]->data_len = pkts[i]->pkt_len;
    3283                 :          0 :                 ids[i] = descs[avail_idx + i].id;
    3284                 :            :         }
    3285                 :            : 
    3286                 :            :         return 0;
    3287                 :            : 
    3288                 :            : err:
    3289                 :            :         return -1;
    3290                 :            : }
    3291                 :            : 
    3292                 :            : static __rte_always_inline int
    3293                 :            : vhost_async_tx_batch_packed_check(struct virtio_net *dev,
    3294                 :            :                                  struct vhost_virtqueue *vq,
    3295                 :            :                                  struct rte_mbuf **pkts,
    3296                 :            :                                  uint16_t avail_idx,
    3297                 :            :                                  uintptr_t *desc_addrs,
    3298                 :            :                                  uint64_t *lens,
    3299                 :            :                                  uint16_t *ids,
    3300                 :            :                                  int16_t dma_id,
    3301                 :            :                                  uint16_t vchan_id)
    3302                 :            : {
    3303                 :          0 :         bool wrap = vq->avail_wrap_counter;
    3304                 :            :         struct vring_packed_desc *descs = vq->desc_packed;
    3305                 :            :         uint64_t buf_lens[PACKED_BATCH_SIZE];
    3306                 :            :         uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
    3307                 :            :         uint16_t flags, i;
    3308                 :            : 
    3309                 :          0 :         if (unlikely(avail_idx & PACKED_BATCH_MASK))
    3310                 :            :                 return -1;
    3311   [ #  #  #  # ]:          0 :         if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
    3312                 :            :                 return -1;
    3313                 :            : 
    3314   [ #  #  #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    3315                 :          0 :                 flags = descs[avail_idx + i].flags;
    3316   [ #  #  #  #  :          0 :                 if (unlikely((wrap != !!(flags & VRING_DESC_F_AVAIL)) ||
          #  #  #  #  #  
                #  #  # ]
    3317                 :            :                              (wrap == !!(flags & VRING_DESC_F_USED))  ||
    3318                 :            :                              (flags & PACKED_DESC_SINGLE_DEQUEUE_FLAG)))
    3319                 :            :                         return -1;
    3320                 :            :         }
    3321                 :            : 
    3322                 :            :         rte_atomic_thread_fence(rte_memory_order_acquire);
    3323                 :            : 
    3324   [ #  #  #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
    3325                 :          0 :                 lens[i] = descs[avail_idx + i].len;
    3326                 :            : 
    3327   [ #  #  #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    3328                 :          0 :                 desc_addrs[i] = descs[avail_idx + i].addr;
    3329                 :            :         }
    3330                 :            : 
    3331   [ #  #  #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    3332   [ #  #  #  # ]:          0 :                 if (unlikely(!desc_addrs[i]))
    3333                 :            :                         return -1;
    3334   [ #  #  #  # ]:          0 :                 if (unlikely((lens[i] != descs[avail_idx + i].len)))
    3335                 :            :                         return -1;
    3336                 :            :         }
    3337                 :            : 
    3338   [ #  #  #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    3339   [ #  #  #  # ]:          0 :                 if (virtio_dev_pktmbuf_prep(dev, pkts[i], lens[i]))
    3340                 :          0 :                         goto err;
    3341                 :            :         }
    3342                 :            : 
    3343   [ #  #  #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
    3344                 :          0 :                 buf_lens[i] = pkts[i]->buf_len - pkts[i]->data_off;
    3345                 :            : 
    3346   [ #  #  #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    3347   [ #  #  #  # ]:          0 :                 if (unlikely(buf_lens[i] < (lens[i] - buf_offset)))
    3348                 :          0 :                         goto err;
    3349                 :            :         }
    3350                 :            : 
    3351   [ #  #  #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    3352                 :          0 :                 pkts[i]->pkt_len = lens[i] - buf_offset;
    3353                 :          0 :                 pkts[i]->data_len = pkts[i]->pkt_len;
    3354                 :          0 :                 ids[i] = descs[avail_idx + i].id;
    3355                 :            :         }
    3356                 :            : 
    3357   [ #  #  #  # ]:          0 :         if (rte_dma_burst_capacity(dma_id, vchan_id) < PACKED_BATCH_SIZE)
    3358                 :            :                 return -1;
    3359                 :            : 
    3360                 :            :         return 0;
    3361                 :            : 
    3362                 :            : err:
    3363                 :            :         return -1;
    3364                 :            : }
    3365                 :            : 
    3366                 :            : static __rte_always_inline int
    3367                 :            : virtio_dev_tx_batch_packed(struct virtio_net *dev,
    3368                 :            :                            struct vhost_virtqueue *vq,
    3369                 :            :                            struct rte_mbuf **pkts,
    3370                 :            :                            bool legacy_ol_flags)
    3371                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    3372                 :            : {
    3373                 :            :         uint16_t avail_idx = vq->last_avail_idx;
    3374                 :            :         uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
    3375                 :            :         struct virtio_net_hdr *hdr;
    3376                 :            :         uintptr_t desc_addrs[PACKED_BATCH_SIZE];
    3377                 :            :         uint16_t ids[PACKED_BATCH_SIZE];
    3378                 :            :         uint16_t i;
    3379                 :            : 
    3380   [ #  #  #  # ]:          0 :         if (vhost_reserve_avail_batch_packed(dev, vq, pkts, avail_idx,
    3381                 :            :                                              desc_addrs, ids))
    3382                 :            :                 return -1;
    3383                 :            : 
    3384   [ #  #  #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
    3385                 :          0 :                 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
    3386                 :            : 
    3387   [ #  #  #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
    3388                 :          0 :                 rte_memcpy(rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
    3389                 :          0 :                            (void *)(uintptr_t)(desc_addrs[i] + buf_offset),
    3390   [ #  #  #  # ]:          0 :                            pkts[i]->pkt_len);
    3391                 :            : 
    3392                 :            :         if (virtio_net_with_host_offload(dev)) {
    3393   [ #  #  #  # ]:          0 :                 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    3394                 :          0 :                         hdr = (struct virtio_net_hdr *)(desc_addrs[i]);
    3395   [ #  #  #  # ]:          0 :                         vhost_dequeue_offload(dev, hdr, pkts[i], legacy_ol_flags);
    3396                 :            :                 }
    3397                 :            :         }
    3398                 :            : 
    3399   [ #  #  #  # ]:          0 :         if (virtio_net_is_inorder(dev))
    3400                 :            :                 vhost_shadow_dequeue_batch_packed_inorder(vq,
    3401   [ #  #  #  # ]:          0 :                         ids[PACKED_BATCH_SIZE - 1]);
    3402                 :            :         else
    3403                 :            :                 vhost_shadow_dequeue_batch_packed(dev, vq, ids);
    3404                 :            : 
    3405                 :            :         vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
    3406                 :            : 
    3407                 :            :         return 0;
    3408                 :            : }
    3409                 :            : 
    3410                 :            : static __rte_always_inline int
    3411                 :            : vhost_dequeue_single_packed(struct virtio_net *dev,
    3412                 :            :                             struct vhost_virtqueue *vq,
    3413                 :            :                             struct rte_mempool *mbuf_pool,
    3414                 :            :                             struct rte_mbuf *pkts,
    3415                 :            :                             uint16_t *buf_id,
    3416                 :            :                             uint16_t *desc_count,
    3417                 :            :                             bool legacy_ol_flags)
    3418                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    3419                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    3420                 :            : {
    3421                 :            :         struct buf_vector buf_vec[BUF_VECTOR_MAX];
    3422                 :            :         uint32_t buf_len;
    3423                 :            :         uint16_t nr_vec = 0;
    3424                 :            :         int err;
    3425                 :            :         static bool allocerr_warned;
    3426                 :            : 
    3427   [ #  #  #  # ]:          0 :         if (unlikely(fill_vec_buf_packed(dev, vq,
    3428                 :            :                                          vq->last_avail_idx, desc_count,
    3429                 :            :                                          buf_vec, &nr_vec,
    3430                 :            :                                          buf_id, &buf_len,
    3431                 :            :                                          VHOST_ACCESS_RO) < 0))
    3432                 :            :                 return -1;
    3433                 :            : 
    3434   [ #  #  #  # ]:          0 :         if (unlikely(buf_len <= dev->vhost_hlen))
    3435                 :            :                 return -1;
    3436                 :            : 
    3437   [ #  #  #  # ]:          0 :         buf_len -= dev->vhost_hlen;
    3438                 :            : 
    3439   [ #  #  #  # ]:          0 :         if (unlikely(virtio_dev_pktmbuf_prep(dev, pkts, buf_len))) {
    3440   [ #  #  #  # ]:          0 :                 if (!allocerr_warned) {
    3441                 :          0 :                         VHOST_DATA_LOG(dev->ifname, ERR,
    3442                 :            :                                 "failed mbuf alloc of size %d from %s.",
    3443                 :            :                                 buf_len, mbuf_pool->name);
    3444                 :          0 :                         allocerr_warned = true;
    3445                 :            :                 }
    3446                 :            :                 return -1;
    3447                 :            :         }
    3448                 :            : 
    3449                 :            :         err = desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts,
    3450                 :            :                            mbuf_pool, legacy_ol_flags, 0, false);
    3451   [ #  #  #  # ]:          0 :         if (unlikely(err)) {
    3452   [ #  #  #  # ]:          0 :                 if (!allocerr_warned) {
    3453                 :          0 :                         VHOST_DATA_LOG(dev->ifname, ERR, "failed to copy desc to mbuf.");
    3454                 :          0 :                         allocerr_warned = true;
    3455                 :            :                 }
    3456                 :            :                 return -1;
    3457                 :            :         }
    3458                 :            : 
    3459                 :            :         return 0;
    3460                 :            : }
    3461                 :            : 
    3462                 :            : static __rte_always_inline int
    3463                 :            : virtio_dev_tx_single_packed(struct virtio_net *dev,
    3464                 :            :                             struct vhost_virtqueue *vq,
    3465                 :            :                             struct rte_mempool *mbuf_pool,
    3466                 :            :                             struct rte_mbuf *pkts,
    3467                 :            :                             bool legacy_ol_flags)
    3468                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    3469                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    3470                 :            : {
    3471                 :            : 
    3472                 :            :         uint16_t buf_id, desc_count = 0;
    3473                 :            :         int ret;
    3474                 :            : 
    3475                 :            :         ret = vhost_dequeue_single_packed(dev, vq, mbuf_pool, pkts, &buf_id,
    3476                 :            :                                         &desc_count, legacy_ol_flags);
    3477                 :            : 
    3478   [ #  #  #  # ]:          0 :         if (likely(desc_count > 0)) {
    3479   [ #  #  #  # ]:          0 :                 if (virtio_net_is_inorder(dev))
    3480                 :            :                         vhost_shadow_dequeue_single_packed_inorder(vq, buf_id,
    3481                 :            :                                                                    desc_count);
    3482                 :            :                 else
    3483                 :            :                         vhost_shadow_dequeue_single_packed(vq, buf_id,
    3484                 :            :                                         desc_count);
    3485                 :            : 
    3486                 :            :                 vq_inc_last_avail_packed(vq, desc_count);
    3487                 :            :         }
    3488                 :            : 
    3489                 :            :         return ret;
    3490                 :            : }
    3491                 :            : 
    3492                 :            : static __rte_always_inline uint16_t
    3493                 :            : get_nb_avail_entries_packed(const struct vhost_virtqueue *__rte_restrict vq,
    3494                 :            :                             uint16_t max_nb_avail_entries)
    3495                 :            : {
    3496                 :          0 :         const struct vring_packed_desc *descs = vq->desc_packed;
    3497                 :          0 :         bool avail_wrap = vq->avail_wrap_counter;
    3498                 :          0 :         uint16_t avail_idx = vq->last_avail_idx;
    3499                 :            :         uint16_t nb_avail_entries = 0;
    3500                 :            :         uint16_t flags;
    3501                 :            : 
    3502   [ #  #  #  # ]:          0 :         while (nb_avail_entries < max_nb_avail_entries) {
    3503                 :          0 :                 flags = descs[avail_idx].flags;
    3504                 :            : 
    3505   [ #  #  #  # ]:          0 :                 if ((avail_wrap != !!(flags & VRING_DESC_F_AVAIL)) ||
    3506   [ #  #  #  # ]:          0 :                     (avail_wrap == !!(flags & VRING_DESC_F_USED)))
    3507                 :            :                         return nb_avail_entries;
    3508                 :            : 
    3509   [ #  #  #  # ]:          0 :                 if (!(flags & VRING_DESC_F_NEXT))
    3510                 :          0 :                         ++nb_avail_entries;
    3511                 :            : 
    3512   [ #  #  #  # ]:          0 :                 if (unlikely(++avail_idx >= vq->size)) {
    3513                 :          0 :                         avail_idx -= vq->size;
    3514                 :          0 :                         avail_wrap = !avail_wrap;
    3515                 :            :                 }
    3516                 :            :         }
    3517                 :            : 
    3518                 :            :         return nb_avail_entries;
    3519                 :            : }
    3520                 :            : 
    3521                 :            : __rte_always_inline
    3522                 :            : static uint16_t
    3523                 :            : virtio_dev_tx_packed(struct virtio_net *dev,
    3524                 :            :                      struct vhost_virtqueue *__rte_restrict vq,
    3525                 :            :                      struct rte_mempool *mbuf_pool,
    3526                 :            :                      struct rte_mbuf **__rte_restrict pkts,
    3527                 :            :                      uint32_t count,
    3528                 :            :                      bool legacy_ol_flags)
    3529                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    3530                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    3531                 :            : {
    3532                 :            :         uint32_t pkt_idx = 0;
    3533                 :            : 
    3534                 :          0 :         count = get_nb_avail_entries_packed(vq, count);
    3535   [ #  #  #  # ]:          0 :         if (count == 0)
    3536                 :            :                 return 0;
    3537                 :            : 
    3538   [ #  #  #  # ]:          0 :         if (rte_pktmbuf_alloc_bulk(mbuf_pool, pkts, count)) {
    3539                 :          0 :                 vq->stats.mbuf_alloc_failed += count;
    3540                 :          0 :                 return 0;
    3541                 :            :         }
    3542                 :            : 
    3543                 :            :         do {
    3544                 :          0 :                 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
    3545                 :            : 
    3546   [ #  #  #  # ]:          0 :                 if (count - pkt_idx >= PACKED_BATCH_SIZE) {
    3547                 :          0 :                         if (!virtio_dev_tx_batch_packed(dev, vq,
    3548   [ #  #  #  # ]:          0 :                                                         &pkts[pkt_idx],
    3549                 :            :                                                         legacy_ol_flags)) {
    3550                 :          0 :                                 pkt_idx += PACKED_BATCH_SIZE;
    3551                 :          0 :                                 continue;
    3552                 :            :                         }
    3553                 :            :                 }
    3554                 :            : 
    3555   [ #  #  #  # ]:          0 :                 if (virtio_dev_tx_single_packed(dev, vq, mbuf_pool,
    3556                 :          0 :                                                 pkts[pkt_idx],
    3557                 :            :                                                 legacy_ol_flags))
    3558                 :            :                         break;
    3559                 :          0 :                 pkt_idx++;
    3560   [ #  #  #  # ]:          0 :         } while (pkt_idx < count);
    3561                 :            : 
    3562   [ #  #  #  # ]:          0 :         if (pkt_idx != count)
    3563                 :          0 :                 rte_pktmbuf_free_bulk(&pkts[pkt_idx], count - pkt_idx);
    3564                 :            : 
    3565   [ #  #  #  # ]:          0 :         if (vq->shadow_used_idx) {
    3566                 :          0 :                 do_data_copy_dequeue(vq);
    3567                 :            : 
    3568                 :            :                 vhost_flush_dequeue_shadow_packed(dev, vq);
    3569                 :            :                 vhost_vring_call_packed(dev, vq);
    3570                 :            :         }
    3571                 :            : 
    3572                 :          0 :         return pkt_idx;
    3573                 :            : }
    3574                 :            : 
    3575                 :            : __rte_noinline
    3576                 :            : static uint16_t
    3577                 :          0 : virtio_dev_tx_packed_legacy(struct virtio_net *dev,
    3578                 :            :         struct vhost_virtqueue *__rte_restrict vq, struct rte_mempool *mbuf_pool,
    3579                 :            :         struct rte_mbuf **__rte_restrict pkts, uint32_t count)
    3580                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    3581                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    3582                 :            : {
    3583                 :          0 :         return virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count, true);
    3584                 :            : }
    3585                 :            : 
    3586                 :            : __rte_noinline
    3587                 :            : static uint16_t
    3588                 :          0 : virtio_dev_tx_packed_compliant(struct virtio_net *dev,
    3589                 :            :         struct vhost_virtqueue *__rte_restrict vq, struct rte_mempool *mbuf_pool,
    3590                 :            :         struct rte_mbuf **__rte_restrict pkts, uint32_t count)
    3591                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    3592                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    3593                 :            : {
    3594                 :          0 :         return virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count, false);
    3595                 :            : }
    3596                 :            : 
    3597                 :            : RTE_EXPORT_SYMBOL(rte_vhost_dequeue_burst)
    3598                 :            : uint16_t
    3599         [ #  # ]:          0 : rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
    3600                 :            :         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
    3601                 :            : {
    3602                 :            :         struct virtio_net *dev;
    3603                 :            :         struct vhost_virtqueue *vq;
    3604                 :            :         int16_t success = 1;
    3605                 :            :         uint16_t nb_rx = 0;
    3606                 :            : 
    3607                 :            :         dev = get_device(vid);
    3608         [ #  # ]:          0 :         if (!dev)
    3609                 :            :                 return 0;
    3610                 :            : 
    3611         [ #  # ]:          0 :         if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
    3612                 :          0 :                 VHOST_DATA_LOG(dev->ifname, ERR,
    3613                 :            :                         "%s: built-in vhost net backend is disabled.",
    3614                 :            :                         __func__);
    3615                 :          0 :                 goto out_no_unlock;
    3616                 :            :         }
    3617                 :            : 
    3618   [ #  #  #  # ]:          0 :         if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
    3619                 :          0 :                 VHOST_DATA_LOG(dev->ifname, ERR,
    3620                 :            :                         "%s: invalid virtqueue idx %d.",
    3621                 :            :                         __func__, queue_id);
    3622                 :          0 :                 goto out_no_unlock;
    3623                 :            :         }
    3624                 :            : 
    3625                 :          0 :         vq = dev->virtqueue[queue_id];
    3626                 :            : 
    3627         [ #  # ]:          0 :         if (unlikely(rte_rwlock_read_trylock(&vq->access_lock) != 0))
    3628                 :          0 :                 goto out_no_unlock;
    3629                 :            : 
    3630         [ #  # ]:          0 :         if (unlikely(!vq->enabled))
    3631                 :          0 :                 goto out_access_unlock;
    3632                 :            : 
    3633                 :            :         vhost_user_iotlb_rd_lock(vq);
    3634                 :            : 
    3635         [ #  # ]:          0 :         if (unlikely(!vq->access_ok)) {
    3636                 :            :                 vhost_user_iotlb_rd_unlock(vq);
    3637                 :            :                 rte_rwlock_read_unlock(&vq->access_lock);
    3638                 :            : 
    3639                 :          0 :                 virtio_dev_vring_translate(dev, vq);
    3640                 :            : 
    3641                 :          0 :                 goto out_no_unlock;
    3642                 :            :         }
    3643                 :            : 
    3644                 :            :         /*
    3645                 :            :          * Construct a RARP broadcast packet, and inject it to the "pkts"
    3646                 :            :          * array, to looks like that guest actually send such packet.
    3647                 :            :          *
    3648                 :            :          * Check user_send_rarp() for more information.
    3649                 :            :          *
    3650                 :            :          * broadcast_rarp shares a cacheline in the virtio_net structure
    3651                 :            :          * with some fields that are accessed during enqueue and
    3652                 :            :          * rte_atomic_compare_exchange_strong_explicit causes a write if performed compare
    3653                 :            :          * and exchange. This could result in false sharing between enqueue
    3654                 :            :          * and dequeue.
    3655                 :            :          *
    3656                 :            :          * Prevent unnecessary false sharing by reading broadcast_rarp first
    3657                 :            :          * and only performing compare and exchange if the read indicates it
    3658                 :            :          * is likely to be set.
    3659                 :            :          */
    3660   [ #  #  #  # ]:          0 :         if (unlikely(rte_atomic_load_explicit(&dev->broadcast_rarp, rte_memory_order_acquire) &&
    3661                 :            :                         rte_atomic_compare_exchange_strong_explicit(&dev->broadcast_rarp,
    3662                 :            :                         &success, 0, rte_memory_order_release, rte_memory_order_relaxed))) {
    3663                 :            :                 /*
    3664                 :            :                  * Inject the RARP packet to the head of "pkts" array,
    3665                 :            :                  * so that switch's mac learning table will get updated first.
    3666                 :            :                  */
    3667                 :          0 :                 pkts[nb_rx] = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
    3668         [ #  # ]:          0 :                 if (pkts[nb_rx] == NULL) {
    3669                 :          0 :                         VHOST_DATA_LOG(dev->ifname, ERR, "failed to make RARP packet.");
    3670                 :          0 :                         goto out;
    3671                 :            :                 }
    3672                 :            :                 nb_rx += 1;
    3673                 :            :         }
    3674                 :            : 
    3675         [ #  # ]:          0 :         if (vq_is_packed(dev)) {
    3676         [ #  # ]:          0 :                 if (dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS)
    3677                 :          0 :                         nb_rx += virtio_dev_tx_packed_legacy(dev, vq, mbuf_pool,
    3678                 :          0 :                                         pkts + nb_rx, count - nb_rx);
    3679                 :            :                 else
    3680                 :          0 :                         nb_rx += virtio_dev_tx_packed_compliant(dev, vq, mbuf_pool,
    3681                 :          0 :                                         pkts + nb_rx, count - nb_rx);
    3682                 :            :         } else {
    3683         [ #  # ]:          0 :                 if (dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS)
    3684                 :          0 :                         nb_rx += virtio_dev_tx_split_legacy(dev, vq, mbuf_pool,
    3685                 :          0 :                                         pkts + nb_rx, count - nb_rx);
    3686                 :            :                 else
    3687                 :          0 :                         nb_rx += virtio_dev_tx_split_compliant(dev, vq, mbuf_pool,
    3688                 :          0 :                                         pkts + nb_rx, count - nb_rx);
    3689                 :            :         }
    3690                 :            : 
    3691                 :          0 :         vhost_queue_stats_update(dev, vq, pkts, nb_rx);
    3692                 :            : 
    3693                 :          0 : out:
    3694                 :            :         vhost_user_iotlb_rd_unlock(vq);
    3695                 :            : 
    3696                 :          0 : out_access_unlock:
    3697                 :            :         rte_rwlock_read_unlock(&vq->access_lock);
    3698                 :            : 
    3699                 :            : out_no_unlock:
    3700                 :            :         return nb_rx;
    3701                 :            : }
    3702                 :            : 
    3703                 :            : static __rte_always_inline uint16_t
    3704                 :            : async_poll_dequeue_completed(struct virtio_net *dev, struct vhost_virtqueue *vq,
    3705                 :            :                 struct rte_mbuf **pkts, uint16_t count, int16_t dma_id,
    3706                 :            :                 uint16_t vchan_id, bool legacy_ol_flags)
    3707                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    3708                 :            : {
    3709                 :            :         uint16_t start_idx, from, i;
    3710                 :            :         uint16_t nr_cpl_pkts = 0;
    3711                 :          0 :         struct async_inflight_info *pkts_info = vq->async->pkts_info;
    3712                 :            : 
    3713                 :            :         vhost_async_dma_check_completed(dev, dma_id, vchan_id, VHOST_DMA_MAX_COPY_COMPLETE);
    3714                 :            : 
    3715                 :            :         start_idx = async_get_first_inflight_pkt_idx(vq);
    3716                 :            : 
    3717                 :            :         from = start_idx;
    3718   [ #  #  #  #  :          0 :         while (vq->async->pkts_cmpl_flag[from] && count--) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    3719                 :          0 :                 vq->async->pkts_cmpl_flag[from] = false;
    3720                 :          0 :                 from = (from + 1) % vq->size;
    3721                 :          0 :                 nr_cpl_pkts++;
    3722                 :            :         }
    3723                 :            : 
    3724   [ #  #  #  #  :          0 :         if (nr_cpl_pkts == 0)
          #  #  #  #  #  
                #  #  # ]
    3725                 :            :                 return 0;
    3726                 :            : 
    3727   [ #  #  #  #  :          0 :         for (i = 0; i < nr_cpl_pkts; i++) {
          #  #  #  #  #  
                #  #  # ]
    3728                 :          0 :                 from = (start_idx + i) % vq->size;
    3729   [ #  #  #  #  :          0 :                 pkts[i] = pkts_info[from].mbuf;
          #  #  #  #  #  
                #  #  # ]
    3730                 :            : 
    3731                 :            :                 if (virtio_net_with_host_offload(dev))
    3732                 :            :                         vhost_dequeue_offload(dev, &pkts_info[from].nethdr, pkts[i],
    3733                 :            :                                               legacy_ol_flags);
    3734                 :            :         }
    3735                 :            : 
    3736                 :            :         /* write back completed descs to used ring and update used idx */
    3737   [ #  #  #  #  :          0 :         if (vq_is_packed(dev)) {
          #  #  #  #  #  
                #  #  # ]
    3738                 :            :                 write_back_completed_descs_packed(vq, nr_cpl_pkts);
    3739                 :            :                 vhost_vring_call_packed(dev, vq);
    3740                 :            :         } else {
    3741                 :          0 :                 write_back_completed_descs_split(vq, nr_cpl_pkts);
    3742                 :          0 :                 rte_atomic_fetch_add_explicit((unsigned short __rte_atomic *)&vq->used->idx,
    3743                 :            :                         nr_cpl_pkts, rte_memory_order_release);
    3744                 :            :                 vhost_vring_call_split(dev, vq);
    3745                 :            :         }
    3746                 :          0 :         vq->async->pkts_inflight_n -= nr_cpl_pkts;
    3747                 :            : 
    3748                 :          0 :         return nr_cpl_pkts;
    3749                 :            : }
    3750                 :            : 
    3751                 :            : static __rte_always_inline uint16_t
    3752                 :            : virtio_dev_tx_async_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
    3753                 :            :                 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count,
    3754                 :            :                 int16_t dma_id, uint16_t vchan_id, bool legacy_ol_flags)
    3755                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    3756                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    3757                 :            : {
    3758                 :            :         static bool allocerr_warned;
    3759                 :            :         bool dropped = false;
    3760                 :            :         uint16_t avail_entries;
    3761                 :            :         uint16_t pkt_idx, slot_idx = 0;
    3762                 :            :         uint16_t nr_done_pkts = 0;
    3763                 :            :         uint16_t pkt_err = 0;
    3764                 :            :         uint16_t n_xfer;
    3765                 :          0 :         struct vhost_async *async = vq->async;
    3766                 :          0 :         struct async_inflight_info *pkts_info = async->pkts_info;
    3767                 :            :         struct rte_mbuf *pkts_prealloc[MAX_PKT_BURST];
    3768                 :            :         uint16_t pkts_size = count;
    3769                 :            : 
    3770                 :            :         /**
    3771                 :            :          * The ordering between avail index and
    3772                 :            :          * desc reads needs to be enforced.
    3773                 :            :          */
    3774                 :          0 :         avail_entries = rte_atomic_load_explicit((unsigned short __rte_atomic *)&vq->avail->idx,
    3775                 :          0 :                 rte_memory_order_acquire) - vq->last_avail_idx;
    3776                 :          0 :         if (avail_entries == 0)
    3777                 :          0 :                 goto out;
    3778                 :            : 
    3779                 :          0 :         rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
    3780                 :            : 
    3781                 :            :         async_iter_reset(async);
    3782                 :            : 
    3783                 :          0 :         count = RTE_MIN(count, MAX_PKT_BURST);
    3784                 :          0 :         count = RTE_MIN(count, avail_entries);
    3785                 :            :         VHOST_DATA_LOG(dev->ifname, DEBUG, "about to dequeue %u buffers", count);
    3786                 :            : 
    3787   [ #  #  #  # ]:          0 :         if (rte_pktmbuf_alloc_bulk(mbuf_pool, pkts_prealloc, count)) {
    3788                 :          0 :                 vq->stats.mbuf_alloc_failed += count;
    3789                 :          0 :                 goto out;
    3790                 :            :         }
    3791                 :            : 
    3792   [ #  #  #  # ]:          0 :         for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
    3793                 :            :                 uint16_t head_idx = 0;
    3794                 :            :                 uint16_t nr_vec = 0;
    3795                 :            :                 uint16_t to;
    3796                 :            :                 uint32_t buf_len;
    3797                 :            :                 int err;
    3798                 :            :                 struct buf_vector buf_vec[BUF_VECTOR_MAX];
    3799                 :          0 :                 struct rte_mbuf *pkt = pkts_prealloc[pkt_idx];
    3800                 :            : 
    3801   [ #  #  #  #  :          0 :                 if (unlikely(fill_vec_buf_split(dev, vq, vq->last_avail_idx,
             #  #  #  # ]
    3802                 :            :                                                 &nr_vec, buf_vec,
    3803                 :            :                                                 &head_idx, &buf_len,
    3804                 :            :                                                 VHOST_ACCESS_RO) < 0)) {
    3805                 :            :                         dropped = true;
    3806                 :          0 :                         break;
    3807                 :            :                 }
    3808                 :            : 
    3809   [ #  #  #  # ]:          0 :                 if (unlikely(buf_len <= dev->vhost_hlen)) {
    3810                 :            :                         dropped = true;
    3811                 :            :                         break;
    3812                 :            :                 }
    3813                 :            : 
    3814   [ #  #  #  # ]:          0 :                 buf_len -= dev->vhost_hlen;
    3815                 :            : 
    3816                 :            :                 err = virtio_dev_pktmbuf_prep(dev, pkt, buf_len);
    3817   [ #  #  #  # ]:          0 :                 if (unlikely(err)) {
    3818                 :            :                         /**
    3819                 :            :                          * mbuf allocation fails for jumbo packets when external
    3820                 :            :                          * buffer allocation is not allowed and linear buffer
    3821                 :            :                          * is required. Drop this packet.
    3822                 :            :                          */
    3823   [ #  #  #  # ]:          0 :                         if (!allocerr_warned) {
    3824                 :          0 :                                 VHOST_DATA_LOG(dev->ifname, ERR,
    3825                 :            :                                         "%s: Failed mbuf alloc of size %d from %s",
    3826                 :            :                                         __func__, buf_len, mbuf_pool->name);
    3827                 :          0 :                                 allocerr_warned = true;
    3828                 :            :                         }
    3829                 :            :                         dropped = true;
    3830                 :          0 :                         slot_idx--;
    3831                 :          0 :                         break;
    3832                 :            :                 }
    3833                 :            : 
    3834   [ #  #  #  # ]:          0 :                 slot_idx = (async->pkts_idx + pkt_idx) & (vq->size - 1);
    3835                 :            :                 err = desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkt, mbuf_pool,
    3836                 :            :                                         legacy_ol_flags, slot_idx, true);
    3837   [ #  #  #  # ]:          0 :                 if (unlikely(err)) {
    3838   [ #  #  #  # ]:          0 :                         if (!allocerr_warned) {
    3839                 :          0 :                                 VHOST_DATA_LOG(dev->ifname, ERR,
    3840                 :            :                                         "%s: Failed to offload copies to async channel.",
    3841                 :            :                                         __func__);
    3842                 :          0 :                                 allocerr_warned = true;
    3843                 :            :                         }
    3844                 :            :                         dropped = true;
    3845                 :          0 :                         slot_idx--;
    3846                 :          0 :                         break;
    3847                 :            :                 }
    3848                 :            : 
    3849                 :          0 :                 pkts_info[slot_idx].mbuf = pkt;
    3850                 :            : 
    3851                 :            :                 /* store used descs */
    3852                 :          0 :                 to = async->desc_idx_split & (vq->size - 1);
    3853                 :          0 :                 async->descs_split[to].id = head_idx;
    3854                 :          0 :                 async->descs_split[to].len = 0;
    3855                 :          0 :                 async->desc_idx_split++;
    3856                 :            : 
    3857   [ #  #  #  # ]:          0 :                 vq->last_avail_idx++;
    3858                 :            :                 vhost_virtqueue_reconnect_log_split(vq);
    3859                 :            :         }
    3860                 :            : 
    3861   [ #  #  #  # ]:          0 :         if (unlikely(dropped))
    3862                 :          0 :                 rte_pktmbuf_free_bulk(&pkts_prealloc[pkt_idx], count - pkt_idx);
    3863                 :            : 
    3864                 :          0 :         n_xfer = vhost_async_dma_transfer(dev, vq, dma_id, vchan_id, async->pkts_idx,
    3865                 :          0 :                                           async->iov_iter, pkt_idx);
    3866                 :            : 
    3867                 :          0 :         async->pkts_inflight_n += n_xfer;
    3868                 :            : 
    3869                 :          0 :         pkt_err = pkt_idx - n_xfer;
    3870   [ #  #  #  # ]:          0 :         if (unlikely(pkt_err)) {
    3871                 :            :                 VHOST_DATA_LOG(dev->ifname, DEBUG, "%s: failed to transfer data.",
    3872                 :            :                         __func__);
    3873                 :            : 
    3874                 :            :                 pkt_idx = n_xfer;
    3875                 :            :                 /* recover available ring */
    3876   [ #  #  #  # ]:          0 :                 vq->last_avail_idx -= pkt_err;
    3877                 :            :                 vhost_virtqueue_reconnect_log_split(vq);
    3878                 :            : 
    3879                 :            :                 /**
    3880                 :            :                  * recover async channel copy related structures and free pktmbufs
    3881                 :            :                  * for error pkts.
    3882                 :            :                  */
    3883                 :          0 :                 async->desc_idx_split -= pkt_err;
    3884   [ #  #  #  # ]:          0 :                 while (pkt_err-- > 0) {
    3885                 :          0 :                         rte_pktmbuf_free(pkts_info[slot_idx & (vq->size - 1)].mbuf);
    3886                 :          0 :                         slot_idx--;
    3887                 :            :                 }
    3888                 :            :         }
    3889                 :            : 
    3890                 :          0 :         async->pkts_idx += pkt_idx;
    3891   [ #  #  #  # ]:          0 :         if (async->pkts_idx >= vq->size)
    3892                 :          0 :                 async->pkts_idx -= vq->size;
    3893                 :            : 
    3894                 :          0 : out:
    3895                 :            :         /* DMA device may serve other queues, unconditionally check completed. */
    3896                 :            :         nr_done_pkts = async_poll_dequeue_completed(dev, vq, pkts, pkts_size,
    3897                 :            :                                                         dma_id, vchan_id, legacy_ol_flags);
    3898                 :            : 
    3899                 :            :         return nr_done_pkts;
    3900                 :            : }
    3901                 :            : 
    3902                 :            : __rte_noinline
    3903                 :            : static uint16_t
    3904                 :          0 : virtio_dev_tx_async_split_legacy(struct virtio_net *dev,
    3905                 :            :                 struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
    3906                 :            :                 struct rte_mbuf **pkts, uint16_t count,
    3907                 :            :                 int16_t dma_id, uint16_t vchan_id)
    3908                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    3909                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    3910                 :            : {
    3911         [ #  # ]:          0 :         return virtio_dev_tx_async_split(dev, vq, mbuf_pool,
    3912                 :            :                                 pkts, count, dma_id, vchan_id, true);
    3913                 :            : }
    3914                 :            : 
    3915                 :            : __rte_noinline
    3916                 :            : static uint16_t
    3917                 :          0 : virtio_dev_tx_async_split_compliant(struct virtio_net *dev,
    3918                 :            :                 struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
    3919                 :            :                 struct rte_mbuf **pkts, uint16_t count,
    3920                 :            :                 int16_t dma_id, uint16_t vchan_id)
    3921                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    3922                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    3923                 :            : {
    3924         [ #  # ]:          0 :         return virtio_dev_tx_async_split(dev, vq, mbuf_pool,
    3925                 :            :                                 pkts, count, dma_id, vchan_id, false);
    3926                 :            : }
    3927                 :            : 
    3928                 :            : static __rte_always_inline void
    3929                 :            : vhost_async_shadow_dequeue_single_packed(struct vhost_virtqueue *vq,
    3930                 :            :                                 uint16_t buf_id, uint16_t count)
    3931                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    3932                 :            : {
    3933                 :          0 :         struct vhost_async *async = vq->async;
    3934                 :          0 :         uint16_t idx = async->buffer_idx_packed;
    3935                 :            : 
    3936                 :          0 :         async->buffers_packed[idx].id = buf_id;
    3937                 :          0 :         async->buffers_packed[idx].len = 0;
    3938                 :          0 :         async->buffers_packed[idx].count = count;
    3939                 :            : 
    3940                 :          0 :         async->buffer_idx_packed++;
    3941                 :          0 :         if (async->buffer_idx_packed >= vq->size)
    3942                 :          0 :                 async->buffer_idx_packed -= vq->size;
    3943                 :            : 
    3944                 :            : }
    3945                 :            : 
    3946                 :            : static __rte_always_inline int
    3947                 :            : virtio_dev_tx_async_single_packed(struct virtio_net *dev,
    3948                 :            :                         struct vhost_virtqueue *vq,
    3949                 :            :                         struct rte_mempool *mbuf_pool,
    3950                 :            :                         struct rte_mbuf *pkts,
    3951                 :            :                         uint16_t slot_idx,
    3952                 :            :                         bool legacy_ol_flags)
    3953                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    3954                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    3955                 :            : {
    3956                 :            :         int err;
    3957                 :            :         uint16_t buf_id, desc_count = 0;
    3958                 :            :         uint16_t nr_vec = 0;
    3959                 :            :         uint32_t buf_len;
    3960                 :            :         struct buf_vector buf_vec[BUF_VECTOR_MAX];
    3961                 :          0 :         struct vhost_async *async = vq->async;
    3962                 :          0 :         struct async_inflight_info *pkts_info = async->pkts_info;
    3963                 :            :         static bool allocerr_warned;
    3964                 :            : 
    3965   [ #  #  #  # ]:          0 :         if (unlikely(fill_vec_buf_packed(dev, vq, vq->last_avail_idx, &desc_count,
    3966                 :            :                                          buf_vec, &nr_vec, &buf_id, &buf_len,
    3967                 :            :                                          VHOST_ACCESS_RO) < 0))
    3968                 :            :                 return -1;
    3969                 :            : 
    3970   [ #  #  #  # ]:          0 :         if (unlikely(buf_len <= dev->vhost_hlen)) {
    3971   [ #  #  #  # ]:          0 :                 if (!allocerr_warned) {
    3972                 :          0 :                         VHOST_DATA_LOG(dev->ifname, ERR, "Invalid buffer length.");
    3973                 :          0 :                         allocerr_warned = true;
    3974                 :            :                 }
    3975                 :            :                 return -1;
    3976                 :            :         }
    3977                 :            : 
    3978   [ #  #  #  # ]:          0 :         buf_len -= dev->vhost_hlen;
    3979                 :            : 
    3980   [ #  #  #  # ]:          0 :         if (unlikely(virtio_dev_pktmbuf_prep(dev, pkts, buf_len))) {
    3981   [ #  #  #  # ]:          0 :                 if (!allocerr_warned) {
    3982                 :          0 :                         VHOST_DATA_LOG(dev->ifname, ERR, "Failed mbuf alloc of size %d from %s.",
    3983                 :            :                                 buf_len, mbuf_pool->name);
    3984                 :            : 
    3985                 :          0 :                         allocerr_warned = true;
    3986                 :            :                 }
    3987                 :            :                 return -1;
    3988                 :            :         }
    3989                 :            : 
    3990                 :            :         err = desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts, mbuf_pool,
    3991                 :            :                 legacy_ol_flags, slot_idx, true);
    3992   [ #  #  #  # ]:          0 :         if (unlikely(err)) {
    3993                 :          0 :                 rte_pktmbuf_free(pkts);
    3994   [ #  #  #  # ]:          0 :                 if (!allocerr_warned) {
    3995                 :          0 :                         VHOST_DATA_LOG(dev->ifname, ERR, "Failed to copy desc to mbuf on.");
    3996                 :          0 :                         allocerr_warned = true;
    3997                 :            :                 }
    3998                 :            :                 return -1;
    3999                 :            :         }
    4000                 :            : 
    4001   [ #  #  #  # ]:          0 :         pkts_info[slot_idx].descs = desc_count;
    4002                 :            : 
    4003                 :            :         /* update async shadow packed ring */
    4004                 :            :         vhost_async_shadow_dequeue_single_packed(vq, buf_id, desc_count);
    4005                 :            : 
    4006                 :            :         vq_inc_last_avail_packed(vq, desc_count);
    4007                 :            : 
    4008                 :            :         return err;
    4009                 :            : }
    4010                 :            : 
    4011                 :            : static __rte_always_inline int
    4012                 :            : virtio_dev_tx_async_packed_batch(struct virtio_net *dev,
    4013                 :            :                            struct vhost_virtqueue *vq,
    4014                 :            :                            struct rte_mbuf **pkts, uint16_t slot_idx,
    4015                 :            :                            uint16_t dma_id, uint16_t vchan_id)
    4016                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    4017                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    4018                 :            : {
    4019                 :            :         uint16_t avail_idx = vq->last_avail_idx;
    4020                 :            :         uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
    4021                 :          0 :         struct vhost_async *async = vq->async;
    4022                 :          0 :         struct async_inflight_info *pkts_info = async->pkts_info;
    4023                 :            :         struct virtio_net_hdr *hdr;
    4024                 :            :         uint32_t mbuf_offset = 0;
    4025                 :            :         uintptr_t desc_addrs[PACKED_BATCH_SIZE];
    4026                 :            :         uint64_t desc_vva;
    4027                 :            :         uint64_t lens[PACKED_BATCH_SIZE];
    4028                 :            :         void *host_iova[PACKED_BATCH_SIZE];
    4029                 :            :         uint64_t mapped_len[PACKED_BATCH_SIZE];
    4030                 :            :         uint16_t ids[PACKED_BATCH_SIZE];
    4031                 :            :         uint16_t i;
    4032                 :            : 
    4033                 :          0 :         if (vhost_async_tx_batch_packed_check(dev, vq, pkts, avail_idx,
    4034                 :            :                                              desc_addrs, lens, ids, dma_id, vchan_id))
    4035                 :            :                 return -1;
    4036                 :            : 
    4037   [ #  #  #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
    4038                 :          0 :                 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
    4039                 :            : 
    4040   [ #  #  #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    4041                 :          0 :                 host_iova[i] = (void *)(uintptr_t)gpa_to_first_hpa(dev,
    4042   [ #  #  #  # ]:          0 :                         desc_addrs[i] + buf_offset, pkts[i]->pkt_len, &mapped_len[i]);
    4043                 :            :         }
    4044                 :            : 
    4045   [ #  #  #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    4046                 :            :                 async_iter_initialize(dev, async);
    4047                 :          0 :                 async_iter_add_iovec(dev, async,
    4048                 :            :                 host_iova[i],
    4049   [ #  #  #  # ]:          0 :                 (void *)(uintptr_t)rte_pktmbuf_iova_offset(pkts[i], mbuf_offset),
    4050                 :            :                 mapped_len[i]);
    4051                 :          0 :                 async->iter_idx++;
    4052                 :            :         }
    4053                 :            : 
    4054                 :            :         if (virtio_net_with_host_offload(dev)) {
    4055   [ #  #  #  # ]:          0 :                 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    4056                 :          0 :                         desc_vva = vhost_iova_to_vva(dev, vq, desc_addrs[i],
    4057   [ #  #  #  # ]:          0 :                                                 &lens[i], VHOST_ACCESS_RO);
    4058                 :          0 :                         hdr = (struct virtio_net_hdr *)(uintptr_t)desc_vva;
    4059                 :          0 :                         pkts_info[slot_idx + i].nethdr = *hdr;
    4060                 :            :                 }
    4061                 :            :         }
    4062                 :            : 
    4063                 :            :         vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
    4064                 :            : 
    4065                 :            :         vhost_async_shadow_dequeue_packed_batch(vq, ids);
    4066                 :            : 
    4067                 :            :         return 0;
    4068                 :            : }
    4069                 :            : 
    4070                 :            : static __rte_always_inline uint16_t
    4071                 :            : virtio_dev_tx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
    4072                 :            :                 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts,
    4073                 :            :                 uint16_t count, uint16_t dma_id, uint16_t vchan_id, bool legacy_ol_flags)
    4074                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    4075                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    4076                 :            : {
    4077                 :            :         uint32_t pkt_idx = 0;
    4078                 :            :         uint16_t slot_idx = 0;
    4079                 :            :         uint16_t nr_done_pkts = 0;
    4080                 :            :         uint16_t pkt_err = 0;
    4081                 :            :         uint32_t n_xfer;
    4082                 :            :         uint16_t i;
    4083                 :          0 :         struct vhost_async *async = vq->async;
    4084                 :          0 :         struct async_inflight_info *pkts_info = async->pkts_info;
    4085                 :            :         struct rte_mbuf *pkts_prealloc[MAX_PKT_BURST];
    4086                 :            : 
    4087                 :            :         VHOST_DATA_LOG(dev->ifname, DEBUG, "(%d) about to dequeue %u buffers", dev->vid, count);
    4088                 :            : 
    4089                 :            :         async_iter_reset(async);
    4090                 :            : 
    4091   [ #  #  #  # ]:          0 :         if (rte_pktmbuf_alloc_bulk(mbuf_pool, pkts_prealloc, count)) {
    4092                 :          0 :                 vq->stats.mbuf_alloc_failed += count;
    4093                 :          0 :                 goto out;
    4094                 :            :         }
    4095                 :            : 
    4096                 :            :         do {
    4097                 :          0 :                 struct rte_mbuf *pkt = pkts_prealloc[pkt_idx];
    4098                 :            : 
    4099                 :          0 :                 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
    4100                 :            : 
    4101                 :          0 :                 slot_idx = (async->pkts_idx + pkt_idx) % vq->size;
    4102   [ #  #  #  # ]:          0 :                 if (count - pkt_idx >= PACKED_BATCH_SIZE) {
    4103   [ #  #  #  # ]:          0 :                         if (!virtio_dev_tx_async_packed_batch(dev, vq, &pkts_prealloc[pkt_idx],
    4104                 :            :                                                 slot_idx, dma_id, vchan_id)) {
    4105   [ #  #  #  # ]:          0 :                                 for (i = 0; i < PACKED_BATCH_SIZE; i++) {
    4106                 :          0 :                                         slot_idx = (async->pkts_idx + pkt_idx) % vq->size;
    4107                 :          0 :                                         pkts_info[slot_idx].descs = 1;
    4108                 :          0 :                                         pkts_info[slot_idx].nr_buffers = 1;
    4109                 :          0 :                                         pkts_info[slot_idx].mbuf = pkts_prealloc[pkt_idx];
    4110                 :          0 :                                         pkt_idx++;
    4111                 :            :                                 }
    4112                 :          0 :                                 continue;
    4113                 :            :                         }
    4114                 :            :                 }
    4115                 :            : 
    4116   [ #  #  #  # ]:          0 :                 if (unlikely(virtio_dev_tx_async_single_packed(dev, vq, mbuf_pool, pkt,
    4117                 :            :                                 slot_idx, legacy_ol_flags))) {
    4118                 :          0 :                         rte_pktmbuf_free_bulk(&pkts_prealloc[pkt_idx], count - pkt_idx);
    4119                 :            : 
    4120   [ #  #  #  # ]:          0 :                         if (slot_idx == 0)
    4121                 :          0 :                                 slot_idx = vq->size - 1;
    4122                 :            :                         else
    4123                 :          0 :                                 slot_idx--;
    4124                 :            : 
    4125                 :            :                         break;
    4126                 :            :                 }
    4127                 :            : 
    4128                 :          0 :                 pkts_info[slot_idx].mbuf = pkt;
    4129                 :          0 :                 pkt_idx++;
    4130   [ #  #  #  # ]:          0 :         } while (pkt_idx < count);
    4131                 :            : 
    4132                 :          0 :         n_xfer = vhost_async_dma_transfer(dev, vq, dma_id, vchan_id, async->pkts_idx,
    4133                 :          0 :                                         async->iov_iter, pkt_idx);
    4134                 :            : 
    4135                 :          0 :         async->pkts_inflight_n += n_xfer;
    4136                 :            : 
    4137                 :          0 :         pkt_err = pkt_idx - n_xfer;
    4138                 :            : 
    4139   [ #  #  #  # ]:          0 :         if (unlikely(pkt_err)) {
    4140                 :            :                 uint16_t descs_err = 0;
    4141                 :            : 
    4142                 :          0 :                 pkt_idx -= pkt_err;
    4143                 :            : 
    4144                 :            :                 /**
    4145                 :            :                  * recover DMA-copy related structures and free pktmbuf for DMA-error pkts.
    4146                 :            :                  */
    4147   [ #  #  #  # ]:          0 :                 if (async->buffer_idx_packed >= pkt_err)
    4148                 :          0 :                         async->buffer_idx_packed -= pkt_err;
    4149                 :            :                 else
    4150                 :          0 :                         async->buffer_idx_packed += vq->size - pkt_err;
    4151                 :            : 
    4152   [ #  #  #  # ]:          0 :                 while (pkt_err-- > 0) {
    4153                 :          0 :                         rte_pktmbuf_free(pkts_info[slot_idx].mbuf);
    4154                 :          0 :                         descs_err += pkts_info[slot_idx].descs;
    4155                 :            : 
    4156   [ #  #  #  # ]:          0 :                         if (slot_idx == 0)
    4157                 :          0 :                                 slot_idx = vq->size - 1;
    4158                 :            :                         else
    4159                 :          0 :                                 slot_idx--;
    4160                 :            :                 }
    4161                 :            : 
    4162                 :            :                 /* recover available ring */
    4163   [ #  #  #  # ]:          0 :                 if (vq->last_avail_idx >= descs_err) {
    4164                 :          0 :                         vq->last_avail_idx -= descs_err;
    4165                 :            :                 } else {
    4166                 :          0 :                         vq->last_avail_idx += vq->size - descs_err;
    4167                 :          0 :                         vq->avail_wrap_counter ^= 1;
    4168                 :            :                 }
    4169                 :            :                 vhost_virtqueue_reconnect_log_packed(vq);
    4170                 :            :         }
    4171                 :            : 
    4172                 :          0 :         async->pkts_idx += pkt_idx;
    4173   [ #  #  #  # ]:          0 :         if (async->pkts_idx >= vq->size)
    4174                 :          0 :                 async->pkts_idx -= vq->size;
    4175                 :            : 
    4176                 :          0 : out:
    4177                 :          0 :         nr_done_pkts = async_poll_dequeue_completed(dev, vq, pkts, count,
    4178                 :            :                                         dma_id, vchan_id, legacy_ol_flags);
    4179                 :            : 
    4180                 :            :         return nr_done_pkts;
    4181                 :            : }
    4182                 :            : 
    4183                 :            : __rte_noinline
    4184                 :            : static uint16_t
    4185                 :          0 : virtio_dev_tx_async_packed_legacy(struct virtio_net *dev, struct vhost_virtqueue *vq,
    4186                 :            :                 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts,
    4187                 :            :                 uint16_t count, uint16_t dma_id, uint16_t vchan_id)
    4188                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    4189                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    4190                 :            : {
    4191                 :          0 :         return virtio_dev_tx_async_packed(dev, vq, mbuf_pool,
    4192                 :            :                                 pkts, count, dma_id, vchan_id, true);
    4193                 :            : }
    4194                 :            : 
    4195                 :            : __rte_noinline
    4196                 :            : static uint16_t
    4197                 :          0 : virtio_dev_tx_async_packed_compliant(struct virtio_net *dev, struct vhost_virtqueue *vq,
    4198                 :            :                 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts,
    4199                 :            :                 uint16_t count, uint16_t dma_id, uint16_t vchan_id)
    4200                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    4201                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    4202                 :            : {
    4203                 :          0 :         return virtio_dev_tx_async_packed(dev, vq, mbuf_pool,
    4204                 :            :                                 pkts, count, dma_id, vchan_id, false);
    4205                 :            : }
    4206                 :            : 
    4207                 :            : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_vhost_async_try_dequeue_burst, 22.07)
    4208                 :            : uint16_t
    4209         [ #  # ]:          0 : rte_vhost_async_try_dequeue_burst(int vid, uint16_t queue_id,
    4210                 :            :         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count,
    4211                 :            :         int *nr_inflight, int16_t dma_id, uint16_t vchan_id)
    4212                 :            : {
    4213                 :            :         struct virtio_net *dev;
    4214                 :            :         struct vhost_virtqueue *vq;
    4215                 :            :         int16_t success = 1;
    4216                 :            :         uint16_t nb_rx = 0;
    4217                 :            : 
    4218                 :            :         dev = get_device(vid);
    4219         [ #  # ]:          0 :         if (!dev || !nr_inflight)
    4220                 :          0 :                 goto out_no_unlock;
    4221                 :            : 
    4222                 :          0 :         *nr_inflight = -1;
    4223                 :            : 
    4224         [ #  # ]:          0 :         if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
    4225                 :          0 :                 VHOST_DATA_LOG(dev->ifname, ERR, "%s: built-in vhost net backend is disabled.",
    4226                 :            :                         __func__);
    4227                 :          0 :                 goto out_no_unlock;
    4228                 :            :         }
    4229                 :            : 
    4230   [ #  #  #  # ]:          0 :         if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
    4231                 :          0 :                 VHOST_DATA_LOG(dev->ifname, ERR, "%s: invalid virtqueue idx %d.",
    4232                 :            :                         __func__, queue_id);
    4233                 :          0 :                 goto out_no_unlock;
    4234                 :            :         }
    4235                 :            : 
    4236         [ #  # ]:          0 :         if (unlikely(dma_id < 0 || dma_id >= RTE_DMADEV_DEFAULT_MAX)) {
    4237                 :          0 :                 VHOST_DATA_LOG(dev->ifname, ERR, "%s: invalid dma id %d.",
    4238                 :            :                         __func__, dma_id);
    4239                 :          0 :                 goto out_no_unlock;
    4240                 :            :         }
    4241                 :            : 
    4242   [ #  #  #  # ]:          0 :         if (unlikely(!dma_copy_track[dma_id].vchans ||
    4243                 :            :                                 !dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr)) {
    4244                 :          0 :                 VHOST_DATA_LOG(dev->ifname, ERR, "%s: invalid channel %d:%u.",
    4245                 :            :                         __func__, dma_id, vchan_id);
    4246                 :          0 :                 goto out_no_unlock;
    4247                 :            :         }
    4248                 :            : 
    4249                 :          0 :         vq = dev->virtqueue[queue_id];
    4250                 :            : 
    4251         [ #  # ]:          0 :         if (unlikely(rte_rwlock_read_trylock(&vq->access_lock) != 0))
    4252                 :          0 :                 goto out_no_unlock;
    4253                 :            : 
    4254         [ #  # ]:          0 :         if (unlikely(vq->enabled == 0))
    4255                 :          0 :                 goto out_access_unlock;
    4256                 :            : 
    4257         [ #  # ]:          0 :         if (unlikely(!vq->async)) {
    4258                 :          0 :                 VHOST_DATA_LOG(dev->ifname, ERR, "%s: async not registered for queue id %d.",
    4259                 :            :                         __func__, queue_id);
    4260                 :          0 :                 goto out_access_unlock;
    4261                 :            :         }
    4262                 :            : 
    4263                 :            :         vhost_user_iotlb_rd_lock(vq);
    4264                 :            : 
    4265         [ #  # ]:          0 :         if (unlikely(vq->access_ok == 0)) {
    4266                 :            :                 vhost_user_iotlb_rd_unlock(vq);
    4267                 :            :                 rte_rwlock_read_unlock(&vq->access_lock);
    4268                 :            : 
    4269                 :          0 :                 virtio_dev_vring_translate(dev, vq);
    4270                 :          0 :                 goto out_no_unlock;
    4271                 :            :         }
    4272                 :            : 
    4273                 :            :         /*
    4274                 :            :          * Construct a RARP broadcast packet, and inject it to the "pkts"
    4275                 :            :          * array, to looks like that guest actually send such packet.
    4276                 :            :          *
    4277                 :            :          * Check user_send_rarp() for more information.
    4278                 :            :          *
    4279                 :            :          * broadcast_rarp shares a cacheline in the virtio_net structure
    4280                 :            :          * with some fields that are accessed during enqueue and
    4281                 :            :          * rte_atomic_compare_exchange_strong_explicit causes a write if performed compare
    4282                 :            :          * and exchange. This could result in false sharing between enqueue
    4283                 :            :          * and dequeue.
    4284                 :            :          *
    4285                 :            :          * Prevent unnecessary false sharing by reading broadcast_rarp first
    4286                 :            :          * and only performing compare and exchange if the read indicates it
    4287                 :            :          * is likely to be set.
    4288                 :            :          */
    4289   [ #  #  #  # ]:          0 :         if (unlikely(rte_atomic_load_explicit(&dev->broadcast_rarp, rte_memory_order_acquire) &&
    4290                 :            :                         rte_atomic_compare_exchange_strong_explicit(&dev->broadcast_rarp,
    4291                 :            :                         &success, 0, rte_memory_order_release, rte_memory_order_relaxed))) {
    4292                 :            :                 /*
    4293                 :            :                  * Inject the RARP packet to the head of "pkts" array,
    4294                 :            :                  * so that switch's mac learning table will get updated first.
    4295                 :            :                  */
    4296                 :          0 :                 pkts[nb_rx] = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
    4297         [ #  # ]:          0 :                 if (pkts[nb_rx] == NULL) {
    4298                 :          0 :                         VHOST_DATA_LOG(dev->ifname, ERR, "failed to make RARP packet.");
    4299                 :          0 :                         goto out;
    4300                 :            :                 }
    4301                 :            :                 nb_rx += 1;
    4302                 :            :         }
    4303                 :            : 
    4304         [ #  # ]:          0 :         if (vq_is_packed(dev)) {
    4305         [ #  # ]:          0 :                 if (dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS)
    4306                 :          0 :                         nb_rx += virtio_dev_tx_async_packed_legacy(dev, vq, mbuf_pool,
    4307                 :          0 :                                         pkts + nb_rx, count - nb_rx, dma_id, vchan_id);
    4308                 :            :                 else
    4309                 :          0 :                         nb_rx += virtio_dev_tx_async_packed_compliant(dev, vq, mbuf_pool,
    4310                 :          0 :                                         pkts + nb_rx, count - nb_rx, dma_id, vchan_id);
    4311                 :            :         } else {
    4312         [ #  # ]:          0 :                 if (dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS)
    4313                 :          0 :                         nb_rx += virtio_dev_tx_async_split_legacy(dev, vq, mbuf_pool,
    4314                 :          0 :                                         pkts + nb_rx, count - nb_rx, dma_id, vchan_id);
    4315                 :            :                 else
    4316                 :          0 :                         nb_rx += virtio_dev_tx_async_split_compliant(dev, vq, mbuf_pool,
    4317                 :          0 :                                         pkts + nb_rx, count - nb_rx, dma_id, vchan_id);
    4318                 :            :         }
    4319                 :            : 
    4320                 :          0 :         *nr_inflight = vq->async->pkts_inflight_n;
    4321                 :          0 :         vhost_queue_stats_update(dev, vq, pkts, nb_rx);
    4322                 :            : 
    4323                 :          0 : out:
    4324                 :            :         vhost_user_iotlb_rd_unlock(vq);
    4325                 :            : 
    4326                 :          0 : out_access_unlock:
    4327                 :            :         rte_rwlock_read_unlock(&vq->access_lock);
    4328                 :            : 
    4329                 :          0 : out_no_unlock:
    4330                 :          0 :         return nb_rx;
    4331                 :            : }

Generated by: LCOV version 1.14