LCOV - code coverage report
Current view: top level - lib/vhost - virtio_net.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 811 0.0 %
Date: 2026-11-01 19:11:22 Functions: 0 17 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 1419 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_vhost.h>
      16                 :            : #include <rte_tcp.h>
      17                 :            : #include <rte_udp.h>
      18                 :            : #include <rte_sctp.h>
      19                 :            : #include <rte_arp.h>
      20                 :            : #include <rte_malloc.h>
      21                 :            : 
      22                 :            : #include "iotlb.h"
      23                 :            : #include "vhost.h"
      24                 :            : 
      25                 :            : #define MAX_BATCH_LEN 256
      26                 :            : 
      27                 :            : static  __rte_always_inline bool
      28                 :            : rxvq_is_mergeable(struct virtio_net *dev)
      29                 :            : {
      30                 :          0 :         return dev->features & (1ULL << VIRTIO_NET_F_MRG_RXBUF);
      31                 :            : }
      32                 :            : 
      33                 :            : static  __rte_always_inline bool
      34                 :            : virtio_net_is_inorder(struct virtio_net *dev)
      35                 :            : {
      36                 :          0 :         return dev->features & (1ULL << VIRTIO_F_IN_ORDER);
      37                 :            : }
      38                 :            : 
      39                 :            : static bool
      40                 :            : is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t nr_vring)
      41                 :            : {
      42   [ #  #  #  # ]:          0 :         return (is_tx ^ (idx & 1)) == 0 && idx < nr_vring;
      43                 :            : }
      44                 :            : 
      45                 :            : static inline void
      46                 :          0 : vhost_queue_stats_update(const struct virtio_net *dev, struct vhost_virtqueue *vq,
      47                 :            :                 struct rte_mbuf **pkts, uint16_t count)
      48                 :            :         __rte_requires_shared_capability(&vq->access_lock)
      49                 :            : {
      50                 :            :         struct virtqueue_stats *stats = &vq->stats;
      51                 :            :         int i;
      52                 :            : 
      53         [ #  # ]:          0 :         if (!(dev->flags & VIRTIO_DEV_STATS_ENABLED))
      54                 :            :                 return;
      55                 :            : 
      56         [ #  # ]:          0 :         for (i = 0; i < count; i++) {
      57                 :            :                 const struct rte_ether_addr *ea;
      58                 :          0 :                 const struct rte_mbuf *pkt = pkts[i];
      59                 :          0 :                 uint32_t pkt_len = rte_pktmbuf_pkt_len(pkt);
      60                 :            : 
      61                 :          0 :                 stats->packets++;
      62                 :          0 :                 stats->bytes += pkt_len;
      63                 :            : 
      64         [ #  # ]:          0 :                 if (pkt_len >= 1024)
      65         [ #  # ]:          0 :                         stats->size_bins[6 + (pkt_len > 1518)]++;
      66         [ #  # ]:          0 :                 else if (pkt_len <= 64)
      67                 :          0 :                         stats->size_bins[pkt_len >> 6]++;
      68                 :            :                 else
      69                 :          0 :                         stats->size_bins[32UL - rte_clz32(pkt_len) - 5]++;
      70                 :            : 
      71         [ #  # ]:          0 :                 ea = rte_pktmbuf_mtod(pkt, const struct rte_ether_addr *);
      72                 :            :                 RTE_BUILD_BUG_ON(offsetof(struct virtqueue_stats, broadcast) !=
      73                 :            :                                 offsetof(struct virtqueue_stats, multicast) + sizeof(uint64_t));
      74         [ #  # ]:          0 :                 if (unlikely(rte_is_multicast_ether_addr(ea)))
      75                 :          0 :                         (&stats->multicast)[rte_is_broadcast_ether_addr(ea)]++;
      76                 :            :         }
      77                 :            : }
      78                 :            : 
      79                 :            : static __rte_always_inline bool
      80                 :            : vhost_can_batch_copy(struct vhost_virtqueue *vq, uint32_t cpy_len)
      81                 :            : {
      82   [ #  #  #  #  :          0 :         return cpy_len <= MAX_BATCH_LEN && vq->batch_copy_nb_elems < vq->size;
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
      83                 :            : }
      84                 :            : 
      85                 :            : static inline void
      86                 :          0 : do_data_copy_enqueue(struct virtio_net *dev, struct vhost_virtqueue *vq)
      87                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
      88                 :            : {
      89                 :          0 :         struct batch_copy_elem *elem = vq->batch_copy_elems;
      90                 :          0 :         uint16_t count = vq->batch_copy_nb_elems;
      91                 :            :         int i;
      92                 :            : 
      93         [ #  # ]:          0 :         for (i = 0; i < count; i++) {
      94         [ #  # ]:          0 :                 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
      95                 :          0 :                 vhost_log_cache_write_iova(dev, vq, elem[i].log_addr,
      96         [ #  # ]:          0 :                                            elem[i].len);
      97                 :            :                 PRINT_PACKET(dev, (uintptr_t)elem[i].dst, elem[i].len, 0);
      98                 :            :         }
      99                 :            : 
     100                 :          0 :         vq->batch_copy_nb_elems = 0;
     101                 :          0 : }
     102                 :            : 
     103                 :            : static inline void
     104                 :          0 : do_data_copy_dequeue(struct vhost_virtqueue *vq)
     105                 :            : {
     106                 :          0 :         struct batch_copy_elem *elem = vq->batch_copy_elems;
     107                 :          0 :         uint16_t count = vq->batch_copy_nb_elems;
     108                 :            :         int i;
     109                 :            : 
     110         [ #  # ]:          0 :         for (i = 0; i < count; i++)
     111         [ #  # ]:          0 :                 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
     112                 :            : 
     113                 :          0 :         vq->batch_copy_nb_elems = 0;
     114                 :          0 : }
     115                 :            : 
     116                 :            : static __rte_always_inline void
     117                 :            : do_flush_shadow_used_ring_split(struct virtio_net *dev,
     118                 :            :                         struct vhost_virtqueue *vq,
     119                 :            :                         uint16_t to, uint16_t from, uint16_t size)
     120                 :            : {
     121                 :          0 :         rte_memcpy(&vq->used->ring[to],
     122   [ #  #  #  #  :          0 :                         &vq->shadow_used_split[from],
                   #  # ]
     123                 :            :                         size * sizeof(struct vring_used_elem));
     124   [ #  #  #  #  :          0 :         vhost_log_cache_used_vring(dev, vq,
          #  #  #  #  #  
                #  #  # ]
     125                 :            :                         offsetof(struct vring_used, ring[to]),
     126                 :            :                         size * sizeof(struct vring_used_elem));
     127                 :            : }
     128                 :            : 
     129                 :            : static __rte_always_inline void
     130                 :            : flush_shadow_used_ring_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
     131                 :            : {
     132                 :          0 :         uint16_t used_idx = vq->last_used_idx & (vq->size - 1);
     133                 :            : 
     134   [ #  #  #  #  :          0 :         if (used_idx + vq->shadow_used_idx <= vq->size) {
                   #  # ]
     135                 :            :                 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0,
     136                 :            :                                           vq->shadow_used_idx);
     137                 :            :         } else {
     138                 :            :                 uint16_t size;
     139                 :            : 
     140                 :            :                 /* update used ring interval [used_idx, vq->size] */
     141   [ #  #  #  #  :          0 :                 size = vq->size - used_idx;
                   #  # ]
     142                 :            :                 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0, size);
     143                 :            : 
     144                 :            :                 /* update the left half used ring interval [0, left_size] */
     145                 :            :                 do_flush_shadow_used_ring_split(dev, vq, 0, size,
     146   [ #  #  #  #  :          0 :                                           vq->shadow_used_idx - size);
                   #  # ]
     147                 :            :         }
     148   [ #  #  #  #  :          0 :         vq->last_used_idx += vq->shadow_used_idx;
                   #  # ]
     149                 :            : 
     150                 :            :         vhost_log_cache_sync(dev, vq);
     151                 :            : 
     152                 :          0 :         rte_atomic_fetch_add_explicit((unsigned short __rte_atomic *)&vq->used->idx,
     153                 :            :                 vq->shadow_used_idx, rte_memory_order_release);
     154   [ #  #  #  #  :          0 :         vq->shadow_used_idx = 0;
                   #  # ]
     155                 :            :         vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
     156                 :            :                 sizeof(vq->used->idx));
     157                 :            : }
     158                 :            : 
     159                 :            : static __rte_always_inline void
     160                 :            : update_shadow_used_ring_split(struct vhost_virtqueue *vq,
     161                 :            :                          uint16_t desc_idx, uint32_t len)
     162                 :            : {
     163                 :          0 :         uint16_t i = vq->shadow_used_idx++;
     164                 :            : 
     165                 :          0 :         vq->shadow_used_split[i].id  = desc_idx;
     166                 :          0 :         vq->shadow_used_split[i].len = len;
     167                 :            : }
     168                 :            : 
     169                 :            : static __rte_always_inline void
     170                 :            : vhost_flush_enqueue_shadow_packed(struct virtio_net *dev,
     171                 :            :                                   struct vhost_virtqueue *vq)
     172                 :            : {
     173                 :            :         int i;
     174                 :          0 :         uint16_t used_idx = vq->last_used_idx;
     175                 :            :         uint16_t head_idx = vq->last_used_idx;
     176                 :            :         uint16_t head_flags = 0;
     177                 :            : 
     178                 :            :         /* Split loop in two to save memory barriers */
     179   [ #  #  #  #  :          0 :         for (i = 0; i < vq->shadow_used_idx; i++) {
                   #  # ]
     180                 :          0 :                 vq->desc_packed[used_idx].id = vq->shadow_used_packed[i].id;
     181                 :          0 :                 vq->desc_packed[used_idx].len = vq->shadow_used_packed[i].len;
     182                 :            : 
     183                 :          0 :                 used_idx += vq->shadow_used_packed[i].count;
     184   [ #  #  #  #  :          0 :                 if (used_idx >= vq->size)
                   #  # ]
     185                 :          0 :                         used_idx -= vq->size;
     186                 :            :         }
     187                 :            : 
     188                 :            :         /* The ordering for storing desc flags needs to be enforced. */
     189                 :            :         rte_atomic_thread_fence(rte_memory_order_release);
     190                 :            : 
     191   [ #  #  #  #  :          0 :         for (i = 0; i < vq->shadow_used_idx; i++) {
                   #  # ]
     192                 :            :                 uint16_t flags;
     193                 :            : 
     194   [ #  #  #  #  :          0 :                 if (vq->shadow_used_packed[i].len)
                   #  # ]
     195                 :            :                         flags = VRING_DESC_F_WRITE;
     196                 :            :                 else
     197                 :            :                         flags = 0;
     198                 :            : 
     199   [ #  #  #  #  :          0 :                 if (vq->used_wrap_counter) {
                   #  # ]
     200                 :            :                         flags |= VRING_DESC_F_USED;
     201                 :          0 :                         flags |= VRING_DESC_F_AVAIL;
     202                 :            :                 } else {
     203                 :            :                         flags &= ~VRING_DESC_F_USED;
     204                 :            :                         flags &= ~VRING_DESC_F_AVAIL;
     205                 :            :                 }
     206                 :            : 
     207   [ #  #  #  #  :          0 :                 if (i > 0) {
                   #  # ]
     208                 :          0 :                         vq->desc_packed[vq->last_used_idx].flags = flags;
     209                 :            : 
     210                 :          0 :                         vhost_log_cache_used_vring(dev, vq,
     211   [ #  #  #  #  :          0 :                                         vq->last_used_idx *
                   #  # ]
     212                 :            :                                         sizeof(struct vring_packed_desc),
     213                 :            :                                         sizeof(struct vring_packed_desc));
     214                 :            :                 } else {
     215                 :          0 :                         head_idx = vq->last_used_idx;
     216                 :            :                         head_flags = flags;
     217                 :            :                 }
     218                 :            : 
     219   [ #  #  #  #  :          0 :                 vq_inc_last_used_packed(vq, vq->shadow_used_packed[i].count);
                   #  # ]
     220                 :            :         }
     221                 :            : 
     222   [ #  #  #  #  :          0 :         vq->desc_packed[head_idx].flags = head_flags;
                   #  # ]
     223                 :            : 
     224                 :            :         vhost_log_cache_used_vring(dev, vq,
     225                 :            :                                 head_idx *
     226                 :            :                                 sizeof(struct vring_packed_desc),
     227                 :            :                                 sizeof(struct vring_packed_desc));
     228                 :            : 
     229   [ #  #  #  #  :          0 :         vq->shadow_used_idx = 0;
                   #  # ]
     230                 :            :         vhost_log_cache_sync(dev, vq);
     231                 :            : }
     232                 :            : 
     233                 :            : static __rte_always_inline void
     234                 :            : vhost_flush_dequeue_shadow_packed(struct virtio_net *dev,
     235                 :            :                                   struct vhost_virtqueue *vq)
     236                 :            : {
     237                 :          0 :         struct vring_used_elem_packed *used_elem = &vq->shadow_used_packed[0];
     238                 :            : 
     239                 :          0 :         vq->desc_packed[vq->shadow_last_used_idx].id = used_elem->id;
     240                 :            :         /* desc flags is the synchronization point for virtio packed vring */
     241                 :          0 :         rte_atomic_store_explicit(
     242                 :            :                 (unsigned short __rte_atomic *)&vq->desc_packed[vq->shadow_last_used_idx].flags,
     243                 :            :                 used_elem->flags, rte_memory_order_release);
     244                 :            : 
     245   [ #  #  #  # ]:          0 :         vhost_log_cache_used_vring(dev, vq, vq->shadow_last_used_idx *
     246                 :            :                                    sizeof(struct vring_packed_desc),
     247                 :            :                                    sizeof(struct vring_packed_desc));
     248   [ #  #  #  # ]:          0 :         vq->shadow_used_idx = 0;
     249                 :            :         vhost_log_cache_sync(dev, vq);
     250                 :            : }
     251                 :            : 
     252                 :            : static __rte_always_inline void
     253                 :            : vhost_flush_enqueue_batch_packed(struct virtio_net *dev,
     254                 :            :                                  struct vhost_virtqueue *vq,
     255                 :            :                                  uint64_t *lens,
     256                 :            :                                  uint16_t *ids)
     257                 :            : {
     258                 :            :         uint16_t i;
     259                 :            :         uint16_t flags;
     260                 :            :         uint16_t last_used_idx;
     261                 :            :         struct vring_packed_desc *desc_base;
     262                 :            : 
     263                 :          0 :         last_used_idx = vq->last_used_idx;
     264                 :          0 :         desc_base = &vq->desc_packed[last_used_idx];
     265                 :            : 
     266         [ #  # ]:          0 :         flags = PACKED_DESC_ENQUEUE_USED_FLAG(vq->used_wrap_counter);
     267                 :            : 
     268         [ #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
     269                 :          0 :                 desc_base[i].id = ids[i];
     270                 :          0 :                 desc_base[i].len = lens[i];
     271                 :            :         }
     272                 :            : 
     273                 :            :         rte_atomic_thread_fence(rte_memory_order_release);
     274                 :            : 
     275         [ #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
     276                 :          0 :                 desc_base[i].flags = flags;
     277                 :            :         }
     278                 :            : 
     279                 :            :         vhost_log_cache_used_vring(dev, vq, last_used_idx *
     280                 :            :                                    sizeof(struct vring_packed_desc),
     281                 :            :                                    sizeof(struct vring_packed_desc) *
     282                 :            :                                    PACKED_BATCH_SIZE);
     283                 :            :         vhost_log_cache_sync(dev, vq);
     284                 :            : 
     285                 :            :         vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
     286                 :            : }
     287                 :            : 
     288                 :            : static __rte_always_inline void
     289                 :            : vhost_shadow_dequeue_batch_packed_inorder(struct vhost_virtqueue *vq,
     290                 :            :                                           uint16_t id)
     291                 :            : {
     292                 :          0 :         vq->shadow_used_packed[0].id = id;
     293                 :            : 
     294                 :          0 :         if (!vq->shadow_used_idx) {
     295                 :          0 :                 vq->shadow_last_used_idx = vq->last_used_idx;
     296                 :          0 :                 vq->shadow_used_packed[0].flags =
     297   [ #  #  #  # ]:          0 :                         PACKED_DESC_DEQUEUE_USED_FLAG(vq->used_wrap_counter);
     298                 :          0 :                 vq->shadow_used_packed[0].len = 0;
     299                 :          0 :                 vq->shadow_used_packed[0].count = 1;
     300                 :          0 :                 vq->shadow_used_idx++;
     301                 :            :         }
     302                 :            : 
     303                 :            :         vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
     304                 :            : }
     305                 :            : 
     306                 :            : static __rte_always_inline void
     307                 :            : vhost_shadow_dequeue_batch_packed(struct virtio_net *dev,
     308                 :            :                                   struct vhost_virtqueue *vq,
     309                 :            :                                   uint16_t *ids)
     310                 :            : {
     311                 :            :         uint16_t flags;
     312                 :            :         uint16_t i;
     313                 :            :         uint16_t begin;
     314                 :            : 
     315   [ #  #  #  # ]:          0 :         flags = PACKED_DESC_DEQUEUE_USED_FLAG(vq->used_wrap_counter);
     316                 :            : 
     317   [ #  #  #  # ]:          0 :         if (!vq->shadow_used_idx) {
     318                 :          0 :                 vq->shadow_last_used_idx = vq->last_used_idx;
     319                 :          0 :                 vq->shadow_used_packed[0].id  = ids[0];
     320                 :          0 :                 vq->shadow_used_packed[0].len = 0;
     321                 :          0 :                 vq->shadow_used_packed[0].count = 1;
     322                 :          0 :                 vq->shadow_used_packed[0].flags = flags;
     323                 :          0 :                 vq->shadow_used_idx++;
     324                 :            :                 begin = 1;
     325                 :            :         } else
     326                 :            :                 begin = 0;
     327                 :            : 
     328   [ #  #  #  # ]:          0 :         vhost_for_each_try_unroll(i, begin, PACKED_BATCH_SIZE) {
     329                 :          0 :                 vq->desc_packed[vq->last_used_idx + i].id = ids[i];
     330                 :          0 :                 vq->desc_packed[vq->last_used_idx + i].len = 0;
     331                 :            :         }
     332                 :            : 
     333                 :            :         rte_atomic_thread_fence(rte_memory_order_release);
     334   [ #  #  #  # ]:          0 :         vhost_for_each_try_unroll(i, begin, PACKED_BATCH_SIZE)
     335                 :          0 :                 vq->desc_packed[vq->last_used_idx + i].flags = flags;
     336                 :            : 
     337   [ #  #  #  # ]:          0 :         vhost_log_cache_used_vring(dev, vq, vq->last_used_idx *
     338                 :            :                                    sizeof(struct vring_packed_desc),
     339                 :            :                                    sizeof(struct vring_packed_desc) *
     340                 :            :                                    PACKED_BATCH_SIZE);
     341                 :            :         vhost_log_cache_sync(dev, vq);
     342                 :            : 
     343                 :            :         vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
     344                 :            : }
     345                 :            : 
     346                 :            : static __rte_always_inline void
     347                 :            : vhost_shadow_dequeue_single_packed(struct vhost_virtqueue *vq,
     348                 :            :                                    uint16_t buf_id,
     349                 :            :                                    uint16_t count)
     350                 :            : {
     351                 :            :         uint16_t flags;
     352                 :            : 
     353                 :          0 :         flags = vq->desc_packed[vq->last_used_idx].flags;
     354   [ #  #  #  # ]:          0 :         if (vq->used_wrap_counter) {
     355                 :            :                 flags |= VRING_DESC_F_USED;
     356                 :          0 :                 flags |= VRING_DESC_F_AVAIL;
     357                 :            :         } else {
     358                 :            :                 flags &= ~VRING_DESC_F_USED;
     359                 :          0 :                 flags &= ~VRING_DESC_F_AVAIL;
     360                 :            :         }
     361                 :            : 
     362   [ #  #  #  # ]:          0 :         if (!vq->shadow_used_idx) {
     363                 :          0 :                 vq->shadow_last_used_idx = vq->last_used_idx;
     364                 :            : 
     365                 :          0 :                 vq->shadow_used_packed[0].id  = buf_id;
     366                 :          0 :                 vq->shadow_used_packed[0].len = 0;
     367                 :          0 :                 vq->shadow_used_packed[0].flags = flags;
     368                 :          0 :                 vq->shadow_used_idx++;
     369                 :            :         } else {
     370                 :          0 :                 vq->desc_packed[vq->last_used_idx].id = buf_id;
     371                 :          0 :                 vq->desc_packed[vq->last_used_idx].len = 0;
     372                 :          0 :                 vq->desc_packed[vq->last_used_idx].flags = flags;
     373                 :            :         }
     374                 :            : 
     375                 :            :         vq_inc_last_used_packed(vq, count);
     376                 :            : }
     377                 :            : 
     378                 :            : static __rte_always_inline void
     379                 :            : vhost_shadow_dequeue_single_packed_inorder(struct vhost_virtqueue *vq,
     380                 :            :                                            uint16_t buf_id,
     381                 :            :                                            uint16_t count)
     382                 :            : {
     383                 :            :         uint16_t flags;
     384                 :            : 
     385                 :          0 :         vq->shadow_used_packed[0].id = buf_id;
     386                 :            : 
     387                 :          0 :         flags = vq->desc_packed[vq->last_used_idx].flags;
     388   [ #  #  #  # ]:          0 :         if (vq->used_wrap_counter) {
     389                 :            :                 flags |= VRING_DESC_F_USED;
     390                 :          0 :                 flags |= VRING_DESC_F_AVAIL;
     391                 :            :         } else {
     392                 :            :                 flags &= ~VRING_DESC_F_USED;
     393                 :          0 :                 flags &= ~VRING_DESC_F_AVAIL;
     394                 :            :         }
     395                 :            : 
     396   [ #  #  #  # ]:          0 :         if (!vq->shadow_used_idx) {
     397                 :          0 :                 vq->shadow_last_used_idx = vq->last_used_idx;
     398                 :          0 :                 vq->shadow_used_packed[0].len = 0;
     399                 :          0 :                 vq->shadow_used_packed[0].flags = flags;
     400                 :          0 :                 vq->shadow_used_idx++;
     401                 :            :         }
     402                 :            : 
     403                 :            :         vq_inc_last_used_packed(vq, count);
     404                 :            : }
     405                 :            : 
     406                 :            : static __rte_always_inline void
     407                 :            : vhost_shadow_enqueue_packed(struct vhost_virtqueue *vq,
     408                 :            :                                    uint32_t *len,
     409                 :            :                                    uint16_t *id,
     410                 :            :                                    uint16_t *count,
     411                 :            :                                    uint16_t num_buffers)
     412                 :            : {
     413                 :            :         uint16_t i;
     414                 :            : 
     415         [ #  # ]:          0 :         for (i = 0; i < num_buffers; i++) {
     416                 :            :                 /* enqueue shadow flush action aligned with batch num */
     417         [ #  # ]:          0 :                 if (!vq->shadow_used_idx)
     418                 :          0 :                         vq->shadow_aligned_idx = vq->last_used_idx &
     419                 :            :                                 PACKED_BATCH_MASK;
     420                 :          0 :                 vq->shadow_used_packed[vq->shadow_used_idx].id  = id[i];
     421                 :          0 :                 vq->shadow_used_packed[vq->shadow_used_idx].len = len[i];
     422                 :          0 :                 vq->shadow_used_packed[vq->shadow_used_idx].count = count[i];
     423                 :          0 :                 vq->shadow_aligned_idx += count[i];
     424                 :          0 :                 vq->shadow_used_idx++;
     425                 :            :         }
     426                 :            : }
     427                 :            : 
     428                 :            : static __rte_always_inline void
     429                 :            : vhost_shadow_enqueue_single_packed(struct virtio_net *dev,
     430                 :            :                                    struct vhost_virtqueue *vq,
     431                 :            :                                    uint32_t *len,
     432                 :            :                                    uint16_t *id,
     433                 :            :                                    uint16_t *count,
     434                 :            :                                    uint16_t num_buffers)
     435                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
     436                 :            : {
     437                 :            :         vhost_shadow_enqueue_packed(vq, len, id, count, num_buffers);
     438                 :            : 
     439         [ #  # ]:          0 :         if (vq->shadow_aligned_idx >= PACKED_BATCH_SIZE) {
     440                 :          0 :                 do_data_copy_enqueue(dev, vq);
     441                 :            :                 vhost_flush_enqueue_shadow_packed(dev, vq);
     442                 :            :         }
     443                 :            : }
     444                 :            : 
     445                 :            : /* avoid write operation when necessary, to lessen cache issues */
     446                 :            : #define ASSIGN_UNLESS_EQUAL(var, val) do {      \
     447                 :            :         if ((var) != (val))                     \
     448                 :            :                 (var) = (val);                  \
     449                 :            : } while (0)
     450                 :            : 
     451                 :            : static __rte_always_inline void
     452                 :            : virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
     453                 :            : {
     454                 :          0 :         uint64_t csum_l4 = m_buf->ol_flags & RTE_MBUF_F_TX_L4_MASK;
     455                 :            : 
     456   [ #  #  #  # ]:          0 :         if (m_buf->ol_flags & RTE_MBUF_F_TX_TCP_SEG)
     457                 :          0 :                 csum_l4 |= RTE_MBUF_F_TX_TCP_CKSUM;
     458                 :            : 
     459   [ #  #  #  #  :          0 :         if (csum_l4) {
          #  #  #  #  #  
                      # ]
     460                 :            :                 /*
     461                 :            :                  * Pseudo-header checksum must be set as per Virtio spec.
     462                 :            :                  *
     463                 :            :                  * Note: We don't propagate rte_net_intel_cksum_prepare()
     464                 :            :                  * errors, as it would have an impact on performance, and an
     465                 :            :                  * error would mean the packet is dropped by the guest instead
     466                 :            :                  * of being dropped here.
     467                 :            :                  */
     468                 :          0 :                 rte_net_intel_cksum_prepare(m_buf);
     469                 :            : 
     470                 :          0 :                 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
     471                 :          0 :                 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
     472                 :            : 
     473   [ #  #  #  #  :          0 :                 switch (csum_l4) {
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
     474                 :          0 :                 case RTE_MBUF_F_TX_TCP_CKSUM:
     475                 :          0 :                         net_hdr->csum_offset = (offsetof(struct rte_tcp_hdr,
     476                 :            :                                                 cksum));
     477                 :          0 :                         break;
     478                 :          0 :                 case RTE_MBUF_F_TX_UDP_CKSUM:
     479                 :          0 :                         net_hdr->csum_offset = (offsetof(struct rte_udp_hdr,
     480                 :            :                                                 dgram_cksum));
     481                 :          0 :                         break;
     482                 :          0 :                 case RTE_MBUF_F_TX_SCTP_CKSUM:
     483                 :          0 :                         net_hdr->csum_offset = (offsetof(struct rte_sctp_hdr,
     484                 :            :                                                 cksum));
     485                 :          0 :                         break;
     486                 :            :                 }
     487                 :            :         } else {
     488   [ #  #  #  #  :          0 :                 ASSIGN_UNLESS_EQUAL(net_hdr->csum_start, 0);
                   #  # ]
     489   [ #  #  #  #  :          0 :                 ASSIGN_UNLESS_EQUAL(net_hdr->csum_offset, 0);
                   #  # ]
     490   [ #  #  #  #  :          0 :                 ASSIGN_UNLESS_EQUAL(net_hdr->flags, 0);
                   #  # ]
     491                 :            :         }
     492                 :            : 
     493                 :            :         /* IP cksum verification cannot be bypassed, then calculate here */
     494   [ #  #  #  #  :          0 :         if (m_buf->ol_flags & RTE_MBUF_F_TX_IP_CKSUM) {
          #  #  #  #  #  
                      # ]
     495                 :            :                 struct rte_ipv4_hdr *ipv4_hdr;
     496                 :            : 
     497                 :          0 :                 ipv4_hdr = rte_pktmbuf_mtod_offset(m_buf, struct rte_ipv4_hdr *,
     498                 :            :                                                    m_buf->l2_len);
     499                 :          0 :                 ipv4_hdr->hdr_checksum = 0;
     500                 :          0 :                 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
     501                 :            :         }
     502                 :            : 
     503   [ #  #  #  #  :          0 :         if (m_buf->ol_flags & RTE_MBUF_F_TX_TCP_SEG) {
          #  #  #  #  #  
                      # ]
     504   [ #  #  #  #  :          0 :                 if (m_buf->ol_flags & RTE_MBUF_F_TX_IPV4)
          #  #  #  #  #  
                      # ]
     505                 :          0 :                         net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
     506                 :            :                 else
     507                 :          0 :                         net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
     508                 :          0 :                 net_hdr->gso_size = m_buf->tso_segsz;
     509                 :          0 :                 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
     510                 :          0 :                                         + m_buf->l4_len;
     511   [ #  #  #  #  :          0 :         } else if (m_buf->ol_flags & RTE_MBUF_F_TX_UDP_SEG) {
          #  #  #  #  #  
                      # ]
     512                 :          0 :                 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
     513                 :          0 :                 net_hdr->gso_size = m_buf->tso_segsz;
     514                 :          0 :                 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len +
     515                 :          0 :                         m_buf->l4_len;
     516                 :            :         } else {
     517   [ #  #  #  #  :          0 :                 ASSIGN_UNLESS_EQUAL(net_hdr->gso_type, 0);
          #  #  #  #  #  
                      # ]
     518   [ #  #  #  #  :          0 :                 ASSIGN_UNLESS_EQUAL(net_hdr->gso_size, 0);
          #  #  #  #  #  
                      # ]
     519   [ #  #  #  #  :          0 :                 ASSIGN_UNLESS_EQUAL(net_hdr->hdr_len, 0);
          #  #  #  #  #  
                      # ]
     520                 :            :         }
     521                 :            : }
     522                 :            : 
     523                 :            : static __rte_always_inline int
     524                 :            : map_one_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
     525                 :            :                 struct buf_vector *buf_vec, uint16_t *vec_idx,
     526                 :            :                 uint64_t desc_iova, uint64_t desc_len, uint8_t perm)
     527                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
     528                 :            : {
     529                 :            :         uint16_t vec_id = *vec_idx;
     530                 :            : 
     531   [ #  #  #  #  :          0 :         while (desc_len) {
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     532                 :            :                 uint64_t desc_addr;
     533                 :          0 :                 uint64_t desc_chunck_len = desc_len;
     534                 :            : 
     535   [ #  #  #  #  :          0 :                 if (unlikely(vec_id >= BUF_VECTOR_MAX))
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     536                 :          0 :                         return -1;
     537                 :            : 
     538                 :            :                 desc_addr = vhost_iova_to_vva(dev, vq,
     539                 :            :                                 desc_iova,
     540                 :            :                                 &desc_chunck_len,
     541                 :            :                                 perm);
     542   [ #  #  #  #  :          0 :                 if (unlikely(!desc_addr))
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     543                 :            :                         return -1;
     544                 :            : 
     545                 :          0 :                 rte_prefetch0((void *)(uintptr_t)desc_addr);
     546                 :            : 
     547                 :          0 :                 buf_vec[vec_id].buf_iova = desc_iova;
     548                 :          0 :                 buf_vec[vec_id].buf_addr = desc_addr;
     549                 :          0 :                 buf_vec[vec_id].buf_len  = desc_chunck_len;
     550                 :            : 
     551                 :          0 :                 desc_len -= desc_chunck_len;
     552                 :          0 :                 desc_iova += desc_chunck_len;
     553                 :          0 :                 vec_id++;
     554                 :            :         }
     555                 :            :         *vec_idx = vec_id;
     556                 :            : 
     557                 :            :         return 0;
     558                 :            : }
     559                 :            : 
     560                 :            : static __rte_always_inline int
     561                 :            : fill_vec_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
     562                 :            :                          uint32_t avail_idx, uint16_t *vec_idx,
     563                 :            :                          struct buf_vector *buf_vec, uint16_t *desc_chain_head,
     564                 :            :                          uint32_t *desc_chain_len, uint8_t perm)
     565                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
     566                 :            : {
     567                 :          0 :         uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
     568                 :            :         uint16_t vec_id = *vec_idx;
     569                 :            :         uint32_t len    = 0;
     570                 :            :         uint64_t dlen;
     571                 :          0 :         uint32_t nr_descs = vq->size;
     572                 :            :         uint32_t cnt    = 0;
     573                 :          0 :         struct vring_desc *descs = vq->desc;
     574                 :            :         struct vring_desc *idesc = NULL;
     575                 :            : 
     576                 :          0 :         if (unlikely(idx >= vq->size))
     577                 :            :                 return -1;
     578                 :            : 
     579                 :            :         *desc_chain_head = idx;
     580                 :            : 
     581   [ #  #  #  #  :          0 :         if (vq->desc[idx].flags & VRING_DESC_F_INDIRECT) {
                   #  # ]
     582                 :          0 :                 uint64_t desc_len = vq->desc[idx].len;
     583                 :            : 
     584                 :          0 :                 dlen = desc_len;
     585                 :          0 :                 nr_descs = dlen / sizeof(struct vring_desc);
     586   [ #  #  #  #  :          0 :                 if (unlikely(nr_descs > vq->size))
                   #  # ]
     587                 :            :                         return -1;
     588                 :            : 
     589                 :          0 :                 descs = (struct vring_desc *)(uintptr_t)
     590   [ #  #  #  #  :          0 :                         vhost_iova_to_vva(dev, vq, vq->desc[idx].addr,
                   #  # ]
     591                 :            :                                                 &dlen,
     592                 :            :                                                 VHOST_ACCESS_RO);
     593   [ #  #  #  #  :          0 :                 if (unlikely(!descs))
                   #  # ]
     594                 :            :                         return -1;
     595                 :            : 
     596   [ #  #  #  #  :          0 :                 if (unlikely(dlen < desc_len)) {
                   #  # ]
     597                 :            :                         /*
     598                 :            :                          * The indirect desc table is not contiguous
     599                 :            :                          * in process VA space, we have to copy it.
     600                 :            :                          */
     601                 :          0 :                         idesc = vhost_alloc_copy_ind_table(dev, vq,
     602                 :          0 :                                         vq->desc[idx].addr, desc_len);
     603   [ #  #  #  #  :          0 :                         if (unlikely(!idesc))
                   #  # ]
     604                 :            :                                 return -1;
     605                 :            : 
     606                 :            :                         descs = idesc;
     607                 :            :                 }
     608                 :            : 
     609                 :            :                 idx = 0;
     610                 :            :         }
     611                 :            : 
     612                 :            :         while (1) {
     613   [ #  #  #  #  :          0 :                 if (unlikely(idx >= nr_descs || cnt++ >= nr_descs)) {
          #  #  #  #  #  
                #  #  # ]
     614                 :            :                         free_ind_table(idesc);
     615                 :          0 :                         return -1;
     616                 :            :                 }
     617                 :            : 
     618                 :          0 :                 dlen = descs[idx].len;
     619                 :          0 :                 len += dlen;
     620                 :            : 
     621   [ #  #  #  #  :          0 :                 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
                   #  # ]
     622                 :            :                                                 descs[idx].addr, dlen,
     623                 :            :                                                 perm))) {
     624                 :            :                         free_ind_table(idesc);
     625                 :          0 :                         return -1;
     626                 :            :                 }
     627                 :            : 
     628   [ #  #  #  #  :          0 :                 if ((descs[idx].flags & VRING_DESC_F_NEXT) == 0)
                   #  # ]
     629                 :            :                         break;
     630                 :            : 
     631                 :          0 :                 idx = descs[idx].next;
     632                 :            :         }
     633                 :            : 
     634                 :            :         *desc_chain_len = len;
     635                 :            :         *vec_idx = vec_id;
     636                 :            : 
     637   [ #  #  #  #  :          0 :         if (unlikely(!!idesc))
                   #  # ]
     638                 :            :                 free_ind_table(idesc);
     639                 :            : 
     640                 :            :         return 0;
     641                 :            : }
     642                 :            : 
     643                 :            : /*
     644                 :            :  * Returns -1 on fail, 0 on success
     645                 :            :  */
     646                 :            : static inline int
     647                 :          0 : reserve_avail_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
     648                 :            :                                 uint64_t size, struct buf_vector *buf_vec,
     649                 :            :                                 uint16_t *num_buffers, uint16_t avail_head,
     650                 :            :                                 uint16_t *nr_vec)
     651                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
     652                 :            : {
     653                 :            :         uint16_t cur_idx;
     654                 :            :         uint16_t vec_idx = 0;
     655                 :            :         uint16_t max_tries, tries = 0;
     656                 :            : 
     657                 :            :         uint16_t head_idx = 0;
     658                 :            :         uint32_t len = 0;
     659                 :            : 
     660                 :          0 :         *num_buffers = 0;
     661                 :          0 :         cur_idx  = vq->last_avail_idx;
     662                 :            : 
     663         [ #  # ]:          0 :         if (rxvq_is_mergeable(dev))
     664                 :          0 :                 max_tries = vq->size - 1;
     665                 :            :         else
     666                 :            :                 max_tries = 1;
     667                 :            : 
     668         [ #  # ]:          0 :         while (size > 0) {
     669         [ #  # ]:          0 :                 if (unlikely(cur_idx == avail_head))
     670                 :            :                         return -1;
     671                 :            :                 /*
     672                 :            :                  * if we tried all available ring items, and still
     673                 :            :                  * can't get enough buf, it means something abnormal
     674                 :            :                  * happened.
     675                 :            :                  */
     676         [ #  # ]:          0 :                 if (unlikely(++tries > max_tries))
     677                 :            :                         return -1;
     678                 :            : 
     679   [ #  #  #  # ]:          0 :                 if (unlikely(fill_vec_buf_split(dev, vq, cur_idx,
     680                 :            :                                                 &vec_idx, buf_vec,
     681                 :            :                                                 &head_idx, &len,
     682                 :            :                                                 VHOST_ACCESS_RW) < 0))
     683                 :            :                         return -1;
     684                 :          0 :                 len = RTE_MIN(len, size);
     685                 :            :                 update_shadow_used_ring_split(vq, head_idx, len);
     686                 :          0 :                 size -= len;
     687                 :            : 
     688                 :          0 :                 cur_idx++;
     689                 :          0 :                 *num_buffers += 1;
     690                 :            :         }
     691                 :            : 
     692                 :          0 :         *nr_vec = vec_idx;
     693                 :            : 
     694                 :          0 :         return 0;
     695                 :            : }
     696                 :            : 
     697                 :            : static __rte_always_inline int
     698                 :            : fill_vec_buf_packed_indirect(struct virtio_net *dev,
     699                 :            :                         struct vhost_virtqueue *vq,
     700                 :            :                         struct vring_packed_desc *desc, uint16_t *vec_idx,
     701                 :            :                         struct buf_vector *buf_vec, uint32_t *len, uint8_t perm)
     702                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
     703                 :            : {
     704                 :            :         uint16_t i;
     705                 :            :         uint32_t nr_descs;
     706                 :            :         uint16_t vec_id = *vec_idx;
     707                 :          0 :         uint64_t desc_len = desc->len;
     708                 :          0 :         uint64_t dlen = desc_len;
     709                 :            :         struct vring_packed_desc *descs, *idescs = NULL;
     710                 :            : 
     711                 :          0 :         descs = (struct vring_packed_desc *)(uintptr_t)
     712   [ #  #  #  #  :          0 :                 vhost_iova_to_vva(dev, vq, desc->addr, &dlen, VHOST_ACCESS_RO);
                   #  # ]
     713   [ #  #  #  #  :          0 :         if (unlikely(!descs))
                   #  # ]
     714                 :            :                 return -1;
     715                 :            : 
     716   [ #  #  #  #  :          0 :         if (unlikely(dlen < desc_len)) {
                   #  # ]
     717                 :            :                 /*
     718                 :            :                  * The indirect desc table is not contiguous
     719                 :            :                  * in process VA space, we have to copy it.
     720                 :            :                  */
     721                 :          0 :                 idescs = vhost_alloc_copy_ind_table(dev,
     722                 :          0 :                                 vq, desc->addr, desc_len);
     723   [ #  #  #  #  :          0 :                 if (unlikely(!idescs))
                   #  # ]
     724                 :            :                         return -1;
     725                 :            : 
     726                 :            :                 descs = idescs;
     727                 :            :         }
     728                 :            : 
     729                 :          0 :         nr_descs = desc_len / sizeof(struct vring_packed_desc);
     730   [ #  #  #  #  :          0 :         if (unlikely(nr_descs >= vq->size)) {
                   #  # ]
     731                 :            :                 free_ind_table(idescs);
     732                 :          0 :                 return -1;
     733                 :            :         }
     734                 :            : 
     735   [ #  #  #  #  :          0 :         for (i = 0; i < nr_descs; i++) {
                   #  # ]
     736   [ #  #  #  #  :          0 :                 if (unlikely(vec_id >= BUF_VECTOR_MAX)) {
                   #  # ]
     737                 :            :                         free_ind_table(idescs);
     738                 :          0 :                         return -1;
     739                 :            :                 }
     740                 :            : 
     741                 :          0 :                 dlen = descs[i].len;
     742                 :          0 :                 *len += dlen;
     743   [ #  #  #  #  :          0 :                 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
                   #  # ]
     744                 :            :                                                 descs[i].addr, dlen,
     745                 :            :                                                 perm))) {
     746                 :            :                         free_ind_table(idescs);
     747                 :          0 :                         return -1;
     748                 :            :                 }
     749                 :            :         }
     750                 :            :         *vec_idx = vec_id;
     751                 :            : 
     752   [ #  #  #  #  :          0 :         if (unlikely(!!idescs))
                   #  # ]
     753                 :            :                 free_ind_table(idescs);
     754                 :            : 
     755                 :            :         return 0;
     756                 :            : }
     757                 :            : 
     758                 :            : static __rte_always_inline int
     759                 :            : fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
     760                 :            :                                 uint16_t avail_idx, uint16_t *desc_count,
     761                 :            :                                 struct buf_vector *buf_vec, uint16_t *vec_idx,
     762                 :            :                                 uint16_t *buf_id, uint32_t *len, uint8_t perm)
     763                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
     764                 :            : {
     765                 :          0 :         bool wrap_counter = vq->avail_wrap_counter;
     766                 :          0 :         struct vring_packed_desc *descs = vq->desc_packed;
     767                 :            :         uint16_t vec_id = *vec_idx;
     768                 :            :         uint64_t dlen;
     769                 :            : 
     770         [ #  # ]:          0 :         if (avail_idx < vq->last_avail_idx)
     771                 :          0 :                 wrap_counter ^= 1;
     772                 :            : 
     773                 :            :         /*
     774                 :            :          * Perform a load-acquire barrier in desc_is_avail to
     775                 :            :          * enforce the ordering between desc flags and desc
     776                 :            :          * content.
     777                 :            :          */
     778   [ #  #  #  #  :          0 :         if (unlikely(!desc_is_avail(&descs[avail_idx], wrap_counter)))
                   #  # ]
     779                 :            :                 return -1;
     780                 :            : 
     781                 :            :         *desc_count = 0;
     782                 :            :         *len = 0;
     783                 :            : 
     784                 :            :         while (1) {
     785   [ #  #  #  #  :          0 :                 if (unlikely(vec_id >= BUF_VECTOR_MAX))
                   #  # ]
     786                 :            :                         return -1;
     787                 :            : 
     788   [ #  #  #  #  :          0 :                 if (unlikely(*desc_count >= vq->size))
                   #  # ]
     789                 :            :                         return -1;
     790                 :            : 
     791                 :          0 :                 *desc_count += 1;
     792                 :          0 :                 *buf_id = descs[avail_idx].id;
     793                 :            : 
     794   [ #  #  #  #  :          0 :                 if (descs[avail_idx].flags & VRING_DESC_F_INDIRECT) {
                   #  # ]
     795   [ #  #  #  #  :          0 :                         if (unlikely(fill_vec_buf_packed_indirect(dev, vq,
                   #  # ]
     796                 :            :                                                         &descs[avail_idx],
     797                 :            :                                                         &vec_id, buf_vec,
     798                 :            :                                                         len, perm) < 0))
     799                 :            :                                 return -1;
     800                 :            :                 } else {
     801                 :          0 :                         dlen = descs[avail_idx].len;
     802                 :          0 :                         *len += dlen;
     803                 :            : 
     804   [ #  #  #  #  :          0 :                         if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
                   #  # ]
     805                 :            :                                                         descs[avail_idx].addr,
     806                 :            :                                                         dlen,
     807                 :            :                                                         perm)))
     808                 :            :                                 return -1;
     809                 :            :                 }
     810                 :            : 
     811   [ #  #  #  #  :          0 :                 if ((descs[avail_idx].flags & VRING_DESC_F_NEXT) == 0)
                   #  # ]
     812                 :            :                         break;
     813                 :            : 
     814   [ #  #  #  #  :          0 :                 if (++avail_idx >= vq->size) {
                   #  # ]
     815                 :          0 :                         avail_idx -= vq->size;
     816                 :            :                         wrap_counter ^= 1;
     817                 :            :                 }
     818                 :            :         }
     819                 :            : 
     820                 :            :         *vec_idx = vec_id;
     821                 :            : 
     822                 :            :         return 0;
     823                 :            : }
     824                 :            : 
     825                 :            : static __rte_noinline int
     826                 :          0 : copy_vnet_hdr_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
     827                 :            :                 struct buf_vector *buf_vec, uint16_t nr_vec,
     828                 :            :                 struct virtio_net_hdr_mrg_rxbuf *hdr,
     829                 :            :                 uint32_t *vec_idx, uint32_t *buf_offset)
     830                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
     831                 :            : {
     832                 :            :         uint64_t len = 0;
     833                 :          0 :         uint64_t remain = dev->vhost_hlen;
     834                 :          0 :         uint64_t src = (uint64_t)(uintptr_t)hdr;
     835                 :            :         uint32_t idx = 0;
     836                 :            : 
     837         [ #  # ]:          0 :         while (remain) {
     838                 :          0 :                 uint64_t iova = buf_vec->buf_iova;
     839                 :          0 :                 uint64_t dst = buf_vec->buf_addr;
     840                 :            : 
     841                 :          0 :                 len = RTE_MIN(remain,
     842                 :            :                                 buf_vec->buf_len);
     843         [ #  # ]:          0 :                 rte_memcpy((void *)(uintptr_t)dst,
     844                 :            :                                 (void *)(uintptr_t)src,
     845                 :            :                                 len);
     846                 :            : 
     847                 :            :                 PRINT_PACKET(dev, (uintptr_t)dst,
     848                 :            :                                 (uint32_t)len, 0);
     849                 :            :                 vhost_log_cache_write_iova(dev, vq,
     850                 :            :                                 iova, len);
     851                 :            : 
     852                 :          0 :                 remain -= len;
     853                 :          0 :                 src += len;
     854                 :            : 
     855         [ #  # ]:          0 :                 if (remain) {
     856         [ #  # ]:          0 :                         if (unlikely(idx + 1 >= nr_vec))
     857                 :            :                                 return -1;
     858                 :            : 
     859                 :          0 :                         buf_vec++;
     860                 :            :                         idx++;
     861                 :            :                 }
     862                 :            :         }
     863                 :            : 
     864                 :          0 :         *vec_idx = idx;
     865                 :          0 :         *buf_offset = (uint32_t)len;
     866                 :            : 
     867                 :          0 :         return 0;
     868                 :            : }
     869                 :            : 
     870                 :            : static __rte_always_inline int
     871                 :            : mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
     872                 :            :                 struct rte_mbuf *m, struct buf_vector *buf_vec,
     873                 :            :                 uint16_t nr_vec, uint16_t num_buffers)
     874                 :            :         __rte_requires_shared_capability(&vq->access_lock)
     875                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
     876                 :            : {
     877                 :          0 :         uint32_t vec_idx = 0;
     878                 :            :         uint32_t mbuf_offset, mbuf_avail;
     879                 :            :         uint32_t buf_offset, buf_avail;
     880                 :            :         uint64_t buf_addr, buf_iova, buf_len;
     881                 :            :         uint32_t cpy_len;
     882                 :            : 
     883                 :          0 :         if (unlikely(m == NULL))
     884                 :            :                 return -1;
     885                 :            : 
     886                 :          0 :         buf_addr = buf_vec[vec_idx].buf_addr;
     887                 :          0 :         buf_iova = buf_vec[vec_idx].buf_iova;
     888                 :          0 :         buf_len = buf_vec[vec_idx].buf_len;
     889                 :            : 
     890   [ #  #  #  #  :          0 :         if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1))
                   #  # ]
     891                 :            :                 return -1;
     892                 :            : 
     893                 :            :         VHOST_DATA_LOG(dev->ifname, DEBUG, "RX: num merge buffers %d", num_buffers);
     894                 :            : 
     895   [ #  #  #  # ]:          0 :         if (unlikely(buf_len < dev->vhost_hlen)) {
     896                 :            :                 struct virtio_net_hdr_mrg_rxbuf tmp_hdr;
     897                 :            : 
     898                 :            :                 memset(&tmp_hdr, 0, sizeof(struct virtio_net_hdr_mrg_rxbuf));
     899                 :            :                 virtio_enqueue_offload(m, &tmp_hdr.hdr);
     900   [ #  #  #  # ]:          0 :                 if (rxvq_is_mergeable(dev))
     901   [ #  #  #  # ]:          0 :                         ASSIGN_UNLESS_EQUAL(tmp_hdr.num_buffers, num_buffers);
     902                 :            : 
     903   [ #  #  #  # ]:          0 :                 if (copy_vnet_hdr_to_desc(dev, vq, buf_vec, nr_vec,
     904                 :            :                                 &tmp_hdr, &vec_idx, &buf_offset) < 0)
     905                 :          0 :                         return -1;
     906                 :            : 
     907                 :          0 :                 buf_addr = buf_vec[vec_idx].buf_addr;
     908                 :          0 :                 buf_iova = buf_vec[vec_idx].buf_iova;
     909                 :          0 :                 buf_len = buf_vec[vec_idx].buf_len;
     910                 :          0 :                 buf_avail = buf_len - buf_offset;
     911                 :            :         } else {
     912                 :            :                 struct virtio_net_hdr_mrg_rxbuf *hdr;
     913                 :            : 
     914   [ #  #  #  # ]:          0 :                 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)buf_addr;
     915                 :            :                 virtio_enqueue_offload(m, &hdr->hdr);
     916   [ #  #  #  # ]:          0 :                 if (rxvq_is_mergeable(dev))
     917   [ #  #  #  # ]:          0 :                         ASSIGN_UNLESS_EQUAL(hdr->num_buffers, num_buffers);
     918                 :            : 
     919                 :            :                 PRINT_PACKET(dev, (uintptr_t)buf_addr, dev->vhost_hlen, 0);
     920   [ #  #  #  # ]:          0 :                 vhost_log_cache_write_iova(dev, vq, buf_iova, dev->vhost_hlen);
     921                 :            : 
     922                 :          0 :                 buf_offset = dev->vhost_hlen;
     923                 :          0 :                 buf_avail = buf_len - dev->vhost_hlen;
     924                 :            :         }
     925                 :            : 
     926                 :          0 :         mbuf_avail  = rte_pktmbuf_data_len(m);
     927                 :            :         mbuf_offset = 0;
     928                 :            : 
     929   [ #  #  #  #  :          0 :         while (mbuf_avail != 0 || m->next != NULL) {
             #  #  #  # ]
     930                 :            :                 /* done with current buf, get the next one */
     931   [ #  #  #  # ]:          0 :                 if (buf_avail == 0) {
     932                 :          0 :                         vec_idx++;
     933   [ #  #  #  # ]:          0 :                         if (unlikely(vec_idx >= nr_vec))
     934                 :            :                                 return -1;
     935                 :            : 
     936                 :          0 :                         buf_addr = buf_vec[vec_idx].buf_addr;
     937                 :          0 :                         buf_iova = buf_vec[vec_idx].buf_iova;
     938                 :          0 :                         buf_len = buf_vec[vec_idx].buf_len;
     939                 :            : 
     940                 :          0 :                         buf_offset = 0;
     941                 :            :                         buf_avail  = buf_len;
     942                 :            :                 }
     943                 :            : 
     944                 :            :                 /* done with current mbuf, get the next one */
     945   [ #  #  #  # ]:          0 :                 if (mbuf_avail == 0) {
     946                 :          0 :                         m = m->next;
     947                 :            : 
     948                 :            :                         mbuf_offset = 0;
     949                 :          0 :                         mbuf_avail  = rte_pktmbuf_data_len(m);
     950                 :            :                 }
     951                 :            : 
     952   [ #  #  #  # ]:          0 :                 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
     953                 :            : 
     954   [ #  #  #  # ]:          0 :                 if (likely(!vhost_can_batch_copy(vq, cpy_len))) {
     955                 :          0 :                         rte_memcpy(RTE_PTR_ADD(RTE_PTR_UNQUAL(buf_addr), buf_offset),
     956   [ #  #  #  # ]:          0 :                                 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
     957                 :            :                                 cpy_len);
     958   [ #  #  #  # ]:          0 :                         vhost_log_cache_write_iova(dev, vq, buf_iova + buf_offset, cpy_len);
     959                 :            :                         PRINT_PACKET(dev, RTE_PTR_ADD(RTE_PTR_UNQUAL(buf_addr), buf_offset),
     960                 :            :                                 cpy_len, 0);
     961                 :            :                 } else {
     962                 :          0 :                         vq->batch_copy_elems[vq->batch_copy_nb_elems].dst =
     963                 :          0 :                                 RTE_PTR_ADD(RTE_PTR_UNQUAL(buf_addr), buf_offset);
     964                 :          0 :                         vq->batch_copy_elems[vq->batch_copy_nb_elems].src =
     965                 :          0 :                                 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
     966                 :          0 :                         vq->batch_copy_elems[vq->batch_copy_nb_elems].log_addr =
     967                 :          0 :                                 buf_iova + buf_offset;
     968                 :          0 :                         vq->batch_copy_elems[vq->batch_copy_nb_elems].len = cpy_len;
     969                 :          0 :                         vq->batch_copy_nb_elems++;
     970                 :            :                 }
     971                 :            : 
     972                 :          0 :                 mbuf_avail  -= cpy_len;
     973                 :          0 :                 mbuf_offset += cpy_len;
     974                 :          0 :                 buf_avail  -= cpy_len;
     975                 :          0 :                 buf_offset += cpy_len;
     976                 :            :         }
     977                 :            : 
     978                 :            :         return 0;
     979                 :            : }
     980                 :            : 
     981                 :            : static __rte_always_inline int
     982                 :            : vhost_enqueue_single_packed(struct virtio_net *dev,
     983                 :            :                             struct vhost_virtqueue *vq,
     984                 :            :                             struct rte_mbuf *pkt,
     985                 :            :                             struct buf_vector *buf_vec,
     986                 :            :                             uint16_t *nr_descs)
     987                 :            :         __rte_requires_shared_capability(&vq->access_lock)
     988                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
     989                 :          0 : {
     990                 :            :         uint16_t nr_vec = 0;
     991                 :          0 :         uint16_t avail_idx = vq->last_avail_idx;
     992                 :            :         uint16_t max_tries, tries = 0;
     993                 :            :         uint16_t buf_id = 0;
     994                 :            :         uint32_t len = 0;
     995                 :            :         uint16_t desc_count;
     996                 :          0 :         uint64_t size = pkt->pkt_len + sizeof(struct virtio_net_hdr_mrg_rxbuf);
     997                 :            :         uint16_t num_buffers = 0;
     998                 :          0 :         uint32_t buffer_len[vq->size];
     999                 :          0 :         uint16_t buffer_buf_id[vq->size];
    1000                 :          0 :         uint16_t buffer_desc_count[vq->size];
    1001                 :            : 
    1002                 :          0 :         if (rxvq_is_mergeable(dev))
    1003                 :          0 :                 max_tries = vq->size - 1;
    1004                 :            :         else
    1005                 :            :                 max_tries = 1;
    1006                 :            : 
    1007         [ #  # ]:          0 :         while (size > 0) {
    1008                 :            :                 /*
    1009                 :            :                  * if we tried all available ring items, and still
    1010                 :            :                  * can't get enough buf, it means something abnormal
    1011                 :            :                  * happened.
    1012                 :            :                  */
    1013         [ #  # ]:          0 :                 if (unlikely(++tries > max_tries))
    1014                 :            :                         return -1;
    1015                 :            : 
    1016         [ #  # ]:          0 :                 if (unlikely(fill_vec_buf_packed(dev, vq,
    1017                 :            :                                                 avail_idx, &desc_count,
    1018                 :            :                                                 buf_vec, &nr_vec,
    1019                 :            :                                                 &buf_id, &len,
    1020                 :            :                                                 VHOST_ACCESS_RW) < 0))
    1021                 :            :                         return -1;
    1022                 :            : 
    1023                 :          0 :                 len = RTE_MIN(len, size);
    1024                 :          0 :                 size -= len;
    1025                 :            : 
    1026                 :          0 :                 buffer_len[num_buffers] = len;
    1027                 :          0 :                 buffer_buf_id[num_buffers] = buf_id;
    1028                 :          0 :                 buffer_desc_count[num_buffers] = desc_count;
    1029                 :            :                 num_buffers += 1;
    1030                 :            : 
    1031                 :          0 :                 *nr_descs += desc_count;
    1032                 :          0 :                 avail_idx += desc_count;
    1033         [ #  # ]:          0 :                 if (avail_idx >= vq->size)
    1034                 :          0 :                         avail_idx -= vq->size;
    1035                 :            :         }
    1036                 :            : 
    1037   [ #  #  #  # ]:          0 :         if (mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec, num_buffers) < 0)
    1038                 :            :                 return -1;
    1039                 :            : 
    1040                 :            :         vhost_shadow_enqueue_single_packed(dev, vq, buffer_len, buffer_buf_id,
    1041                 :            :                                            buffer_desc_count, num_buffers);
    1042                 :            : 
    1043                 :            :         return 0;
    1044                 :            : }
    1045                 :            : 
    1046                 :            : static __rte_noinline uint32_t
    1047                 :          0 : virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
    1048                 :            :         struct rte_mbuf **pkts, uint32_t count)
    1049                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    1050                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    1051                 :            : {
    1052                 :            :         uint32_t pkt_idx = 0;
    1053                 :            :         uint16_t num_buffers;
    1054                 :            :         struct buf_vector buf_vec[BUF_VECTOR_MAX];
    1055                 :            :         uint16_t avail_head;
    1056                 :            : 
    1057                 :            :         /*
    1058                 :            :          * The ordering between avail index and
    1059                 :            :          * desc reads needs to be enforced.
    1060                 :            :          */
    1061                 :          0 :         avail_head = rte_atomic_load_explicit((unsigned short __rte_atomic *)&vq->avail->idx,
    1062                 :            :                 rte_memory_order_acquire);
    1063                 :            : 
    1064                 :          0 :         rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
    1065                 :            : 
    1066         [ #  # ]:          0 :         for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
    1067                 :          0 :                 uint64_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
    1068                 :          0 :                 uint16_t nr_vec = 0;
    1069                 :            : 
    1070         [ #  # ]:          0 :                 if (unlikely(reserve_avail_buf_split(dev, vq,
    1071                 :            :                                                 pkt_len, buf_vec, &num_buffers,
    1072                 :            :                                                 avail_head, &nr_vec) < 0)) {
    1073                 :            :                         VHOST_DATA_LOG(dev->ifname, DEBUG,
    1074                 :            :                                 "failed to get enough desc from vring");
    1075                 :          0 :                         vq->shadow_used_idx -= num_buffers;
    1076                 :          0 :                         break;
    1077                 :            :                 }
    1078                 :            : 
    1079                 :            :                 VHOST_DATA_LOG(dev->ifname, DEBUG,
    1080                 :            :                         "current index %d | end index %d",
    1081                 :            :                         vq->last_avail_idx, vq->last_avail_idx + num_buffers);
    1082                 :            : 
    1083   [ #  #  #  # ]:          0 :                 if (mbuf_to_desc(dev, vq, pkts[pkt_idx], buf_vec, nr_vec, num_buffers) < 0) {
    1084                 :          0 :                         vq->shadow_used_idx -= num_buffers;
    1085                 :          0 :                         break;
    1086                 :            :                 }
    1087                 :            : 
    1088         [ #  # ]:          0 :                 vq->last_avail_idx += num_buffers;
    1089                 :            :                 vhost_virtqueue_reconnect_log_split(vq);
    1090                 :            :         }
    1091                 :            : 
    1092                 :          0 :         do_data_copy_enqueue(dev, vq);
    1093                 :            : 
    1094         [ #  # ]:          0 :         if (likely(vq->shadow_used_idx)) {
    1095                 :            :                 flush_shadow_used_ring_split(dev, vq);
    1096                 :            :                 vhost_vring_call_split(dev, vq);
    1097                 :            :         }
    1098                 :            : 
    1099                 :          0 :         return pkt_idx;
    1100                 :            : }
    1101                 :            : 
    1102                 :            : static __rte_always_inline int
    1103                 :            : virtio_dev_rx_batch_check(struct virtio_net *dev, struct vhost_virtqueue *vq,
    1104                 :            :                 struct rte_mbuf **pkts, uint64_t *desc_addrs, uint64_t *lens)
    1105                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    1106                 :            : {
    1107                 :          0 :         bool wrap_counter = vq->avail_wrap_counter;
    1108                 :            :         struct vring_packed_desc *descs = vq->desc_packed;
    1109                 :            :         uint16_t avail_idx = vq->last_avail_idx;
    1110                 :            :         uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
    1111                 :            :         uint16_t i;
    1112                 :            : 
    1113                 :          0 :         if (unlikely(avail_idx & PACKED_BATCH_MASK))
    1114                 :            :                 return -1;
    1115                 :            : 
    1116         [ #  # ]:          0 :         if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
    1117                 :            :                 return -1;
    1118                 :            : 
    1119         [ #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    1120         [ #  # ]:          0 :                 if (unlikely(pkts[i]->next != NULL))
    1121                 :            :                         return -1;
    1122         [ #  # ]:          0 :                 if (unlikely(!desc_is_avail(&descs[avail_idx + i],
    1123                 :            :                                             wrap_counter)))
    1124                 :            :                         return -1;
    1125                 :            :         }
    1126                 :            : 
    1127         [ #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
    1128                 :          0 :                 lens[i] = descs[avail_idx + i].len;
    1129                 :            : 
    1130         [ #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    1131         [ #  # ]:          0 :                 if (unlikely(pkts[i]->pkt_len > (lens[i] - buf_offset)))
    1132                 :            :                         return -1;
    1133                 :            :         }
    1134                 :            : 
    1135         [ #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
    1136                 :          0 :                 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
    1137                 :          0 :                                                   descs[avail_idx + i].addr,
    1138         [ #  # ]:          0 :                                                   &lens[i],
    1139                 :            :                                                   VHOST_ACCESS_RW);
    1140                 :            : 
    1141         [ #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    1142         [ #  # ]:          0 :                 if (unlikely(!desc_addrs[i]))
    1143                 :            :                         return -1;
    1144         [ #  # ]:          0 :                 if (unlikely(lens[i] != descs[avail_idx + i].len))
    1145                 :            :                         return -1;
    1146                 :            :         }
    1147                 :            : 
    1148                 :            :         return 0;
    1149                 :            : }
    1150                 :            : 
    1151                 :            : static __rte_always_inline void
    1152                 :            : virtio_dev_rx_batch_packed_copy(struct virtio_net *dev,
    1153                 :            :                            struct vhost_virtqueue *vq,
    1154                 :            :                            struct rte_mbuf **pkts,
    1155                 :            :                            uint64_t *desc_addrs,
    1156                 :            :                            uint64_t *lens)
    1157                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    1158                 :            : {
    1159                 :            :         uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
    1160                 :            :         struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE];
    1161                 :          0 :         struct vring_packed_desc *descs = vq->desc_packed;
    1162                 :          0 :         uint16_t avail_idx = vq->last_avail_idx;
    1163                 :            :         uint16_t ids[PACKED_BATCH_SIZE];
    1164                 :            :         uint16_t i;
    1165                 :            : 
    1166         [ #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    1167                 :          0 :                 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
    1168                 :          0 :                 hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *)
    1169                 :            :                                         (uintptr_t)desc_addrs[i];
    1170                 :          0 :                 lens[i] = pkts[i]->pkt_len +
    1171                 :            :                         sizeof(struct virtio_net_hdr_mrg_rxbuf);
    1172                 :            :         }
    1173                 :            : 
    1174         [ #  # ]:          0 :         if (rxvq_is_mergeable(dev)) {
    1175         [ #  # ]:          0 :                 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    1176         [ #  # ]:          0 :                         ASSIGN_UNLESS_EQUAL(hdrs[i]->num_buffers, 1);
    1177                 :            :                 }
    1178                 :            :         }
    1179                 :            : 
    1180         [ #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
    1181         [ #  # ]:          0 :                 virtio_enqueue_offload(pkts[i], &hdrs[i]->hdr);
    1182                 :            : 
    1183                 :            :         vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
    1184                 :            : 
    1185         [ #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    1186                 :          0 :                 rte_memcpy((void *)(uintptr_t)(desc_addrs[i] + buf_offset),
    1187                 :          0 :                            rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
    1188         [ #  # ]:          0 :                            pkts[i]->pkt_len);
    1189                 :            :         }
    1190                 :            : 
    1191         [ #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
    1192                 :          0 :                 vhost_log_cache_write_iova(dev, vq, descs[avail_idx + i].addr,
    1193         [ #  # ]:          0 :                                            lens[i]);
    1194                 :            : 
    1195         [ #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
    1196                 :          0 :                 ids[i] = descs[avail_idx + i].id;
    1197                 :            : 
    1198                 :            :         vhost_flush_enqueue_batch_packed(dev, vq, lens, ids);
    1199                 :            : }
    1200                 :            : 
    1201                 :            : static __rte_always_inline int
    1202                 :            : virtio_dev_rx_batch_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
    1203                 :            :                 struct rte_mbuf **pkts)
    1204                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    1205                 :            : {
    1206                 :            :         uint64_t desc_addrs[PACKED_BATCH_SIZE];
    1207                 :            :         uint64_t lens[PACKED_BATCH_SIZE];
    1208                 :            : 
    1209         [ #  # ]:          0 :         if (virtio_dev_rx_batch_check(dev, vq, pkts, desc_addrs, lens) == -1)
    1210                 :            :                 return -1;
    1211                 :            : 
    1212         [ #  # ]:          0 :         if (vq->shadow_used_idx) {
    1213                 :          0 :                 do_data_copy_enqueue(dev, vq);
    1214                 :            :                 vhost_flush_enqueue_shadow_packed(dev, vq);
    1215                 :            :         }
    1216                 :            : 
    1217                 :            :         virtio_dev_rx_batch_packed_copy(dev, vq, pkts, desc_addrs, lens);
    1218                 :            : 
    1219                 :            :         return 0;
    1220                 :            : }
    1221                 :            : 
    1222                 :            : static __rte_always_inline int16_t
    1223                 :            : virtio_dev_rx_single_packed(struct virtio_net *dev,
    1224                 :            :                             struct vhost_virtqueue *vq,
    1225                 :            :                             struct rte_mbuf *pkt)
    1226                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    1227                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    1228                 :            : {
    1229                 :            :         struct buf_vector buf_vec[BUF_VECTOR_MAX];
    1230                 :            :         uint16_t nr_descs = 0;
    1231                 :            : 
    1232         [ #  # ]:          0 :         if (unlikely(vhost_enqueue_single_packed(dev, vq, pkt, buf_vec,
    1233                 :            :                                                  &nr_descs) < 0)) {
    1234                 :            :                 VHOST_DATA_LOG(dev->ifname, DEBUG, "failed to get enough desc from vring");
    1235                 :            :                 return -1;
    1236                 :            :         }
    1237                 :            : 
    1238                 :            :         VHOST_DATA_LOG(dev->ifname, DEBUG,
    1239                 :            :                 "current index %d | end index %d",
    1240                 :            :                 vq->last_avail_idx, vq->last_avail_idx + nr_descs);
    1241                 :            : 
    1242                 :            :         vq_inc_last_avail_packed(vq, nr_descs);
    1243                 :            : 
    1244                 :            :         return 0;
    1245                 :            : }
    1246                 :            : 
    1247                 :            : static __rte_noinline uint32_t
    1248                 :          0 : virtio_dev_rx_packed(struct virtio_net *dev,
    1249                 :            :                      struct vhost_virtqueue *__rte_restrict vq,
    1250                 :            :                      struct rte_mbuf **__rte_restrict pkts,
    1251                 :            :                      uint32_t count)
    1252                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    1253                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    1254                 :            : {
    1255                 :            :         uint32_t pkt_idx = 0;
    1256                 :            : 
    1257                 :            :         do {
    1258                 :          0 :                 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
    1259                 :            : 
    1260         [ #  # ]:          0 :                 if (count - pkt_idx >= PACKED_BATCH_SIZE) {
    1261         [ #  # ]:          0 :                         if (!virtio_dev_rx_batch_packed(dev, vq, &pkts[pkt_idx])) {
    1262                 :          0 :                                 pkt_idx += PACKED_BATCH_SIZE;
    1263                 :          0 :                                 continue;
    1264                 :            :                         }
    1265                 :            :                 }
    1266                 :            : 
    1267         [ #  # ]:          0 :                 if (virtio_dev_rx_single_packed(dev, vq, pkts[pkt_idx]))
    1268                 :            :                         break;
    1269                 :          0 :                 pkt_idx++;
    1270                 :            : 
    1271         [ #  # ]:          0 :         } while (pkt_idx < count);
    1272                 :            : 
    1273         [ #  # ]:          0 :         if (vq->shadow_used_idx) {
    1274                 :          0 :                 do_data_copy_enqueue(dev, vq);
    1275                 :            :                 vhost_flush_enqueue_shadow_packed(dev, vq);
    1276                 :            :         }
    1277                 :            : 
    1278         [ #  # ]:          0 :         if (pkt_idx)
    1279                 :            :                 vhost_vring_call_packed(dev, vq);
    1280                 :            : 
    1281                 :          0 :         return pkt_idx;
    1282                 :            : }
    1283                 :            : 
    1284                 :            : static void
    1285                 :          0 : virtio_dev_vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
    1286                 :            : {
    1287                 :          0 :         rte_rwlock_write_lock(&vq->access_lock);
    1288                 :            :         vhost_user_iotlb_rd_lock(vq);
    1289         [ #  # ]:          0 :         if (!vq->access_ok)
    1290                 :          0 :                 vring_translate(dev, vq);
    1291                 :            :         vhost_user_iotlb_rd_unlock(vq);
    1292                 :            :         rte_rwlock_write_unlock(&vq->access_lock);
    1293                 :          0 : }
    1294                 :            : 
    1295                 :            : static __rte_always_inline uint32_t
    1296                 :            : virtio_dev_rx(struct virtio_net *dev, struct vhost_virtqueue *vq,
    1297                 :            :         struct rte_mbuf **pkts, uint32_t count)
    1298                 :            : {
    1299                 :            :         uint32_t nb_tx = 0;
    1300                 :            : 
    1301                 :            :         VHOST_DATA_LOG(dev->ifname, DEBUG, "%s", __func__);
    1302                 :          0 :         rte_rwlock_read_lock(&vq->access_lock);
    1303                 :            : 
    1304         [ #  # ]:          0 :         if (unlikely(!vq->enabled))
    1305                 :          0 :                 goto out_access_unlock;
    1306                 :            : 
    1307                 :            :         vhost_user_iotlb_rd_lock(vq);
    1308                 :            : 
    1309         [ #  # ]:          0 :         if (unlikely(!vq->access_ok)) {
    1310                 :            :                 vhost_user_iotlb_rd_unlock(vq);
    1311                 :            :                 rte_rwlock_read_unlock(&vq->access_lock);
    1312                 :            : 
    1313                 :          0 :                 virtio_dev_vring_translate(dev, vq);
    1314                 :          0 :                 goto out_no_unlock;
    1315                 :            :         }
    1316                 :            : 
    1317                 :          0 :         count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
    1318         [ #  # ]:          0 :         if (count == 0)
    1319                 :          0 :                 goto out;
    1320                 :            : 
    1321         [ #  # ]:          0 :         if (vq_is_packed(dev))
    1322                 :          0 :                 nb_tx = virtio_dev_rx_packed(dev, vq, pkts, count);
    1323                 :            :         else
    1324                 :          0 :                 nb_tx = virtio_dev_rx_split(dev, vq, pkts, count);
    1325                 :            : 
    1326                 :          0 :         vhost_queue_stats_update(dev, vq, pkts, nb_tx);
    1327                 :            : 
    1328                 :          0 : out:
    1329                 :            :         vhost_user_iotlb_rd_unlock(vq);
    1330                 :            : 
    1331                 :          0 : out_access_unlock:
    1332                 :            :         rte_rwlock_read_unlock(&vq->access_lock);
    1333                 :            : 
    1334                 :          0 : out_no_unlock:
    1335                 :            :         return nb_tx;
    1336                 :            : }
    1337                 :            : 
    1338                 :            : RTE_EXPORT_SYMBOL(rte_vhost_enqueue_burst)
    1339                 :            : uint16_t
    1340         [ #  # ]:          0 : rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
    1341                 :            :         struct rte_mbuf **__rte_restrict pkts, uint16_t count)
    1342                 :            : {
    1343                 :            :         struct virtio_net *dev = get_device(vid);
    1344                 :            : 
    1345         [ #  # ]:          0 :         if (!dev)
    1346                 :            :                 return 0;
    1347                 :            : 
    1348         [ #  # ]:          0 :         if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
    1349                 :          0 :                 VHOST_DATA_LOG(dev->ifname, ERR,
    1350                 :            :                         "%s: built-in vhost net backend is disabled.",
    1351                 :            :                         __func__);
    1352                 :          0 :                 return 0;
    1353                 :            :         }
    1354                 :            : 
    1355   [ #  #  #  # ]:          0 :         if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
    1356                 :          0 :                 VHOST_DATA_LOG(dev->ifname, ERR,
    1357                 :            :                         "%s: invalid virtqueue idx %d.",
    1358                 :            :                         __func__, queue_id);
    1359                 :          0 :                 return 0;
    1360                 :            :         }
    1361                 :            : 
    1362                 :          0 :         return virtio_dev_rx(dev, dev->virtqueue[queue_id], pkts, count);
    1363                 :            : }
    1364                 :            : 
    1365                 :            : static inline bool
    1366                 :            : virtio_net_with_host_offload(struct virtio_net *dev)
    1367                 :            : {
    1368   [ #  #  #  # ]:          0 :         if (dev->features &
    1369                 :            :                         ((1ULL << VIRTIO_NET_F_CSUM) |
    1370                 :            :                          (1ULL << VIRTIO_NET_F_HOST_ECN) |
    1371                 :            :                          (1ULL << VIRTIO_NET_F_HOST_TSO4) |
    1372                 :            :                          (1ULL << VIRTIO_NET_F_HOST_TSO6) |
    1373                 :            :                          (1ULL << VIRTIO_NET_F_HOST_UFO)))
    1374                 :            :                 return true;
    1375                 :            : 
    1376                 :            :         return false;
    1377                 :            : }
    1378                 :            : 
    1379                 :            : static int
    1380                 :          0 : parse_headers(struct rte_mbuf *m, uint8_t *l4_proto)
    1381                 :            : {
    1382                 :            :         struct rte_ipv4_hdr *ipv4_hdr;
    1383                 :            :         struct rte_ipv6_hdr *ipv6_hdr;
    1384                 :            :         struct rte_ether_hdr *eth_hdr;
    1385                 :            :         uint16_t ethertype;
    1386                 :          0 :         uint16_t data_len = rte_pktmbuf_data_len(m);
    1387                 :            : 
    1388         [ #  # ]:          0 :         if (data_len < sizeof(struct rte_ether_hdr))
    1389                 :            :                 return -EINVAL;
    1390                 :            : 
    1391                 :          0 :         eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
    1392                 :            : 
    1393                 :          0 :         m->l2_len = sizeof(struct rte_ether_hdr);
    1394         [ #  # ]:          0 :         ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
    1395                 :            : 
    1396         [ #  # ]:          0 :         if (ethertype == RTE_ETHER_TYPE_VLAN) {
    1397         [ #  # ]:          0 :                 if (data_len < sizeof(struct rte_ether_hdr) +
    1398                 :            :                                 sizeof(struct rte_vlan_hdr))
    1399                 :          0 :                         goto error;
    1400                 :            : 
    1401                 :            :                 struct rte_vlan_hdr *vlan_hdr =
    1402                 :            :                         (struct rte_vlan_hdr *)(eth_hdr + 1);
    1403                 :            : 
    1404                 :          0 :                 m->l2_len += sizeof(struct rte_vlan_hdr);
    1405         [ #  # ]:          0 :                 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
    1406                 :            :         }
    1407                 :            : 
    1408      [ #  #  # ]:          0 :         switch (ethertype) {
    1409                 :          0 :         case RTE_ETHER_TYPE_IPV4:
    1410         [ #  # ]:          0 :                 if (data_len < m->l2_len + sizeof(struct rte_ipv4_hdr))
    1411                 :          0 :                         goto error;
    1412         [ #  # ]:          0 :                 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *,
    1413                 :            :                                 m->l2_len);
    1414                 :          0 :                 m->l3_len = rte_ipv4_hdr_len(ipv4_hdr);
    1415         [ #  # ]:          0 :                 if (data_len < m->l2_len + m->l3_len)
    1416                 :          0 :                         goto error;
    1417                 :          0 :                 m->ol_flags |= RTE_MBUF_F_TX_IPV4;
    1418                 :          0 :                 *l4_proto = ipv4_hdr->next_proto_id;
    1419                 :          0 :                 break;
    1420                 :          0 :         case RTE_ETHER_TYPE_IPV6:
    1421         [ #  # ]:          0 :                 if (data_len < m->l2_len + sizeof(struct rte_ipv6_hdr))
    1422                 :          0 :                         goto error;
    1423                 :          0 :                 ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv6_hdr *,
    1424                 :            :                                 m->l2_len);
    1425                 :          0 :                 m->l3_len = sizeof(struct rte_ipv6_hdr);
    1426                 :          0 :                 m->ol_flags |= RTE_MBUF_F_TX_IPV6;
    1427                 :          0 :                 *l4_proto = ipv6_hdr->proto;
    1428                 :          0 :                 break;
    1429                 :          0 :         default:
    1430                 :            :                 /* a valid L3 header is needed for further L4 parsing */
    1431                 :          0 :                 goto error;
    1432                 :            :         }
    1433                 :            : 
    1434                 :            :         /* both CSUM and GSO need a valid L4 header */
    1435   [ #  #  #  # ]:          0 :         switch (*l4_proto) {
    1436                 :          0 :         case IPPROTO_TCP:
    1437         [ #  # ]:          0 :                 if (data_len < m->l2_len + m->l3_len +
    1438                 :            :                                 sizeof(struct rte_tcp_hdr))
    1439                 :          0 :                         goto error;
    1440                 :            :                 break;
    1441                 :          0 :         case IPPROTO_UDP:
    1442         [ #  # ]:          0 :                 if (data_len < m->l2_len + m->l3_len +
    1443                 :            :                                 sizeof(struct rte_udp_hdr))
    1444                 :          0 :                         goto error;
    1445                 :            :                 break;
    1446                 :          0 :         case IPPROTO_SCTP:
    1447         [ #  # ]:          0 :                 if (data_len < m->l2_len + m->l3_len +
    1448                 :            :                                 sizeof(struct rte_sctp_hdr))
    1449                 :          0 :                         goto error;
    1450                 :            :                 break;
    1451                 :          0 :         default:
    1452                 :          0 :                 goto error;
    1453                 :            :         }
    1454                 :            : 
    1455                 :            :         return 0;
    1456                 :            : 
    1457                 :          0 : error:
    1458                 :          0 :         m->l2_len = 0;
    1459                 :          0 :         m->l3_len = 0;
    1460                 :          0 :         m->ol_flags = 0;
    1461                 :          0 :         return -EINVAL;
    1462                 :            : }
    1463                 :            : 
    1464                 :            : static __rte_always_inline void
    1465                 :            : vhost_dequeue_offload_legacy(struct virtio_net *dev, struct virtio_net_hdr *hdr,
    1466                 :            :                 struct rte_mbuf *m)
    1467                 :            : {
    1468                 :          0 :         uint8_t l4_proto = 0;
    1469                 :            :         struct rte_tcp_hdr *tcp_hdr = NULL;
    1470                 :            :         uint16_t tcp_len;
    1471                 :          0 :         uint16_t data_len = rte_pktmbuf_data_len(m);
    1472                 :            : 
    1473   [ #  #  #  #  :          0 :         if (parse_headers(m, &l4_proto) < 0)
                   #  # ]
    1474                 :          0 :                 return;
    1475                 :            : 
    1476   [ #  #  #  #  :          0 :         if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
                   #  # ]
    1477   [ #  #  #  #  :          0 :                 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
                   #  # ]
    1478   [ #  #  #  #  :          0 :                         switch (hdr->csum_offset) {
          #  #  #  #  #  
                #  #  # ]
    1479                 :          0 :                         case (offsetof(struct rte_tcp_hdr, cksum)):
    1480   [ #  #  #  #  :          0 :                                 if (l4_proto != IPPROTO_TCP)
                   #  # ]
    1481                 :          0 :                                         goto error;
    1482                 :          0 :                                 m->ol_flags |= RTE_MBUF_F_TX_TCP_CKSUM;
    1483                 :          0 :                                 break;
    1484                 :          0 :                         case (offsetof(struct rte_udp_hdr, dgram_cksum)):
    1485   [ #  #  #  #  :          0 :                                 if (l4_proto != IPPROTO_UDP)
                   #  # ]
    1486                 :          0 :                                         goto error;
    1487                 :          0 :                                 m->ol_flags |= RTE_MBUF_F_TX_UDP_CKSUM;
    1488                 :          0 :                                 break;
    1489                 :          0 :                         case (offsetof(struct rte_sctp_hdr, cksum)):
    1490   [ #  #  #  #  :          0 :                                 if (l4_proto != IPPROTO_SCTP)
                   #  # ]
    1491                 :          0 :                                         goto error;
    1492                 :          0 :                                 m->ol_flags |= RTE_MBUF_F_TX_SCTP_CKSUM;
    1493                 :          0 :                                 break;
    1494                 :          0 :                         default:
    1495                 :          0 :                                 goto error;
    1496                 :            :                         }
    1497                 :            :                 } else {
    1498                 :          0 :                         goto error;
    1499                 :            :                 }
    1500                 :            :         }
    1501                 :            : 
    1502   [ #  #  #  #  :          0 :         if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
                   #  # ]
    1503   [ #  #  #  #  :          0 :                 if (hdr->gso_size == 0)
                   #  # ]
    1504                 :          0 :                         goto error;
    1505                 :            : 
    1506   [ #  #  #  #  :          0 :                 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
             #  #  #  #  
                      # ]
    1507                 :          0 :                 case VIRTIO_NET_HDR_GSO_TCPV4:
    1508                 :            :                 case VIRTIO_NET_HDR_GSO_TCPV6:
    1509   [ #  #  #  #  :          0 :                         if (l4_proto != IPPROTO_TCP)
                   #  # ]
    1510                 :          0 :                                 goto error;
    1511                 :          0 :                         tcp_hdr = rte_pktmbuf_mtod_offset(m,
    1512                 :            :                                         struct rte_tcp_hdr *,
    1513                 :            :                                         m->l2_len + m->l3_len);
    1514                 :          0 :                         tcp_len = (tcp_hdr->data_off & 0xf0) >> 2;
    1515   [ #  #  #  #  :          0 :                         if (data_len < m->l2_len + m->l3_len + tcp_len)
                   #  # ]
    1516                 :          0 :                                 goto error;
    1517                 :          0 :                         m->ol_flags |= RTE_MBUF_F_TX_TCP_SEG;
    1518                 :          0 :                         m->tso_segsz = hdr->gso_size;
    1519                 :          0 :                         m->l4_len = tcp_len;
    1520                 :          0 :                         break;
    1521                 :          0 :                 case VIRTIO_NET_HDR_GSO_UDP:
    1522   [ #  #  #  #  :          0 :                         if (l4_proto != IPPROTO_UDP)
                   #  # ]
    1523                 :          0 :                                 goto error;
    1524                 :          0 :                         m->ol_flags |= RTE_MBUF_F_TX_UDP_SEG;
    1525                 :          0 :                         m->tso_segsz = hdr->gso_size;
    1526                 :          0 :                         m->l4_len = sizeof(struct rte_udp_hdr);
    1527                 :          0 :                         break;
    1528                 :          0 :                 default:
    1529                 :          0 :                         VHOST_DATA_LOG(dev->ifname, WARNING,
    1530                 :            :                                 "unsupported gso type %u.",
    1531                 :            :                                 hdr->gso_type);
    1532                 :          0 :                         goto error;
    1533                 :            :                 }
    1534                 :            :         }
    1535                 :            :         return;
    1536                 :            : 
    1537                 :          0 : error:
    1538                 :          0 :         m->l2_len = 0;
    1539                 :          0 :         m->l3_len = 0;
    1540                 :          0 :         m->ol_flags = 0;
    1541                 :            : }
    1542                 :            : 
    1543                 :            : static __rte_always_inline void
    1544                 :            : vhost_dequeue_offload(struct virtio_net *dev, struct virtio_net_hdr *hdr,
    1545                 :            :                 struct rte_mbuf *m, bool legacy_ol_flags)
    1546                 :            : {
    1547                 :            :         struct rte_net_hdr_lens hdr_lens;
    1548                 :            :         int l4_supported = 0;
    1549                 :            :         uint32_t ptype;
    1550                 :            : 
    1551   [ #  #  #  #  :          0 :         if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
             #  #  #  # ]
    1552                 :          0 :                 return;
    1553                 :            : 
    1554                 :            :         if (legacy_ol_flags) {
    1555                 :            :                 vhost_dequeue_offload_legacy(dev, hdr, m);
    1556                 :            :                 return;
    1557                 :            :         }
    1558                 :            : 
    1559                 :            :         m->ol_flags |= RTE_MBUF_F_RX_IP_CKSUM_UNKNOWN;
    1560                 :            : 
    1561                 :          0 :         ptype = rte_net_get_ptype(m, &hdr_lens, RTE_PTYPE_ALL_MASK);
    1562                 :          0 :         m->packet_type = ptype;
    1563   [ #  #  #  #  :          0 :         if ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP ||
                   #  # ]
    1564   [ #  #  #  #  :          0 :             (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP ||
                   #  # ]
    1565                 :            :             (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_SCTP)
    1566                 :            :                 l4_supported = 1;
    1567                 :            : 
    1568                 :            :         /* According to Virtio 1.1 spec, the device only needs to look at
    1569                 :            :          * VIRTIO_NET_HDR_F_NEEDS_CSUM in the packet transmission path.
    1570                 :            :          * This differs from the processing incoming packets path where the
    1571                 :            :          * driver could rely on VIRTIO_NET_HDR_F_DATA_VALID flag set by the
    1572                 :            :          * device.
    1573                 :            :          *
    1574                 :            :          * 5.1.6.2.1 Driver Requirements: Packet Transmission
    1575                 :            :          * The driver MUST NOT set the VIRTIO_NET_HDR_F_DATA_VALID and
    1576                 :            :          * VIRTIO_NET_HDR_F_RSC_INFO bits in flags.
    1577                 :            :          *
    1578                 :            :          * 5.1.6.2.2 Device Requirements: Packet Transmission
    1579                 :            :          * The device MUST ignore flag bits that it does not recognize.
    1580                 :            :          */
    1581   [ #  #  #  #  :          0 :         if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
                   #  # ]
    1582                 :            :                 uint32_t hdrlen;
    1583                 :            : 
    1584                 :          0 :                 hdrlen = hdr_lens.l2_len + hdr_lens.l3_len + hdr_lens.l4_len;
    1585   [ #  #  #  #  :          0 :                 if (hdr->csum_start <= hdrlen && l4_supported != 0) {
          #  #  #  #  #  
                #  #  # ]
    1586                 :          0 :                         m->ol_flags |= RTE_MBUF_F_RX_L4_CKSUM_NONE;
    1587                 :            :                 } else {
    1588                 :            :                         /* Unknown proto or tunnel, do sw cksum. We can assume
    1589                 :            :                          * the cksum field is in the first segment since the
    1590                 :            :                          * buffers we provided to the host are large enough.
    1591                 :            :                          * In case of SCTP, this will be wrong since it's a CRC
    1592                 :            :                          * but there's nothing we can do.
    1593                 :            :                          */
    1594                 :          0 :                         uint16_t csum = 0, off;
    1595                 :            : 
    1596   [ #  #  #  #  :          0 :                         if (hdr->csum_start >= rte_pktmbuf_pkt_len(m))
                   #  # ]
    1597                 :          0 :                                 return;
    1598                 :            : 
    1599   [ #  #  #  #  :          0 :                         if (rte_raw_cksum_mbuf(m, hdr->csum_start,
                   #  # ]
    1600                 :            :                                         rte_pktmbuf_pkt_len(m) - hdr->csum_start, &csum) < 0)
    1601                 :            :                                 return;
    1602   [ #  #  #  #  :          0 :                         if (likely(csum != 0xffff))
                   #  # ]
    1603                 :          0 :                                 csum = ~csum;
    1604                 :          0 :                         off = hdr->csum_offset + hdr->csum_start;
    1605   [ #  #  #  #  :          0 :                         if (rte_pktmbuf_data_len(m) >= off + sizeof(uint16_t))
                   #  # ]
    1606                 :          0 :                                 *rte_pktmbuf_mtod_offset(m, uint16_t *, off) = csum;
    1607                 :            :                 }
    1608                 :            :         }
    1609                 :            : 
    1610   [ #  #  #  #  :          0 :         if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
                   #  # ]
    1611   [ #  #  #  #  :          0 :                 if (hdr->gso_size == 0)
                   #  # ]
    1612                 :            :                         return;
    1613                 :            : 
    1614   [ #  #  #  #  :          0 :                 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
             #  #  #  #  
                      # ]
    1615                 :          0 :                 case VIRTIO_NET_HDR_GSO_TCPV4:
    1616                 :            :                 case VIRTIO_NET_HDR_GSO_TCPV6:
    1617   [ #  #  #  #  :          0 :                         if ((ptype & RTE_PTYPE_L4_MASK) != RTE_PTYPE_L4_TCP)
                   #  # ]
    1618                 :            :                                 break;
    1619                 :          0 :                         m->ol_flags |= RTE_MBUF_F_RX_LRO | RTE_MBUF_F_RX_L4_CKSUM_NONE;
    1620                 :          0 :                         m->tso_segsz = hdr->gso_size;
    1621                 :          0 :                         break;
    1622                 :          0 :                 case VIRTIO_NET_HDR_GSO_UDP:
    1623   [ #  #  #  #  :          0 :                         if ((ptype & RTE_PTYPE_L4_MASK) != RTE_PTYPE_L4_UDP)
                   #  # ]
    1624                 :            :                                 break;
    1625                 :          0 :                         m->ol_flags |= RTE_MBUF_F_RX_LRO | RTE_MBUF_F_RX_L4_CKSUM_NONE;
    1626                 :          0 :                         m->tso_segsz = hdr->gso_size;
    1627                 :          0 :                         break;
    1628                 :            :                 default:
    1629                 :            :                         break;
    1630                 :            :                 }
    1631                 :            :         }
    1632                 :            : }
    1633                 :            : 
    1634                 :            : static __rte_always_inline int
    1635                 :            : copy_vnet_hdr_from_desc(struct virtio_net_hdr *hdr,
    1636                 :            :                         const struct buf_vector *buf_vec,
    1637                 :            :                         uint16_t nr_vec)
    1638                 :            : {
    1639                 :            :         size_t remain = sizeof(struct virtio_net_hdr);
    1640                 :            :         uint8_t *dst = (uint8_t *)hdr;
    1641                 :            : 
    1642   [ #  #  #  #  :          0 :         while (remain > 0) {
             #  #  #  # ]
    1643                 :          0 :                 size_t len = RTE_MIN(remain, buf_vec->buf_len);
    1644                 :          0 :                 const void *src = (const void *)(uintptr_t)buf_vec->buf_addr;
    1645                 :            : 
    1646   [ #  #  #  #  :          0 :                 if (unlikely(nr_vec == 0))
             #  #  #  # ]
    1647                 :            :                         return -1;
    1648                 :            : 
    1649                 :            :                 memcpy(dst, src, len);
    1650                 :          0 :                 remain -= len;
    1651                 :          0 :                 dst += len;
    1652                 :          0 :                 buf_vec++;
    1653                 :          0 :                 --nr_vec;
    1654                 :            :         }
    1655                 :            :         return 0;
    1656                 :            : }
    1657                 :            : 
    1658                 :            : static __rte_always_inline int
    1659                 :            : desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
    1660                 :            :                   struct buf_vector *buf_vec, uint16_t nr_vec,
    1661                 :            :                   struct rte_mbuf *m, struct rte_mempool *mbuf_pool,
    1662                 :            :                   bool legacy_ol_flags)
    1663                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    1664                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    1665                 :            : {
    1666                 :            :         uint32_t buf_avail, buf_offset, buf_len;
    1667                 :            :         uint64_t buf_addr;
    1668                 :            :         uint32_t mbuf_avail, mbuf_offset;
    1669   [ #  #  #  # ]:          0 :         uint32_t hdr_remain = dev->vhost_hlen;
    1670                 :            :         uint32_t cpy_len;
    1671                 :            :         struct rte_mbuf *cur = m, *prev = m;
    1672                 :            :         struct virtio_net_hdr tmp_hdr;
    1673                 :            :         struct virtio_net_hdr *hdr = NULL;
    1674                 :            :         uint16_t vec_idx;
    1675                 :            : 
    1676                 :            :         /*
    1677                 :            :          * The caller has checked the descriptors chain is larger than the
    1678                 :            :          * header size.
    1679                 :            :          */
    1680                 :            : 
    1681                 :            :         if (virtio_net_with_host_offload(dev)) {
    1682   [ #  #  #  #  :          0 :                 if (unlikely(copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec, nr_vec) != 0))
             #  #  #  # ]
    1683                 :            :                         return -1;
    1684                 :            : 
    1685                 :            :                 /* ensure that compiler does not delay copy */
    1686                 :          0 :                 rte_compiler_barrier();
    1687                 :            :                 hdr = &tmp_hdr;
    1688                 :            :         }
    1689                 :            : 
    1690   [ #  #  #  #  :          0 :         for (vec_idx = 0; vec_idx < nr_vec; vec_idx++) {
             #  #  #  # ]
    1691   [ #  #  #  #  :          0 :                 if (buf_vec[vec_idx].buf_len > hdr_remain)
             #  #  #  # ]
    1692                 :            :                         break;
    1693                 :            : 
    1694                 :          0 :                 hdr_remain -= buf_vec[vec_idx].buf_len;
    1695                 :            :         }
    1696                 :            : 
    1697                 :          0 :         buf_addr = buf_vec[vec_idx].buf_addr;
    1698                 :          0 :         buf_len = buf_vec[vec_idx].buf_len;
    1699                 :            :         buf_offset = hdr_remain;
    1700                 :          0 :         buf_avail = buf_vec[vec_idx].buf_len - hdr_remain;
    1701                 :            : 
    1702                 :            :         PRINT_PACKET(dev,
    1703                 :            :                         (uintptr_t)(buf_addr + buf_offset),
    1704                 :            :                         (uint32_t)buf_avail, 0);
    1705                 :            : 
    1706                 :            :         mbuf_offset = 0;
    1707                 :          0 :         mbuf_avail  = m->buf_len - RTE_PKTMBUF_HEADROOM;
    1708                 :            : 
    1709                 :            :         while (1) {
    1710                 :          0 :                 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
    1711                 :            : 
    1712   [ #  #  #  #  :          0 :                 if (likely((hdr && cur == m) || !vhost_can_batch_copy(vq, cpy_len))) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    1713                 :          0 :                         rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *, mbuf_offset),
    1714   [ #  #  #  #  :          0 :                                 RTE_PTR_ADD(RTE_PTR_UNQUAL(buf_addr), buf_offset),
             #  #  #  # ]
    1715                 :            :                                 cpy_len);
    1716                 :            :                 } else {
    1717                 :          0 :                         vq->batch_copy_elems[vq->batch_copy_nb_elems].dst =
    1718                 :          0 :                                 rte_pktmbuf_mtod_offset(cur, void *, mbuf_offset);
    1719                 :          0 :                         vq->batch_copy_elems[vq->batch_copy_nb_elems].src =
    1720                 :          0 :                                 RTE_PTR_ADD(RTE_PTR_UNQUAL(buf_addr), buf_offset);
    1721                 :          0 :                         vq->batch_copy_elems[vq->batch_copy_nb_elems].len = cpy_len;
    1722                 :          0 :                         vq->batch_copy_nb_elems++;
    1723                 :            :                 }
    1724                 :            : 
    1725                 :          0 :                 mbuf_avail  -= cpy_len;
    1726                 :          0 :                 mbuf_offset += cpy_len;
    1727                 :          0 :                 buf_avail -= cpy_len;
    1728                 :          0 :                 buf_offset += cpy_len;
    1729                 :            : 
    1730                 :            :                 /* This buf reaches to its end, get the next one */
    1731   [ #  #  #  #  :          0 :                 if (buf_avail == 0) {
             #  #  #  # ]
    1732   [ #  #  #  #  :          0 :                         if (++vec_idx >= nr_vec)
             #  #  #  # ]
    1733                 :            :                                 break;
    1734                 :            : 
    1735                 :          0 :                         buf_addr = buf_vec[vec_idx].buf_addr;
    1736                 :          0 :                         buf_len = buf_vec[vec_idx].buf_len;
    1737                 :            : 
    1738                 :            :                         buf_offset = 0;
    1739                 :            :                         buf_avail  = buf_len;
    1740                 :            : 
    1741                 :            :                         PRINT_PACKET(dev, (uintptr_t)buf_addr,
    1742                 :            :                                         (uint32_t)buf_avail, 0);
    1743                 :            :                 }
    1744                 :            : 
    1745                 :            :                 /*
    1746                 :            :                  * This mbuf reaches to its end, get a new one
    1747                 :            :                  * to hold more data.
    1748                 :            :                  */
    1749   [ #  #  #  #  :          0 :                 if (mbuf_avail == 0) {
             #  #  #  # ]
    1750                 :          0 :                         cur = rte_pktmbuf_alloc(mbuf_pool);
    1751   [ #  #  #  #  :          0 :                         if (unlikely(cur == NULL)) {
             #  #  #  # ]
    1752                 :          0 :                                 vq->stats.mbuf_alloc_failed++;
    1753                 :          0 :                                 VHOST_DATA_LOG(dev->ifname, ERR,
    1754                 :            :                                         "failed to allocate memory for mbuf.");
    1755                 :          0 :                                 return -1;
    1756                 :            :                         }
    1757                 :            : 
    1758                 :          0 :                         prev->next = cur;
    1759                 :          0 :                         prev->data_len = mbuf_offset;
    1760                 :          0 :                         m->nb_segs += 1;
    1761                 :          0 :                         m->pkt_len += mbuf_offset;
    1762                 :            :                         prev = cur;
    1763                 :            : 
    1764                 :            :                         mbuf_offset = 0;
    1765                 :          0 :                         mbuf_avail  = cur->buf_len - RTE_PKTMBUF_HEADROOM;
    1766                 :            :                 }
    1767                 :            :         }
    1768                 :            : 
    1769                 :          0 :         prev->data_len = mbuf_offset;
    1770                 :          0 :         m->pkt_len    += mbuf_offset;
    1771                 :            : 
    1772   [ #  #  #  #  :          0 :         if (hdr)
             #  #  #  # ]
    1773                 :            :                 vhost_dequeue_offload(dev, hdr, m, legacy_ol_flags);
    1774                 :            : 
    1775                 :            :         return 0;
    1776                 :            : }
    1777                 :            : 
    1778                 :            : static void
    1779                 :          0 : virtio_dev_extbuf_free(void *addr __rte_unused, void *opaque)
    1780                 :            : {
    1781                 :          0 :         rte_free(opaque);
    1782                 :          0 : }
    1783                 :            : 
    1784                 :            : static int
    1785                 :          0 : virtio_dev_extbuf_alloc(struct virtio_net *dev, struct rte_mbuf *pkt, uint32_t size)
    1786                 :            : {
    1787                 :            :         struct rte_mbuf_ext_shared_info *shinfo = NULL;
    1788                 :            :         uint32_t total_len = RTE_PKTMBUF_HEADROOM + size;
    1789                 :            :         uint16_t buf_len;
    1790                 :            :         rte_iova_t iova;
    1791                 :            :         void *buf;
    1792                 :            : 
    1793                 :            :         total_len += sizeof(*shinfo) + sizeof(uintptr_t);
    1794                 :          0 :         total_len = RTE_ALIGN_CEIL(total_len, sizeof(uintptr_t));
    1795                 :            : 
    1796         [ #  # ]:          0 :         if (unlikely(total_len > UINT16_MAX))
    1797                 :            :                 return -ENOSPC;
    1798                 :            : 
    1799                 :          0 :         buf_len = total_len;
    1800                 :          0 :         buf = rte_malloc(NULL, buf_len, RTE_CACHE_LINE_SIZE);
    1801         [ #  # ]:          0 :         if (unlikely(buf == NULL))
    1802                 :            :                 return -ENOMEM;
    1803                 :            : 
    1804                 :            :         /* Initialize shinfo */
    1805                 :            :         shinfo = rte_pktmbuf_ext_shinfo_init_helper(buf, &buf_len,
    1806                 :            :                                                 virtio_dev_extbuf_free, buf);
    1807         [ #  # ]:          0 :         if (unlikely(shinfo == NULL)) {
    1808                 :          0 :                 rte_free(buf);
    1809                 :          0 :                 VHOST_DATA_LOG(dev->ifname, ERR, "failed to init shinfo");
    1810                 :          0 :                 return -1;
    1811                 :            :         }
    1812                 :            : 
    1813                 :          0 :         iova = rte_malloc_virt2iova(buf);
    1814                 :            :         rte_pktmbuf_attach_extbuf(pkt, buf, iova, buf_len, shinfo);
    1815                 :            :         rte_pktmbuf_reset_headroom(pkt);
    1816                 :            : 
    1817                 :          0 :         return 0;
    1818                 :            : }
    1819                 :            : 
    1820                 :            : /*
    1821                 :            :  * Prepare a host supported pktmbuf.
    1822                 :            :  */
    1823                 :            : static __rte_always_inline int
    1824                 :            : virtio_dev_pktmbuf_prep(struct virtio_net *dev, struct rte_mbuf *pkt,
    1825                 :            :                          uint32_t data_len)
    1826                 :            : {
    1827   [ #  #  #  #  :          0 :         if (rte_pktmbuf_tailroom(pkt) >= data_len)
          #  #  #  #  #  
                #  #  # ]
    1828                 :            :                 return 0;
    1829                 :            : 
    1830                 :            :         /* attach an external buffer if supported */
    1831   [ #  #  #  #  :          0 :         if (dev->extbuf && !virtio_dev_extbuf_alloc(dev, pkt, data_len))
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    1832                 :            :                 return 0;
    1833                 :            : 
    1834                 :            :         /* check if chained buffers are allowed */
    1835   [ #  #  #  #  :          0 :         if (!dev->linearbuf)
          #  #  #  #  #  
                #  #  # ]
    1836                 :          0 :                 return 0;
    1837                 :            : 
    1838                 :            :         return -1;
    1839                 :            : }
    1840                 :            : 
    1841                 :            : __rte_always_inline
    1842                 :            : static uint16_t
    1843                 :            : virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
    1844                 :            :         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count,
    1845                 :            :         bool legacy_ol_flags)
    1846                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    1847                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    1848                 :            : {
    1849                 :            :         uint16_t i;
    1850                 :            :         uint16_t avail_entries;
    1851                 :            :         static bool allocerr_warned;
    1852                 :            : 
    1853                 :            :         /*
    1854                 :            :          * The ordering between avail index and
    1855                 :            :          * desc reads needs to be enforced.
    1856                 :            :          */
    1857                 :          0 :         avail_entries = rte_atomic_load_explicit((unsigned short __rte_atomic *)&vq->avail->idx,
    1858                 :          0 :                 rte_memory_order_acquire) - vq->last_avail_idx;
    1859                 :          0 :         if (avail_entries == 0)
    1860                 :            :                 return 0;
    1861                 :            : 
    1862                 :          0 :         rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
    1863                 :            : 
    1864                 :            :         VHOST_DATA_LOG(dev->ifname, DEBUG, "%s", __func__);
    1865                 :            : 
    1866                 :          0 :         count = RTE_MIN(count, MAX_PKT_BURST);
    1867                 :          0 :         count = RTE_MIN(count, avail_entries);
    1868                 :            :         VHOST_DATA_LOG(dev->ifname, DEBUG, "about to dequeue %u buffers", count);
    1869                 :            : 
    1870   [ #  #  #  # ]:          0 :         if (rte_pktmbuf_alloc_bulk(mbuf_pool, pkts, count)) {
    1871                 :          0 :                 vq->stats.mbuf_alloc_failed += count;
    1872                 :          0 :                 return 0;
    1873                 :            :         }
    1874                 :            : 
    1875   [ #  #  #  # ]:          0 :         for (i = 0; i < count; i++) {
    1876                 :            :                 struct buf_vector buf_vec[BUF_VECTOR_MAX];
    1877                 :            :                 uint16_t head_idx;
    1878                 :            :                 uint32_t buf_len;
    1879                 :            :                 uint16_t nr_vec = 0;
    1880                 :            :                 int err;
    1881                 :            : 
    1882   [ #  #  #  #  :          0 :                 if (unlikely(fill_vec_buf_split(dev, vq,
             #  #  #  # ]
    1883                 :            :                                                 vq->last_avail_idx + i,
    1884                 :            :                                                 &nr_vec, buf_vec,
    1885                 :            :                                                 &head_idx, &buf_len,
    1886                 :            :                                                 VHOST_ACCESS_RO) < 0))
    1887                 :            :                         break;
    1888                 :            : 
    1889                 :            :                 update_shadow_used_ring_split(vq, head_idx, 0);
    1890                 :            : 
    1891   [ #  #  #  # ]:          0 :                 if (unlikely(buf_len <= dev->vhost_hlen))
    1892                 :            :                         break;
    1893                 :            : 
    1894                 :          0 :                 buf_len -= dev->vhost_hlen;
    1895                 :            : 
    1896   [ #  #  #  # ]:          0 :                 err = virtio_dev_pktmbuf_prep(dev, pkts[i], buf_len);
    1897   [ #  #  #  # ]:          0 :                 if (unlikely(err)) {
    1898                 :            :                         /*
    1899                 :            :                          * mbuf allocation fails for jumbo packets when external
    1900                 :            :                          * buffer allocation is not allowed and linear buffer
    1901                 :            :                          * is required. Drop this packet.
    1902                 :            :                          */
    1903   [ #  #  #  # ]:          0 :                         if (!allocerr_warned) {
    1904                 :          0 :                                 VHOST_DATA_LOG(dev->ifname, ERR,
    1905                 :            :                                         "failed mbuf alloc of size %d from %s.",
    1906                 :            :                                         buf_len, mbuf_pool->name);
    1907                 :          0 :                                 allocerr_warned = true;
    1908                 :            :                         }
    1909                 :            :                         break;
    1910                 :            :                 }
    1911                 :            : 
    1912   [ #  #  #  # ]:          0 :                 err = desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i], mbuf_pool, legacy_ol_flags);
    1913   [ #  #  #  # ]:          0 :                 if (unlikely(err)) {
    1914   [ #  #  #  # ]:          0 :                         if (!allocerr_warned) {
    1915                 :          0 :                                 VHOST_DATA_LOG(dev->ifname, ERR, "failed to copy desc to mbuf.");
    1916                 :          0 :                                 allocerr_warned = true;
    1917                 :            :                         }
    1918                 :            :                         break;
    1919                 :            :                 }
    1920                 :            :         }
    1921                 :            : 
    1922   [ #  #  #  # ]:          0 :         if (unlikely(count != i))
    1923                 :          0 :                 rte_pktmbuf_free_bulk(&pkts[i], count - i);
    1924                 :            : 
    1925   [ #  #  #  # ]:          0 :         if (likely(vq->shadow_used_idx)) {
    1926   [ #  #  #  # ]:          0 :                 vq->last_avail_idx += vq->shadow_used_idx;
    1927                 :            :                 vhost_virtqueue_reconnect_log_split(vq);
    1928                 :          0 :                 do_data_copy_dequeue(vq);
    1929                 :            :                 flush_shadow_used_ring_split(dev, vq);
    1930                 :            :                 vhost_vring_call_split(dev, vq);
    1931                 :            :         }
    1932                 :            : 
    1933                 :            :         return i;
    1934                 :            : }
    1935                 :            : 
    1936                 :            : __rte_noinline
    1937                 :            : static uint16_t
    1938                 :          0 : virtio_dev_tx_split_legacy(struct virtio_net *dev,
    1939                 :            :         struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
    1940                 :            :         struct rte_mbuf **pkts, uint16_t count)
    1941                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    1942                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    1943                 :            : {
    1944         [ #  # ]:          0 :         return virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count, true);
    1945                 :            : }
    1946                 :            : 
    1947                 :            : __rte_noinline
    1948                 :            : static uint16_t
    1949                 :          0 : virtio_dev_tx_split_compliant(struct virtio_net *dev,
    1950                 :            :         struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
    1951                 :            :         struct rte_mbuf **pkts, uint16_t count)
    1952                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    1953                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    1954                 :            : {
    1955         [ #  # ]:          0 :         return virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count, false);
    1956                 :            : }
    1957                 :            : 
    1958                 :            : static __rte_always_inline int
    1959                 :            : virtio_dev_tx_batch_packed_check(struct virtio_net *dev, struct vhost_virtqueue *vq,
    1960                 :            :                 struct rte_mbuf **pkts, uint16_t avail_idx, uintptr_t *desc_addrs, uint16_t *ids)
    1961                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    1962                 :            : {
    1963                 :          0 :         bool wrap = vq->avail_wrap_counter;
    1964                 :            :         struct vring_packed_desc *descs = vq->desc_packed;
    1965                 :            :         uint64_t lens[PACKED_BATCH_SIZE];
    1966                 :            :         uint64_t buf_lens[PACKED_BATCH_SIZE];
    1967                 :            :         uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
    1968                 :            :         uint16_t flags, i;
    1969                 :            : 
    1970                 :          0 :         if (unlikely(avail_idx & PACKED_BATCH_MASK))
    1971                 :            :                 return -1;
    1972   [ #  #  #  # ]:          0 :         if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
    1973                 :            :                 return -1;
    1974                 :            : 
    1975   [ #  #  #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    1976                 :          0 :                 flags = descs[avail_idx + i].flags;
    1977   [ #  #  #  #  :          0 :                 if (unlikely((wrap != !!(flags & VRING_DESC_F_AVAIL)) ||
          #  #  #  #  #  
                #  #  # ]
    1978                 :            :                              (wrap == !!(flags & VRING_DESC_F_USED))  ||
    1979                 :            :                              (flags & PACKED_DESC_SINGLE_DEQUEUE_FLAG)))
    1980                 :            :                         return -1;
    1981                 :            :         }
    1982                 :            : 
    1983                 :            :         rte_atomic_thread_fence(rte_memory_order_acquire);
    1984                 :            : 
    1985   [ #  #  #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
    1986                 :          0 :                 lens[i] = descs[avail_idx + i].len;
    1987                 :            : 
    1988   [ #  #  #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    1989                 :          0 :                 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
    1990                 :          0 :                                                   descs[avail_idx + i].addr,
    1991   [ #  #  #  # ]:          0 :                                                   &lens[i], VHOST_ACCESS_RW);
    1992                 :            :         }
    1993                 :            : 
    1994   [ #  #  #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    1995   [ #  #  #  # ]:          0 :                 if (unlikely(!desc_addrs[i]))
    1996                 :            :                         return -1;
    1997   [ #  #  #  # ]:          0 :                 if (unlikely((lens[i] != descs[avail_idx + i].len)))
    1998                 :            :                         return -1;
    1999                 :            :         }
    2000                 :            : 
    2001   [ #  #  #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    2002   [ #  #  #  # ]:          0 :                 if (virtio_dev_pktmbuf_prep(dev, pkts[i], lens[i]))
    2003                 :          0 :                         goto err;
    2004                 :            :         }
    2005                 :            : 
    2006   [ #  #  #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
    2007                 :          0 :                 buf_lens[i] = pkts[i]->buf_len - pkts[i]->data_off;
    2008                 :            : 
    2009   [ #  #  #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    2010   [ #  #  #  # ]:          0 :                 if (unlikely(buf_lens[i] < (lens[i] - buf_offset)))
    2011                 :          0 :                         goto err;
    2012                 :            :         }
    2013                 :            : 
    2014   [ #  #  #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    2015                 :          0 :                 pkts[i]->pkt_len = lens[i] - buf_offset;
    2016                 :          0 :                 pkts[i]->data_len = pkts[i]->pkt_len;
    2017                 :          0 :                 ids[i] = descs[avail_idx + i].id;
    2018                 :            :         }
    2019                 :            : 
    2020                 :            :         return 0;
    2021                 :            : 
    2022                 :            : err:
    2023                 :            :         return -1;
    2024                 :            : }
    2025                 :            : 
    2026                 :            : static __rte_always_inline int
    2027                 :            : virtio_dev_tx_batch_packed(struct virtio_net *dev,
    2028                 :            :                            struct vhost_virtqueue *vq,
    2029                 :            :                            struct rte_mbuf **pkts,
    2030                 :            :                            bool legacy_ol_flags)
    2031                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    2032                 :            : {
    2033                 :            :         uint16_t avail_idx = vq->last_avail_idx;
    2034                 :            :         uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
    2035                 :            :         uintptr_t desc_addrs[PACKED_BATCH_SIZE];
    2036                 :            :         uint16_t ids[PACKED_BATCH_SIZE];
    2037                 :            :         uint16_t i;
    2038                 :            : 
    2039   [ #  #  #  # ]:          0 :         if (virtio_dev_tx_batch_packed_check(dev, vq, pkts, avail_idx, desc_addrs, ids))
    2040                 :            :                 return -1;
    2041                 :            : 
    2042   [ #  #  #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
    2043                 :          0 :                 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
    2044                 :            : 
    2045   [ #  #  #  # ]:          0 :         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
    2046                 :          0 :                 rte_memcpy(rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
    2047                 :          0 :                            (void *)(uintptr_t)(desc_addrs[i] + buf_offset),
    2048   [ #  #  #  # ]:          0 :                            pkts[i]->pkt_len);
    2049                 :            : 
    2050                 :            :         if (virtio_net_with_host_offload(dev)) {
    2051   [ #  #  #  # ]:          0 :                 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
    2052                 :            :                         struct virtio_net_hdr hdr;
    2053                 :            : 
    2054                 :          0 :                         memcpy(&hdr, (void *)desc_addrs[i], sizeof(struct virtio_net_hdr));
    2055                 :          0 :                         rte_compiler_barrier();
    2056                 :            : 
    2057   [ #  #  #  # ]:          0 :                         vhost_dequeue_offload(dev, &hdr, pkts[i], legacy_ol_flags);
    2058                 :            :                 }
    2059                 :            :         }
    2060                 :            : 
    2061   [ #  #  #  # ]:          0 :         if (virtio_net_is_inorder(dev))
    2062                 :            :                 vhost_shadow_dequeue_batch_packed_inorder(vq,
    2063   [ #  #  #  # ]:          0 :                         ids[PACKED_BATCH_SIZE - 1]);
    2064                 :            :         else
    2065                 :            :                 vhost_shadow_dequeue_batch_packed(dev, vq, ids);
    2066                 :            : 
    2067                 :            :         vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
    2068                 :            : 
    2069                 :            :         return 0;
    2070                 :            : }
    2071                 :            : 
    2072                 :            : static __rte_always_inline int
    2073                 :            : vhost_dequeue_single_packed(struct virtio_net *dev,
    2074                 :            :                             struct vhost_virtqueue *vq,
    2075                 :            :                             struct rte_mempool *mbuf_pool,
    2076                 :            :                             struct rte_mbuf *pkts,
    2077                 :            :                             uint16_t *buf_id,
    2078                 :            :                             uint16_t *desc_count,
    2079                 :            :                             bool legacy_ol_flags)
    2080                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    2081                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    2082                 :            : {
    2083                 :            :         struct buf_vector buf_vec[BUF_VECTOR_MAX];
    2084                 :            :         uint32_t buf_len;
    2085                 :            :         uint16_t nr_vec = 0;
    2086                 :            :         int err;
    2087                 :            :         static bool allocerr_warned;
    2088                 :            : 
    2089   [ #  #  #  # ]:          0 :         if (unlikely(fill_vec_buf_packed(dev, vq,
    2090                 :            :                                          vq->last_avail_idx, desc_count,
    2091                 :            :                                          buf_vec, &nr_vec,
    2092                 :            :                                          buf_id, &buf_len,
    2093                 :            :                                          VHOST_ACCESS_RO) < 0))
    2094                 :            :                 return -1;
    2095                 :            : 
    2096   [ #  #  #  # ]:          0 :         if (unlikely(buf_len <= dev->vhost_hlen))
    2097                 :            :                 return -1;
    2098                 :            : 
    2099   [ #  #  #  # ]:          0 :         buf_len -= dev->vhost_hlen;
    2100                 :            : 
    2101   [ #  #  #  # ]:          0 :         if (unlikely(virtio_dev_pktmbuf_prep(dev, pkts, buf_len))) {
    2102   [ #  #  #  # ]:          0 :                 if (!allocerr_warned) {
    2103                 :          0 :                         VHOST_DATA_LOG(dev->ifname, ERR, "failed mbuf alloc of size %d from %s.",
    2104                 :            :                                 buf_len, mbuf_pool->name);
    2105                 :          0 :                         allocerr_warned = true;
    2106                 :            :                 }
    2107                 :            :                 return -1;
    2108                 :            :         }
    2109                 :            : 
    2110                 :            :         err = desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts, mbuf_pool, legacy_ol_flags);
    2111   [ #  #  #  # ]:          0 :         if (unlikely(err)) {
    2112   [ #  #  #  # ]:          0 :                 if (!allocerr_warned) {
    2113                 :          0 :                         VHOST_DATA_LOG(dev->ifname, ERR, "failed to copy desc to mbuf.");
    2114                 :          0 :                         allocerr_warned = true;
    2115                 :            :                 }
    2116                 :            :                 return -1;
    2117                 :            :         }
    2118                 :            : 
    2119                 :            :         return 0;
    2120                 :            : }
    2121                 :            : 
    2122                 :            : static __rte_always_inline int
    2123                 :            : virtio_dev_tx_single_packed(struct virtio_net *dev,
    2124                 :            :                             struct vhost_virtqueue *vq,
    2125                 :            :                             struct rte_mempool *mbuf_pool,
    2126                 :            :                             struct rte_mbuf *pkts,
    2127                 :            :                             bool legacy_ol_flags)
    2128                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    2129                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    2130                 :            : {
    2131                 :            : 
    2132                 :            :         uint16_t buf_id, desc_count = 0;
    2133                 :            :         int ret;
    2134                 :            : 
    2135                 :            :         ret = vhost_dequeue_single_packed(dev, vq, mbuf_pool, pkts, &buf_id,
    2136                 :            :                                         &desc_count, legacy_ol_flags);
    2137                 :            : 
    2138   [ #  #  #  # ]:          0 :         if (likely(desc_count > 0)) {
    2139   [ #  #  #  # ]:          0 :                 if (virtio_net_is_inorder(dev))
    2140                 :            :                         vhost_shadow_dequeue_single_packed_inorder(vq, buf_id,
    2141                 :            :                                                                    desc_count);
    2142                 :            :                 else
    2143                 :            :                         vhost_shadow_dequeue_single_packed(vq, buf_id,
    2144                 :            :                                         desc_count);
    2145                 :            : 
    2146                 :            :                 vq_inc_last_avail_packed(vq, desc_count);
    2147                 :            :         }
    2148                 :            : 
    2149                 :            :         return ret;
    2150                 :            : }
    2151                 :            : 
    2152                 :            : static __rte_always_inline uint16_t
    2153                 :            : get_nb_avail_entries_packed(const struct vhost_virtqueue *__rte_restrict vq,
    2154                 :            :                             uint16_t max_nb_avail_entries)
    2155                 :            : {
    2156                 :          0 :         const struct vring_packed_desc *descs = vq->desc_packed;
    2157                 :          0 :         bool avail_wrap = vq->avail_wrap_counter;
    2158                 :          0 :         uint16_t avail_idx = vq->last_avail_idx;
    2159                 :            :         uint16_t nb_avail_entries = 0;
    2160                 :            :         uint16_t flags;
    2161                 :            : 
    2162   [ #  #  #  # ]:          0 :         while (nb_avail_entries < max_nb_avail_entries) {
    2163                 :          0 :                 flags = descs[avail_idx].flags;
    2164                 :            : 
    2165   [ #  #  #  # ]:          0 :                 if ((avail_wrap != !!(flags & VRING_DESC_F_AVAIL)) ||
    2166   [ #  #  #  # ]:          0 :                     (avail_wrap == !!(flags & VRING_DESC_F_USED)))
    2167                 :            :                         return nb_avail_entries;
    2168                 :            : 
    2169   [ #  #  #  # ]:          0 :                 if (!(flags & VRING_DESC_F_NEXT))
    2170                 :          0 :                         ++nb_avail_entries;
    2171                 :            : 
    2172   [ #  #  #  # ]:          0 :                 if (unlikely(++avail_idx >= vq->size)) {
    2173                 :          0 :                         avail_idx -= vq->size;
    2174                 :          0 :                         avail_wrap = !avail_wrap;
    2175                 :            :                 }
    2176                 :            :         }
    2177                 :            : 
    2178                 :            :         return nb_avail_entries;
    2179                 :            : }
    2180                 :            : 
    2181                 :            : __rte_always_inline
    2182                 :            : static uint16_t
    2183                 :            : virtio_dev_tx_packed(struct virtio_net *dev,
    2184                 :            :                      struct vhost_virtqueue *__rte_restrict vq,
    2185                 :            :                      struct rte_mempool *mbuf_pool,
    2186                 :            :                      struct rte_mbuf **__rte_restrict pkts,
    2187                 :            :                      uint32_t count,
    2188                 :            :                      bool legacy_ol_flags)
    2189                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    2190                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    2191                 :            : {
    2192                 :            :         uint32_t pkt_idx = 0;
    2193                 :            : 
    2194                 :          0 :         count = get_nb_avail_entries_packed(vq, count);
    2195   [ #  #  #  # ]:          0 :         if (count == 0)
    2196                 :            :                 return 0;
    2197                 :            : 
    2198   [ #  #  #  # ]:          0 :         if (rte_pktmbuf_alloc_bulk(mbuf_pool, pkts, count)) {
    2199                 :          0 :                 vq->stats.mbuf_alloc_failed += count;
    2200                 :          0 :                 return 0;
    2201                 :            :         }
    2202                 :            : 
    2203                 :            :         do {
    2204                 :          0 :                 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
    2205                 :            : 
    2206   [ #  #  #  # ]:          0 :                 if (count - pkt_idx >= PACKED_BATCH_SIZE) {
    2207                 :          0 :                         if (!virtio_dev_tx_batch_packed(dev, vq,
    2208   [ #  #  #  # ]:          0 :                                                         &pkts[pkt_idx],
    2209                 :            :                                                         legacy_ol_flags)) {
    2210                 :          0 :                                 pkt_idx += PACKED_BATCH_SIZE;
    2211                 :          0 :                                 continue;
    2212                 :            :                         }
    2213                 :            :                 }
    2214                 :            : 
    2215   [ #  #  #  # ]:          0 :                 if (virtio_dev_tx_single_packed(dev, vq, mbuf_pool,
    2216                 :          0 :                                                 pkts[pkt_idx],
    2217                 :            :                                                 legacy_ol_flags))
    2218                 :            :                         break;
    2219                 :          0 :                 pkt_idx++;
    2220   [ #  #  #  # ]:          0 :         } while (pkt_idx < count);
    2221                 :            : 
    2222   [ #  #  #  # ]:          0 :         if (pkt_idx != count)
    2223                 :          0 :                 rte_pktmbuf_free_bulk(&pkts[pkt_idx], count - pkt_idx);
    2224                 :            : 
    2225   [ #  #  #  # ]:          0 :         if (vq->shadow_used_idx) {
    2226                 :          0 :                 do_data_copy_dequeue(vq);
    2227                 :            : 
    2228                 :            :                 vhost_flush_dequeue_shadow_packed(dev, vq);
    2229                 :            :                 vhost_vring_call_packed(dev, vq);
    2230                 :            :         }
    2231                 :            : 
    2232                 :          0 :         return pkt_idx;
    2233                 :            : }
    2234                 :            : 
    2235                 :            : __rte_noinline
    2236                 :            : static uint16_t
    2237                 :          0 : virtio_dev_tx_packed_legacy(struct virtio_net *dev,
    2238                 :            :         struct vhost_virtqueue *__rte_restrict vq, struct rte_mempool *mbuf_pool,
    2239                 :            :         struct rte_mbuf **__rte_restrict pkts, uint32_t count)
    2240                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    2241                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    2242                 :            : {
    2243                 :          0 :         return virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count, true);
    2244                 :            : }
    2245                 :            : 
    2246                 :            : __rte_noinline
    2247                 :            : static uint16_t
    2248                 :          0 : virtio_dev_tx_packed_compliant(struct virtio_net *dev,
    2249                 :            :         struct vhost_virtqueue *__rte_restrict vq, struct rte_mempool *mbuf_pool,
    2250                 :            :         struct rte_mbuf **__rte_restrict pkts, uint32_t count)
    2251                 :            :         __rte_requires_shared_capability(&vq->access_lock)
    2252                 :            :         __rte_requires_shared_capability(&vq->iotlb_lock)
    2253                 :            : {
    2254                 :          0 :         return virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count, false);
    2255                 :            : }
    2256                 :            : 
    2257                 :            : RTE_EXPORT_SYMBOL(rte_vhost_dequeue_burst)
    2258                 :            : uint16_t
    2259         [ #  # ]:          0 : rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
    2260                 :            :         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
    2261                 :            : {
    2262                 :            :         struct virtio_net *dev;
    2263                 :            :         struct vhost_virtqueue *vq;
    2264                 :            :         int16_t success = 1;
    2265                 :            :         uint16_t nb_rx = 0;
    2266                 :            : 
    2267                 :            :         dev = get_device(vid);
    2268         [ #  # ]:          0 :         if (!dev)
    2269                 :            :                 return 0;
    2270                 :            : 
    2271         [ #  # ]:          0 :         if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
    2272                 :          0 :                 VHOST_DATA_LOG(dev->ifname, ERR, "%s: built-in vhost net backend is disabled.",
    2273                 :            :                         __func__);
    2274                 :          0 :                 goto out_no_unlock;
    2275                 :            :         }
    2276                 :            : 
    2277   [ #  #  #  # ]:          0 :         if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
    2278                 :          0 :                 VHOST_DATA_LOG(dev->ifname, ERR, "%s: invalid virtqueue idx %d.",
    2279                 :            :                         __func__, queue_id);
    2280                 :          0 :                 goto out_no_unlock;
    2281                 :            :         }
    2282                 :            : 
    2283                 :          0 :         vq = dev->virtqueue[queue_id];
    2284                 :            : 
    2285         [ #  # ]:          0 :         if (unlikely(rte_rwlock_read_trylock(&vq->access_lock) != 0))
    2286                 :          0 :                 goto out_no_unlock;
    2287                 :            : 
    2288         [ #  # ]:          0 :         if (unlikely(!vq->enabled))
    2289                 :          0 :                 goto out_access_unlock;
    2290                 :            : 
    2291                 :            :         vhost_user_iotlb_rd_lock(vq);
    2292                 :            : 
    2293         [ #  # ]:          0 :         if (unlikely(!vq->access_ok)) {
    2294                 :            :                 vhost_user_iotlb_rd_unlock(vq);
    2295                 :            :                 rte_rwlock_read_unlock(&vq->access_lock);
    2296                 :            : 
    2297                 :          0 :                 virtio_dev_vring_translate(dev, vq);
    2298                 :          0 :                 goto out_no_unlock;
    2299                 :            :         }
    2300                 :            : 
    2301                 :            :         /*
    2302                 :            :          * Construct a RARP broadcast packet, and inject it to the "pkts"
    2303                 :            :          * array, to looks like that guest actually send such packet.
    2304                 :            :          *
    2305                 :            :          * Check user_send_rarp() for more information.
    2306                 :            :          *
    2307                 :            :          * broadcast_rarp shares a cacheline in the virtio_net structure
    2308                 :            :          * with some fields that are accessed during enqueue and
    2309                 :            :          * rte_atomic_compare_exchange_strong_explicit causes a write if performed compare
    2310                 :            :          * and exchange. This could result in false sharing between enqueue
    2311                 :            :          * and dequeue.
    2312                 :            :          *
    2313                 :            :          * Prevent unnecessary false sharing by reading broadcast_rarp first
    2314                 :            :          * and only performing compare and exchange if the read indicates it
    2315                 :            :          * is likely to be set.
    2316                 :            :          */
    2317   [ #  #  #  # ]:          0 :         if (unlikely(rte_atomic_load_explicit(&dev->broadcast_rarp, rte_memory_order_acquire) &&
    2318                 :            :                         rte_atomic_compare_exchange_strong_explicit(&dev->broadcast_rarp,
    2319                 :            :                         &success, 0, rte_memory_order_release, rte_memory_order_relaxed))) {
    2320                 :            :                 /*
    2321                 :            :                  * Inject the RARP packet to the head of "pkts" array,
    2322                 :            :                  * so that switch's mac learning table will get updated first.
    2323                 :            :                  */
    2324                 :          0 :                 pkts[nb_rx] = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
    2325         [ #  # ]:          0 :                 if (pkts[nb_rx] == NULL) {
    2326                 :          0 :                         VHOST_DATA_LOG(dev->ifname, ERR, "failed to make RARP packet.");
    2327                 :          0 :                         goto out;
    2328                 :            :                 }
    2329                 :            :                 nb_rx += 1;
    2330                 :            :         }
    2331                 :            : 
    2332         [ #  # ]:          0 :         if (vq_is_packed(dev)) {
    2333         [ #  # ]:          0 :                 if (dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS)
    2334                 :          0 :                         nb_rx += virtio_dev_tx_packed_legacy(dev, vq, mbuf_pool,
    2335                 :          0 :                                         pkts + nb_rx, count - nb_rx);
    2336                 :            :                 else
    2337                 :          0 :                         nb_rx += virtio_dev_tx_packed_compliant(dev, vq, mbuf_pool,
    2338                 :          0 :                                         pkts + nb_rx, count - nb_rx);
    2339                 :            :         } else {
    2340         [ #  # ]:          0 :                 if (dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS)
    2341                 :          0 :                         nb_rx += virtio_dev_tx_split_legacy(dev, vq, mbuf_pool,
    2342                 :          0 :                                         pkts + nb_rx, count - nb_rx);
    2343                 :            :                 else
    2344                 :          0 :                         nb_rx += virtio_dev_tx_split_compliant(dev, vq, mbuf_pool,
    2345                 :          0 :                                         pkts + nb_rx, count - nb_rx);
    2346                 :            :         }
    2347                 :            : 
    2348                 :          0 :         vhost_queue_stats_update(dev, vq, pkts, nb_rx);
    2349                 :            : 
    2350                 :          0 : out:
    2351                 :            :         vhost_user_iotlb_rd_unlock(vq);
    2352                 :            : 
    2353                 :          0 : out_access_unlock:
    2354                 :            :         rte_rwlock_read_unlock(&vq->access_lock);
    2355                 :            : 
    2356                 :            : out_no_unlock:
    2357                 :            :         return nb_rx;
    2358                 :            : }

Generated by: LCOV version 1.14