vhost: simplify descriptor buffer prefetching
[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         if (unlikely(!desc_is_avail(&descs[avail_idx], wrap_counter)))
507                 return -1;
508
509         /*
510          * The ordering between desc flags and desc
511          * content reads need to be enforced.
512          */
513         rte_smp_rmb();
514
515         *desc_count = 0;
516         *len = 0;
517
518         while (1) {
519                 if (unlikely(vec_id >= BUF_VECTOR_MAX))
520                         return -1;
521
522                 if (unlikely(*desc_count >= vq->size))
523                         return -1;
524
525                 *desc_count += 1;
526                 *buf_id = descs[avail_idx].id;
527
528                 if (descs[avail_idx].flags & VRING_DESC_F_INDIRECT) {
529                         if (unlikely(fill_vec_buf_packed_indirect(dev, vq,
530                                                         &descs[avail_idx],
531                                                         &vec_id, buf_vec,
532                                                         len, perm) < 0))
533                                 return -1;
534                 } else {
535                         *len += descs[avail_idx].len;
536
537                         if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
538                                                         descs[avail_idx].addr,
539                                                         descs[avail_idx].len,
540                                                         perm)))
541                                 return -1;
542                 }
543
544                 if ((descs[avail_idx].flags & VRING_DESC_F_NEXT) == 0)
545                         break;
546
547                 if (++avail_idx >= vq->size) {
548                         avail_idx -= vq->size;
549                         wrap_counter ^= 1;
550                 }
551         }
552
553         *vec_idx = vec_id;
554
555         return 0;
556 }
557
558 /*
559  * Returns -1 on fail, 0 on success
560  */
561 static inline int
562 reserve_avail_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
563                                 uint32_t size, struct buf_vector *buf_vec,
564                                 uint16_t *nr_vec, uint16_t *num_buffers,
565                                 uint16_t *nr_descs)
566 {
567         uint16_t avail_idx;
568         uint16_t vec_idx = 0;
569         uint16_t max_tries, tries = 0;
570
571         uint16_t buf_id = 0;
572         uint32_t len = 0;
573         uint16_t desc_count;
574
575         *num_buffers = 0;
576         avail_idx = vq->last_avail_idx;
577
578         if (rxvq_is_mergeable(dev))
579                 max_tries = vq->size - 1;
580         else
581                 max_tries = 1;
582
583         while (size > 0) {
584                 /*
585                  * if we tried all available ring items, and still
586                  * can't get enough buf, it means something abnormal
587                  * happened.
588                  */
589                 if (unlikely(++tries > max_tries))
590                         return -1;
591
592                 if (unlikely(fill_vec_buf_packed(dev, vq,
593                                                 avail_idx, &desc_count,
594                                                 buf_vec, &vec_idx,
595                                                 &buf_id, &len,
596                                                 VHOST_ACCESS_RW) < 0))
597                         return -1;
598
599                 len = RTE_MIN(len, size);
600                 update_shadow_used_ring_packed(vq, buf_id, len, desc_count);
601                 size -= len;
602
603                 avail_idx += desc_count;
604                 if (avail_idx >= vq->size)
605                         avail_idx -= vq->size;
606
607                 *nr_descs += desc_count;
608                 *num_buffers += 1;
609         }
610
611         *nr_vec = vec_idx;
612
613         return 0;
614 }
615
616 static __rte_noinline void
617 copy_vnet_hdr_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
618                 struct buf_vector *buf_vec,
619                 struct virtio_net_hdr_mrg_rxbuf *hdr)
620 {
621         uint64_t len;
622         uint64_t remain = dev->vhost_hlen;
623         uint64_t src = (uint64_t)(uintptr_t)hdr, dst;
624         uint64_t iova = buf_vec->buf_iova;
625
626         while (remain) {
627                 len = RTE_MIN(remain,
628                                 buf_vec->buf_len);
629                 dst = buf_vec->buf_addr;
630                 rte_memcpy((void *)(uintptr_t)dst,
631                                 (void *)(uintptr_t)src,
632                                 len);
633
634                 PRINT_PACKET(dev, (uintptr_t)dst,
635                                 (uint32_t)len, 0);
636                 vhost_log_cache_write(dev, vq,
637                                 iova, len);
638
639                 remain -= len;
640                 iova += len;
641                 src += len;
642                 buf_vec++;
643         }
644 }
645
646 static __rte_always_inline int
647 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
648                             struct rte_mbuf *m, struct buf_vector *buf_vec,
649                             uint16_t nr_vec, uint16_t num_buffers)
650 {
651         uint32_t vec_idx = 0;
652         uint32_t mbuf_offset, mbuf_avail;
653         uint32_t buf_offset, buf_avail;
654         uint64_t buf_addr, buf_iova, buf_len;
655         uint32_t cpy_len;
656         uint64_t hdr_addr;
657         struct rte_mbuf *hdr_mbuf;
658         struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
659         struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
660         int error = 0;
661
662         if (unlikely(m == NULL)) {
663                 error = -1;
664                 goto out;
665         }
666
667         buf_addr = buf_vec[vec_idx].buf_addr;
668         buf_iova = buf_vec[vec_idx].buf_iova;
669         buf_len = buf_vec[vec_idx].buf_len;
670
671         if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
672                 error = -1;
673                 goto out;
674         }
675
676         hdr_mbuf = m;
677         hdr_addr = buf_addr;
678         if (unlikely(buf_len < dev->vhost_hlen))
679                 hdr = &tmp_hdr;
680         else
681                 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(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         if (unlikely(buf_len < dev->vhost_hlen)) {
687                 buf_offset = dev->vhost_hlen - buf_len;
688                 vec_idx++;
689                 buf_addr = buf_vec[vec_idx].buf_addr;
690                 buf_iova = buf_vec[vec_idx].buf_iova;
691                 buf_len = buf_vec[vec_idx].buf_len;
692                 buf_avail = buf_len - buf_offset;
693         } else {
694                 buf_offset = dev->vhost_hlen;
695                 buf_avail = buf_len - dev->vhost_hlen;
696         }
697
698         mbuf_avail  = rte_pktmbuf_data_len(m);
699         mbuf_offset = 0;
700         while (mbuf_avail != 0 || m->next != NULL) {
701                 /* done with current buf, get the next one */
702                 if (buf_avail == 0) {
703                         vec_idx++;
704                         if (unlikely(vec_idx >= nr_vec)) {
705                                 error = -1;
706                                 goto out;
707                         }
708
709                         buf_addr = buf_vec[vec_idx].buf_addr;
710                         buf_iova = buf_vec[vec_idx].buf_iova;
711                         buf_len = buf_vec[vec_idx].buf_len;
712
713                         buf_offset = 0;
714                         buf_avail  = buf_len;
715                 }
716
717                 /* done with current mbuf, get the next one */
718                 if (mbuf_avail == 0) {
719                         m = m->next;
720
721                         mbuf_offset = 0;
722                         mbuf_avail  = rte_pktmbuf_data_len(m);
723                 }
724
725                 if (hdr_addr) {
726                         virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
727                         if (rxvq_is_mergeable(dev))
728                                 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
729                                                 num_buffers);
730
731                         if (unlikely(hdr == &tmp_hdr)) {
732                                 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
733                         } else {
734                                 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
735                                                 dev->vhost_hlen, 0);
736                                 vhost_log_cache_write(dev, vq,
737                                                 buf_vec[0].buf_iova,
738                                                 dev->vhost_hlen);
739                         }
740
741                         hdr_addr = 0;
742                 }
743
744                 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
745
746                 if (likely(cpy_len > MAX_BATCH_LEN ||
747                                         vq->batch_copy_nb_elems >= vq->size)) {
748                         rte_memcpy((void *)((uintptr_t)(buf_addr + buf_offset)),
749                                 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
750                                 cpy_len);
751                         vhost_log_cache_write(dev, vq, buf_iova + buf_offset,
752                                         cpy_len);
753                         PRINT_PACKET(dev, (uintptr_t)(buf_addr + buf_offset),
754                                 cpy_len, 0);
755                 } else {
756                         batch_copy[vq->batch_copy_nb_elems].dst =
757                                 (void *)((uintptr_t)(buf_addr + buf_offset));
758                         batch_copy[vq->batch_copy_nb_elems].src =
759                                 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
760                         batch_copy[vq->batch_copy_nb_elems].log_addr =
761                                 buf_iova + buf_offset;
762                         batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
763                         vq->batch_copy_nb_elems++;
764                 }
765
766                 mbuf_avail  -= cpy_len;
767                 mbuf_offset += cpy_len;
768                 buf_avail  -= cpy_len;
769                 buf_offset += cpy_len;
770         }
771
772 out:
773
774         return error;
775 }
776
777 static __rte_noinline uint32_t
778 virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
779         struct rte_mbuf **pkts, uint32_t count)
780 {
781         uint32_t pkt_idx = 0;
782         uint16_t num_buffers;
783         struct buf_vector buf_vec[BUF_VECTOR_MAX];
784         uint16_t avail_head;
785
786         avail_head = *((volatile uint16_t *)&vq->avail->idx);
787
788         /*
789          * The ordering between avail index and
790          * desc reads needs to be enforced.
791          */
792         rte_smp_rmb();
793
794         rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
795
796         for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
797                 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
798                 uint16_t nr_vec = 0;
799
800                 if (unlikely(reserve_avail_buf_split(dev, vq,
801                                                 pkt_len, buf_vec, &num_buffers,
802                                                 avail_head, &nr_vec) < 0)) {
803                         VHOST_LOG_DEBUG(VHOST_DATA,
804                                 "(%d) failed to get enough desc from vring\n",
805                                 dev->vid);
806                         vq->shadow_used_idx -= num_buffers;
807                         break;
808                 }
809
810                 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
811                         dev->vid, vq->last_avail_idx,
812                         vq->last_avail_idx + num_buffers);
813
814                 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
815                                                 buf_vec, nr_vec,
816                                                 num_buffers) < 0) {
817                         vq->shadow_used_idx -= num_buffers;
818                         break;
819                 }
820
821                 vq->last_avail_idx += num_buffers;
822         }
823
824         do_data_copy_enqueue(dev, vq);
825
826         if (likely(vq->shadow_used_idx)) {
827                 flush_shadow_used_ring_split(dev, vq);
828                 vhost_vring_call_split(dev, vq);
829         }
830
831         return pkt_idx;
832 }
833
834 static __rte_noinline uint32_t
835 virtio_dev_rx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
836         struct rte_mbuf **pkts, uint32_t count)
837 {
838         uint32_t pkt_idx = 0;
839         uint16_t num_buffers;
840         struct buf_vector buf_vec[BUF_VECTOR_MAX];
841
842         for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
843                 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
844                 uint16_t nr_vec = 0;
845                 uint16_t nr_descs = 0;
846
847                 if (unlikely(reserve_avail_buf_packed(dev, vq,
848                                                 pkt_len, buf_vec, &nr_vec,
849                                                 &num_buffers, &nr_descs) < 0)) {
850                         VHOST_LOG_DEBUG(VHOST_DATA,
851                                 "(%d) failed to get enough desc from vring\n",
852                                 dev->vid);
853                         vq->shadow_used_idx -= num_buffers;
854                         break;
855                 }
856
857                 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
858                         dev->vid, vq->last_avail_idx,
859                         vq->last_avail_idx + num_buffers);
860
861                 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
862                                                 buf_vec, nr_vec,
863                                                 num_buffers) < 0) {
864                         vq->shadow_used_idx -= num_buffers;
865                         break;
866                 }
867
868                 vq->last_avail_idx += nr_descs;
869                 if (vq->last_avail_idx >= vq->size) {
870                         vq->last_avail_idx -= vq->size;
871                         vq->avail_wrap_counter ^= 1;
872                 }
873         }
874
875         do_data_copy_enqueue(dev, vq);
876
877         if (likely(vq->shadow_used_idx)) {
878                 flush_shadow_used_ring_packed(dev, vq);
879                 vhost_vring_call_packed(dev, vq);
880         }
881
882         return pkt_idx;
883 }
884
885 static __rte_always_inline uint32_t
886 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
887         struct rte_mbuf **pkts, uint32_t count)
888 {
889         struct vhost_virtqueue *vq;
890         uint32_t nb_tx = 0;
891
892         VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
893         if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
894                 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
895                         dev->vid, __func__, queue_id);
896                 return 0;
897         }
898
899         vq = dev->virtqueue[queue_id];
900
901         rte_spinlock_lock(&vq->access_lock);
902
903         if (unlikely(vq->enabled == 0))
904                 goto out_access_unlock;
905
906         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
907                 vhost_user_iotlb_rd_lock(vq);
908
909         if (unlikely(vq->access_ok == 0))
910                 if (unlikely(vring_translate(dev, vq) < 0))
911                         goto out;
912
913         count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
914         if (count == 0)
915                 goto out;
916
917         if (vq_is_packed(dev))
918                 nb_tx = virtio_dev_rx_packed(dev, vq, pkts, count);
919         else
920                 nb_tx = virtio_dev_rx_split(dev, vq, pkts, count);
921
922 out:
923         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
924                 vhost_user_iotlb_rd_unlock(vq);
925
926 out_access_unlock:
927         rte_spinlock_unlock(&vq->access_lock);
928
929         return nb_tx;
930 }
931
932 uint16_t
933 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
934         struct rte_mbuf **pkts, uint16_t count)
935 {
936         struct virtio_net *dev = get_device(vid);
937
938         if (!dev)
939                 return 0;
940
941         if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
942                 RTE_LOG(ERR, VHOST_DATA,
943                         "(%d) %s: built-in vhost net backend is disabled.\n",
944                         dev->vid, __func__);
945                 return 0;
946         }
947
948         return virtio_dev_rx(dev, queue_id, pkts, count);
949 }
950
951 static inline bool
952 virtio_net_with_host_offload(struct virtio_net *dev)
953 {
954         if (dev->features &
955                         ((1ULL << VIRTIO_NET_F_CSUM) |
956                          (1ULL << VIRTIO_NET_F_HOST_ECN) |
957                          (1ULL << VIRTIO_NET_F_HOST_TSO4) |
958                          (1ULL << VIRTIO_NET_F_HOST_TSO6) |
959                          (1ULL << VIRTIO_NET_F_HOST_UFO)))
960                 return true;
961
962         return false;
963 }
964
965 static void
966 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
967 {
968         struct rte_ipv4_hdr *ipv4_hdr;
969         struct rte_ipv6_hdr *ipv6_hdr;
970         void *l3_hdr = NULL;
971         struct rte_ether_hdr *eth_hdr;
972         uint16_t ethertype;
973
974         eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
975
976         m->l2_len = sizeof(struct rte_ether_hdr);
977         ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
978
979         if (ethertype == RTE_ETHER_TYPE_VLAN) {
980                 struct rte_vlan_hdr *vlan_hdr =
981                         (struct rte_vlan_hdr *)(eth_hdr + 1);
982
983                 m->l2_len += sizeof(struct rte_vlan_hdr);
984                 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
985         }
986
987         l3_hdr = (char *)eth_hdr + m->l2_len;
988
989         switch (ethertype) {
990         case RTE_ETHER_TYPE_IPV4:
991                 ipv4_hdr = l3_hdr;
992                 *l4_proto = ipv4_hdr->next_proto_id;
993                 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
994                 *l4_hdr = (char *)l3_hdr + m->l3_len;
995                 m->ol_flags |= PKT_TX_IPV4;
996                 break;
997         case RTE_ETHER_TYPE_IPV6:
998                 ipv6_hdr = l3_hdr;
999                 *l4_proto = ipv6_hdr->proto;
1000                 m->l3_len = sizeof(struct rte_ipv6_hdr);
1001                 *l4_hdr = (char *)l3_hdr + m->l3_len;
1002                 m->ol_flags |= PKT_TX_IPV6;
1003                 break;
1004         default:
1005                 m->l3_len = 0;
1006                 *l4_proto = 0;
1007                 *l4_hdr = NULL;
1008                 break;
1009         }
1010 }
1011
1012 static __rte_always_inline void
1013 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
1014 {
1015         uint16_t l4_proto = 0;
1016         void *l4_hdr = NULL;
1017         struct rte_tcp_hdr *tcp_hdr = NULL;
1018
1019         if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
1020                 return;
1021
1022         parse_ethernet(m, &l4_proto, &l4_hdr);
1023         if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1024                 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
1025                         switch (hdr->csum_offset) {
1026                         case (offsetof(struct rte_tcp_hdr, cksum)):
1027                                 if (l4_proto == IPPROTO_TCP)
1028                                         m->ol_flags |= PKT_TX_TCP_CKSUM;
1029                                 break;
1030                         case (offsetof(struct rte_udp_hdr, dgram_cksum)):
1031                                 if (l4_proto == IPPROTO_UDP)
1032                                         m->ol_flags |= PKT_TX_UDP_CKSUM;
1033                                 break;
1034                         case (offsetof(struct rte_sctp_hdr, cksum)):
1035                                 if (l4_proto == IPPROTO_SCTP)
1036                                         m->ol_flags |= PKT_TX_SCTP_CKSUM;
1037                                 break;
1038                         default:
1039                                 break;
1040                         }
1041                 }
1042         }
1043
1044         if (l4_hdr && hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1045                 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1046                 case VIRTIO_NET_HDR_GSO_TCPV4:
1047                 case VIRTIO_NET_HDR_GSO_TCPV6:
1048                         tcp_hdr = l4_hdr;
1049                         m->ol_flags |= PKT_TX_TCP_SEG;
1050                         m->tso_segsz = hdr->gso_size;
1051                         m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
1052                         break;
1053                 case VIRTIO_NET_HDR_GSO_UDP:
1054                         m->ol_flags |= PKT_TX_UDP_SEG;
1055                         m->tso_segsz = hdr->gso_size;
1056                         m->l4_len = sizeof(struct rte_udp_hdr);
1057                         break;
1058                 default:
1059                         RTE_LOG(WARNING, VHOST_DATA,
1060                                 "unsupported gso type %u.\n", hdr->gso_type);
1061                         break;
1062                 }
1063         }
1064 }
1065
1066 static __rte_noinline void
1067 copy_vnet_hdr_from_desc(struct virtio_net_hdr *hdr,
1068                 struct buf_vector *buf_vec)
1069 {
1070         uint64_t len;
1071         uint64_t remain = sizeof(struct virtio_net_hdr);
1072         uint64_t src;
1073         uint64_t dst = (uint64_t)(uintptr_t)hdr;
1074
1075         while (remain) {
1076                 len = RTE_MIN(remain, buf_vec->buf_len);
1077                 src = buf_vec->buf_addr;
1078                 rte_memcpy((void *)(uintptr_t)dst,
1079                                 (void *)(uintptr_t)src, len);
1080
1081                 remain -= len;
1082                 dst += len;
1083                 buf_vec++;
1084         }
1085 }
1086
1087 static __rte_always_inline int
1088 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
1089                   struct buf_vector *buf_vec, uint16_t nr_vec,
1090                   struct rte_mbuf *m, struct rte_mempool *mbuf_pool)
1091 {
1092         uint32_t buf_avail, buf_offset;
1093         uint64_t buf_addr, buf_iova, buf_len;
1094         uint32_t mbuf_avail, mbuf_offset;
1095         uint32_t cpy_len;
1096         struct rte_mbuf *cur = m, *prev = m;
1097         struct virtio_net_hdr tmp_hdr;
1098         struct virtio_net_hdr *hdr = NULL;
1099         /* A counter to avoid desc dead loop chain */
1100         uint16_t vec_idx = 0;
1101         struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
1102         int error = 0;
1103
1104         buf_addr = buf_vec[vec_idx].buf_addr;
1105         buf_iova = buf_vec[vec_idx].buf_iova;
1106         buf_len = buf_vec[vec_idx].buf_len;
1107
1108         if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
1109                 error = -1;
1110                 goto out;
1111         }
1112
1113         if (virtio_net_with_host_offload(dev)) {
1114                 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
1115                         /*
1116                          * No luck, the virtio-net header doesn't fit
1117                          * in a contiguous virtual area.
1118                          */
1119                         copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
1120                         hdr = &tmp_hdr;
1121                 } else {
1122                         hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
1123                 }
1124         }
1125
1126         /*
1127          * A virtio driver normally uses at least 2 desc buffers
1128          * for Tx: the first for storing the header, and others
1129          * for storing the data.
1130          */
1131         if (unlikely(buf_len < dev->vhost_hlen)) {
1132                 buf_offset = dev->vhost_hlen - buf_len;
1133                 vec_idx++;
1134                 buf_addr = buf_vec[vec_idx].buf_addr;
1135                 buf_iova = buf_vec[vec_idx].buf_iova;
1136                 buf_len = buf_vec[vec_idx].buf_len;
1137                 buf_avail  = buf_len - buf_offset;
1138         } else if (buf_len == dev->vhost_hlen) {
1139                 if (unlikely(++vec_idx >= nr_vec))
1140                         goto out;
1141                 buf_addr = buf_vec[vec_idx].buf_addr;
1142                 buf_iova = buf_vec[vec_idx].buf_iova;
1143                 buf_len = buf_vec[vec_idx].buf_len;
1144
1145                 buf_offset = 0;
1146                 buf_avail = buf_len;
1147         } else {
1148                 buf_offset = dev->vhost_hlen;
1149                 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
1150         }
1151
1152         PRINT_PACKET(dev,
1153                         (uintptr_t)(buf_addr + buf_offset),
1154                         (uint32_t)buf_avail, 0);
1155
1156         mbuf_offset = 0;
1157         mbuf_avail  = m->buf_len - RTE_PKTMBUF_HEADROOM;
1158         while (1) {
1159                 uint64_t hpa;
1160
1161                 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1162
1163                 /*
1164                  * A desc buf might across two host physical pages that are
1165                  * not continuous. In such case (gpa_to_hpa returns 0), data
1166                  * will be copied even though zero copy is enabled.
1167                  */
1168                 if (unlikely(dev->dequeue_zero_copy && (hpa = gpa_to_hpa(dev,
1169                                         buf_iova + buf_offset, cpy_len)))) {
1170                         cur->data_len = cpy_len;
1171                         cur->data_off = 0;
1172                         cur->buf_addr =
1173                                 (void *)(uintptr_t)(buf_addr + buf_offset);
1174                         cur->buf_iova = hpa;
1175
1176                         /*
1177                          * In zero copy mode, one mbuf can only reference data
1178                          * for one or partial of one desc buff.
1179                          */
1180                         mbuf_avail = cpy_len;
1181                 } else {
1182                         if (likely(cpy_len > MAX_BATCH_LEN ||
1183                                    vq->batch_copy_nb_elems >= vq->size ||
1184                                    (hdr && cur == m))) {
1185                                 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
1186                                                                    mbuf_offset),
1187                                            (void *)((uintptr_t)(buf_addr +
1188                                                            buf_offset)),
1189                                            cpy_len);
1190                         } else {
1191                                 batch_copy[vq->batch_copy_nb_elems].dst =
1192                                         rte_pktmbuf_mtod_offset(cur, void *,
1193                                                                 mbuf_offset);
1194                                 batch_copy[vq->batch_copy_nb_elems].src =
1195                                         (void *)((uintptr_t)(buf_addr +
1196                                                                 buf_offset));
1197                                 batch_copy[vq->batch_copy_nb_elems].len =
1198                                         cpy_len;
1199                                 vq->batch_copy_nb_elems++;
1200                         }
1201                 }
1202
1203                 mbuf_avail  -= cpy_len;
1204                 mbuf_offset += cpy_len;
1205                 buf_avail -= cpy_len;
1206                 buf_offset += cpy_len;
1207
1208                 /* This buf reaches to its end, get the next one */
1209                 if (buf_avail == 0) {
1210                         if (++vec_idx >= nr_vec)
1211                                 break;
1212
1213                         buf_addr = buf_vec[vec_idx].buf_addr;
1214                         buf_iova = buf_vec[vec_idx].buf_iova;
1215                         buf_len = buf_vec[vec_idx].buf_len;
1216
1217                         buf_offset = 0;
1218                         buf_avail  = buf_len;
1219
1220                         PRINT_PACKET(dev, (uintptr_t)buf_addr,
1221                                         (uint32_t)buf_avail, 0);
1222                 }
1223
1224                 /*
1225                  * This mbuf reaches to its end, get a new one
1226                  * to hold more data.
1227                  */
1228                 if (mbuf_avail == 0) {
1229                         cur = rte_pktmbuf_alloc(mbuf_pool);
1230                         if (unlikely(cur == NULL)) {
1231                                 RTE_LOG(ERR, VHOST_DATA, "Failed to "
1232                                         "allocate memory for mbuf.\n");
1233                                 error = -1;
1234                                 goto out;
1235                         }
1236                         if (unlikely(dev->dequeue_zero_copy))
1237                                 rte_mbuf_refcnt_update(cur, 1);
1238
1239                         prev->next = cur;
1240                         prev->data_len = mbuf_offset;
1241                         m->nb_segs += 1;
1242                         m->pkt_len += mbuf_offset;
1243                         prev = cur;
1244
1245                         mbuf_offset = 0;
1246                         mbuf_avail  = cur->buf_len - RTE_PKTMBUF_HEADROOM;
1247                 }
1248         }
1249
1250         prev->data_len = mbuf_offset;
1251         m->pkt_len    += mbuf_offset;
1252
1253         if (hdr)
1254                 vhost_dequeue_offload(hdr, m);
1255
1256 out:
1257
1258         return error;
1259 }
1260
1261 static __rte_always_inline struct zcopy_mbuf *
1262 get_zmbuf(struct vhost_virtqueue *vq)
1263 {
1264         uint16_t i;
1265         uint16_t last;
1266         int tries = 0;
1267
1268         /* search [last_zmbuf_idx, zmbuf_size) */
1269         i = vq->last_zmbuf_idx;
1270         last = vq->zmbuf_size;
1271
1272 again:
1273         for (; i < last; i++) {
1274                 if (vq->zmbufs[i].in_use == 0) {
1275                         vq->last_zmbuf_idx = i + 1;
1276                         vq->zmbufs[i].in_use = 1;
1277                         return &vq->zmbufs[i];
1278                 }
1279         }
1280
1281         tries++;
1282         if (tries == 1) {
1283                 /* search [0, last_zmbuf_idx) */
1284                 i = 0;
1285                 last = vq->last_zmbuf_idx;
1286                 goto again;
1287         }
1288
1289         return NULL;
1290 }
1291
1292 static __rte_noinline uint16_t
1293 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1294         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1295 {
1296         uint16_t i;
1297         uint16_t free_entries;
1298
1299         if (unlikely(dev->dequeue_zero_copy)) {
1300                 struct zcopy_mbuf *zmbuf, *next;
1301
1302                 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1303                      zmbuf != NULL; zmbuf = next) {
1304                         next = TAILQ_NEXT(zmbuf, next);
1305
1306                         if (mbuf_is_consumed(zmbuf->mbuf)) {
1307                                 update_shadow_used_ring_split(vq,
1308                                                 zmbuf->desc_idx, 0);
1309                                 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1310                                 restore_mbuf(zmbuf->mbuf);
1311                                 rte_pktmbuf_free(zmbuf->mbuf);
1312                                 put_zmbuf(zmbuf);
1313                                 vq->nr_zmbuf -= 1;
1314                         }
1315                 }
1316
1317                 if (likely(vq->shadow_used_idx)) {
1318                         flush_shadow_used_ring_split(dev, vq);
1319                         vhost_vring_call_split(dev, vq);
1320                 }
1321         }
1322
1323         free_entries = *((volatile uint16_t *)&vq->avail->idx) -
1324                         vq->last_avail_idx;
1325         if (free_entries == 0)
1326                 return 0;
1327
1328         /*
1329          * The ordering between avail index and
1330          * desc reads needs to be enforced.
1331          */
1332         rte_smp_rmb();
1333
1334         rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1335
1336         VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1337
1338         count = RTE_MIN(count, MAX_PKT_BURST);
1339         count = RTE_MIN(count, free_entries);
1340         VHOST_LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1341                         dev->vid, count);
1342
1343         for (i = 0; i < count; i++) {
1344                 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1345                 uint16_t head_idx;
1346                 uint32_t dummy_len;
1347                 uint16_t nr_vec = 0;
1348                 int err;
1349
1350                 if (unlikely(fill_vec_buf_split(dev, vq,
1351                                                 vq->last_avail_idx + i,
1352                                                 &nr_vec, buf_vec,
1353                                                 &head_idx, &dummy_len,
1354                                                 VHOST_ACCESS_RO) < 0))
1355                         break;
1356
1357                 if (likely(dev->dequeue_zero_copy == 0))
1358                         update_shadow_used_ring_split(vq, head_idx, 0);
1359
1360                 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
1361                 if (unlikely(pkts[i] == NULL)) {
1362                         RTE_LOG(ERR, VHOST_DATA,
1363                                 "Failed to allocate memory for mbuf.\n");
1364                         break;
1365                 }
1366
1367                 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
1368                                 mbuf_pool);
1369                 if (unlikely(err)) {
1370                         rte_pktmbuf_free(pkts[i]);
1371                         break;
1372                 }
1373
1374                 if (unlikely(dev->dequeue_zero_copy)) {
1375                         struct zcopy_mbuf *zmbuf;
1376
1377                         zmbuf = get_zmbuf(vq);
1378                         if (!zmbuf) {
1379                                 rte_pktmbuf_free(pkts[i]);
1380                                 break;
1381                         }
1382                         zmbuf->mbuf = pkts[i];
1383                         zmbuf->desc_idx = head_idx;
1384
1385                         /*
1386                          * Pin lock the mbuf; we will check later to see
1387                          * whether the mbuf is freed (when we are the last
1388                          * user) or not. If that's the case, we then could
1389                          * update the used ring safely.
1390                          */
1391                         rte_mbuf_refcnt_update(pkts[i], 1);
1392
1393                         vq->nr_zmbuf += 1;
1394                         TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1395                 }
1396         }
1397         vq->last_avail_idx += i;
1398
1399         if (likely(dev->dequeue_zero_copy == 0)) {
1400                 do_data_copy_dequeue(vq);
1401                 if (unlikely(i < count))
1402                         vq->shadow_used_idx = i;
1403                 if (likely(vq->shadow_used_idx)) {
1404                         flush_shadow_used_ring_split(dev, vq);
1405                         vhost_vring_call_split(dev, vq);
1406                 }
1407         }
1408
1409         return i;
1410 }
1411
1412 static __rte_noinline uint16_t
1413 virtio_dev_tx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
1414         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1415 {
1416         uint16_t i;
1417
1418         if (unlikely(dev->dequeue_zero_copy)) {
1419                 struct zcopy_mbuf *zmbuf, *next;
1420
1421                 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1422                      zmbuf != NULL; zmbuf = next) {
1423                         next = TAILQ_NEXT(zmbuf, next);
1424
1425                         if (mbuf_is_consumed(zmbuf->mbuf)) {
1426                                 update_shadow_used_ring_packed(vq,
1427                                                 zmbuf->desc_idx,
1428                                                 0,
1429                                                 zmbuf->desc_count);
1430
1431                                 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1432                                 restore_mbuf(zmbuf->mbuf);
1433                                 rte_pktmbuf_free(zmbuf->mbuf);
1434                                 put_zmbuf(zmbuf);
1435                                 vq->nr_zmbuf -= 1;
1436                         }
1437                 }
1438
1439                 if (likely(vq->shadow_used_idx)) {
1440                         flush_shadow_used_ring_packed(dev, vq);
1441                         vhost_vring_call_packed(dev, vq);
1442                 }
1443         }
1444
1445         VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1446
1447         count = RTE_MIN(count, MAX_PKT_BURST);
1448         VHOST_LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1449                         dev->vid, count);
1450
1451         for (i = 0; i < count; i++) {
1452                 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1453                 uint16_t buf_id;
1454                 uint32_t dummy_len;
1455                 uint16_t desc_count, nr_vec = 0;
1456                 int err;
1457
1458                 if (unlikely(fill_vec_buf_packed(dev, vq,
1459                                                 vq->last_avail_idx, &desc_count,
1460                                                 buf_vec, &nr_vec,
1461                                                 &buf_id, &dummy_len,
1462                                                 VHOST_ACCESS_RO) < 0))
1463                         break;
1464
1465                 if (likely(dev->dequeue_zero_copy == 0))
1466                         update_shadow_used_ring_packed(vq, buf_id, 0,
1467                                         desc_count);
1468
1469                 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
1470                 if (unlikely(pkts[i] == NULL)) {
1471                         RTE_LOG(ERR, VHOST_DATA,
1472                                 "Failed to allocate memory for mbuf.\n");
1473                         break;
1474                 }
1475
1476                 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
1477                                 mbuf_pool);
1478                 if (unlikely(err)) {
1479                         rte_pktmbuf_free(pkts[i]);
1480                         break;
1481                 }
1482
1483                 if (unlikely(dev->dequeue_zero_copy)) {
1484                         struct zcopy_mbuf *zmbuf;
1485
1486                         zmbuf = get_zmbuf(vq);
1487                         if (!zmbuf) {
1488                                 rte_pktmbuf_free(pkts[i]);
1489                                 break;
1490                         }
1491                         zmbuf->mbuf = pkts[i];
1492                         zmbuf->desc_idx = buf_id;
1493                         zmbuf->desc_count = desc_count;
1494
1495                         /*
1496                          * Pin lock the mbuf; we will check later to see
1497                          * whether the mbuf is freed (when we are the last
1498                          * user) or not. If that's the case, we then could
1499                          * update the used ring safely.
1500                          */
1501                         rte_mbuf_refcnt_update(pkts[i], 1);
1502
1503                         vq->nr_zmbuf += 1;
1504                         TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1505                 }
1506
1507                 vq->last_avail_idx += desc_count;
1508                 if (vq->last_avail_idx >= vq->size) {
1509                         vq->last_avail_idx -= vq->size;
1510                         vq->avail_wrap_counter ^= 1;
1511                 }
1512         }
1513
1514         if (likely(dev->dequeue_zero_copy == 0)) {
1515                 do_data_copy_dequeue(vq);
1516                 if (unlikely(i < count))
1517                         vq->shadow_used_idx = i;
1518                 if (likely(vq->shadow_used_idx)) {
1519                         flush_shadow_used_ring_packed(dev, vq);
1520                         vhost_vring_call_packed(dev, vq);
1521                 }
1522         }
1523
1524         return i;
1525 }
1526
1527 uint16_t
1528 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
1529         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1530 {
1531         struct virtio_net *dev;
1532         struct rte_mbuf *rarp_mbuf = NULL;
1533         struct vhost_virtqueue *vq;
1534
1535         dev = get_device(vid);
1536         if (!dev)
1537                 return 0;
1538
1539         if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1540                 RTE_LOG(ERR, VHOST_DATA,
1541                         "(%d) %s: built-in vhost net backend is disabled.\n",
1542                         dev->vid, __func__);
1543                 return 0;
1544         }
1545
1546         if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
1547                 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
1548                         dev->vid, __func__, queue_id);
1549                 return 0;
1550         }
1551
1552         vq = dev->virtqueue[queue_id];
1553
1554         if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
1555                 return 0;
1556
1557         if (unlikely(vq->enabled == 0)) {
1558                 count = 0;
1559                 goto out_access_unlock;
1560         }
1561
1562         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1563                 vhost_user_iotlb_rd_lock(vq);
1564
1565         if (unlikely(vq->access_ok == 0))
1566                 if (unlikely(vring_translate(dev, vq) < 0)) {
1567                         count = 0;
1568                         goto out;
1569                 }
1570
1571         /*
1572          * Construct a RARP broadcast packet, and inject it to the "pkts"
1573          * array, to looks like that guest actually send such packet.
1574          *
1575          * Check user_send_rarp() for more information.
1576          *
1577          * broadcast_rarp shares a cacheline in the virtio_net structure
1578          * with some fields that are accessed during enqueue and
1579          * rte_atomic16_cmpset() causes a write if using cmpxchg. This could
1580          * result in false sharing between enqueue and dequeue.
1581          *
1582          * Prevent unnecessary false sharing by reading broadcast_rarp first
1583          * and only performing cmpset if the read indicates it is likely to
1584          * be set.
1585          */
1586         if (unlikely(rte_atomic16_read(&dev->broadcast_rarp) &&
1587                         rte_atomic16_cmpset((volatile uint16_t *)
1588                                 &dev->broadcast_rarp.cnt, 1, 0))) {
1589
1590                 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
1591                 if (rarp_mbuf == NULL) {
1592                         RTE_LOG(ERR, VHOST_DATA,
1593                                 "Failed to make RARP packet.\n");
1594                         count = 0;
1595                         goto out;
1596                 }
1597                 count -= 1;
1598         }
1599
1600         if (vq_is_packed(dev))
1601                 count = virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count);
1602         else
1603                 count = virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count);
1604
1605 out:
1606         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1607                 vhost_user_iotlb_rd_unlock(vq);
1608
1609 out_access_unlock:
1610         rte_spinlock_unlock(&vq->access_lock);
1611
1612         if (unlikely(rarp_mbuf != NULL)) {
1613                 /*
1614                  * Inject it to the head of "pkts" array, so that switch's mac
1615                  * learning table will get updated first.
1616                  */
1617                 memmove(&pkts[1], pkts, count * sizeof(struct rte_mbuf *));
1618                 pkts[0] = rarp_mbuf;
1619                 count += 1;
1620         }
1621
1622         return count;
1623 }