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