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