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