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