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