8d87508c5a27292fc9e7f7d8c1cf3f53169033bd
[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 vhost_virtqueue *vq, uint64_t desc_addr,
130                     struct virtio_net_hdr_mrg_rxbuf hdr)
131 {
132         if (vq->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 < vq->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(vq, desc_addr, virtio_hdr);
158         vhost_log_write(dev, desc->addr, vq->vhost_hlen);
159         PRINT_PACKET(dev, (uintptr_t)desc_addr, vq->vhost_hlen, 0);
160
161         desc_offset = vq->vhost_hlen;
162         desc_avail  = desc->len - vq->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 = vq->vhost_hlen;
304                 else
305                         vq->used->ring[used_idx].len = copied + vq->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 {
340         uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
341         uint32_t vec_id = *vec_idx;
342         uint32_t len    = *allocated;
343
344         while (1) {
345                 if (unlikely(vec_id >= BUF_VECTOR_MAX || idx >= vq->size))
346                         return -1;
347
348                 len += vq->desc[idx].len;
349                 vq->buf_vec[vec_id].buf_addr = vq->desc[idx].addr;
350                 vq->buf_vec[vec_id].buf_len  = vq->desc[idx].len;
351                 vq->buf_vec[vec_id].desc_idx = idx;
352                 vec_id++;
353
354                 if ((vq->desc[idx].flags & VRING_DESC_F_NEXT) == 0)
355                         break;
356
357                 idx = vq->desc[idx].next;
358         }
359
360         *allocated = len;
361         *vec_idx   = vec_id;
362
363         return 0;
364 }
365
366 /*
367  * As many data cores may want to access available buffers concurrently,
368  * they need to be reserved.
369  *
370  * Returns -1 on fail, 0 on success
371  */
372 static inline int
373 reserve_avail_buf_mergeable(struct vhost_virtqueue *vq, uint32_t size,
374                             uint16_t *start, uint16_t *end)
375 {
376         uint16_t res_start_idx;
377         uint16_t res_cur_idx;
378         uint16_t avail_idx;
379         uint32_t allocated;
380         uint32_t vec_idx;
381         uint16_t tries;
382
383 again:
384         res_start_idx = vq->last_used_idx_res;
385         res_cur_idx  = res_start_idx;
386
387         allocated = 0;
388         vec_idx   = 0;
389         tries     = 0;
390         while (1) {
391                 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
392                 if (unlikely(res_cur_idx == avail_idx))
393                         return -1;
394
395                 if (unlikely(fill_vec_buf(vq, res_cur_idx, &allocated,
396                                           &vec_idx) < 0))
397                         return -1;
398
399                 res_cur_idx++;
400                 tries++;
401
402                 if (allocated >= size)
403                         break;
404
405                 /*
406                  * if we tried all available ring items, and still
407                  * can't get enough buf, it means something abnormal
408                  * happened.
409                  */
410                 if (unlikely(tries >= vq->size))
411                         return -1;
412         }
413
414         /*
415          * update vq->last_used_idx_res atomically.
416          * retry again if failed.
417          */
418         if (rte_atomic16_cmpset(&vq->last_used_idx_res,
419                                 res_start_idx, res_cur_idx) == 0)
420                 goto again;
421
422         *start = res_start_idx;
423         *end   = res_cur_idx;
424         return 0;
425 }
426
427 static inline uint32_t __attribute__((always_inline))
428 copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,
429                             uint16_t res_start_idx, uint16_t res_end_idx,
430                             struct rte_mbuf *m)
431 {
432         struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0, 0, 0, 0, 0, 0}, 0};
433         uint32_t vec_idx = 0;
434         uint16_t cur_idx = res_start_idx;
435         uint64_t desc_addr;
436         uint32_t mbuf_offset, mbuf_avail;
437         uint32_t desc_offset, desc_avail;
438         uint32_t cpy_len;
439         uint16_t desc_idx, used_idx;
440
441         if (unlikely(m == NULL))
442                 return 0;
443
444         LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
445                 dev->vid, cur_idx, res_end_idx);
446
447         if (vq->buf_vec[vec_idx].buf_len < vq->vhost_hlen)
448                 return -1;
449
450         desc_addr = gpa_to_vva(dev, vq->buf_vec[vec_idx].buf_addr);
451         rte_prefetch0((void *)(uintptr_t)desc_addr);
452
453         virtio_hdr.num_buffers = res_end_idx - res_start_idx;
454         LOG_DEBUG(VHOST_DATA, "(%d) RX: num merge buffers %d\n",
455                 dev->vid, virtio_hdr.num_buffers);
456
457         virtio_enqueue_offload(m, &virtio_hdr.hdr);
458         copy_virtio_net_hdr(vq, desc_addr, virtio_hdr);
459         vhost_log_write(dev, vq->buf_vec[vec_idx].buf_addr, vq->vhost_hlen);
460         PRINT_PACKET(dev, (uintptr_t)desc_addr, vq->vhost_hlen, 0);
461
462         desc_avail  = vq->buf_vec[vec_idx].buf_len - vq->vhost_hlen;
463         desc_offset = vq->vhost_hlen;
464
465         mbuf_avail  = rte_pktmbuf_data_len(m);
466         mbuf_offset = 0;
467         while (mbuf_avail != 0 || m->next != NULL) {
468                 /* done with current desc buf, get the next one */
469                 if (desc_avail == 0) {
470                         desc_idx = vq->buf_vec[vec_idx].desc_idx;
471
472                         if (!(vq->desc[desc_idx].flags & VRING_DESC_F_NEXT)) {
473                                 /* Update used ring with desc information */
474                                 used_idx = cur_idx++ & (vq->size - 1);
475                                 vq->used->ring[used_idx].id  = desc_idx;
476                                 vq->used->ring[used_idx].len = desc_offset;
477                                 vhost_log_used_vring(dev, vq,
478                                         offsetof(struct vring_used,
479                                                  ring[used_idx]),
480                                         sizeof(vq->used->ring[used_idx]));
481                         }
482
483                         vec_idx++;
484                         desc_addr = gpa_to_vva(dev, vq->buf_vec[vec_idx].buf_addr);
485
486                         /* Prefetch buffer address. */
487                         rte_prefetch0((void *)(uintptr_t)desc_addr);
488                         desc_offset = 0;
489                         desc_avail  = vq->buf_vec[vec_idx].buf_len;
490                 }
491
492                 /* done with current mbuf, get the next one */
493                 if (mbuf_avail == 0) {
494                         m = m->next;
495
496                         mbuf_offset = 0;
497                         mbuf_avail  = rte_pktmbuf_data_len(m);
498                 }
499
500                 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
501                 rte_memcpy((void *)((uintptr_t)(desc_addr + desc_offset)),
502                         rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
503                         cpy_len);
504                 vhost_log_write(dev, vq->buf_vec[vec_idx].buf_addr + desc_offset,
505                         cpy_len);
506                 PRINT_PACKET(dev, (uintptr_t)(desc_addr + desc_offset),
507                         cpy_len, 0);
508
509                 mbuf_avail  -= cpy_len;
510                 mbuf_offset += cpy_len;
511                 desc_avail  -= cpy_len;
512                 desc_offset += cpy_len;
513         }
514
515         used_idx = cur_idx & (vq->size - 1);
516         vq->used->ring[used_idx].id = vq->buf_vec[vec_idx].desc_idx;
517         vq->used->ring[used_idx].len = desc_offset;
518         vhost_log_used_vring(dev, vq,
519                 offsetof(struct vring_used, ring[used_idx]),
520                 sizeof(vq->used->ring[used_idx]));
521
522         return res_end_idx - res_start_idx;
523 }
524
525 static inline uint32_t __attribute__((always_inline))
526 virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,
527         struct rte_mbuf **pkts, uint32_t count)
528 {
529         struct vhost_virtqueue *vq;
530         uint32_t pkt_idx = 0, nr_used = 0;
531         uint16_t start, end;
532
533         LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
534         if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->virt_qp_nb))) {
535                 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
536                         dev->vid, __func__, queue_id);
537                 return 0;
538         }
539
540         vq = dev->virtqueue[queue_id];
541         if (unlikely(vq->enabled == 0))
542                 return 0;
543
544         count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
545         if (count == 0)
546                 return 0;
547
548         for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
549                 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + vq->vhost_hlen;
550
551                 if (unlikely(reserve_avail_buf_mergeable(vq, pkt_len,
552                                                          &start, &end) < 0)) {
553                         LOG_DEBUG(VHOST_DATA,
554                                 "(%d) failed to get enough desc from vring\n",
555                                 dev->vid);
556                         break;
557                 }
558
559                 nr_used = copy_mbuf_to_desc_mergeable(dev, vq, start, end,
560                                                       pkts[pkt_idx]);
561                 rte_smp_wmb();
562
563                 /*
564                  * Wait until it's our turn to add our buffer
565                  * to the used ring.
566                  */
567                 while (unlikely(vq->last_used_idx != start))
568                         rte_pause();
569
570                 *(volatile uint16_t *)&vq->used->idx += nr_used;
571                 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
572                         sizeof(vq->used->idx));
573                 vq->last_used_idx = end;
574         }
575
576         if (likely(pkt_idx)) {
577                 /* flush used->idx update before we read avail->flags. */
578                 rte_mb();
579
580                 /* Kick the guest if necessary. */
581                 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
582                                 && (vq->callfd >= 0))
583                         eventfd_write(vq->callfd, (eventfd_t)1);
584         }
585
586         return pkt_idx;
587 }
588
589 uint16_t
590 rte_vhost_enqueue_burst(struct virtio_net *dev, uint16_t queue_id,
591         struct rte_mbuf **pkts, uint16_t count)
592 {
593         if (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF))
594                 return virtio_dev_merge_rx(dev, queue_id, pkts, count);
595         else
596                 return virtio_dev_rx(dev, queue_id, pkts, count);
597 }
598
599 static void
600 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
601 {
602         struct ipv4_hdr *ipv4_hdr;
603         struct ipv6_hdr *ipv6_hdr;
604         void *l3_hdr = NULL;
605         struct ether_hdr *eth_hdr;
606         uint16_t ethertype;
607
608         eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
609
610         m->l2_len = sizeof(struct ether_hdr);
611         ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
612
613         if (ethertype == ETHER_TYPE_VLAN) {
614                 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
615
616                 m->l2_len += sizeof(struct vlan_hdr);
617                 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
618         }
619
620         l3_hdr = (char *)eth_hdr + m->l2_len;
621
622         switch (ethertype) {
623         case ETHER_TYPE_IPv4:
624                 ipv4_hdr = (struct ipv4_hdr *)l3_hdr;
625                 *l4_proto = ipv4_hdr->next_proto_id;
626                 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
627                 *l4_hdr = (char *)l3_hdr + m->l3_len;
628                 m->ol_flags |= PKT_TX_IPV4;
629                 break;
630         case ETHER_TYPE_IPv6:
631                 ipv6_hdr = (struct ipv6_hdr *)l3_hdr;
632                 *l4_proto = ipv6_hdr->proto;
633                 m->l3_len = sizeof(struct ipv6_hdr);
634                 *l4_hdr = (char *)l3_hdr + m->l3_len;
635                 m->ol_flags |= PKT_TX_IPV6;
636                 break;
637         default:
638                 m->l3_len = 0;
639                 *l4_proto = 0;
640                 break;
641         }
642 }
643
644 static inline void __attribute__((always_inline))
645 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
646 {
647         uint16_t l4_proto = 0;
648         void *l4_hdr = NULL;
649         struct tcp_hdr *tcp_hdr = NULL;
650
651         parse_ethernet(m, &l4_proto, &l4_hdr);
652         if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
653                 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
654                         switch (hdr->csum_offset) {
655                         case (offsetof(struct tcp_hdr, cksum)):
656                                 if (l4_proto == IPPROTO_TCP)
657                                         m->ol_flags |= PKT_TX_TCP_CKSUM;
658                                 break;
659                         case (offsetof(struct udp_hdr, dgram_cksum)):
660                                 if (l4_proto == IPPROTO_UDP)
661                                         m->ol_flags |= PKT_TX_UDP_CKSUM;
662                                 break;
663                         case (offsetof(struct sctp_hdr, cksum)):
664                                 if (l4_proto == IPPROTO_SCTP)
665                                         m->ol_flags |= PKT_TX_SCTP_CKSUM;
666                                 break;
667                         default:
668                                 break;
669                         }
670                 }
671         }
672
673         if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
674                 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
675                 case VIRTIO_NET_HDR_GSO_TCPV4:
676                 case VIRTIO_NET_HDR_GSO_TCPV6:
677                         tcp_hdr = (struct tcp_hdr *)l4_hdr;
678                         m->ol_flags |= PKT_TX_TCP_SEG;
679                         m->tso_segsz = hdr->gso_size;
680                         m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
681                         break;
682                 default:
683                         RTE_LOG(WARNING, VHOST_DATA,
684                                 "unsupported gso type %u.\n", hdr->gso_type);
685                         break;
686                 }
687         }
688 }
689
690 #define RARP_PKT_SIZE   64
691
692 static int
693 make_rarp_packet(struct rte_mbuf *rarp_mbuf, const struct ether_addr *mac)
694 {
695         struct ether_hdr *eth_hdr;
696         struct arp_hdr  *rarp;
697
698         if (rarp_mbuf->buf_len < 64) {
699                 RTE_LOG(WARNING, VHOST_DATA,
700                         "failed to make RARP; mbuf size too small %u (< %d)\n",
701                         rarp_mbuf->buf_len, RARP_PKT_SIZE);
702                 return -1;
703         }
704
705         /* Ethernet header. */
706         eth_hdr = rte_pktmbuf_mtod_offset(rarp_mbuf, struct ether_hdr *, 0);
707         memset(eth_hdr->d_addr.addr_bytes, 0xff, ETHER_ADDR_LEN);
708         ether_addr_copy(mac, &eth_hdr->s_addr);
709         eth_hdr->ether_type = htons(ETHER_TYPE_RARP);
710
711         /* RARP header. */
712         rarp = (struct arp_hdr *)(eth_hdr + 1);
713         rarp->arp_hrd = htons(ARP_HRD_ETHER);
714         rarp->arp_pro = htons(ETHER_TYPE_IPv4);
715         rarp->arp_hln = ETHER_ADDR_LEN;
716         rarp->arp_pln = 4;
717         rarp->arp_op  = htons(ARP_OP_REVREQUEST);
718
719         ether_addr_copy(mac, &rarp->arp_data.arp_sha);
720         ether_addr_copy(mac, &rarp->arp_data.arp_tha);
721         memset(&rarp->arp_data.arp_sip, 0x00, 4);
722         memset(&rarp->arp_data.arp_tip, 0x00, 4);
723
724         rarp_mbuf->pkt_len  = rarp_mbuf->data_len = RARP_PKT_SIZE;
725
726         return 0;
727 }
728
729 static inline int __attribute__((always_inline))
730 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
731                   struct rte_mbuf *m, uint16_t desc_idx,
732                   struct rte_mempool *mbuf_pool)
733 {
734         struct vring_desc *desc;
735         uint64_t desc_addr;
736         uint32_t desc_avail, desc_offset;
737         uint32_t mbuf_avail, mbuf_offset;
738         uint32_t cpy_len;
739         struct rte_mbuf *cur = m, *prev = m;
740         struct virtio_net_hdr *hdr;
741         /* A counter to avoid desc dead loop chain */
742         uint32_t nr_desc = 1;
743
744         desc = &vq->desc[desc_idx];
745         if (unlikely(desc->len < vq->vhost_hlen))
746                 return -1;
747
748         desc_addr = gpa_to_vva(dev, desc->addr);
749         rte_prefetch0((void *)(uintptr_t)desc_addr);
750
751         /* Retrieve virtio net header */
752         hdr = (struct virtio_net_hdr *)((uintptr_t)desc_addr);
753         desc_avail  = desc->len - vq->vhost_hlen;
754         desc_offset = vq->vhost_hlen;
755
756         mbuf_offset = 0;
757         mbuf_avail  = m->buf_len - RTE_PKTMBUF_HEADROOM;
758         while (desc_avail != 0 || (desc->flags & VRING_DESC_F_NEXT) != 0) {
759                 /* This desc reaches to its end, get the next one */
760                 if (desc_avail == 0) {
761                         if (unlikely(desc->next >= vq->size ||
762                                      ++nr_desc >= vq->size))
763                                 return -1;
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, "(%d) %s: invalid virtqueue idx %d.\n",
831                         dev->vid, __func__, queue_id);
832                 return 0;
833         }
834
835         vq = dev->virtqueue[queue_id];
836         if (unlikely(vq->enabled == 0))
837                 return 0;
838
839         /*
840          * Construct a RARP broadcast packet, and inject it to the "pkts"
841          * array, to looks like that guest actually send such packet.
842          *
843          * Check user_send_rarp() for more information.
844          */
845         if (unlikely(rte_atomic16_cmpset((volatile uint16_t *)
846                                          &dev->broadcast_rarp.cnt, 1, 0))) {
847                 rarp_mbuf = rte_pktmbuf_alloc(mbuf_pool);
848                 if (rarp_mbuf == NULL) {
849                         RTE_LOG(ERR, VHOST_DATA,
850                                 "Failed to allocate memory for mbuf.\n");
851                         return 0;
852                 }
853
854                 if (make_rarp_packet(rarp_mbuf, &dev->mac)) {
855                         rte_pktmbuf_free(rarp_mbuf);
856                         rarp_mbuf = NULL;
857                 } else {
858                         count -= 1;
859                 }
860         }
861
862         avail_idx =  *((volatile uint16_t *)&vq->avail->idx);
863         free_entries = avail_idx - vq->last_used_idx;
864         if (free_entries == 0)
865                 goto out;
866
867         LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
868
869         /* Prefetch available ring to retrieve head indexes. */
870         used_idx = vq->last_used_idx & (vq->size - 1);
871         rte_prefetch0(&vq->avail->ring[used_idx]);
872
873         count = RTE_MIN(count, MAX_PKT_BURST);
874         count = RTE_MIN(count, free_entries);
875         LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
876                         dev->vid, count);
877
878         /* Retrieve all of the head indexes first to avoid caching issues. */
879         for (i = 0; i < count; i++) {
880                 desc_indexes[i] = vq->avail->ring[(vq->last_used_idx + i) &
881                                         (vq->size - 1)];
882         }
883
884         /* Prefetch descriptor index. */
885         rte_prefetch0(&vq->desc[desc_indexes[0]]);
886         rte_prefetch0(&vq->used->ring[vq->last_used_idx & (vq->size - 1)]);
887
888         for (i = 0; i < count; i++) {
889                 int err;
890
891                 if (likely(i + 1 < count)) {
892                         rte_prefetch0(&vq->desc[desc_indexes[i + 1]]);
893                         rte_prefetch0(&vq->used->ring[(used_idx + 1) &
894                                                       (vq->size - 1)]);
895                 }
896
897                 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
898                 if (unlikely(pkts[i] == NULL)) {
899                         RTE_LOG(ERR, VHOST_DATA,
900                                 "Failed to allocate memory for mbuf.\n");
901                         break;
902                 }
903                 err = copy_desc_to_mbuf(dev, vq, pkts[i], desc_indexes[i],
904                                         mbuf_pool);
905                 if (unlikely(err)) {
906                         rte_pktmbuf_free(pkts[i]);
907                         break;
908                 }
909
910                 used_idx = vq->last_used_idx++ & (vq->size - 1);
911                 vq->used->ring[used_idx].id  = desc_indexes[i];
912                 vq->used->ring[used_idx].len = 0;
913                 vhost_log_used_vring(dev, vq,
914                                 offsetof(struct vring_used, ring[used_idx]),
915                                 sizeof(vq->used->ring[used_idx]));
916         }
917
918         rte_smp_wmb();
919         rte_smp_rmb();
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                         && (vq->callfd >= 0))
927                 eventfd_write(vq->callfd, (eventfd_t)1);
928
929 out:
930         if (unlikely(rarp_mbuf != NULL)) {
931                 /*
932                  * Inject it to the head of "pkts" array, so that switch's mac
933                  * learning table will get updated first.
934                  */
935                 memmove(&pkts[1], pkts, i * sizeof(struct rte_mbuf *));
936                 pkts[0] = rarp_mbuf;
937                 i += 1;
938         }
939
940         return i;
941 }