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