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