vhost: extract split ring handling from Rx and Tx functions
[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(struct virtio_net *dev, struct vhost_virtqueue *vq,
81                           uint16_t to, uint16_t from, uint16_t size)
82 {
83         rte_memcpy(&vq->used->ring[to],
84                         &vq->shadow_used_ring[from],
85                         size * sizeof(struct vring_used_elem));
86         vhost_log_cache_used_vring(dev, vq,
87                         offsetof(struct vring_used, ring[to]),
88                         size * sizeof(struct vring_used_elem));
89 }
90
91 static __rte_always_inline void
92 flush_shadow_used_ring(struct virtio_net *dev, struct vhost_virtqueue *vq)
93 {
94         uint16_t used_idx = vq->last_used_idx & (vq->size - 1);
95
96         if (used_idx + vq->shadow_used_idx <= vq->size) {
97                 do_flush_shadow_used_ring(dev, vq, used_idx, 0,
98                                           vq->shadow_used_idx);
99         } else {
100                 uint16_t size;
101
102                 /* update used ring interval [used_idx, vq->size] */
103                 size = vq->size - used_idx;
104                 do_flush_shadow_used_ring(dev, vq, used_idx, 0, size);
105
106                 /* update the left half used ring interval [0, left_size] */
107                 do_flush_shadow_used_ring(dev, vq, 0, size,
108                                           vq->shadow_used_idx - size);
109         }
110         vq->last_used_idx += vq->shadow_used_idx;
111
112         rte_smp_wmb();
113
114         vhost_log_cache_sync(dev, vq);
115
116         *(volatile uint16_t *)&vq->used->idx += vq->shadow_used_idx;
117         vq->shadow_used_idx = 0;
118         vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
119                 sizeof(vq->used->idx));
120 }
121
122 static __rte_always_inline void
123 update_shadow_used_ring(struct vhost_virtqueue *vq,
124                          uint16_t desc_idx, uint16_t len)
125 {
126         uint16_t i = vq->shadow_used_idx++;
127
128         vq->shadow_used_ring[i].id  = desc_idx;
129         vq->shadow_used_ring[i].len = len;
130 }
131
132 static inline void
133 do_data_copy_enqueue(struct virtio_net *dev, struct vhost_virtqueue *vq)
134 {
135         struct batch_copy_elem *elem = vq->batch_copy_elems;
136         uint16_t count = vq->batch_copy_nb_elems;
137         int i;
138
139         for (i = 0; i < count; i++) {
140                 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
141                 vhost_log_cache_write(dev, vq, elem[i].log_addr, elem[i].len);
142                 PRINT_PACKET(dev, (uintptr_t)elem[i].dst, elem[i].len, 0);
143         }
144
145         vq->batch_copy_nb_elems = 0;
146 }
147
148 static inline void
149 do_data_copy_dequeue(struct vhost_virtqueue *vq)
150 {
151         struct batch_copy_elem *elem = vq->batch_copy_elems;
152         uint16_t count = vq->batch_copy_nb_elems;
153         int i;
154
155         for (i = 0; i < count; i++)
156                 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
157
158         vq->batch_copy_nb_elems = 0;
159 }
160
161 /* avoid write operation when necessary, to lessen cache issues */
162 #define ASSIGN_UNLESS_EQUAL(var, val) do {      \
163         if ((var) != (val))                     \
164                 (var) = (val);                  \
165 } while (0)
166
167 static __rte_always_inline void
168 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
169 {
170         uint64_t csum_l4 = m_buf->ol_flags & PKT_TX_L4_MASK;
171
172         if (m_buf->ol_flags & PKT_TX_TCP_SEG)
173                 csum_l4 |= PKT_TX_TCP_CKSUM;
174
175         if (csum_l4) {
176                 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
177                 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
178
179                 switch (csum_l4) {
180                 case PKT_TX_TCP_CKSUM:
181                         net_hdr->csum_offset = (offsetof(struct tcp_hdr,
182                                                 cksum));
183                         break;
184                 case PKT_TX_UDP_CKSUM:
185                         net_hdr->csum_offset = (offsetof(struct udp_hdr,
186                                                 dgram_cksum));
187                         break;
188                 case PKT_TX_SCTP_CKSUM:
189                         net_hdr->csum_offset = (offsetof(struct sctp_hdr,
190                                                 cksum));
191                         break;
192                 }
193         } else {
194                 ASSIGN_UNLESS_EQUAL(net_hdr->csum_start, 0);
195                 ASSIGN_UNLESS_EQUAL(net_hdr->csum_offset, 0);
196                 ASSIGN_UNLESS_EQUAL(net_hdr->flags, 0);
197         }
198
199         /* IP cksum verification cannot be bypassed, then calculate here */
200         if (m_buf->ol_flags & PKT_TX_IP_CKSUM) {
201                 struct ipv4_hdr *ipv4_hdr;
202
203                 ipv4_hdr = rte_pktmbuf_mtod_offset(m_buf, struct ipv4_hdr *,
204                                                    m_buf->l2_len);
205                 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
206         }
207
208         if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
209                 if (m_buf->ol_flags & PKT_TX_IPV4)
210                         net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
211                 else
212                         net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
213                 net_hdr->gso_size = m_buf->tso_segsz;
214                 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
215                                         + m_buf->l4_len;
216         } else if (m_buf->ol_flags & PKT_TX_UDP_SEG) {
217                 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
218                 net_hdr->gso_size = m_buf->tso_segsz;
219                 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len +
220                         m_buf->l4_len;
221         } else {
222                 ASSIGN_UNLESS_EQUAL(net_hdr->gso_type, 0);
223                 ASSIGN_UNLESS_EQUAL(net_hdr->gso_size, 0);
224                 ASSIGN_UNLESS_EQUAL(net_hdr->hdr_len, 0);
225         }
226 }
227
228 static __rte_always_inline int
229 fill_vec_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
230                          uint32_t avail_idx, uint16_t *vec_idx,
231                          struct buf_vector *buf_vec, uint16_t *desc_chain_head,
232                          uint16_t *desc_chain_len, uint8_t perm)
233 {
234         uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
235         uint16_t vec_id = *vec_idx;
236         uint32_t len    = 0;
237         uint64_t dlen, desc_avail, desc_iova;
238         struct vring_desc *descs = vq->desc;
239         struct vring_desc *idesc = NULL;
240
241         *desc_chain_head = idx;
242
243         if (vq->desc[idx].flags & VRING_DESC_F_INDIRECT) {
244                 dlen = vq->desc[idx].len;
245                 descs = (struct vring_desc *)(uintptr_t)
246                         vhost_iova_to_vva(dev, vq, vq->desc[idx].addr,
247                                                 &dlen,
248                                                 VHOST_ACCESS_RO);
249                 if (unlikely(!descs))
250                         return -1;
251
252                 if (unlikely(dlen < vq->desc[idx].len)) {
253                         /*
254                          * The indirect desc table is not contiguous
255                          * in process VA space, we have to copy it.
256                          */
257                         idesc = alloc_copy_ind_table(dev, vq,
258                                         vq->desc[idx].addr, vq->desc[idx].len);
259                         if (unlikely(!idesc))
260                                 return -1;
261
262                         descs = idesc;
263                 }
264
265                 idx = 0;
266         }
267
268         while (1) {
269                 if (unlikely(idx >= vq->size)) {
270                         free_ind_table(idesc);
271                         return -1;
272                 }
273
274
275                 len += descs[idx].len;
276                 desc_avail = descs[idx].len;
277                 desc_iova = descs[idx].addr;
278
279                 while (desc_avail) {
280                         uint64_t desc_addr;
281                         uint64_t desc_chunck_len = desc_avail;
282
283                         if (unlikely(vec_id >= BUF_VECTOR_MAX)) {
284                                 free_ind_table(idesc);
285                                 return -1;
286                         }
287
288                         desc_addr = vhost_iova_to_vva(dev, vq,
289                                         desc_iova,
290                                         &desc_chunck_len,
291                                         perm);
292                         if (unlikely(!desc_addr)) {
293                                 free_ind_table(idesc);
294                                 return -1;
295                         }
296
297                         buf_vec[vec_id].buf_iova = desc_iova;
298                         buf_vec[vec_id].buf_addr = desc_addr;
299                         buf_vec[vec_id].buf_len  = desc_chunck_len;
300                         buf_vec[vec_id].desc_idx = idx;
301
302                         desc_avail -= desc_chunck_len;
303                         desc_iova += desc_chunck_len;
304                         vec_id++;
305                 }
306
307                 if ((descs[idx].flags & VRING_DESC_F_NEXT) == 0)
308                         break;
309
310                 idx = descs[idx].next;
311         }
312
313         *desc_chain_len = len;
314         *vec_idx = vec_id;
315
316         if (unlikely(!!idesc))
317                 free_ind_table(idesc);
318
319         return 0;
320 }
321
322 /*
323  * Returns -1 on fail, 0 on success
324  */
325 static inline int
326 reserve_avail_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
327                                 uint32_t size, struct buf_vector *buf_vec,
328                                 uint16_t *num_buffers, uint16_t avail_head,
329                                 uint16_t *nr_vec)
330 {
331         uint16_t cur_idx;
332         uint16_t vec_idx = 0;
333         uint16_t max_tries, tries = 0;
334
335         uint16_t head_idx = 0;
336         uint16_t len = 0;
337
338         *num_buffers = 0;
339         cur_idx  = vq->last_avail_idx;
340
341         if (rxvq_is_mergeable(dev))
342                 max_tries = vq->size;
343         else
344                 max_tries = 1;
345
346         while (size > 0) {
347                 if (unlikely(cur_idx == avail_head))
348                         return -1;
349
350                 if (unlikely(fill_vec_buf_split(dev, vq, cur_idx,
351                                                 &vec_idx, buf_vec,
352                                                 &head_idx, &len,
353                                                 VHOST_ACCESS_RW) < 0))
354                         return -1;
355                 len = RTE_MIN(len, size);
356                 update_shadow_used_ring(vq, head_idx, len);
357                 size -= len;
358
359                 cur_idx++;
360                 tries++;
361                 *num_buffers += 1;
362
363                 /*
364                  * if we tried all available ring items, and still
365                  * can't get enough buf, it means something abnormal
366                  * happened.
367                  */
368                 if (unlikely(tries > max_tries))
369                         return -1;
370         }
371
372         *nr_vec = vec_idx;
373
374         return 0;
375 }
376
377 static __rte_always_inline int
378 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
379                             struct rte_mbuf *m, struct buf_vector *buf_vec,
380                             uint16_t nr_vec, uint16_t num_buffers)
381 {
382         uint32_t vec_idx = 0;
383         uint32_t mbuf_offset, mbuf_avail;
384         uint32_t buf_offset, buf_avail;
385         uint64_t buf_addr, buf_iova, buf_len;
386         uint32_t cpy_len;
387         uint64_t hdr_addr;
388         struct rte_mbuf *hdr_mbuf;
389         struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
390         struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
391         int error = 0;
392
393         if (unlikely(m == NULL)) {
394                 error = -1;
395                 goto out;
396         }
397
398         buf_addr = buf_vec[vec_idx].buf_addr;
399         buf_iova = buf_vec[vec_idx].buf_iova;
400         buf_len = buf_vec[vec_idx].buf_len;
401
402         if (nr_vec > 1)
403                 rte_prefetch0((void *)(uintptr_t)buf_vec[1].buf_addr);
404
405         if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
406                 error = -1;
407                 goto out;
408         }
409
410         hdr_mbuf = m;
411         hdr_addr = buf_addr;
412         if (unlikely(buf_len < dev->vhost_hlen))
413                 hdr = &tmp_hdr;
414         else
415                 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
416
417         VHOST_LOG_DEBUG(VHOST_DATA, "(%d) RX: num merge buffers %d\n",
418                 dev->vid, num_buffers);
419
420         if (unlikely(buf_len < dev->vhost_hlen)) {
421                 buf_offset = dev->vhost_hlen - buf_len;
422                 vec_idx++;
423                 buf_addr = buf_vec[vec_idx].buf_addr;
424                 buf_iova = buf_vec[vec_idx].buf_iova;
425                 buf_len = buf_vec[vec_idx].buf_len;
426                 buf_avail = buf_len - buf_offset;
427         } else {
428                 buf_offset = dev->vhost_hlen;
429                 buf_avail = buf_len - dev->vhost_hlen;
430         }
431
432         mbuf_avail  = rte_pktmbuf_data_len(m);
433         mbuf_offset = 0;
434         while (mbuf_avail != 0 || m->next != NULL) {
435                 /* done with current buf, get the next one */
436                 if (buf_avail == 0) {
437                         vec_idx++;
438                         if (unlikely(vec_idx >= nr_vec)) {
439                                 error = -1;
440                                 goto out;
441                         }
442
443                         buf_addr = buf_vec[vec_idx].buf_addr;
444                         buf_iova = buf_vec[vec_idx].buf_iova;
445                         buf_len = buf_vec[vec_idx].buf_len;
446
447                         /* Prefetch next buffer address. */
448                         if (vec_idx + 1 < nr_vec)
449                                 rte_prefetch0((void *)(uintptr_t)
450                                                 buf_vec[vec_idx + 1].buf_addr);
451                         buf_offset = 0;
452                         buf_avail  = buf_len;
453                 }
454
455                 /* done with current mbuf, get the next one */
456                 if (mbuf_avail == 0) {
457                         m = m->next;
458
459                         mbuf_offset = 0;
460                         mbuf_avail  = rte_pktmbuf_data_len(m);
461                 }
462
463                 if (hdr_addr) {
464                         virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
465                         if (rxvq_is_mergeable(dev))
466                                 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
467                                                 num_buffers);
468
469                         if (unlikely(hdr == &tmp_hdr)) {
470                                 uint64_t len;
471                                 uint64_t remain = dev->vhost_hlen;
472                                 uint64_t src = (uint64_t)(uintptr_t)hdr, dst;
473                                 uint64_t iova = buf_vec[0].buf_iova;
474                                 uint16_t hdr_vec_idx = 0;
475
476                                 while (remain) {
477                                         len = remain;
478                                         dst = buf_vec[hdr_vec_idx].buf_addr;
479                                         rte_memcpy((void *)(uintptr_t)dst,
480                                                         (void *)(uintptr_t)src,
481                                                         len);
482
483                                         PRINT_PACKET(dev, (uintptr_t)dst,
484                                                         (uint32_t)len, 0);
485                                         vhost_log_cache_write(dev, vq,
486                                                         iova, len);
487
488                                         remain -= len;
489                                         iova += len;
490                                         src += len;
491                                         hdr_vec_idx++;
492                                 }
493                         } else {
494                                 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
495                                                 dev->vhost_hlen, 0);
496                                 vhost_log_cache_write(dev, vq,
497                                                 buf_vec[0].buf_iova,
498                                                 dev->vhost_hlen);
499                         }
500
501                         hdr_addr = 0;
502                 }
503
504                 cpy_len = RTE_MIN(buf_len, mbuf_avail);
505
506                 if (likely(cpy_len > MAX_BATCH_LEN ||
507                                         vq->batch_copy_nb_elems >= vq->size)) {
508                         rte_memcpy((void *)((uintptr_t)(buf_addr + buf_offset)),
509                                 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
510                                 cpy_len);
511                         vhost_log_cache_write(dev, vq, buf_iova + buf_offset,
512                                         cpy_len);
513                         PRINT_PACKET(dev, (uintptr_t)(buf_addr + buf_offset),
514                                 cpy_len, 0);
515                 } else {
516                         batch_copy[vq->batch_copy_nb_elems].dst =
517                                 (void *)((uintptr_t)(buf_addr + buf_offset));
518                         batch_copy[vq->batch_copy_nb_elems].src =
519                                 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
520                         batch_copy[vq->batch_copy_nb_elems].log_addr =
521                                 buf_iova + buf_offset;
522                         batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
523                         vq->batch_copy_nb_elems++;
524                 }
525
526                 mbuf_avail  -= cpy_len;
527                 mbuf_offset += cpy_len;
528                 buf_avail  -= cpy_len;
529                 buf_offset += cpy_len;
530         }
531
532 out:
533
534         return error;
535 }
536
537 static __rte_always_inline uint32_t
538 virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
539         struct rte_mbuf **pkts, uint32_t count)
540 {
541         uint32_t pkt_idx = 0;
542         uint16_t num_buffers;
543         struct buf_vector buf_vec[BUF_VECTOR_MAX];
544         uint16_t avail_head;
545
546         rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
547         avail_head = *((volatile uint16_t *)&vq->avail->idx);
548
549         for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
550                 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
551                 uint16_t nr_vec = 0;
552
553                 if (unlikely(reserve_avail_buf_split(dev, vq,
554                                                 pkt_len, buf_vec, &num_buffers,
555                                                 avail_head, &nr_vec) < 0)) {
556                         VHOST_LOG_DEBUG(VHOST_DATA,
557                                 "(%d) failed to get enough desc from vring\n",
558                                 dev->vid);
559                         vq->shadow_used_idx -= num_buffers;
560                         break;
561                 }
562
563                 rte_prefetch0((void *)(uintptr_t)buf_vec[0].buf_addr);
564
565                 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
566                         dev->vid, vq->last_avail_idx,
567                         vq->last_avail_idx + num_buffers);
568
569                 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
570                                                 buf_vec, nr_vec,
571                                                 num_buffers) < 0) {
572                         vq->shadow_used_idx -= num_buffers;
573                         break;
574                 }
575
576                 vq->last_avail_idx += num_buffers;
577         }
578
579         do_data_copy_enqueue(dev, vq);
580
581         if (likely(vq->shadow_used_idx)) {
582                 flush_shadow_used_ring(dev, vq);
583                 vhost_vring_call(dev, vq);
584         }
585
586         return pkt_idx;
587 }
588
589 static __rte_always_inline uint32_t
590 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
591         struct rte_mbuf **pkts, uint32_t count)
592 {
593         struct vhost_virtqueue *vq;
594
595         VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
596         if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
597                 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
598                         dev->vid, __func__, queue_id);
599                 return 0;
600         }
601
602         vq = dev->virtqueue[queue_id];
603
604         rte_spinlock_lock(&vq->access_lock);
605
606         if (unlikely(vq->enabled == 0))
607                 goto out_access_unlock;
608
609         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
610                 vhost_user_iotlb_rd_lock(vq);
611
612         if (unlikely(vq->access_ok == 0))
613                 if (unlikely(vring_translate(dev, vq) < 0))
614                         goto out;
615
616         count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
617         if (count == 0)
618                 goto out;
619
620         count = virtio_dev_rx_split(dev, vq, pkts, count);
621
622 out:
623         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
624                 vhost_user_iotlb_rd_unlock(vq);
625
626 out_access_unlock:
627         rte_spinlock_unlock(&vq->access_lock);
628
629         return count;
630 }
631
632 uint16_t
633 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
634         struct rte_mbuf **pkts, uint16_t count)
635 {
636         struct virtio_net *dev = get_device(vid);
637
638         if (!dev)
639                 return 0;
640
641         if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
642                 RTE_LOG(ERR, VHOST_DATA,
643                         "(%d) %s: built-in vhost net backend is disabled.\n",
644                         dev->vid, __func__);
645                 return 0;
646         }
647
648         return virtio_dev_rx(dev, queue_id, pkts, count);
649 }
650
651 static inline bool
652 virtio_net_with_host_offload(struct virtio_net *dev)
653 {
654         if (dev->features &
655                         ((1ULL << VIRTIO_NET_F_CSUM) |
656                          (1ULL << VIRTIO_NET_F_HOST_ECN) |
657                          (1ULL << VIRTIO_NET_F_HOST_TSO4) |
658                          (1ULL << VIRTIO_NET_F_HOST_TSO6) |
659                          (1ULL << VIRTIO_NET_F_HOST_UFO)))
660                 return true;
661
662         return false;
663 }
664
665 static void
666 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
667 {
668         struct ipv4_hdr *ipv4_hdr;
669         struct ipv6_hdr *ipv6_hdr;
670         void *l3_hdr = NULL;
671         struct ether_hdr *eth_hdr;
672         uint16_t ethertype;
673
674         eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
675
676         m->l2_len = sizeof(struct ether_hdr);
677         ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
678
679         if (ethertype == ETHER_TYPE_VLAN) {
680                 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
681
682                 m->l2_len += sizeof(struct vlan_hdr);
683                 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
684         }
685
686         l3_hdr = (char *)eth_hdr + m->l2_len;
687
688         switch (ethertype) {
689         case ETHER_TYPE_IPv4:
690                 ipv4_hdr = l3_hdr;
691                 *l4_proto = ipv4_hdr->next_proto_id;
692                 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
693                 *l4_hdr = (char *)l3_hdr + m->l3_len;
694                 m->ol_flags |= PKT_TX_IPV4;
695                 break;
696         case ETHER_TYPE_IPv6:
697                 ipv6_hdr = l3_hdr;
698                 *l4_proto = ipv6_hdr->proto;
699                 m->l3_len = sizeof(struct ipv6_hdr);
700                 *l4_hdr = (char *)l3_hdr + m->l3_len;
701                 m->ol_flags |= PKT_TX_IPV6;
702                 break;
703         default:
704                 m->l3_len = 0;
705                 *l4_proto = 0;
706                 *l4_hdr = NULL;
707                 break;
708         }
709 }
710
711 static __rte_always_inline void
712 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
713 {
714         uint16_t l4_proto = 0;
715         void *l4_hdr = NULL;
716         struct tcp_hdr *tcp_hdr = NULL;
717
718         if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
719                 return;
720
721         parse_ethernet(m, &l4_proto, &l4_hdr);
722         if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
723                 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
724                         switch (hdr->csum_offset) {
725                         case (offsetof(struct tcp_hdr, cksum)):
726                                 if (l4_proto == IPPROTO_TCP)
727                                         m->ol_flags |= PKT_TX_TCP_CKSUM;
728                                 break;
729                         case (offsetof(struct udp_hdr, dgram_cksum)):
730                                 if (l4_proto == IPPROTO_UDP)
731                                         m->ol_flags |= PKT_TX_UDP_CKSUM;
732                                 break;
733                         case (offsetof(struct sctp_hdr, cksum)):
734                                 if (l4_proto == IPPROTO_SCTP)
735                                         m->ol_flags |= PKT_TX_SCTP_CKSUM;
736                                 break;
737                         default:
738                                 break;
739                         }
740                 }
741         }
742
743         if (l4_hdr && hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
744                 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
745                 case VIRTIO_NET_HDR_GSO_TCPV4:
746                 case VIRTIO_NET_HDR_GSO_TCPV6:
747                         tcp_hdr = l4_hdr;
748                         m->ol_flags |= PKT_TX_TCP_SEG;
749                         m->tso_segsz = hdr->gso_size;
750                         m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
751                         break;
752                 case VIRTIO_NET_HDR_GSO_UDP:
753                         m->ol_flags |= PKT_TX_UDP_SEG;
754                         m->tso_segsz = hdr->gso_size;
755                         m->l4_len = sizeof(struct udp_hdr);
756                         break;
757                 default:
758                         RTE_LOG(WARNING, VHOST_DATA,
759                                 "unsupported gso type %u.\n", hdr->gso_type);
760                         break;
761                 }
762         }
763 }
764
765 static __rte_always_inline void
766 put_zmbuf(struct zcopy_mbuf *zmbuf)
767 {
768         zmbuf->in_use = 0;
769 }
770
771 static __rte_always_inline int
772 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
773                   struct buf_vector *buf_vec, uint16_t nr_vec,
774                   struct rte_mbuf *m, struct rte_mempool *mbuf_pool)
775 {
776         uint32_t buf_avail, buf_offset;
777         uint64_t buf_addr, buf_iova, buf_len;
778         uint32_t mbuf_avail, mbuf_offset;
779         uint32_t cpy_len;
780         struct rte_mbuf *cur = m, *prev = m;
781         struct virtio_net_hdr tmp_hdr;
782         struct virtio_net_hdr *hdr = NULL;
783         /* A counter to avoid desc dead loop chain */
784         uint16_t vec_idx = 0;
785         struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
786         int error = 0;
787
788         buf_addr = buf_vec[vec_idx].buf_addr;
789         buf_iova = buf_vec[vec_idx].buf_iova;
790         buf_len = buf_vec[vec_idx].buf_len;
791
792         if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
793                 error = -1;
794                 goto out;
795         }
796
797         if (likely(nr_vec > 1))
798                 rte_prefetch0((void *)(uintptr_t)buf_vec[1].buf_addr);
799
800         if (virtio_net_with_host_offload(dev)) {
801                 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
802                         uint64_t len;
803                         uint64_t remain = sizeof(struct virtio_net_hdr);
804                         uint64_t src;
805                         uint64_t dst = (uint64_t)(uintptr_t)&tmp_hdr;
806                         uint16_t hdr_vec_idx = 0;
807
808                         /*
809                          * No luck, the virtio-net header doesn't fit
810                          * in a contiguous virtual area.
811                          */
812                         while (remain) {
813                                 len = remain;
814                                 src = buf_vec[hdr_vec_idx].buf_addr;
815                                 rte_memcpy((void *)(uintptr_t)dst,
816                                                    (void *)(uintptr_t)src, len);
817
818                                 remain -= len;
819                                 dst += len;
820                                 hdr_vec_idx++;
821                         }
822
823                         hdr = &tmp_hdr;
824                 } else {
825                         hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
826                         rte_prefetch0(hdr);
827                 }
828         }
829
830         /*
831          * A virtio driver normally uses at least 2 desc buffers
832          * for Tx: the first for storing the header, and others
833          * for storing the data.
834          */
835         if (unlikely(buf_len < dev->vhost_hlen)) {
836                 buf_offset = dev->vhost_hlen - buf_len;
837                 vec_idx++;
838                 buf_addr = buf_vec[vec_idx].buf_addr;
839                 buf_iova = buf_vec[vec_idx].buf_iova;
840                 buf_len = buf_vec[vec_idx].buf_len;
841                 buf_avail  = buf_len - buf_offset;
842         } else if (buf_len == dev->vhost_hlen) {
843                 if (unlikely(++vec_idx >= nr_vec))
844                         goto out;
845                 buf_addr = buf_vec[vec_idx].buf_addr;
846                 buf_iova = buf_vec[vec_idx].buf_iova;
847                 buf_len = buf_vec[vec_idx].buf_len;
848
849                 buf_offset = 0;
850                 buf_avail = buf_len;
851         } else {
852                 buf_offset = dev->vhost_hlen;
853                 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
854         }
855
856         rte_prefetch0((void *)(uintptr_t)
857                         (buf_addr + buf_offset));
858
859         PRINT_PACKET(dev,
860                         (uintptr_t)(buf_addr + buf_offset),
861                         (uint32_t)buf_avail, 0);
862
863         mbuf_offset = 0;
864         mbuf_avail  = m->buf_len - RTE_PKTMBUF_HEADROOM;
865         while (1) {
866                 uint64_t hpa;
867
868                 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
869
870                 /*
871                  * A desc buf might across two host physical pages that are
872                  * not continuous. In such case (gpa_to_hpa returns 0), data
873                  * will be copied even though zero copy is enabled.
874                  */
875                 if (unlikely(dev->dequeue_zero_copy && (hpa = gpa_to_hpa(dev,
876                                         buf_iova + buf_offset, cpy_len)))) {
877                         cur->data_len = cpy_len;
878                         cur->data_off = 0;
879                         cur->buf_addr =
880                                 (void *)(uintptr_t)(buf_addr + buf_offset);
881                         cur->buf_iova = hpa;
882
883                         /*
884                          * In zero copy mode, one mbuf can only reference data
885                          * for one or partial of one desc buff.
886                          */
887                         mbuf_avail = cpy_len;
888                 } else {
889                         if (likely(cpy_len > MAX_BATCH_LEN ||
890                                    vq->batch_copy_nb_elems >= vq->size ||
891                                    (hdr && cur == m))) {
892                                 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
893                                                                    mbuf_offset),
894                                            (void *)((uintptr_t)(buf_addr +
895                                                            buf_offset)),
896                                            cpy_len);
897                         } else {
898                                 batch_copy[vq->batch_copy_nb_elems].dst =
899                                         rte_pktmbuf_mtod_offset(cur, void *,
900                                                                 mbuf_offset);
901                                 batch_copy[vq->batch_copy_nb_elems].src =
902                                         (void *)((uintptr_t)(buf_addr +
903                                                                 buf_offset));
904                                 batch_copy[vq->batch_copy_nb_elems].len =
905                                         cpy_len;
906                                 vq->batch_copy_nb_elems++;
907                         }
908                 }
909
910                 mbuf_avail  -= cpy_len;
911                 mbuf_offset += cpy_len;
912                 buf_avail -= cpy_len;
913                 buf_offset += cpy_len;
914
915                 /* This buf reaches to its end, get the next one */
916                 if (buf_avail == 0) {
917                         if (++vec_idx >= nr_vec)
918                                 break;
919
920                         buf_addr = buf_vec[vec_idx].buf_addr;
921                         buf_iova = buf_vec[vec_idx].buf_iova;
922                         buf_len = buf_vec[vec_idx].buf_len;
923
924                         /*
925                          * Prefecth desc n + 1 buffer while
926                          * desc n buffer is processed.
927                          */
928                         if (vec_idx + 1 < nr_vec)
929                                 rte_prefetch0((void *)(uintptr_t)
930                                                 buf_vec[vec_idx + 1].buf_addr);
931
932                         buf_offset = 0;
933                         buf_avail  = buf_len;
934
935                         PRINT_PACKET(dev, (uintptr_t)buf_addr,
936                                         (uint32_t)buf_avail, 0);
937                 }
938
939                 /*
940                  * This mbuf reaches to its end, get a new one
941                  * to hold more data.
942                  */
943                 if (mbuf_avail == 0) {
944                         cur = rte_pktmbuf_alloc(mbuf_pool);
945                         if (unlikely(cur == NULL)) {
946                                 RTE_LOG(ERR, VHOST_DATA, "Failed to "
947                                         "allocate memory for mbuf.\n");
948                                 error = -1;
949                                 goto out;
950                         }
951                         if (unlikely(dev->dequeue_zero_copy))
952                                 rte_mbuf_refcnt_update(cur, 1);
953
954                         prev->next = cur;
955                         prev->data_len = mbuf_offset;
956                         m->nb_segs += 1;
957                         m->pkt_len += mbuf_offset;
958                         prev = cur;
959
960                         mbuf_offset = 0;
961                         mbuf_avail  = cur->buf_len - RTE_PKTMBUF_HEADROOM;
962                 }
963         }
964
965         prev->data_len = mbuf_offset;
966         m->pkt_len    += mbuf_offset;
967
968         if (hdr)
969                 vhost_dequeue_offload(hdr, m);
970
971 out:
972
973         return error;
974 }
975
976 static __rte_always_inline struct zcopy_mbuf *
977 get_zmbuf(struct vhost_virtqueue *vq)
978 {
979         uint16_t i;
980         uint16_t last;
981         int tries = 0;
982
983         /* search [last_zmbuf_idx, zmbuf_size) */
984         i = vq->last_zmbuf_idx;
985         last = vq->zmbuf_size;
986
987 again:
988         for (; i < last; i++) {
989                 if (vq->zmbufs[i].in_use == 0) {
990                         vq->last_zmbuf_idx = i + 1;
991                         vq->zmbufs[i].in_use = 1;
992                         return &vq->zmbufs[i];
993                 }
994         }
995
996         tries++;
997         if (tries == 1) {
998                 /* search [0, last_zmbuf_idx) */
999                 i = 0;
1000                 last = vq->last_zmbuf_idx;
1001                 goto again;
1002         }
1003
1004         return NULL;
1005 }
1006
1007 static __rte_always_inline bool
1008 mbuf_is_consumed(struct rte_mbuf *m)
1009 {
1010         while (m) {
1011                 if (rte_mbuf_refcnt_read(m) > 1)
1012                         return false;
1013                 m = m->next;
1014         }
1015
1016         return true;
1017 }
1018
1019 static __rte_always_inline void
1020 restore_mbuf(struct rte_mbuf *m)
1021 {
1022         uint32_t mbuf_size, priv_size;
1023
1024         while (m) {
1025                 priv_size = rte_pktmbuf_priv_size(m->pool);
1026                 mbuf_size = sizeof(struct rte_mbuf) + priv_size;
1027                 /* start of buffer is after mbuf structure and priv data */
1028
1029                 m->buf_addr = (char *)m + mbuf_size;
1030                 m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size;
1031                 m = m->next;
1032         }
1033 }
1034
1035 static __rte_always_inline uint16_t
1036 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1037         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1038 {
1039         uint16_t i;
1040         uint16_t free_entries;
1041
1042         if (unlikely(dev->dequeue_zero_copy)) {
1043                 struct zcopy_mbuf *zmbuf, *next;
1044                 int nr_updated = 0;
1045
1046                 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1047                      zmbuf != NULL; zmbuf = next) {
1048                         next = TAILQ_NEXT(zmbuf, next);
1049
1050                         if (mbuf_is_consumed(zmbuf->mbuf)) {
1051                                 update_shadow_used_ring(vq, zmbuf->desc_idx, 0);
1052                                 nr_updated += 1;
1053
1054                                 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1055                                 restore_mbuf(zmbuf->mbuf);
1056                                 rte_pktmbuf_free(zmbuf->mbuf);
1057                                 put_zmbuf(zmbuf);
1058                                 vq->nr_zmbuf -= 1;
1059                         }
1060                 }
1061
1062                 flush_shadow_used_ring(dev, vq);
1063                 vhost_vring_call(dev, vq);
1064         }
1065
1066         rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1067
1068         free_entries = *((volatile uint16_t *)&vq->avail->idx) -
1069                         vq->last_avail_idx;
1070         if (free_entries == 0)
1071                 return 0;
1072
1073         VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1074
1075         count = RTE_MIN(count, MAX_PKT_BURST);
1076         count = RTE_MIN(count, free_entries);
1077         VHOST_LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1078                         dev->vid, count);
1079
1080         for (i = 0; i < count; i++) {
1081                 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1082                 uint16_t head_idx, dummy_len;
1083                 uint16_t nr_vec = 0;
1084                 int err;
1085
1086                 if (unlikely(fill_vec_buf_split(dev, vq,
1087                                                 vq->last_avail_idx + i,
1088                                                 &nr_vec, buf_vec,
1089                                                 &head_idx, &dummy_len,
1090                                                 VHOST_ACCESS_RO) < 0))
1091                         break;
1092
1093                 if (likely(dev->dequeue_zero_copy == 0))
1094                         update_shadow_used_ring(vq, head_idx, 0);
1095
1096                 rte_prefetch0((void *)(uintptr_t)buf_vec[0].buf_addr);
1097
1098                 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
1099                 if (unlikely(pkts[i] == NULL)) {
1100                         RTE_LOG(ERR, VHOST_DATA,
1101                                 "Failed to allocate memory for mbuf.\n");
1102                         break;
1103                 }
1104
1105                 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
1106                                 mbuf_pool);
1107                 if (unlikely(err)) {
1108                         rte_pktmbuf_free(pkts[i]);
1109                         break;
1110                 }
1111
1112                 if (unlikely(dev->dequeue_zero_copy)) {
1113                         struct zcopy_mbuf *zmbuf;
1114
1115                         zmbuf = get_zmbuf(vq);
1116                         if (!zmbuf) {
1117                                 rte_pktmbuf_free(pkts[i]);
1118                                 break;
1119                         }
1120                         zmbuf->mbuf = pkts[i];
1121                         zmbuf->desc_idx = head_idx;
1122
1123                         /*
1124                          * Pin lock the mbuf; we will check later to see
1125                          * whether the mbuf is freed (when we are the last
1126                          * user) or not. If that's the case, we then could
1127                          * update the used ring safely.
1128                          */
1129                         rte_mbuf_refcnt_update(pkts[i], 1);
1130
1131                         vq->nr_zmbuf += 1;
1132                         TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1133                 }
1134         }
1135         vq->last_avail_idx += i;
1136
1137         if (likely(dev->dequeue_zero_copy == 0)) {
1138                 do_data_copy_dequeue(vq);
1139                 if (unlikely(i < count))
1140                         vq->shadow_used_idx = i;
1141                 flush_shadow_used_ring(dev, vq);
1142                 vhost_vring_call(dev, vq);
1143         }
1144
1145         return i;
1146 }
1147
1148 uint16_t
1149 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
1150         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1151 {
1152         struct virtio_net *dev;
1153         struct rte_mbuf *rarp_mbuf = NULL;
1154         struct vhost_virtqueue *vq;
1155
1156         dev = get_device(vid);
1157         if (!dev)
1158                 return 0;
1159
1160         if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1161                 RTE_LOG(ERR, VHOST_DATA,
1162                         "(%d) %s: built-in vhost net backend is disabled.\n",
1163                         dev->vid, __func__);
1164                 return 0;
1165         }
1166
1167         if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
1168                 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
1169                         dev->vid, __func__, queue_id);
1170                 return 0;
1171         }
1172
1173         vq = dev->virtqueue[queue_id];
1174
1175         if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
1176                 return 0;
1177
1178         if (unlikely(vq->enabled == 0))
1179                 goto out_access_unlock;
1180
1181         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1182                 vhost_user_iotlb_rd_lock(vq);
1183
1184         if (unlikely(vq->access_ok == 0))
1185                 if (unlikely(vring_translate(dev, vq) < 0))
1186                         goto out;
1187
1188         /*
1189          * Construct a RARP broadcast packet, and inject it to the "pkts"
1190          * array, to looks like that guest actually send such packet.
1191          *
1192          * Check user_send_rarp() for more information.
1193          *
1194          * broadcast_rarp shares a cacheline in the virtio_net structure
1195          * with some fields that are accessed during enqueue and
1196          * rte_atomic16_cmpset() causes a write if using cmpxchg. This could
1197          * result in false sharing between enqueue and dequeue.
1198          *
1199          * Prevent unnecessary false sharing by reading broadcast_rarp first
1200          * and only performing cmpset if the read indicates it is likely to
1201          * be set.
1202          */
1203         if (unlikely(rte_atomic16_read(&dev->broadcast_rarp) &&
1204                         rte_atomic16_cmpset((volatile uint16_t *)
1205                                 &dev->broadcast_rarp.cnt, 1, 0))) {
1206
1207                 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
1208                 if (rarp_mbuf == NULL) {
1209                         RTE_LOG(ERR, VHOST_DATA,
1210                                 "Failed to make RARP packet.\n");
1211                         return 0;
1212                 }
1213                 count -= 1;
1214         }
1215
1216         count = virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count);
1217
1218 out:
1219         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1220                 vhost_user_iotlb_rd_unlock(vq);
1221
1222 out_access_unlock:
1223         rte_spinlock_unlock(&vq->access_lock);
1224
1225         if (unlikely(rarp_mbuf != NULL)) {
1226                 /*
1227                  * Inject it to the head of "pkts" array, so that switch's mac
1228                  * learning table will get updated first.
1229                  */
1230                 memmove(&pkts[1], pkts, count * sizeof(struct rte_mbuf *));
1231                 pkts[0] = rarp_mbuf;
1232                 count += 1;
1233         }
1234
1235         return count;
1236 }