vhost: prepare sync for mbuf to descriptor refactoring
[dpdk.git] / lib / vhost / virtio_net.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2016 Intel Corporation
3  */
4
5 #include <stdint.h>
6 #include <stdbool.h>
7 #include <linux/virtio_net.h>
8
9 #include <rte_mbuf.h>
10 #include <rte_memcpy.h>
11 #include <rte_net.h>
12 #include <rte_ether.h>
13 #include <rte_ip.h>
14 #include <rte_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 async_iter_initialize(struct vhost_async *async)
796 {
797         struct rte_vhost_iov_iter *iter;
798
799         if (unlikely(async->iovec_idx >= VHOST_MAX_ASYNC_VEC)) {
800                 VHOST_LOG_DATA(ERR, "no more async iovec available\n");
801                 return -1;
802         }
803
804         iter = async->iov_iter + async->iter_idx;
805         iter->iov = async->iovec + async->iovec_idx;
806         iter->nr_segs = 0;
807
808         return 0;
809 }
810
811 static __rte_always_inline int
812 async_iter_add_iovec(struct vhost_async *async, void *src, void *dst, size_t len)
813 {
814         struct rte_vhost_iov_iter *iter;
815         struct rte_vhost_iovec *iovec;
816
817         if (unlikely(async->iovec_idx >= VHOST_MAX_ASYNC_VEC)) {
818                 static bool vhost_max_async_vec_log;
819
820                 if (!vhost_max_async_vec_log) {
821                         VHOST_LOG_DATA(ERR, "no more async iovec available\n");
822                         vhost_max_async_vec_log = true;
823                 }
824
825                 return -1;
826         }
827
828         iter = async->iov_iter + async->iter_idx;
829         iovec = async->iovec + async->iovec_idx;
830
831         iovec->src_addr = src;
832         iovec->dst_addr = dst;
833         iovec->len = len;
834
835         iter->nr_segs++;
836         async->iovec_idx++;
837
838         return 0;
839 }
840
841 static __rte_always_inline void
842 async_iter_finalize(struct vhost_async *async)
843 {
844         async->iter_idx++;
845 }
846
847 static __rte_always_inline void
848 async_iter_cancel(struct vhost_async *async)
849 {
850         struct rte_vhost_iov_iter *iter;
851
852         iter = async->iov_iter + async->iter_idx;
853         async->iovec_idx -= iter->nr_segs;
854         iter->nr_segs = 0;
855         iter->iov = NULL;
856 }
857
858 static __rte_always_inline void
859 async_iter_reset(struct vhost_async *async)
860 {
861         async->iter_idx = 0;
862         async->iovec_idx = 0;
863 }
864
865 static __rte_always_inline int
866 async_mbuf_to_desc_seg(struct virtio_net *dev, struct vhost_virtqueue *vq,
867                 struct rte_mbuf *m, uint32_t mbuf_offset,
868                 uint64_t buf_iova, uint32_t cpy_len)
869 {
870         struct vhost_async *async = vq->async;
871         uint64_t mapped_len;
872         uint32_t buf_offset = 0;
873         void *hpa;
874
875         while (cpy_len) {
876                 hpa = (void *)(uintptr_t)gpa_to_first_hpa(dev,
877                                 buf_iova + buf_offset, cpy_len, &mapped_len);
878                 if (unlikely(!hpa)) {
879                         VHOST_LOG_DATA(ERR, "(%d) %s: failed to get hpa.\n", dev->vid, __func__);
880                         return -1;
881                 }
882
883                 if (unlikely(async_iter_add_iovec(async,
884                                                 (void *)(uintptr_t)rte_pktmbuf_iova_offset(m,
885                                                         mbuf_offset),
886                                                 hpa, (size_t)mapped_len)))
887                         return -1;
888
889                 cpy_len -= (uint32_t)mapped_len;
890                 mbuf_offset += (uint32_t)mapped_len;
891                 buf_offset += (uint32_t)mapped_len;
892         }
893
894         return 0;
895 }
896
897 static __rte_always_inline void
898 sync_mbuf_to_desc_seg(struct virtio_net *dev, struct vhost_virtqueue *vq,
899                 struct rte_mbuf *m, uint32_t mbuf_offset,
900                 uint64_t buf_addr, uint64_t buf_iova, uint32_t cpy_len)
901 {
902         struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
903
904         if (likely(cpy_len > MAX_BATCH_LEN || vq->batch_copy_nb_elems >= vq->size)) {
905                 rte_memcpy((void *)((uintptr_t)(buf_addr)),
906                                 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
907                                 cpy_len);
908                 vhost_log_cache_write_iova(dev, vq, buf_iova, cpy_len);
909                 PRINT_PACKET(dev, (uintptr_t)(buf_addr), cpy_len, 0);
910         } else {
911                 batch_copy[vq->batch_copy_nb_elems].dst =
912                         (void *)((uintptr_t)(buf_addr));
913                 batch_copy[vq->batch_copy_nb_elems].src =
914                         rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
915                 batch_copy[vq->batch_copy_nb_elems].log_addr = buf_iova;
916                 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
917                 vq->batch_copy_nb_elems++;
918         }
919 }
920
921 static __rte_always_inline int
922 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
923                             struct rte_mbuf *m, struct buf_vector *buf_vec,
924                             uint16_t nr_vec, uint16_t num_buffers)
925 {
926         uint32_t vec_idx = 0;
927         uint32_t mbuf_offset, mbuf_avail;
928         uint32_t buf_offset, buf_avail;
929         uint64_t buf_addr, buf_iova, buf_len;
930         uint32_t cpy_len;
931         uint64_t hdr_addr;
932         struct rte_mbuf *hdr_mbuf;
933         struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
934
935         if (unlikely(m == NULL))
936                 return -1;
937
938         buf_addr = buf_vec[vec_idx].buf_addr;
939         buf_iova = buf_vec[vec_idx].buf_iova;
940         buf_len = buf_vec[vec_idx].buf_len;
941
942         if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1))
943                 return -1;
944
945         hdr_mbuf = m;
946         hdr_addr = buf_addr;
947         if (unlikely(buf_len < dev->vhost_hlen)) {
948                 memset(&tmp_hdr, 0, sizeof(struct virtio_net_hdr_mrg_rxbuf));
949                 hdr = &tmp_hdr;
950         } else
951                 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
952
953         VHOST_LOG_DATA(DEBUG, "(%d) RX: num merge buffers %d\n",
954                 dev->vid, num_buffers);
955
956         if (unlikely(buf_len < dev->vhost_hlen)) {
957                 buf_offset = dev->vhost_hlen - buf_len;
958                 vec_idx++;
959                 buf_addr = buf_vec[vec_idx].buf_addr;
960                 buf_iova = buf_vec[vec_idx].buf_iova;
961                 buf_len = buf_vec[vec_idx].buf_len;
962                 buf_avail = buf_len - buf_offset;
963         } else {
964                 buf_offset = dev->vhost_hlen;
965                 buf_avail = buf_len - dev->vhost_hlen;
966         }
967
968         mbuf_avail  = rte_pktmbuf_data_len(m);
969         mbuf_offset = 0;
970         while (mbuf_avail != 0 || m->next != NULL) {
971                 /* done with current buf, get the next one */
972                 if (buf_avail == 0) {
973                         vec_idx++;
974                         if (unlikely(vec_idx >= nr_vec))
975                                 goto error;
976
977                         buf_addr = buf_vec[vec_idx].buf_addr;
978                         buf_iova = buf_vec[vec_idx].buf_iova;
979                         buf_len = buf_vec[vec_idx].buf_len;
980
981                         buf_offset = 0;
982                         buf_avail  = buf_len;
983                 }
984
985                 /* done with current mbuf, get the next one */
986                 if (mbuf_avail == 0) {
987                         m = m->next;
988
989                         mbuf_offset = 0;
990                         mbuf_avail  = rte_pktmbuf_data_len(m);
991                 }
992
993                 if (hdr_addr) {
994                         virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
995                         if (rxvq_is_mergeable(dev))
996                                 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
997                                                 num_buffers);
998
999                         if (unlikely(hdr == &tmp_hdr)) {
1000                                 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
1001                         } else {
1002                                 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
1003                                                 dev->vhost_hlen, 0);
1004                                 vhost_log_cache_write_iova(dev, vq,
1005                                                 buf_vec[0].buf_iova,
1006                                                 dev->vhost_hlen);
1007                         }
1008
1009                         hdr_addr = 0;
1010                 }
1011
1012                 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1013
1014                 sync_mbuf_to_desc_seg(dev, vq, m, mbuf_offset,
1015                                 buf_addr + buf_offset,
1016                                 buf_iova + buf_offset, cpy_len);
1017
1018                 mbuf_avail  -= cpy_len;
1019                 mbuf_offset += cpy_len;
1020                 buf_avail  -= cpy_len;
1021                 buf_offset += cpy_len;
1022         }
1023
1024         return 0;
1025 error:
1026         return -1;
1027 }
1028
1029 static __rte_always_inline int
1030 async_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
1031                         struct rte_mbuf *m, struct buf_vector *buf_vec,
1032                         uint16_t nr_vec, uint16_t num_buffers)
1033 {
1034         struct vhost_async *async = vq->async;
1035         struct rte_mbuf *hdr_mbuf;
1036         struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
1037         uint64_t buf_addr, buf_iova;
1038         uint64_t hdr_addr;
1039         uint32_t vec_idx = 0;
1040         uint32_t mbuf_offset, mbuf_avail;
1041         uint32_t buf_offset, buf_avail;
1042         uint32_t cpy_len, buf_len;
1043
1044         if (unlikely(m == NULL))
1045                 return -1;
1046
1047         buf_addr = buf_vec[vec_idx].buf_addr;
1048         buf_iova = buf_vec[vec_idx].buf_iova;
1049         buf_len = buf_vec[vec_idx].buf_len;
1050
1051         if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1))
1052                 return -1;
1053
1054         hdr_mbuf = m;
1055         hdr_addr = buf_addr;
1056         if (unlikely(buf_len < dev->vhost_hlen)) {
1057                 memset(&tmp_hdr, 0, sizeof(struct virtio_net_hdr_mrg_rxbuf));
1058                 hdr = &tmp_hdr;
1059         } else
1060                 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
1061
1062         VHOST_LOG_DATA(DEBUG, "(%d) RX: num merge buffers %d\n",
1063                 dev->vid, num_buffers);
1064
1065         if (unlikely(buf_len < dev->vhost_hlen)) {
1066                 buf_offset = dev->vhost_hlen - buf_len;
1067                 vec_idx++;
1068                 buf_addr = buf_vec[vec_idx].buf_addr;
1069                 buf_iova = buf_vec[vec_idx].buf_iova;
1070                 buf_len = buf_vec[vec_idx].buf_len;
1071                 buf_avail = buf_len - buf_offset;
1072         } else {
1073                 buf_offset = dev->vhost_hlen;
1074                 buf_avail = buf_len - dev->vhost_hlen;
1075         }
1076
1077         mbuf_avail  = rte_pktmbuf_data_len(m);
1078         mbuf_offset = 0;
1079
1080         if (async_iter_initialize(async))
1081                 return -1;
1082
1083         while (mbuf_avail != 0 || m->next != NULL) {
1084                 /* done with current buf, get the next one */
1085                 if (buf_avail == 0) {
1086                         vec_idx++;
1087                         if (unlikely(vec_idx >= nr_vec))
1088                                 goto error;
1089
1090                         buf_addr = buf_vec[vec_idx].buf_addr;
1091                         buf_iova = buf_vec[vec_idx].buf_iova;
1092                         buf_len = buf_vec[vec_idx].buf_len;
1093
1094                         buf_offset = 0;
1095                         buf_avail = buf_len;
1096                 }
1097
1098                 /* done with current mbuf, get the next one */
1099                 if (mbuf_avail == 0) {
1100                         m = m->next;
1101
1102                         mbuf_offset = 0;
1103                         mbuf_avail = rte_pktmbuf_data_len(m);
1104                 }
1105
1106                 if (hdr_addr) {
1107                         virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
1108                         if (rxvq_is_mergeable(dev))
1109                                 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
1110                                                 num_buffers);
1111
1112                         if (unlikely(hdr == &tmp_hdr)) {
1113                                 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
1114                         } else {
1115                                 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
1116                                                 dev->vhost_hlen, 0);
1117                                 vhost_log_cache_write_iova(dev, vq,
1118                                                 buf_vec[0].buf_iova,
1119                                                 dev->vhost_hlen);
1120                         }
1121
1122                         hdr_addr = 0;
1123                 }
1124
1125                 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1126
1127                 if (async_mbuf_to_desc_seg(dev, vq, m, mbuf_offset,
1128                                         buf_iova + buf_offset, cpy_len) < 0) {
1129                         goto error;
1130                 }
1131
1132                 mbuf_avail  -= cpy_len;
1133                 mbuf_offset += cpy_len;
1134                 buf_avail  -= cpy_len;
1135                 buf_offset += cpy_len;
1136         }
1137
1138         async_iter_finalize(async);
1139
1140         return 0;
1141 error:
1142         async_iter_cancel(async);
1143
1144         return -1;
1145 }
1146
1147 static __rte_always_inline int
1148 vhost_enqueue_single_packed(struct virtio_net *dev,
1149                             struct vhost_virtqueue *vq,
1150                             struct rte_mbuf *pkt,
1151                             struct buf_vector *buf_vec,
1152                             uint16_t *nr_descs)
1153 {
1154         uint16_t nr_vec = 0;
1155         uint16_t avail_idx = vq->last_avail_idx;
1156         uint16_t max_tries, tries = 0;
1157         uint16_t buf_id = 0;
1158         uint32_t len = 0;
1159         uint16_t desc_count;
1160         uint32_t size = pkt->pkt_len + sizeof(struct virtio_net_hdr_mrg_rxbuf);
1161         uint16_t num_buffers = 0;
1162         uint32_t buffer_len[vq->size];
1163         uint16_t buffer_buf_id[vq->size];
1164         uint16_t buffer_desc_count[vq->size];
1165
1166         if (rxvq_is_mergeable(dev))
1167                 max_tries = vq->size - 1;
1168         else
1169                 max_tries = 1;
1170
1171         while (size > 0) {
1172                 /*
1173                  * if we tried all available ring items, and still
1174                  * can't get enough buf, it means something abnormal
1175                  * happened.
1176                  */
1177                 if (unlikely(++tries > max_tries))
1178                         return -1;
1179
1180                 if (unlikely(fill_vec_buf_packed(dev, vq,
1181                                                 avail_idx, &desc_count,
1182                                                 buf_vec, &nr_vec,
1183                                                 &buf_id, &len,
1184                                                 VHOST_ACCESS_RW) < 0))
1185                         return -1;
1186
1187                 len = RTE_MIN(len, size);
1188                 size -= len;
1189
1190                 buffer_len[num_buffers] = len;
1191                 buffer_buf_id[num_buffers] = buf_id;
1192                 buffer_desc_count[num_buffers] = desc_count;
1193                 num_buffers += 1;
1194
1195                 *nr_descs += desc_count;
1196                 avail_idx += desc_count;
1197                 if (avail_idx >= vq->size)
1198                         avail_idx -= vq->size;
1199         }
1200
1201         if (copy_mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec, num_buffers) < 0)
1202                 return -1;
1203
1204         vhost_shadow_enqueue_single_packed(dev, vq, buffer_len, buffer_buf_id,
1205                                            buffer_desc_count, num_buffers);
1206
1207         return 0;
1208 }
1209
1210 static __rte_noinline uint32_t
1211 virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1212         struct rte_mbuf **pkts, uint32_t count)
1213 {
1214         uint32_t pkt_idx = 0;
1215         uint16_t num_buffers;
1216         struct buf_vector buf_vec[BUF_VECTOR_MAX];
1217         uint16_t avail_head;
1218
1219         /*
1220          * The ordering between avail index and
1221          * desc reads needs to be enforced.
1222          */
1223         avail_head = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE);
1224
1225         rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1226
1227         for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1228                 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1229                 uint16_t nr_vec = 0;
1230
1231                 if (unlikely(reserve_avail_buf_split(dev, vq,
1232                                                 pkt_len, buf_vec, &num_buffers,
1233                                                 avail_head, &nr_vec) < 0)) {
1234                         VHOST_LOG_DATA(DEBUG,
1235                                 "(%d) failed to get enough desc from vring\n",
1236                                 dev->vid);
1237                         vq->shadow_used_idx -= num_buffers;
1238                         break;
1239                 }
1240
1241                 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1242                         dev->vid, vq->last_avail_idx,
1243                         vq->last_avail_idx + num_buffers);
1244
1245                 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
1246                                                 buf_vec, nr_vec,
1247                                                 num_buffers) < 0) {
1248                         vq->shadow_used_idx -= num_buffers;
1249                         break;
1250                 }
1251
1252                 vq->last_avail_idx += num_buffers;
1253         }
1254
1255         do_data_copy_enqueue(dev, vq);
1256
1257         if (likely(vq->shadow_used_idx)) {
1258                 flush_shadow_used_ring_split(dev, vq);
1259                 vhost_vring_call_split(dev, vq);
1260         }
1261
1262         return pkt_idx;
1263 }
1264
1265 static __rte_always_inline int
1266 virtio_dev_rx_sync_batch_check(struct virtio_net *dev,
1267                            struct vhost_virtqueue *vq,
1268                            struct rte_mbuf **pkts,
1269                            uint64_t *desc_addrs,
1270                            uint64_t *lens)
1271 {
1272         bool wrap_counter = vq->avail_wrap_counter;
1273         struct vring_packed_desc *descs = vq->desc_packed;
1274         uint16_t avail_idx = vq->last_avail_idx;
1275         uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1276         uint16_t i;
1277
1278         if (unlikely(avail_idx & PACKED_BATCH_MASK))
1279                 return -1;
1280
1281         if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
1282                 return -1;
1283
1284         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1285                 if (unlikely(pkts[i]->next != NULL))
1286                         return -1;
1287                 if (unlikely(!desc_is_avail(&descs[avail_idx + i],
1288                                             wrap_counter)))
1289                         return -1;
1290         }
1291
1292         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1293                 lens[i] = descs[avail_idx + i].len;
1294
1295         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1296                 if (unlikely(pkts[i]->pkt_len > (lens[i] - buf_offset)))
1297                         return -1;
1298         }
1299
1300         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1301                 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
1302                                                   descs[avail_idx + i].addr,
1303                                                   &lens[i],
1304                                                   VHOST_ACCESS_RW);
1305
1306         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1307                 if (unlikely(!desc_addrs[i]))
1308                         return -1;
1309                 if (unlikely(lens[i] != descs[avail_idx + i].len))
1310                         return -1;
1311         }
1312
1313         return 0;
1314 }
1315
1316 static __rte_always_inline void
1317 virtio_dev_rx_batch_packed_copy(struct virtio_net *dev,
1318                            struct vhost_virtqueue *vq,
1319                            struct rte_mbuf **pkts,
1320                            uint64_t *desc_addrs,
1321                            uint64_t *lens)
1322 {
1323         uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1324         struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE];
1325         struct vring_packed_desc *descs = vq->desc_packed;
1326         uint16_t avail_idx = vq->last_avail_idx;
1327         uint16_t ids[PACKED_BATCH_SIZE];
1328         uint16_t i;
1329
1330         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1331                 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
1332                 hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *)
1333                                         (uintptr_t)desc_addrs[i];
1334                 lens[i] = pkts[i]->pkt_len +
1335                         sizeof(struct virtio_net_hdr_mrg_rxbuf);
1336         }
1337
1338         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1339                 virtio_enqueue_offload(pkts[i], &hdrs[i]->hdr);
1340
1341         vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
1342
1343         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1344                 rte_memcpy((void *)(uintptr_t)(desc_addrs[i] + buf_offset),
1345                            rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
1346                            pkts[i]->pkt_len);
1347         }
1348
1349         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1350                 vhost_log_cache_write_iova(dev, vq, descs[avail_idx + i].addr,
1351                                            lens[i]);
1352
1353         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1354                 ids[i] = descs[avail_idx + i].id;
1355
1356         vhost_flush_enqueue_batch_packed(dev, vq, lens, ids);
1357 }
1358
1359 static __rte_always_inline int
1360 virtio_dev_rx_sync_batch_packed(struct virtio_net *dev,
1361                            struct vhost_virtqueue *vq,
1362                            struct rte_mbuf **pkts)
1363 {
1364         uint64_t desc_addrs[PACKED_BATCH_SIZE];
1365         uint64_t lens[PACKED_BATCH_SIZE];
1366
1367         if (virtio_dev_rx_sync_batch_check(dev, vq, pkts, desc_addrs, lens) == -1)
1368                 return -1;
1369
1370         if (vq->shadow_used_idx) {
1371                 do_data_copy_enqueue(dev, vq);
1372                 vhost_flush_enqueue_shadow_packed(dev, vq);
1373         }
1374
1375         virtio_dev_rx_batch_packed_copy(dev, vq, pkts, desc_addrs, lens);
1376
1377         return 0;
1378 }
1379
1380 static __rte_always_inline int16_t
1381 virtio_dev_rx_single_packed(struct virtio_net *dev,
1382                             struct vhost_virtqueue *vq,
1383                             struct rte_mbuf *pkt)
1384 {
1385         struct buf_vector buf_vec[BUF_VECTOR_MAX];
1386         uint16_t nr_descs = 0;
1387
1388         if (unlikely(vhost_enqueue_single_packed(dev, vq, pkt, buf_vec,
1389                                                  &nr_descs) < 0)) {
1390                 VHOST_LOG_DATA(DEBUG,
1391                                 "(%d) failed to get enough desc from vring\n",
1392                                 dev->vid);
1393                 return -1;
1394         }
1395
1396         VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1397                         dev->vid, vq->last_avail_idx,
1398                         vq->last_avail_idx + nr_descs);
1399
1400         vq_inc_last_avail_packed(vq, nr_descs);
1401
1402         return 0;
1403 }
1404
1405 static __rte_noinline uint32_t
1406 virtio_dev_rx_packed(struct virtio_net *dev,
1407                      struct vhost_virtqueue *__rte_restrict vq,
1408                      struct rte_mbuf **__rte_restrict pkts,
1409                      uint32_t count)
1410 {
1411         uint32_t pkt_idx = 0;
1412
1413         do {
1414                 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1415
1416                 if (count - pkt_idx >= PACKED_BATCH_SIZE) {
1417                         if (!virtio_dev_rx_sync_batch_packed(dev, vq,
1418                                                         &pkts[pkt_idx])) {
1419                                 pkt_idx += PACKED_BATCH_SIZE;
1420                                 continue;
1421                         }
1422                 }
1423
1424                 if (virtio_dev_rx_single_packed(dev, vq, pkts[pkt_idx]))
1425                         break;
1426                 pkt_idx++;
1427
1428         } while (pkt_idx < count);
1429
1430         if (vq->shadow_used_idx) {
1431                 do_data_copy_enqueue(dev, vq);
1432                 vhost_flush_enqueue_shadow_packed(dev, vq);
1433         }
1434
1435         if (pkt_idx)
1436                 vhost_vring_call_packed(dev, vq);
1437
1438         return pkt_idx;
1439 }
1440
1441 static __rte_always_inline uint32_t
1442 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
1443         struct rte_mbuf **pkts, uint32_t count)
1444 {
1445         struct vhost_virtqueue *vq;
1446         uint32_t nb_tx = 0;
1447
1448         VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
1449         if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1450                 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
1451                         dev->vid, __func__, queue_id);
1452                 return 0;
1453         }
1454
1455         vq = dev->virtqueue[queue_id];
1456
1457         rte_spinlock_lock(&vq->access_lock);
1458
1459         if (unlikely(!vq->enabled))
1460                 goto out_access_unlock;
1461
1462         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1463                 vhost_user_iotlb_rd_lock(vq);
1464
1465         if (unlikely(!vq->access_ok))
1466                 if (unlikely(vring_translate(dev, vq) < 0))
1467                         goto out;
1468
1469         count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
1470         if (count == 0)
1471                 goto out;
1472
1473         if (vq_is_packed(dev))
1474                 nb_tx = virtio_dev_rx_packed(dev, vq, pkts, count);
1475         else
1476                 nb_tx = virtio_dev_rx_split(dev, vq, pkts, count);
1477
1478 out:
1479         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1480                 vhost_user_iotlb_rd_unlock(vq);
1481
1482 out_access_unlock:
1483         rte_spinlock_unlock(&vq->access_lock);
1484
1485         return nb_tx;
1486 }
1487
1488 uint16_t
1489 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
1490         struct rte_mbuf **__rte_restrict pkts, uint16_t count)
1491 {
1492         struct virtio_net *dev = get_device(vid);
1493
1494         if (!dev)
1495                 return 0;
1496
1497         if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1498                 VHOST_LOG_DATA(ERR,
1499                         "(%d) %s: built-in vhost net backend is disabled.\n",
1500                         dev->vid, __func__);
1501                 return 0;
1502         }
1503
1504         return virtio_dev_rx(dev, queue_id, pkts, count);
1505 }
1506
1507 static __rte_always_inline uint16_t
1508 async_get_first_inflight_pkt_idx(struct vhost_virtqueue *vq)
1509 {
1510         struct vhost_async *async = vq->async;
1511
1512         if (async->pkts_idx >= async->pkts_inflight_n)
1513                 return async->pkts_idx - async->pkts_inflight_n;
1514         else
1515                 return vq->size - async->pkts_inflight_n + async->pkts_idx;
1516 }
1517
1518 static __rte_always_inline void
1519 store_dma_desc_info_split(struct vring_used_elem *s_ring, struct vring_used_elem *d_ring,
1520                 uint16_t ring_size, uint16_t s_idx, uint16_t d_idx, uint16_t count)
1521 {
1522         size_t elem_size = sizeof(struct vring_used_elem);
1523
1524         if (d_idx + count <= ring_size) {
1525                 rte_memcpy(d_ring + d_idx, s_ring + s_idx, count * elem_size);
1526         } else {
1527                 uint16_t size = ring_size - d_idx;
1528
1529                 rte_memcpy(d_ring + d_idx, s_ring + s_idx, size * elem_size);
1530                 rte_memcpy(d_ring, s_ring + s_idx + size, (count - size) * elem_size);
1531         }
1532 }
1533
1534 static __rte_always_inline void
1535 store_dma_desc_info_packed(struct vring_used_elem_packed *s_ring,
1536                 struct vring_used_elem_packed *d_ring,
1537                 uint16_t ring_size, uint16_t s_idx, uint16_t d_idx, uint16_t count)
1538 {
1539         size_t elem_size = sizeof(struct vring_used_elem_packed);
1540
1541         if (d_idx + count <= ring_size) {
1542                 rte_memcpy(d_ring + d_idx, s_ring + s_idx, count * elem_size);
1543         } else {
1544                 uint16_t size = ring_size - d_idx;
1545
1546                 rte_memcpy(d_ring + d_idx, s_ring + s_idx, size * elem_size);
1547                 rte_memcpy(d_ring, s_ring + s_idx + size, (count - size) * elem_size);
1548         }
1549 }
1550
1551 static __rte_noinline uint32_t
1552 virtio_dev_rx_async_submit_split(struct virtio_net *dev,
1553         struct vhost_virtqueue *vq, uint16_t queue_id,
1554         struct rte_mbuf **pkts, uint32_t count)
1555 {
1556         struct buf_vector buf_vec[BUF_VECTOR_MAX];
1557         uint32_t pkt_idx = 0;
1558         uint16_t num_buffers;
1559         uint16_t avail_head;
1560
1561         struct vhost_async *async = vq->async;
1562         struct async_inflight_info *pkts_info = async->pkts_info;
1563         uint32_t pkt_err = 0;
1564         int32_t n_xfer;
1565         uint16_t slot_idx = 0;
1566
1567         /*
1568          * The ordering between avail index and desc reads need to be enforced.
1569          */
1570         avail_head = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE);
1571
1572         rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1573
1574         async_iter_reset(async);
1575
1576         for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1577                 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1578                 uint16_t nr_vec = 0;
1579
1580                 if (unlikely(reserve_avail_buf_split(dev, vq, pkt_len, buf_vec,
1581                                                 &num_buffers, avail_head, &nr_vec) < 0)) {
1582                         VHOST_LOG_DATA(DEBUG, "(%d) failed to get enough desc from vring\n",
1583                                         dev->vid);
1584                         vq->shadow_used_idx -= num_buffers;
1585                         break;
1586                 }
1587
1588                 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1589                         dev->vid, vq->last_avail_idx, vq->last_avail_idx + num_buffers);
1590
1591                 if (async_mbuf_to_desc(dev, vq, pkts[pkt_idx], buf_vec, nr_vec, num_buffers) < 0) {
1592                         vq->shadow_used_idx -= num_buffers;
1593                         break;
1594                 }
1595
1596                 slot_idx = (async->pkts_idx + pkt_idx) & (vq->size - 1);
1597                 pkts_info[slot_idx].descs = num_buffers;
1598                 pkts_info[slot_idx].mbuf = pkts[pkt_idx];
1599
1600                 vq->last_avail_idx += num_buffers;
1601         }
1602
1603         if (unlikely(pkt_idx == 0))
1604                 return 0;
1605
1606         n_xfer = async->ops.transfer_data(dev->vid, queue_id, async->iov_iter, 0, pkt_idx);
1607         if (unlikely(n_xfer < 0)) {
1608                 VHOST_LOG_DATA(ERR, "(%d) %s: failed to transfer data for queue id %d.\n",
1609                                 dev->vid, __func__, queue_id);
1610                 n_xfer = 0;
1611         }
1612
1613         pkt_err = pkt_idx - n_xfer;
1614         if (unlikely(pkt_err)) {
1615                 uint16_t num_descs = 0;
1616
1617                 /* update number of completed packets */
1618                 pkt_idx = n_xfer;
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
1641                 async->pkts_idx += pkt_idx;
1642                 if (async->pkts_idx >= vq->size)
1643                         async->pkts_idx -= vq->size;
1644
1645                 async->pkts_inflight_n += pkt_idx;
1646                 vq->shadow_used_idx = 0;
1647         }
1648
1649         return pkt_idx;
1650 }
1651
1652 static __rte_always_inline void
1653 vhost_update_used_packed(struct vhost_virtqueue *vq,
1654                         struct vring_used_elem_packed *shadow_ring,
1655                         uint16_t count)
1656 {
1657         int i;
1658         uint16_t used_idx = vq->last_used_idx;
1659         uint16_t head_idx = vq->last_used_idx;
1660         uint16_t head_flags = 0;
1661
1662         if (count == 0)
1663                 return;
1664
1665         /* Split loop in two to save memory barriers */
1666         for (i = 0; i < count; i++) {
1667                 vq->desc_packed[used_idx].id = shadow_ring[i].id;
1668                 vq->desc_packed[used_idx].len = shadow_ring[i].len;
1669
1670                 used_idx += shadow_ring[i].count;
1671                 if (used_idx >= vq->size)
1672                         used_idx -= vq->size;
1673         }
1674
1675         /* The ordering for storing desc flags needs to be enforced. */
1676         rte_atomic_thread_fence(__ATOMIC_RELEASE);
1677
1678         for (i = 0; i < count; i++) {
1679                 uint16_t flags;
1680
1681                 if (vq->shadow_used_packed[i].len)
1682                         flags = VRING_DESC_F_WRITE;
1683                 else
1684                         flags = 0;
1685
1686                 if (vq->used_wrap_counter) {
1687                         flags |= VRING_DESC_F_USED;
1688                         flags |= VRING_DESC_F_AVAIL;
1689                 } else {
1690                         flags &= ~VRING_DESC_F_USED;
1691                         flags &= ~VRING_DESC_F_AVAIL;
1692                 }
1693
1694                 if (i > 0) {
1695                         vq->desc_packed[vq->last_used_idx].flags = flags;
1696                 } else {
1697                         head_idx = vq->last_used_idx;
1698                         head_flags = flags;
1699                 }
1700
1701                 vq_inc_last_used_packed(vq, shadow_ring[i].count);
1702         }
1703
1704         vq->desc_packed[head_idx].flags = head_flags;
1705 }
1706
1707 static __rte_always_inline int
1708 vhost_enqueue_async_packed(struct virtio_net *dev,
1709                             struct vhost_virtqueue *vq,
1710                             struct rte_mbuf *pkt,
1711                             struct buf_vector *buf_vec,
1712                             uint16_t *nr_descs,
1713                             uint16_t *nr_buffers)
1714 {
1715         uint16_t nr_vec = 0;
1716         uint16_t avail_idx = vq->last_avail_idx;
1717         uint16_t max_tries, tries = 0;
1718         uint16_t buf_id = 0;
1719         uint32_t len = 0;
1720         uint16_t desc_count = 0;
1721         uint32_t size = pkt->pkt_len + sizeof(struct virtio_net_hdr_mrg_rxbuf);
1722         uint32_t buffer_len[vq->size];
1723         uint16_t buffer_buf_id[vq->size];
1724         uint16_t buffer_desc_count[vq->size];
1725
1726         if (rxvq_is_mergeable(dev))
1727                 max_tries = vq->size - 1;
1728         else
1729                 max_tries = 1;
1730
1731         while (size > 0) {
1732                 /*
1733                  * if we tried all available ring items, and still
1734                  * can't get enough buf, it means something abnormal
1735                  * happened.
1736                  */
1737                 if (unlikely(++tries > max_tries))
1738                         return -1;
1739
1740                 if (unlikely(fill_vec_buf_packed(dev, vq,
1741                                                 avail_idx, &desc_count,
1742                                                 buf_vec, &nr_vec,
1743                                                 &buf_id, &len,
1744                                                 VHOST_ACCESS_RW) < 0))
1745                         return -1;
1746
1747                 len = RTE_MIN(len, size);
1748                 size -= len;
1749
1750                 buffer_len[*nr_buffers] = len;
1751                 buffer_buf_id[*nr_buffers] = buf_id;
1752                 buffer_desc_count[*nr_buffers] = desc_count;
1753                 *nr_buffers += 1;
1754                 *nr_descs += desc_count;
1755                 avail_idx += desc_count;
1756                 if (avail_idx >= vq->size)
1757                         avail_idx -= vq->size;
1758         }
1759
1760         if (unlikely(async_mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec,
1761                                         *nr_buffers) < 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 {
1773         struct buf_vector buf_vec[BUF_VECTOR_MAX];
1774
1775         if (unlikely(vhost_enqueue_async_packed(dev, vq, pkt, buf_vec,
1776                                         nr_descs, nr_buffers) < 0)) {
1777                 VHOST_LOG_DATA(DEBUG, "(%d) failed to get enough desc from vring\n", dev->vid);
1778                 return -1;
1779         }
1780
1781         VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1782                         dev->vid, vq->last_avail_idx, vq->last_avail_idx + *nr_descs);
1783
1784         return 0;
1785 }
1786
1787 static __rte_always_inline void
1788 dma_error_handler_packed(struct vhost_virtqueue *vq, uint16_t slot_idx,
1789                         uint32_t nr_err, uint32_t *pkt_idx)
1790 {
1791         uint16_t descs_err = 0;
1792         uint16_t buffers_err = 0;
1793         struct async_inflight_info *pkts_info = vq->async->pkts_info;
1794
1795         *pkt_idx -= nr_err;
1796         /* calculate the sum of buffers and descs of DMA-error packets. */
1797         while (nr_err-- > 0) {
1798                 descs_err += pkts_info[slot_idx % vq->size].descs;
1799                 buffers_err += pkts_info[slot_idx % vq->size].nr_buffers;
1800                 slot_idx--;
1801         }
1802
1803         if (vq->last_avail_idx >= descs_err) {
1804                 vq->last_avail_idx -= descs_err;
1805         } else {
1806                 vq->last_avail_idx = vq->last_avail_idx + vq->size - descs_err;
1807                 vq->avail_wrap_counter ^= 1;
1808         }
1809
1810         vq->shadow_used_idx -= buffers_err;
1811 }
1812
1813 static __rte_noinline uint32_t
1814 virtio_dev_rx_async_submit_packed(struct virtio_net *dev,
1815         struct vhost_virtqueue *vq, uint16_t queue_id,
1816         struct rte_mbuf **pkts, uint32_t count)
1817 {
1818         uint32_t pkt_idx = 0;
1819         uint32_t remained = count;
1820         int32_t n_xfer;
1821         uint16_t num_buffers;
1822         uint16_t num_descs;
1823
1824         struct vhost_async *async = vq->async;
1825         struct async_inflight_info *pkts_info = async->pkts_info;
1826         uint32_t pkt_err = 0;
1827         uint16_t slot_idx = 0;
1828
1829         do {
1830                 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1831
1832                 num_buffers = 0;
1833                 num_descs = 0;
1834                 if (unlikely(virtio_dev_rx_async_packed(dev, vq, pkts[pkt_idx],
1835                                                 &num_descs, &num_buffers) < 0))
1836                         break;
1837
1838                 slot_idx = (async->pkts_idx + pkt_idx) % vq->size;
1839
1840                 pkts_info[slot_idx].descs = num_descs;
1841                 pkts_info[slot_idx].nr_buffers = num_buffers;
1842                 pkts_info[slot_idx].mbuf = pkts[pkt_idx];
1843
1844                 pkt_idx++;
1845                 remained--;
1846                 vq_inc_last_avail_packed(vq, num_descs);
1847         } while (pkt_idx < count);
1848
1849         if (unlikely(pkt_idx == 0))
1850                 return 0;
1851
1852         n_xfer = async->ops.transfer_data(dev->vid, queue_id, async->iov_iter, 0, pkt_idx);
1853         if (unlikely(n_xfer < 0)) {
1854                 VHOST_LOG_DATA(ERR, "(%d) %s: failed to transfer data for queue id %d.\n",
1855                                 dev->vid, __func__, queue_id);
1856                 n_xfer = 0;
1857         }
1858
1859         pkt_err = pkt_idx - n_xfer;
1860
1861         async_iter_reset(async);
1862
1863         if (unlikely(pkt_err))
1864                 dma_error_handler_packed(vq, slot_idx, pkt_err, &pkt_idx);
1865
1866         if (likely(vq->shadow_used_idx)) {
1867                 /* keep used descriptors. */
1868                 store_dma_desc_info_packed(vq->shadow_used_packed, async->buffers_packed,
1869                                         vq->size, 0, async->buffer_idx_packed,
1870                                         vq->shadow_used_idx);
1871
1872                 async->buffer_idx_packed += vq->shadow_used_idx;
1873                 if (async->buffer_idx_packed >= vq->size)
1874                         async->buffer_idx_packed -= vq->size;
1875
1876                 async->pkts_idx += pkt_idx;
1877                 if (async->pkts_idx >= vq->size)
1878                         async->pkts_idx -= vq->size;
1879
1880                 vq->shadow_used_idx = 0;
1881                 async->pkts_inflight_n += pkt_idx;
1882         }
1883
1884         return pkt_idx;
1885 }
1886
1887 static __rte_always_inline void
1888 write_back_completed_descs_split(struct vhost_virtqueue *vq, uint16_t n_descs)
1889 {
1890         struct vhost_async *async = vq->async;
1891         uint16_t nr_left = n_descs;
1892         uint16_t nr_copy;
1893         uint16_t to, from;
1894
1895         do {
1896                 from = async->last_desc_idx_split & (vq->size - 1);
1897                 nr_copy = nr_left + from <= vq->size ? nr_left : vq->size - from;
1898                 to = vq->last_used_idx & (vq->size - 1);
1899
1900                 if (to + nr_copy <= vq->size) {
1901                         rte_memcpy(&vq->used->ring[to], &async->descs_split[from],
1902                                         nr_copy * sizeof(struct vring_used_elem));
1903                 } else {
1904                         uint16_t size = vq->size - to;
1905
1906                         rte_memcpy(&vq->used->ring[to], &async->descs_split[from],
1907                                         size * sizeof(struct vring_used_elem));
1908                         rte_memcpy(&vq->used->ring[0], &async->descs_split[from + size],
1909                                         (nr_copy - size) * sizeof(struct vring_used_elem));
1910                 }
1911
1912                 async->last_desc_idx_split += nr_copy;
1913                 vq->last_used_idx += nr_copy;
1914                 nr_left -= nr_copy;
1915         } while (nr_left > 0);
1916 }
1917
1918 static __rte_always_inline void
1919 write_back_completed_descs_packed(struct vhost_virtqueue *vq,
1920                                 uint16_t n_buffers)
1921 {
1922         struct vhost_async *async = vq->async;
1923         uint16_t nr_left = n_buffers;
1924         uint16_t from, to;
1925
1926         do {
1927                 from = async->last_buffer_idx_packed;
1928                 to = (from + nr_left) % vq->size;
1929                 if (to > from) {
1930                         vhost_update_used_packed(vq, async->buffers_packed + from, to - from);
1931                         async->last_buffer_idx_packed += nr_left;
1932                         nr_left = 0;
1933                 } else {
1934                         vhost_update_used_packed(vq, async->buffers_packed + from,
1935                                 vq->size - from);
1936                         async->last_buffer_idx_packed = 0;
1937                         nr_left -= vq->size - from;
1938                 }
1939         } while (nr_left > 0);
1940 }
1941
1942 static __rte_always_inline uint16_t
1943 vhost_poll_enqueue_completed(struct virtio_net *dev, uint16_t queue_id,
1944                 struct rte_mbuf **pkts, uint16_t count)
1945 {
1946         struct vhost_virtqueue *vq = dev->virtqueue[queue_id];
1947         struct vhost_async *async = vq->async;
1948         struct async_inflight_info *pkts_info = async->pkts_info;
1949         int32_t n_cpl;
1950         uint16_t n_descs = 0, n_buffers = 0;
1951         uint16_t start_idx, from, i;
1952
1953         n_cpl = async->ops.check_completed_copies(dev->vid, queue_id, 0, count);
1954         if (unlikely(n_cpl < 0)) {
1955                 VHOST_LOG_DATA(ERR, "(%d) %s: failed to check completed copies for queue id %d.\n",
1956                                 dev->vid, __func__, queue_id);
1957                 return 0;
1958         }
1959
1960         if (n_cpl == 0)
1961                 return 0;
1962
1963         start_idx = async_get_first_inflight_pkt_idx(vq);
1964
1965         for (i = 0; i < n_cpl; i++) {
1966                 from = (start_idx + i) % vq->size;
1967                 /* Only used with packed ring */
1968                 n_buffers += pkts_info[from].nr_buffers;
1969                 /* Only used with split ring */
1970                 n_descs += pkts_info[from].descs;
1971                 pkts[i] = pkts_info[from].mbuf;
1972         }
1973
1974         async->pkts_inflight_n -= n_cpl;
1975
1976         if (likely(vq->enabled && vq->access_ok)) {
1977                 if (vq_is_packed(dev)) {
1978                         write_back_completed_descs_packed(vq, n_buffers);
1979                         vhost_vring_call_packed(dev, vq);
1980                 } else {
1981                         write_back_completed_descs_split(vq, n_descs);
1982                         __atomic_add_fetch(&vq->used->idx, n_descs, __ATOMIC_RELEASE);
1983                         vhost_vring_call_split(dev, vq);
1984                 }
1985         } else {
1986                 if (vq_is_packed(dev)) {
1987                         async->last_buffer_idx_packed += n_buffers;
1988                         if (async->last_buffer_idx_packed >= vq->size)
1989                                 async->last_buffer_idx_packed -= vq->size;
1990                 } else {
1991                         async->last_desc_idx_split += n_descs;
1992                 }
1993         }
1994
1995         return n_cpl;
1996 }
1997
1998 uint16_t
1999 rte_vhost_poll_enqueue_completed(int vid, uint16_t queue_id,
2000                 struct rte_mbuf **pkts, uint16_t count)
2001 {
2002         struct virtio_net *dev = get_device(vid);
2003         struct vhost_virtqueue *vq;
2004         uint16_t n_pkts_cpl = 0;
2005
2006         if (unlikely(!dev))
2007                 return 0;
2008
2009         VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
2010         if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
2011                 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
2012                         dev->vid, __func__, queue_id);
2013                 return 0;
2014         }
2015
2016         vq = dev->virtqueue[queue_id];
2017
2018         if (unlikely(!vq->async)) {
2019                 VHOST_LOG_DATA(ERR, "(%d) %s: async not registered for queue id %d.\n",
2020                         dev->vid, __func__, queue_id);
2021                 return 0;
2022         }
2023
2024         rte_spinlock_lock(&vq->access_lock);
2025
2026         n_pkts_cpl = vhost_poll_enqueue_completed(dev, queue_id, pkts, count);
2027
2028         rte_spinlock_unlock(&vq->access_lock);
2029
2030         return n_pkts_cpl;
2031 }
2032
2033 uint16_t
2034 rte_vhost_clear_queue_thread_unsafe(int vid, uint16_t queue_id,
2035                 struct rte_mbuf **pkts, uint16_t count)
2036 {
2037         struct virtio_net *dev = get_device(vid);
2038         struct vhost_virtqueue *vq;
2039         uint16_t n_pkts_cpl = 0;
2040
2041         if (!dev)
2042                 return 0;
2043
2044         VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
2045         if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
2046                 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
2047                         dev->vid, __func__, queue_id);
2048                 return 0;
2049         }
2050
2051         vq = dev->virtqueue[queue_id];
2052
2053         if (unlikely(!vq->async)) {
2054                 VHOST_LOG_DATA(ERR, "(%d) %s: async not registered for queue id %d.\n",
2055                         dev->vid, __func__, queue_id);
2056                 return 0;
2057         }
2058
2059         n_pkts_cpl = vhost_poll_enqueue_completed(dev, queue_id, pkts, count);
2060
2061         return n_pkts_cpl;
2062 }
2063
2064 static __rte_always_inline uint32_t
2065 virtio_dev_rx_async_submit(struct virtio_net *dev, uint16_t queue_id,
2066         struct rte_mbuf **pkts, uint32_t count)
2067 {
2068         struct vhost_virtqueue *vq;
2069         uint32_t nb_tx = 0;
2070
2071         VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
2072         if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
2073                 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
2074                         dev->vid, __func__, queue_id);
2075                 return 0;
2076         }
2077
2078         vq = dev->virtqueue[queue_id];
2079
2080         rte_spinlock_lock(&vq->access_lock);
2081
2082         if (unlikely(!vq->enabled || !vq->async))
2083                 goto out_access_unlock;
2084
2085         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2086                 vhost_user_iotlb_rd_lock(vq);
2087
2088         if (unlikely(!vq->access_ok))
2089                 if (unlikely(vring_translate(dev, vq) < 0))
2090                         goto out;
2091
2092         count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
2093         if (count == 0)
2094                 goto out;
2095
2096         if (vq_is_packed(dev))
2097                 nb_tx = virtio_dev_rx_async_submit_packed(dev, vq, queue_id,
2098                                 pkts, count);
2099         else
2100                 nb_tx = virtio_dev_rx_async_submit_split(dev, vq, queue_id,
2101                                 pkts, count);
2102
2103 out:
2104         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2105                 vhost_user_iotlb_rd_unlock(vq);
2106
2107 out_access_unlock:
2108         rte_spinlock_unlock(&vq->access_lock);
2109
2110         return nb_tx;
2111 }
2112
2113 uint16_t
2114 rte_vhost_submit_enqueue_burst(int vid, uint16_t queue_id,
2115                 struct rte_mbuf **pkts, uint16_t count)
2116 {
2117         struct virtio_net *dev = get_device(vid);
2118
2119         if (!dev)
2120                 return 0;
2121
2122         if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
2123                 VHOST_LOG_DATA(ERR,
2124                         "(%d) %s: built-in vhost net backend is disabled.\n",
2125                         dev->vid, __func__);
2126                 return 0;
2127         }
2128
2129         return virtio_dev_rx_async_submit(dev, queue_id, pkts, count);
2130 }
2131
2132 static inline bool
2133 virtio_net_with_host_offload(struct virtio_net *dev)
2134 {
2135         if (dev->features &
2136                         ((1ULL << VIRTIO_NET_F_CSUM) |
2137                          (1ULL << VIRTIO_NET_F_HOST_ECN) |
2138                          (1ULL << VIRTIO_NET_F_HOST_TSO4) |
2139                          (1ULL << VIRTIO_NET_F_HOST_TSO6) |
2140                          (1ULL << VIRTIO_NET_F_HOST_UFO)))
2141                 return true;
2142
2143         return false;
2144 }
2145
2146 static int
2147 parse_headers(struct rte_mbuf *m, uint8_t *l4_proto)
2148 {
2149         struct rte_ipv4_hdr *ipv4_hdr;
2150         struct rte_ipv6_hdr *ipv6_hdr;
2151         struct rte_ether_hdr *eth_hdr;
2152         uint16_t ethertype;
2153         uint16_t data_len = rte_pktmbuf_data_len(m);
2154
2155         if (data_len < sizeof(struct rte_ether_hdr))
2156                 return -EINVAL;
2157
2158         eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
2159
2160         m->l2_len = sizeof(struct rte_ether_hdr);
2161         ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
2162
2163         if (ethertype == RTE_ETHER_TYPE_VLAN) {
2164                 if (data_len < sizeof(struct rte_ether_hdr) +
2165                                 sizeof(struct rte_vlan_hdr))
2166                         goto error;
2167
2168                 struct rte_vlan_hdr *vlan_hdr =
2169                         (struct rte_vlan_hdr *)(eth_hdr + 1);
2170
2171                 m->l2_len += sizeof(struct rte_vlan_hdr);
2172                 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
2173         }
2174
2175         switch (ethertype) {
2176         case RTE_ETHER_TYPE_IPV4:
2177                 if (data_len < m->l2_len + sizeof(struct rte_ipv4_hdr))
2178                         goto error;
2179                 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *,
2180                                 m->l2_len);
2181                 m->l3_len = rte_ipv4_hdr_len(ipv4_hdr);
2182                 if (data_len < m->l2_len + m->l3_len)
2183                         goto error;
2184                 m->ol_flags |= RTE_MBUF_F_TX_IPV4;
2185                 *l4_proto = ipv4_hdr->next_proto_id;
2186                 break;
2187         case RTE_ETHER_TYPE_IPV6:
2188                 if (data_len < m->l2_len + sizeof(struct rte_ipv6_hdr))
2189                         goto error;
2190                 ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv6_hdr *,
2191                                 m->l2_len);
2192                 m->l3_len = sizeof(struct rte_ipv6_hdr);
2193                 m->ol_flags |= RTE_MBUF_F_TX_IPV6;
2194                 *l4_proto = ipv6_hdr->proto;
2195                 break;
2196         default:
2197                 /* a valid L3 header is needed for further L4 parsing */
2198                 goto error;
2199         }
2200
2201         /* both CSUM and GSO need a valid L4 header */
2202         switch (*l4_proto) {
2203         case IPPROTO_TCP:
2204                 if (data_len < m->l2_len + m->l3_len +
2205                                 sizeof(struct rte_tcp_hdr))
2206                         goto error;
2207                 break;
2208         case IPPROTO_UDP:
2209                 if (data_len < m->l2_len + m->l3_len +
2210                                 sizeof(struct rte_udp_hdr))
2211                         goto error;
2212                 break;
2213         case IPPROTO_SCTP:
2214                 if (data_len < m->l2_len + m->l3_len +
2215                                 sizeof(struct rte_sctp_hdr))
2216                         goto error;
2217                 break;
2218         default:
2219                 goto error;
2220         }
2221
2222         return 0;
2223
2224 error:
2225         m->l2_len = 0;
2226         m->l3_len = 0;
2227         m->ol_flags = 0;
2228         return -EINVAL;
2229 }
2230
2231 static __rte_always_inline void
2232 vhost_dequeue_offload_legacy(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
2233 {
2234         uint8_t l4_proto = 0;
2235         struct rte_tcp_hdr *tcp_hdr = NULL;
2236         uint16_t tcp_len;
2237         uint16_t data_len = rte_pktmbuf_data_len(m);
2238
2239         if (parse_headers(m, &l4_proto) < 0)
2240                 return;
2241
2242         if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
2243                 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
2244                         switch (hdr->csum_offset) {
2245                         case (offsetof(struct rte_tcp_hdr, cksum)):
2246                                 if (l4_proto != IPPROTO_TCP)
2247                                         goto error;
2248                                 m->ol_flags |= RTE_MBUF_F_TX_TCP_CKSUM;
2249                                 break;
2250                         case (offsetof(struct rte_udp_hdr, dgram_cksum)):
2251                                 if (l4_proto != IPPROTO_UDP)
2252                                         goto error;
2253                                 m->ol_flags |= RTE_MBUF_F_TX_UDP_CKSUM;
2254                                 break;
2255                         case (offsetof(struct rte_sctp_hdr, cksum)):
2256                                 if (l4_proto != IPPROTO_SCTP)
2257                                         goto error;
2258                                 m->ol_flags |= RTE_MBUF_F_TX_SCTP_CKSUM;
2259                                 break;
2260                         default:
2261                                 goto error;
2262                         }
2263                 } else {
2264                         goto error;
2265                 }
2266         }
2267
2268         if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
2269                 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
2270                 case VIRTIO_NET_HDR_GSO_TCPV4:
2271                 case VIRTIO_NET_HDR_GSO_TCPV6:
2272                         if (l4_proto != IPPROTO_TCP)
2273                                 goto error;
2274                         tcp_hdr = rte_pktmbuf_mtod_offset(m,
2275                                         struct rte_tcp_hdr *,
2276                                         m->l2_len + m->l3_len);
2277                         tcp_len = (tcp_hdr->data_off & 0xf0) >> 2;
2278                         if (data_len < m->l2_len + m->l3_len + tcp_len)
2279                                 goto error;
2280                         m->ol_flags |= RTE_MBUF_F_TX_TCP_SEG;
2281                         m->tso_segsz = hdr->gso_size;
2282                         m->l4_len = tcp_len;
2283                         break;
2284                 case VIRTIO_NET_HDR_GSO_UDP:
2285                         if (l4_proto != IPPROTO_UDP)
2286                                 goto error;
2287                         m->ol_flags |= RTE_MBUF_F_TX_UDP_SEG;
2288                         m->tso_segsz = hdr->gso_size;
2289                         m->l4_len = sizeof(struct rte_udp_hdr);
2290                         break;
2291                 default:
2292                         VHOST_LOG_DATA(WARNING,
2293                                 "unsupported gso type %u.\n", hdr->gso_type);
2294                         goto error;
2295                 }
2296         }
2297         return;
2298
2299 error:
2300         m->l2_len = 0;
2301         m->l3_len = 0;
2302         m->ol_flags = 0;
2303 }
2304
2305 static __rte_always_inline void
2306 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m,
2307         bool legacy_ol_flags)
2308 {
2309         struct rte_net_hdr_lens hdr_lens;
2310         int l4_supported = 0;
2311         uint32_t ptype;
2312
2313         if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
2314                 return;
2315
2316         if (legacy_ol_flags) {
2317                 vhost_dequeue_offload_legacy(hdr, m);
2318                 return;
2319         }
2320
2321         m->ol_flags |= RTE_MBUF_F_RX_IP_CKSUM_UNKNOWN;
2322
2323         ptype = rte_net_get_ptype(m, &hdr_lens, RTE_PTYPE_ALL_MASK);
2324         m->packet_type = ptype;
2325         if ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP ||
2326             (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP ||
2327             (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_SCTP)
2328                 l4_supported = 1;
2329
2330         /* According to Virtio 1.1 spec, the device only needs to look at
2331          * VIRTIO_NET_HDR_F_NEEDS_CSUM in the packet transmission path.
2332          * This differs from the processing incoming packets path where the
2333          * driver could rely on VIRTIO_NET_HDR_F_DATA_VALID flag set by the
2334          * device.
2335          *
2336          * 5.1.6.2.1 Driver Requirements: Packet Transmission
2337          * The driver MUST NOT set the VIRTIO_NET_HDR_F_DATA_VALID and
2338          * VIRTIO_NET_HDR_F_RSC_INFO bits in flags.
2339          *
2340          * 5.1.6.2.2 Device Requirements: Packet Transmission
2341          * The device MUST ignore flag bits that it does not recognize.
2342          */
2343         if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
2344                 uint32_t hdrlen;
2345
2346                 hdrlen = hdr_lens.l2_len + hdr_lens.l3_len + hdr_lens.l4_len;
2347                 if (hdr->csum_start <= hdrlen && l4_supported != 0) {
2348                         m->ol_flags |= RTE_MBUF_F_RX_L4_CKSUM_NONE;
2349                 } else {
2350                         /* Unknown proto or tunnel, do sw cksum. We can assume
2351                          * the cksum field is in the first segment since the
2352                          * buffers we provided to the host are large enough.
2353                          * In case of SCTP, this will be wrong since it's a CRC
2354                          * but there's nothing we can do.
2355                          */
2356                         uint16_t csum = 0, off;
2357
2358                         if (rte_raw_cksum_mbuf(m, hdr->csum_start,
2359                                         rte_pktmbuf_pkt_len(m) - hdr->csum_start, &csum) < 0)
2360                                 return;
2361                         if (likely(csum != 0xffff))
2362                                 csum = ~csum;
2363                         off = hdr->csum_offset + hdr->csum_start;
2364                         if (rte_pktmbuf_data_len(m) >= off + 1)
2365                                 *rte_pktmbuf_mtod_offset(m, uint16_t *, off) = csum;
2366                 }
2367         }
2368
2369         if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
2370                 if (hdr->gso_size == 0)
2371                         return;
2372
2373                 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
2374                 case VIRTIO_NET_HDR_GSO_TCPV4:
2375                 case VIRTIO_NET_HDR_GSO_TCPV6:
2376                         if ((ptype & RTE_PTYPE_L4_MASK) != RTE_PTYPE_L4_TCP)
2377                                 break;
2378                         m->ol_flags |= RTE_MBUF_F_RX_LRO | RTE_MBUF_F_RX_L4_CKSUM_NONE;
2379                         m->tso_segsz = hdr->gso_size;
2380                         break;
2381                 case VIRTIO_NET_HDR_GSO_UDP:
2382                         if ((ptype & RTE_PTYPE_L4_MASK) != RTE_PTYPE_L4_UDP)
2383                                 break;
2384                         m->ol_flags |= RTE_MBUF_F_RX_LRO | RTE_MBUF_F_RX_L4_CKSUM_NONE;
2385                         m->tso_segsz = hdr->gso_size;
2386                         break;
2387                 default:
2388                         break;
2389                 }
2390         }
2391 }
2392
2393 static __rte_noinline void
2394 copy_vnet_hdr_from_desc(struct virtio_net_hdr *hdr,
2395                 struct buf_vector *buf_vec)
2396 {
2397         uint64_t len;
2398         uint64_t remain = sizeof(struct virtio_net_hdr);
2399         uint64_t src;
2400         uint64_t dst = (uint64_t)(uintptr_t)hdr;
2401
2402         while (remain) {
2403                 len = RTE_MIN(remain, buf_vec->buf_len);
2404                 src = buf_vec->buf_addr;
2405                 rte_memcpy((void *)(uintptr_t)dst,
2406                                 (void *)(uintptr_t)src, len);
2407
2408                 remain -= len;
2409                 dst += len;
2410                 buf_vec++;
2411         }
2412 }
2413
2414 static __rte_always_inline int
2415 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
2416                   struct buf_vector *buf_vec, uint16_t nr_vec,
2417                   struct rte_mbuf *m, struct rte_mempool *mbuf_pool,
2418                   bool legacy_ol_flags)
2419 {
2420         uint32_t buf_avail, buf_offset;
2421         uint64_t buf_addr, buf_len;
2422         uint32_t mbuf_avail, mbuf_offset;
2423         uint32_t cpy_len;
2424         struct rte_mbuf *cur = m, *prev = m;
2425         struct virtio_net_hdr tmp_hdr;
2426         struct virtio_net_hdr *hdr = NULL;
2427         /* A counter to avoid desc dead loop chain */
2428         uint16_t vec_idx = 0;
2429         struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
2430         int error = 0;
2431
2432         buf_addr = buf_vec[vec_idx].buf_addr;
2433         buf_len = buf_vec[vec_idx].buf_len;
2434
2435         if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
2436                 error = -1;
2437                 goto out;
2438         }
2439
2440         if (virtio_net_with_host_offload(dev)) {
2441                 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
2442                         /*
2443                          * No luck, the virtio-net header doesn't fit
2444                          * in a contiguous virtual area.
2445                          */
2446                         copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
2447                         hdr = &tmp_hdr;
2448                 } else {
2449                         hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
2450                 }
2451         }
2452
2453         /*
2454          * A virtio driver normally uses at least 2 desc buffers
2455          * for Tx: the first for storing the header, and others
2456          * for storing the data.
2457          */
2458         if (unlikely(buf_len < dev->vhost_hlen)) {
2459                 buf_offset = dev->vhost_hlen - buf_len;
2460                 vec_idx++;
2461                 buf_addr = buf_vec[vec_idx].buf_addr;
2462                 buf_len = buf_vec[vec_idx].buf_len;
2463                 buf_avail  = buf_len - buf_offset;
2464         } else if (buf_len == dev->vhost_hlen) {
2465                 if (unlikely(++vec_idx >= nr_vec))
2466                         goto out;
2467                 buf_addr = buf_vec[vec_idx].buf_addr;
2468                 buf_len = buf_vec[vec_idx].buf_len;
2469
2470                 buf_offset = 0;
2471                 buf_avail = buf_len;
2472         } else {
2473                 buf_offset = dev->vhost_hlen;
2474                 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
2475         }
2476
2477         PRINT_PACKET(dev,
2478                         (uintptr_t)(buf_addr + buf_offset),
2479                         (uint32_t)buf_avail, 0);
2480
2481         mbuf_offset = 0;
2482         mbuf_avail  = m->buf_len - RTE_PKTMBUF_HEADROOM;
2483         while (1) {
2484                 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
2485
2486                 if (likely(cpy_len > MAX_BATCH_LEN ||
2487                                         vq->batch_copy_nb_elems >= vq->size ||
2488                                         (hdr && cur == m))) {
2489                         rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
2490                                                 mbuf_offset),
2491                                         (void *)((uintptr_t)(buf_addr +
2492                                                         buf_offset)), cpy_len);
2493                 } else {
2494                         batch_copy[vq->batch_copy_nb_elems].dst =
2495                                 rte_pktmbuf_mtod_offset(cur, void *,
2496                                                 mbuf_offset);
2497                         batch_copy[vq->batch_copy_nb_elems].src =
2498                                 (void *)((uintptr_t)(buf_addr + buf_offset));
2499                         batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
2500                         vq->batch_copy_nb_elems++;
2501                 }
2502
2503                 mbuf_avail  -= cpy_len;
2504                 mbuf_offset += cpy_len;
2505                 buf_avail -= cpy_len;
2506                 buf_offset += cpy_len;
2507
2508                 /* This buf reaches to its end, get the next one */
2509                 if (buf_avail == 0) {
2510                         if (++vec_idx >= nr_vec)
2511                                 break;
2512
2513                         buf_addr = buf_vec[vec_idx].buf_addr;
2514                         buf_len = buf_vec[vec_idx].buf_len;
2515
2516                         buf_offset = 0;
2517                         buf_avail  = buf_len;
2518
2519                         PRINT_PACKET(dev, (uintptr_t)buf_addr,
2520                                         (uint32_t)buf_avail, 0);
2521                 }
2522
2523                 /*
2524                  * This mbuf reaches to its end, get a new one
2525                  * to hold more data.
2526                  */
2527                 if (mbuf_avail == 0) {
2528                         cur = rte_pktmbuf_alloc(mbuf_pool);
2529                         if (unlikely(cur == NULL)) {
2530                                 VHOST_LOG_DATA(ERR, "Failed to "
2531                                         "allocate memory for mbuf.\n");
2532                                 error = -1;
2533                                 goto out;
2534                         }
2535
2536                         prev->next = cur;
2537                         prev->data_len = mbuf_offset;
2538                         m->nb_segs += 1;
2539                         m->pkt_len += mbuf_offset;
2540                         prev = cur;
2541
2542                         mbuf_offset = 0;
2543                         mbuf_avail  = cur->buf_len - RTE_PKTMBUF_HEADROOM;
2544                 }
2545         }
2546
2547         prev->data_len = mbuf_offset;
2548         m->pkt_len    += mbuf_offset;
2549
2550         if (hdr)
2551                 vhost_dequeue_offload(hdr, m, legacy_ol_flags);
2552
2553 out:
2554
2555         return error;
2556 }
2557
2558 static void
2559 virtio_dev_extbuf_free(void *addr __rte_unused, void *opaque)
2560 {
2561         rte_free(opaque);
2562 }
2563
2564 static int
2565 virtio_dev_extbuf_alloc(struct rte_mbuf *pkt, uint32_t size)
2566 {
2567         struct rte_mbuf_ext_shared_info *shinfo = NULL;
2568         uint32_t total_len = RTE_PKTMBUF_HEADROOM + size;
2569         uint16_t buf_len;
2570         rte_iova_t iova;
2571         void *buf;
2572
2573         total_len += sizeof(*shinfo) + sizeof(uintptr_t);
2574         total_len = RTE_ALIGN_CEIL(total_len, sizeof(uintptr_t));
2575
2576         if (unlikely(total_len > UINT16_MAX))
2577                 return -ENOSPC;
2578
2579         buf_len = total_len;
2580         buf = rte_malloc(NULL, buf_len, RTE_CACHE_LINE_SIZE);
2581         if (unlikely(buf == NULL))
2582                 return -ENOMEM;
2583
2584         /* Initialize shinfo */
2585         shinfo = rte_pktmbuf_ext_shinfo_init_helper(buf, &buf_len,
2586                                                 virtio_dev_extbuf_free, buf);
2587         if (unlikely(shinfo == NULL)) {
2588                 rte_free(buf);
2589                 VHOST_LOG_DATA(ERR, "Failed to init shinfo\n");
2590                 return -1;
2591         }
2592
2593         iova = rte_malloc_virt2iova(buf);
2594         rte_pktmbuf_attach_extbuf(pkt, buf, iova, buf_len, shinfo);
2595         rte_pktmbuf_reset_headroom(pkt);
2596
2597         return 0;
2598 }
2599
2600 /*
2601  * Prepare a host supported pktmbuf.
2602  */
2603 static __rte_always_inline int
2604 virtio_dev_pktmbuf_prep(struct virtio_net *dev, struct rte_mbuf *pkt,
2605                          uint32_t data_len)
2606 {
2607         if (rte_pktmbuf_tailroom(pkt) >= data_len)
2608                 return 0;
2609
2610         /* attach an external buffer if supported */
2611         if (dev->extbuf && !virtio_dev_extbuf_alloc(pkt, data_len))
2612                 return 0;
2613
2614         /* check if chained buffers are allowed */
2615         if (!dev->linearbuf)
2616                 return 0;
2617
2618         return -1;
2619 }
2620
2621 __rte_always_inline
2622 static uint16_t
2623 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
2624         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count,
2625         bool legacy_ol_flags)
2626 {
2627         uint16_t i;
2628         uint16_t free_entries;
2629         uint16_t dropped = 0;
2630         static bool allocerr_warned;
2631
2632         /*
2633          * The ordering between avail index and
2634          * desc reads needs to be enforced.
2635          */
2636         free_entries = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE) -
2637                         vq->last_avail_idx;
2638         if (free_entries == 0)
2639                 return 0;
2640
2641         rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
2642
2643         VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
2644
2645         count = RTE_MIN(count, MAX_PKT_BURST);
2646         count = RTE_MIN(count, free_entries);
2647         VHOST_LOG_DATA(DEBUG, "(%d) about to dequeue %u buffers\n",
2648                         dev->vid, count);
2649
2650         if (rte_pktmbuf_alloc_bulk(mbuf_pool, pkts, count))
2651                 return 0;
2652
2653         for (i = 0; i < count; i++) {
2654                 struct buf_vector buf_vec[BUF_VECTOR_MAX];
2655                 uint16_t head_idx;
2656                 uint32_t buf_len;
2657                 uint16_t nr_vec = 0;
2658                 int err;
2659
2660                 if (unlikely(fill_vec_buf_split(dev, vq,
2661                                                 vq->last_avail_idx + i,
2662                                                 &nr_vec, buf_vec,
2663                                                 &head_idx, &buf_len,
2664                                                 VHOST_ACCESS_RO) < 0))
2665                         break;
2666
2667                 update_shadow_used_ring_split(vq, head_idx, 0);
2668
2669                 err = virtio_dev_pktmbuf_prep(dev, pkts[i], buf_len);
2670                 if (unlikely(err)) {
2671                         /*
2672                          * mbuf allocation fails for jumbo packets when external
2673                          * buffer allocation is not allowed and linear buffer
2674                          * is required. Drop this packet.
2675                          */
2676                         if (!allocerr_warned) {
2677                                 VHOST_LOG_DATA(ERR,
2678                                         "Failed mbuf alloc of size %d from %s on %s.\n",
2679                                         buf_len, mbuf_pool->name, dev->ifname);
2680                                 allocerr_warned = true;
2681                         }
2682                         dropped += 1;
2683                         i++;
2684                         break;
2685                 }
2686
2687                 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
2688                                 mbuf_pool, legacy_ol_flags);
2689                 if (unlikely(err)) {
2690                         if (!allocerr_warned) {
2691                                 VHOST_LOG_DATA(ERR,
2692                                         "Failed to copy desc to mbuf on %s.\n",
2693                                         dev->ifname);
2694                                 allocerr_warned = true;
2695                         }
2696                         dropped += 1;
2697                         i++;
2698                         break;
2699                 }
2700         }
2701
2702         if (dropped)
2703                 rte_pktmbuf_free_bulk(&pkts[i - 1], count - i + 1);
2704
2705         vq->last_avail_idx += i;
2706
2707         do_data_copy_dequeue(vq);
2708         if (unlikely(i < count))
2709                 vq->shadow_used_idx = i;
2710         if (likely(vq->shadow_used_idx)) {
2711                 flush_shadow_used_ring_split(dev, vq);
2712                 vhost_vring_call_split(dev, vq);
2713         }
2714
2715         return (i - dropped);
2716 }
2717
2718 __rte_noinline
2719 static uint16_t
2720 virtio_dev_tx_split_legacy(struct virtio_net *dev,
2721         struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
2722         struct rte_mbuf **pkts, uint16_t count)
2723 {
2724         return virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count, true);
2725 }
2726
2727 __rte_noinline
2728 static uint16_t
2729 virtio_dev_tx_split_compliant(struct virtio_net *dev,
2730         struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
2731         struct rte_mbuf **pkts, uint16_t count)
2732 {
2733         return virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count, false);
2734 }
2735
2736 static __rte_always_inline int
2737 vhost_reserve_avail_batch_packed(struct virtio_net *dev,
2738                                  struct vhost_virtqueue *vq,
2739                                  struct rte_mbuf **pkts,
2740                                  uint16_t avail_idx,
2741                                  uintptr_t *desc_addrs,
2742                                  uint16_t *ids)
2743 {
2744         bool wrap = vq->avail_wrap_counter;
2745         struct vring_packed_desc *descs = vq->desc_packed;
2746         uint64_t lens[PACKED_BATCH_SIZE];
2747         uint64_t buf_lens[PACKED_BATCH_SIZE];
2748         uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2749         uint16_t flags, i;
2750
2751         if (unlikely(avail_idx & PACKED_BATCH_MASK))
2752                 return -1;
2753         if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
2754                 return -1;
2755
2756         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2757                 flags = descs[avail_idx + i].flags;
2758                 if (unlikely((wrap != !!(flags & VRING_DESC_F_AVAIL)) ||
2759                              (wrap == !!(flags & VRING_DESC_F_USED))  ||
2760                              (flags & PACKED_DESC_SINGLE_DEQUEUE_FLAG)))
2761                         return -1;
2762         }
2763
2764         rte_atomic_thread_fence(__ATOMIC_ACQUIRE);
2765
2766         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2767                 lens[i] = descs[avail_idx + i].len;
2768
2769         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2770                 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
2771                                                   descs[avail_idx + i].addr,
2772                                                   &lens[i], VHOST_ACCESS_RW);
2773         }
2774
2775         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2776                 if (unlikely(!desc_addrs[i]))
2777                         return -1;
2778                 if (unlikely((lens[i] != descs[avail_idx + i].len)))
2779                         return -1;
2780         }
2781
2782         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2783                 if (virtio_dev_pktmbuf_prep(dev, pkts[i], lens[i]))
2784                         goto err;
2785         }
2786
2787         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2788                 buf_lens[i] = pkts[i]->buf_len - pkts[i]->data_off;
2789
2790         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2791                 if (unlikely(buf_lens[i] < (lens[i] - buf_offset)))
2792                         goto err;
2793         }
2794
2795         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2796                 pkts[i]->pkt_len = lens[i] - buf_offset;
2797                 pkts[i]->data_len = pkts[i]->pkt_len;
2798                 ids[i] = descs[avail_idx + i].id;
2799         }
2800
2801         return 0;
2802
2803 err:
2804         return -1;
2805 }
2806
2807 static __rte_always_inline int
2808 virtio_dev_tx_batch_packed(struct virtio_net *dev,
2809                            struct vhost_virtqueue *vq,
2810                            struct rte_mbuf **pkts,
2811                            bool legacy_ol_flags)
2812 {
2813         uint16_t avail_idx = vq->last_avail_idx;
2814         uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2815         struct virtio_net_hdr *hdr;
2816         uintptr_t desc_addrs[PACKED_BATCH_SIZE];
2817         uint16_t ids[PACKED_BATCH_SIZE];
2818         uint16_t i;
2819
2820         if (vhost_reserve_avail_batch_packed(dev, vq, pkts, avail_idx,
2821                                              desc_addrs, ids))
2822                 return -1;
2823
2824         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2825                 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
2826
2827         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2828                 rte_memcpy(rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
2829                            (void *)(uintptr_t)(desc_addrs[i] + buf_offset),
2830                            pkts[i]->pkt_len);
2831
2832         if (virtio_net_with_host_offload(dev)) {
2833                 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2834                         hdr = (struct virtio_net_hdr *)(desc_addrs[i]);
2835                         vhost_dequeue_offload(hdr, pkts[i], legacy_ol_flags);
2836                 }
2837         }
2838
2839         if (virtio_net_is_inorder(dev))
2840                 vhost_shadow_dequeue_batch_packed_inorder(vq,
2841                         ids[PACKED_BATCH_SIZE - 1]);
2842         else
2843                 vhost_shadow_dequeue_batch_packed(dev, vq, ids);
2844
2845         vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
2846
2847         return 0;
2848 }
2849
2850 static __rte_always_inline int
2851 vhost_dequeue_single_packed(struct virtio_net *dev,
2852                             struct vhost_virtqueue *vq,
2853                             struct rte_mempool *mbuf_pool,
2854                             struct rte_mbuf *pkts,
2855                             uint16_t *buf_id,
2856                             uint16_t *desc_count,
2857                             bool legacy_ol_flags)
2858 {
2859         struct buf_vector buf_vec[BUF_VECTOR_MAX];
2860         uint32_t buf_len;
2861         uint16_t nr_vec = 0;
2862         int err;
2863         static bool allocerr_warned;
2864
2865         if (unlikely(fill_vec_buf_packed(dev, vq,
2866                                          vq->last_avail_idx, desc_count,
2867                                          buf_vec, &nr_vec,
2868                                          buf_id, &buf_len,
2869                                          VHOST_ACCESS_RO) < 0))
2870                 return -1;
2871
2872         if (unlikely(virtio_dev_pktmbuf_prep(dev, pkts, buf_len))) {
2873                 if (!allocerr_warned) {
2874                         VHOST_LOG_DATA(ERR,
2875                                 "Failed mbuf alloc of size %d from %s on %s.\n",
2876                                 buf_len, mbuf_pool->name, dev->ifname);
2877                         allocerr_warned = true;
2878                 }
2879                 return -1;
2880         }
2881
2882         err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts,
2883                                 mbuf_pool, legacy_ol_flags);
2884         if (unlikely(err)) {
2885                 if (!allocerr_warned) {
2886                         VHOST_LOG_DATA(ERR,
2887                                 "Failed to copy desc to mbuf on %s.\n",
2888                                 dev->ifname);
2889                         allocerr_warned = true;
2890                 }
2891                 return -1;
2892         }
2893
2894         return 0;
2895 }
2896
2897 static __rte_always_inline int
2898 virtio_dev_tx_single_packed(struct virtio_net *dev,
2899                             struct vhost_virtqueue *vq,
2900                             struct rte_mempool *mbuf_pool,
2901                             struct rte_mbuf *pkts,
2902                             bool legacy_ol_flags)
2903 {
2904
2905         uint16_t buf_id, desc_count = 0;
2906         int ret;
2907
2908         ret = vhost_dequeue_single_packed(dev, vq, mbuf_pool, pkts, &buf_id,
2909                                         &desc_count, legacy_ol_flags);
2910
2911         if (likely(desc_count > 0)) {
2912                 if (virtio_net_is_inorder(dev))
2913                         vhost_shadow_dequeue_single_packed_inorder(vq, buf_id,
2914                                                                    desc_count);
2915                 else
2916                         vhost_shadow_dequeue_single_packed(vq, buf_id,
2917                                         desc_count);
2918
2919                 vq_inc_last_avail_packed(vq, desc_count);
2920         }
2921
2922         return ret;
2923 }
2924
2925 __rte_always_inline
2926 static uint16_t
2927 virtio_dev_tx_packed(struct virtio_net *dev,
2928                      struct vhost_virtqueue *__rte_restrict vq,
2929                      struct rte_mempool *mbuf_pool,
2930                      struct rte_mbuf **__rte_restrict pkts,
2931                      uint32_t count,
2932                      bool legacy_ol_flags)
2933 {
2934         uint32_t pkt_idx = 0;
2935
2936         if (rte_pktmbuf_alloc_bulk(mbuf_pool, pkts, count))
2937                 return 0;
2938
2939         do {
2940                 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
2941
2942                 if (count - pkt_idx >= PACKED_BATCH_SIZE) {
2943                         if (!virtio_dev_tx_batch_packed(dev, vq,
2944                                                         &pkts[pkt_idx],
2945                                                         legacy_ol_flags)) {
2946                                 pkt_idx += PACKED_BATCH_SIZE;
2947                                 continue;
2948                         }
2949                 }
2950
2951                 if (virtio_dev_tx_single_packed(dev, vq, mbuf_pool,
2952                                                 pkts[pkt_idx],
2953                                                 legacy_ol_flags))
2954                         break;
2955                 pkt_idx++;
2956         } while (pkt_idx < count);
2957
2958         if (pkt_idx != count)
2959                 rte_pktmbuf_free_bulk(&pkts[pkt_idx], count - pkt_idx);
2960
2961         if (vq->shadow_used_idx) {
2962                 do_data_copy_dequeue(vq);
2963
2964                 vhost_flush_dequeue_shadow_packed(dev, vq);
2965                 vhost_vring_call_packed(dev, vq);
2966         }
2967
2968         return pkt_idx;
2969 }
2970
2971 __rte_noinline
2972 static uint16_t
2973 virtio_dev_tx_packed_legacy(struct virtio_net *dev,
2974         struct vhost_virtqueue *__rte_restrict vq, struct rte_mempool *mbuf_pool,
2975         struct rte_mbuf **__rte_restrict pkts, uint32_t count)
2976 {
2977         return virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count, true);
2978 }
2979
2980 __rte_noinline
2981 static uint16_t
2982 virtio_dev_tx_packed_compliant(struct virtio_net *dev,
2983         struct vhost_virtqueue *__rte_restrict vq, struct rte_mempool *mbuf_pool,
2984         struct rte_mbuf **__rte_restrict pkts, uint32_t count)
2985 {
2986         return virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count, false);
2987 }
2988
2989 uint16_t
2990 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
2991         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
2992 {
2993         struct virtio_net *dev;
2994         struct rte_mbuf *rarp_mbuf = NULL;
2995         struct vhost_virtqueue *vq;
2996         int16_t success = 1;
2997
2998         dev = get_device(vid);
2999         if (!dev)
3000                 return 0;
3001
3002         if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
3003                 VHOST_LOG_DATA(ERR,
3004                         "(%d) %s: built-in vhost net backend is disabled.\n",
3005                         dev->vid, __func__);
3006                 return 0;
3007         }
3008
3009         if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
3010                 VHOST_LOG_DATA(ERR,
3011                         "(%d) %s: invalid virtqueue idx %d.\n",
3012                         dev->vid, __func__, queue_id);
3013                 return 0;
3014         }
3015
3016         vq = dev->virtqueue[queue_id];
3017
3018         if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
3019                 return 0;
3020
3021         if (unlikely(!vq->enabled)) {
3022                 count = 0;
3023                 goto out_access_unlock;
3024         }
3025
3026         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
3027                 vhost_user_iotlb_rd_lock(vq);
3028
3029         if (unlikely(!vq->access_ok))
3030                 if (unlikely(vring_translate(dev, vq) < 0)) {
3031                         count = 0;
3032                         goto out;
3033                 }
3034
3035         /*
3036          * Construct a RARP broadcast packet, and inject it to the "pkts"
3037          * array, to looks like that guest actually send such packet.
3038          *
3039          * Check user_send_rarp() for more information.
3040          *
3041          * broadcast_rarp shares a cacheline in the virtio_net structure
3042          * with some fields that are accessed during enqueue and
3043          * __atomic_compare_exchange_n causes a write if performed compare
3044          * and exchange. This could result in false sharing between enqueue
3045          * and dequeue.
3046          *
3047          * Prevent unnecessary false sharing by reading broadcast_rarp first
3048          * and only performing compare and exchange if the read indicates it
3049          * is likely to be set.
3050          */
3051         if (unlikely(__atomic_load_n(&dev->broadcast_rarp, __ATOMIC_ACQUIRE) &&
3052                         __atomic_compare_exchange_n(&dev->broadcast_rarp,
3053                         &success, 0, 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED))) {
3054
3055                 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
3056                 if (rarp_mbuf == NULL) {
3057                         VHOST_LOG_DATA(ERR, "Failed to make RARP packet.\n");
3058                         count = 0;
3059                         goto out;
3060                 }
3061                 /*
3062                  * Inject it to the head of "pkts" array, so that switch's mac
3063                  * learning table will get updated first.
3064                  */
3065                 pkts[0] = rarp_mbuf;
3066                 pkts++;
3067                 count -= 1;
3068         }
3069
3070         if (vq_is_packed(dev)) {
3071                 if (dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS)
3072                         count = virtio_dev_tx_packed_legacy(dev, vq, mbuf_pool, pkts, count);
3073                 else
3074                         count = virtio_dev_tx_packed_compliant(dev, vq, mbuf_pool, pkts, count);
3075         } else {
3076                 if (dev->flags & VIRTIO_DEV_LEGACY_OL_FLAGS)
3077                         count = virtio_dev_tx_split_legacy(dev, vq, mbuf_pool, pkts, count);
3078                 else
3079                         count = virtio_dev_tx_split_compliant(dev, vq, mbuf_pool, pkts, count);
3080         }
3081
3082 out:
3083         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
3084                 vhost_user_iotlb_rd_unlock(vq);
3085
3086 out_access_unlock:
3087         rte_spinlock_unlock(&vq->access_lock);
3088
3089         if (unlikely(rarp_mbuf != NULL))
3090                 count += 1;
3091
3092         return count;
3093 }