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