net/virtio: improve perf via one-way barrier on avail flag
[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
21 #include "iotlb.h"
22 #include "vhost.h"
23
24 #define MAX_PKT_BURST 32
25
26 #define MAX_BATCH_LEN 256
27
28 static  __rte_always_inline bool
29 rxvq_is_mergeable(struct virtio_net *dev)
30 {
31         return dev->features & (1ULL << VIRTIO_NET_F_MRG_RXBUF);
32 }
33
34 static bool
35 is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t nr_vring)
36 {
37         return (is_tx ^ (idx & 1)) == 0 && idx < nr_vring;
38 }
39
40 static __rte_always_inline void
41 do_flush_shadow_used_ring_split(struct virtio_net *dev,
42                         struct vhost_virtqueue *vq,
43                         uint16_t to, uint16_t from, uint16_t size)
44 {
45         rte_memcpy(&vq->used->ring[to],
46                         &vq->shadow_used_split[from],
47                         size * sizeof(struct vring_used_elem));
48         vhost_log_cache_used_vring(dev, vq,
49                         offsetof(struct vring_used, ring[to]),
50                         size * sizeof(struct vring_used_elem));
51 }
52
53 static __rte_always_inline void
54 flush_shadow_used_ring_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
55 {
56         uint16_t used_idx = vq->last_used_idx & (vq->size - 1);
57
58         if (used_idx + vq->shadow_used_idx <= vq->size) {
59                 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0,
60                                           vq->shadow_used_idx);
61         } else {
62                 uint16_t size;
63
64                 /* update used ring interval [used_idx, vq->size] */
65                 size = vq->size - used_idx;
66                 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0, size);
67
68                 /* update the left half used ring interval [0, left_size] */
69                 do_flush_shadow_used_ring_split(dev, vq, 0, size,
70                                           vq->shadow_used_idx - size);
71         }
72         vq->last_used_idx += vq->shadow_used_idx;
73
74         rte_smp_wmb();
75
76         vhost_log_cache_sync(dev, vq);
77
78         *(volatile uint16_t *)&vq->used->idx += vq->shadow_used_idx;
79         vq->shadow_used_idx = 0;
80         vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
81                 sizeof(vq->used->idx));
82 }
83
84 static __rte_always_inline void
85 update_shadow_used_ring_split(struct vhost_virtqueue *vq,
86                          uint16_t desc_idx, uint32_t len)
87 {
88         uint16_t i = vq->shadow_used_idx++;
89
90         vq->shadow_used_split[i].id  = desc_idx;
91         vq->shadow_used_split[i].len = len;
92 }
93
94 static __rte_always_inline void
95 flush_shadow_used_ring_packed(struct virtio_net *dev,
96                         struct vhost_virtqueue *vq)
97 {
98         int i;
99         uint16_t used_idx = vq->last_used_idx;
100         uint16_t head_idx = vq->last_used_idx;
101         uint16_t head_flags = 0;
102
103         /* Split loop in two to save memory barriers */
104         for (i = 0; i < vq->shadow_used_idx; i++) {
105                 vq->desc_packed[used_idx].id = vq->shadow_used_packed[i].id;
106                 vq->desc_packed[used_idx].len = vq->shadow_used_packed[i].len;
107
108                 used_idx += vq->shadow_used_packed[i].count;
109                 if (used_idx >= vq->size)
110                         used_idx -= vq->size;
111         }
112
113         rte_smp_wmb();
114
115         for (i = 0; i < vq->shadow_used_idx; i++) {
116                 uint16_t flags;
117
118                 if (vq->shadow_used_packed[i].len)
119                         flags = VRING_DESC_F_WRITE;
120                 else
121                         flags = 0;
122
123                 if (vq->used_wrap_counter) {
124                         flags |= VRING_DESC_F_USED;
125                         flags |= VRING_DESC_F_AVAIL;
126                 } else {
127                         flags &= ~VRING_DESC_F_USED;
128                         flags &= ~VRING_DESC_F_AVAIL;
129                 }
130
131                 if (i > 0) {
132                         vq->desc_packed[vq->last_used_idx].flags = flags;
133
134                         vhost_log_cache_used_vring(dev, vq,
135                                         vq->last_used_idx *
136                                         sizeof(struct vring_packed_desc),
137                                         sizeof(struct vring_packed_desc));
138                 } else {
139                         head_idx = vq->last_used_idx;
140                         head_flags = flags;
141                 }
142
143                 vq->last_used_idx += vq->shadow_used_packed[i].count;
144                 if (vq->last_used_idx >= vq->size) {
145                         vq->used_wrap_counter ^= 1;
146                         vq->last_used_idx -= vq->size;
147                 }
148         }
149
150         vq->desc_packed[head_idx].flags = head_flags;
151
152         vhost_log_cache_used_vring(dev, vq,
153                                 head_idx *
154                                 sizeof(struct vring_packed_desc),
155                                 sizeof(struct vring_packed_desc));
156
157         vq->shadow_used_idx = 0;
158         vhost_log_cache_sync(dev, vq);
159 }
160
161 static __rte_always_inline void
162 update_shadow_used_ring_packed(struct vhost_virtqueue *vq,
163                          uint16_t desc_idx, uint32_t len, uint16_t count)
164 {
165         uint16_t i = vq->shadow_used_idx++;
166
167         vq->shadow_used_packed[i].id  = desc_idx;
168         vq->shadow_used_packed[i].len = len;
169         vq->shadow_used_packed[i].count = count;
170 }
171
172 static inline void
173 do_data_copy_enqueue(struct virtio_net *dev, struct vhost_virtqueue *vq)
174 {
175         struct batch_copy_elem *elem = vq->batch_copy_elems;
176         uint16_t count = vq->batch_copy_nb_elems;
177         int i;
178
179         for (i = 0; i < count; i++) {
180                 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
181                 vhost_log_cache_write(dev, vq, elem[i].log_addr, elem[i].len);
182                 PRINT_PACKET(dev, (uintptr_t)elem[i].dst, elem[i].len, 0);
183         }
184
185         vq->batch_copy_nb_elems = 0;
186 }
187
188 static inline void
189 do_data_copy_dequeue(struct vhost_virtqueue *vq)
190 {
191         struct batch_copy_elem *elem = vq->batch_copy_elems;
192         uint16_t count = vq->batch_copy_nb_elems;
193         int i;
194
195         for (i = 0; i < count; i++)
196                 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
197
198         vq->batch_copy_nb_elems = 0;
199 }
200
201 /* avoid write operation when necessary, to lessen cache issues */
202 #define ASSIGN_UNLESS_EQUAL(var, val) do {      \
203         if ((var) != (val))                     \
204                 (var) = (val);                  \
205 } while (0)
206
207 static __rte_always_inline void
208 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
209 {
210         uint64_t csum_l4 = m_buf->ol_flags & PKT_TX_L4_MASK;
211
212         if (m_buf->ol_flags & PKT_TX_TCP_SEG)
213                 csum_l4 |= PKT_TX_TCP_CKSUM;
214
215         if (csum_l4) {
216                 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
217                 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
218
219                 switch (csum_l4) {
220                 case PKT_TX_TCP_CKSUM:
221                         net_hdr->csum_offset = (offsetof(struct rte_tcp_hdr,
222                                                 cksum));
223                         break;
224                 case PKT_TX_UDP_CKSUM:
225                         net_hdr->csum_offset = (offsetof(struct rte_udp_hdr,
226                                                 dgram_cksum));
227                         break;
228                 case PKT_TX_SCTP_CKSUM:
229                         net_hdr->csum_offset = (offsetof(struct rte_sctp_hdr,
230                                                 cksum));
231                         break;
232                 }
233         } else {
234                 ASSIGN_UNLESS_EQUAL(net_hdr->csum_start, 0);
235                 ASSIGN_UNLESS_EQUAL(net_hdr->csum_offset, 0);
236                 ASSIGN_UNLESS_EQUAL(net_hdr->flags, 0);
237         }
238
239         /* IP cksum verification cannot be bypassed, then calculate here */
240         if (m_buf->ol_flags & PKT_TX_IP_CKSUM) {
241                 struct rte_ipv4_hdr *ipv4_hdr;
242
243                 ipv4_hdr = rte_pktmbuf_mtod_offset(m_buf, struct rte_ipv4_hdr *,
244                                                    m_buf->l2_len);
245                 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
246         }
247
248         if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
249                 if (m_buf->ol_flags & PKT_TX_IPV4)
250                         net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
251                 else
252                         net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
253                 net_hdr->gso_size = m_buf->tso_segsz;
254                 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
255                                         + m_buf->l4_len;
256         } else if (m_buf->ol_flags & PKT_TX_UDP_SEG) {
257                 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
258                 net_hdr->gso_size = m_buf->tso_segsz;
259                 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len +
260                         m_buf->l4_len;
261         } else {
262                 ASSIGN_UNLESS_EQUAL(net_hdr->gso_type, 0);
263                 ASSIGN_UNLESS_EQUAL(net_hdr->gso_size, 0);
264                 ASSIGN_UNLESS_EQUAL(net_hdr->hdr_len, 0);
265         }
266 }
267
268 static __rte_always_inline int
269 map_one_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
270                 struct buf_vector *buf_vec, uint16_t *vec_idx,
271                 uint64_t desc_iova, uint64_t desc_len, uint8_t perm)
272 {
273         uint16_t vec_id = *vec_idx;
274
275         while (desc_len) {
276                 uint64_t desc_addr;
277                 uint64_t desc_chunck_len = desc_len;
278
279                 if (unlikely(vec_id >= BUF_VECTOR_MAX))
280                         return -1;
281
282                 desc_addr = vhost_iova_to_vva(dev, vq,
283                                 desc_iova,
284                                 &desc_chunck_len,
285                                 perm);
286                 if (unlikely(!desc_addr))
287                         return -1;
288
289                 rte_prefetch0((void *)(uintptr_t)desc_addr);
290
291                 buf_vec[vec_id].buf_iova = desc_iova;
292                 buf_vec[vec_id].buf_addr = desc_addr;
293                 buf_vec[vec_id].buf_len  = desc_chunck_len;
294
295                 desc_len -= desc_chunck_len;
296                 desc_iova += desc_chunck_len;
297                 vec_id++;
298         }
299         *vec_idx = vec_id;
300
301         return 0;
302 }
303
304 static __rte_always_inline int
305 fill_vec_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
306                          uint32_t avail_idx, uint16_t *vec_idx,
307                          struct buf_vector *buf_vec, uint16_t *desc_chain_head,
308                          uint32_t *desc_chain_len, uint8_t perm)
309 {
310         uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
311         uint16_t vec_id = *vec_idx;
312         uint32_t len    = 0;
313         uint64_t dlen;
314         uint32_t nr_descs = vq->size;
315         uint32_t cnt    = 0;
316         struct vring_desc *descs = vq->desc;
317         struct vring_desc *idesc = NULL;
318
319         if (unlikely(idx >= vq->size))
320                 return -1;
321
322         *desc_chain_head = idx;
323
324         if (vq->desc[idx].flags & VRING_DESC_F_INDIRECT) {
325                 dlen = vq->desc[idx].len;
326                 nr_descs = dlen / sizeof(struct vring_desc);
327                 if (unlikely(nr_descs > vq->size))
328                         return -1;
329
330                 descs = (struct vring_desc *)(uintptr_t)
331                         vhost_iova_to_vva(dev, vq, vq->desc[idx].addr,
332                                                 &dlen,
333                                                 VHOST_ACCESS_RO);
334                 if (unlikely(!descs))
335                         return -1;
336
337                 if (unlikely(dlen < vq->desc[idx].len)) {
338                         /*
339                          * The indirect desc table is not contiguous
340                          * in process VA space, we have to copy it.
341                          */
342                         idesc = vhost_alloc_copy_ind_table(dev, vq,
343                                         vq->desc[idx].addr, vq->desc[idx].len);
344                         if (unlikely(!idesc))
345                                 return -1;
346
347                         descs = idesc;
348                 }
349
350                 idx = 0;
351         }
352
353         while (1) {
354                 if (unlikely(idx >= nr_descs || cnt++ >= nr_descs)) {
355                         free_ind_table(idesc);
356                         return -1;
357                 }
358
359                 len += descs[idx].len;
360
361                 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
362                                                 descs[idx].addr, descs[idx].len,
363                                                 perm))) {
364                         free_ind_table(idesc);
365                         return -1;
366                 }
367
368                 if ((descs[idx].flags & VRING_DESC_F_NEXT) == 0)
369                         break;
370
371                 idx = descs[idx].next;
372         }
373
374         *desc_chain_len = len;
375         *vec_idx = vec_id;
376
377         if (unlikely(!!idesc))
378                 free_ind_table(idesc);
379
380         return 0;
381 }
382
383 /*
384  * Returns -1 on fail, 0 on success
385  */
386 static inline int
387 reserve_avail_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
388                                 uint32_t size, struct buf_vector *buf_vec,
389                                 uint16_t *num_buffers, uint16_t avail_head,
390                                 uint16_t *nr_vec)
391 {
392         uint16_t cur_idx;
393         uint16_t vec_idx = 0;
394         uint16_t max_tries, tries = 0;
395
396         uint16_t head_idx = 0;
397         uint32_t len = 0;
398
399         *num_buffers = 0;
400         cur_idx  = vq->last_avail_idx;
401
402         if (rxvq_is_mergeable(dev))
403                 max_tries = vq->size - 1;
404         else
405                 max_tries = 1;
406
407         while (size > 0) {
408                 if (unlikely(cur_idx == avail_head))
409                         return -1;
410                 /*
411                  * if we tried all available ring items, and still
412                  * can't get enough buf, it means something abnormal
413                  * happened.
414                  */
415                 if (unlikely(++tries > max_tries))
416                         return -1;
417
418                 if (unlikely(fill_vec_buf_split(dev, vq, cur_idx,
419                                                 &vec_idx, buf_vec,
420                                                 &head_idx, &len,
421                                                 VHOST_ACCESS_RW) < 0))
422                         return -1;
423                 len = RTE_MIN(len, size);
424                 update_shadow_used_ring_split(vq, head_idx, len);
425                 size -= len;
426
427                 cur_idx++;
428                 *num_buffers += 1;
429         }
430
431         *nr_vec = vec_idx;
432
433         return 0;
434 }
435
436 static __rte_always_inline int
437 fill_vec_buf_packed_indirect(struct virtio_net *dev,
438                         struct vhost_virtqueue *vq,
439                         struct vring_packed_desc *desc, uint16_t *vec_idx,
440                         struct buf_vector *buf_vec, uint32_t *len, uint8_t perm)
441 {
442         uint16_t i;
443         uint32_t nr_descs;
444         uint16_t vec_id = *vec_idx;
445         uint64_t dlen;
446         struct vring_packed_desc *descs, *idescs = NULL;
447
448         dlen = desc->len;
449         descs = (struct vring_packed_desc *)(uintptr_t)
450                 vhost_iova_to_vva(dev, vq, desc->addr, &dlen, VHOST_ACCESS_RO);
451         if (unlikely(!descs))
452                 return -1;
453
454         if (unlikely(dlen < desc->len)) {
455                 /*
456                  * The indirect desc table is not contiguous
457                  * in process VA space, we have to copy it.
458                  */
459                 idescs = vhost_alloc_copy_ind_table(dev,
460                                 vq, desc->addr, desc->len);
461                 if (unlikely(!idescs))
462                         return -1;
463
464                 descs = idescs;
465         }
466
467         nr_descs =  desc->len / sizeof(struct vring_packed_desc);
468         if (unlikely(nr_descs >= vq->size)) {
469                 free_ind_table(idescs);
470                 return -1;
471         }
472
473         for (i = 0; i < nr_descs; i++) {
474                 if (unlikely(vec_id >= BUF_VECTOR_MAX)) {
475                         free_ind_table(idescs);
476                         return -1;
477                 }
478
479                 *len += descs[i].len;
480                 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
481                                                 descs[i].addr, descs[i].len,
482                                                 perm)))
483                         return -1;
484         }
485         *vec_idx = vec_id;
486
487         if (unlikely(!!idescs))
488                 free_ind_table(idescs);
489
490         return 0;
491 }
492
493 static __rte_always_inline int
494 fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
495                                 uint16_t avail_idx, uint16_t *desc_count,
496                                 struct buf_vector *buf_vec, uint16_t *vec_idx,
497                                 uint16_t *buf_id, uint32_t *len, uint8_t perm)
498 {
499         bool wrap_counter = vq->avail_wrap_counter;
500         struct vring_packed_desc *descs = vq->desc_packed;
501         uint16_t vec_id = *vec_idx;
502
503         if (avail_idx < vq->last_avail_idx)
504                 wrap_counter ^= 1;
505
506         /*
507          * Perform a load-acquire barrier in desc_is_avail to
508          * enforce the ordering between desc flags and desc
509          * content.
510          */
511         if (unlikely(!desc_is_avail(&descs[avail_idx], wrap_counter)))
512                 return -1;
513
514         *desc_count = 0;
515         *len = 0;
516
517         while (1) {
518                 if (unlikely(vec_id >= BUF_VECTOR_MAX))
519                         return -1;
520
521                 if (unlikely(*desc_count >= vq->size))
522                         return -1;
523
524                 *desc_count += 1;
525                 *buf_id = descs[avail_idx].id;
526
527                 if (descs[avail_idx].flags & VRING_DESC_F_INDIRECT) {
528                         if (unlikely(fill_vec_buf_packed_indirect(dev, vq,
529                                                         &descs[avail_idx],
530                                                         &vec_id, buf_vec,
531                                                         len, perm) < 0))
532                                 return -1;
533                 } else {
534                         *len += descs[avail_idx].len;
535
536                         if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
537                                                         descs[avail_idx].addr,
538                                                         descs[avail_idx].len,
539                                                         perm)))
540                                 return -1;
541                 }
542
543                 if ((descs[avail_idx].flags & VRING_DESC_F_NEXT) == 0)
544                         break;
545
546                 if (++avail_idx >= vq->size) {
547                         avail_idx -= vq->size;
548                         wrap_counter ^= 1;
549                 }
550         }
551
552         *vec_idx = vec_id;
553
554         return 0;
555 }
556
557 /*
558  * Returns -1 on fail, 0 on success
559  */
560 static inline int
561 reserve_avail_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
562                                 uint32_t size, struct buf_vector *buf_vec,
563                                 uint16_t *nr_vec, uint16_t *num_buffers,
564                                 uint16_t *nr_descs)
565 {
566         uint16_t avail_idx;
567         uint16_t vec_idx = 0;
568         uint16_t max_tries, tries = 0;
569
570         uint16_t buf_id = 0;
571         uint32_t len = 0;
572         uint16_t desc_count;
573
574         *num_buffers = 0;
575         avail_idx = vq->last_avail_idx;
576
577         if (rxvq_is_mergeable(dev))
578                 max_tries = vq->size - 1;
579         else
580                 max_tries = 1;
581
582         while (size > 0) {
583                 /*
584                  * if we tried all available ring items, and still
585                  * can't get enough buf, it means something abnormal
586                  * happened.
587                  */
588                 if (unlikely(++tries > max_tries))
589                         return -1;
590
591                 if (unlikely(fill_vec_buf_packed(dev, vq,
592                                                 avail_idx, &desc_count,
593                                                 buf_vec, &vec_idx,
594                                                 &buf_id, &len,
595                                                 VHOST_ACCESS_RW) < 0))
596                         return -1;
597
598                 len = RTE_MIN(len, size);
599                 update_shadow_used_ring_packed(vq, buf_id, len, desc_count);
600                 size -= len;
601
602                 avail_idx += desc_count;
603                 if (avail_idx >= vq->size)
604                         avail_idx -= vq->size;
605
606                 *nr_descs += desc_count;
607                 *num_buffers += 1;
608         }
609
610         *nr_vec = vec_idx;
611
612         return 0;
613 }
614
615 static __rte_noinline void
616 copy_vnet_hdr_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
617                 struct buf_vector *buf_vec,
618                 struct virtio_net_hdr_mrg_rxbuf *hdr)
619 {
620         uint64_t len;
621         uint64_t remain = dev->vhost_hlen;
622         uint64_t src = (uint64_t)(uintptr_t)hdr, dst;
623         uint64_t iova = buf_vec->buf_iova;
624
625         while (remain) {
626                 len = RTE_MIN(remain,
627                                 buf_vec->buf_len);
628                 dst = buf_vec->buf_addr;
629                 rte_memcpy((void *)(uintptr_t)dst,
630                                 (void *)(uintptr_t)src,
631                                 len);
632
633                 PRINT_PACKET(dev, (uintptr_t)dst,
634                                 (uint32_t)len, 0);
635                 vhost_log_cache_write(dev, vq,
636                                 iova, len);
637
638                 remain -= len;
639                 iova += len;
640                 src += len;
641                 buf_vec++;
642         }
643 }
644
645 static __rte_always_inline int
646 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
647                             struct rte_mbuf *m, struct buf_vector *buf_vec,
648                             uint16_t nr_vec, uint16_t num_buffers)
649 {
650         uint32_t vec_idx = 0;
651         uint32_t mbuf_offset, mbuf_avail;
652         uint32_t buf_offset, buf_avail;
653         uint64_t buf_addr, buf_iova, buf_len;
654         uint32_t cpy_len;
655         uint64_t hdr_addr;
656         struct rte_mbuf *hdr_mbuf;
657         struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
658         struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
659         int error = 0;
660
661         if (unlikely(m == NULL)) {
662                 error = -1;
663                 goto out;
664         }
665
666         buf_addr = buf_vec[vec_idx].buf_addr;
667         buf_iova = buf_vec[vec_idx].buf_iova;
668         buf_len = buf_vec[vec_idx].buf_len;
669
670         if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
671                 error = -1;
672                 goto out;
673         }
674
675         hdr_mbuf = m;
676         hdr_addr = buf_addr;
677         if (unlikely(buf_len < dev->vhost_hlen))
678                 hdr = &tmp_hdr;
679         else
680                 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
681
682         VHOST_LOG_DEBUG(VHOST_DATA, "(%d) RX: num merge buffers %d\n",
683                 dev->vid, num_buffers);
684
685         if (unlikely(buf_len < dev->vhost_hlen)) {
686                 buf_offset = dev->vhost_hlen - buf_len;
687                 vec_idx++;
688                 buf_addr = buf_vec[vec_idx].buf_addr;
689                 buf_iova = buf_vec[vec_idx].buf_iova;
690                 buf_len = buf_vec[vec_idx].buf_len;
691                 buf_avail = buf_len - buf_offset;
692         } else {
693                 buf_offset = dev->vhost_hlen;
694                 buf_avail = buf_len - dev->vhost_hlen;
695         }
696
697         mbuf_avail  = rte_pktmbuf_data_len(m);
698         mbuf_offset = 0;
699         while (mbuf_avail != 0 || m->next != NULL) {
700                 /* done with current buf, get the next one */
701                 if (buf_avail == 0) {
702                         vec_idx++;
703                         if (unlikely(vec_idx >= nr_vec)) {
704                                 error = -1;
705                                 goto out;
706                         }
707
708                         buf_addr = buf_vec[vec_idx].buf_addr;
709                         buf_iova = buf_vec[vec_idx].buf_iova;
710                         buf_len = buf_vec[vec_idx].buf_len;
711
712                         buf_offset = 0;
713                         buf_avail  = buf_len;
714                 }
715
716                 /* done with current mbuf, get the next one */
717                 if (mbuf_avail == 0) {
718                         m = m->next;
719
720                         mbuf_offset = 0;
721                         mbuf_avail  = rte_pktmbuf_data_len(m);
722                 }
723
724                 if (hdr_addr) {
725                         virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
726                         if (rxvq_is_mergeable(dev))
727                                 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
728                                                 num_buffers);
729
730                         if (unlikely(hdr == &tmp_hdr)) {
731                                 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
732                         } else {
733                                 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
734                                                 dev->vhost_hlen, 0);
735                                 vhost_log_cache_write(dev, vq,
736                                                 buf_vec[0].buf_iova,
737                                                 dev->vhost_hlen);
738                         }
739
740                         hdr_addr = 0;
741                 }
742
743                 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
744
745                 if (likely(cpy_len > MAX_BATCH_LEN ||
746                                         vq->batch_copy_nb_elems >= vq->size)) {
747                         rte_memcpy((void *)((uintptr_t)(buf_addr + buf_offset)),
748                                 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
749                                 cpy_len);
750                         vhost_log_cache_write(dev, vq, buf_iova + buf_offset,
751                                         cpy_len);
752                         PRINT_PACKET(dev, (uintptr_t)(buf_addr + buf_offset),
753                                 cpy_len, 0);
754                 } else {
755                         batch_copy[vq->batch_copy_nb_elems].dst =
756                                 (void *)((uintptr_t)(buf_addr + buf_offset));
757                         batch_copy[vq->batch_copy_nb_elems].src =
758                                 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
759                         batch_copy[vq->batch_copy_nb_elems].log_addr =
760                                 buf_iova + buf_offset;
761                         batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
762                         vq->batch_copy_nb_elems++;
763                 }
764
765                 mbuf_avail  -= cpy_len;
766                 mbuf_offset += cpy_len;
767                 buf_avail  -= cpy_len;
768                 buf_offset += cpy_len;
769         }
770
771 out:
772
773         return error;
774 }
775
776 static __rte_noinline uint32_t
777 virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
778         struct rte_mbuf **pkts, uint32_t count)
779 {
780         uint32_t pkt_idx = 0;
781         uint16_t num_buffers;
782         struct buf_vector buf_vec[BUF_VECTOR_MAX];
783         uint16_t avail_head;
784
785         avail_head = *((volatile uint16_t *)&vq->avail->idx);
786
787         /*
788          * The ordering between avail index and
789          * desc reads needs to be enforced.
790          */
791         rte_smp_rmb();
792
793         rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
794
795         for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
796                 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
797                 uint16_t nr_vec = 0;
798
799                 if (unlikely(reserve_avail_buf_split(dev, vq,
800                                                 pkt_len, buf_vec, &num_buffers,
801                                                 avail_head, &nr_vec) < 0)) {
802                         VHOST_LOG_DEBUG(VHOST_DATA,
803                                 "(%d) failed to get enough desc from vring\n",
804                                 dev->vid);
805                         vq->shadow_used_idx -= num_buffers;
806                         break;
807                 }
808
809                 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
810                         dev->vid, vq->last_avail_idx,
811                         vq->last_avail_idx + num_buffers);
812
813                 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
814                                                 buf_vec, nr_vec,
815                                                 num_buffers) < 0) {
816                         vq->shadow_used_idx -= num_buffers;
817                         break;
818                 }
819
820                 vq->last_avail_idx += num_buffers;
821         }
822
823         do_data_copy_enqueue(dev, vq);
824
825         if (likely(vq->shadow_used_idx)) {
826                 flush_shadow_used_ring_split(dev, vq);
827                 vhost_vring_call_split(dev, vq);
828         }
829
830         return pkt_idx;
831 }
832
833 static __rte_noinline uint32_t
834 virtio_dev_rx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
835         struct rte_mbuf **pkts, uint32_t count)
836 {
837         uint32_t pkt_idx = 0;
838         uint16_t num_buffers;
839         struct buf_vector buf_vec[BUF_VECTOR_MAX];
840
841         for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
842                 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
843                 uint16_t nr_vec = 0;
844                 uint16_t nr_descs = 0;
845
846                 if (unlikely(reserve_avail_buf_packed(dev, vq,
847                                                 pkt_len, buf_vec, &nr_vec,
848                                                 &num_buffers, &nr_descs) < 0)) {
849                         VHOST_LOG_DEBUG(VHOST_DATA,
850                                 "(%d) failed to get enough desc from vring\n",
851                                 dev->vid);
852                         vq->shadow_used_idx -= num_buffers;
853                         break;
854                 }
855
856                 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
857                         dev->vid, vq->last_avail_idx,
858                         vq->last_avail_idx + num_buffers);
859
860                 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
861                                                 buf_vec, nr_vec,
862                                                 num_buffers) < 0) {
863                         vq->shadow_used_idx -= num_buffers;
864                         break;
865                 }
866
867                 vq->last_avail_idx += nr_descs;
868                 if (vq->last_avail_idx >= vq->size) {
869                         vq->last_avail_idx -= vq->size;
870                         vq->avail_wrap_counter ^= 1;
871                 }
872         }
873
874         do_data_copy_enqueue(dev, vq);
875
876         if (likely(vq->shadow_used_idx)) {
877                 flush_shadow_used_ring_packed(dev, vq);
878                 vhost_vring_call_packed(dev, vq);
879         }
880
881         return pkt_idx;
882 }
883
884 static __rte_always_inline uint32_t
885 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
886         struct rte_mbuf **pkts, uint32_t count)
887 {
888         struct vhost_virtqueue *vq;
889         uint32_t nb_tx = 0;
890
891         VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
892         if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
893                 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
894                         dev->vid, __func__, queue_id);
895                 return 0;
896         }
897
898         vq = dev->virtqueue[queue_id];
899
900         rte_spinlock_lock(&vq->access_lock);
901
902         if (unlikely(vq->enabled == 0))
903                 goto out_access_unlock;
904
905         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
906                 vhost_user_iotlb_rd_lock(vq);
907
908         if (unlikely(vq->access_ok == 0))
909                 if (unlikely(vring_translate(dev, vq) < 0))
910                         goto out;
911
912         count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
913         if (count == 0)
914                 goto out;
915
916         if (vq_is_packed(dev))
917                 nb_tx = virtio_dev_rx_packed(dev, vq, pkts, count);
918         else
919                 nb_tx = virtio_dev_rx_split(dev, vq, pkts, count);
920
921 out:
922         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
923                 vhost_user_iotlb_rd_unlock(vq);
924
925 out_access_unlock:
926         rte_spinlock_unlock(&vq->access_lock);
927
928         return nb_tx;
929 }
930
931 uint16_t
932 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
933         struct rte_mbuf **pkts, uint16_t count)
934 {
935         struct virtio_net *dev = get_device(vid);
936
937         if (!dev)
938                 return 0;
939
940         if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
941                 RTE_LOG(ERR, VHOST_DATA,
942                         "(%d) %s: built-in vhost net backend is disabled.\n",
943                         dev->vid, __func__);
944                 return 0;
945         }
946
947         return virtio_dev_rx(dev, queue_id, pkts, count);
948 }
949
950 static inline bool
951 virtio_net_with_host_offload(struct virtio_net *dev)
952 {
953         if (dev->features &
954                         ((1ULL << VIRTIO_NET_F_CSUM) |
955                          (1ULL << VIRTIO_NET_F_HOST_ECN) |
956                          (1ULL << VIRTIO_NET_F_HOST_TSO4) |
957                          (1ULL << VIRTIO_NET_F_HOST_TSO6) |
958                          (1ULL << VIRTIO_NET_F_HOST_UFO)))
959                 return true;
960
961         return false;
962 }
963
964 static void
965 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
966 {
967         struct rte_ipv4_hdr *ipv4_hdr;
968         struct rte_ipv6_hdr *ipv6_hdr;
969         void *l3_hdr = NULL;
970         struct rte_ether_hdr *eth_hdr;
971         uint16_t ethertype;
972
973         eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
974
975         m->l2_len = sizeof(struct rte_ether_hdr);
976         ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
977
978         if (ethertype == RTE_ETHER_TYPE_VLAN) {
979                 struct rte_vlan_hdr *vlan_hdr =
980                         (struct rte_vlan_hdr *)(eth_hdr + 1);
981
982                 m->l2_len += sizeof(struct rte_vlan_hdr);
983                 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
984         }
985
986         l3_hdr = (char *)eth_hdr + m->l2_len;
987
988         switch (ethertype) {
989         case RTE_ETHER_TYPE_IPV4:
990                 ipv4_hdr = l3_hdr;
991                 *l4_proto = ipv4_hdr->next_proto_id;
992                 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
993                 *l4_hdr = (char *)l3_hdr + m->l3_len;
994                 m->ol_flags |= PKT_TX_IPV4;
995                 break;
996         case RTE_ETHER_TYPE_IPV6:
997                 ipv6_hdr = l3_hdr;
998                 *l4_proto = ipv6_hdr->proto;
999                 m->l3_len = sizeof(struct rte_ipv6_hdr);
1000                 *l4_hdr = (char *)l3_hdr + m->l3_len;
1001                 m->ol_flags |= PKT_TX_IPV6;
1002                 break;
1003         default:
1004                 m->l3_len = 0;
1005                 *l4_proto = 0;
1006                 *l4_hdr = NULL;
1007                 break;
1008         }
1009 }
1010
1011 static __rte_always_inline void
1012 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
1013 {
1014         uint16_t l4_proto = 0;
1015         void *l4_hdr = NULL;
1016         struct rte_tcp_hdr *tcp_hdr = NULL;
1017
1018         if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
1019                 return;
1020
1021         parse_ethernet(m, &l4_proto, &l4_hdr);
1022         if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1023                 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
1024                         switch (hdr->csum_offset) {
1025                         case (offsetof(struct rte_tcp_hdr, cksum)):
1026                                 if (l4_proto == IPPROTO_TCP)
1027                                         m->ol_flags |= PKT_TX_TCP_CKSUM;
1028                                 break;
1029                         case (offsetof(struct rte_udp_hdr, dgram_cksum)):
1030                                 if (l4_proto == IPPROTO_UDP)
1031                                         m->ol_flags |= PKT_TX_UDP_CKSUM;
1032                                 break;
1033                         case (offsetof(struct rte_sctp_hdr, cksum)):
1034                                 if (l4_proto == IPPROTO_SCTP)
1035                                         m->ol_flags |= PKT_TX_SCTP_CKSUM;
1036                                 break;
1037                         default:
1038                                 break;
1039                         }
1040                 }
1041         }
1042
1043         if (l4_hdr && hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1044                 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1045                 case VIRTIO_NET_HDR_GSO_TCPV4:
1046                 case VIRTIO_NET_HDR_GSO_TCPV6:
1047                         tcp_hdr = l4_hdr;
1048                         m->ol_flags |= PKT_TX_TCP_SEG;
1049                         m->tso_segsz = hdr->gso_size;
1050                         m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
1051                         break;
1052                 case VIRTIO_NET_HDR_GSO_UDP:
1053                         m->ol_flags |= PKT_TX_UDP_SEG;
1054                         m->tso_segsz = hdr->gso_size;
1055                         m->l4_len = sizeof(struct rte_udp_hdr);
1056                         break;
1057                 default:
1058                         RTE_LOG(WARNING, VHOST_DATA,
1059                                 "unsupported gso type %u.\n", hdr->gso_type);
1060                         break;
1061                 }
1062         }
1063 }
1064
1065 static __rte_noinline void
1066 copy_vnet_hdr_from_desc(struct virtio_net_hdr *hdr,
1067                 struct buf_vector *buf_vec)
1068 {
1069         uint64_t len;
1070         uint64_t remain = sizeof(struct virtio_net_hdr);
1071         uint64_t src;
1072         uint64_t dst = (uint64_t)(uintptr_t)hdr;
1073
1074         while (remain) {
1075                 len = RTE_MIN(remain, buf_vec->buf_len);
1076                 src = buf_vec->buf_addr;
1077                 rte_memcpy((void *)(uintptr_t)dst,
1078                                 (void *)(uintptr_t)src, len);
1079
1080                 remain -= len;
1081                 dst += len;
1082                 buf_vec++;
1083         }
1084 }
1085
1086 static __rte_always_inline int
1087 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
1088                   struct buf_vector *buf_vec, uint16_t nr_vec,
1089                   struct rte_mbuf *m, struct rte_mempool *mbuf_pool)
1090 {
1091         uint32_t buf_avail, buf_offset;
1092         uint64_t buf_addr, buf_iova, buf_len;
1093         uint32_t mbuf_avail, mbuf_offset;
1094         uint32_t cpy_len;
1095         struct rte_mbuf *cur = m, *prev = m;
1096         struct virtio_net_hdr tmp_hdr;
1097         struct virtio_net_hdr *hdr = NULL;
1098         /* A counter to avoid desc dead loop chain */
1099         uint16_t vec_idx = 0;
1100         struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
1101         int error = 0;
1102
1103         buf_addr = buf_vec[vec_idx].buf_addr;
1104         buf_iova = buf_vec[vec_idx].buf_iova;
1105         buf_len = buf_vec[vec_idx].buf_len;
1106
1107         if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
1108                 error = -1;
1109                 goto out;
1110         }
1111
1112         if (virtio_net_with_host_offload(dev)) {
1113                 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
1114                         /*
1115                          * No luck, the virtio-net header doesn't fit
1116                          * in a contiguous virtual area.
1117                          */
1118                         copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
1119                         hdr = &tmp_hdr;
1120                 } else {
1121                         hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
1122                 }
1123         }
1124
1125         /*
1126          * A virtio driver normally uses at least 2 desc buffers
1127          * for Tx: the first for storing the header, and others
1128          * for storing the data.
1129          */
1130         if (unlikely(buf_len < dev->vhost_hlen)) {
1131                 buf_offset = dev->vhost_hlen - buf_len;
1132                 vec_idx++;
1133                 buf_addr = buf_vec[vec_idx].buf_addr;
1134                 buf_iova = buf_vec[vec_idx].buf_iova;
1135                 buf_len = buf_vec[vec_idx].buf_len;
1136                 buf_avail  = buf_len - buf_offset;
1137         } else if (buf_len == dev->vhost_hlen) {
1138                 if (unlikely(++vec_idx >= nr_vec))
1139                         goto out;
1140                 buf_addr = buf_vec[vec_idx].buf_addr;
1141                 buf_iova = buf_vec[vec_idx].buf_iova;
1142                 buf_len = buf_vec[vec_idx].buf_len;
1143
1144                 buf_offset = 0;
1145                 buf_avail = buf_len;
1146         } else {
1147                 buf_offset = dev->vhost_hlen;
1148                 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
1149         }
1150
1151         PRINT_PACKET(dev,
1152                         (uintptr_t)(buf_addr + buf_offset),
1153                         (uint32_t)buf_avail, 0);
1154
1155         mbuf_offset = 0;
1156         mbuf_avail  = m->buf_len - RTE_PKTMBUF_HEADROOM;
1157         while (1) {
1158                 uint64_t hpa;
1159
1160                 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1161
1162                 /*
1163                  * A desc buf might across two host physical pages that are
1164                  * not continuous. In such case (gpa_to_hpa returns 0), data
1165                  * will be copied even though zero copy is enabled.
1166                  */
1167                 if (unlikely(dev->dequeue_zero_copy && (hpa = gpa_to_hpa(dev,
1168                                         buf_iova + buf_offset, cpy_len)))) {
1169                         cur->data_len = cpy_len;
1170                         cur->data_off = 0;
1171                         cur->buf_addr =
1172                                 (void *)(uintptr_t)(buf_addr + buf_offset);
1173                         cur->buf_iova = hpa;
1174
1175                         /*
1176                          * In zero copy mode, one mbuf can only reference data
1177                          * for one or partial of one desc buff.
1178                          */
1179                         mbuf_avail = cpy_len;
1180                 } else {
1181                         if (likely(cpy_len > MAX_BATCH_LEN ||
1182                                    vq->batch_copy_nb_elems >= vq->size ||
1183                                    (hdr && cur == m))) {
1184                                 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
1185                                                                    mbuf_offset),
1186                                            (void *)((uintptr_t)(buf_addr +
1187                                                            buf_offset)),
1188                                            cpy_len);
1189                         } else {
1190                                 batch_copy[vq->batch_copy_nb_elems].dst =
1191                                         rte_pktmbuf_mtod_offset(cur, void *,
1192                                                                 mbuf_offset);
1193                                 batch_copy[vq->batch_copy_nb_elems].src =
1194                                         (void *)((uintptr_t)(buf_addr +
1195                                                                 buf_offset));
1196                                 batch_copy[vq->batch_copy_nb_elems].len =
1197                                         cpy_len;
1198                                 vq->batch_copy_nb_elems++;
1199                         }
1200                 }
1201
1202                 mbuf_avail  -= cpy_len;
1203                 mbuf_offset += cpy_len;
1204                 buf_avail -= cpy_len;
1205                 buf_offset += cpy_len;
1206
1207                 /* This buf reaches to its end, get the next one */
1208                 if (buf_avail == 0) {
1209                         if (++vec_idx >= nr_vec)
1210                                 break;
1211
1212                         buf_addr = buf_vec[vec_idx].buf_addr;
1213                         buf_iova = buf_vec[vec_idx].buf_iova;
1214                         buf_len = buf_vec[vec_idx].buf_len;
1215
1216                         buf_offset = 0;
1217                         buf_avail  = buf_len;
1218
1219                         PRINT_PACKET(dev, (uintptr_t)buf_addr,
1220                                         (uint32_t)buf_avail, 0);
1221                 }
1222
1223                 /*
1224                  * This mbuf reaches to its end, get a new one
1225                  * to hold more data.
1226                  */
1227                 if (mbuf_avail == 0) {
1228                         cur = rte_pktmbuf_alloc(mbuf_pool);
1229                         if (unlikely(cur == NULL)) {
1230                                 RTE_LOG(ERR, VHOST_DATA, "Failed to "
1231                                         "allocate memory for mbuf.\n");
1232                                 error = -1;
1233                                 goto out;
1234                         }
1235                         if (unlikely(dev->dequeue_zero_copy))
1236                                 rte_mbuf_refcnt_update(cur, 1);
1237
1238                         prev->next = cur;
1239                         prev->data_len = mbuf_offset;
1240                         m->nb_segs += 1;
1241                         m->pkt_len += mbuf_offset;
1242                         prev = cur;
1243
1244                         mbuf_offset = 0;
1245                         mbuf_avail  = cur->buf_len - RTE_PKTMBUF_HEADROOM;
1246                 }
1247         }
1248
1249         prev->data_len = mbuf_offset;
1250         m->pkt_len    += mbuf_offset;
1251
1252         if (hdr)
1253                 vhost_dequeue_offload(hdr, m);
1254
1255 out:
1256
1257         return error;
1258 }
1259
1260 static __rte_always_inline struct zcopy_mbuf *
1261 get_zmbuf(struct vhost_virtqueue *vq)
1262 {
1263         uint16_t i;
1264         uint16_t last;
1265         int tries = 0;
1266
1267         /* search [last_zmbuf_idx, zmbuf_size) */
1268         i = vq->last_zmbuf_idx;
1269         last = vq->zmbuf_size;
1270
1271 again:
1272         for (; i < last; i++) {
1273                 if (vq->zmbufs[i].in_use == 0) {
1274                         vq->last_zmbuf_idx = i + 1;
1275                         vq->zmbufs[i].in_use = 1;
1276                         return &vq->zmbufs[i];
1277                 }
1278         }
1279
1280         tries++;
1281         if (tries == 1) {
1282                 /* search [0, last_zmbuf_idx) */
1283                 i = 0;
1284                 last = vq->last_zmbuf_idx;
1285                 goto again;
1286         }
1287
1288         return NULL;
1289 }
1290
1291 static __rte_noinline uint16_t
1292 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1293         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1294 {
1295         uint16_t i;
1296         uint16_t free_entries;
1297
1298         if (unlikely(dev->dequeue_zero_copy)) {
1299                 struct zcopy_mbuf *zmbuf, *next;
1300
1301                 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1302                      zmbuf != NULL; zmbuf = next) {
1303                         next = TAILQ_NEXT(zmbuf, next);
1304
1305                         if (mbuf_is_consumed(zmbuf->mbuf)) {
1306                                 update_shadow_used_ring_split(vq,
1307                                                 zmbuf->desc_idx, 0);
1308                                 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1309                                 restore_mbuf(zmbuf->mbuf);
1310                                 rte_pktmbuf_free(zmbuf->mbuf);
1311                                 put_zmbuf(zmbuf);
1312                                 vq->nr_zmbuf -= 1;
1313                         }
1314                 }
1315
1316                 if (likely(vq->shadow_used_idx)) {
1317                         flush_shadow_used_ring_split(dev, vq);
1318                         vhost_vring_call_split(dev, vq);
1319                 }
1320         }
1321
1322         free_entries = *((volatile uint16_t *)&vq->avail->idx) -
1323                         vq->last_avail_idx;
1324         if (free_entries == 0)
1325                 return 0;
1326
1327         /*
1328          * The ordering between avail index and
1329          * desc reads needs to be enforced.
1330          */
1331         rte_smp_rmb();
1332
1333         rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1334
1335         VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1336
1337         count = RTE_MIN(count, MAX_PKT_BURST);
1338         count = RTE_MIN(count, free_entries);
1339         VHOST_LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1340                         dev->vid, count);
1341
1342         for (i = 0; i < count; i++) {
1343                 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1344                 uint16_t head_idx;
1345                 uint32_t dummy_len;
1346                 uint16_t nr_vec = 0;
1347                 int err;
1348
1349                 if (unlikely(fill_vec_buf_split(dev, vq,
1350                                                 vq->last_avail_idx + i,
1351                                                 &nr_vec, buf_vec,
1352                                                 &head_idx, &dummy_len,
1353                                                 VHOST_ACCESS_RO) < 0))
1354                         break;
1355
1356                 if (likely(dev->dequeue_zero_copy == 0))
1357                         update_shadow_used_ring_split(vq, head_idx, 0);
1358
1359                 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
1360                 if (unlikely(pkts[i] == NULL)) {
1361                         RTE_LOG(ERR, VHOST_DATA,
1362                                 "Failed to allocate memory for mbuf.\n");
1363                         break;
1364                 }
1365
1366                 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
1367                                 mbuf_pool);
1368                 if (unlikely(err)) {
1369                         rte_pktmbuf_free(pkts[i]);
1370                         break;
1371                 }
1372
1373                 if (unlikely(dev->dequeue_zero_copy)) {
1374                         struct zcopy_mbuf *zmbuf;
1375
1376                         zmbuf = get_zmbuf(vq);
1377                         if (!zmbuf) {
1378                                 rte_pktmbuf_free(pkts[i]);
1379                                 break;
1380                         }
1381                         zmbuf->mbuf = pkts[i];
1382                         zmbuf->desc_idx = head_idx;
1383
1384                         /*
1385                          * Pin lock the mbuf; we will check later to see
1386                          * whether the mbuf is freed (when we are the last
1387                          * user) or not. If that's the case, we then could
1388                          * update the used ring safely.
1389                          */
1390                         rte_mbuf_refcnt_update(pkts[i], 1);
1391
1392                         vq->nr_zmbuf += 1;
1393                         TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1394                 }
1395         }
1396         vq->last_avail_idx += i;
1397
1398         if (likely(dev->dequeue_zero_copy == 0)) {
1399                 do_data_copy_dequeue(vq);
1400                 if (unlikely(i < count))
1401                         vq->shadow_used_idx = i;
1402                 if (likely(vq->shadow_used_idx)) {
1403                         flush_shadow_used_ring_split(dev, vq);
1404                         vhost_vring_call_split(dev, vq);
1405                 }
1406         }
1407
1408         return i;
1409 }
1410
1411 static __rte_noinline uint16_t
1412 virtio_dev_tx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
1413         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1414 {
1415         uint16_t i;
1416
1417         if (unlikely(dev->dequeue_zero_copy)) {
1418                 struct zcopy_mbuf *zmbuf, *next;
1419
1420                 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1421                      zmbuf != NULL; zmbuf = next) {
1422                         next = TAILQ_NEXT(zmbuf, next);
1423
1424                         if (mbuf_is_consumed(zmbuf->mbuf)) {
1425                                 update_shadow_used_ring_packed(vq,
1426                                                 zmbuf->desc_idx,
1427                                                 0,
1428                                                 zmbuf->desc_count);
1429
1430                                 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1431                                 restore_mbuf(zmbuf->mbuf);
1432                                 rte_pktmbuf_free(zmbuf->mbuf);
1433                                 put_zmbuf(zmbuf);
1434                                 vq->nr_zmbuf -= 1;
1435                         }
1436                 }
1437
1438                 if (likely(vq->shadow_used_idx)) {
1439                         flush_shadow_used_ring_packed(dev, vq);
1440                         vhost_vring_call_packed(dev, vq);
1441                 }
1442         }
1443
1444         VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1445
1446         count = RTE_MIN(count, MAX_PKT_BURST);
1447         VHOST_LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1448                         dev->vid, count);
1449
1450         for (i = 0; i < count; i++) {
1451                 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1452                 uint16_t buf_id;
1453                 uint32_t dummy_len;
1454                 uint16_t desc_count, nr_vec = 0;
1455                 int err;
1456
1457                 if (unlikely(fill_vec_buf_packed(dev, vq,
1458                                                 vq->last_avail_idx, &desc_count,
1459                                                 buf_vec, &nr_vec,
1460                                                 &buf_id, &dummy_len,
1461                                                 VHOST_ACCESS_RO) < 0))
1462                         break;
1463
1464                 if (likely(dev->dequeue_zero_copy == 0))
1465                         update_shadow_used_ring_packed(vq, buf_id, 0,
1466                                         desc_count);
1467
1468                 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
1469                 if (unlikely(pkts[i] == NULL)) {
1470                         RTE_LOG(ERR, VHOST_DATA,
1471                                 "Failed to allocate memory for mbuf.\n");
1472                         break;
1473                 }
1474
1475                 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
1476                                 mbuf_pool);
1477                 if (unlikely(err)) {
1478                         rte_pktmbuf_free(pkts[i]);
1479                         break;
1480                 }
1481
1482                 if (unlikely(dev->dequeue_zero_copy)) {
1483                         struct zcopy_mbuf *zmbuf;
1484
1485                         zmbuf = get_zmbuf(vq);
1486                         if (!zmbuf) {
1487                                 rte_pktmbuf_free(pkts[i]);
1488                                 break;
1489                         }
1490                         zmbuf->mbuf = pkts[i];
1491                         zmbuf->desc_idx = buf_id;
1492                         zmbuf->desc_count = desc_count;
1493
1494                         /*
1495                          * Pin lock the mbuf; we will check later to see
1496                          * whether the mbuf is freed (when we are the last
1497                          * user) or not. If that's the case, we then could
1498                          * update the used ring safely.
1499                          */
1500                         rte_mbuf_refcnt_update(pkts[i], 1);
1501
1502                         vq->nr_zmbuf += 1;
1503                         TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1504                 }
1505
1506                 vq->last_avail_idx += desc_count;
1507                 if (vq->last_avail_idx >= vq->size) {
1508                         vq->last_avail_idx -= vq->size;
1509                         vq->avail_wrap_counter ^= 1;
1510                 }
1511         }
1512
1513         if (likely(dev->dequeue_zero_copy == 0)) {
1514                 do_data_copy_dequeue(vq);
1515                 if (unlikely(i < count))
1516                         vq->shadow_used_idx = i;
1517                 if (likely(vq->shadow_used_idx)) {
1518                         flush_shadow_used_ring_packed(dev, vq);
1519                         vhost_vring_call_packed(dev, vq);
1520                 }
1521         }
1522
1523         return i;
1524 }
1525
1526 uint16_t
1527 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
1528         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1529 {
1530         struct virtio_net *dev;
1531         struct rte_mbuf *rarp_mbuf = NULL;
1532         struct vhost_virtqueue *vq;
1533
1534         dev = get_device(vid);
1535         if (!dev)
1536                 return 0;
1537
1538         if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1539                 RTE_LOG(ERR, VHOST_DATA,
1540                         "(%d) %s: built-in vhost net backend is disabled.\n",
1541                         dev->vid, __func__);
1542                 return 0;
1543         }
1544
1545         if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
1546                 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
1547                         dev->vid, __func__, queue_id);
1548                 return 0;
1549         }
1550
1551         vq = dev->virtqueue[queue_id];
1552
1553         if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
1554                 return 0;
1555
1556         if (unlikely(vq->enabled == 0)) {
1557                 count = 0;
1558                 goto out_access_unlock;
1559         }
1560
1561         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1562                 vhost_user_iotlb_rd_lock(vq);
1563
1564         if (unlikely(vq->access_ok == 0))
1565                 if (unlikely(vring_translate(dev, vq) < 0)) {
1566                         count = 0;
1567                         goto out;
1568                 }
1569
1570         /*
1571          * Construct a RARP broadcast packet, and inject it to the "pkts"
1572          * array, to looks like that guest actually send such packet.
1573          *
1574          * Check user_send_rarp() for more information.
1575          *
1576          * broadcast_rarp shares a cacheline in the virtio_net structure
1577          * with some fields that are accessed during enqueue and
1578          * rte_atomic16_cmpset() causes a write if using cmpxchg. This could
1579          * result in false sharing between enqueue and dequeue.
1580          *
1581          * Prevent unnecessary false sharing by reading broadcast_rarp first
1582          * and only performing cmpset if the read indicates it is likely to
1583          * be set.
1584          */
1585         if (unlikely(rte_atomic16_read(&dev->broadcast_rarp) &&
1586                         rte_atomic16_cmpset((volatile uint16_t *)
1587                                 &dev->broadcast_rarp.cnt, 1, 0))) {
1588
1589                 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
1590                 if (rarp_mbuf == NULL) {
1591                         RTE_LOG(ERR, VHOST_DATA,
1592                                 "Failed to make RARP packet.\n");
1593                         count = 0;
1594                         goto out;
1595                 }
1596                 count -= 1;
1597         }
1598
1599         if (vq_is_packed(dev))
1600                 count = virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count);
1601         else
1602                 count = virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count);
1603
1604 out:
1605         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1606                 vhost_user_iotlb_rd_unlock(vq);
1607
1608 out_access_unlock:
1609         rte_spinlock_unlock(&vq->access_lock);
1610
1611         if (unlikely(rarp_mbuf != NULL)) {
1612                 /*
1613                  * Inject it to the head of "pkts" array, so that switch's mac
1614                  * learning table will get updated first.
1615                  */
1616                 memmove(&pkts[1], pkts, count * sizeof(struct rte_mbuf *));
1617                 pkts[0] = rarp_mbuf;
1618                 count += 1;
1619         }
1620
1621         return count;
1622 }