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