Imported Upstream version 16.07.2
[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 desc_chain_head;
388         uint32_t desc_chain_len;
389         uint32_t mbuf_offset, mbuf_avail;
390         uint32_t desc_offset, desc_avail;
391         uint32_t cpy_len;
392         uint16_t desc_idx, used_idx;
393
394         if (unlikely(m == NULL))
395                 return 0;
396
397         LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
398                 dev->vid, cur_idx, end_idx);
399
400         desc_addr = gpa_to_vva(dev, buf_vec[vec_idx].buf_addr);
401         if (buf_vec[vec_idx].buf_len < dev->vhost_hlen || !desc_addr)
402                 return 0;
403
404         rte_prefetch0((void *)(uintptr_t)desc_addr);
405
406         virtio_hdr.num_buffers = end_idx - start_idx;
407         LOG_DEBUG(VHOST_DATA, "(%d) RX: num merge buffers %d\n",
408                 dev->vid, virtio_hdr.num_buffers);
409
410         virtio_enqueue_offload(m, &virtio_hdr.hdr);
411         copy_virtio_net_hdr(dev, desc_addr, virtio_hdr);
412         vhost_log_write(dev, buf_vec[vec_idx].buf_addr, dev->vhost_hlen);
413         PRINT_PACKET(dev, (uintptr_t)desc_addr, dev->vhost_hlen, 0);
414
415         desc_avail  = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
416         desc_offset = dev->vhost_hlen;
417         desc_chain_head = buf_vec[vec_idx].desc_idx;
418         desc_chain_len = desc_offset;
419
420         mbuf_avail  = rte_pktmbuf_data_len(m);
421         mbuf_offset = 0;
422         while (mbuf_avail != 0 || m->next != NULL) {
423                 /* done with current desc buf, get the next one */
424                 if (desc_avail == 0) {
425                         desc_idx = buf_vec[vec_idx].desc_idx;
426                         vec_idx++;
427
428                         if (!(vq->desc[desc_idx].flags & VRING_DESC_F_NEXT)) {
429                                 /* Update used ring with desc information */
430                                 used_idx = cur_idx++ & (vq->size - 1);
431                                 vq->used->ring[used_idx].id = desc_chain_head;
432                                 vq->used->ring[used_idx].len = desc_chain_len;
433                                 vhost_log_used_vring(dev, vq,
434                                         offsetof(struct vring_used,
435                                                  ring[used_idx]),
436                                         sizeof(vq->used->ring[used_idx]));
437                                 desc_chain_head = buf_vec[vec_idx].desc_idx;
438                                 desc_chain_len = 0;
439                         }
440
441                         desc_addr = gpa_to_vva(dev, buf_vec[vec_idx].buf_addr);
442                         if (unlikely(!desc_addr))
443                                 return 0;
444
445                         /* Prefetch buffer address. */
446                         rte_prefetch0((void *)(uintptr_t)desc_addr);
447                         desc_offset = 0;
448                         desc_avail  = buf_vec[vec_idx].buf_len;
449                 }
450
451                 /* done with current mbuf, get the next one */
452                 if (mbuf_avail == 0) {
453                         m = m->next;
454
455                         mbuf_offset = 0;
456                         mbuf_avail  = rte_pktmbuf_data_len(m);
457                 }
458
459                 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
460                 rte_memcpy((void *)((uintptr_t)(desc_addr + desc_offset)),
461                         rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
462                         cpy_len);
463                 vhost_log_write(dev, buf_vec[vec_idx].buf_addr + desc_offset,
464                         cpy_len);
465                 PRINT_PACKET(dev, (uintptr_t)(desc_addr + desc_offset),
466                         cpy_len, 0);
467
468                 mbuf_avail  -= cpy_len;
469                 mbuf_offset += cpy_len;
470                 desc_avail  -= cpy_len;
471                 desc_offset += cpy_len;
472                 desc_chain_len += cpy_len;
473         }
474
475         used_idx = cur_idx & (vq->size - 1);
476         vq->used->ring[used_idx].id = desc_chain_head;
477         vq->used->ring[used_idx].len = desc_chain_len;
478         vhost_log_used_vring(dev, vq,
479                 offsetof(struct vring_used, ring[used_idx]),
480                 sizeof(vq->used->ring[used_idx]));
481
482         return end_idx - start_idx;
483 }
484
485 static inline uint32_t __attribute__((always_inline))
486 virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,
487         struct rte_mbuf **pkts, uint32_t count)
488 {
489         struct vhost_virtqueue *vq;
490         uint32_t pkt_idx = 0, nr_used = 0;
491         uint16_t end;
492         struct buf_vector buf_vec[BUF_VECTOR_MAX];
493
494         LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
495         if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->virt_qp_nb))) {
496                 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
497                         dev->vid, __func__, queue_id);
498                 return 0;
499         }
500
501         vq = dev->virtqueue[queue_id];
502         if (unlikely(vq->enabled == 0))
503                 return 0;
504
505         count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
506         if (count == 0)
507                 return 0;
508
509         for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
510                 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
511
512                 if (unlikely(reserve_avail_buf_mergeable(vq, pkt_len,
513                                                          &end, buf_vec) < 0)) {
514                         LOG_DEBUG(VHOST_DATA,
515                                 "(%d) failed to get enough desc from vring\n",
516                                 dev->vid);
517                         break;
518                 }
519
520                 nr_used = copy_mbuf_to_desc_mergeable(dev, vq, end,
521                                                       pkts[pkt_idx], buf_vec);
522                 rte_smp_wmb();
523
524                 *(volatile uint16_t *)&vq->used->idx += nr_used;
525                 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
526                         sizeof(vq->used->idx));
527                 vq->last_used_idx += nr_used;
528         }
529
530         if (likely(pkt_idx)) {
531                 /* flush used->idx update before we read avail->flags. */
532                 rte_mb();
533
534                 /* Kick the guest if necessary. */
535                 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
536                                 && (vq->callfd >= 0))
537                         eventfd_write(vq->callfd, (eventfd_t)1);
538         }
539
540         return pkt_idx;
541 }
542
543 uint16_t
544 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
545         struct rte_mbuf **pkts, uint16_t count)
546 {
547         struct virtio_net *dev = get_device(vid);
548
549         if (!dev)
550                 return 0;
551
552         if (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF))
553                 return virtio_dev_merge_rx(dev, queue_id, pkts, count);
554         else
555                 return virtio_dev_rx(dev, queue_id, pkts, count);
556 }
557
558 static void
559 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
560 {
561         struct ipv4_hdr *ipv4_hdr;
562         struct ipv6_hdr *ipv6_hdr;
563         void *l3_hdr = NULL;
564         struct ether_hdr *eth_hdr;
565         uint16_t ethertype;
566
567         eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
568
569         m->l2_len = sizeof(struct ether_hdr);
570         ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
571
572         if (ethertype == ETHER_TYPE_VLAN) {
573                 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
574
575                 m->l2_len += sizeof(struct vlan_hdr);
576                 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
577         }
578
579         l3_hdr = (char *)eth_hdr + m->l2_len;
580
581         switch (ethertype) {
582         case ETHER_TYPE_IPv4:
583                 ipv4_hdr = (struct ipv4_hdr *)l3_hdr;
584                 *l4_proto = ipv4_hdr->next_proto_id;
585                 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
586                 *l4_hdr = (char *)l3_hdr + m->l3_len;
587                 m->ol_flags |= PKT_TX_IPV4;
588                 break;
589         case ETHER_TYPE_IPv6:
590                 ipv6_hdr = (struct ipv6_hdr *)l3_hdr;
591                 *l4_proto = ipv6_hdr->proto;
592                 m->l3_len = sizeof(struct ipv6_hdr);
593                 *l4_hdr = (char *)l3_hdr + m->l3_len;
594                 m->ol_flags |= PKT_TX_IPV6;
595                 break;
596         default:
597                 m->l3_len = 0;
598                 *l4_proto = 0;
599                 break;
600         }
601 }
602
603 static inline void __attribute__((always_inline))
604 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
605 {
606         uint16_t l4_proto = 0;
607         void *l4_hdr = NULL;
608         struct tcp_hdr *tcp_hdr = NULL;
609
610         parse_ethernet(m, &l4_proto, &l4_hdr);
611         if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
612                 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
613                         switch (hdr->csum_offset) {
614                         case (offsetof(struct tcp_hdr, cksum)):
615                                 if (l4_proto == IPPROTO_TCP)
616                                         m->ol_flags |= PKT_TX_TCP_CKSUM;
617                                 break;
618                         case (offsetof(struct udp_hdr, dgram_cksum)):
619                                 if (l4_proto == IPPROTO_UDP)
620                                         m->ol_flags |= PKT_TX_UDP_CKSUM;
621                                 break;
622                         case (offsetof(struct sctp_hdr, cksum)):
623                                 if (l4_proto == IPPROTO_SCTP)
624                                         m->ol_flags |= PKT_TX_SCTP_CKSUM;
625                                 break;
626                         default:
627                                 break;
628                         }
629                 }
630         }
631
632         if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
633                 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
634                 case VIRTIO_NET_HDR_GSO_TCPV4:
635                 case VIRTIO_NET_HDR_GSO_TCPV6:
636                         tcp_hdr = (struct tcp_hdr *)l4_hdr;
637                         m->ol_flags |= PKT_TX_TCP_SEG;
638                         m->tso_segsz = hdr->gso_size;
639                         m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
640                         break;
641                 default:
642                         RTE_LOG(WARNING, VHOST_DATA,
643                                 "unsupported gso type %u.\n", hdr->gso_type);
644                         break;
645                 }
646         }
647 }
648
649 #define RARP_PKT_SIZE   64
650
651 static int
652 make_rarp_packet(struct rte_mbuf *rarp_mbuf, const struct ether_addr *mac)
653 {
654         struct ether_hdr *eth_hdr;
655         struct arp_hdr  *rarp;
656
657         if (rarp_mbuf->buf_len < 64) {
658                 RTE_LOG(WARNING, VHOST_DATA,
659                         "failed to make RARP; mbuf size too small %u (< %d)\n",
660                         rarp_mbuf->buf_len, RARP_PKT_SIZE);
661                 return -1;
662         }
663
664         /* Ethernet header. */
665         eth_hdr = rte_pktmbuf_mtod_offset(rarp_mbuf, struct ether_hdr *, 0);
666         memset(eth_hdr->d_addr.addr_bytes, 0xff, ETHER_ADDR_LEN);
667         ether_addr_copy(mac, &eth_hdr->s_addr);
668         eth_hdr->ether_type = htons(ETHER_TYPE_RARP);
669
670         /* RARP header. */
671         rarp = (struct arp_hdr *)(eth_hdr + 1);
672         rarp->arp_hrd = htons(ARP_HRD_ETHER);
673         rarp->arp_pro = htons(ETHER_TYPE_IPv4);
674         rarp->arp_hln = ETHER_ADDR_LEN;
675         rarp->arp_pln = 4;
676         rarp->arp_op  = htons(ARP_OP_REVREQUEST);
677
678         ether_addr_copy(mac, &rarp->arp_data.arp_sha);
679         ether_addr_copy(mac, &rarp->arp_data.arp_tha);
680         memset(&rarp->arp_data.arp_sip, 0x00, 4);
681         memset(&rarp->arp_data.arp_tip, 0x00, 4);
682
683         rarp_mbuf->pkt_len  = rarp_mbuf->data_len = RARP_PKT_SIZE;
684
685         return 0;
686 }
687
688 static inline int __attribute__((always_inline))
689 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
690                   struct rte_mbuf *m, uint16_t desc_idx,
691                   struct rte_mempool *mbuf_pool)
692 {
693         struct vring_desc *desc;
694         uint64_t desc_addr;
695         uint32_t desc_avail, desc_offset;
696         uint32_t mbuf_avail, mbuf_offset;
697         uint32_t cpy_len;
698         struct rte_mbuf *cur = m, *prev = m;
699         struct virtio_net_hdr *hdr;
700         /* A counter to avoid desc dead loop chain */
701         uint32_t nr_desc = 1;
702
703         desc = &vq->desc[desc_idx];
704         if (unlikely(desc->len < dev->vhost_hlen))
705                 return -1;
706
707         desc_addr = gpa_to_vva(dev, desc->addr);
708         if (unlikely(!desc_addr))
709                 return -1;
710
711         hdr = (struct virtio_net_hdr *)((uintptr_t)desc_addr);
712         rte_prefetch0(hdr);
713
714         /*
715          * A virtio driver normally uses at least 2 desc buffers
716          * for Tx: the first for storing the header, and others
717          * for storing the data.
718          */
719         if (likely((desc->len == dev->vhost_hlen) &&
720                    (desc->flags & VRING_DESC_F_NEXT) != 0)) {
721                 desc = &vq->desc[desc->next];
722
723                 desc_addr = gpa_to_vva(dev, desc->addr);
724                 if (unlikely(!desc_addr))
725                         return -1;
726
727                 rte_prefetch0((void *)(uintptr_t)desc_addr);
728
729                 desc_offset = 0;
730                 desc_avail  = desc->len;
731                 nr_desc    += 1;
732
733                 PRINT_PACKET(dev, (uintptr_t)desc_addr, desc->len, 0);
734         } else {
735                 desc_avail  = desc->len - dev->vhost_hlen;
736                 desc_offset = dev->vhost_hlen;
737         }
738
739         mbuf_offset = 0;
740         mbuf_avail  = m->buf_len - RTE_PKTMBUF_HEADROOM;
741         while (1) {
742                 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
743                 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *, mbuf_offset),
744                         (void *)((uintptr_t)(desc_addr + desc_offset)),
745                         cpy_len);
746
747                 mbuf_avail  -= cpy_len;
748                 mbuf_offset += cpy_len;
749                 desc_avail  -= cpy_len;
750                 desc_offset += cpy_len;
751
752                 /* This desc reaches to its end, get the next one */
753                 if (desc_avail == 0) {
754                         if ((desc->flags & VRING_DESC_F_NEXT) == 0)
755                                 break;
756
757                         if (unlikely(desc->next >= vq->size ||
758                                      ++nr_desc > vq->size))
759                                 return -1;
760                         desc = &vq->desc[desc->next];
761
762                         desc_addr = gpa_to_vva(dev, desc->addr);
763                         if (unlikely(!desc_addr))
764                                 return -1;
765
766                         rte_prefetch0((void *)(uintptr_t)desc_addr);
767
768                         desc_offset = 0;
769                         desc_avail  = desc->len;
770
771                         PRINT_PACKET(dev, (uintptr_t)desc_addr, desc->len, 0);
772                 }
773
774                 /*
775                  * This mbuf reaches to its end, get a new one
776                  * to hold more data.
777                  */
778                 if (mbuf_avail == 0) {
779                         cur = rte_pktmbuf_alloc(mbuf_pool);
780                         if (unlikely(cur == NULL)) {
781                                 RTE_LOG(ERR, VHOST_DATA, "Failed to "
782                                         "allocate memory for mbuf.\n");
783                                 return -1;
784                         }
785
786                         prev->next = cur;
787                         prev->data_len = mbuf_offset;
788                         m->nb_segs += 1;
789                         m->pkt_len += mbuf_offset;
790                         prev = cur;
791
792                         mbuf_offset = 0;
793                         mbuf_avail  = cur->buf_len - RTE_PKTMBUF_HEADROOM;
794                 }
795         }
796
797         prev->data_len = mbuf_offset;
798         m->pkt_len    += mbuf_offset;
799
800         if (hdr->flags != 0 || hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE)
801                 vhost_dequeue_offload(hdr, m);
802
803         return 0;
804 }
805
806 uint16_t
807 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
808         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
809 {
810         struct virtio_net *dev;
811         struct rte_mbuf *rarp_mbuf = NULL;
812         struct vhost_virtqueue *vq;
813         uint32_t desc_indexes[MAX_PKT_BURST];
814         uint32_t used_idx;
815         uint32_t i = 0;
816         uint16_t free_entries;
817         uint16_t avail_idx;
818
819         dev = get_device(vid);
820         if (!dev)
821                 return 0;
822
823         if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->virt_qp_nb))) {
824                 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
825                         dev->vid, __func__, queue_id);
826                 return 0;
827         }
828
829         vq = dev->virtqueue[queue_id];
830         if (unlikely(vq->enabled == 0))
831                 return 0;
832
833         /*
834          * Construct a RARP broadcast packet, and inject it to the "pkts"
835          * array, to looks like that guest actually send such packet.
836          *
837          * Check user_send_rarp() for more information.
838          */
839         if (unlikely(rte_atomic16_cmpset((volatile uint16_t *)
840                                          &dev->broadcast_rarp.cnt, 1, 0))) {
841                 rarp_mbuf = rte_pktmbuf_alloc(mbuf_pool);
842                 if (rarp_mbuf == NULL) {
843                         RTE_LOG(ERR, VHOST_DATA,
844                                 "Failed to allocate memory for mbuf.\n");
845                         return 0;
846                 }
847
848                 if (make_rarp_packet(rarp_mbuf, &dev->mac)) {
849                         rte_pktmbuf_free(rarp_mbuf);
850                         rarp_mbuf = NULL;
851                 } else {
852                         count -= 1;
853                 }
854         }
855
856         avail_idx =  *((volatile uint16_t *)&vq->avail->idx);
857         free_entries = avail_idx - vq->last_used_idx;
858         if (free_entries == 0)
859                 goto out;
860
861         LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
862
863         /* Prefetch available ring to retrieve head indexes. */
864         used_idx = vq->last_used_idx & (vq->size - 1);
865         rte_prefetch0(&vq->avail->ring[used_idx]);
866         rte_prefetch0(&vq->used->ring[used_idx]);
867
868         count = RTE_MIN(count, MAX_PKT_BURST);
869         count = RTE_MIN(count, free_entries);
870         LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
871                         dev->vid, count);
872
873         /* Retrieve all of the head indexes first to avoid caching issues. */
874         for (i = 0; i < count; i++) {
875                 used_idx = (vq->last_used_idx + i) & (vq->size - 1);
876                 desc_indexes[i] = vq->avail->ring[used_idx];
877
878                 vq->used->ring[used_idx].id  = desc_indexes[i];
879                 vq->used->ring[used_idx].len = 0;
880                 vhost_log_used_vring(dev, vq,
881                                 offsetof(struct vring_used, ring[used_idx]),
882                                 sizeof(vq->used->ring[used_idx]));
883         }
884
885         /* Prefetch descriptor index. */
886         rte_prefetch0(&vq->desc[desc_indexes[0]]);
887         for (i = 0; i < count; i++) {
888                 int err;
889
890                 if (likely(i + 1 < count))
891                         rte_prefetch0(&vq->desc[desc_indexes[i + 1]]);
892
893                 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
894                 if (unlikely(pkts[i] == NULL)) {
895                         RTE_LOG(ERR, VHOST_DATA,
896                                 "Failed to allocate memory for mbuf.\n");
897                         break;
898                 }
899                 err = copy_desc_to_mbuf(dev, vq, pkts[i], desc_indexes[i],
900                                         mbuf_pool);
901                 if (unlikely(err)) {
902                         rte_pktmbuf_free(pkts[i]);
903                         break;
904                 }
905         }
906
907         rte_smp_wmb();
908         rte_smp_rmb();
909         vq->used->idx += i;
910         vq->last_used_idx += i;
911         vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
912                         sizeof(vq->used->idx));
913
914         /* Kick guest if required. */
915         if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
916                         && (vq->callfd >= 0))
917                 eventfd_write(vq->callfd, (eventfd_t)1);
918
919 out:
920         if (unlikely(rarp_mbuf != NULL)) {
921                 /*
922                  * Inject it to the head of "pkts" array, so that switch's mac
923                  * learning table will get updated first.
924                  */
925                 memmove(&pkts[1], pkts, i * sizeof(struct rte_mbuf *));
926                 pkts[0] = rarp_mbuf;
927                 i += 1;
928         }
929
930         return i;
931 }