vhost: make buffer vector for scatter Rx local
[dpdk.git] / lib / librte_vhost / vhost_rxtx.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdint.h>
35 #include <stdbool.h>
36 #include <linux/virtio_net.h>
37
38 #include <rte_mbuf.h>
39 #include <rte_memcpy.h>
40 #include <rte_ether.h>
41 #include <rte_ip.h>
42 #include <rte_virtio_net.h>
43 #include <rte_tcp.h>
44 #include <rte_udp.h>
45 #include <rte_sctp.h>
46 #include <rte_arp.h>
47
48 #include "vhost-net.h"
49
50 #define MAX_PKT_BURST 32
51 #define VHOST_LOG_PAGE  4096
52
53 static inline void __attribute__((always_inline))
54 vhost_log_page(uint8_t *log_base, uint64_t page)
55 {
56         log_base[page / 8] |= 1 << (page % 8);
57 }
58
59 static inline void __attribute__((always_inline))
60 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
61 {
62         uint64_t page;
63
64         if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
65                    !dev->log_base || !len))
66                 return;
67
68         if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
69                 return;
70
71         /* To make sure guest memory updates are committed before logging */
72         rte_smp_wmb();
73
74         page = addr / VHOST_LOG_PAGE;
75         while (page * VHOST_LOG_PAGE < addr + len) {
76                 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
77                 page += 1;
78         }
79 }
80
81 static inline void __attribute__((always_inline))
82 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
83                      uint64_t offset, uint64_t len)
84 {
85         vhost_log_write(dev, vq->log_guest_addr + offset, len);
86 }
87
88 static bool
89 is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t qp_nb)
90 {
91         return (is_tx ^ (idx & 1)) == 0 && idx < qp_nb * VIRTIO_QNUM;
92 }
93
94 static void
95 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
96 {
97         if (m_buf->ol_flags & PKT_TX_L4_MASK) {
98                 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
99                 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
100
101                 switch (m_buf->ol_flags & PKT_TX_L4_MASK) {
102                 case PKT_TX_TCP_CKSUM:
103                         net_hdr->csum_offset = (offsetof(struct tcp_hdr,
104                                                 cksum));
105                         break;
106                 case PKT_TX_UDP_CKSUM:
107                         net_hdr->csum_offset = (offsetof(struct udp_hdr,
108                                                 dgram_cksum));
109                         break;
110                 case PKT_TX_SCTP_CKSUM:
111                         net_hdr->csum_offset = (offsetof(struct sctp_hdr,
112                                                 cksum));
113                         break;
114                 }
115         }
116
117         if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
118                 if (m_buf->ol_flags & PKT_TX_IPV4)
119                         net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
120                 else
121                         net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
122                 net_hdr->gso_size = m_buf->tso_segsz;
123                 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
124                                         + m_buf->l4_len;
125         }
126 }
127
128 static inline void
129 copy_virtio_net_hdr(struct virtio_net *dev, uint64_t desc_addr,
130                     struct virtio_net_hdr_mrg_rxbuf hdr)
131 {
132         if (dev->vhost_hlen == sizeof(struct virtio_net_hdr_mrg_rxbuf))
133                 *(struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)desc_addr = hdr;
134         else
135                 *(struct virtio_net_hdr *)(uintptr_t)desc_addr = hdr.hdr;
136 }
137
138 static inline int __attribute__((always_inline))
139 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
140                   struct rte_mbuf *m, uint16_t desc_idx, uint32_t *copied)
141 {
142         uint32_t desc_avail, desc_offset;
143         uint32_t mbuf_avail, mbuf_offset;
144         uint32_t cpy_len;
145         struct vring_desc *desc;
146         uint64_t desc_addr;
147         struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0, 0, 0, 0, 0, 0}, 0};
148
149         desc = &vq->desc[desc_idx];
150         if (unlikely(desc->len < dev->vhost_hlen))
151                 return -1;
152
153         desc_addr = gpa_to_vva(dev, desc->addr);
154         rte_prefetch0((void *)(uintptr_t)desc_addr);
155
156         virtio_enqueue_offload(m, &virtio_hdr.hdr);
157         copy_virtio_net_hdr(dev, desc_addr, virtio_hdr);
158         vhost_log_write(dev, desc->addr, dev->vhost_hlen);
159         PRINT_PACKET(dev, (uintptr_t)desc_addr, dev->vhost_hlen, 0);
160
161         desc_offset = dev->vhost_hlen;
162         desc_avail  = desc->len - dev->vhost_hlen;
163
164         *copied = rte_pktmbuf_pkt_len(m);
165         mbuf_avail  = rte_pktmbuf_data_len(m);
166         mbuf_offset = 0;
167         while (mbuf_avail != 0 || m->next != NULL) {
168                 /* done with current mbuf, fetch next */
169                 if (mbuf_avail == 0) {
170                         m = m->next;
171
172                         mbuf_offset = 0;
173                         mbuf_avail  = rte_pktmbuf_data_len(m);
174                 }
175
176                 /* done with current desc buf, fetch next */
177                 if (desc_avail == 0) {
178                         if ((desc->flags & VRING_DESC_F_NEXT) == 0) {
179                                 /* Room in vring buffer is not enough */
180                                 return -1;
181                         }
182                         if (unlikely(desc->next >= vq->size))
183                                 return -1;
184
185                         desc = &vq->desc[desc->next];
186                         desc_addr   = gpa_to_vva(dev, desc->addr);
187                         desc_offset = 0;
188                         desc_avail  = desc->len;
189                 }
190
191                 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
192                 rte_memcpy((void *)((uintptr_t)(desc_addr + desc_offset)),
193                         rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
194                         cpy_len);
195                 vhost_log_write(dev, desc->addr + desc_offset, cpy_len);
196                 PRINT_PACKET(dev, (uintptr_t)(desc_addr + desc_offset),
197                              cpy_len, 0);
198
199                 mbuf_avail  -= cpy_len;
200                 mbuf_offset += cpy_len;
201                 desc_avail  -= cpy_len;
202                 desc_offset += cpy_len;
203         }
204
205         return 0;
206 }
207
208 /*
209  * As many data cores may want to access available buffers
210  * they need to be reserved.
211  */
212 static inline uint32_t
213 reserve_avail_buf(struct vhost_virtqueue *vq, uint32_t count,
214                   uint16_t *start, uint16_t *end)
215 {
216         uint16_t res_start_idx;
217         uint16_t res_end_idx;
218         uint16_t avail_idx;
219         uint16_t free_entries;
220         int success;
221
222         count = RTE_MIN(count, (uint32_t)MAX_PKT_BURST);
223
224 again:
225         res_start_idx = vq->last_used_idx_res;
226         avail_idx = *((volatile uint16_t *)&vq->avail->idx);
227
228         free_entries = avail_idx - res_start_idx;
229         count = RTE_MIN(count, free_entries);
230         if (count == 0)
231                 return 0;
232
233         res_end_idx = res_start_idx + count;
234
235         /*
236          * update vq->last_used_idx_res atomically; try again if failed.
237          *
238          * TODO: Allow to disable cmpset if no concurrency in application.
239          */
240         success = rte_atomic16_cmpset(&vq->last_used_idx_res,
241                                       res_start_idx, res_end_idx);
242         if (unlikely(!success))
243                 goto again;
244
245         *start = res_start_idx;
246         *end   = res_end_idx;
247
248         return count;
249 }
250
251 /**
252  * This function adds buffers to the virtio devices RX virtqueue. Buffers can
253  * be received from the physical port or from another virtio device. A packet
254  * count is returned to indicate the number of packets that are succesfully
255  * added to the RX queue. This function works when the mbuf is scattered, but
256  * it doesn't support the mergeable feature.
257  */
258 static inline uint32_t __attribute__((always_inline))
259 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
260               struct rte_mbuf **pkts, uint32_t count)
261 {
262         struct vhost_virtqueue *vq;
263         uint16_t res_start_idx, res_end_idx;
264         uint16_t desc_indexes[MAX_PKT_BURST];
265         uint32_t i;
266
267         LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
268         if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->virt_qp_nb))) {
269                 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
270                         dev->vid, __func__, queue_id);
271                 return 0;
272         }
273
274         vq = dev->virtqueue[queue_id];
275         if (unlikely(vq->enabled == 0))
276                 return 0;
277
278         count = reserve_avail_buf(vq, count, &res_start_idx, &res_end_idx);
279         if (count == 0)
280                 return 0;
281
282         LOG_DEBUG(VHOST_DATA, "(%d) res_start_idx %d | res_end_idx Index %d\n",
283                 dev->vid, res_start_idx, res_end_idx);
284
285         /* Retrieve all of the desc indexes first to avoid caching issues. */
286         rte_prefetch0(&vq->avail->ring[res_start_idx & (vq->size - 1)]);
287         for (i = 0; i < count; i++) {
288                 desc_indexes[i] = vq->avail->ring[(res_start_idx + i) &
289                                                   (vq->size - 1)];
290         }
291
292         rte_prefetch0(&vq->desc[desc_indexes[0]]);
293         for (i = 0; i < count; i++) {
294                 uint16_t desc_idx = desc_indexes[i];
295                 uint16_t used_idx = (res_start_idx + i) & (vq->size - 1);
296                 uint32_t copied;
297                 int err;
298
299                 err = copy_mbuf_to_desc(dev, vq, pkts[i], desc_idx, &copied);
300
301                 vq->used->ring[used_idx].id = desc_idx;
302                 if (unlikely(err))
303                         vq->used->ring[used_idx].len = dev->vhost_hlen;
304                 else
305                         vq->used->ring[used_idx].len = copied + dev->vhost_hlen;
306                 vhost_log_used_vring(dev, vq,
307                         offsetof(struct vring_used, ring[used_idx]),
308                         sizeof(vq->used->ring[used_idx]));
309
310                 if (i + 1 < count)
311                         rte_prefetch0(&vq->desc[desc_indexes[i+1]]);
312         }
313
314         rte_smp_wmb();
315
316         /* Wait until it's our turn to add our buffer to the used ring. */
317         while (unlikely(vq->last_used_idx != res_start_idx))
318                 rte_pause();
319
320         *(volatile uint16_t *)&vq->used->idx += count;
321         vq->last_used_idx = res_end_idx;
322         vhost_log_used_vring(dev, vq,
323                 offsetof(struct vring_used, idx),
324                 sizeof(vq->used->idx));
325
326         /* flush used->idx update before we read avail->flags. */
327         rte_mb();
328
329         /* Kick the guest if necessary. */
330         if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
331                         && (vq->callfd >= 0))
332                 eventfd_write(vq->callfd, (eventfd_t)1);
333         return count;
334 }
335
336 static inline int
337 fill_vec_buf(struct vhost_virtqueue *vq, uint32_t avail_idx,
338              uint32_t *allocated, uint32_t *vec_idx,
339              struct buf_vector *buf_vec)
340 {
341         uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
342         uint32_t vec_id = *vec_idx;
343         uint32_t len    = *allocated;
344
345         while (1) {
346                 if (unlikely(vec_id >= BUF_VECTOR_MAX || idx >= vq->size))
347                         return -1;
348
349                 len += vq->desc[idx].len;
350                 buf_vec[vec_id].buf_addr = vq->desc[idx].addr;
351                 buf_vec[vec_id].buf_len  = vq->desc[idx].len;
352                 buf_vec[vec_id].desc_idx = idx;
353                 vec_id++;
354
355                 if ((vq->desc[idx].flags & VRING_DESC_F_NEXT) == 0)
356                         break;
357
358                 idx = vq->desc[idx].next;
359         }
360
361         *allocated = len;
362         *vec_idx   = vec_id;
363
364         return 0;
365 }
366
367 /*
368  * As many data cores may want to access available buffers concurrently,
369  * they need to be reserved.
370  *
371  * Returns -1 on fail, 0 on success
372  */
373 static inline int
374 reserve_avail_buf_mergeable(struct vhost_virtqueue *vq, uint32_t size,
375                             uint16_t *start, uint16_t *end,
376                             struct buf_vector *buf_vec)
377 {
378         uint16_t res_start_idx;
379         uint16_t res_cur_idx;
380         uint16_t avail_idx;
381         uint32_t allocated;
382         uint32_t vec_idx;
383         uint16_t tries;
384
385 again:
386         res_start_idx = vq->last_used_idx_res;
387         res_cur_idx  = res_start_idx;
388
389         allocated = 0;
390         vec_idx   = 0;
391         tries     = 0;
392         while (1) {
393                 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
394                 if (unlikely(res_cur_idx == avail_idx))
395                         return -1;
396
397                 if (unlikely(fill_vec_buf(vq, res_cur_idx, &allocated,
398                                           &vec_idx, buf_vec) < 0))
399                         return -1;
400
401                 res_cur_idx++;
402                 tries++;
403
404                 if (allocated >= size)
405                         break;
406
407                 /*
408                  * if we tried all available ring items, and still
409                  * can't get enough buf, it means something abnormal
410                  * happened.
411                  */
412                 if (unlikely(tries >= vq->size))
413                         return -1;
414         }
415
416         /*
417          * update vq->last_used_idx_res atomically.
418          * retry again if failed.
419          */
420         if (rte_atomic16_cmpset(&vq->last_used_idx_res,
421                                 res_start_idx, res_cur_idx) == 0)
422                 goto again;
423
424         *start = res_start_idx;
425         *end   = res_cur_idx;
426         return 0;
427 }
428
429 static inline uint32_t __attribute__((always_inline))
430 copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,
431                             uint16_t res_start_idx, uint16_t res_end_idx,
432                             struct rte_mbuf *m, struct buf_vector *buf_vec)
433 {
434         struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0, 0, 0, 0, 0, 0}, 0};
435         uint32_t vec_idx = 0;
436         uint16_t cur_idx = res_start_idx;
437         uint64_t desc_addr;
438         uint32_t mbuf_offset, mbuf_avail;
439         uint32_t desc_offset, desc_avail;
440         uint32_t cpy_len;
441         uint16_t desc_idx, used_idx;
442
443         if (unlikely(m == NULL))
444                 return 0;
445
446         LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
447                 dev->vid, cur_idx, res_end_idx);
448
449         if (buf_vec[vec_idx].buf_len < dev->vhost_hlen)
450                 return -1;
451
452         desc_addr = gpa_to_vva(dev, buf_vec[vec_idx].buf_addr);
453         rte_prefetch0((void *)(uintptr_t)desc_addr);
454
455         virtio_hdr.num_buffers = res_end_idx - res_start_idx;
456         LOG_DEBUG(VHOST_DATA, "(%d) RX: num merge buffers %d\n",
457                 dev->vid, virtio_hdr.num_buffers);
458
459         virtio_enqueue_offload(m, &virtio_hdr.hdr);
460         copy_virtio_net_hdr(dev, desc_addr, virtio_hdr);
461         vhost_log_write(dev, buf_vec[vec_idx].buf_addr, dev->vhost_hlen);
462         PRINT_PACKET(dev, (uintptr_t)desc_addr, dev->vhost_hlen, 0);
463
464         desc_avail  = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
465         desc_offset = dev->vhost_hlen;
466
467         mbuf_avail  = rte_pktmbuf_data_len(m);
468         mbuf_offset = 0;
469         while (mbuf_avail != 0 || m->next != NULL) {
470                 /* done with current desc buf, get the next one */
471                 if (desc_avail == 0) {
472                         desc_idx = buf_vec[vec_idx].desc_idx;
473
474                         if (!(vq->desc[desc_idx].flags & VRING_DESC_F_NEXT)) {
475                                 /* Update used ring with desc information */
476                                 used_idx = cur_idx++ & (vq->size - 1);
477                                 vq->used->ring[used_idx].id  = desc_idx;
478                                 vq->used->ring[used_idx].len = desc_offset;
479                                 vhost_log_used_vring(dev, vq,
480                                         offsetof(struct vring_used,
481                                                  ring[used_idx]),
482                                         sizeof(vq->used->ring[used_idx]));
483                         }
484
485                         vec_idx++;
486                         desc_addr = gpa_to_vva(dev, buf_vec[vec_idx].buf_addr);
487
488                         /* Prefetch buffer address. */
489                         rte_prefetch0((void *)(uintptr_t)desc_addr);
490                         desc_offset = 0;
491                         desc_avail  = buf_vec[vec_idx].buf_len;
492                 }
493
494                 /* done with current mbuf, get the next one */
495                 if (mbuf_avail == 0) {
496                         m = m->next;
497
498                         mbuf_offset = 0;
499                         mbuf_avail  = rte_pktmbuf_data_len(m);
500                 }
501
502                 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
503                 rte_memcpy((void *)((uintptr_t)(desc_addr + desc_offset)),
504                         rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
505                         cpy_len);
506                 vhost_log_write(dev, buf_vec[vec_idx].buf_addr + desc_offset,
507                         cpy_len);
508                 PRINT_PACKET(dev, (uintptr_t)(desc_addr + desc_offset),
509                         cpy_len, 0);
510
511                 mbuf_avail  -= cpy_len;
512                 mbuf_offset += cpy_len;
513                 desc_avail  -= cpy_len;
514                 desc_offset += cpy_len;
515         }
516
517         used_idx = cur_idx & (vq->size - 1);
518         vq->used->ring[used_idx].id = buf_vec[vec_idx].desc_idx;
519         vq->used->ring[used_idx].len = desc_offset;
520         vhost_log_used_vring(dev, vq,
521                 offsetof(struct vring_used, ring[used_idx]),
522                 sizeof(vq->used->ring[used_idx]));
523
524         return res_end_idx - res_start_idx;
525 }
526
527 static inline uint32_t __attribute__((always_inline))
528 virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,
529         struct rte_mbuf **pkts, uint32_t count)
530 {
531         struct vhost_virtqueue *vq;
532         uint32_t pkt_idx = 0, nr_used = 0;
533         uint16_t start, end;
534         struct buf_vector buf_vec[BUF_VECTOR_MAX];
535
536         LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
537         if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->virt_qp_nb))) {
538                 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
539                         dev->vid, __func__, queue_id);
540                 return 0;
541         }
542
543         vq = dev->virtqueue[queue_id];
544         if (unlikely(vq->enabled == 0))
545                 return 0;
546
547         count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
548         if (count == 0)
549                 return 0;
550
551         for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
552                 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
553
554                 if (unlikely(reserve_avail_buf_mergeable(vq, pkt_len, &start,
555                                                          &end, buf_vec) < 0)) {
556                         LOG_DEBUG(VHOST_DATA,
557                                 "(%d) failed to get enough desc from vring\n",
558                                 dev->vid);
559                         break;
560                 }
561
562                 nr_used = copy_mbuf_to_desc_mergeable(dev, vq, start, end,
563                                                       pkts[pkt_idx], buf_vec);
564                 rte_smp_wmb();
565
566                 /*
567                  * Wait until it's our turn to add our buffer
568                  * to the used ring.
569                  */
570                 while (unlikely(vq->last_used_idx != start))
571                         rte_pause();
572
573                 *(volatile uint16_t *)&vq->used->idx += nr_used;
574                 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
575                         sizeof(vq->used->idx));
576                 vq->last_used_idx = end;
577         }
578
579         if (likely(pkt_idx)) {
580                 /* flush used->idx update before we read avail->flags. */
581                 rte_mb();
582
583                 /* Kick the guest if necessary. */
584                 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
585                                 && (vq->callfd >= 0))
586                         eventfd_write(vq->callfd, (eventfd_t)1);
587         }
588
589         return pkt_idx;
590 }
591
592 uint16_t
593 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
594         struct rte_mbuf **pkts, uint16_t count)
595 {
596         struct virtio_net *dev = get_device(vid);
597
598         if (!dev)
599                 return 0;
600
601         if (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF))
602                 return virtio_dev_merge_rx(dev, queue_id, pkts, count);
603         else
604                 return virtio_dev_rx(dev, queue_id, pkts, count);
605 }
606
607 static void
608 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
609 {
610         struct ipv4_hdr *ipv4_hdr;
611         struct ipv6_hdr *ipv6_hdr;
612         void *l3_hdr = NULL;
613         struct ether_hdr *eth_hdr;
614         uint16_t ethertype;
615
616         eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
617
618         m->l2_len = sizeof(struct ether_hdr);
619         ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
620
621         if (ethertype == ETHER_TYPE_VLAN) {
622                 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
623
624                 m->l2_len += sizeof(struct vlan_hdr);
625                 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
626         }
627
628         l3_hdr = (char *)eth_hdr + m->l2_len;
629
630         switch (ethertype) {
631         case ETHER_TYPE_IPv4:
632                 ipv4_hdr = (struct ipv4_hdr *)l3_hdr;
633                 *l4_proto = ipv4_hdr->next_proto_id;
634                 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
635                 *l4_hdr = (char *)l3_hdr + m->l3_len;
636                 m->ol_flags |= PKT_TX_IPV4;
637                 break;
638         case ETHER_TYPE_IPv6:
639                 ipv6_hdr = (struct ipv6_hdr *)l3_hdr;
640                 *l4_proto = ipv6_hdr->proto;
641                 m->l3_len = sizeof(struct ipv6_hdr);
642                 *l4_hdr = (char *)l3_hdr + m->l3_len;
643                 m->ol_flags |= PKT_TX_IPV6;
644                 break;
645         default:
646                 m->l3_len = 0;
647                 *l4_proto = 0;
648                 break;
649         }
650 }
651
652 static inline void __attribute__((always_inline))
653 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
654 {
655         uint16_t l4_proto = 0;
656         void *l4_hdr = NULL;
657         struct tcp_hdr *tcp_hdr = NULL;
658
659         parse_ethernet(m, &l4_proto, &l4_hdr);
660         if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
661                 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
662                         switch (hdr->csum_offset) {
663                         case (offsetof(struct tcp_hdr, cksum)):
664                                 if (l4_proto == IPPROTO_TCP)
665                                         m->ol_flags |= PKT_TX_TCP_CKSUM;
666                                 break;
667                         case (offsetof(struct udp_hdr, dgram_cksum)):
668                                 if (l4_proto == IPPROTO_UDP)
669                                         m->ol_flags |= PKT_TX_UDP_CKSUM;
670                                 break;
671                         case (offsetof(struct sctp_hdr, cksum)):
672                                 if (l4_proto == IPPROTO_SCTP)
673                                         m->ol_flags |= PKT_TX_SCTP_CKSUM;
674                                 break;
675                         default:
676                                 break;
677                         }
678                 }
679         }
680
681         if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
682                 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
683                 case VIRTIO_NET_HDR_GSO_TCPV4:
684                 case VIRTIO_NET_HDR_GSO_TCPV6:
685                         tcp_hdr = (struct tcp_hdr *)l4_hdr;
686                         m->ol_flags |= PKT_TX_TCP_SEG;
687                         m->tso_segsz = hdr->gso_size;
688                         m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
689                         break;
690                 default:
691                         RTE_LOG(WARNING, VHOST_DATA,
692                                 "unsupported gso type %u.\n", hdr->gso_type);
693                         break;
694                 }
695         }
696 }
697
698 #define RARP_PKT_SIZE   64
699
700 static int
701 make_rarp_packet(struct rte_mbuf *rarp_mbuf, const struct ether_addr *mac)
702 {
703         struct ether_hdr *eth_hdr;
704         struct arp_hdr  *rarp;
705
706         if (rarp_mbuf->buf_len < 64) {
707                 RTE_LOG(WARNING, VHOST_DATA,
708                         "failed to make RARP; mbuf size too small %u (< %d)\n",
709                         rarp_mbuf->buf_len, RARP_PKT_SIZE);
710                 return -1;
711         }
712
713         /* Ethernet header. */
714         eth_hdr = rte_pktmbuf_mtod_offset(rarp_mbuf, struct ether_hdr *, 0);
715         memset(eth_hdr->d_addr.addr_bytes, 0xff, ETHER_ADDR_LEN);
716         ether_addr_copy(mac, &eth_hdr->s_addr);
717         eth_hdr->ether_type = htons(ETHER_TYPE_RARP);
718
719         /* RARP header. */
720         rarp = (struct arp_hdr *)(eth_hdr + 1);
721         rarp->arp_hrd = htons(ARP_HRD_ETHER);
722         rarp->arp_pro = htons(ETHER_TYPE_IPv4);
723         rarp->arp_hln = ETHER_ADDR_LEN;
724         rarp->arp_pln = 4;
725         rarp->arp_op  = htons(ARP_OP_REVREQUEST);
726
727         ether_addr_copy(mac, &rarp->arp_data.arp_sha);
728         ether_addr_copy(mac, &rarp->arp_data.arp_tha);
729         memset(&rarp->arp_data.arp_sip, 0x00, 4);
730         memset(&rarp->arp_data.arp_tip, 0x00, 4);
731
732         rarp_mbuf->pkt_len  = rarp_mbuf->data_len = RARP_PKT_SIZE;
733
734         return 0;
735 }
736
737 static inline int __attribute__((always_inline))
738 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
739                   struct rte_mbuf *m, uint16_t desc_idx,
740                   struct rte_mempool *mbuf_pool)
741 {
742         struct vring_desc *desc;
743         uint64_t desc_addr;
744         uint32_t desc_avail, desc_offset;
745         uint32_t mbuf_avail, mbuf_offset;
746         uint32_t cpy_len;
747         struct rte_mbuf *cur = m, *prev = m;
748         struct virtio_net_hdr *hdr;
749         /* A counter to avoid desc dead loop chain */
750         uint32_t nr_desc = 1;
751
752         desc = &vq->desc[desc_idx];
753         if (unlikely(desc->len < dev->vhost_hlen))
754                 return -1;
755
756         desc_addr = gpa_to_vva(dev, desc->addr);
757         rte_prefetch0((void *)(uintptr_t)desc_addr);
758
759         /* Retrieve virtio net header */
760         hdr = (struct virtio_net_hdr *)((uintptr_t)desc_addr);
761         desc_avail  = desc->len - dev->vhost_hlen;
762         desc_offset = dev->vhost_hlen;
763
764         mbuf_offset = 0;
765         mbuf_avail  = m->buf_len - RTE_PKTMBUF_HEADROOM;
766         while (desc_avail != 0 || (desc->flags & VRING_DESC_F_NEXT) != 0) {
767                 /* This desc reaches to its end, get the next one */
768                 if (desc_avail == 0) {
769                         if (unlikely(desc->next >= vq->size ||
770                                      ++nr_desc >= vq->size))
771                                 return -1;
772                         desc = &vq->desc[desc->next];
773
774                         desc_addr = gpa_to_vva(dev, desc->addr);
775                         rte_prefetch0((void *)(uintptr_t)desc_addr);
776
777                         desc_offset = 0;
778                         desc_avail  = desc->len;
779
780                         PRINT_PACKET(dev, (uintptr_t)desc_addr, desc->len, 0);
781                 }
782
783                 /*
784                  * This mbuf reaches to its end, get a new one
785                  * to hold more data.
786                  */
787                 if (mbuf_avail == 0) {
788                         cur = rte_pktmbuf_alloc(mbuf_pool);
789                         if (unlikely(cur == NULL)) {
790                                 RTE_LOG(ERR, VHOST_DATA, "Failed to "
791                                         "allocate memory for mbuf.\n");
792                                 return -1;
793                         }
794
795                         prev->next = cur;
796                         prev->data_len = mbuf_offset;
797                         m->nb_segs += 1;
798                         m->pkt_len += mbuf_offset;
799                         prev = cur;
800
801                         mbuf_offset = 0;
802                         mbuf_avail  = cur->buf_len - RTE_PKTMBUF_HEADROOM;
803                 }
804
805                 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
806                 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *, mbuf_offset),
807                         (void *)((uintptr_t)(desc_addr + desc_offset)),
808                         cpy_len);
809
810                 mbuf_avail  -= cpy_len;
811                 mbuf_offset += cpy_len;
812                 desc_avail  -= cpy_len;
813                 desc_offset += cpy_len;
814         }
815
816         prev->data_len = mbuf_offset;
817         m->pkt_len    += mbuf_offset;
818
819         if (hdr->flags != 0 || hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE)
820                 vhost_dequeue_offload(hdr, m);
821
822         return 0;
823 }
824
825 uint16_t
826 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
827         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
828 {
829         struct virtio_net *dev;
830         struct rte_mbuf *rarp_mbuf = NULL;
831         struct vhost_virtqueue *vq;
832         uint32_t desc_indexes[MAX_PKT_BURST];
833         uint32_t used_idx;
834         uint32_t i = 0;
835         uint16_t free_entries;
836         uint16_t avail_idx;
837
838         dev = get_device(vid);
839         if (!dev)
840                 return 0;
841
842         if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->virt_qp_nb))) {
843                 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
844                         dev->vid, __func__, queue_id);
845                 return 0;
846         }
847
848         vq = dev->virtqueue[queue_id];
849         if (unlikely(vq->enabled == 0))
850                 return 0;
851
852         /*
853          * Construct a RARP broadcast packet, and inject it to the "pkts"
854          * array, to looks like that guest actually send such packet.
855          *
856          * Check user_send_rarp() for more information.
857          */
858         if (unlikely(rte_atomic16_cmpset((volatile uint16_t *)
859                                          &dev->broadcast_rarp.cnt, 1, 0))) {
860                 rarp_mbuf = rte_pktmbuf_alloc(mbuf_pool);
861                 if (rarp_mbuf == NULL) {
862                         RTE_LOG(ERR, VHOST_DATA,
863                                 "Failed to allocate memory for mbuf.\n");
864                         return 0;
865                 }
866
867                 if (make_rarp_packet(rarp_mbuf, &dev->mac)) {
868                         rte_pktmbuf_free(rarp_mbuf);
869                         rarp_mbuf = NULL;
870                 } else {
871                         count -= 1;
872                 }
873         }
874
875         avail_idx =  *((volatile uint16_t *)&vq->avail->idx);
876         free_entries = avail_idx - vq->last_used_idx;
877         if (free_entries == 0)
878                 goto out;
879
880         LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
881
882         /* Prefetch available ring to retrieve head indexes. */
883         used_idx = vq->last_used_idx & (vq->size - 1);
884         rte_prefetch0(&vq->avail->ring[used_idx]);
885
886         count = RTE_MIN(count, MAX_PKT_BURST);
887         count = RTE_MIN(count, free_entries);
888         LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
889                         dev->vid, count);
890
891         /* Retrieve all of the head indexes first to avoid caching issues. */
892         for (i = 0; i < count; i++) {
893                 desc_indexes[i] = vq->avail->ring[(vq->last_used_idx + i) &
894                                         (vq->size - 1)];
895         }
896
897         /* Prefetch descriptor index. */
898         rte_prefetch0(&vq->desc[desc_indexes[0]]);
899         rte_prefetch0(&vq->used->ring[vq->last_used_idx & (vq->size - 1)]);
900
901         for (i = 0; i < count; i++) {
902                 int err;
903
904                 if (likely(i + 1 < count)) {
905                         rte_prefetch0(&vq->desc[desc_indexes[i + 1]]);
906                         rte_prefetch0(&vq->used->ring[(used_idx + 1) &
907                                                       (vq->size - 1)]);
908                 }
909
910                 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
911                 if (unlikely(pkts[i] == NULL)) {
912                         RTE_LOG(ERR, VHOST_DATA,
913                                 "Failed to allocate memory for mbuf.\n");
914                         break;
915                 }
916                 err = copy_desc_to_mbuf(dev, vq, pkts[i], desc_indexes[i],
917                                         mbuf_pool);
918                 if (unlikely(err)) {
919                         rte_pktmbuf_free(pkts[i]);
920                         break;
921                 }
922
923                 used_idx = vq->last_used_idx++ & (vq->size - 1);
924                 vq->used->ring[used_idx].id  = desc_indexes[i];
925                 vq->used->ring[used_idx].len = 0;
926                 vhost_log_used_vring(dev, vq,
927                                 offsetof(struct vring_used, ring[used_idx]),
928                                 sizeof(vq->used->ring[used_idx]));
929         }
930
931         rte_smp_wmb();
932         rte_smp_rmb();
933         vq->used->idx += i;
934         vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
935                         sizeof(vq->used->idx));
936
937         /* Kick guest if required. */
938         if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
939                         && (vq->callfd >= 0))
940                 eventfd_write(vq->callfd, (eventfd_t)1);
941
942 out:
943         if (unlikely(rarp_mbuf != NULL)) {
944                 /*
945                  * Inject it to the head of "pkts" array, so that switch's mac
946                  * learning table will get updated first.
947                  */
948                 memmove(&pkts[1], pkts, i * sizeof(struct rte_mbuf *));
949                 pkts[0] = rarp_mbuf;
950                 i += 1;
951         }
952
953         return i;
954 }