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