vhost: relax full barriers for desc flags
[dpdk.git] / lib / librte_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_ether.h>
12 #include <rte_ip.h>
13 #include <rte_vhost.h>
14 #include <rte_tcp.h>
15 #include <rte_udp.h>
16 #include <rte_sctp.h>
17 #include <rte_arp.h>
18 #include <rte_spinlock.h>
19 #include <rte_malloc.h>
20 #include <rte_vhost_async.h>
21
22 #include "iotlb.h"
23 #include "vhost.h"
24
25 #define MAX_BATCH_LEN 256
26
27 #define VHOST_ASYNC_BATCH_THRESHOLD 32
28
29 static  __rte_always_inline bool
30 rxvq_is_mergeable(struct virtio_net *dev)
31 {
32         return dev->features & (1ULL << VIRTIO_NET_F_MRG_RXBUF);
33 }
34
35 static  __rte_always_inline bool
36 virtio_net_is_inorder(struct virtio_net *dev)
37 {
38         return dev->features & (1ULL << VIRTIO_F_IN_ORDER);
39 }
40
41 static bool
42 is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t nr_vring)
43 {
44         return (is_tx ^ (idx & 1)) == 0 && idx < nr_vring;
45 }
46
47 static inline void
48 do_data_copy_enqueue(struct virtio_net *dev, struct vhost_virtqueue *vq)
49 {
50         struct batch_copy_elem *elem = vq->batch_copy_elems;
51         uint16_t count = vq->batch_copy_nb_elems;
52         int i;
53
54         for (i = 0; i < count; i++) {
55                 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
56                 vhost_log_cache_write_iova(dev, vq, elem[i].log_addr,
57                                            elem[i].len);
58                 PRINT_PACKET(dev, (uintptr_t)elem[i].dst, elem[i].len, 0);
59         }
60
61         vq->batch_copy_nb_elems = 0;
62 }
63
64 static inline void
65 do_data_copy_dequeue(struct vhost_virtqueue *vq)
66 {
67         struct batch_copy_elem *elem = vq->batch_copy_elems;
68         uint16_t count = vq->batch_copy_nb_elems;
69         int i;
70
71         for (i = 0; i < count; i++)
72                 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
73
74         vq->batch_copy_nb_elems = 0;
75 }
76
77 static __rte_always_inline void
78 do_flush_shadow_used_ring_split(struct virtio_net *dev,
79                         struct vhost_virtqueue *vq,
80                         uint16_t to, uint16_t from, uint16_t size)
81 {
82         rte_memcpy(&vq->used->ring[to],
83                         &vq->shadow_used_split[from],
84                         size * sizeof(struct vring_used_elem));
85         vhost_log_cache_used_vring(dev, vq,
86                         offsetof(struct vring_used, ring[to]),
87                         size * sizeof(struct vring_used_elem));
88 }
89
90 static __rte_always_inline void
91 flush_shadow_used_ring_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
92 {
93         uint16_t used_idx = vq->last_used_idx & (vq->size - 1);
94
95         if (used_idx + vq->shadow_used_idx <= vq->size) {
96                 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0,
97                                           vq->shadow_used_idx);
98         } else {
99                 uint16_t size;
100
101                 /* update used ring interval [used_idx, vq->size] */
102                 size = vq->size - used_idx;
103                 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0, size);
104
105                 /* update the left half used ring interval [0, left_size] */
106                 do_flush_shadow_used_ring_split(dev, vq, 0, size,
107                                           vq->shadow_used_idx - size);
108         }
109         vq->last_used_idx += vq->shadow_used_idx;
110
111         vhost_log_cache_sync(dev, vq);
112
113         __atomic_add_fetch(&vq->used->idx, vq->shadow_used_idx,
114                            __ATOMIC_RELEASE);
115         vq->shadow_used_idx = 0;
116         vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
117                 sizeof(vq->used->idx));
118 }
119
120 static __rte_always_inline void
121 async_flush_shadow_used_ring_split(struct virtio_net *dev,
122         struct vhost_virtqueue *vq)
123 {
124         uint16_t used_idx = vq->last_used_idx & (vq->size - 1);
125
126         if (used_idx + vq->shadow_used_idx <= vq->size) {
127                 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0,
128                                           vq->shadow_used_idx);
129         } else {
130                 uint16_t size;
131
132                 /* update used ring interval [used_idx, vq->size] */
133                 size = vq->size - used_idx;
134                 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0, size);
135
136                 /* update the left half used ring interval [0, left_size] */
137                 do_flush_shadow_used_ring_split(dev, vq, 0, size,
138                                           vq->shadow_used_idx - size);
139         }
140
141         vq->last_used_idx += vq->shadow_used_idx;
142         vq->shadow_used_idx = 0;
143 }
144
145 static __rte_always_inline void
146 update_shadow_used_ring_split(struct vhost_virtqueue *vq,
147                          uint16_t desc_idx, uint32_t len)
148 {
149         uint16_t i = vq->shadow_used_idx++;
150
151         vq->shadow_used_split[i].id  = desc_idx;
152         vq->shadow_used_split[i].len = len;
153 }
154
155 static __rte_always_inline void
156 vhost_flush_enqueue_shadow_packed(struct virtio_net *dev,
157                                   struct vhost_virtqueue *vq)
158 {
159         int i;
160         uint16_t used_idx = vq->last_used_idx;
161         uint16_t head_idx = vq->last_used_idx;
162         uint16_t head_flags = 0;
163
164         /* Split loop in two to save memory barriers */
165         for (i = 0; i < vq->shadow_used_idx; i++) {
166                 vq->desc_packed[used_idx].id = vq->shadow_used_packed[i].id;
167                 vq->desc_packed[used_idx].len = vq->shadow_used_packed[i].len;
168
169                 used_idx += vq->shadow_used_packed[i].count;
170                 if (used_idx >= vq->size)
171                         used_idx -= vq->size;
172         }
173
174         rte_smp_wmb();
175
176         for (i = 0; i < vq->shadow_used_idx; i++) {
177                 uint16_t flags;
178
179                 if (vq->shadow_used_packed[i].len)
180                         flags = VRING_DESC_F_WRITE;
181                 else
182                         flags = 0;
183
184                 if (vq->used_wrap_counter) {
185                         flags |= VRING_DESC_F_USED;
186                         flags |= VRING_DESC_F_AVAIL;
187                 } else {
188                         flags &= ~VRING_DESC_F_USED;
189                         flags &= ~VRING_DESC_F_AVAIL;
190                 }
191
192                 if (i > 0) {
193                         vq->desc_packed[vq->last_used_idx].flags = flags;
194
195                         vhost_log_cache_used_vring(dev, vq,
196                                         vq->last_used_idx *
197                                         sizeof(struct vring_packed_desc),
198                                         sizeof(struct vring_packed_desc));
199                 } else {
200                         head_idx = vq->last_used_idx;
201                         head_flags = flags;
202                 }
203
204                 vq_inc_last_used_packed(vq, vq->shadow_used_packed[i].count);
205         }
206
207         vq->desc_packed[head_idx].flags = head_flags;
208
209         vhost_log_cache_used_vring(dev, vq,
210                                 head_idx *
211                                 sizeof(struct vring_packed_desc),
212                                 sizeof(struct vring_packed_desc));
213
214         vq->shadow_used_idx = 0;
215         vhost_log_cache_sync(dev, vq);
216 }
217
218 static __rte_always_inline void
219 vhost_flush_dequeue_shadow_packed(struct virtio_net *dev,
220                                   struct vhost_virtqueue *vq)
221 {
222         struct vring_used_elem_packed *used_elem = &vq->shadow_used_packed[0];
223
224         vq->desc_packed[vq->shadow_last_used_idx].id = used_elem->id;
225         /* desc flags is the synchronization point for virtio packed vring */
226         __atomic_store_n(&vq->desc_packed[vq->shadow_last_used_idx].flags,
227                          used_elem->flags, __ATOMIC_RELEASE);
228
229         vhost_log_cache_used_vring(dev, vq, vq->shadow_last_used_idx *
230                                    sizeof(struct vring_packed_desc),
231                                    sizeof(struct vring_packed_desc));
232         vq->shadow_used_idx = 0;
233         vhost_log_cache_sync(dev, vq);
234 }
235
236 static __rte_always_inline void
237 vhost_flush_enqueue_batch_packed(struct virtio_net *dev,
238                                  struct vhost_virtqueue *vq,
239                                  uint64_t *lens,
240                                  uint16_t *ids)
241 {
242         uint16_t i;
243         uint16_t flags;
244
245         if (vq->shadow_used_idx) {
246                 do_data_copy_enqueue(dev, vq);
247                 vhost_flush_enqueue_shadow_packed(dev, vq);
248         }
249
250         flags = PACKED_DESC_ENQUEUE_USED_FLAG(vq->used_wrap_counter);
251
252         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
253                 vq->desc_packed[vq->last_used_idx + i].id = ids[i];
254                 vq->desc_packed[vq->last_used_idx + i].len = lens[i];
255         }
256
257         rte_smp_wmb();
258
259         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
260                 vq->desc_packed[vq->last_used_idx + i].flags = flags;
261
262         vhost_log_cache_used_vring(dev, vq, vq->last_used_idx *
263                                    sizeof(struct vring_packed_desc),
264                                    sizeof(struct vring_packed_desc) *
265                                    PACKED_BATCH_SIZE);
266         vhost_log_cache_sync(dev, vq);
267
268         vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
269 }
270
271 static __rte_always_inline void
272 vhost_shadow_dequeue_batch_packed_inorder(struct vhost_virtqueue *vq,
273                                           uint16_t id)
274 {
275         vq->shadow_used_packed[0].id = id;
276
277         if (!vq->shadow_used_idx) {
278                 vq->shadow_last_used_idx = vq->last_used_idx;
279                 vq->shadow_used_packed[0].flags =
280                         PACKED_DESC_DEQUEUE_USED_FLAG(vq->used_wrap_counter);
281                 vq->shadow_used_packed[0].len = 0;
282                 vq->shadow_used_packed[0].count = 1;
283                 vq->shadow_used_idx++;
284         }
285
286         vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
287 }
288
289 static __rte_always_inline void
290 vhost_shadow_dequeue_batch_packed(struct virtio_net *dev,
291                                   struct vhost_virtqueue *vq,
292                                   uint16_t *ids)
293 {
294         uint16_t flags;
295         uint16_t i;
296         uint16_t begin;
297
298         flags = PACKED_DESC_DEQUEUE_USED_FLAG(vq->used_wrap_counter);
299
300         if (!vq->shadow_used_idx) {
301                 vq->shadow_last_used_idx = vq->last_used_idx;
302                 vq->shadow_used_packed[0].id  = ids[0];
303                 vq->shadow_used_packed[0].len = 0;
304                 vq->shadow_used_packed[0].count = 1;
305                 vq->shadow_used_packed[0].flags = flags;
306                 vq->shadow_used_idx++;
307                 begin = 1;
308         } else
309                 begin = 0;
310
311         vhost_for_each_try_unroll(i, begin, PACKED_BATCH_SIZE) {
312                 vq->desc_packed[vq->last_used_idx + i].id = ids[i];
313                 vq->desc_packed[vq->last_used_idx + i].len = 0;
314         }
315
316         rte_smp_wmb();
317         vhost_for_each_try_unroll(i, begin, PACKED_BATCH_SIZE)
318                 vq->desc_packed[vq->last_used_idx + i].flags = flags;
319
320         vhost_log_cache_used_vring(dev, vq, vq->last_used_idx *
321                                    sizeof(struct vring_packed_desc),
322                                    sizeof(struct vring_packed_desc) *
323                                    PACKED_BATCH_SIZE);
324         vhost_log_cache_sync(dev, vq);
325
326         vq_inc_last_used_packed(vq, PACKED_BATCH_SIZE);
327 }
328
329 static __rte_always_inline void
330 vhost_shadow_dequeue_single_packed(struct vhost_virtqueue *vq,
331                                    uint16_t buf_id,
332                                    uint16_t count)
333 {
334         uint16_t flags;
335
336         flags = vq->desc_packed[vq->last_used_idx].flags;
337         if (vq->used_wrap_counter) {
338                 flags |= VRING_DESC_F_USED;
339                 flags |= VRING_DESC_F_AVAIL;
340         } else {
341                 flags &= ~VRING_DESC_F_USED;
342                 flags &= ~VRING_DESC_F_AVAIL;
343         }
344
345         if (!vq->shadow_used_idx) {
346                 vq->shadow_last_used_idx = vq->last_used_idx;
347
348                 vq->shadow_used_packed[0].id  = buf_id;
349                 vq->shadow_used_packed[0].len = 0;
350                 vq->shadow_used_packed[0].flags = flags;
351                 vq->shadow_used_idx++;
352         } else {
353                 vq->desc_packed[vq->last_used_idx].id = buf_id;
354                 vq->desc_packed[vq->last_used_idx].len = 0;
355                 vq->desc_packed[vq->last_used_idx].flags = flags;
356         }
357
358         vq_inc_last_used_packed(vq, count);
359 }
360
361 static __rte_always_inline void
362 vhost_shadow_dequeue_single_packed_inorder(struct vhost_virtqueue *vq,
363                                            uint16_t buf_id,
364                                            uint16_t count)
365 {
366         uint16_t flags;
367
368         vq->shadow_used_packed[0].id = buf_id;
369
370         flags = vq->desc_packed[vq->last_used_idx].flags;
371         if (vq->used_wrap_counter) {
372                 flags |= VRING_DESC_F_USED;
373                 flags |= VRING_DESC_F_AVAIL;
374         } else {
375                 flags &= ~VRING_DESC_F_USED;
376                 flags &= ~VRING_DESC_F_AVAIL;
377         }
378
379         if (!vq->shadow_used_idx) {
380                 vq->shadow_last_used_idx = vq->last_used_idx;
381                 vq->shadow_used_packed[0].len = 0;
382                 vq->shadow_used_packed[0].flags = flags;
383                 vq->shadow_used_idx++;
384         }
385
386         vq_inc_last_used_packed(vq, count);
387 }
388
389 static __rte_always_inline void
390 vhost_shadow_enqueue_single_packed(struct virtio_net *dev,
391                                    struct vhost_virtqueue *vq,
392                                    uint32_t len[],
393                                    uint16_t id[],
394                                    uint16_t count[],
395                                    uint16_t num_buffers)
396 {
397         uint16_t i;
398         for (i = 0; i < num_buffers; i++) {
399                 /* enqueue shadow flush action aligned with batch num */
400                 if (!vq->shadow_used_idx)
401                         vq->shadow_aligned_idx = vq->last_used_idx &
402                                 PACKED_BATCH_MASK;
403                 vq->shadow_used_packed[vq->shadow_used_idx].id  = id[i];
404                 vq->shadow_used_packed[vq->shadow_used_idx].len = len[i];
405                 vq->shadow_used_packed[vq->shadow_used_idx].count = count[i];
406                 vq->shadow_aligned_idx += count[i];
407                 vq->shadow_used_idx++;
408         }
409
410         if (vq->shadow_aligned_idx >= PACKED_BATCH_SIZE) {
411                 do_data_copy_enqueue(dev, vq);
412                 vhost_flush_enqueue_shadow_packed(dev, vq);
413         }
414 }
415
416 /* avoid write operation when necessary, to lessen cache issues */
417 #define ASSIGN_UNLESS_EQUAL(var, val) do {      \
418         if ((var) != (val))                     \
419                 (var) = (val);                  \
420 } while (0)
421
422 static __rte_always_inline void
423 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
424 {
425         uint64_t csum_l4 = m_buf->ol_flags & PKT_TX_L4_MASK;
426
427         if (m_buf->ol_flags & PKT_TX_TCP_SEG)
428                 csum_l4 |= PKT_TX_TCP_CKSUM;
429
430         if (csum_l4) {
431                 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
432                 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
433
434                 switch (csum_l4) {
435                 case PKT_TX_TCP_CKSUM:
436                         net_hdr->csum_offset = (offsetof(struct rte_tcp_hdr,
437                                                 cksum));
438                         break;
439                 case PKT_TX_UDP_CKSUM:
440                         net_hdr->csum_offset = (offsetof(struct rte_udp_hdr,
441                                                 dgram_cksum));
442                         break;
443                 case PKT_TX_SCTP_CKSUM:
444                         net_hdr->csum_offset = (offsetof(struct rte_sctp_hdr,
445                                                 cksum));
446                         break;
447                 }
448         } else {
449                 ASSIGN_UNLESS_EQUAL(net_hdr->csum_start, 0);
450                 ASSIGN_UNLESS_EQUAL(net_hdr->csum_offset, 0);
451                 ASSIGN_UNLESS_EQUAL(net_hdr->flags, 0);
452         }
453
454         /* IP cksum verification cannot be bypassed, then calculate here */
455         if (m_buf->ol_flags & PKT_TX_IP_CKSUM) {
456                 struct rte_ipv4_hdr *ipv4_hdr;
457
458                 ipv4_hdr = rte_pktmbuf_mtod_offset(m_buf, struct rte_ipv4_hdr *,
459                                                    m_buf->l2_len);
460                 ipv4_hdr->hdr_checksum = 0;
461                 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
462         }
463
464         if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
465                 if (m_buf->ol_flags & PKT_TX_IPV4)
466                         net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
467                 else
468                         net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
469                 net_hdr->gso_size = m_buf->tso_segsz;
470                 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
471                                         + m_buf->l4_len;
472         } else if (m_buf->ol_flags & PKT_TX_UDP_SEG) {
473                 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
474                 net_hdr->gso_size = m_buf->tso_segsz;
475                 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len +
476                         m_buf->l4_len;
477         } else {
478                 ASSIGN_UNLESS_EQUAL(net_hdr->gso_type, 0);
479                 ASSIGN_UNLESS_EQUAL(net_hdr->gso_size, 0);
480                 ASSIGN_UNLESS_EQUAL(net_hdr->hdr_len, 0);
481         }
482 }
483
484 static __rte_always_inline int
485 map_one_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
486                 struct buf_vector *buf_vec, uint16_t *vec_idx,
487                 uint64_t desc_iova, uint64_t desc_len, uint8_t perm)
488 {
489         uint16_t vec_id = *vec_idx;
490
491         while (desc_len) {
492                 uint64_t desc_addr;
493                 uint64_t desc_chunck_len = desc_len;
494
495                 if (unlikely(vec_id >= BUF_VECTOR_MAX))
496                         return -1;
497
498                 desc_addr = vhost_iova_to_vva(dev, vq,
499                                 desc_iova,
500                                 &desc_chunck_len,
501                                 perm);
502                 if (unlikely(!desc_addr))
503                         return -1;
504
505                 rte_prefetch0((void *)(uintptr_t)desc_addr);
506
507                 buf_vec[vec_id].buf_iova = desc_iova;
508                 buf_vec[vec_id].buf_addr = desc_addr;
509                 buf_vec[vec_id].buf_len  = desc_chunck_len;
510
511                 desc_len -= desc_chunck_len;
512                 desc_iova += desc_chunck_len;
513                 vec_id++;
514         }
515         *vec_idx = vec_id;
516
517         return 0;
518 }
519
520 static __rte_always_inline int
521 fill_vec_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
522                          uint32_t avail_idx, uint16_t *vec_idx,
523                          struct buf_vector *buf_vec, uint16_t *desc_chain_head,
524                          uint32_t *desc_chain_len, uint8_t perm)
525 {
526         uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
527         uint16_t vec_id = *vec_idx;
528         uint32_t len    = 0;
529         uint64_t dlen;
530         uint32_t nr_descs = vq->size;
531         uint32_t cnt    = 0;
532         struct vring_desc *descs = vq->desc;
533         struct vring_desc *idesc = NULL;
534
535         if (unlikely(idx >= vq->size))
536                 return -1;
537
538         *desc_chain_head = idx;
539
540         if (vq->desc[idx].flags & VRING_DESC_F_INDIRECT) {
541                 dlen = vq->desc[idx].len;
542                 nr_descs = dlen / sizeof(struct vring_desc);
543                 if (unlikely(nr_descs > vq->size))
544                         return -1;
545
546                 descs = (struct vring_desc *)(uintptr_t)
547                         vhost_iova_to_vva(dev, vq, vq->desc[idx].addr,
548                                                 &dlen,
549                                                 VHOST_ACCESS_RO);
550                 if (unlikely(!descs))
551                         return -1;
552
553                 if (unlikely(dlen < vq->desc[idx].len)) {
554                         /*
555                          * The indirect desc table is not contiguous
556                          * in process VA space, we have to copy it.
557                          */
558                         idesc = vhost_alloc_copy_ind_table(dev, vq,
559                                         vq->desc[idx].addr, vq->desc[idx].len);
560                         if (unlikely(!idesc))
561                                 return -1;
562
563                         descs = idesc;
564                 }
565
566                 idx = 0;
567         }
568
569         while (1) {
570                 if (unlikely(idx >= nr_descs || cnt++ >= nr_descs)) {
571                         free_ind_table(idesc);
572                         return -1;
573                 }
574
575                 len += descs[idx].len;
576
577                 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
578                                                 descs[idx].addr, descs[idx].len,
579                                                 perm))) {
580                         free_ind_table(idesc);
581                         return -1;
582                 }
583
584                 if ((descs[idx].flags & VRING_DESC_F_NEXT) == 0)
585                         break;
586
587                 idx = descs[idx].next;
588         }
589
590         *desc_chain_len = len;
591         *vec_idx = vec_id;
592
593         if (unlikely(!!idesc))
594                 free_ind_table(idesc);
595
596         return 0;
597 }
598
599 /*
600  * Returns -1 on fail, 0 on success
601  */
602 static inline int
603 reserve_avail_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
604                                 uint32_t size, struct buf_vector *buf_vec,
605                                 uint16_t *num_buffers, uint16_t avail_head,
606                                 uint16_t *nr_vec)
607 {
608         uint16_t cur_idx;
609         uint16_t vec_idx = 0;
610         uint16_t max_tries, tries = 0;
611
612         uint16_t head_idx = 0;
613         uint32_t len = 0;
614
615         *num_buffers = 0;
616         cur_idx  = vq->last_avail_idx;
617
618         if (rxvq_is_mergeable(dev))
619                 max_tries = vq->size - 1;
620         else
621                 max_tries = 1;
622
623         while (size > 0) {
624                 if (unlikely(cur_idx == avail_head))
625                         return -1;
626                 /*
627                  * if we tried all available ring items, and still
628                  * can't get enough buf, it means something abnormal
629                  * happened.
630                  */
631                 if (unlikely(++tries > max_tries))
632                         return -1;
633
634                 if (unlikely(fill_vec_buf_split(dev, vq, cur_idx,
635                                                 &vec_idx, buf_vec,
636                                                 &head_idx, &len,
637                                                 VHOST_ACCESS_RW) < 0))
638                         return -1;
639                 len = RTE_MIN(len, size);
640                 update_shadow_used_ring_split(vq, head_idx, len);
641                 size -= len;
642
643                 cur_idx++;
644                 *num_buffers += 1;
645         }
646
647         *nr_vec = vec_idx;
648
649         return 0;
650 }
651
652 static __rte_always_inline int
653 fill_vec_buf_packed_indirect(struct virtio_net *dev,
654                         struct vhost_virtqueue *vq,
655                         struct vring_packed_desc *desc, uint16_t *vec_idx,
656                         struct buf_vector *buf_vec, uint32_t *len, uint8_t perm)
657 {
658         uint16_t i;
659         uint32_t nr_descs;
660         uint16_t vec_id = *vec_idx;
661         uint64_t dlen;
662         struct vring_packed_desc *descs, *idescs = NULL;
663
664         dlen = desc->len;
665         descs = (struct vring_packed_desc *)(uintptr_t)
666                 vhost_iova_to_vva(dev, vq, desc->addr, &dlen, VHOST_ACCESS_RO);
667         if (unlikely(!descs))
668                 return -1;
669
670         if (unlikely(dlen < desc->len)) {
671                 /*
672                  * The indirect desc table is not contiguous
673                  * in process VA space, we have to copy it.
674                  */
675                 idescs = vhost_alloc_copy_ind_table(dev,
676                                 vq, desc->addr, desc->len);
677                 if (unlikely(!idescs))
678                         return -1;
679
680                 descs = idescs;
681         }
682
683         nr_descs =  desc->len / sizeof(struct vring_packed_desc);
684         if (unlikely(nr_descs >= vq->size)) {
685                 free_ind_table(idescs);
686                 return -1;
687         }
688
689         for (i = 0; i < nr_descs; i++) {
690                 if (unlikely(vec_id >= BUF_VECTOR_MAX)) {
691                         free_ind_table(idescs);
692                         return -1;
693                 }
694
695                 *len += descs[i].len;
696                 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
697                                                 descs[i].addr, descs[i].len,
698                                                 perm)))
699                         return -1;
700         }
701         *vec_idx = vec_id;
702
703         if (unlikely(!!idescs))
704                 free_ind_table(idescs);
705
706         return 0;
707 }
708
709 static __rte_always_inline int
710 fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
711                                 uint16_t avail_idx, uint16_t *desc_count,
712                                 struct buf_vector *buf_vec, uint16_t *vec_idx,
713                                 uint16_t *buf_id, uint32_t *len, uint8_t perm)
714 {
715         bool wrap_counter = vq->avail_wrap_counter;
716         struct vring_packed_desc *descs = vq->desc_packed;
717         uint16_t vec_id = *vec_idx;
718
719         if (avail_idx < vq->last_avail_idx)
720                 wrap_counter ^= 1;
721
722         /*
723          * Perform a load-acquire barrier in desc_is_avail to
724          * enforce the ordering between desc flags and desc
725          * content.
726          */
727         if (unlikely(!desc_is_avail(&descs[avail_idx], wrap_counter)))
728                 return -1;
729
730         *desc_count = 0;
731         *len = 0;
732
733         while (1) {
734                 if (unlikely(vec_id >= BUF_VECTOR_MAX))
735                         return -1;
736
737                 if (unlikely(*desc_count >= vq->size))
738                         return -1;
739
740                 *desc_count += 1;
741                 *buf_id = descs[avail_idx].id;
742
743                 if (descs[avail_idx].flags & VRING_DESC_F_INDIRECT) {
744                         if (unlikely(fill_vec_buf_packed_indirect(dev, vq,
745                                                         &descs[avail_idx],
746                                                         &vec_id, buf_vec,
747                                                         len, perm) < 0))
748                                 return -1;
749                 } else {
750                         *len += descs[avail_idx].len;
751
752                         if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
753                                                         descs[avail_idx].addr,
754                                                         descs[avail_idx].len,
755                                                         perm)))
756                                 return -1;
757                 }
758
759                 if ((descs[avail_idx].flags & VRING_DESC_F_NEXT) == 0)
760                         break;
761
762                 if (++avail_idx >= vq->size) {
763                         avail_idx -= vq->size;
764                         wrap_counter ^= 1;
765                 }
766         }
767
768         *vec_idx = vec_id;
769
770         return 0;
771 }
772
773 static __rte_noinline void
774 copy_vnet_hdr_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
775                 struct buf_vector *buf_vec,
776                 struct virtio_net_hdr_mrg_rxbuf *hdr)
777 {
778         uint64_t len;
779         uint64_t remain = dev->vhost_hlen;
780         uint64_t src = (uint64_t)(uintptr_t)hdr, dst;
781         uint64_t iova = buf_vec->buf_iova;
782
783         while (remain) {
784                 len = RTE_MIN(remain,
785                                 buf_vec->buf_len);
786                 dst = buf_vec->buf_addr;
787                 rte_memcpy((void *)(uintptr_t)dst,
788                                 (void *)(uintptr_t)src,
789                                 len);
790
791                 PRINT_PACKET(dev, (uintptr_t)dst,
792                                 (uint32_t)len, 0);
793                 vhost_log_cache_write_iova(dev, vq,
794                                 iova, len);
795
796                 remain -= len;
797                 iova += len;
798                 src += len;
799                 buf_vec++;
800         }
801 }
802
803 static __rte_always_inline int
804 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
805                             struct rte_mbuf *m, struct buf_vector *buf_vec,
806                             uint16_t nr_vec, uint16_t num_buffers)
807 {
808         uint32_t vec_idx = 0;
809         uint32_t mbuf_offset, mbuf_avail;
810         uint32_t buf_offset, buf_avail;
811         uint64_t buf_addr, buf_iova, buf_len;
812         uint32_t cpy_len;
813         uint64_t hdr_addr;
814         struct rte_mbuf *hdr_mbuf;
815         struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
816         struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
817         int error = 0;
818
819         if (unlikely(m == NULL)) {
820                 error = -1;
821                 goto out;
822         }
823
824         buf_addr = buf_vec[vec_idx].buf_addr;
825         buf_iova = buf_vec[vec_idx].buf_iova;
826         buf_len = buf_vec[vec_idx].buf_len;
827
828         if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
829                 error = -1;
830                 goto out;
831         }
832
833         hdr_mbuf = m;
834         hdr_addr = buf_addr;
835         if (unlikely(buf_len < dev->vhost_hlen))
836                 hdr = &tmp_hdr;
837         else
838                 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
839
840         VHOST_LOG_DATA(DEBUG, "(%d) RX: num merge buffers %d\n",
841                 dev->vid, num_buffers);
842
843         if (unlikely(buf_len < dev->vhost_hlen)) {
844                 buf_offset = dev->vhost_hlen - buf_len;
845                 vec_idx++;
846                 buf_addr = buf_vec[vec_idx].buf_addr;
847                 buf_iova = buf_vec[vec_idx].buf_iova;
848                 buf_len = buf_vec[vec_idx].buf_len;
849                 buf_avail = buf_len - buf_offset;
850         } else {
851                 buf_offset = dev->vhost_hlen;
852                 buf_avail = buf_len - dev->vhost_hlen;
853         }
854
855         mbuf_avail  = rte_pktmbuf_data_len(m);
856         mbuf_offset = 0;
857         while (mbuf_avail != 0 || m->next != NULL) {
858                 /* done with current buf, get the next one */
859                 if (buf_avail == 0) {
860                         vec_idx++;
861                         if (unlikely(vec_idx >= nr_vec)) {
862                                 error = -1;
863                                 goto out;
864                         }
865
866                         buf_addr = buf_vec[vec_idx].buf_addr;
867                         buf_iova = buf_vec[vec_idx].buf_iova;
868                         buf_len = buf_vec[vec_idx].buf_len;
869
870                         buf_offset = 0;
871                         buf_avail  = buf_len;
872                 }
873
874                 /* done with current mbuf, get the next one */
875                 if (mbuf_avail == 0) {
876                         m = m->next;
877
878                         mbuf_offset = 0;
879                         mbuf_avail  = rte_pktmbuf_data_len(m);
880                 }
881
882                 if (hdr_addr) {
883                         virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
884                         if (rxvq_is_mergeable(dev))
885                                 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
886                                                 num_buffers);
887
888                         if (unlikely(hdr == &tmp_hdr)) {
889                                 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
890                         } else {
891                                 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
892                                                 dev->vhost_hlen, 0);
893                                 vhost_log_cache_write_iova(dev, vq,
894                                                 buf_vec[0].buf_iova,
895                                                 dev->vhost_hlen);
896                         }
897
898                         hdr_addr = 0;
899                 }
900
901                 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
902
903                 if (likely(cpy_len > MAX_BATCH_LEN ||
904                                         vq->batch_copy_nb_elems >= vq->size)) {
905                         rte_memcpy((void *)((uintptr_t)(buf_addr + buf_offset)),
906                                 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
907                                 cpy_len);
908                         vhost_log_cache_write_iova(dev, vq,
909                                                    buf_iova + buf_offset,
910                                                    cpy_len);
911                         PRINT_PACKET(dev, (uintptr_t)(buf_addr + buf_offset),
912                                 cpy_len, 0);
913                 } else {
914                         batch_copy[vq->batch_copy_nb_elems].dst =
915                                 (void *)((uintptr_t)(buf_addr + buf_offset));
916                         batch_copy[vq->batch_copy_nb_elems].src =
917                                 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
918                         batch_copy[vq->batch_copy_nb_elems].log_addr =
919                                 buf_iova + buf_offset;
920                         batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
921                         vq->batch_copy_nb_elems++;
922                 }
923
924                 mbuf_avail  -= cpy_len;
925                 mbuf_offset += cpy_len;
926                 buf_avail  -= cpy_len;
927                 buf_offset += cpy_len;
928         }
929
930 out:
931
932         return error;
933 }
934
935 static __rte_always_inline void
936 async_fill_vec(struct iovec *v, void *base, size_t len)
937 {
938         v->iov_base = base;
939         v->iov_len = len;
940 }
941
942 static __rte_always_inline void
943 async_fill_iter(struct rte_vhost_iov_iter *it, size_t count,
944         struct iovec *vec, unsigned long nr_seg)
945 {
946         it->offset = 0;
947         it->count = count;
948
949         if (count) {
950                 it->iov = vec;
951                 it->nr_segs = nr_seg;
952         } else {
953                 it->iov = 0;
954                 it->nr_segs = 0;
955         }
956 }
957
958 static __rte_always_inline void
959 async_fill_desc(struct rte_vhost_async_desc *desc,
960         struct rte_vhost_iov_iter *src, struct rte_vhost_iov_iter *dst)
961 {
962         desc->src = src;
963         desc->dst = dst;
964 }
965
966 static __rte_always_inline int
967 async_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
968                         struct rte_mbuf *m, struct buf_vector *buf_vec,
969                         uint16_t nr_vec, uint16_t num_buffers,
970                         struct iovec *src_iovec, struct iovec *dst_iovec,
971                         struct rte_vhost_iov_iter *src_it,
972                         struct rte_vhost_iov_iter *dst_it)
973 {
974         uint32_t vec_idx = 0;
975         uint32_t mbuf_offset, mbuf_avail;
976         uint32_t buf_offset, buf_avail;
977         uint64_t buf_addr, buf_iova, buf_len;
978         uint32_t cpy_len, cpy_threshold;
979         uint64_t hdr_addr;
980         struct rte_mbuf *hdr_mbuf;
981         struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
982         struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
983         int error = 0;
984         uint64_t mapped_len;
985
986         uint32_t tlen = 0;
987         int tvec_idx = 0;
988         void *hpa;
989
990         if (unlikely(m == NULL)) {
991                 error = -1;
992                 goto out;
993         }
994
995         cpy_threshold = vq->async_threshold;
996
997         buf_addr = buf_vec[vec_idx].buf_addr;
998         buf_iova = buf_vec[vec_idx].buf_iova;
999         buf_len = buf_vec[vec_idx].buf_len;
1000
1001         if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
1002                 error = -1;
1003                 goto out;
1004         }
1005
1006         hdr_mbuf = m;
1007         hdr_addr = buf_addr;
1008         if (unlikely(buf_len < dev->vhost_hlen))
1009                 hdr = &tmp_hdr;
1010         else
1011                 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
1012
1013         VHOST_LOG_DATA(DEBUG, "(%d) RX: num merge buffers %d\n",
1014                 dev->vid, num_buffers);
1015
1016         if (unlikely(buf_len < dev->vhost_hlen)) {
1017                 buf_offset = dev->vhost_hlen - buf_len;
1018                 vec_idx++;
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                 buf_avail = buf_len - buf_offset;
1023         } else {
1024                 buf_offset = dev->vhost_hlen;
1025                 buf_avail = buf_len - dev->vhost_hlen;
1026         }
1027
1028         mbuf_avail  = rte_pktmbuf_data_len(m);
1029         mbuf_offset = 0;
1030
1031         while (mbuf_avail != 0 || m->next != NULL) {
1032                 /* done with current buf, get the next one */
1033                 if (buf_avail == 0) {
1034                         vec_idx++;
1035                         if (unlikely(vec_idx >= nr_vec)) {
1036                                 error = -1;
1037                                 goto out;
1038                         }
1039
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
1044                         buf_offset = 0;
1045                         buf_avail  = buf_len;
1046                 }
1047
1048                 /* done with current mbuf, get the next one */
1049                 if (mbuf_avail == 0) {
1050                         m = m->next;
1051
1052                         mbuf_offset = 0;
1053                         mbuf_avail  = rte_pktmbuf_data_len(m);
1054                 }
1055
1056                 if (hdr_addr) {
1057                         virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
1058                         if (rxvq_is_mergeable(dev))
1059                                 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
1060                                                 num_buffers);
1061
1062                         if (unlikely(hdr == &tmp_hdr)) {
1063                                 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
1064                         } else {
1065                                 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
1066                                                 dev->vhost_hlen, 0);
1067                                 vhost_log_cache_write_iova(dev, vq,
1068                                                 buf_vec[0].buf_iova,
1069                                                 dev->vhost_hlen);
1070                         }
1071
1072                         hdr_addr = 0;
1073                 }
1074
1075                 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1076
1077                 while (unlikely(cpy_len && cpy_len >= cpy_threshold)) {
1078                         hpa = (void *)(uintptr_t)gpa_to_first_hpa(dev,
1079                                         buf_iova + buf_offset,
1080                                         cpy_len, &mapped_len);
1081
1082                         if (unlikely(!hpa || mapped_len < cpy_threshold))
1083                                 break;
1084
1085                         async_fill_vec(src_iovec + tvec_idx,
1086                                 (void *)(uintptr_t)rte_pktmbuf_iova_offset(m,
1087                                 mbuf_offset), (size_t)mapped_len);
1088
1089                         async_fill_vec(dst_iovec + tvec_idx,
1090                                         hpa, (size_t)mapped_len);
1091
1092                         tlen += (uint32_t)mapped_len;
1093                         cpy_len -= (uint32_t)mapped_len;
1094                         mbuf_avail  -= (uint32_t)mapped_len;
1095                         mbuf_offset += (uint32_t)mapped_len;
1096                         buf_avail  -= (uint32_t)mapped_len;
1097                         buf_offset += (uint32_t)mapped_len;
1098                         tvec_idx++;
1099                 }
1100
1101                 if (likely(cpy_len)) {
1102                         if (unlikely(vq->batch_copy_nb_elems >= vq->size)) {
1103                                 rte_memcpy(
1104                                 (void *)((uintptr_t)(buf_addr + buf_offset)),
1105                                 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
1106                                 cpy_len);
1107
1108                                 PRINT_PACKET(dev,
1109                                         (uintptr_t)(buf_addr + buf_offset),
1110                                         cpy_len, 0);
1111                         } else {
1112                                 batch_copy[vq->batch_copy_nb_elems].dst =
1113                                 (void *)((uintptr_t)(buf_addr + buf_offset));
1114                                 batch_copy[vq->batch_copy_nb_elems].src =
1115                                 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
1116                                 batch_copy[vq->batch_copy_nb_elems].log_addr =
1117                                         buf_iova + buf_offset;
1118                                 batch_copy[vq->batch_copy_nb_elems].len =
1119                                         cpy_len;
1120                                 vq->batch_copy_nb_elems++;
1121                         }
1122
1123                         mbuf_avail  -= cpy_len;
1124                         mbuf_offset += cpy_len;
1125                         buf_avail  -= cpy_len;
1126                         buf_offset += cpy_len;
1127                 }
1128
1129         }
1130
1131 out:
1132         async_fill_iter(src_it, tlen, src_iovec, tvec_idx);
1133         async_fill_iter(dst_it, tlen, dst_iovec, tvec_idx);
1134
1135         return error;
1136 }
1137
1138 static __rte_always_inline int
1139 vhost_enqueue_single_packed(struct virtio_net *dev,
1140                             struct vhost_virtqueue *vq,
1141                             struct rte_mbuf *pkt,
1142                             struct buf_vector *buf_vec,
1143                             uint16_t *nr_descs)
1144 {
1145         uint16_t nr_vec = 0;
1146         uint16_t avail_idx = vq->last_avail_idx;
1147         uint16_t max_tries, tries = 0;
1148         uint16_t buf_id = 0;
1149         uint32_t len = 0;
1150         uint16_t desc_count;
1151         uint32_t size = pkt->pkt_len + sizeof(struct virtio_net_hdr_mrg_rxbuf);
1152         uint16_t num_buffers = 0;
1153         uint32_t buffer_len[vq->size];
1154         uint16_t buffer_buf_id[vq->size];
1155         uint16_t buffer_desc_count[vq->size];
1156
1157         if (rxvq_is_mergeable(dev))
1158                 max_tries = vq->size - 1;
1159         else
1160                 max_tries = 1;
1161
1162         while (size > 0) {
1163                 /*
1164                  * if we tried all available ring items, and still
1165                  * can't get enough buf, it means something abnormal
1166                  * happened.
1167                  */
1168                 if (unlikely(++tries > max_tries))
1169                         return -1;
1170
1171                 if (unlikely(fill_vec_buf_packed(dev, vq,
1172                                                 avail_idx, &desc_count,
1173                                                 buf_vec, &nr_vec,
1174                                                 &buf_id, &len,
1175                                                 VHOST_ACCESS_RW) < 0))
1176                         return -1;
1177
1178                 len = RTE_MIN(len, size);
1179                 size -= len;
1180
1181                 buffer_len[num_buffers] = len;
1182                 buffer_buf_id[num_buffers] = buf_id;
1183                 buffer_desc_count[num_buffers] = desc_count;
1184                 num_buffers += 1;
1185
1186                 *nr_descs += desc_count;
1187                 avail_idx += desc_count;
1188                 if (avail_idx >= vq->size)
1189                         avail_idx -= vq->size;
1190         }
1191
1192         if (copy_mbuf_to_desc(dev, vq, pkt, buf_vec, nr_vec, num_buffers) < 0)
1193                 return -1;
1194
1195         vhost_shadow_enqueue_single_packed(dev, vq, buffer_len, buffer_buf_id,
1196                                            buffer_desc_count, num_buffers);
1197
1198         return 0;
1199 }
1200
1201 static __rte_noinline uint32_t
1202 virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1203         struct rte_mbuf **pkts, uint32_t count)
1204 {
1205         uint32_t pkt_idx = 0;
1206         uint16_t num_buffers;
1207         struct buf_vector buf_vec[BUF_VECTOR_MAX];
1208         uint16_t avail_head;
1209
1210         /*
1211          * The ordering between avail index and
1212          * desc reads needs to be enforced.
1213          */
1214         avail_head = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE);
1215
1216         rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1217
1218         for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1219                 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1220                 uint16_t nr_vec = 0;
1221
1222                 if (unlikely(reserve_avail_buf_split(dev, vq,
1223                                                 pkt_len, buf_vec, &num_buffers,
1224                                                 avail_head, &nr_vec) < 0)) {
1225                         VHOST_LOG_DATA(DEBUG,
1226                                 "(%d) failed to get enough desc from vring\n",
1227                                 dev->vid);
1228                         vq->shadow_used_idx -= num_buffers;
1229                         break;
1230                 }
1231
1232                 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1233                         dev->vid, vq->last_avail_idx,
1234                         vq->last_avail_idx + num_buffers);
1235
1236                 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
1237                                                 buf_vec, nr_vec,
1238                                                 num_buffers) < 0) {
1239                         vq->shadow_used_idx -= num_buffers;
1240                         break;
1241                 }
1242
1243                 vq->last_avail_idx += num_buffers;
1244         }
1245
1246         do_data_copy_enqueue(dev, vq);
1247
1248         if (likely(vq->shadow_used_idx)) {
1249                 flush_shadow_used_ring_split(dev, vq);
1250                 vhost_vring_call_split(dev, vq);
1251         }
1252
1253         return pkt_idx;
1254 }
1255
1256 static __rte_always_inline int
1257 virtio_dev_rx_batch_packed(struct virtio_net *dev,
1258                            struct vhost_virtqueue *vq,
1259                            struct rte_mbuf **pkts)
1260 {
1261         bool wrap_counter = vq->avail_wrap_counter;
1262         struct vring_packed_desc *descs = vq->desc_packed;
1263         uint16_t avail_idx = vq->last_avail_idx;
1264         uint64_t desc_addrs[PACKED_BATCH_SIZE];
1265         struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE];
1266         uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
1267         uint64_t lens[PACKED_BATCH_SIZE];
1268         uint16_t ids[PACKED_BATCH_SIZE];
1269         uint16_t i;
1270
1271         if (unlikely(avail_idx & PACKED_BATCH_MASK))
1272                 return -1;
1273
1274         if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
1275                 return -1;
1276
1277         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1278                 if (unlikely(pkts[i]->next != NULL))
1279                         return -1;
1280                 if (unlikely(!desc_is_avail(&descs[avail_idx + i],
1281                                             wrap_counter)))
1282                         return -1;
1283         }
1284
1285         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1286                 lens[i] = descs[avail_idx + i].len;
1287
1288         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1289                 if (unlikely(pkts[i]->pkt_len > (lens[i] - buf_offset)))
1290                         return -1;
1291         }
1292
1293         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1294                 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
1295                                                   descs[avail_idx + i].addr,
1296                                                   &lens[i],
1297                                                   VHOST_ACCESS_RW);
1298
1299         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1300                 if (unlikely(!desc_addrs[i]))
1301                         return -1;
1302                 if (unlikely(lens[i] != descs[avail_idx + i].len))
1303                         return -1;
1304         }
1305
1306         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1307                 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
1308                 hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *)
1309                                         (uintptr_t)desc_addrs[i];
1310                 lens[i] = pkts[i]->pkt_len +
1311                         sizeof(struct virtio_net_hdr_mrg_rxbuf);
1312         }
1313
1314         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1315                 virtio_enqueue_offload(pkts[i], &hdrs[i]->hdr);
1316
1317         vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
1318
1319         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
1320                 rte_memcpy((void *)(uintptr_t)(desc_addrs[i] + buf_offset),
1321                            rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
1322                            pkts[i]->pkt_len);
1323         }
1324
1325         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1326                 vhost_log_cache_write_iova(dev, vq, descs[avail_idx + i].addr,
1327                                            lens[i]);
1328
1329         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
1330                 ids[i] = descs[avail_idx + i].id;
1331
1332         vhost_flush_enqueue_batch_packed(dev, vq, lens, ids);
1333
1334         return 0;
1335 }
1336
1337 static __rte_always_inline int16_t
1338 virtio_dev_rx_single_packed(struct virtio_net *dev,
1339                             struct vhost_virtqueue *vq,
1340                             struct rte_mbuf *pkt)
1341 {
1342         struct buf_vector buf_vec[BUF_VECTOR_MAX];
1343         uint16_t nr_descs = 0;
1344
1345         if (unlikely(vhost_enqueue_single_packed(dev, vq, pkt, buf_vec,
1346                                                  &nr_descs) < 0)) {
1347                 VHOST_LOG_DATA(DEBUG,
1348                                 "(%d) failed to get enough desc from vring\n",
1349                                 dev->vid);
1350                 return -1;
1351         }
1352
1353         VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1354                         dev->vid, vq->last_avail_idx,
1355                         vq->last_avail_idx + nr_descs);
1356
1357         vq_inc_last_avail_packed(vq, nr_descs);
1358
1359         return 0;
1360 }
1361
1362 static __rte_noinline uint32_t
1363 virtio_dev_rx_packed(struct virtio_net *dev,
1364                      struct vhost_virtqueue *__rte_restrict vq,
1365                      struct rte_mbuf **__rte_restrict pkts,
1366                      uint32_t count)
1367 {
1368         uint32_t pkt_idx = 0;
1369         uint32_t remained = count;
1370
1371         do {
1372                 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1373
1374                 if (remained >= PACKED_BATCH_SIZE) {
1375                         if (!virtio_dev_rx_batch_packed(dev, vq,
1376                                                         &pkts[pkt_idx])) {
1377                                 pkt_idx += PACKED_BATCH_SIZE;
1378                                 remained -= PACKED_BATCH_SIZE;
1379                                 continue;
1380                         }
1381                 }
1382
1383                 if (virtio_dev_rx_single_packed(dev, vq, pkts[pkt_idx]))
1384                         break;
1385                 pkt_idx++;
1386                 remained--;
1387
1388         } while (pkt_idx < count);
1389
1390         if (vq->shadow_used_idx) {
1391                 do_data_copy_enqueue(dev, vq);
1392                 vhost_flush_enqueue_shadow_packed(dev, vq);
1393         }
1394
1395         if (pkt_idx)
1396                 vhost_vring_call_packed(dev, vq);
1397
1398         return pkt_idx;
1399 }
1400
1401 static __rte_always_inline uint32_t
1402 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
1403         struct rte_mbuf **pkts, uint32_t count)
1404 {
1405         struct vhost_virtqueue *vq;
1406         uint32_t nb_tx = 0;
1407
1408         VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
1409         if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1410                 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
1411                         dev->vid, __func__, queue_id);
1412                 return 0;
1413         }
1414
1415         vq = dev->virtqueue[queue_id];
1416
1417         rte_spinlock_lock(&vq->access_lock);
1418
1419         if (unlikely(vq->enabled == 0))
1420                 goto out_access_unlock;
1421
1422         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1423                 vhost_user_iotlb_rd_lock(vq);
1424
1425         if (unlikely(vq->access_ok == 0))
1426                 if (unlikely(vring_translate(dev, vq) < 0))
1427                         goto out;
1428
1429         count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
1430         if (count == 0)
1431                 goto out;
1432
1433         if (vq_is_packed(dev))
1434                 nb_tx = virtio_dev_rx_packed(dev, vq, pkts, count);
1435         else
1436                 nb_tx = virtio_dev_rx_split(dev, vq, pkts, count);
1437
1438 out:
1439         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1440                 vhost_user_iotlb_rd_unlock(vq);
1441
1442 out_access_unlock:
1443         rte_spinlock_unlock(&vq->access_lock);
1444
1445         return nb_tx;
1446 }
1447
1448 uint16_t
1449 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
1450         struct rte_mbuf **__rte_restrict pkts, uint16_t count)
1451 {
1452         struct virtio_net *dev = get_device(vid);
1453
1454         if (!dev)
1455                 return 0;
1456
1457         if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1458                 VHOST_LOG_DATA(ERR,
1459                         "(%d) %s: built-in vhost net backend is disabled.\n",
1460                         dev->vid, __func__);
1461                 return 0;
1462         }
1463
1464         return virtio_dev_rx(dev, queue_id, pkts, count);
1465 }
1466
1467 static __rte_always_inline uint16_t
1468 virtio_dev_rx_async_get_info_idx(uint16_t pkts_idx,
1469         uint16_t vq_size, uint16_t n_inflight)
1470 {
1471         return pkts_idx > n_inflight ? (pkts_idx - n_inflight) :
1472                 (vq_size - n_inflight + pkts_idx) & (vq_size - 1);
1473 }
1474
1475 static __rte_noinline uint32_t
1476 virtio_dev_rx_async_submit_split(struct virtio_net *dev,
1477         struct vhost_virtqueue *vq, uint16_t queue_id,
1478         struct rte_mbuf **pkts, uint32_t count)
1479 {
1480         uint32_t pkt_idx = 0, pkt_burst_idx = 0;
1481         uint16_t num_buffers;
1482         struct buf_vector buf_vec[BUF_VECTOR_MAX];
1483         uint16_t avail_head;
1484
1485         struct rte_vhost_iov_iter *it_pool = vq->it_pool;
1486         struct iovec *vec_pool = vq->vec_pool;
1487         struct rte_vhost_async_desc tdes[MAX_PKT_BURST];
1488         struct iovec *src_iovec = vec_pool;
1489         struct iovec *dst_iovec = vec_pool + (VHOST_MAX_ASYNC_VEC >> 1);
1490         struct rte_vhost_iov_iter *src_it = it_pool;
1491         struct rte_vhost_iov_iter *dst_it = it_pool + 1;
1492         uint16_t n_free_slot, slot_idx = 0;
1493         uint16_t pkt_err = 0;
1494         uint16_t segs_await = 0;
1495         struct async_inflight_info *pkts_info = vq->async_pkts_info;
1496         int n_pkts = 0;
1497
1498         /*
1499          * The ordering between avail index and desc reads need to be enforced.
1500          */
1501         avail_head = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE);
1502
1503         rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1504
1505         for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
1506                 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
1507                 uint16_t nr_vec = 0;
1508
1509                 if (unlikely(reserve_avail_buf_split(dev, vq,
1510                                                 pkt_len, buf_vec, &num_buffers,
1511                                                 avail_head, &nr_vec) < 0)) {
1512                         VHOST_LOG_DATA(DEBUG,
1513                                 "(%d) failed to get enough desc from vring\n",
1514                                 dev->vid);
1515                         vq->shadow_used_idx -= num_buffers;
1516                         break;
1517                 }
1518
1519                 VHOST_LOG_DATA(DEBUG, "(%d) current index %d | end index %d\n",
1520                         dev->vid, vq->last_avail_idx,
1521                         vq->last_avail_idx + num_buffers);
1522
1523                 if (async_mbuf_to_desc(dev, vq, pkts[pkt_idx],
1524                                 buf_vec, nr_vec, num_buffers,
1525                                 src_iovec, dst_iovec, src_it, dst_it) < 0) {
1526                         vq->shadow_used_idx -= num_buffers;
1527                         break;
1528                 }
1529
1530                 slot_idx = (vq->async_pkts_idx + pkt_idx) & (vq->size - 1);
1531                 if (src_it->count) {
1532                         async_fill_desc(&tdes[pkt_burst_idx], src_it, dst_it);
1533                         pkt_burst_idx++;
1534                         pkts_info[slot_idx].descs = num_buffers;
1535                         pkts_info[slot_idx].segs = src_it->nr_segs;
1536                         src_iovec += src_it->nr_segs;
1537                         dst_iovec += dst_it->nr_segs;
1538                         src_it += 2;
1539                         dst_it += 2;
1540                         segs_await += src_it->nr_segs;
1541                 } else {
1542                         pkts_info[slot_idx].info = num_buffers;
1543                         vq->async_pkts_inflight_n++;
1544                 }
1545
1546                 vq->last_avail_idx += num_buffers;
1547
1548                 /*
1549                  * conditions to trigger async device transfer:
1550                  * - buffered packet number reaches transfer threshold
1551                  * - this is the last packet in the burst enqueue
1552                  * - unused async iov number is less than max vhost vector
1553                  */
1554                 if (pkt_burst_idx >= VHOST_ASYNC_BATCH_THRESHOLD ||
1555                         (pkt_idx == count - 1 && pkt_burst_idx) ||
1556                         (VHOST_MAX_ASYNC_VEC / 2 - segs_await <
1557                         BUF_VECTOR_MAX)) {
1558                         n_pkts = vq->async_ops.transfer_data(dev->vid,
1559                                         queue_id, tdes, 0, pkt_burst_idx);
1560                         src_iovec = vec_pool;
1561                         dst_iovec = vec_pool + (VHOST_MAX_ASYNC_VEC >> 1);
1562                         src_it = it_pool;
1563                         dst_it = it_pool + 1;
1564                         segs_await = 0;
1565                         vq->async_pkts_inflight_n += pkt_burst_idx;
1566
1567                         if (unlikely(n_pkts < (int)pkt_burst_idx)) {
1568                                 /*
1569                                  * log error packets number here and do actual
1570                                  * error processing when applications poll
1571                                  * completion
1572                                  */
1573                                 pkt_err = pkt_burst_idx - n_pkts;
1574                                 pkt_burst_idx = 0;
1575                                 break;
1576                         }
1577
1578                         pkt_burst_idx = 0;
1579                 }
1580         }
1581
1582         if (pkt_burst_idx) {
1583                 n_pkts = vq->async_ops.transfer_data(dev->vid,
1584                                 queue_id, tdes, 0, pkt_burst_idx);
1585                 vq->async_pkts_inflight_n += pkt_burst_idx;
1586
1587                 if (unlikely(n_pkts < (int)pkt_burst_idx))
1588                         pkt_err = pkt_burst_idx - n_pkts;
1589         }
1590
1591         do_data_copy_enqueue(dev, vq);
1592
1593         while (unlikely(pkt_err && pkt_idx)) {
1594                 if (pkts_info[slot_idx].segs)
1595                         pkt_err--;
1596                 vq->last_avail_idx -= pkts_info[slot_idx].descs;
1597                 vq->shadow_used_idx -= pkts_info[slot_idx].descs;
1598                 vq->async_pkts_inflight_n--;
1599                 slot_idx = (slot_idx - 1) & (vq->size - 1);
1600                 pkt_idx--;
1601         }
1602
1603         n_free_slot = vq->size - vq->async_pkts_idx;
1604         if (n_free_slot > pkt_idx) {
1605                 rte_memcpy(&vq->async_pkts_pending[vq->async_pkts_idx],
1606                         pkts, pkt_idx * sizeof(uintptr_t));
1607                 vq->async_pkts_idx += pkt_idx;
1608         } else {
1609                 rte_memcpy(&vq->async_pkts_pending[vq->async_pkts_idx],
1610                         pkts, n_free_slot * sizeof(uintptr_t));
1611                 rte_memcpy(&vq->async_pkts_pending[0],
1612                         &pkts[n_free_slot],
1613                         (pkt_idx - n_free_slot) * sizeof(uintptr_t));
1614                 vq->async_pkts_idx = pkt_idx - n_free_slot;
1615         }
1616
1617         if (likely(vq->shadow_used_idx))
1618                 async_flush_shadow_used_ring_split(dev, vq);
1619
1620         return pkt_idx;
1621 }
1622
1623 uint16_t rte_vhost_poll_enqueue_completed(int vid, uint16_t queue_id,
1624                 struct rte_mbuf **pkts, uint16_t count)
1625 {
1626         struct virtio_net *dev = get_device(vid);
1627         struct vhost_virtqueue *vq;
1628         uint16_t n_pkts_cpl = 0, n_pkts_put = 0, n_descs = 0;
1629         uint16_t start_idx, pkts_idx, vq_size;
1630         uint16_t n_inflight;
1631         struct async_inflight_info *pkts_info;
1632
1633         if (!dev)
1634                 return 0;
1635
1636         VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
1637         if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1638                 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
1639                         dev->vid, __func__, queue_id);
1640                 return 0;
1641         }
1642
1643         vq = dev->virtqueue[queue_id];
1644
1645         if (unlikely(!vq->async_registered)) {
1646                 VHOST_LOG_DATA(ERR, "(%d) %s: async not registered for queue id %d.\n",
1647                         dev->vid, __func__, queue_id);
1648                 return 0;
1649         }
1650
1651         rte_spinlock_lock(&vq->access_lock);
1652
1653         n_inflight = vq->async_pkts_inflight_n;
1654         pkts_idx = vq->async_pkts_idx;
1655         pkts_info = vq->async_pkts_info;
1656         vq_size = vq->size;
1657         start_idx = virtio_dev_rx_async_get_info_idx(pkts_idx,
1658                 vq_size, vq->async_pkts_inflight_n);
1659
1660         if (count > vq->async_last_pkts_n)
1661                 n_pkts_cpl = vq->async_ops.check_completed_copies(vid,
1662                         queue_id, 0, count - vq->async_last_pkts_n);
1663         n_pkts_cpl += vq->async_last_pkts_n;
1664
1665         rte_smp_wmb();
1666
1667         while (likely((n_pkts_put < count) && n_inflight)) {
1668                 uint16_t info_idx = (start_idx + n_pkts_put) & (vq_size - 1);
1669                 if (n_pkts_cpl && pkts_info[info_idx].segs)
1670                         n_pkts_cpl--;
1671                 else if (!n_pkts_cpl && pkts_info[info_idx].segs)
1672                         break;
1673                 n_pkts_put++;
1674                 n_inflight--;
1675                 n_descs += pkts_info[info_idx].descs;
1676         }
1677
1678         vq->async_last_pkts_n = n_pkts_cpl;
1679
1680         if (n_pkts_put) {
1681                 vq->async_pkts_inflight_n = n_inflight;
1682                 if (likely(vq->enabled && vq->access_ok)) {
1683                         __atomic_add_fetch(&vq->used->idx,
1684                                         n_descs, __ATOMIC_RELEASE);
1685                         vhost_vring_call_split(dev, vq);
1686                 }
1687
1688                 if (start_idx + n_pkts_put <= vq_size) {
1689                         rte_memcpy(pkts, &vq->async_pkts_pending[start_idx],
1690                                 n_pkts_put * sizeof(uintptr_t));
1691                 } else {
1692                         rte_memcpy(pkts, &vq->async_pkts_pending[start_idx],
1693                                 (vq_size - start_idx) * sizeof(uintptr_t));
1694                         rte_memcpy(&pkts[vq_size - start_idx],
1695                                 vq->async_pkts_pending,
1696                                 (n_pkts_put + start_idx - vq_size) *
1697                                 sizeof(uintptr_t));
1698                 }
1699         }
1700
1701         rte_spinlock_unlock(&vq->access_lock);
1702
1703         return n_pkts_put;
1704 }
1705
1706 static __rte_always_inline uint32_t
1707 virtio_dev_rx_async_submit(struct virtio_net *dev, uint16_t queue_id,
1708         struct rte_mbuf **pkts, uint32_t count)
1709 {
1710         struct vhost_virtqueue *vq;
1711         uint32_t nb_tx = 0;
1712
1713         VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
1714         if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
1715                 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
1716                         dev->vid, __func__, queue_id);
1717                 return 0;
1718         }
1719
1720         vq = dev->virtqueue[queue_id];
1721
1722         rte_spinlock_lock(&vq->access_lock);
1723
1724         if (unlikely(vq->enabled == 0 || !vq->async_registered))
1725                 goto out_access_unlock;
1726
1727         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1728                 vhost_user_iotlb_rd_lock(vq);
1729
1730         if (unlikely(vq->access_ok == 0))
1731                 if (unlikely(vring_translate(dev, vq) < 0))
1732                         goto out;
1733
1734         count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
1735         if (count == 0)
1736                 goto out;
1737
1738         /* TODO: packed queue not implemented */
1739         if (vq_is_packed(dev))
1740                 nb_tx = 0;
1741         else
1742                 nb_tx = virtio_dev_rx_async_submit_split(dev,
1743                                 vq, queue_id, pkts, count);
1744
1745 out:
1746         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1747                 vhost_user_iotlb_rd_unlock(vq);
1748
1749 out_access_unlock:
1750         rte_spinlock_unlock(&vq->access_lock);
1751
1752         return nb_tx;
1753 }
1754
1755 uint16_t
1756 rte_vhost_submit_enqueue_burst(int vid, uint16_t queue_id,
1757                 struct rte_mbuf **pkts, uint16_t count)
1758 {
1759         struct virtio_net *dev = get_device(vid);
1760
1761         if (!dev)
1762                 return 0;
1763
1764         if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1765                 VHOST_LOG_DATA(ERR,
1766                         "(%d) %s: built-in vhost net backend is disabled.\n",
1767                         dev->vid, __func__);
1768                 return 0;
1769         }
1770
1771         return virtio_dev_rx_async_submit(dev, queue_id, pkts, count);
1772 }
1773
1774 static inline bool
1775 virtio_net_with_host_offload(struct virtio_net *dev)
1776 {
1777         if (dev->features &
1778                         ((1ULL << VIRTIO_NET_F_CSUM) |
1779                          (1ULL << VIRTIO_NET_F_HOST_ECN) |
1780                          (1ULL << VIRTIO_NET_F_HOST_TSO4) |
1781                          (1ULL << VIRTIO_NET_F_HOST_TSO6) |
1782                          (1ULL << VIRTIO_NET_F_HOST_UFO)))
1783                 return true;
1784
1785         return false;
1786 }
1787
1788 static void
1789 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
1790 {
1791         struct rte_ipv4_hdr *ipv4_hdr;
1792         struct rte_ipv6_hdr *ipv6_hdr;
1793         void *l3_hdr = NULL;
1794         struct rte_ether_hdr *eth_hdr;
1795         uint16_t ethertype;
1796
1797         eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
1798
1799         m->l2_len = sizeof(struct rte_ether_hdr);
1800         ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
1801
1802         if (ethertype == RTE_ETHER_TYPE_VLAN) {
1803                 struct rte_vlan_hdr *vlan_hdr =
1804                         (struct rte_vlan_hdr *)(eth_hdr + 1);
1805
1806                 m->l2_len += sizeof(struct rte_vlan_hdr);
1807                 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
1808         }
1809
1810         l3_hdr = (char *)eth_hdr + m->l2_len;
1811
1812         switch (ethertype) {
1813         case RTE_ETHER_TYPE_IPV4:
1814                 ipv4_hdr = l3_hdr;
1815                 *l4_proto = ipv4_hdr->next_proto_id;
1816                 m->l3_len = rte_ipv4_hdr_len(ipv4_hdr);
1817                 *l4_hdr = (char *)l3_hdr + m->l3_len;
1818                 m->ol_flags |= PKT_TX_IPV4;
1819                 break;
1820         case RTE_ETHER_TYPE_IPV6:
1821                 ipv6_hdr = l3_hdr;
1822                 *l4_proto = ipv6_hdr->proto;
1823                 m->l3_len = sizeof(struct rte_ipv6_hdr);
1824                 *l4_hdr = (char *)l3_hdr + m->l3_len;
1825                 m->ol_flags |= PKT_TX_IPV6;
1826                 break;
1827         default:
1828                 m->l3_len = 0;
1829                 *l4_proto = 0;
1830                 *l4_hdr = NULL;
1831                 break;
1832         }
1833 }
1834
1835 static __rte_always_inline void
1836 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
1837 {
1838         uint16_t l4_proto = 0;
1839         void *l4_hdr = NULL;
1840         struct rte_tcp_hdr *tcp_hdr = NULL;
1841
1842         if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
1843                 return;
1844
1845         parse_ethernet(m, &l4_proto, &l4_hdr);
1846         if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1847                 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
1848                         switch (hdr->csum_offset) {
1849                         case (offsetof(struct rte_tcp_hdr, cksum)):
1850                                 if (l4_proto == IPPROTO_TCP)
1851                                         m->ol_flags |= PKT_TX_TCP_CKSUM;
1852                                 break;
1853                         case (offsetof(struct rte_udp_hdr, dgram_cksum)):
1854                                 if (l4_proto == IPPROTO_UDP)
1855                                         m->ol_flags |= PKT_TX_UDP_CKSUM;
1856                                 break;
1857                         case (offsetof(struct rte_sctp_hdr, cksum)):
1858                                 if (l4_proto == IPPROTO_SCTP)
1859                                         m->ol_flags |= PKT_TX_SCTP_CKSUM;
1860                                 break;
1861                         default:
1862                                 break;
1863                         }
1864                 }
1865         }
1866
1867         if (l4_hdr && hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1868                 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1869                 case VIRTIO_NET_HDR_GSO_TCPV4:
1870                 case VIRTIO_NET_HDR_GSO_TCPV6:
1871                         tcp_hdr = l4_hdr;
1872                         m->ol_flags |= PKT_TX_TCP_SEG;
1873                         m->tso_segsz = hdr->gso_size;
1874                         m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
1875                         break;
1876                 case VIRTIO_NET_HDR_GSO_UDP:
1877                         m->ol_flags |= PKT_TX_UDP_SEG;
1878                         m->tso_segsz = hdr->gso_size;
1879                         m->l4_len = sizeof(struct rte_udp_hdr);
1880                         break;
1881                 default:
1882                         VHOST_LOG_DATA(WARNING,
1883                                 "unsupported gso type %u.\n", hdr->gso_type);
1884                         break;
1885                 }
1886         }
1887 }
1888
1889 static __rte_noinline void
1890 copy_vnet_hdr_from_desc(struct virtio_net_hdr *hdr,
1891                 struct buf_vector *buf_vec)
1892 {
1893         uint64_t len;
1894         uint64_t remain = sizeof(struct virtio_net_hdr);
1895         uint64_t src;
1896         uint64_t dst = (uint64_t)(uintptr_t)hdr;
1897
1898         while (remain) {
1899                 len = RTE_MIN(remain, buf_vec->buf_len);
1900                 src = buf_vec->buf_addr;
1901                 rte_memcpy((void *)(uintptr_t)dst,
1902                                 (void *)(uintptr_t)src, len);
1903
1904                 remain -= len;
1905                 dst += len;
1906                 buf_vec++;
1907         }
1908 }
1909
1910 static __rte_always_inline int
1911 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
1912                   struct buf_vector *buf_vec, uint16_t nr_vec,
1913                   struct rte_mbuf *m, struct rte_mempool *mbuf_pool)
1914 {
1915         uint32_t buf_avail, buf_offset;
1916         uint64_t buf_addr, buf_len;
1917         uint32_t mbuf_avail, mbuf_offset;
1918         uint32_t cpy_len;
1919         struct rte_mbuf *cur = m, *prev = m;
1920         struct virtio_net_hdr tmp_hdr;
1921         struct virtio_net_hdr *hdr = NULL;
1922         /* A counter to avoid desc dead loop chain */
1923         uint16_t vec_idx = 0;
1924         struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
1925         int error = 0;
1926
1927         buf_addr = buf_vec[vec_idx].buf_addr;
1928         buf_len = buf_vec[vec_idx].buf_len;
1929
1930         if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
1931                 error = -1;
1932                 goto out;
1933         }
1934
1935         if (virtio_net_with_host_offload(dev)) {
1936                 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
1937                         /*
1938                          * No luck, the virtio-net header doesn't fit
1939                          * in a contiguous virtual area.
1940                          */
1941                         copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
1942                         hdr = &tmp_hdr;
1943                 } else {
1944                         hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
1945                 }
1946         }
1947
1948         /*
1949          * A virtio driver normally uses at least 2 desc buffers
1950          * for Tx: the first for storing the header, and others
1951          * for storing the data.
1952          */
1953         if (unlikely(buf_len < dev->vhost_hlen)) {
1954                 buf_offset = dev->vhost_hlen - buf_len;
1955                 vec_idx++;
1956                 buf_addr = buf_vec[vec_idx].buf_addr;
1957                 buf_len = buf_vec[vec_idx].buf_len;
1958                 buf_avail  = buf_len - buf_offset;
1959         } else if (buf_len == dev->vhost_hlen) {
1960                 if (unlikely(++vec_idx >= nr_vec))
1961                         goto out;
1962                 buf_addr = buf_vec[vec_idx].buf_addr;
1963                 buf_len = buf_vec[vec_idx].buf_len;
1964
1965                 buf_offset = 0;
1966                 buf_avail = buf_len;
1967         } else {
1968                 buf_offset = dev->vhost_hlen;
1969                 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
1970         }
1971
1972         PRINT_PACKET(dev,
1973                         (uintptr_t)(buf_addr + buf_offset),
1974                         (uint32_t)buf_avail, 0);
1975
1976         mbuf_offset = 0;
1977         mbuf_avail  = m->buf_len - RTE_PKTMBUF_HEADROOM;
1978         while (1) {
1979                 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1980
1981                 if (likely(cpy_len > MAX_BATCH_LEN ||
1982                                         vq->batch_copy_nb_elems >= vq->size ||
1983                                         (hdr && cur == m))) {
1984                         rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
1985                                                 mbuf_offset),
1986                                         (void *)((uintptr_t)(buf_addr +
1987                                                         buf_offset)), cpy_len);
1988                 } else {
1989                         batch_copy[vq->batch_copy_nb_elems].dst =
1990                                 rte_pktmbuf_mtod_offset(cur, void *,
1991                                                 mbuf_offset);
1992                         batch_copy[vq->batch_copy_nb_elems].src =
1993                                 (void *)((uintptr_t)(buf_addr + buf_offset));
1994                         batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
1995                         vq->batch_copy_nb_elems++;
1996                 }
1997
1998                 mbuf_avail  -= cpy_len;
1999                 mbuf_offset += cpy_len;
2000                 buf_avail -= cpy_len;
2001                 buf_offset += cpy_len;
2002
2003                 /* This buf reaches to its end, get the next one */
2004                 if (buf_avail == 0) {
2005                         if (++vec_idx >= nr_vec)
2006                                 break;
2007
2008                         buf_addr = buf_vec[vec_idx].buf_addr;
2009                         buf_len = buf_vec[vec_idx].buf_len;
2010
2011                         buf_offset = 0;
2012                         buf_avail  = buf_len;
2013
2014                         PRINT_PACKET(dev, (uintptr_t)buf_addr,
2015                                         (uint32_t)buf_avail, 0);
2016                 }
2017
2018                 /*
2019                  * This mbuf reaches to its end, get a new one
2020                  * to hold more data.
2021                  */
2022                 if (mbuf_avail == 0) {
2023                         cur = rte_pktmbuf_alloc(mbuf_pool);
2024                         if (unlikely(cur == NULL)) {
2025                                 VHOST_LOG_DATA(ERR, "Failed to "
2026                                         "allocate memory for mbuf.\n");
2027                                 error = -1;
2028                                 goto out;
2029                         }
2030
2031                         prev->next = cur;
2032                         prev->data_len = mbuf_offset;
2033                         m->nb_segs += 1;
2034                         m->pkt_len += mbuf_offset;
2035                         prev = cur;
2036
2037                         mbuf_offset = 0;
2038                         mbuf_avail  = cur->buf_len - RTE_PKTMBUF_HEADROOM;
2039                 }
2040         }
2041
2042         prev->data_len = mbuf_offset;
2043         m->pkt_len    += mbuf_offset;
2044
2045         if (hdr)
2046                 vhost_dequeue_offload(hdr, m);
2047
2048 out:
2049
2050         return error;
2051 }
2052
2053 static void
2054 virtio_dev_extbuf_free(void *addr __rte_unused, void *opaque)
2055 {
2056         rte_free(opaque);
2057 }
2058
2059 static int
2060 virtio_dev_extbuf_alloc(struct rte_mbuf *pkt, uint32_t size)
2061 {
2062         struct rte_mbuf_ext_shared_info *shinfo = NULL;
2063         uint32_t total_len = RTE_PKTMBUF_HEADROOM + size;
2064         uint16_t buf_len;
2065         rte_iova_t iova;
2066         void *buf;
2067
2068         total_len += sizeof(*shinfo) + sizeof(uintptr_t);
2069         total_len = RTE_ALIGN_CEIL(total_len, sizeof(uintptr_t));
2070
2071         if (unlikely(total_len > UINT16_MAX))
2072                 return -ENOSPC;
2073
2074         buf_len = total_len;
2075         buf = rte_malloc(NULL, buf_len, RTE_CACHE_LINE_SIZE);
2076         if (unlikely(buf == NULL))
2077                 return -ENOMEM;
2078
2079         /* Initialize shinfo */
2080         shinfo = rte_pktmbuf_ext_shinfo_init_helper(buf, &buf_len,
2081                                                 virtio_dev_extbuf_free, buf);
2082         if (unlikely(shinfo == NULL)) {
2083                 rte_free(buf);
2084                 VHOST_LOG_DATA(ERR, "Failed to init shinfo\n");
2085                 return -1;
2086         }
2087
2088         iova = rte_malloc_virt2iova(buf);
2089         rte_pktmbuf_attach_extbuf(pkt, buf, iova, buf_len, shinfo);
2090         rte_pktmbuf_reset_headroom(pkt);
2091
2092         return 0;
2093 }
2094
2095 /*
2096  * Allocate a host supported pktmbuf.
2097  */
2098 static __rte_always_inline struct rte_mbuf *
2099 virtio_dev_pktmbuf_alloc(struct virtio_net *dev, struct rte_mempool *mp,
2100                          uint32_t data_len)
2101 {
2102         struct rte_mbuf *pkt = rte_pktmbuf_alloc(mp);
2103
2104         if (unlikely(pkt == NULL)) {
2105                 VHOST_LOG_DATA(ERR,
2106                         "Failed to allocate memory for mbuf.\n");
2107                 return NULL;
2108         }
2109
2110         if (rte_pktmbuf_tailroom(pkt) >= data_len)
2111                 return pkt;
2112
2113         /* attach an external buffer if supported */
2114         if (dev->extbuf && !virtio_dev_extbuf_alloc(pkt, data_len))
2115                 return pkt;
2116
2117         /* check if chained buffers are allowed */
2118         if (!dev->linearbuf)
2119                 return pkt;
2120
2121         /* Data doesn't fit into the buffer and the host supports
2122          * only linear buffers
2123          */
2124         rte_pktmbuf_free(pkt);
2125
2126         return NULL;
2127 }
2128
2129 static __rte_noinline uint16_t
2130 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
2131         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
2132 {
2133         uint16_t i;
2134         uint16_t free_entries;
2135         uint16_t dropped = 0;
2136         static bool allocerr_warned;
2137
2138         /*
2139          * The ordering between avail index and
2140          * desc reads needs to be enforced.
2141          */
2142         free_entries = __atomic_load_n(&vq->avail->idx, __ATOMIC_ACQUIRE) -
2143                         vq->last_avail_idx;
2144         if (free_entries == 0)
2145                 return 0;
2146
2147         rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
2148
2149         VHOST_LOG_DATA(DEBUG, "(%d) %s\n", dev->vid, __func__);
2150
2151         count = RTE_MIN(count, MAX_PKT_BURST);
2152         count = RTE_MIN(count, free_entries);
2153         VHOST_LOG_DATA(DEBUG, "(%d) about to dequeue %u buffers\n",
2154                         dev->vid, count);
2155
2156         for (i = 0; i < count; i++) {
2157                 struct buf_vector buf_vec[BUF_VECTOR_MAX];
2158                 uint16_t head_idx;
2159                 uint32_t buf_len;
2160                 uint16_t nr_vec = 0;
2161                 int err;
2162
2163                 if (unlikely(fill_vec_buf_split(dev, vq,
2164                                                 vq->last_avail_idx + i,
2165                                                 &nr_vec, buf_vec,
2166                                                 &head_idx, &buf_len,
2167                                                 VHOST_ACCESS_RO) < 0))
2168                         break;
2169
2170                 update_shadow_used_ring_split(vq, head_idx, 0);
2171
2172                 pkts[i] = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, buf_len);
2173                 if (unlikely(pkts[i] == NULL)) {
2174                         /*
2175                          * mbuf allocation fails for jumbo packets when external
2176                          * buffer allocation is not allowed and linear buffer
2177                          * is required. Drop this packet.
2178                          */
2179                         if (!allocerr_warned) {
2180                                 VHOST_LOG_DATA(ERR,
2181                                         "Failed mbuf alloc of size %d from %s on %s.\n",
2182                                         buf_len, mbuf_pool->name, dev->ifname);
2183                                 allocerr_warned = true;
2184                         }
2185                         dropped += 1;
2186                         i++;
2187                         break;
2188                 }
2189
2190                 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
2191                                 mbuf_pool);
2192                 if (unlikely(err)) {
2193                         rte_pktmbuf_free(pkts[i]);
2194                         if (!allocerr_warned) {
2195                                 VHOST_LOG_DATA(ERR,
2196                                         "Failed to copy desc to mbuf on %s.\n",
2197                                         dev->ifname);
2198                                 allocerr_warned = true;
2199                         }
2200                         dropped += 1;
2201                         i++;
2202                         break;
2203                 }
2204         }
2205
2206         vq->last_avail_idx += i;
2207
2208         do_data_copy_dequeue(vq);
2209         if (unlikely(i < count))
2210                 vq->shadow_used_idx = i;
2211         if (likely(vq->shadow_used_idx)) {
2212                 flush_shadow_used_ring_split(dev, vq);
2213                 vhost_vring_call_split(dev, vq);
2214         }
2215
2216         return (i - dropped);
2217 }
2218
2219 static __rte_always_inline int
2220 vhost_reserve_avail_batch_packed(struct virtio_net *dev,
2221                                  struct vhost_virtqueue *vq,
2222                                  struct rte_mempool *mbuf_pool,
2223                                  struct rte_mbuf **pkts,
2224                                  uint16_t avail_idx,
2225                                  uintptr_t *desc_addrs,
2226                                  uint16_t *ids)
2227 {
2228         bool wrap = vq->avail_wrap_counter;
2229         struct vring_packed_desc *descs = vq->desc_packed;
2230         struct virtio_net_hdr *hdr;
2231         uint64_t lens[PACKED_BATCH_SIZE];
2232         uint64_t buf_lens[PACKED_BATCH_SIZE];
2233         uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2234         uint16_t flags, i;
2235
2236         if (unlikely(avail_idx & PACKED_BATCH_MASK))
2237                 return -1;
2238         if (unlikely((avail_idx + PACKED_BATCH_SIZE) > vq->size))
2239                 return -1;
2240
2241         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2242                 flags = descs[avail_idx + i].flags;
2243                 if (unlikely((wrap != !!(flags & VRING_DESC_F_AVAIL)) ||
2244                              (wrap == !!(flags & VRING_DESC_F_USED))  ||
2245                              (flags & PACKED_DESC_SINGLE_DEQUEUE_FLAG)))
2246                         return -1;
2247         }
2248
2249         rte_smp_rmb();
2250
2251         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2252                 lens[i] = descs[avail_idx + i].len;
2253
2254         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2255                 desc_addrs[i] = vhost_iova_to_vva(dev, vq,
2256                                                   descs[avail_idx + i].addr,
2257                                                   &lens[i], VHOST_ACCESS_RW);
2258         }
2259
2260         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2261                 if (unlikely(!desc_addrs[i]))
2262                         return -1;
2263                 if (unlikely((lens[i] != descs[avail_idx + i].len)))
2264                         return -1;
2265         }
2266
2267         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2268                 pkts[i] = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, lens[i]);
2269                 if (!pkts[i])
2270                         goto free_buf;
2271         }
2272
2273         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2274                 buf_lens[i] = pkts[i]->buf_len - pkts[i]->data_off;
2275
2276         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2277                 if (unlikely(buf_lens[i] < (lens[i] - buf_offset)))
2278                         goto free_buf;
2279         }
2280
2281         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2282                 pkts[i]->pkt_len = descs[avail_idx + i].len - buf_offset;
2283                 pkts[i]->data_len = pkts[i]->pkt_len;
2284                 ids[i] = descs[avail_idx + i].id;
2285         }
2286
2287         if (virtio_net_with_host_offload(dev)) {
2288                 vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE) {
2289                         hdr = (struct virtio_net_hdr *)(desc_addrs[i]);
2290                         vhost_dequeue_offload(hdr, pkts[i]);
2291                 }
2292         }
2293
2294         return 0;
2295
2296 free_buf:
2297         for (i = 0; i < PACKED_BATCH_SIZE; i++)
2298                 rte_pktmbuf_free(pkts[i]);
2299
2300         return -1;
2301 }
2302
2303 static __rte_always_inline int
2304 virtio_dev_tx_batch_packed(struct virtio_net *dev,
2305                            struct vhost_virtqueue *vq,
2306                            struct rte_mempool *mbuf_pool,
2307                            struct rte_mbuf **pkts)
2308 {
2309         uint16_t avail_idx = vq->last_avail_idx;
2310         uint32_t buf_offset = sizeof(struct virtio_net_hdr_mrg_rxbuf);
2311         uintptr_t desc_addrs[PACKED_BATCH_SIZE];
2312         uint16_t ids[PACKED_BATCH_SIZE];
2313         uint16_t i;
2314
2315         if (vhost_reserve_avail_batch_packed(dev, vq, mbuf_pool, pkts,
2316                                              avail_idx, desc_addrs, ids))
2317                 return -1;
2318
2319         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2320                 rte_prefetch0((void *)(uintptr_t)desc_addrs[i]);
2321
2322         vhost_for_each_try_unroll(i, 0, PACKED_BATCH_SIZE)
2323                 rte_memcpy(rte_pktmbuf_mtod_offset(pkts[i], void *, 0),
2324                            (void *)(uintptr_t)(desc_addrs[i] + buf_offset),
2325                            pkts[i]->pkt_len);
2326
2327         if (virtio_net_is_inorder(dev))
2328                 vhost_shadow_dequeue_batch_packed_inorder(vq,
2329                         ids[PACKED_BATCH_SIZE - 1]);
2330         else
2331                 vhost_shadow_dequeue_batch_packed(dev, vq, ids);
2332
2333         vq_inc_last_avail_packed(vq, PACKED_BATCH_SIZE);
2334
2335         return 0;
2336 }
2337
2338 static __rte_always_inline int
2339 vhost_dequeue_single_packed(struct virtio_net *dev,
2340                             struct vhost_virtqueue *vq,
2341                             struct rte_mempool *mbuf_pool,
2342                             struct rte_mbuf **pkts,
2343                             uint16_t *buf_id,
2344                             uint16_t *desc_count)
2345 {
2346         struct buf_vector buf_vec[BUF_VECTOR_MAX];
2347         uint32_t buf_len;
2348         uint16_t nr_vec = 0;
2349         int err;
2350         static bool allocerr_warned;
2351
2352         if (unlikely(fill_vec_buf_packed(dev, vq,
2353                                          vq->last_avail_idx, desc_count,
2354                                          buf_vec, &nr_vec,
2355                                          buf_id, &buf_len,
2356                                          VHOST_ACCESS_RO) < 0))
2357                 return -1;
2358
2359         *pkts = virtio_dev_pktmbuf_alloc(dev, mbuf_pool, buf_len);
2360         if (unlikely(*pkts == NULL)) {
2361                 if (!allocerr_warned) {
2362                         VHOST_LOG_DATA(ERR,
2363                                 "Failed mbuf alloc of size %d from %s on %s.\n",
2364                                 buf_len, mbuf_pool->name, dev->ifname);
2365                         allocerr_warned = true;
2366                 }
2367                 return -1;
2368         }
2369
2370         err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, *pkts,
2371                                 mbuf_pool);
2372         if (unlikely(err)) {
2373                 if (!allocerr_warned) {
2374                         VHOST_LOG_DATA(ERR,
2375                                 "Failed to copy desc to mbuf on %s.\n",
2376                                 dev->ifname);
2377                         allocerr_warned = true;
2378                 }
2379                 rte_pktmbuf_free(*pkts);
2380                 return -1;
2381         }
2382
2383         return 0;
2384 }
2385
2386 static __rte_always_inline int
2387 virtio_dev_tx_single_packed(struct virtio_net *dev,
2388                             struct vhost_virtqueue *vq,
2389                             struct rte_mempool *mbuf_pool,
2390                             struct rte_mbuf **pkts)
2391 {
2392
2393         uint16_t buf_id, desc_count = 0;
2394         int ret;
2395
2396         ret = vhost_dequeue_single_packed(dev, vq, mbuf_pool, pkts, &buf_id,
2397                                         &desc_count);
2398
2399         if (likely(desc_count > 0)) {
2400                 if (virtio_net_is_inorder(dev))
2401                         vhost_shadow_dequeue_single_packed_inorder(vq, buf_id,
2402                                                                    desc_count);
2403                 else
2404                         vhost_shadow_dequeue_single_packed(vq, buf_id,
2405                                         desc_count);
2406
2407                 vq_inc_last_avail_packed(vq, desc_count);
2408         }
2409
2410         return ret;
2411 }
2412
2413 static __rte_noinline uint16_t
2414 virtio_dev_tx_packed(struct virtio_net *dev,
2415                      struct vhost_virtqueue *__rte_restrict vq,
2416                      struct rte_mempool *mbuf_pool,
2417                      struct rte_mbuf **__rte_restrict pkts,
2418                      uint32_t count)
2419 {
2420         uint32_t pkt_idx = 0;
2421         uint32_t remained = count;
2422
2423         do {
2424                 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
2425
2426                 if (remained >= PACKED_BATCH_SIZE) {
2427                         if (!virtio_dev_tx_batch_packed(dev, vq, mbuf_pool,
2428                                                         &pkts[pkt_idx])) {
2429                                 pkt_idx += PACKED_BATCH_SIZE;
2430                                 remained -= PACKED_BATCH_SIZE;
2431                                 continue;
2432                         }
2433                 }
2434
2435                 if (virtio_dev_tx_single_packed(dev, vq, mbuf_pool,
2436                                                 &pkts[pkt_idx]))
2437                         break;
2438                 pkt_idx++;
2439                 remained--;
2440
2441         } while (remained);
2442
2443         if (vq->shadow_used_idx) {
2444                 do_data_copy_dequeue(vq);
2445
2446                 vhost_flush_dequeue_shadow_packed(dev, vq);
2447                 vhost_vring_call_packed(dev, vq);
2448         }
2449
2450         return pkt_idx;
2451 }
2452
2453 uint16_t
2454 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
2455         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
2456 {
2457         struct virtio_net *dev;
2458         struct rte_mbuf *rarp_mbuf = NULL;
2459         struct vhost_virtqueue *vq;
2460         int16_t success = 1;
2461
2462         dev = get_device(vid);
2463         if (!dev)
2464                 return 0;
2465
2466         if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
2467                 VHOST_LOG_DATA(ERR,
2468                         "(%d) %s: built-in vhost net backend is disabled.\n",
2469                         dev->vid, __func__);
2470                 return 0;
2471         }
2472
2473         if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
2474                 VHOST_LOG_DATA(ERR,
2475                         "(%d) %s: invalid virtqueue idx %d.\n",
2476                         dev->vid, __func__, queue_id);
2477                 return 0;
2478         }
2479
2480         vq = dev->virtqueue[queue_id];
2481
2482         if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
2483                 return 0;
2484
2485         if (unlikely(vq->enabled == 0)) {
2486                 count = 0;
2487                 goto out_access_unlock;
2488         }
2489
2490         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2491                 vhost_user_iotlb_rd_lock(vq);
2492
2493         if (unlikely(vq->access_ok == 0))
2494                 if (unlikely(vring_translate(dev, vq) < 0)) {
2495                         count = 0;
2496                         goto out;
2497                 }
2498
2499         /*
2500          * Construct a RARP broadcast packet, and inject it to the "pkts"
2501          * array, to looks like that guest actually send such packet.
2502          *
2503          * Check user_send_rarp() for more information.
2504          *
2505          * broadcast_rarp shares a cacheline in the virtio_net structure
2506          * with some fields that are accessed during enqueue and
2507          * __atomic_compare_exchange_n causes a write if performed compare
2508          * and exchange. This could result in false sharing between enqueue
2509          * and dequeue.
2510          *
2511          * Prevent unnecessary false sharing by reading broadcast_rarp first
2512          * and only performing compare and exchange if the read indicates it
2513          * is likely to be set.
2514          */
2515         if (unlikely(__atomic_load_n(&dev->broadcast_rarp, __ATOMIC_ACQUIRE) &&
2516                         __atomic_compare_exchange_n(&dev->broadcast_rarp,
2517                         &success, 0, 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED))) {
2518
2519                 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
2520                 if (rarp_mbuf == NULL) {
2521                         VHOST_LOG_DATA(ERR, "Failed to make RARP packet.\n");
2522                         count = 0;
2523                         goto out;
2524                 }
2525                 count -= 1;
2526         }
2527
2528         if (vq_is_packed(dev))
2529                 count = virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count);
2530         else
2531                 count = virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count);
2532
2533 out:
2534         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
2535                 vhost_user_iotlb_rd_unlock(vq);
2536
2537 out_access_unlock:
2538         rte_spinlock_unlock(&vq->access_lock);
2539
2540         if (unlikely(rarp_mbuf != NULL)) {
2541                 /*
2542                  * Inject it to the head of "pkts" array, so that switch's mac
2543                  * learning table will get updated first.
2544                  */
2545                 memmove(&pkts[1], pkts, count * sizeof(struct rte_mbuf *));
2546                 pkts[0] = rarp_mbuf;
2547                 count += 1;
2548         }
2549
2550         return count;
2551 }