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