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