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