crypto/cnxk: prevent out-of-bound access in capabilities
[dpdk.git] / lib / vhost / virtio_net.c
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 <rte_mbuf.h>
10 #include <rte_memcpy.h>
11 #include <rte_net.h>
12 #include <rte_ether.h>
13 #include <rte_ip.h>
14 #include <rte_dmadev.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_spinlock.h>
21 #include <rte_malloc.h>
22 #include <rte_vhost_async.h>
23
24 #include "iotlb.h"
25 #include "vhost.h"
26
27 #define MAX_BATCH_LEN 256
28
29 /* DMA device copy operation tracking array. */
30 struct async_dma_info dma_copy_track[RTE_DMADEV_DEFAULT_MAX];
31
32 static  __rte_always_inline bool
33 rxvq_is_mergeable(struct virtio_net *dev)
34 {
35         return dev->features & (1ULL << VIRTIO_NET_F_MRG_RXBUF);
36 }
37
38 static  __rte_always_inline bool
39 virtio_net_is_inorder(struct virtio_net *dev)
40 {
41         return dev->features & (1ULL << VIRTIO_F_IN_ORDER);
42 }
43
44 static bool
45 is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t nr_vring)
46 {
47         return (is_tx ^ (idx & 1)) == 0 && idx < nr_vring;
48 }
49
50 static __rte_always_inline int64_t
51 vhost_async_dma_transfer_one(struct virtio_net *dev, struct vhost_virtqueue *vq,
52                 int16_t dma_id, uint16_t vchan_id, uint16_t flag_idx,
53                 struct vhost_iov_iter *pkt)
54 {
55         struct async_dma_vchan_info *dma_info = &dma_copy_track[dma_id].vchans[vchan_id];
56         uint16_t ring_mask = dma_info->ring_mask;
57         static bool vhost_async_dma_copy_log;
58
59
60         struct vhost_iovec *iov = pkt->iov;
61         int copy_idx = 0;
62         uint32_t nr_segs = pkt->nr_segs;
63         uint16_t i;
64
65         if (rte_dma_burst_capacity(dma_id, vchan_id) < nr_segs)
66                 return -1;
67
68         for (i = 0; i < nr_segs; i++) {
69                 copy_idx = rte_dma_copy(dma_id, vchan_id, (rte_iova_t)iov[i].src_addr,
70                                 (rte_iova_t)iov[i].dst_addr, iov[i].len, RTE_DMA_OP_FLAG_LLC);
71                 /**
72                  * Since all memory is pinned and DMA vChannel
73                  * ring has enough space, failure should be a
74                  * rare case. If failure happens, it means DMA
75                  * device encounters serious errors; in this
76                  * case, please stop async data-path and check
77                  * what has happened to DMA device.
78                  */
79                 if (unlikely(copy_idx < 0)) {
80                         if (!vhost_async_dma_copy_log) {
81                                 VHOST_LOG_DATA(ERR, "(%s) DMA copy failed for channel %d:%u\n",
82                                                 dev->ifname, dma_id, vchan_id);
83                                 vhost_async_dma_copy_log = true;
84                         }
85                         return -1;
86                 }
87         }
88
89         /**
90          * Only store packet completion flag address in the last copy's
91          * slot, and other slots are set to NULL.
92          */
93         dma_info->pkts_cmpl_flag_addr[copy_idx & ring_mask] = &vq->async->pkts_cmpl_flag[flag_idx];
94
95         return nr_segs;
96 }
97
98 static __rte_always_inline uint16_t
99 vhost_async_dma_transfer(struct virtio_net *dev, struct vhost_virtqueue *vq,
100                 int16_t dma_id, uint16_t vchan_id, uint16_t head_idx,
101                 struct vhost_iov_iter *pkts, uint16_t nr_pkts)
102 {
103         struct async_dma_vchan_info *dma_info = &dma_copy_track[dma_id].vchans[vchan_id];
104         int64_t ret, nr_copies = 0;
105         uint16_t pkt_idx;
106
107         rte_spinlock_lock(&dma_info->dma_lock);
108
109         for (pkt_idx = 0; pkt_idx < nr_pkts; pkt_idx++) {
110                 ret = vhost_async_dma_transfer_one(dev, vq, dma_id, vchan_id, head_idx,
111                                 &pkts[pkt_idx]);
112                 if (unlikely(ret < 0))
113                         break;
114
115                 nr_copies += ret;
116                 head_idx++;
117                 if (head_idx >= vq->size)
118                         head_idx -= vq->size;
119         }
120
121         if (likely(nr_copies > 0))
122                 rte_dma_submit(dma_id, vchan_id);
123
124         rte_spinlock_unlock(&dma_info->dma_lock);
125
126         return pkt_idx;
127 }
128
129 static __rte_always_inline uint16_t
130 vhost_async_dma_check_completed(struct virtio_net *dev, int16_t dma_id, uint16_t vchan_id,
131                 uint16_t max_pkts)
132 {
133         struct async_dma_vchan_info *dma_info = &dma_copy_track[dma_id].vchans[vchan_id];
134         uint16_t ring_mask = dma_info->ring_mask;
135         uint16_t last_idx = 0;
136         uint16_t nr_copies;
137         uint16_t copy_idx;
138         uint16_t i;
139         bool has_error = false;
140         static bool vhost_async_dma_complete_log;
141
142         rte_spinlock_lock(&dma_info->dma_lock);
143
144         /**
145          * Print error log for debugging, if DMA reports error during
146          * DMA transfer. We do not handle error in vhost level.
147          */
148         nr_copies = rte_dma_completed(dma_id, vchan_id, max_pkts, &last_idx, &has_error);
149         if (unlikely(!vhost_async_dma_complete_log && has_error)) {
150                 VHOST_LOG_DATA(ERR, "(%s) DMA completion failure on channel %d:%u\n", dev->ifname,
151                                 dma_id, vchan_id);
152                 vhost_async_dma_complete_log = true;
153         } else if (nr_copies == 0) {
154                 goto out;
155         }
156
157         copy_idx = last_idx - nr_copies + 1;
158         for (i = 0; i < nr_copies; i++) {
159                 bool *flag;
160
161                 flag = dma_info->pkts_cmpl_flag_addr[copy_idx & ring_mask];
162                 if (flag) {
163                         /**
164                          * Mark the packet flag as received. The flag
165                          * could belong to another virtqueue but write
166                          * is atomic.
167                          */
168                         *flag = true;
169                         dma_info->pkts_cmpl_flag_addr[copy_idx & ring_mask] = NULL;
170                 }
171                 copy_idx++;
172         }
173
174 out:
175         rte_spinlock_unlock(&dma_info->dma_lock);
176         return nr_copies;
177 }
178
179 static inline void
180 do_data_copy_enqueue(struct virtio_net *dev, struct vhost_virtqueue *vq)
181 {
182         struct batch_copy_elem *elem = vq->batch_copy_elems;
183         uint16_t count = vq->batch_copy_nb_elems;
184         int i;
185
186         for (i = 0; i < count; i++) {
187                 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
188                 vhost_log_cache_write_iova(dev, vq, elem[i].log_addr,
189                                            elem[i].len);
190                 PRINT_PACKET(dev, (uintptr_t)elem[i].dst, elem[i].len, 0);
191         }
192
193         vq->batch_copy_nb_elems = 0;
194 }
195
196 static inline void
197 do_data_copy_dequeue(struct vhost_virtqueue *vq)
198 {
199         struct batch_copy_elem *elem = vq->batch_copy_elems;
200         uint16_t count = vq->batch_copy_nb_elems;
201         int i;
202
203         for (i = 0; i < count; i++)
204                 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
205
206         vq->batch_copy_nb_elems = 0;
207 }
208
209 static __rte_always_inline void
210 do_flush_shadow_used_ring_split(struct virtio_net *dev,
211                         struct vhost_virtqueue *vq,
212                         uint16_t to, uint16_t from, uint16_t size)
213 {
214         rte_memcpy(&vq->used->ring[to],
215                         &vq->shadow_used_split[from],
216                         size * sizeof(struct vring_used_elem));
217         vhost_log_cache_used_vring(dev, vq,
218                         offsetof(struct vring_used, ring[to]),
219                         size * sizeof(struct vring_used_elem));
220 }
221
222 static __rte_always_inline void
223 flush_shadow_used_ring_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
224 {
225         uint16_t used_idx = vq->last_used_idx & (vq->size - 1);
226
227         if (used_idx + vq->shadow_used_idx <= vq->size) {
228                 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0,
229                                           vq->shadow_used_idx);
230         } else {
231                 uint16_t size;
232
233                 /* update used ring interval [used_idx, vq->size] */
234                 size = vq->size - used_idx;
235                 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0, size);
236
237                 /* update the left half used ring interval [0, left_size] */
238                 do_flush_shadow_used_ring_split(dev, vq, 0, size,
239                                           vq->shadow_used_idx - size);
240         }
241         vq->last_used_idx += vq->shadow_used_idx;
242
243         vhost_log_cache_sync(dev, vq);
244
245         __atomic_add_fetch(&vq->used->idx, vq->shadow_used_idx,
246                            __ATOMIC_RELEASE);
247         vq->shadow_used_idx = 0;
248         vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
249                 sizeof(vq->used->idx));
250 }
251
252 static __rte_always_inline void
253 update_shadow_used_ring_split(struct vhost_virtqueue *vq,
254                          uint16_t desc_idx, uint32_t len)
255 {
256         uint16_t i = vq->shadow_used_idx++;
257
258         vq->shadow_used_split[i].id  = desc_idx;
259         vq->shadow_used_split[i].len = len;
260 }
261
262 static __rte_always_inline void
263 vhost_flush_enqueue_shadow_packed(struct virtio_net *dev,
264                                   struct vhost_virtqueue *vq)
265 {
266         int i;
267         uint16_t used_idx = vq->last_used_idx;
268         uint16_t head_idx = vq->last_used_idx;
269         uint16_t head_flags = 0;
270
271         /* Split loop in two to save memory barriers */
272         for (i = 0; i < vq->shadow_used_idx; i++) {
273                 vq->desc_packed[used_idx].id = vq->shadow_used_packed[i].id;
274                 vq->desc_packed[used_idx].len = vq->shadow_used_packed[i].len;
275
276                 used_idx += vq->shadow_used_packed[i].count;
277                 if (used_idx >= vq->size)
278                         used_idx -= vq->size;
279         }
280
281         /* The ordering for storing desc flags needs to be enforced. */
282         rte_atomic_thread_fence(__ATOMIC_RELEASE);
283
284         for (i = 0; i < vq->shadow_used_idx; i++) {
285                 uint16_t flags;
286
287                 if (vq->shadow_used_packed[i].len)
288                         flags = VRING_DESC_F_WRITE;
289                 else
290                         flags = 0;
291
292                 if (vq->used_wrap_counter) {
293                         flags |= VRING_DESC_F_USED;
294                         flags |= VRING_DESC_F_AVAIL;
295                 } else {
296                         flags &= ~VRING_DESC_F_USED;
297                         flags &= ~VRING_DESC_F_AVAIL;
298                 }
299
300                 if (i > 0) {
301                         vq->desc_packed[vq->last_used_idx].flags = flags;
302
303                         vhost_log_cache_used_vring(dev, vq,
304                                         vq->last_used_idx *
305                                         sizeof(struct vring_packed_desc),
306                                         sizeof(struct vring_packed_desc));
307                 } else {
308                         head_idx = vq->last_used_idx;
309                         head_flags = flags;
310                 }
311
312                 vq_inc_last_used_packed(vq, vq->shadow_used_packed[i].count);
313         }
314
315         vq->desc_packed[head_idx].flags = head_flags;
316
317         vhost_log_cache_used_vring(dev, vq,
318                                 head_idx *
319                                 sizeof(struct vring_packed_desc),
320                                 sizeof(struct vring_packed_desc));
321
322         vq->shadow_used_idx = 0;
323         vhost_log_cache_sync(dev, vq);
324 }
325
326 static __rte_always_inline void
327 vhost_flush_dequeue_shadow_packed(struct virtio_net *dev,
328                                   struct vhost_virtqueue *vq)
329 {
330         struct vring_used_elem_packed *used_elem = &vq->shadow_used_packed[0];
331
332         vq->desc_packed[vq->shadow_last_used_idx].id = used_elem->id;
333         /* desc flags is the synchronization point for virtio packed vring */
334         __atomic_store_n(&vq->desc_packed[vq->shadow_last_used_idx].flags,
335                          used_elem->flags, __ATOMIC_RELEASE);
336
337         vhost_log_cache_used_vring(dev, vq, vq->shadow_last_used_idx *
338                                    sizeof(struct vring_packed_desc),
339                                    sizeof(struct vring_packed_desc));
340         vq->shadow_used_idx = 0;
341         vhost_log_cache_sync(dev, vq);
342 }
343
344 static __rte_always_inline void
345 vhost_flush_enqueue_batch_packed(struct virtio_net *dev,
346                                  struct vhost_virtqueue *vq,
347                                  uint64_t *lens,
348                                  uint16_t *ids)
349 {
350         uint16_t i;
351         uint16_t flags;
352         uint16_t last_used_idx;
353         struct vring_packed_desc *desc_base;
354
355         last_used_idx = vq->last_used_idx;
356         desc_base = &vq->desc_packed[last_used_idx];
357
358         flags = PACKED_DESC_ENQUEUE_USED_FLAG(vq->used_wrap_counter);
359
360         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
361                 desc_base[i].id = ids[i];
362                 desc_base[i].len = lens[i];
363         }
364
365         rte_atomic_thread_fence(__ATOMIC_RELEASE);
366
367         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
368                 desc_base[i].flags = flags;
369         }
370
371         vhost_log_cache_used_vring(dev, vq, last_used_idx *
372                                    sizeof(struct vring_packed_desc),
373                                    sizeof(struct vring_packed_desc) *
374                                    PACKED_BATCH_SIZE);
375         vhost_log_cache_sync(dev, vq);
376
377         vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
378 }
379
380 static __rte_always_inline void
381 vhost_shadow_dequeue_batch_packed_inorder(struct vhost_virtqueue *vq,
382                                           uint16_t id)
383 {
384         vq->shadow_used_packed[0].id = id;
385
386         if (!vq->shadow_used_idx) {
387                 vq->shadow_last_used_idx = vq->last_used_idx;
388                 vq->shadow_used_packed[0].flags =
389                         PACKED_DESC_DEQUEUE_USED_FLAG(vq->used_wrap_counter);
390                 vq->shadow_used_packed[0].len = 0;
391                 vq->shadow_used_packed[0].count = 1;
392                 vq->shadow_used_idx++;
393         }
394
395         vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
396 }
397
398 static __rte_always_inline void
399 vhost_shadow_dequeue_batch_packed(struct virtio_net *dev,
400                                   struct vhost_virtqueue *vq,
401                                   uint16_t *ids)
402 {
403         uint16_t flags;
404         uint16_t i;
405         uint16_t begin;
406
407         flags = PACKED_DESC_DEQUEUE_USED_FLAG(vq->used_wrap_counter);
408
409         if (!vq->shadow_used_idx) {
410                 vq->shadow_last_used_idx = vq->last_used_idx;
411                 vq->shadow_used_packed[0].id  = ids[0];
412                 vq->shadow_used_packed[0].len = 0;
413                 vq->shadow_used_packed[0].count = 1;
414                 vq->shadow_used_packed[0].flags = flags;
415                 vq->shadow_used_idx++;
416                 begin = 1;
417         } else
418                 begin = 0;
419
420         vhost_for_each_try_unroll(i, begin, PACKED_BATCH_SIZE) {
421                 vq->desc_packed[vq->last_used_idx + i].id = ids[i];
422                 vq->desc_packed[vq->last_used_idx + i].len = 0;
423         }
424
425         rte_atomic_thread_fence(__ATOMIC_RELEASE);
426         vhost_for_each_try_unroll(i, begin, PACKED_BATCH_SIZE)
427                 vq->desc_packed[vq->last_used_idx + i].flags = flags;
428
429         vhost_log_cache_used_vring(dev, vq, vq->last_used_idx *
430                                    sizeof(struct vring_packed_desc),
431                                    sizeof(struct vring_packed_desc) *
432                                    PACKED_BATCH_SIZE);
433         vhost_log_cache_sync(dev, vq);
434
435         vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
436 }
437
438 static __rte_always_inline void
439 vhost_shadow_dequeue_single_packed(struct vhost_virtqueue *vq,
440                                    uint16_t buf_id,
441                                    uint16_t count)
442 {
443         uint16_t flags;
444
445         flags = vq->desc_packed[vq->last_used_idx].flags;
446         if (vq->used_wrap_counter) {
447                 flags |= VRING_DESC_F_USED;
448                 flags |= VRING_DESC_F_AVAIL;
449         } else {
450                 flags &= ~VRING_DESC_F_USED;
451                 flags &= ~VRING_DESC_F_AVAIL;
452         }
453
454         if (!vq->shadow_used_idx) {
455                 vq->shadow_last_used_idx = vq->last_used_idx;
456
457                 vq->shadow_used_packed[0].id  = buf_id;
458                 vq->shadow_used_packed[0].len = 0;
459                 vq->shadow_used_packed[0].flags = flags;
460                 vq->shadow_used_idx++;
461         } else {
462                 vq->desc_packed[vq->last_used_idx].id = buf_id;
463                 vq->desc_packed[vq->last_used_idx].len = 0;
464                 vq->desc_packed[vq->last_used_idx].flags = flags;
465         }
466
467         vq_inc_last_used_packed(vq, count);
468 }
469
470 static __rte_always_inline void
471 vhost_shadow_dequeue_single_packed_inorder(struct vhost_virtqueue *vq,
472                                            uint16_t buf_id,
473                                            uint16_t count)
474 {
475         uint16_t flags;
476
477         vq->shadow_used_packed[0].id = buf_id;
478
479         flags = vq->desc_packed[vq->last_used_idx].flags;
480         if (vq->used_wrap_counter) {
481                 flags |= VRING_DESC_F_USED;
482                 flags |= VRING_DESC_F_AVAIL;
483         } else {
484                 flags &= ~VRING_DESC_F_USED;
485                 flags &= ~VRING_DESC_F_AVAIL;
486         }
487
488         if (!vq->shadow_used_idx) {
489                 vq->shadow_last_used_idx = vq->last_used_idx;
490                 vq->shadow_used_packed[0].len = 0;
491                 vq->shadow_used_packed[0].flags = flags;
492                 vq->shadow_used_idx++;
493         }
494
495         vq_inc_last_used_packed(vq, count);
496 }
497
498 static __rte_always_inline void
499 vhost_shadow_enqueue_packed(struct vhost_virtqueue *vq,
500                                    uint32_t *len,
501                                    uint16_t *id,
502                                    uint16_t *count,
503                                    uint16_t num_buffers)
504 {
505         uint16_t i;
506
507         for (i = 0; i < num_buffers; i++) {
508                 /* enqueue shadow flush action aligned with batch num */
509                 if (!vq->shadow_used_idx)
510                         vq->shadow_aligned_idx = vq->last_used_idx &
511                                 PACKED_BATCH_MASK;
512                 vq->shadow_used_packed[vq->shadow_used_idx].id  = id[i];
513                 vq->shadow_used_packed[vq->shadow_used_idx].len = len[i];
514                 vq->shadow_used_packed[vq->shadow_used_idx].count = count[i];
515                 vq->shadow_aligned_idx += count[i];
516                 vq->shadow_used_idx++;
517         }
518 }
519
520 static __rte_always_inline void
521 vhost_shadow_enqueue_single_packed(struct virtio_net *dev,
522                                    struct vhost_virtqueue *vq,
523                                    uint32_t *len,
524                                    uint16_t *id,
525                                    uint16_t *count,
526                                    uint16_t num_buffers)
527 {
528         vhost_shadow_enqueue_packed(vq, len, id, count, num_buffers);
529
530         if (vq->shadow_aligned_idx >= PACKED_BATCH_SIZE) {
531                 do_data_copy_enqueue(dev, vq);
532                 vhost_flush_enqueue_shadow_packed(dev, vq);
533         }
534 }
535
536 /* avoid write operation when necessary, to lessen cache issues */
537 #define ASSIGN_UNLESS_EQUAL(var, val) do {      \
538         if ((var) != (val))                     \
539                 (var) = (val);                  \
540 } while (0)
541
542 static __rte_always_inline void
543 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
544 {
545         uint64_t csum_l4 = m_buf->ol_flags & RTE_MBUF_F_TX_L4_MASK;
546
547         if (m_buf->ol_flags & RTE_MBUF_F_TX_TCP_SEG)
548                 csum_l4 |= RTE_MBUF_F_TX_TCP_CKSUM;
549
550         if (csum_l4) {
551                 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
552                 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
553
554                 switch (csum_l4) {
555                 case RTE_MBUF_F_TX_TCP_CKSUM:
556                         net_hdr->csum_offset = (offsetof(struct rte_tcp_hdr,
557                                                 cksum));
558                         break;
559                 case RTE_MBUF_F_TX_UDP_CKSUM:
560                         net_hdr->csum_offset = (offsetof(struct rte_udp_hdr,
561                                                 dgram_cksum));
562                         break;
563                 case RTE_MBUF_F_TX_SCTP_CKSUM:
564                         net_hdr->csum_offset = (offsetof(struct rte_sctp_hdr,
565                                                 cksum));
566                         break;
567                 }
568         } else {
569                 ASSIGN_UNLESS_EQUAL(net_hdr->csum_start, 0);
570                 ASSIGN_UNLESS_EQUAL(net_hdr->csum_offset, 0);
571                 ASSIGN_UNLESS_EQUAL(net_hdr->flags, 0);
572         }
573
574         /* IP cksum verification cannot be bypassed, then calculate here */
575         if (m_buf->ol_flags & RTE_MBUF_F_TX_IP_CKSUM) {
576                 struct rte_ipv4_hdr *ipv4_hdr;
577
578                 ipv4_hdr = rte_pktmbuf_mtod_offset(m_buf, struct rte_ipv4_hdr *,
579                                                    m_buf->l2_len);
580                 ipv4_hdr->hdr_checksum = 0;
581                 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
582         }
583
584         if (m_buf->ol_flags & RTE_MBUF_F_TX_TCP_SEG) {
585                 if (m_buf->ol_flags & RTE_MBUF_F_TX_IPV4)
586                         net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
587                 else
588                         net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
589                 net_hdr->gso_size = m_buf->tso_segsz;
590                 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
591                                         + m_buf->l4_len;
592         } else if (m_buf->ol_flags & RTE_MBUF_F_TX_UDP_SEG) {
593                 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
594                 net_hdr->gso_size = m_buf->tso_segsz;
595                 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len +
596                         m_buf->l4_len;
597         } else {
598                 ASSIGN_UNLESS_EQUAL(net_hdr->gso_type, 0);
599                 ASSIGN_UNLESS_EQUAL(net_hdr->gso_size, 0);
600                 ASSIGN_UNLESS_EQUAL(net_hdr->hdr_len, 0);
601         }
602 }
603
604 static __rte_always_inline int
605 map_one_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
606                 struct buf_vector *buf_vec, uint16_t *vec_idx,
607                 uint64_t desc_iova, uint64_t desc_len, uint8_t perm)
608 {
609         uint16_t vec_id = *vec_idx;
610
611         while (desc_len) {
612                 uint64_t desc_addr;
613                 uint64_t desc_chunck_len = desc_len;
614
615                 if (unlikely(vec_id >= BUF_VECTOR_MAX))
616                         return -1;
617
618                 desc_addr = vhost_iova_to_vva(dev, vq,
619                                 desc_iova,
620                                 &desc_chunck_len,
621                                 perm);
622                 if (unlikely(!desc_addr))
623                         return -1;
624
625                 rte_prefetch0((void *)(uintptr_t)desc_addr);
626
627                 buf_vec[vec_id].buf_iova = desc_iova;
628                 buf_vec[vec_id].buf_addr = desc_addr;
629                 buf_vec[vec_id].buf_len  = desc_chunck_len;
630
631                 desc_len -= desc_chunck_len;
632                 desc_iova += desc_chunck_len;
633                 vec_id++;
634         }
635         *vec_idx = vec_id;
636
637         return 0;
638 }
639
640 static __rte_always_inline int
641 fill_vec_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
642                          uint32_t avail_idx, uint16_t *vec_idx,
643                          struct buf_vector *buf_vec, uint16_t *desc_chain_head,
644                          uint32_t *desc_chain_len, uint8_t perm)
645 {
646         uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
647         uint16_t vec_id = *vec_idx;
648         uint32_t len    = 0;
649         uint64_t dlen;
650         uint32_t nr_descs = vq->size;
651         uint32_t cnt    = 0;
652         struct vring_desc *descs = vq->desc;
653         struct vring_desc *idesc = NULL;
654
655         if (unlikely(idx >= vq->size))
656                 return -1;
657
658         *desc_chain_head = idx;
659
660         if (vq->desc[idx].flags & VRING_DESC_F_INDIRECT) {
661                 dlen = vq->desc[idx].len;
662                 nr_descs = dlen / sizeof(struct vring_desc);
663                 if (unlikely(nr_descs > vq->size))
664                         return -1;
665
666                 descs = (struct vring_desc *)(uintptr_t)
667                         vhost_iova_to_vva(dev, vq, vq->desc[idx].addr,
668                                                 &dlen,
669                                                 VHOST_ACCESS_RO);
670                 if (unlikely(!descs))
671                         return -1;
672
673                 if (unlikely(dlen < vq->desc[idx].len)) {
674                         /*
675                          * The indirect desc table is not contiguous
676                          * in process VA space, we have to copy it.
677                          */
678                         idesc = vhost_alloc_copy_ind_table(dev, vq,
679                                         vq->desc[idx].addr, vq->desc[idx].len);
680                         if (unlikely(!idesc))
681                                 return -1;
682
683                         descs = idesc;
684                 }
685
686                 idx = 0;
687         }
688
689         while (1) {
690                 if (unlikely(idx >= nr_descs || cnt++ >= nr_descs)) {
691                         free_ind_table(idesc);
692                         return -1;
693                 }
694
695                 dlen = descs[idx].len;
696                 len += dlen;
697
698                 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
699                                                 descs[idx].addr, dlen,
700                                                 perm))) {
701                         free_ind_table(idesc);
702                         return -1;
703                 }
704
705                 if ((descs[idx].flags & VRING_DESC_F_NEXT) == 0)
706                         break;
707
708                 idx = descs[idx].next;
709         }
710
711         *desc_chain_len = len;
712         *vec_idx = vec_id;
713
714         if (unlikely(!!idesc))
715                 free_ind_table(idesc);
716
717         return 0;
718 }
719
720 /*
721  * Returns -1 on fail, 0 on success
722  */
723 static inline int
724 reserve_avail_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
725                                 uint32_t size, struct buf_vector *buf_vec,
726                                 uint16_t *num_buffers, uint16_t avail_head,
727                                 uint16_t *nr_vec)
728 {
729         uint16_t cur_idx;
730         uint16_t vec_idx = 0;
731         uint16_t max_tries, tries = 0;
732
733         uint16_t head_idx = 0;
734         uint32_t len = 0;
735
736         *num_buffers = 0;
737         cur_idx  = vq->last_avail_idx;
738
739         if (rxvq_is_mergeable(dev))
740                 max_tries = vq->size - 1;
741         else
742                 max_tries = 1;
743
744         while (size > 0) {
745                 if (unlikely(cur_idx == avail_head))
746                         return -1;
747                 /*
748                  * if we tried all available ring items, and still
749                  * can't get enough buf, it means something abnormal
750                  * happened.
751                  */
752                 if (unlikely(++tries > max_tries))
753                         return -1;
754
755                 if (unlikely(fill_vec_buf_split(dev, vq, cur_idx,
756                                                 &vec_idx, buf_vec,
757                                                 &head_idx, &len,
758                                                 VHOST_ACCESS_RW) < 0))
759                         return -1;
760                 len = RTE_MIN(len, size);
761                 update_shadow_used_ring_split(vq, head_idx, len);
762                 size -= len;
763
764                 cur_idx++;
765                 *num_buffers += 1;
766         }
767
768         *nr_vec = vec_idx;
769
770         return 0;
771 }
772
773 static __rte_always_inline int
774 fill_vec_buf_packed_indirect(struct virtio_net *dev,
775                         struct vhost_virtqueue *vq,
776                         struct vring_packed_desc *desc, uint16_t *vec_idx,
777                         struct buf_vector *buf_vec, uint32_t *len, uint8_t perm)
778 {
779         uint16_t i;
780         uint32_t nr_descs;
781         uint16_t vec_id = *vec_idx;
782         uint64_t dlen;
783         struct vring_packed_desc *descs, *idescs = NULL;
784
785         dlen = desc->len;
786         descs = (struct vring_packed_desc *)(uintptr_t)
787                 vhost_iova_to_vva(dev, vq, desc->addr, &dlen, VHOST_ACCESS_RO);
788         if (unlikely(!descs))
789                 return -1;
790
791         if (unlikely(dlen < desc->len)) {
792                 /*
793                  * The indirect desc table is not contiguous
794                  * in process VA space, we have to copy it.
795                  */
796                 idescs = vhost_alloc_copy_ind_table(dev,
797                                 vq, desc->addr, desc->len);
798                 if (unlikely(!idescs))
799                         return -1;
800
801                 descs = idescs;
802         }
803
804         nr_descs =  desc->len / sizeof(struct vring_packed_desc);
805         if (unlikely(nr_descs >= vq->size)) {
806                 free_ind_table(idescs);
807                 return -1;
808         }
809
810         for (i = 0; i < nr_descs; i++) {
811                 if (unlikely(vec_id >= BUF_VECTOR_MAX)) {
812                         free_ind_table(idescs);
813                         return -1;
814                 }
815
816                 dlen = descs[i].len;
817                 *len += dlen;
818                 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
819                                                 descs[i].addr, dlen,
820                                                 perm)))
821                         return -1;
822         }
823         *vec_idx = vec_id;
824
825         if (unlikely(!!idescs))
826                 free_ind_table(idescs);
827
828         return 0;
829 }
830
831 static __rte_always_inline int
832 fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
833                                 uint16_t avail_idx, uint16_t *desc_count,
834                                 struct buf_vector *buf_vec, uint16_t *vec_idx,
835                                 uint16_t *buf_id, uint32_t *len, uint8_t perm)
836 {
837         bool wrap_counter = vq->avail_wrap_counter;
838         struct vring_packed_desc *descs = vq->desc_packed;
839         uint16_t vec_id = *vec_idx;
840         uint64_t dlen;
841
842         if (avail_idx < vq->last_avail_idx)
843                 wrap_counter ^= 1;
844
845         /*
846          * Perform a load-acquire barrier in desc_is_avail to
847          * enforce the ordering between desc flags and desc
848          * content.
849          */
850         if (unlikely(!desc_is_avail(&descs[avail_idx], wrap_counter)))
851                 return -1;
852
853         *desc_count = 0;
854         *len = 0;
855
856         while (1) {
857                 if (unlikely(vec_id >= BUF_VECTOR_MAX))
858                         return -1;
859
860                 if (unlikely(*desc_count >= vq->size))
861                         return -1;
862
863                 *desc_count += 1;
864                 *buf_id = descs[avail_idx].id;
865
866                 if (descs[avail_idx].flags & VRING_DESC_F_INDIRECT) {
867                         if (unlikely(fill_vec_buf_packed_indirect(dev, vq,
868                                                         &descs[avail_idx],
869                                                         &vec_id, buf_vec,
870                                                         len, perm) < 0))
871                                 return -1;
872                 } else {
873                         dlen = descs[avail_idx].len;
874                         *len += dlen;
875
876                         if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
877                                                         descs[avail_idx].addr,
878                                                         dlen,
879                                                         perm)))
880                                 return -1;
881                 }
882
883                 if ((descs[avail_idx].flags & VRING_DESC_F_NEXT) == 0)
884                         break;
885
886                 if (++avail_idx >= vq->size) {
887                         avail_idx -= vq->size;
888                         wrap_counter ^= 1;
889                 }
890         }
891
892         *vec_idx = vec_id;
893
894         return 0;
895 }
896
897 static __rte_noinline void
898 copy_vnet_hdr_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
899                 struct buf_vector *buf_vec,
900                 struct virtio_net_hdr_mrg_rxbuf *hdr)
901 {
902         uint64_t len;
903         uint64_t remain = dev->vhost_hlen;
904         uint64_t src = (uint64_t)(uintptr_t)hdr, dst;
905         uint64_t iova = buf_vec->buf_iova;
906
907         while (remain) {
908                 len = RTE_MIN(remain,
909                                 buf_vec->buf_len);
910                 dst = buf_vec->buf_addr;
911                 rte_memcpy((void *)(uintptr_t)dst,
912                                 (void *)(uintptr_t)src,
913                                 len);
914
915                 PRINT_PACKET(dev, (uintptr_t)dst,
916                                 (uint32_t)len, 0);
917                 vhost_log_cache_write_iova(dev, vq,
918                                 iova, len);
919
920                 remain -= len;
921                 iova += len;
922                 src += len;
923                 buf_vec++;
924         }
925 }
926
927 static __rte_always_inline int
928 async_iter_initialize(struct virtio_net *dev, struct vhost_async *async)
929 {
930         struct vhost_iov_iter *iter;
931
932         if (unlikely(async->iovec_idx >= VHOST_MAX_ASYNC_VEC)) {
933                 VHOST_LOG_DATA(ERR, "(%s) no more async iovec available\n", dev->ifname);
934                 return -1;
935         }
936
937         iter = async->iov_iter + async->iter_idx;
938         iter->iov = async->iovec + async->iovec_idx;
939         iter->nr_segs = 0;
940
941         return 0;
942 }
943
944 static __rte_always_inline int
945 async_iter_add_iovec(struct virtio_net *dev, struct vhost_async *async,
946                 void *src, void *dst, size_t len)
947 {
948         struct vhost_iov_iter *iter;
949         struct vhost_iovec *iovec;
950
951         if (unlikely(async->iovec_idx >= VHOST_MAX_ASYNC_VEC)) {
952                 static bool vhost_max_async_vec_log;
953
954                 if (!vhost_max_async_vec_log) {
955                         VHOST_LOG_DATA(ERR, "(%s) no more async iovec available\n", dev->ifname);
956                         vhost_max_async_vec_log = true;
957                 }
958
959                 return -1;
960         }
961
962         iter = async->iov_iter + async->iter_idx;
963         iovec = async->iovec + async->iovec_idx;
964
965         iovec->src_addr = src;
966         iovec->dst_addr = dst;
967         iovec->len = len;
968
969         iter->nr_segs++;
970         async->iovec_idx++;
971
972         return 0;
973 }
974
975 static __rte_always_inline void
976 async_iter_finalize(struct vhost_async *async)
977 {
978         async->iter_idx++;
979 }
980
981 static __rte_always_inline void
982 async_iter_cancel(struct vhost_async *async)
983 {
984         struct vhost_iov_iter *iter;
985
986         iter = async->iov_iter + async->iter_idx;
987         async->iovec_idx -= iter->nr_segs;
988         iter->nr_segs = 0;
989         iter->iov = NULL;
990 }
991
992 static __rte_always_inline void
993 async_iter_reset(struct vhost_async *async)
994 {
995         async->iter_idx = 0;
996         async->iovec_idx = 0;
997 }
998
999 static __rte_always_inline int
1000 async_mbuf_to_desc_seg(struct virtio_net *dev, struct vhost_virtqueue *vq,
1001                 struct rte_mbuf *m, uint32_t mbuf_offset,
1002                 uint64_t buf_iova, uint32_t cpy_len)
1003 {
1004         struct vhost_async *async = vq->async;
1005         uint64_t mapped_len;
1006         uint32_t buf_offset = 0;
1007         void *host_iova;
1008
1009         while (cpy_len) {
1010                 host_iova = (void *)(uintptr_t)gpa_to_first_hpa(dev,
1011                                 buf_iova + buf_offset, cpy_len, &mapped_len);
1012                 if (unlikely(!host_iova)) {
1013                         VHOST_LOG_DATA(ERR, "(%s) %s: failed to get host iova.\n",
1014                                        dev->ifname, __func__);
1015                         return -1;
1016                 }
1017
1018                 if (unlikely(async_iter_add_iovec(dev, async,
1019                                                 (void *)(uintptr_t)rte_pktmbuf_iova_offset(m,
1020                                                         mbuf_offset),
1021                                                 host_iova, (size_t)mapped_len)))
1022                         return -1;
1023
1024                 cpy_len -= (uint32_t)mapped_len;
1025                 mbuf_offset += (uint32_t)mapped_len;
1026                 buf_offset += (uint32_t)mapped_len;
1027         }
1028
1029         return 0;
1030 }
1031
1032 static __rte_always_inline void
1033 sync_mbuf_to_desc_seg(struct virtio_net *dev, struct vhost_virtqueue *vq,
1034                 struct rte_mbuf *m, uint32_t mbuf_offset,
1035                 uint64_t buf_addr, uint64_t buf_iova, uint32_t cpy_len)
1036 {
1037         struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
1038
1039         if (likely(cpy_len > MAX_BATCH_LEN || vq->batch_copy_nb_elems >= vq->size)) {
1040                 rte_memcpy((void *)((uintptr_t)(buf_addr)),
1041                                 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
1042                                 cpy_len);
1043                 vhost_log_cache_write_iova(dev, vq, buf_iova, cpy_len);
1044                 PRINT_PACKET(dev, (uintptr_t)(buf_addr), cpy_len, 0);
1045         } else {
1046                 batch_copy[vq->batch_copy_nb_elems].dst =
1047                         (void *)((uintptr_t)(buf_addr));
1048                 batch_copy[vq->batch_copy_nb_elems].src =
1049                         rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
1050                 batch_copy[vq->batch_copy_nb_elems].log_addr = buf_iova;
1051                 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
1052                 vq->batch_copy_nb_elems++;
1053         }
1054 }
1055
1056 static __rte_always_inline int
1057 mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
1058                 struct rte_mbuf *m, struct buf_vector *buf_vec,
1059                 uint16_t nr_vec, uint16_t num_buffers, bool is_async)
1060 {
1061         uint32_t vec_idx = 0;
1062         uint32_t mbuf_offset, mbuf_avail;
1063         uint32_t buf_offset, buf_avail;
1064         uint64_t buf_addr, buf_iova, buf_len;
1065         uint32_t cpy_len;
1066         uint64_t hdr_addr;
1067         struct rte_mbuf *hdr_mbuf;
1068         struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
1069         struct vhost_async *async = vq->async;
1070
1071         if (unlikely(m == NULL))
1072                 return -1;
1073
1074         buf_addr = buf_vec[vec_idx].buf_addr;
1075         buf_iova = buf_vec[vec_idx].buf_iova;
1076         buf_len = buf_vec[vec_idx].buf_len;
1077
1078         if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1))
1079                 return -1;
1080
1081         hdr_mbuf = m;
1082         hdr_addr = buf_addr;
1083         if (unlikely(buf_len < dev->vhost_hlen)) {
1084                 memset(&tmp_hdr, 0, sizeof(struct virtio_net_hdr_mrg_rxbuf));
1085                 hdr = &tmp_hdr;
1086         } else
1087                 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
1088
1089         VHOST_LOG_DATA(DEBUG, "(%s) RX: num merge buffers %d\n",
1090                 dev->ifname, num_buffers);
1091
1092         if (unlikely(buf_len < dev->vhost_hlen)) {
1093                 buf_offset = dev->vhost_hlen - buf_len;
1094                 vec_idx++;
1095                 buf_addr = buf_vec[vec_idx].buf_addr;
1096                 buf_iova = buf_vec[vec_idx].buf_iova;
1097                 buf_len = buf_vec[vec_idx].buf_len;
1098                 buf_avail = buf_len - buf_offset;
1099         } else {
1100                 buf_offset = dev->vhost_hlen;
1101                 buf_avail = buf_len - dev->vhost_hlen;
1102         }
1103
1104         mbuf_avail  = rte_pktmbuf_data_len(m);
1105         mbuf_offset = 0;
1106
1107         if (is_async) {
1108                 if (async_iter_initialize(dev, async))
1109                         return -1;
1110         }
1111
1112         while (mbuf_avail != 0 || m->next != NULL) {
1113                 /* done with current buf, get the next one */
1114                 if (buf_avail == 0) {
1115                         vec_idx++;
1116                         if (unlikely(vec_idx >= nr_vec))
1117                                 goto error;
1118
1119                         buf_addr = buf_vec[vec_idx].buf_addr;
1120                         buf_iova = buf_vec[vec_idx].buf_iova;
1121                         buf_len = buf_vec[vec_idx].buf_len;
1122
1123                         buf_offset = 0;
1124                         buf_avail  = buf_len;
1125                 }
1126
1127                 /* done with current mbuf, get the next one */
1128                 if (mbuf_avail == 0) {
1129                         m = m->next;
1130
1131                         mbuf_offset = 0;
1132                         mbuf_avail  = rte_pktmbuf_data_len(m);
1133                 }
1134
1135                 if (hdr_addr) {
1136                         virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
1137                         if (rxvq_is_mergeable(dev))
1138                                 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
1139                                                 num_buffers);
1140
1141                         if (unlikely(hdr == &tmp_hdr)) {
1142                                 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
1143                         } else {
1144                                 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
1145                                                 dev->vhost_hlen, 0);
1146                                 vhost_log_cache_write_iova(dev, vq,
1147                                                 buf_vec[0].buf_iova,
1148                                                 dev->vhost_hlen);
1149                         }
1150
1151                         hdr_addr = 0;
1152                 }
1153
1154                 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1155
1156                 if (is_async) {
1157                         if (async_mbuf_to_desc_seg(dev, vq, m, mbuf_offset,
1158                                                 buf_iova + buf_offset, cpy_len) < 0)
1159                                 goto error;
1160                 } else {
1161                         sync_mbuf_to_desc_seg(dev, vq, m, mbuf_offset,
1162                                         buf_addr + buf_offset,
1163                                         buf_iova + buf_offset, cpy_len);
1164                 }
1165
1166                 mbuf_avail  -= cpy_len;
1167                 mbuf_offset += cpy_len;
1168                 buf_avail  -= cpy_len;
1169                 buf_offset += cpy_len;
1170         }
1171
1172         if (is_async)
1173                 async_iter_finalize(async);
1174
1175         return 0;
1176 error:
1177         if (is_async)
1178                 async_iter_cancel(async);
1179
1180         return -1;
1181 }
1182
1183 static __rte_always_inline int
1184 vhost_enqueue_single_packed(struct virtio_net *dev,
1185                             struct vhost_virtqueue *vq,
1186                             struct rte_mbuf *pkt,
1187                             struct buf_vector *buf_vec,
1188                             uint16_t *nr_descs)
1189 {
1190         uint16_t nr_vec = 0;
1191         uint16_t avail_idx = vq->last_avail_idx;
1192         uint16_t max_tries, tries = 0;
1193         uint16_t buf_id = 0;
1194         uint32_t len = 0;
1195         uint16_t desc_count;
1196         uint32_t size = pkt->pkt_len + sizeof(struct virtio_net_hdr_mrg_rxbuf);
1197         uint16_t num_buffers = 0;
1198         uint32_t buffer_len[vq->size];
1199         uint16_t buffer_buf_id[vq->size];
1200         uint16_t buffer_desc_count[vq->size];
1201
1202         if (rxvq_is_mergeable(dev))
1203                 max_tries = vq->size - 1;
1204         else
1205                 max_tries = 1;
1206
1207         while (size > 0) {
1208                 /*
1209                  * if we tried all available ring items, and still
1210                  * can't get enough buf, it means something abnormal
1211                  * happened.
1212                  */
1213                 if (unlikely(++tries > max_tries))
1214                         return -1;
1215
1216                 if (unlikely(fill_vec_buf_packed(dev, vq,
1217                                                 avail_idx, &desc_count,
1218                                                 buf_vec, &nr_vec,
1219                                                 &buf_id, &len,
1220                                                 VHOST_ACCESS_RW) < 0))
1221                         return -1;
1222
1223                 len = RTE_MIN(len, size);
1224                 size -= len;
1225
1226                 buffer_len[num_buffers] = len;
1227                 buffer_buf_id[num_buffers] = buf_id;
1228                 buffer_desc_count[num_buffers] = desc_count;
1229                 num_buffers += 1;
1230
1231                 *nr_descs += desc_count;
1232                 avail_idx += desc_count;
1233                 if (avail_idx >= vq->size)
1234                         avail_idx -= vq->size;
1235         }
1236
1237         if (mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec, num_buffers, false) < 0)
1238                 return -1;
1239
1240         vhost_shadow_enqueue_single_packed(dev, vq, buffer_len, buffer_buf_id,
1241                                            buffer_desc_count, num_buffers);
1242
1243         return 0;
1244 }
1245
1246 static __rte_noinline uint32_t
1247 virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1248         struct rte_mbuf **pkts, uint32_t count)
1249 {
1250         uint32_t pkt_idx = 0;
1251         uint16_t num_buffers;
1252         struct buf_vector buf_vec[BUF_VECTOR_MAX];
1253         uint16_t avail_head;
1254
1255         /*
1256          * The ordering between avail index and
1257          * desc reads needs to be enforced.
1258          */
1259         avail_head = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE);
1260
1261         rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1262
1263         for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1264                 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1265                 uint16_t nr_vec = 0;
1266
1267                 if (unlikely(reserve_avail_buf_split(dev, vq,
1268                                                 pkt_len, buf_vec, &num_buffers,
1269                                                 avail_head, &nr_vec) < 0)) {
1270                         VHOST_LOG_DATA(DEBUG,
1271                                 "(%s) failed to get enough desc from vring\n",
1272                                 dev->ifname);
1273                         vq->shadow_used_idx -= num_buffers;
1274                         break;
1275                 }
1276
1277                 VHOST_LOG_DATA(DEBUG, "(%s) current index %d | end index %d\n",
1278                         dev->ifname, vq->last_avail_idx,
1279                         vq->last_avail_idx + num_buffers);
1280
1281                 if (mbuf_to_desc(dev, vq, pkts[pkt_idx], buf_vec, nr_vec,
1282                                         num_buffers, false) < 0) {
1283                         vq->shadow_used_idx -= num_buffers;
1284                         break;
1285                 }
1286
1287                 vq->last_avail_idx += num_buffers;
1288         }
1289
1290         do_data_copy_enqueue(dev, vq);
1291
1292         if (likely(vq->shadow_used_idx)) {
1293                 flush_shadow_used_ring_split(dev, vq);
1294                 vhost_vring_call_split(dev, vq);
1295         }
1296
1297         return pkt_idx;
1298 }
1299
1300 static __rte_always_inline int
1301 virtio_dev_rx_sync_batch_check(struct virtio_net *dev,
1302                            struct vhost_virtqueue *vq,
1303                            struct rte_mbuf **pkts,
1304                            uint64_t *desc_addrs,
1305                            uint64_t *lens)
1306 {
1307         bool wrap_counter = vq->avail_wrap_counter;
1308         struct vring_packed_desc *descs = vq->desc_packed;
1309         uint16_t avail_idx = vq->last_avail_idx;
1310         uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1311         uint16_t i;
1312
1313         if (unlikely(avail_idx & PACKED_BATCH_MASK))
1314                 return -1;
1315
1316         if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
1317                 return -1;
1318
1319         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1320                 if (unlikely(pkts[i]->next != NULL))
1321                         return -1;
1322                 if (unlikely(!desc_is_avail(&descs[avail_idx + i],
1323                                             wrap_counter)))
1324                         return -1;
1325         }
1326
1327         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1328                 lens[i] = descs[avail_idx + i].len;
1329
1330         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1331                 if (unlikely(pkts[i]->pkt_len > (lens[i] - buf_offset)))
1332                         return -1;
1333         }
1334
1335         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1336                 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
1337                                                   descs[avail_idx + i].addr,
1338                                                   &lens[i],
1339                                                   VHOST_ACCESS_RW);
1340
1341         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1342                 if (unlikely(!desc_addrs[i]))
1343                         return -1;
1344                 if (unlikely(lens[i] != descs[avail_idx + i].len))
1345                         return -1;
1346         }
1347
1348         return 0;
1349 }
1350
1351 static __rte_always_inline void
1352 virtio_dev_rx_batch_packed_copy(struct virtio_net *dev,
1353                            struct vhost_virtqueue *vq,
1354                            struct rte_mbuf **pkts,
1355                            uint64_t *desc_addrs,
1356                            uint64_t *lens)
1357 {
1358         uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1359         struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE];
1360         struct vring_packed_desc *descs = vq->desc_packed;
1361         uint16_t avail_idx = vq->last_avail_idx;
1362         uint16_t ids[PACKED_BATCH_SIZE];
1363         uint16_t i;
1364
1365         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1366                 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
1367                 hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *)
1368                                         (uintptr_t)desc_addrs[i];
1369                 lens[i] = pkts[i]->pkt_len +
1370                         sizeof(struct virtio_net_hdr_mrg_rxbuf);
1371         }
1372
1373         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1374                 virtio_enqueue_offload(pkts[i], &hdrs[i]->hdr);
1375
1376         vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
1377
1378         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1379                 rte_memcpy((void *)(uintptr_t)(desc_addrs[i] + buf_offset),
1380                            rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
1381                            pkts[i]->pkt_len);
1382         }
1383
1384         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1385                 vhost_log_cache_write_iova(dev, vq, descs[avail_idx + i].addr,
1386                                            lens[i]);
1387
1388         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1389                 ids[i] = descs[avail_idx + i].id;
1390
1391         vhost_flush_enqueue_batch_packed(dev, vq, lens, ids);
1392 }
1393
1394 static __rte_always_inline int
1395 virtio_dev_rx_sync_batch_packed(struct virtio_net *dev,
1396                            struct vhost_virtqueue *vq,
1397                            struct rte_mbuf **pkts)
1398 {
1399         uint64_t desc_addrs[PACKED_BATCH_SIZE];
1400         uint64_t lens[PACKED_BATCH_SIZE];
1401
1402         if (virtio_dev_rx_sync_batch_check(dev, vq, pkts, desc_addrs, lens) == -1)
1403                 return -1;
1404
1405         if (vq->shadow_used_idx) {
1406                 do_data_copy_enqueue(dev, vq);
1407                 vhost_flush_enqueue_shadow_packed(dev, vq);
1408         }
1409
1410         virtio_dev_rx_batch_packed_copy(dev, vq, pkts, desc_addrs, lens);
1411
1412         return 0;
1413 }
1414
1415 static __rte_always_inline int16_t
1416 virtio_dev_rx_single_packed(struct virtio_net *dev,
1417                             struct vhost_virtqueue *vq,
1418                             struct rte_mbuf *pkt)
1419 {
1420         struct buf_vector buf_vec[BUF_VECTOR_MAX];
1421         uint16_t nr_descs = 0;
1422
1423         if (unlikely(vhost_enqueue_single_packed(dev, vq, pkt, buf_vec,
1424                                                  &nr_descs) < 0)) {
1425                 VHOST_LOG_DATA(DEBUG, "(%s) failed to get enough desc from vring\n",
1426                                 dev->ifname);
1427                 return -1;
1428         }
1429
1430         VHOST_LOG_DATA(DEBUG, "(%s) current index %d | end index %d\n",
1431                         dev->ifname, vq->last_avail_idx,
1432                         vq->last_avail_idx + nr_descs);
1433
1434         vq_inc_last_avail_packed(vq, nr_descs);
1435
1436         return 0;
1437 }
1438
1439 static __rte_noinline uint32_t
1440 virtio_dev_rx_packed(struct virtio_net *dev,
1441                      struct vhost_virtqueue *__rte_restrict vq,
1442                      struct rte_mbuf **__rte_restrict pkts,
1443                      uint32_t count)
1444 {
1445         uint32_t pkt_idx = 0;
1446
1447         do {
1448                 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1449
1450                 if (count - pkt_idx >= PACKED_BATCH_SIZE) {
1451                         if (!virtio_dev_rx_sync_batch_packed(dev, vq,
1452                                                         &pkts[pkt_idx])) {
1453                                 pkt_idx += PACKED_BATCH_SIZE;
1454                                 continue;
1455                         }
1456                 }
1457
1458                 if (virtio_dev_rx_single_packed(dev, vq, pkts[pkt_idx]))
1459                         break;
1460                 pkt_idx++;
1461
1462         } while (pkt_idx < count);
1463
1464         if (vq->shadow_used_idx) {
1465                 do_data_copy_enqueue(dev, vq);
1466                 vhost_flush_enqueue_shadow_packed(dev, vq);
1467         }
1468
1469         if (pkt_idx)
1470                 vhost_vring_call_packed(dev, vq);
1471
1472         return pkt_idx;
1473 }
1474
1475 static __rte_always_inline uint32_t
1476 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
1477         struct rte_mbuf **pkts, uint32_t count)
1478 {
1479         struct vhost_virtqueue *vq;
1480         uint32_t nb_tx = 0;
1481
1482         VHOST_LOG_DATA(DEBUG, "(%s) %s\n", dev->ifname, __func__);
1483         if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1484                 VHOST_LOG_DATA(ERR, "(%s) %s: invalid virtqueue idx %d.\n",
1485                         dev->ifname, __func__, queue_id);
1486                 return 0;
1487         }
1488
1489         vq = dev->virtqueue[queue_id];
1490
1491         rte_spinlock_lock(&vq->access_lock);
1492
1493         if (unlikely(!vq->enabled))
1494                 goto out_access_unlock;
1495
1496         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1497                 vhost_user_iotlb_rd_lock(vq);
1498
1499         if (unlikely(!vq->access_ok))
1500                 if (unlikely(vring_translate(dev, vq) < 0))
1501                         goto out;
1502
1503         count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
1504         if (count == 0)
1505                 goto out;
1506
1507         if (vq_is_packed(dev))
1508                 nb_tx = virtio_dev_rx_packed(dev, vq, pkts, count);
1509         else
1510                 nb_tx = virtio_dev_rx_split(dev, vq, pkts, count);
1511
1512 out:
1513         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1514                 vhost_user_iotlb_rd_unlock(vq);
1515
1516 out_access_unlock:
1517         rte_spinlock_unlock(&vq->access_lock);
1518
1519         return nb_tx;
1520 }
1521
1522 uint16_t
1523 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
1524         struct rte_mbuf **__rte_restrict pkts, uint16_t count)
1525 {
1526         struct virtio_net *dev = get_device(vid);
1527
1528         if (!dev)
1529                 return 0;
1530
1531         if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1532                 VHOST_LOG_DATA(ERR, "(%s) %s: built-in vhost net backend is disabled.\n",
1533                         dev->ifname, __func__);
1534                 return 0;
1535         }
1536
1537         return virtio_dev_rx(dev, queue_id, pkts, count);
1538 }
1539
1540 static __rte_always_inline uint16_t
1541 async_get_first_inflight_pkt_idx(struct vhost_virtqueue *vq)
1542 {
1543         struct vhost_async *async = vq->async;
1544
1545         if (async->pkts_idx >= async->pkts_inflight_n)
1546                 return async->pkts_idx - async->pkts_inflight_n;
1547         else
1548                 return vq->size - async->pkts_inflight_n + async->pkts_idx;
1549 }
1550
1551 static __rte_always_inline void
1552 store_dma_desc_info_split(struct vring_used_elem *s_ring, struct vring_used_elem *d_ring,
1553                 uint16_t ring_size, uint16_t s_idx, uint16_t d_idx, uint16_t count)
1554 {
1555         size_t elem_size = sizeof(struct vring_used_elem);
1556
1557         if (d_idx + count <= ring_size) {
1558                 rte_memcpy(d_ring + d_idx, s_ring + s_idx, count * elem_size);
1559         } else {
1560                 uint16_t size = ring_size - d_idx;
1561
1562                 rte_memcpy(d_ring + d_idx, s_ring + s_idx, size * elem_size);
1563                 rte_memcpy(d_ring, s_ring + s_idx + size, (count - size) * elem_size);
1564         }
1565 }
1566
1567 static __rte_always_inline void
1568 store_dma_desc_info_packed(struct vring_used_elem_packed *s_ring,
1569                 struct vring_used_elem_packed *d_ring,
1570                 uint16_t ring_size, uint16_t s_idx, uint16_t d_idx, uint16_t count)
1571 {
1572         size_t elem_size = sizeof(struct vring_used_elem_packed);
1573
1574         if (d_idx + count <= ring_size) {
1575                 rte_memcpy(d_ring + d_idx, s_ring + s_idx, count * elem_size);
1576         } else {
1577                 uint16_t size = ring_size - d_idx;
1578
1579                 rte_memcpy(d_ring + d_idx, s_ring + s_idx, size * elem_size);
1580                 rte_memcpy(d_ring, s_ring + s_idx + size, (count - size) * elem_size);
1581         }
1582 }
1583
1584 static __rte_noinline uint32_t
1585 virtio_dev_rx_async_submit_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1586                 uint16_t queue_id, struct rte_mbuf **pkts, uint32_t count,
1587                 int16_t dma_id, uint16_t vchan_id)
1588 {
1589         struct buf_vector buf_vec[BUF_VECTOR_MAX];
1590         uint32_t pkt_idx = 0;
1591         uint16_t num_buffers;
1592         uint16_t avail_head;
1593
1594         struct vhost_async *async = vq->async;
1595         struct async_inflight_info *pkts_info = async->pkts_info;
1596         uint32_t pkt_err = 0;
1597         uint16_t n_xfer;
1598         uint16_t slot_idx = 0;
1599
1600         /*
1601          * The ordering between avail index and desc reads need to be enforced.
1602          */
1603         avail_head = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE);
1604
1605         rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1606
1607         async_iter_reset(async);
1608
1609         for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1610                 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1611                 uint16_t nr_vec = 0;
1612
1613                 if (unlikely(reserve_avail_buf_split(dev, vq, pkt_len, buf_vec,
1614                                                 &num_buffers, avail_head, &nr_vec) < 0)) {
1615                         VHOST_LOG_DATA(DEBUG, "(%s) failed to get enough desc from vring\n",
1616                                         dev->ifname);
1617                         vq->shadow_used_idx -= num_buffers;
1618                         break;
1619                 }
1620
1621                 VHOST_LOG_DATA(DEBUG, "(%s) current index %d | end index %d\n",
1622                         dev->ifname, vq->last_avail_idx, vq->last_avail_idx + num_buffers);
1623
1624                 if (mbuf_to_desc(dev, vq, pkts[pkt_idx], buf_vec, nr_vec, num_buffers, true) < 0) {
1625                         vq->shadow_used_idx -= num_buffers;
1626                         break;
1627                 }
1628
1629                 slot_idx = (async->pkts_idx + pkt_idx) & (vq->size - 1);
1630                 pkts_info[slot_idx].descs = num_buffers;
1631                 pkts_info[slot_idx].mbuf = pkts[pkt_idx];
1632
1633                 vq->last_avail_idx += num_buffers;
1634         }
1635
1636         if (unlikely(pkt_idx == 0))
1637                 return 0;
1638
1639         n_xfer = vhost_async_dma_transfer(dev, vq, dma_id, vchan_id, async->pkts_idx,
1640                         async->iov_iter, pkt_idx);
1641
1642         pkt_err = pkt_idx - n_xfer;
1643         if (unlikely(pkt_err)) {
1644                 uint16_t num_descs = 0;
1645
1646                 VHOST_LOG_DATA(DEBUG, "(%s) %s: failed to transfer %u packets for queue %u.\n",
1647                                 dev->ifname, __func__, pkt_err, queue_id);
1648
1649                 /* update number of completed packets */
1650                 pkt_idx = n_xfer;
1651
1652                 /* calculate the sum of descriptors to revert */
1653                 while (pkt_err-- > 0) {
1654                         num_descs += pkts_info[slot_idx & (vq->size - 1)].descs;
1655                         slot_idx--;
1656                 }
1657
1658                 /* recover shadow used ring and available ring */
1659                 vq->shadow_used_idx -= num_descs;
1660                 vq->last_avail_idx -= num_descs;
1661         }
1662
1663         /* keep used descriptors */
1664         if (likely(vq->shadow_used_idx)) {
1665                 uint16_t to = async->desc_idx_split & (vq->size - 1);
1666
1667                 store_dma_desc_info_split(vq->shadow_used_split,
1668                                 async->descs_split, vq->size, 0, to,
1669                                 vq->shadow_used_idx);
1670
1671                 async->desc_idx_split += vq->shadow_used_idx;
1672
1673                 async->pkts_idx += pkt_idx;
1674                 if (async->pkts_idx >= vq->size)
1675                         async->pkts_idx -= vq->size;
1676
1677                 async->pkts_inflight_n += pkt_idx;
1678                 vq->shadow_used_idx = 0;
1679         }
1680
1681         return pkt_idx;
1682 }
1683
1684
1685 static __rte_always_inline int
1686 vhost_enqueue_async_packed(struct virtio_net *dev,
1687                             struct vhost_virtqueue *vq,
1688                             struct rte_mbuf *pkt,
1689                             struct buf_vector *buf_vec,
1690                             uint16_t *nr_descs,
1691                             uint16_t *nr_buffers)
1692 {
1693         uint16_t nr_vec = 0;
1694         uint16_t avail_idx = vq->last_avail_idx;
1695         uint16_t max_tries, tries = 0;
1696         uint16_t buf_id = 0;
1697         uint32_t len = 0;
1698         uint16_t desc_count = 0;
1699         uint32_t size = pkt->pkt_len + sizeof(struct virtio_net_hdr_mrg_rxbuf);
1700         uint32_t buffer_len[vq->size];
1701         uint16_t buffer_buf_id[vq->size];
1702         uint16_t buffer_desc_count[vq->size];
1703
1704         if (rxvq_is_mergeable(dev))
1705                 max_tries = vq->size - 1;
1706         else
1707                 max_tries = 1;
1708
1709         while (size > 0) {
1710                 /*
1711                  * if we tried all available ring items, and still
1712                  * can't get enough buf, it means something abnormal
1713                  * happened.
1714                  */
1715                 if (unlikely(++tries > max_tries))
1716                         return -1;
1717
1718                 if (unlikely(fill_vec_buf_packed(dev, vq,
1719                                                 avail_idx, &desc_count,
1720                                                 buf_vec, &nr_vec,
1721                                                 &buf_id, &len,
1722                                                 VHOST_ACCESS_RW) < 0))
1723                         return -1;
1724
1725                 len = RTE_MIN(len, size);
1726                 size -= len;
1727
1728                 buffer_len[*nr_buffers] = len;
1729                 buffer_buf_id[*nr_buffers] = buf_id;
1730                 buffer_desc_count[*nr_buffers] = desc_count;
1731                 *nr_buffers += 1;
1732                 *nr_descs += desc_count;
1733                 avail_idx += desc_count;
1734                 if (avail_idx >= vq->size)
1735                         avail_idx -= vq->size;
1736         }
1737
1738         if (unlikely(mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec, *nr_buffers, true) < 0))
1739                 return -1;
1740
1741         vhost_shadow_enqueue_packed(vq, buffer_len, buffer_buf_id, buffer_desc_count, *nr_buffers);
1742
1743         return 0;
1744 }
1745
1746 static __rte_always_inline int16_t
1747 virtio_dev_rx_async_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
1748                             struct rte_mbuf *pkt, uint16_t *nr_descs, uint16_t *nr_buffers)
1749 {
1750         struct buf_vector buf_vec[BUF_VECTOR_MAX];
1751
1752         if (unlikely(vhost_enqueue_async_packed(dev, vq, pkt, buf_vec,
1753                                         nr_descs, nr_buffers) < 0)) {
1754                 VHOST_LOG_DATA(DEBUG, "(%s) failed to get enough desc from vring\n", dev->ifname);
1755                 return -1;
1756         }
1757
1758         VHOST_LOG_DATA(DEBUG, "(%s) current index %d | end index %d\n",
1759                         dev->ifname, vq->last_avail_idx, vq->last_avail_idx + *nr_descs);
1760
1761         return 0;
1762 }
1763
1764 static __rte_always_inline void
1765 dma_error_handler_packed(struct vhost_virtqueue *vq, uint16_t slot_idx,
1766                         uint32_t nr_err, uint32_t *pkt_idx)
1767 {
1768         uint16_t descs_err = 0;
1769         uint16_t buffers_err = 0;
1770         struct async_inflight_info *pkts_info = vq->async->pkts_info;
1771
1772         *pkt_idx -= nr_err;
1773         /* calculate the sum of buffers and descs of DMA-error packets. */
1774         while (nr_err-- > 0) {
1775                 descs_err += pkts_info[slot_idx % vq->size].descs;
1776                 buffers_err += pkts_info[slot_idx % vq->size].nr_buffers;
1777                 slot_idx--;
1778         }
1779
1780         if (vq->last_avail_idx >= descs_err) {
1781                 vq->last_avail_idx -= descs_err;
1782         } else {
1783                 vq->last_avail_idx = vq->last_avail_idx + vq->size - descs_err;
1784                 vq->avail_wrap_counter ^= 1;
1785         }
1786
1787         vq->shadow_used_idx -= buffers_err;
1788 }
1789
1790 static __rte_noinline uint32_t
1791 virtio_dev_rx_async_submit_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
1792                 uint16_t queue_id, struct rte_mbuf **pkts, uint32_t count,
1793                 int16_t dma_id, uint16_t vchan_id)
1794 {
1795         uint32_t pkt_idx = 0;
1796         uint32_t remained = count;
1797         uint16_t n_xfer;
1798         uint16_t num_buffers;
1799         uint16_t num_descs;
1800
1801         struct vhost_async *async = vq->async;
1802         struct async_inflight_info *pkts_info = async->pkts_info;
1803         uint32_t pkt_err = 0;
1804         uint16_t slot_idx = 0;
1805
1806         do {
1807                 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1808
1809                 num_buffers = 0;
1810                 num_descs = 0;
1811                 if (unlikely(virtio_dev_rx_async_packed(dev, vq, pkts[pkt_idx],
1812                                                 &num_descs, &num_buffers) < 0))
1813                         break;
1814
1815                 slot_idx = (async->pkts_idx + pkt_idx) % vq->size;
1816
1817                 pkts_info[slot_idx].descs = num_descs;
1818                 pkts_info[slot_idx].nr_buffers = num_buffers;
1819                 pkts_info[slot_idx].mbuf = pkts[pkt_idx];
1820
1821                 pkt_idx++;
1822                 remained--;
1823                 vq_inc_last_avail_packed(vq, num_descs);
1824         } while (pkt_idx < count);
1825
1826         if (unlikely(pkt_idx == 0))
1827                 return 0;
1828
1829         n_xfer = vhost_async_dma_transfer(dev, vq, dma_id, vchan_id, async->pkts_idx,
1830                         async->iov_iter, pkt_idx);
1831
1832         async_iter_reset(async);
1833
1834         pkt_err = pkt_idx - n_xfer;
1835         if (unlikely(pkt_err)) {
1836                 VHOST_LOG_DATA(DEBUG, "(%s) %s: failed to transfer %u packets for queue %u.\n",
1837                                 dev->ifname, __func__, pkt_err, queue_id);
1838                 dma_error_handler_packed(vq, slot_idx, pkt_err, &pkt_idx);
1839         }
1840
1841         if (likely(vq->shadow_used_idx)) {
1842                 /* keep used descriptors. */
1843                 store_dma_desc_info_packed(vq->shadow_used_packed, async->buffers_packed,
1844                                         vq->size, 0, async->buffer_idx_packed,
1845                                         vq->shadow_used_idx);
1846
1847                 async->buffer_idx_packed += vq->shadow_used_idx;
1848                 if (async->buffer_idx_packed >= vq->size)
1849                         async->buffer_idx_packed -= vq->size;
1850
1851                 async->pkts_idx += pkt_idx;
1852                 if (async->pkts_idx >= vq->size)
1853                         async->pkts_idx -= vq->size;
1854
1855                 vq->shadow_used_idx = 0;
1856                 async->pkts_inflight_n += pkt_idx;
1857         }
1858
1859         return pkt_idx;
1860 }
1861
1862 static __rte_always_inline void
1863 write_back_completed_descs_split(struct vhost_virtqueue *vq, uint16_t n_descs)
1864 {
1865         struct vhost_async *async = vq->async;
1866         uint16_t nr_left = n_descs;
1867         uint16_t nr_copy;
1868         uint16_t to, from;
1869
1870         do {
1871                 from = async->last_desc_idx_split & (vq->size - 1);
1872                 nr_copy = nr_left + from <= vq->size ? nr_left : vq->size - from;
1873                 to = vq->last_used_idx & (vq->size - 1);
1874
1875                 if (to + nr_copy <= vq->size) {
1876                         rte_memcpy(&vq->used->ring[to], &async->descs_split[from],
1877                                         nr_copy * sizeof(struct vring_used_elem));
1878                 } else {
1879                         uint16_t size = vq->size - to;
1880
1881                         rte_memcpy(&vq->used->ring[to], &async->descs_split[from],
1882                                         size * sizeof(struct vring_used_elem));
1883                         rte_memcpy(&vq->used->ring[0], &async->descs_split[from + size],
1884                                         (nr_copy - size) * sizeof(struct vring_used_elem));
1885                 }
1886
1887                 async->last_desc_idx_split += nr_copy;
1888                 vq->last_used_idx += nr_copy;
1889                 nr_left -= nr_copy;
1890         } while (nr_left > 0);
1891 }
1892
1893 static __rte_always_inline void
1894 write_back_completed_descs_packed(struct vhost_virtqueue *vq,
1895                                 uint16_t n_buffers)
1896 {
1897         struct vhost_async *async = vq->async;
1898         uint16_t from = async->last_buffer_idx_packed;
1899         uint16_t used_idx = vq->last_used_idx;
1900         uint16_t head_idx = vq->last_used_idx;
1901         uint16_t head_flags = 0;
1902         uint16_t i;
1903
1904         /* Split loop in two to save memory barriers */
1905         for (i = 0; i < n_buffers; i++) {
1906                 vq->desc_packed[used_idx].id = async->buffers_packed[from].id;
1907                 vq->desc_packed[used_idx].len = async->buffers_packed[from].len;
1908
1909                 used_idx += async->buffers_packed[from].count;
1910                 if (used_idx >= vq->size)
1911                         used_idx -= vq->size;
1912
1913                 from++;
1914                 if (from >= vq->size)
1915                         from = 0;
1916         }
1917
1918         /* The ordering for storing desc flags needs to be enforced. */
1919         rte_atomic_thread_fence(__ATOMIC_RELEASE);
1920
1921         from = async->last_buffer_idx_packed;
1922
1923         for (i = 0; i < n_buffers; i++) {
1924                 uint16_t flags;
1925
1926                 if (async->buffers_packed[from].len)
1927                         flags = VRING_DESC_F_WRITE;
1928                 else
1929                         flags = 0;
1930
1931                 if (vq->used_wrap_counter) {
1932                         flags |= VRING_DESC_F_USED;
1933                         flags |= VRING_DESC_F_AVAIL;
1934                 } else {
1935                         flags &= ~VRING_DESC_F_USED;
1936                         flags &= ~VRING_DESC_F_AVAIL;
1937                 }
1938
1939                 if (i > 0) {
1940                         vq->desc_packed[vq->last_used_idx].flags = flags;
1941                 } else {
1942                         head_idx = vq->last_used_idx;
1943                         head_flags = flags;
1944                 }
1945
1946                 vq_inc_last_used_packed(vq, async->buffers_packed[from].count);
1947
1948                 from++;
1949                 if (from == vq->size)
1950                         from = 0;
1951         }
1952
1953         vq->desc_packed[head_idx].flags = head_flags;
1954         async->last_buffer_idx_packed = from;
1955 }
1956
1957 static __rte_always_inline uint16_t
1958 vhost_poll_enqueue_completed(struct virtio_net *dev, uint16_t queue_id,
1959                 struct rte_mbuf **pkts, uint16_t count, int16_t dma_id,
1960                 uint16_t vchan_id)
1961 {
1962         struct vhost_virtqueue *vq = dev->virtqueue[queue_id];
1963         struct vhost_async *async = vq->async;
1964         struct async_inflight_info *pkts_info = async->pkts_info;
1965         uint16_t nr_cpl_pkts = 0;
1966         uint16_t n_descs = 0, n_buffers = 0;
1967         uint16_t start_idx, from, i;
1968
1969         /* Check completed copies for the given DMA vChannel */
1970         vhost_async_dma_check_completed(dev, dma_id, vchan_id, VHOST_DMA_MAX_COPY_COMPLETE);
1971
1972         start_idx = async_get_first_inflight_pkt_idx(vq);
1973         /**
1974          * Calculate the number of copy completed packets.
1975          * Note that there may be completed packets even if
1976          * no copies are reported done by the given DMA vChannel,
1977          * as it's possible that a virtqueue uses multiple DMA
1978          * vChannels.
1979          */
1980         from = start_idx;
1981         while (vq->async->pkts_cmpl_flag[from] && count--) {
1982                 vq->async->pkts_cmpl_flag[from] = false;
1983                 from++;
1984                 if (from >= vq->size)
1985                         from -= vq->size;
1986                 nr_cpl_pkts++;
1987         }
1988
1989         if (nr_cpl_pkts == 0)
1990                 return 0;
1991
1992         for (i = 0; i < nr_cpl_pkts; i++) {
1993                 from = (start_idx + i) % vq->size;
1994                 /* Only used with packed ring */
1995                 n_buffers += pkts_info[from].nr_buffers;
1996                 /* Only used with split ring */
1997                 n_descs += pkts_info[from].descs;
1998                 pkts[i] = pkts_info[from].mbuf;
1999         }
2000
2001         async->pkts_inflight_n -= nr_cpl_pkts;
2002
2003         if (likely(vq->enabled && vq->access_ok)) {
2004                 if (vq_is_packed(dev)) {
2005                         write_back_completed_descs_packed(vq, n_buffers);
2006                         vhost_vring_call_packed(dev, vq);
2007                 } else {
2008                         write_back_completed_descs_split(vq, n_descs);
2009                         __atomic_add_fetch(&vq->used->idx, n_descs, __ATOMIC_RELEASE);
2010                         vhost_vring_call_split(dev, vq);
2011                 }
2012         } else {
2013                 if (vq_is_packed(dev)) {
2014                         async->last_buffer_idx_packed += n_buffers;
2015                         if (async->last_buffer_idx_packed >= vq->size)
2016                                 async->last_buffer_idx_packed -= vq->size;
2017                 } else {
2018                         async->last_desc_idx_split += n_descs;
2019                 }
2020         }
2021
2022         return nr_cpl_pkts;
2023 }
2024
2025 uint16_t
2026 rte_vhost_poll_enqueue_completed(int vid, uint16_t queue_id,
2027                 struct rte_mbuf **pkts, uint16_t count, int16_t dma_id,
2028                 uint16_t vchan_id)
2029 {
2030         struct virtio_net *dev = get_device(vid);
2031         struct vhost_virtqueue *vq;
2032         uint16_t n_pkts_cpl = 0;
2033
2034         if (unlikely(!dev))
2035                 return 0;
2036
2037         VHOST_LOG_DATA(DEBUG, "(%s) %s\n", dev->ifname, __func__);
2038         if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
2039                 VHOST_LOG_DATA(ERR, "(%s) %s: invalid virtqueue idx %d.\n",
2040                         dev->ifname, __func__, queue_id);
2041                 return 0;
2042         }
2043
2044         if (unlikely(!dma_copy_track[dma_id].vchans ||
2045                                 !dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr)) {
2046                 VHOST_LOG_DATA(ERR, "(%s) %s: invalid channel %d:%u.\n", dev->ifname, __func__,
2047                                dma_id, vchan_id);
2048                 return 0;
2049         }
2050
2051         vq = dev->virtqueue[queue_id];
2052
2053         if (!rte_spinlock_trylock(&vq->access_lock)) {
2054                 VHOST_LOG_DATA(DEBUG, "(%s) %s: virtqueue %u is busy.\n", dev->ifname, __func__,
2055                                 queue_id);
2056                 return 0;
2057         }
2058
2059         if (unlikely(!vq->async)) {
2060                 VHOST_LOG_DATA(ERR, "(%s) %s: async not registered for virtqueue %d.\n",
2061                                 dev->ifname, __func__, queue_id);
2062                 goto out;
2063         }
2064
2065         n_pkts_cpl = vhost_poll_enqueue_completed(dev, queue_id, pkts, count, dma_id, vchan_id);
2066
2067 out:
2068         rte_spinlock_unlock(&vq->access_lock);
2069
2070         return n_pkts_cpl;
2071 }
2072
2073 uint16_t
2074 rte_vhost_clear_queue_thread_unsafe(int vid, uint16_t queue_id,
2075                 struct rte_mbuf **pkts, uint16_t count, int16_t dma_id,
2076                 uint16_t vchan_id)
2077 {
2078         struct virtio_net *dev = get_device(vid);
2079         struct vhost_virtqueue *vq;
2080         uint16_t n_pkts_cpl = 0;
2081
2082         if (!dev)
2083                 return 0;
2084
2085         VHOST_LOG_DATA(DEBUG, "(%s) %s\n", dev->ifname, __func__);
2086         if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
2087                 VHOST_LOG_DATA(ERR, "(%s) %s: invalid virtqueue idx %d.\n",
2088                         dev->ifname, __func__, queue_id);
2089                 return 0;
2090         }
2091
2092         vq = dev->virtqueue[queue_id];
2093
2094         if (unlikely(!vq->async)) {
2095                 VHOST_LOG_DATA(ERR, "(%s) %s: async not registered for queue id %d.\n",
2096                         dev->ifname, __func__, queue_id);
2097                 return 0;
2098         }
2099
2100         if (unlikely(!dma_copy_track[dma_id].vchans ||
2101                                 !dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr)) {
2102                 VHOST_LOG_DATA(ERR, "(%s) %s: invalid channel %d:%u.\n", dev->ifname, __func__,
2103                                 dma_id, vchan_id);
2104                 return 0;
2105         }
2106
2107         n_pkts_cpl = vhost_poll_enqueue_completed(dev, queue_id, pkts, count, dma_id, vchan_id);
2108
2109         return n_pkts_cpl;
2110 }
2111
2112 static __rte_always_inline uint32_t
2113 virtio_dev_rx_async_submit(struct virtio_net *dev, uint16_t queue_id,
2114         struct rte_mbuf **pkts, uint32_t count, int16_t dma_id, uint16_t vchan_id)
2115 {
2116         struct vhost_virtqueue *vq;
2117         uint32_t nb_tx = 0;
2118
2119         VHOST_LOG_DATA(DEBUG, "(%s) %s\n", dev->ifname, __func__);
2120         if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
2121                 VHOST_LOG_DATA(ERR, "(%s) %s: invalid virtqueue idx %d.\n",
2122                         dev->ifname, __func__, queue_id);
2123                 return 0;
2124         }
2125
2126         if (unlikely(!dma_copy_track[dma_id].vchans ||
2127                                 !dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr)) {
2128                 VHOST_LOG_DATA(ERR, "(%s) %s: invalid channel %d:%u.\n", dev->ifname, __func__,
2129                                dma_id, vchan_id);
2130                 return 0;
2131         }
2132
2133         vq = dev->virtqueue[queue_id];
2134
2135         rte_spinlock_lock(&vq->access_lock);
2136
2137         if (unlikely(!vq->enabled || !vq->async))
2138                 goto out_access_unlock;
2139
2140         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2141                 vhost_user_iotlb_rd_lock(vq);
2142
2143         if (unlikely(!vq->access_ok))
2144                 if (unlikely(vring_translate(dev, vq) < 0))
2145                         goto out;
2146
2147         count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
2148         if (count == 0)
2149                 goto out;
2150
2151         if (vq_is_packed(dev))
2152                 nb_tx = virtio_dev_rx_async_submit_packed(dev, vq, queue_id,
2153                                 pkts, count, dma_id, vchan_id);
2154         else
2155                 nb_tx = virtio_dev_rx_async_submit_split(dev, vq, queue_id,
2156                                 pkts, count, dma_id, vchan_id);
2157
2158 out:
2159         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2160                 vhost_user_iotlb_rd_unlock(vq);
2161
2162 out_access_unlock:
2163         rte_spinlock_unlock(&vq->access_lock);
2164
2165         return nb_tx;
2166 }
2167
2168 uint16_t
2169 rte_vhost_submit_enqueue_burst(int vid, uint16_t queue_id,
2170                 struct rte_mbuf **pkts, uint16_t count, int16_t dma_id,
2171                 uint16_t vchan_id)
2172 {
2173         struct virtio_net *dev = get_device(vid);
2174
2175         if (!dev)
2176                 return 0;
2177
2178         if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
2179                 VHOST_LOG_DATA(ERR, "(%s) %s: built-in vhost net backend is disabled.\n",
2180                         dev->ifname, __func__);
2181                 return 0;
2182         }
2183
2184         return virtio_dev_rx_async_submit(dev, queue_id, pkts, count, dma_id, vchan_id);
2185 }
2186
2187 static inline bool
2188 virtio_net_with_host_offload(struct virtio_net *dev)
2189 {
2190         if (dev->features &
2191                         ((1ULL << VIRTIO_NET_F_CSUM) |
2192                          (1ULL << VIRTIO_NET_F_HOST_ECN) |
2193                          (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2194                          (1ULL << VIRTIO_NET_F_HOST_TSO6) |
2195                          (1ULL << VIRTIO_NET_F_HOST_UFO)))
2196                 return true;
2197
2198         return false;
2199 }
2200
2201 static int
2202 parse_headers(struct rte_mbuf *m, uint8_t *l4_proto)
2203 {
2204         struct rte_ipv4_hdr *ipv4_hdr;
2205         struct rte_ipv6_hdr *ipv6_hdr;
2206         struct rte_ether_hdr *eth_hdr;
2207         uint16_t ethertype;
2208         uint16_t data_len = rte_pktmbuf_data_len(m);
2209
2210         if (data_len < sizeof(struct rte_ether_hdr))
2211                 return -EINVAL;
2212
2213         eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
2214
2215         m->l2_len = sizeof(struct rte_ether_hdr);
2216         ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
2217
2218         if (ethertype == RTE_ETHER_TYPE_VLAN) {
2219                 if (data_len < sizeof(struct rte_ether_hdr) +
2220                                 sizeof(struct rte_vlan_hdr))
2221                         goto error;
2222
2223                 struct rte_vlan_hdr *vlan_hdr =
2224                         (struct rte_vlan_hdr *)(eth_hdr + 1);
2225
2226                 m->l2_len += sizeof(struct rte_vlan_hdr);
2227                 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
2228         }
2229
2230         switch (ethertype) {
2231         case RTE_ETHER_TYPE_IPV4:
2232                 if (data_len < m->l2_len + sizeof(struct rte_ipv4_hdr))
2233                         goto error;
2234                 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *,
2235                                 m->l2_len);
2236                 m->l3_len = rte_ipv4_hdr_len(ipv4_hdr);
2237                 if (data_len < m->l2_len + m->l3_len)
2238                         goto error;
2239                 m->ol_flags |= RTE_MBUF_F_TX_IPV4;
2240                 *l4_proto = ipv4_hdr->next_proto_id;
2241                 break;
2242         case RTE_ETHER_TYPE_IPV6:
2243                 if (data_len < m->l2_len + sizeof(struct rte_ipv6_hdr))
2244                         goto error;
2245                 ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv6_hdr *,
2246                                 m->l2_len);
2247                 m->l3_len = sizeof(struct rte_ipv6_hdr);
2248                 m->ol_flags |= RTE_MBUF_F_TX_IPV6;
2249                 *l4_proto = ipv6_hdr->proto;
2250                 break;
2251         default:
2252                 /* a valid L3 header is needed for further L4 parsing */
2253                 goto error;
2254         }
2255
2256         /* both CSUM and GSO need a valid L4 header */
2257         switch (*l4_proto) {
2258         case IPPROTO_TCP:
2259                 if (data_len < m->l2_len + m->l3_len +
2260                                 sizeof(struct rte_tcp_hdr))
2261                         goto error;
2262                 break;
2263         case IPPROTO_UDP:
2264                 if (data_len < m->l2_len + m->l3_len +
2265                                 sizeof(struct rte_udp_hdr))
2266                         goto error;
2267                 break;
2268         case IPPROTO_SCTP:
2269                 if (data_len < m->l2_len + m->l3_len +
2270                                 sizeof(struct rte_sctp_hdr))
2271                         goto error;
2272                 break;
2273         default:
2274                 goto error;
2275         }
2276
2277         return 0;
2278
2279 error:
2280         m->l2_len = 0;
2281         m->l3_len = 0;
2282         m->ol_flags = 0;
2283         return -EINVAL;
2284 }
2285
2286 static __rte_always_inline void
2287 vhost_dequeue_offload_legacy(struct virtio_net *dev, struct virtio_net_hdr *hdr,
2288                 struct rte_mbuf *m)
2289 {
2290         uint8_t l4_proto = 0;
2291         struct rte_tcp_hdr *tcp_hdr = NULL;
2292         uint16_t tcp_len;
2293         uint16_t data_len = rte_pktmbuf_data_len(m);
2294
2295         if (parse_headers(m, &l4_proto) < 0)
2296                 return;
2297
2298         if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
2299                 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
2300                         switch (hdr->csum_offset) {
2301                         case (offsetof(struct rte_tcp_hdr, cksum)):
2302                                 if (l4_proto != IPPROTO_TCP)
2303                                         goto error;
2304                                 m->ol_flags |= RTE_MBUF_F_TX_TCP_CKSUM;
2305                                 break;
2306                         case (offsetof(struct rte_udp_hdr, dgram_cksum)):
2307                                 if (l4_proto != IPPROTO_UDP)
2308                                         goto error;
2309                                 m->ol_flags |= RTE_MBUF_F_TX_UDP_CKSUM;
2310                                 break;
2311                         case (offsetof(struct rte_sctp_hdr, cksum)):
2312                                 if (l4_proto != IPPROTO_SCTP)
2313                                         goto error;
2314                                 m->ol_flags |= RTE_MBUF_F_TX_SCTP_CKSUM;
2315                                 break;
2316                         default:
2317                                 goto error;
2318                         }
2319                 } else {
2320                         goto error;
2321                 }
2322         }
2323
2324         if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
2325                 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
2326                 case VIRTIO_NET_HDR_GSO_TCPV4:
2327                 case VIRTIO_NET_HDR_GSO_TCPV6:
2328                         if (l4_proto != IPPROTO_TCP)
2329                                 goto error;
2330                         tcp_hdr = rte_pktmbuf_mtod_offset(m,
2331                                         struct rte_tcp_hdr *,
2332                                         m->l2_len + m->l3_len);
2333                         tcp_len = (tcp_hdr->data_off & 0xf0) >> 2;
2334                         if (data_len < m->l2_len + m->l3_len + tcp_len)
2335                                 goto error;
2336                         m->ol_flags |= RTE_MBUF_F_TX_TCP_SEG;
2337                         m->tso_segsz = hdr->gso_size;
2338                         m->l4_len = tcp_len;
2339                         break;
2340                 case VIRTIO_NET_HDR_GSO_UDP:
2341                         if (l4_proto != IPPROTO_UDP)
2342                                 goto error;
2343                         m->ol_flags |= RTE_MBUF_F_TX_UDP_SEG;
2344                         m->tso_segsz = hdr->gso_size;
2345                         m->l4_len = sizeof(struct rte_udp_hdr);
2346                         break;
2347                 default:
2348                         VHOST_LOG_DATA(WARNING, "(%s) unsupported gso type %u.\n",
2349                                         dev->ifname, hdr->gso_type);
2350                         goto error;
2351                 }
2352         }
2353         return;
2354
2355 error:
2356         m->l2_len = 0;
2357         m->l3_len = 0;
2358         m->ol_flags = 0;
2359 }
2360
2361 static __rte_always_inline void
2362 vhost_dequeue_offload(struct virtio_net *dev, struct virtio_net_hdr *hdr,
2363                 struct rte_mbuf *m, bool legacy_ol_flags)
2364 {
2365         struct rte_net_hdr_lens hdr_lens;
2366         int l4_supported = 0;
2367         uint32_t ptype;
2368
2369         if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
2370                 return;
2371
2372         if (legacy_ol_flags) {
2373                 vhost_dequeue_offload_legacy(dev, hdr, m);
2374                 return;
2375         }
2376
2377         m->ol_flags |= RTE_MBUF_F_RX_IP_CKSUM_UNKNOWN;
2378
2379         ptype = rte_net_get_ptype(m, &hdr_lens, RTE_PTYPE_ALL_MASK);
2380         m->packet_type = ptype;
2381         if ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP ||
2382             (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP ||
2383             (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_SCTP)
2384                 l4_supported = 1;
2385
2386         /* According to Virtio 1.1 spec, the device only needs to look at
2387          * VIRTIO_NET_HDR_F_NEEDS_CSUM in the packet transmission path.
2388          * This differs from the processing incoming packets path where the
2389          * driver could rely on VIRTIO_NET_HDR_F_DATA_VALID flag set by the
2390          * device.
2391          *
2392          * 5.1.6.2.1 Driver Requirements: Packet Transmission
2393          * The driver MUST NOT set the VIRTIO_NET_HDR_F_DATA_VALID and
2394          * VIRTIO_NET_HDR_F_RSC_INFO bits in flags.
2395          *
2396          * 5.1.6.2.2 Device Requirements: Packet Transmission
2397          * The device MUST ignore flag bits that it does not recognize.
2398          */
2399         if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
2400                 uint32_t hdrlen;
2401
2402                 hdrlen = hdr_lens.l2_len + hdr_lens.l3_len + hdr_lens.l4_len;
2403                 if (hdr->csum_start <= hdrlen && l4_supported != 0) {
2404                         m->ol_flags |= RTE_MBUF_F_RX_L4_CKSUM_NONE;
2405                 } else {
2406                         /* Unknown proto or tunnel, do sw cksum. We can assume
2407                          * the cksum field is in the first segment since the
2408                          * buffers we provided to the host are large enough.
2409                          * In case of SCTP, this will be wrong since it's a CRC
2410                          * but there's nothing we can do.
2411                          */
2412                         uint16_t csum = 0, off;
2413
2414                         if (rte_raw_cksum_mbuf(m, hdr->csum_start,
2415                                         rte_pktmbuf_pkt_len(m) - hdr->csum_start, &csum) < 0)
2416                                 return;
2417                         if (likely(csum != 0xffff))
2418                                 csum = ~csum;
2419                         off = hdr->csum_offset + hdr->csum_start;
2420                         if (rte_pktmbuf_data_len(m) >= off + 1)
2421                                 *rte_pktmbuf_mtod_offset(m, uint16_t *, off) = csum;
2422                 }
2423         }
2424
2425         if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
2426                 if (hdr->gso_size == 0)
2427                         return;
2428
2429                 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
2430                 case VIRTIO_NET_HDR_GSO_TCPV4:
2431                 case VIRTIO_NET_HDR_GSO_TCPV6:
2432                         if ((ptype & RTE_PTYPE_L4_MASK) != RTE_PTYPE_L4_TCP)
2433                                 break;
2434                         m->ol_flags |= RTE_MBUF_F_RX_LRO | RTE_MBUF_F_RX_L4_CKSUM_NONE;
2435                         m->tso_segsz = hdr->gso_size;
2436                         break;
2437                 case VIRTIO_NET_HDR_GSO_UDP:
2438                         if ((ptype & RTE_PTYPE_L4_MASK) != RTE_PTYPE_L4_UDP)
2439                                 break;
2440                         m->ol_flags |= RTE_MBUF_F_RX_LRO | RTE_MBUF_F_RX_L4_CKSUM_NONE;
2441                         m->tso_segsz = hdr->gso_size;
2442                         break;
2443                 default:
2444                         break;
2445                 }
2446         }
2447 }
2448
2449 static __rte_noinline void
2450 copy_vnet_hdr_from_desc(struct virtio_net_hdr *hdr,
2451                 struct buf_vector *buf_vec)
2452 {
2453         uint64_t len;
2454         uint64_t remain = sizeof(struct virtio_net_hdr);
2455         uint64_t src;
2456         uint64_t dst = (uint64_t)(uintptr_t)hdr;
2457
2458         while (remain) {
2459                 len = RTE_MIN(remain, buf_vec->buf_len);
2460                 src = buf_vec->buf_addr;
2461                 rte_memcpy((void *)(uintptr_t)dst,
2462                                 (void *)(uintptr_t)src, len);
2463
2464                 remain -= len;
2465                 dst += len;
2466                 buf_vec++;
2467         }
2468 }
2469
2470 static __rte_always_inline int
2471 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
2472                   struct buf_vector *buf_vec, uint16_t nr_vec,
2473                   struct rte_mbuf *m, struct rte_mempool *mbuf_pool,
2474                   bool legacy_ol_flags)
2475 {
2476         uint32_t buf_avail, buf_offset;
2477         uint64_t buf_addr, buf_len;
2478         uint32_t mbuf_avail, mbuf_offset;
2479         uint32_t cpy_len;
2480         struct rte_mbuf *cur = m, *prev = m;
2481         struct virtio_net_hdr tmp_hdr;
2482         struct virtio_net_hdr *hdr = NULL;
2483         /* A counter to avoid desc dead loop chain */
2484         uint16_t vec_idx = 0;
2485         struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
2486         int error = 0;
2487
2488         buf_addr = buf_vec[vec_idx].buf_addr;
2489         buf_len = buf_vec[vec_idx].buf_len;
2490
2491         if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
2492                 error = -1;
2493                 goto out;
2494         }
2495
2496         if (virtio_net_with_host_offload(dev)) {
2497                 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
2498                         /*
2499                          * No luck, the virtio-net header doesn't fit
2500                          * in a contiguous virtual area.
2501                          */
2502                         copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
2503                         hdr = &tmp_hdr;
2504                 } else {
2505                         hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
2506                 }
2507         }
2508
2509         /*
2510          * A virtio driver normally uses at least 2 desc buffers
2511          * for Tx: the first for storing the header, and others
2512          * for storing the data.
2513          */
2514         if (unlikely(buf_len < dev->vhost_hlen)) {
2515                 buf_offset = dev->vhost_hlen - buf_len;
2516                 vec_idx++;
2517                 buf_addr = buf_vec[vec_idx].buf_addr;
2518                 buf_len = buf_vec[vec_idx].buf_len;
2519                 buf_avail  = buf_len - buf_offset;
2520         } else if (buf_len == dev->vhost_hlen) {
2521                 if (unlikely(++vec_idx >= nr_vec))
2522                         goto out;
2523                 buf_addr = buf_vec[vec_idx].buf_addr;
2524                 buf_len = buf_vec[vec_idx].buf_len;
2525
2526                 buf_offset = 0;
2527                 buf_avail = buf_len;
2528         } else {
2529                 buf_offset = dev->vhost_hlen;
2530                 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
2531         }
2532
2533         PRINT_PACKET(dev,
2534                         (uintptr_t)(buf_addr + buf_offset),
2535                         (uint32_t)buf_avail, 0);
2536
2537         mbuf_offset = 0;
2538         mbuf_avail  = m->buf_len - RTE_PKTMBUF_HEADROOM;
2539         while (1) {
2540                 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
2541
2542                 if (likely(cpy_len > MAX_BATCH_LEN ||
2543                                         vq->batch_copy_nb_elems >= vq->size ||
2544                                         (hdr && cur == m))) {
2545                         rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
2546                                                 mbuf_offset),
2547                                         (void *)((uintptr_t)(buf_addr +
2548                                                         buf_offset)), cpy_len);
2549                 } else {
2550                         batch_copy[vq->batch_copy_nb_elems].dst =
2551                                 rte_pktmbuf_mtod_offset(cur, void *,
2552                                                 mbuf_offset);
2553                         batch_copy[vq->batch_copy_nb_elems].src =
2554                                 (void *)((uintptr_t)(buf_addr + buf_offset));
2555                         batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
2556                         vq->batch_copy_nb_elems++;
2557                 }
2558
2559                 mbuf_avail  -= cpy_len;
2560                 mbuf_offset += cpy_len;
2561                 buf_avail -= cpy_len;
2562                 buf_offset += cpy_len;
2563
2564                 /* This buf reaches to its end, get the next one */
2565                 if (buf_avail == 0) {
2566                         if (++vec_idx >= nr_vec)
2567                                 break;
2568
2569                         buf_addr = buf_vec[vec_idx].buf_addr;
2570                         buf_len = buf_vec[vec_idx].buf_len;
2571
2572                         buf_offset = 0;
2573                         buf_avail  = buf_len;
2574
2575                         PRINT_PACKET(dev, (uintptr_t)buf_addr,
2576                                         (uint32_t)buf_avail, 0);
2577                 }
2578
2579                 /*
2580                  * This mbuf reaches to its end, get a new one
2581                  * to hold more data.
2582                  */
2583                 if (mbuf_avail == 0) {
2584                         cur = rte_pktmbuf_alloc(mbuf_pool);
2585                         if (unlikely(cur == NULL)) {
2586                                 VHOST_LOG_DATA(ERR, "(%s) failed to allocate memory for mbuf.\n",
2587                                                 dev->ifname);
2588                                 error = -1;
2589                                 goto out;
2590                         }
2591
2592                         prev->next = cur;
2593                         prev->data_len = mbuf_offset;
2594                         m->nb_segs += 1;
2595                         m->pkt_len += mbuf_offset;
2596                         prev = cur;
2597
2598                         mbuf_offset = 0;
2599                         mbuf_avail  = cur->buf_len - RTE_PKTMBUF_HEADROOM;
2600                 }
2601         }
2602
2603         prev->data_len = mbuf_offset;
2604         m->pkt_len    += mbuf_offset;
2605
2606         if (hdr)
2607                 vhost_dequeue_offload(dev, hdr, m, legacy_ol_flags);
2608
2609 out:
2610
2611         return error;
2612 }
2613
2614 static void
2615 virtio_dev_extbuf_free(void *addr __rte_unused, void *opaque)
2616 {
2617         rte_free(opaque);
2618 }
2619
2620 static int
2621 virtio_dev_extbuf_alloc(struct virtio_net *dev, struct rte_mbuf *pkt, uint32_t size)
2622 {
2623         struct rte_mbuf_ext_shared_info *shinfo = NULL;
2624         uint32_t total_len = RTE_PKTMBUF_HEADROOM + size;
2625         uint16_t buf_len;
2626         rte_iova_t iova;
2627         void *buf;
2628
2629         total_len += sizeof(*shinfo) + sizeof(uintptr_t);
2630         total_len = RTE_ALIGN_CEIL(total_len, sizeof(uintptr_t));
2631
2632         if (unlikely(total_len > UINT16_MAX))
2633                 return -ENOSPC;
2634
2635         buf_len = total_len;
2636         buf = rte_malloc(NULL, buf_len, RTE_CACHE_LINE_SIZE);
2637         if (unlikely(buf == NULL))
2638                 return -ENOMEM;
2639
2640         /* Initialize shinfo */
2641         shinfo = rte_pktmbuf_ext_shinfo_init_helper(buf, &buf_len,
2642                                                 virtio_dev_extbuf_free, buf);
2643         if (unlikely(shinfo == NULL)) {
2644                 rte_free(buf);
2645                 VHOST_LOG_DATA(ERR, "(%s) failed to init shinfo\n", dev->ifname);
2646                 return -1;
2647         }
2648
2649         iova = rte_malloc_virt2iova(buf);
2650         rte_pktmbuf_attach_extbuf(pkt, buf, iova, buf_len, shinfo);
2651         rte_pktmbuf_reset_headroom(pkt);
2652
2653         return 0;
2654 }
2655
2656 /*
2657  * Prepare a host supported pktmbuf.
2658  */
2659 static __rte_always_inline int
2660 virtio_dev_pktmbuf_prep(struct virtio_net *dev, struct rte_mbuf *pkt,
2661                          uint32_t data_len)
2662 {
2663         if (rte_pktmbuf_tailroom(pkt) >= data_len)
2664                 return 0;
2665
2666         /* attach an external buffer if supported */
2667         if (dev->extbuf && !virtio_dev_extbuf_alloc(dev, pkt, data_len))
2668                 return 0;
2669
2670         /* check if chained buffers are allowed */
2671         if (!dev->linearbuf)
2672                 return 0;
2673
2674         return -1;
2675 }
2676
2677 __rte_always_inline
2678 static uint16_t
2679 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
2680         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count,
2681         bool legacy_ol_flags)
2682 {
2683         uint16_t i;
2684         uint16_t free_entries;
2685         uint16_t dropped = 0;
2686         static bool allocerr_warned;
2687
2688         /*
2689          * The ordering between avail index and
2690          * desc reads needs to be enforced.
2691          */
2692         free_entries = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE) -
2693                         vq->last_avail_idx;
2694         if (free_entries == 0)
2695                 return 0;
2696
2697         rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
2698
2699         VHOST_LOG_DATA(DEBUG, "(%s) %s\n", dev->ifname, __func__);
2700
2701         count = RTE_MIN(count, MAX_PKT_BURST);
2702         count = RTE_MIN(count, free_entries);
2703         VHOST_LOG_DATA(DEBUG, "(%s) about to dequeue %u buffers\n",
2704                         dev->ifname, count);
2705
2706         if (rte_pktmbuf_alloc_bulk(mbuf_pool, pkts, count))
2707                 return 0;
2708
2709         for (i = 0; i < count; i++) {
2710                 struct buf_vector buf_vec[BUF_VECTOR_MAX];
2711                 uint16_t head_idx;
2712                 uint32_t buf_len;
2713                 uint16_t nr_vec = 0;
2714                 int err;
2715
2716                 if (unlikely(fill_vec_buf_split(dev, vq,
2717                                                 vq->last_avail_idx + i,
2718                                                 &nr_vec, buf_vec,
2719                                                 &head_idx, &buf_len,
2720                                                 VHOST_ACCESS_RO) < 0))
2721                         break;
2722
2723                 update_shadow_used_ring_split(vq, head_idx, 0);
2724
2725                 err = virtio_dev_pktmbuf_prep(dev, pkts[i], buf_len);
2726                 if (unlikely(err)) {
2727                         /*
2728                          * mbuf allocation fails for jumbo packets when external
2729                          * buffer allocation is not allowed and linear buffer
2730                          * is required. Drop this packet.
2731                          */
2732                         if (!allocerr_warned) {
2733                                 VHOST_LOG_DATA(ERR, "(%s) failed mbuf alloc of size %d from %s.\n",
2734                                         dev->ifname, buf_len, mbuf_pool->name);
2735                                 allocerr_warned = true;
2736                         }
2737                         dropped += 1;
2738                         i++;
2739                         break;
2740                 }
2741
2742                 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
2743                                 mbuf_pool, legacy_ol_flags);
2744                 if (unlikely(err)) {
2745                         if (!allocerr_warned) {
2746                                 VHOST_LOG_DATA(ERR, "(%s) failed to copy desc to mbuf.\n",
2747                                         dev->ifname);
2748                                 allocerr_warned = true;
2749                         }
2750                         dropped += 1;
2751                         i++;
2752                         break;
2753                 }
2754         }
2755
2756         if (dropped)
2757                 rte_pktmbuf_free_bulk(&pkts[i - 1], count - i + 1);
2758
2759         vq->last_avail_idx += i;
2760
2761         do_data_copy_dequeue(vq);
2762         if (unlikely(i < count))
2763                 vq->shadow_used_idx = i;
2764         if (likely(vq->shadow_used_idx)) {
2765                 flush_shadow_used_ring_split(dev, vq);
2766                 vhost_vring_call_split(dev, vq);
2767         }
2768
2769         return (i - dropped);
2770 }
2771
2772 __rte_noinline
2773 static uint16_t
2774 virtio_dev_tx_split_legacy(struct virtio_net *dev,
2775         struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
2776         struct rte_mbuf **pkts, uint16_t count)
2777 {
2778         return virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count, true);
2779 }
2780
2781 __rte_noinline
2782 static uint16_t
2783 virtio_dev_tx_split_compliant(struct virtio_net *dev,
2784         struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
2785         struct rte_mbuf **pkts, uint16_t count)
2786 {
2787         return virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count, false);
2788 }
2789
2790 static __rte_always_inline int
2791 vhost_reserve_avail_batch_packed(struct virtio_net *dev,
2792                                  struct vhost_virtqueue *vq,
2793                                  struct rte_mbuf **pkts,
2794                                  uint16_t avail_idx,
2795                                  uintptr_t *desc_addrs,
2796                                  uint16_t *ids)
2797 {
2798         bool wrap = vq->avail_wrap_counter;
2799         struct vring_packed_desc *descs = vq->desc_packed;
2800         uint64_t lens[PACKED_BATCH_SIZE];
2801         uint64_t buf_lens[PACKED_BATCH_SIZE];
2802         uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2803         uint16_t flags, i;
2804
2805         if (unlikely(avail_idx & PACKED_BATCH_MASK))
2806                 return -1;
2807         if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
2808                 return -1;
2809
2810         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2811                 flags = descs[avail_idx + i].flags;
2812                 if (unlikely((wrap != !!(flags & VRING_DESC_F_AVAIL)) ||
2813                              (wrap == !!(flags & VRING_DESC_F_USED))  ||
2814                              (flags & PACKED_DESC_SINGLE_DEQUEUE_FLAG)))
2815                         return -1;
2816         }
2817
2818         rte_atomic_thread_fence(__ATOMIC_ACQUIRE);
2819
2820         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2821                 lens[i] = descs[avail_idx + i].len;
2822
2823         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2824                 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
2825                                                   descs[avail_idx + i].addr,
2826                                                   &lens[i], VHOST_ACCESS_RW);
2827         }
2828
2829         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2830                 if (unlikely(!desc_addrs[i]))
2831                         return -1;
2832                 if (unlikely((lens[i] != descs[avail_idx + i].len)))
2833                         return -1;
2834         }
2835
2836         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2837                 if (virtio_dev_pktmbuf_prep(dev, pkts[i], lens[i]))
2838                         goto err;
2839         }
2840
2841         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2842                 buf_lens[i] = pkts[i]->buf_len - pkts[i]->data_off;
2843
2844         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2845                 if (unlikely(buf_lens[i] < (lens[i] - buf_offset)))
2846                         goto err;
2847         }
2848
2849         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2850                 pkts[i]->pkt_len = lens[i] - buf_offset;
2851                 pkts[i]->data_len = pkts[i]->pkt_len;
2852                 ids[i] = descs[avail_idx + i].id;
2853         }
2854
2855         return 0;
2856
2857 err:
2858         return -1;
2859 }
2860
2861 static __rte_always_inline int
2862 virtio_dev_tx_batch_packed(struct virtio_net *dev,
2863                            struct vhost_virtqueue *vq,
2864                            struct rte_mbuf **pkts,
2865                            bool legacy_ol_flags)
2866 {
2867         uint16_t avail_idx = vq->last_avail_idx;
2868         uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2869         struct virtio_net_hdr *hdr;
2870         uintptr_t desc_addrs[PACKED_BATCH_SIZE];
2871         uint16_t ids[PACKED_BATCH_SIZE];
2872         uint16_t i;
2873
2874         if (vhost_reserve_avail_batch_packed(dev, vq, pkts, avail_idx,
2875                                              desc_addrs, ids))
2876                 return -1;
2877
2878         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2879                 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
2880
2881         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2882                 rte_memcpy(rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
2883                            (void *)(uintptr_t)(desc_addrs[i] + buf_offset),
2884                            pkts[i]->pkt_len);
2885
2886         if (virtio_net_with_host_offload(dev)) {
2887                 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2888                         hdr = (struct virtio_net_hdr *)(desc_addrs[i]);
2889                         vhost_dequeue_offload(dev, hdr, pkts[i], legacy_ol_flags);
2890                 }
2891         }
2892
2893         if (virtio_net_is_inorder(dev))
2894                 vhost_shadow_dequeue_batch_packed_inorder(vq,
2895                         ids[PACKED_BATCH_SIZE - 1]);
2896         else
2897                 vhost_shadow_dequeue_batch_packed(dev, vq, ids);
2898
2899         vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
2900
2901         return 0;
2902 }
2903
2904 static __rte_always_inline int
2905 vhost_dequeue_single_packed(struct virtio_net *dev,
2906                             struct vhost_virtqueue *vq,
2907                             struct rte_mempool *mbuf_pool,
2908                             struct rte_mbuf *pkts,
2909                             uint16_t *buf_id,
2910                             uint16_t *desc_count,
2911                             bool legacy_ol_flags)
2912 {
2913         struct buf_vector buf_vec[BUF_VECTOR_MAX];
2914         uint32_t buf_len;
2915         uint16_t nr_vec = 0;
2916         int err;
2917         static bool allocerr_warned;
2918
2919         if (unlikely(fill_vec_buf_packed(dev, vq,
2920                                          vq->last_avail_idx, desc_count,
2921                                          buf_vec, &nr_vec,
2922                                          buf_id, &buf_len,
2923                                          VHOST_ACCESS_RO) < 0))
2924                 return -1;
2925
2926         if (unlikely(virtio_dev_pktmbuf_prep(dev, pkts, buf_len))) {
2927                 if (!allocerr_warned) {
2928                         VHOST_LOG_DATA(ERR, "(%s) failed mbuf alloc of size %d from %s.\n",
2929                                 dev->ifname, buf_len, mbuf_pool->name);
2930                         allocerr_warned = true;
2931                 }
2932                 return -1;
2933         }
2934
2935         err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts,
2936                                 mbuf_pool, legacy_ol_flags);
2937         if (unlikely(err)) {
2938                 if (!allocerr_warned) {
2939                         VHOST_LOG_DATA(ERR, "(%s) failed to copy desc to mbuf.\n",
2940                                 dev->ifname);
2941                         allocerr_warned = true;
2942                 }
2943                 return -1;
2944         }
2945
2946         return 0;
2947 }
2948
2949 static __rte_always_inline int
2950 virtio_dev_tx_single_packed(struct virtio_net *dev,
2951                             struct vhost_virtqueue *vq,
2952                             struct rte_mempool *mbuf_pool,
2953                             struct rte_mbuf *pkts,
2954                             bool legacy_ol_flags)
2955 {
2956
2957         uint16_t buf_id, desc_count = 0;
2958         int ret;
2959
2960         ret = vhost_dequeue_single_packed(dev, vq, mbuf_pool, pkts, &buf_id,
2961                                         &desc_count, legacy_ol_flags);
2962
2963         if (likely(desc_count > 0)) {
2964                 if (virtio_net_is_inorder(dev))
2965                         vhost_shadow_dequeue_single_packed_inorder(vq, buf_id,
2966                                                                    desc_count);
2967                 else
2968                         vhost_shadow_dequeue_single_packed(vq, buf_id,
2969                                         desc_count);
2970
2971                 vq_inc_last_avail_packed(vq, desc_count);
2972         }
2973
2974         return ret;
2975 }
2976
2977 __rte_always_inline
2978 static uint16_t
2979 virtio_dev_tx_packed(struct virtio_net *dev,
2980                      struct vhost_virtqueue *__rte_restrict vq,
2981                      struct rte_mempool *mbuf_pool,
2982                      struct rte_mbuf **__rte_restrict pkts,
2983                      uint32_t count,
2984                      bool legacy_ol_flags)
2985 {
2986         uint32_t pkt_idx = 0;
2987
2988         if (rte_pktmbuf_alloc_bulk(mbuf_pool, pkts, count))
2989                 return 0;
2990
2991         do {
2992                 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
2993
2994                 if (count - pkt_idx >= PACKED_BATCH_SIZE) {
2995                         if (!virtio_dev_tx_batch_packed(dev, vq,
2996                                                         &pkts[pkt_idx],
2997                                                         legacy_ol_flags)) {
2998                                 pkt_idx += PACKED_BATCH_SIZE;
2999                                 continue;
3000                         }
3001                 }
3002
3003                 if (virtio_dev_tx_single_packed(dev, vq, mbuf_pool,
3004                                                 pkts[pkt_idx],
3005                                                 legacy_ol_flags))
3006                         break;
3007                 pkt_idx++;
3008         } while (pkt_idx < count);
3009
3010         if (pkt_idx != count)
3011                 rte_pktmbuf_free_bulk(&pkts[pkt_idx], count - pkt_idx);
3012
3013         if (vq->shadow_used_idx) {
3014                 do_data_copy_dequeue(vq);
3015
3016                 vhost_flush_dequeue_shadow_packed(dev, vq);
3017                 vhost_vring_call_packed(dev, vq);
3018         }
3019
3020         return pkt_idx;
3021 }
3022
3023 __rte_noinline
3024 static uint16_t
3025 virtio_dev_tx_packed_legacy(struct virtio_net *dev,
3026         struct vhost_virtqueue *__rte_restrict vq, struct rte_mempool *mbuf_pool,
3027         struct rte_mbuf **__rte_restrict pkts, uint32_t count)
3028 {
3029         return virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count, true);
3030 }
3031
3032 __rte_noinline
3033 static uint16_t
3034 virtio_dev_tx_packed_compliant(struct virtio_net *dev,
3035         struct vhost_virtqueue *__rte_restrict vq, struct rte_mempool *mbuf_pool,
3036         struct rte_mbuf **__rte_restrict pkts, uint32_t count)
3037 {
3038         return virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count, false);
3039 }
3040
3041 uint16_t
3042 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
3043         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
3044 {
3045         struct virtio_net *dev;
3046         struct rte_mbuf *rarp_mbuf = NULL;
3047         struct vhost_virtqueue *vq;
3048         int16_t success = 1;
3049
3050         dev = get_device(vid);
3051         if (!dev)
3052                 return 0;
3053
3054         if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
3055                 VHOST_LOG_DATA(ERR, "(%s) %s: built-in vhost net backend is disabled.\n",
3056                                 dev->ifname, __func__);
3057                 return 0;
3058         }
3059
3060         if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
3061                 VHOST_LOG_DATA(ERR, "(%s) %s: invalid virtqueue idx %d.\n",
3062                                 dev->ifname, __func__, queue_id);
3063                 return 0;
3064         }
3065
3066         vq = dev->virtqueue[queue_id];
3067
3068         if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
3069                 return 0;
3070
3071         if (unlikely(!vq->enabled)) {
3072                 count = 0;
3073                 goto out_access_unlock;
3074         }
3075
3076         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
3077                 vhost_user_iotlb_rd_lock(vq);
3078
3079         if (unlikely(!vq->access_ok))
3080                 if (unlikely(vring_translate(dev, vq) < 0)) {
3081                         count = 0;
3082                         goto out;
3083                 }
3084
3085         /*
3086          * Construct a RARP broadcast packet, and inject it to the "pkts"
3087          * array, to looks like that guest actually send such packet.
3088          *
3089          * Check user_send_rarp() for more information.
3090          *
3091          * broadcast_rarp shares a cacheline in the virtio_net structure
3092          * with some fields that are accessed during enqueue and
3093          * __atomic_compare_exchange_n causes a write if performed compare
3094          * and exchange. This could result in false sharing between enqueue
3095          * and dequeue.
3096          *
3097          * Prevent unnecessary false sharing by reading broadcast_rarp first
3098          * and only performing compare and exchange if the read indicates it
3099          * is likely to be set.
3100          */
3101         if (unlikely(__atomic_load_n(&dev->broadcast_rarp, __ATOMIC_ACQUIRE) &&
3102                         __atomic_compare_exchange_n(&dev->broadcast_rarp,
3103                         &success, 0, 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED))) {
3104
3105                 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
3106                 if (rarp_mbuf == NULL) {
3107                         VHOST_LOG_DATA(ERR, "(%s) failed to make RARP packet.\n", dev->ifname);
3108                         count = 0;
3109                         goto out;
3110                 }
3111                 /*
3112                  * Inject it to the head of "pkts" array, so that switch's mac
3113                  * learning table will get updated first.
3114                  */
3115                 pkts[0] = rarp_mbuf;
3116                 pkts++;
3117                 count -= 1;
3118         }
3119
3120         if (vq_is_packed(dev)) {
3121                 if (dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS)
3122                         count = virtio_dev_tx_packed_legacy(dev, vq, mbuf_pool, pkts, count);
3123                 else
3124                         count = virtio_dev_tx_packed_compliant(dev, vq, mbuf_pool, pkts, count);
3125         } else {
3126                 if (dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS)
3127                         count = virtio_dev_tx_split_legacy(dev, vq, mbuf_pool, pkts, count);
3128                 else
3129                         count = virtio_dev_tx_split_compliant(dev, vq, mbuf_pool, pkts, count);
3130         }
3131
3132 out:
3133         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
3134                 vhost_user_iotlb_rd_unlock(vq);
3135
3136 out_access_unlock:
3137         rte_spinlock_unlock(&vq->access_lock);
3138
3139         if (unlikely(rarp_mbuf != NULL))
3140                 count += 1;
3141
3142         return count;
3143 }