Imported Upstream version 16.04
[deb_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, "(%"PRIu64") virtio_dev_rx()\n", dev->device_fh);
268         if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->virt_qp_nb))) {
269                 RTE_LOG(ERR, VHOST_DATA,
270                         "%s (%"PRIu64"): virtqueue idx:%d invalid.\n",
271                         __func__, dev->device_fh, queue_id);
272                 return 0;
273         }
274
275         vq = dev->virtqueue[queue_id];
276         if (unlikely(vq->enabled == 0))
277                 return 0;
278
279         count = reserve_avail_buf(vq, count, &res_start_idx, &res_end_idx);
280         if (count == 0)
281                 return 0;
282
283         LOG_DEBUG(VHOST_DATA,
284                 "(%"PRIu64") res_start_idx %d| res_end_idx Index %d\n",
285                 dev->device_fh, res_start_idx, res_end_idx);
286
287         /* Retrieve all of the desc indexes first to avoid caching issues. */
288         rte_prefetch0(&vq->avail->ring[res_start_idx & (vq->size - 1)]);
289         for (i = 0; i < count; i++) {
290                 desc_indexes[i] = vq->avail->ring[(res_start_idx + i) &
291                                                   (vq->size - 1)];
292         }
293
294         rte_prefetch0(&vq->desc[desc_indexes[0]]);
295         for (i = 0; i < count; i++) {
296                 uint16_t desc_idx = desc_indexes[i];
297                 uint16_t used_idx = (res_start_idx + i) & (vq->size - 1);
298                 uint32_t copied;
299                 int err;
300
301                 err = copy_mbuf_to_desc(dev, vq, pkts[i], desc_idx, &copied);
302
303                 vq->used->ring[used_idx].id = desc_idx;
304                 if (unlikely(err))
305                         vq->used->ring[used_idx].len = vq->vhost_hlen;
306                 else
307                         vq->used->ring[used_idx].len = copied + vq->vhost_hlen;
308                 vhost_log_used_vring(dev, vq,
309                         offsetof(struct vring_used, ring[used_idx]),
310                         sizeof(vq->used->ring[used_idx]));
311
312                 if (i + 1 < count)
313                         rte_prefetch0(&vq->desc[desc_indexes[i+1]]);
314         }
315
316         rte_smp_wmb();
317
318         /* Wait until it's our turn to add our buffer to the used ring. */
319         while (unlikely(vq->last_used_idx != res_start_idx))
320                 rte_pause();
321
322         *(volatile uint16_t *)&vq->used->idx += count;
323         vq->last_used_idx = res_end_idx;
324         vhost_log_used_vring(dev, vq,
325                 offsetof(struct vring_used, idx),
326                 sizeof(vq->used->idx));
327
328         /* flush used->idx update before we read avail->flags. */
329         rte_mb();
330
331         /* Kick the guest if necessary. */
332         if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
333                         && (vq->callfd >= 0))
334                 eventfd_write(vq->callfd, (eventfd_t)1);
335         return count;
336 }
337
338 static inline int
339 fill_vec_buf(struct vhost_virtqueue *vq, uint32_t avail_idx,
340              uint32_t *allocated, uint32_t *vec_idx)
341 {
342         uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
343         uint32_t vec_id = *vec_idx;
344         uint32_t len    = *allocated;
345
346         while (1) {
347                 if (unlikely(vec_id >= BUF_VECTOR_MAX || idx >= vq->size))
348                         return -1;
349
350                 len += vq->desc[idx].len;
351                 vq->buf_vec[vec_id].buf_addr = vq->desc[idx].addr;
352                 vq->buf_vec[vec_id].buf_len  = vq->desc[idx].len;
353                 vq->buf_vec[vec_id].desc_idx = idx;
354                 vec_id++;
355
356                 if ((vq->desc[idx].flags & VRING_DESC_F_NEXT) == 0)
357                         break;
358
359                 idx = vq->desc[idx].next;
360         }
361
362         *allocated = len;
363         *vec_idx   = vec_id;
364
365         return 0;
366 }
367
368 /*
369  * As many data cores may want to access available buffers concurrently,
370  * they need to be reserved.
371  *
372  * Returns -1 on fail, 0 on success
373  */
374 static inline int
375 reserve_avail_buf_mergeable(struct vhost_virtqueue *vq, uint32_t size,
376                             uint16_t *start, uint16_t *end)
377 {
378         uint16_t res_start_idx;
379         uint16_t res_cur_idx;
380         uint16_t avail_idx;
381         uint32_t allocated;
382         uint32_t vec_idx;
383         uint16_t tries;
384
385 again:
386         res_start_idx = vq->last_used_idx_res;
387         res_cur_idx  = res_start_idx;
388
389         allocated = 0;
390         vec_idx   = 0;
391         tries     = 0;
392         while (1) {
393                 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
394                 if (unlikely(res_cur_idx == avail_idx))
395                         return -1;
396
397                 if (unlikely(fill_vec_buf(vq, res_cur_idx, &allocated,
398                                           &vec_idx) < 0))
399                         return -1;
400
401                 res_cur_idx++;
402                 tries++;
403
404                 if (allocated >= size)
405                         break;
406
407                 /*
408                  * if we tried all available ring items, and still
409                  * can't get enough buf, it means something abnormal
410                  * happened.
411                  */
412                 if (unlikely(tries >= vq->size))
413                         return -1;
414         }
415
416         /*
417          * update vq->last_used_idx_res atomically.
418          * retry again if failed.
419          */
420         if (rte_atomic16_cmpset(&vq->last_used_idx_res,
421                                 res_start_idx, res_cur_idx) == 0)
422                 goto again;
423
424         *start = res_start_idx;
425         *end   = res_cur_idx;
426         return 0;
427 }
428
429 static inline uint32_t __attribute__((always_inline))
430 copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,
431                             uint16_t res_start_idx, uint16_t res_end_idx,
432                             struct rte_mbuf *m)
433 {
434         struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0, 0, 0, 0, 0, 0}, 0};
435         uint32_t vec_idx = 0;
436         uint16_t cur_idx = res_start_idx;
437         uint64_t desc_addr;
438         uint32_t mbuf_offset, mbuf_avail;
439         uint32_t desc_offset, desc_avail;
440         uint32_t cpy_len;
441         uint16_t desc_idx, used_idx;
442
443         if (unlikely(m == NULL))
444                 return 0;
445
446         LOG_DEBUG(VHOST_DATA,
447                 "(%"PRIu64") Current Index %d| End Index %d\n",
448                 dev->device_fh, cur_idx, res_end_idx);
449
450         if (vq->buf_vec[vec_idx].buf_len < vq->vhost_hlen)
451                 return -1;
452
453         desc_addr = gpa_to_vva(dev, vq->buf_vec[vec_idx].buf_addr);
454         rte_prefetch0((void *)(uintptr_t)desc_addr);
455
456         virtio_hdr.num_buffers = res_end_idx - res_start_idx;
457         LOG_DEBUG(VHOST_DATA, "(%"PRIu64") RX: Num merge buffers %d\n",
458                 dev->device_fh, virtio_hdr.num_buffers);
459
460         virtio_enqueue_offload(m, &virtio_hdr.hdr);
461         copy_virtio_net_hdr(vq, desc_addr, virtio_hdr);
462         vhost_log_write(dev, vq->buf_vec[vec_idx].buf_addr, vq->vhost_hlen);
463         PRINT_PACKET(dev, (uintptr_t)desc_addr, vq->vhost_hlen, 0);
464
465         desc_avail  = vq->buf_vec[vec_idx].buf_len - vq->vhost_hlen;
466         desc_offset = vq->vhost_hlen;
467
468         mbuf_avail  = rte_pktmbuf_data_len(m);
469         mbuf_offset = 0;
470         while (mbuf_avail != 0 || m->next != NULL) {
471                 /* done with current desc buf, get the next one */
472                 if (desc_avail == 0) {
473                         desc_idx = vq->buf_vec[vec_idx].desc_idx;
474
475                         if (!(vq->desc[desc_idx].flags & VRING_DESC_F_NEXT)) {
476                                 /* Update used ring with desc information */
477                                 used_idx = cur_idx++ & (vq->size - 1);
478                                 vq->used->ring[used_idx].id  = desc_idx;
479                                 vq->used->ring[used_idx].len = desc_offset;
480                                 vhost_log_used_vring(dev, vq,
481                                         offsetof(struct vring_used,
482                                                  ring[used_idx]),
483                                         sizeof(vq->used->ring[used_idx]));
484                         }
485
486                         vec_idx++;
487                         desc_addr = gpa_to_vva(dev, vq->buf_vec[vec_idx].buf_addr);
488
489                         /* Prefetch buffer address. */
490                         rte_prefetch0((void *)(uintptr_t)desc_addr);
491                         desc_offset = 0;
492                         desc_avail  = vq->buf_vec[vec_idx].buf_len;
493                 }
494
495                 /* done with current mbuf, get the next one */
496                 if (mbuf_avail == 0) {
497                         m = m->next;
498
499                         mbuf_offset = 0;
500                         mbuf_avail  = rte_pktmbuf_data_len(m);
501                 }
502
503                 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
504                 rte_memcpy((void *)((uintptr_t)(desc_addr + desc_offset)),
505                         rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
506                         cpy_len);
507                 vhost_log_write(dev, vq->buf_vec[vec_idx].buf_addr + desc_offset,
508                         cpy_len);
509                 PRINT_PACKET(dev, (uintptr_t)(desc_addr + desc_offset),
510                         cpy_len, 0);
511
512                 mbuf_avail  -= cpy_len;
513                 mbuf_offset += cpy_len;
514                 desc_avail  -= cpy_len;
515                 desc_offset += cpy_len;
516         }
517
518         used_idx = cur_idx & (vq->size - 1);
519         vq->used->ring[used_idx].id = vq->buf_vec[vec_idx].desc_idx;
520         vq->used->ring[used_idx].len = desc_offset;
521         vhost_log_used_vring(dev, vq,
522                 offsetof(struct vring_used, ring[used_idx]),
523                 sizeof(vq->used->ring[used_idx]));
524
525         return res_end_idx - res_start_idx;
526 }
527
528 static inline uint32_t __attribute__((always_inline))
529 virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,
530         struct rte_mbuf **pkts, uint32_t count)
531 {
532         struct vhost_virtqueue *vq;
533         uint32_t pkt_idx = 0, nr_used = 0;
534         uint16_t start, end;
535
536         LOG_DEBUG(VHOST_DATA, "(%"PRIu64") virtio_dev_merge_rx()\n",
537                 dev->device_fh);
538         if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->virt_qp_nb))) {
539                 RTE_LOG(ERR, VHOST_DATA,
540                         "%s (%"PRIu64"): virtqueue idx:%d invalid.\n",
541                         __func__, dev->device_fh, queue_id);
542                 return 0;
543         }
544
545         vq = dev->virtqueue[queue_id];
546         if (unlikely(vq->enabled == 0))
547                 return 0;
548
549         count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
550         if (count == 0)
551                 return 0;
552
553         for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
554                 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + vq->vhost_hlen;
555
556                 if (unlikely(reserve_avail_buf_mergeable(vq, pkt_len,
557                                                          &start, &end) < 0)) {
558                         LOG_DEBUG(VHOST_DATA,
559                                 "(%" PRIu64 ") Failed to get enough desc from vring\n",
560                                 dev->device_fh);
561                         break;
562                 }
563
564                 nr_used = copy_mbuf_to_desc_mergeable(dev, vq, start, end,
565                                                       pkts[pkt_idx]);
566                 rte_smp_wmb();
567
568                 /*
569                  * Wait until it's our turn to add our buffer
570                  * to the used ring.
571                  */
572                 while (unlikely(vq->last_used_idx != start))
573                         rte_pause();
574
575                 *(volatile uint16_t *)&vq->used->idx += nr_used;
576                 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
577                         sizeof(vq->used->idx));
578                 vq->last_used_idx = end;
579         }
580
581         if (likely(pkt_idx)) {
582                 /* flush used->idx update before we read avail->flags. */
583                 rte_mb();
584
585                 /* Kick the guest if necessary. */
586                 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
587                                 && (vq->callfd >= 0))
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         /* A counter to avoid desc dead loop chain */
747         uint32_t nr_desc = 1;
748
749         desc = &vq->desc[desc_idx];
750         if (unlikely(desc->len < vq->vhost_hlen))
751                 return -1;
752
753         desc_addr = gpa_to_vva(dev, desc->addr);
754         rte_prefetch0((void *)(uintptr_t)desc_addr);
755
756         /* Retrieve virtio net header */
757         hdr = (struct virtio_net_hdr *)((uintptr_t)desc_addr);
758         desc_avail  = desc->len - vq->vhost_hlen;
759         desc_offset = vq->vhost_hlen;
760
761         mbuf_offset = 0;
762         mbuf_avail  = m->buf_len - RTE_PKTMBUF_HEADROOM;
763         while (desc_avail != 0 || (desc->flags & VRING_DESC_F_NEXT) != 0) {
764                 /* This desc reaches to its end, get the next one */
765                 if (desc_avail == 0) {
766                         if (unlikely(desc->next >= vq->size ||
767                                      ++nr_desc >= vq->size))
768                                 return -1;
769                         desc = &vq->desc[desc->next];
770
771                         desc_addr = gpa_to_vva(dev, desc->addr);
772                         rte_prefetch0((void *)(uintptr_t)desc_addr);
773
774                         desc_offset = 0;
775                         desc_avail  = desc->len;
776
777                         PRINT_PACKET(dev, (uintptr_t)desc_addr, desc->len, 0);
778                 }
779
780                 /*
781                  * This mbuf reaches to its end, get a new one
782                  * to hold more data.
783                  */
784                 if (mbuf_avail == 0) {
785                         cur = rte_pktmbuf_alloc(mbuf_pool);
786                         if (unlikely(cur == NULL)) {
787                                 RTE_LOG(ERR, VHOST_DATA, "Failed to "
788                                         "allocate memory for mbuf.\n");
789                                 return -1;
790                         }
791
792                         prev->next = cur;
793                         prev->data_len = mbuf_offset;
794                         m->nb_segs += 1;
795                         m->pkt_len += mbuf_offset;
796                         prev = cur;
797
798                         mbuf_offset = 0;
799                         mbuf_avail  = cur->buf_len - RTE_PKTMBUF_HEADROOM;
800                 }
801
802                 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
803                 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *, mbuf_offset),
804                         (void *)((uintptr_t)(desc_addr + desc_offset)),
805                         cpy_len);
806
807                 mbuf_avail  -= cpy_len;
808                 mbuf_offset += cpy_len;
809                 desc_avail  -= cpy_len;
810                 desc_offset += cpy_len;
811         }
812
813         prev->data_len = mbuf_offset;
814         m->pkt_len    += mbuf_offset;
815
816         if (hdr->flags != 0 || hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE)
817                 vhost_dequeue_offload(hdr, m);
818
819         return 0;
820 }
821
822 uint16_t
823 rte_vhost_dequeue_burst(struct virtio_net *dev, uint16_t queue_id,
824         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
825 {
826         struct rte_mbuf *rarp_mbuf = NULL;
827         struct vhost_virtqueue *vq;
828         uint32_t desc_indexes[MAX_PKT_BURST];
829         uint32_t used_idx;
830         uint32_t i = 0;
831         uint16_t free_entries;
832         uint16_t avail_idx;
833
834         if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->virt_qp_nb))) {
835                 RTE_LOG(ERR, VHOST_DATA,
836                         "%s (%"PRIu64"): virtqueue idx:%d invalid.\n",
837                         __func__, dev->device_fh, queue_id);
838                 return 0;
839         }
840
841         vq = dev->virtqueue[queue_id];
842         if (unlikely(vq->enabled == 0))
843                 return 0;
844
845         /*
846          * Construct a RARP broadcast packet, and inject it to the "pkts"
847          * array, to looks like that guest actually send such packet.
848          *
849          * Check user_send_rarp() for more information.
850          */
851         if (unlikely(rte_atomic16_cmpset((volatile uint16_t *)
852                                          &dev->broadcast_rarp.cnt, 1, 0))) {
853                 rarp_mbuf = rte_pktmbuf_alloc(mbuf_pool);
854                 if (rarp_mbuf == NULL) {
855                         RTE_LOG(ERR, VHOST_DATA,
856                                 "Failed to allocate memory for mbuf.\n");
857                         return 0;
858                 }
859
860                 if (make_rarp_packet(rarp_mbuf, &dev->mac)) {
861                         rte_pktmbuf_free(rarp_mbuf);
862                         rarp_mbuf = NULL;
863                 } else {
864                         count -= 1;
865                 }
866         }
867
868         avail_idx =  *((volatile uint16_t *)&vq->avail->idx);
869         free_entries = avail_idx - vq->last_used_idx;
870         if (free_entries == 0)
871                 goto out;
872
873         LOG_DEBUG(VHOST_DATA, "%s (%"PRIu64")\n", __func__, dev->device_fh);
874
875         /* Prefetch available ring to retrieve head indexes. */
876         used_idx = vq->last_used_idx & (vq->size - 1);
877         rte_prefetch0(&vq->avail->ring[used_idx]);
878
879         count = RTE_MIN(count, MAX_PKT_BURST);
880         count = RTE_MIN(count, free_entries);
881         LOG_DEBUG(VHOST_DATA, "(%"PRIu64") about to dequeue %u buffers\n",
882                         dev->device_fh, count);
883
884         /* Retrieve all of the head indexes first to avoid caching issues. */
885         for (i = 0; i < count; i++) {
886                 desc_indexes[i] = vq->avail->ring[(vq->last_used_idx + i) &
887                                         (vq->size - 1)];
888         }
889
890         /* Prefetch descriptor index. */
891         rte_prefetch0(&vq->desc[desc_indexes[0]]);
892         rte_prefetch0(&vq->used->ring[vq->last_used_idx & (vq->size - 1)]);
893
894         for (i = 0; i < count; i++) {
895                 int err;
896
897                 if (likely(i + 1 < count)) {
898                         rte_prefetch0(&vq->desc[desc_indexes[i + 1]]);
899                         rte_prefetch0(&vq->used->ring[(used_idx + 1) &
900                                                       (vq->size - 1)]);
901                 }
902
903                 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
904                 if (unlikely(pkts[i] == NULL)) {
905                         RTE_LOG(ERR, VHOST_DATA,
906                                 "Failed to allocate memory for mbuf.\n");
907                         break;
908                 }
909                 err = copy_desc_to_mbuf(dev, vq, pkts[i], desc_indexes[i],
910                                         mbuf_pool);
911                 if (unlikely(err)) {
912                         rte_pktmbuf_free(pkts[i]);
913                         break;
914                 }
915
916                 used_idx = vq->last_used_idx++ & (vq->size - 1);
917                 vq->used->ring[used_idx].id  = desc_indexes[i];
918                 vq->used->ring[used_idx].len = 0;
919                 vhost_log_used_vring(dev, vq,
920                                 offsetof(struct vring_used, ring[used_idx]),
921                                 sizeof(vq->used->ring[used_idx]));
922         }
923
924         rte_smp_wmb();
925         rte_smp_rmb();
926         vq->used->idx += i;
927         vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
928                         sizeof(vq->used->idx));
929
930         /* Kick guest if required. */
931         if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
932                         && (vq->callfd >= 0))
933                 eventfd_write(vq->callfd, (eventfd_t)1);
934
935 out:
936         if (unlikely(rarp_mbuf != NULL)) {
937                 /*
938                  * Inject it to the head of "pkts" array, so that switch's mac
939                  * learning table will get updated first.
940                  */
941                 memmove(&pkts[1], pkts, i * sizeof(struct rte_mbuf *));
942                 pkts[0] = rarp_mbuf;
943                 i += 1;
944         }
945
946         return i;
947 }