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