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