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