vhost: broadcast RARP by injecting in receiving mbuf array
[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         memset(net_hdr, 0, sizeof(struct virtio_net_hdr));
98
99         if (m_buf->ol_flags & PKT_TX_L4_MASK) {
100                 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
101                 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
102
103                 switch (m_buf->ol_flags & PKT_TX_L4_MASK) {
104                 case PKT_TX_TCP_CKSUM:
105                         net_hdr->csum_offset = (offsetof(struct tcp_hdr,
106                                                 cksum));
107                         break;
108                 case PKT_TX_UDP_CKSUM:
109                         net_hdr->csum_offset = (offsetof(struct udp_hdr,
110                                                 dgram_cksum));
111                         break;
112                 case PKT_TX_SCTP_CKSUM:
113                         net_hdr->csum_offset = (offsetof(struct sctp_hdr,
114                                                 cksum));
115                         break;
116                 }
117         }
118
119         if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
120                 if (m_buf->ol_flags & PKT_TX_IPV4)
121                         net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
122                 else
123                         net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
124                 net_hdr->gso_size = m_buf->tso_segsz;
125                 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
126                                         + m_buf->l4_len;
127         }
128
129         return;
130 }
131
132 /**
133  * This function adds buffers to the virtio devices RX virtqueue. Buffers can
134  * be received from the physical port or from another virtio device. A packet
135  * count is returned to indicate the number of packets that are succesfully
136  * added to the RX queue. This function works when the mbuf is scattered, but
137  * it doesn't support the mergeable feature.
138  */
139 static inline uint32_t __attribute__((always_inline))
140 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
141         struct rte_mbuf **pkts, uint32_t count)
142 {
143         struct vhost_virtqueue *vq;
144         struct vring_desc *desc, *hdr_desc;
145         struct rte_mbuf *buff, *first_buff;
146         /* The virtio_hdr is initialised to 0. */
147         struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0, 0, 0, 0, 0, 0}, 0};
148         uint64_t buff_addr = 0;
149         uint64_t buff_hdr_addr = 0;
150         uint32_t head[MAX_PKT_BURST];
151         uint32_t head_idx, packet_success = 0;
152         uint16_t avail_idx, res_cur_idx;
153         uint16_t res_base_idx, res_end_idx;
154         uint16_t free_entries;
155         uint8_t success = 0;
156
157         LOG_DEBUG(VHOST_DATA, "(%"PRIu64") virtio_dev_rx()\n", dev->device_fh);
158         if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->virt_qp_nb))) {
159                 RTE_LOG(ERR, VHOST_DATA,
160                         "%s (%"PRIu64"): virtqueue idx:%d invalid.\n",
161                         __func__, dev->device_fh, queue_id);
162                 return 0;
163         }
164
165         vq = dev->virtqueue[queue_id];
166         if (unlikely(vq->enabled == 0))
167                 return 0;
168
169         count = (count > MAX_PKT_BURST) ? MAX_PKT_BURST : count;
170
171         /*
172          * As many data cores may want access to available buffers,
173          * they need to be reserved.
174          */
175         do {
176                 res_base_idx = vq->last_used_idx_res;
177                 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
178
179                 free_entries = (avail_idx - res_base_idx);
180                 /*check that we have enough buffers*/
181                 if (unlikely(count > free_entries))
182                         count = free_entries;
183
184                 if (count == 0)
185                         return 0;
186
187                 res_end_idx = res_base_idx + count;
188                 /* vq->last_used_idx_res is atomically updated. */
189                 /* TODO: Allow to disable cmpset if no concurrency in application. */
190                 success = rte_atomic16_cmpset(&vq->last_used_idx_res,
191                                 res_base_idx, res_end_idx);
192         } while (unlikely(success == 0));
193         res_cur_idx = res_base_idx;
194         LOG_DEBUG(VHOST_DATA, "(%"PRIu64") Current Index %d| End Index %d\n",
195                         dev->device_fh, res_cur_idx, res_end_idx);
196
197         /* Prefetch available ring to retrieve indexes. */
198         rte_prefetch0(&vq->avail->ring[res_cur_idx & (vq->size - 1)]);
199
200         /* Retrieve all of the head indexes first to avoid caching issues. */
201         for (head_idx = 0; head_idx < count; head_idx++)
202                 head[head_idx] = vq->avail->ring[(res_cur_idx + head_idx) &
203                                         (vq->size - 1)];
204
205         /*Prefetch descriptor index. */
206         rte_prefetch0(&vq->desc[head[packet_success]]);
207
208         while (res_cur_idx != res_end_idx) {
209                 uint32_t offset = 0, vb_offset = 0;
210                 uint32_t pkt_len, len_to_cpy, data_len, total_copied = 0;
211                 uint8_t hdr = 0, uncompleted_pkt = 0;
212                 uint16_t idx;
213
214                 /* Get descriptor from available ring */
215                 desc = &vq->desc[head[packet_success]];
216
217                 buff = pkts[packet_success];
218                 first_buff = buff;
219
220                 /* Convert from gpa to vva (guest physical addr -> vhost virtual addr) */
221                 buff_addr = gpa_to_vva(dev, desc->addr);
222                 /* Prefetch buffer address. */
223                 rte_prefetch0((void *)(uintptr_t)buff_addr);
224
225                 /* Copy virtio_hdr to packet and increment buffer address */
226                 buff_hdr_addr = buff_addr;
227                 hdr_desc = desc;
228
229                 /*
230                  * If the descriptors are chained the header and data are
231                  * placed in separate buffers.
232                  */
233                 if ((desc->flags & VRING_DESC_F_NEXT) &&
234                         (desc->len == vq->vhost_hlen)) {
235                         desc = &vq->desc[desc->next];
236                         /* Buffer address translation. */
237                         buff_addr = gpa_to_vva(dev, desc->addr);
238                 } else {
239                         vb_offset += vq->vhost_hlen;
240                         hdr = 1;
241                 }
242
243                 pkt_len = rte_pktmbuf_pkt_len(buff);
244                 data_len = rte_pktmbuf_data_len(buff);
245                 len_to_cpy = RTE_MIN(data_len,
246                         hdr ? desc->len - vq->vhost_hlen : desc->len);
247                 while (total_copied < pkt_len) {
248                         /* Copy mbuf data to buffer */
249                         rte_memcpy((void *)(uintptr_t)(buff_addr + vb_offset),
250                                 rte_pktmbuf_mtod_offset(buff, const void *, offset),
251                                 len_to_cpy);
252                         vhost_log_write(dev, desc->addr + vb_offset, len_to_cpy);
253                         PRINT_PACKET(dev, (uintptr_t)(buff_addr + vb_offset),
254                                 len_to_cpy, 0);
255
256                         offset += len_to_cpy;
257                         vb_offset += len_to_cpy;
258                         total_copied += len_to_cpy;
259
260                         /* The whole packet completes */
261                         if (total_copied == pkt_len)
262                                 break;
263
264                         /* The current segment completes */
265                         if (offset == data_len) {
266                                 buff = buff->next;
267                                 offset = 0;
268                                 data_len = rte_pktmbuf_data_len(buff);
269                         }
270
271                         /* The current vring descriptor done */
272                         if (vb_offset == desc->len) {
273                                 if (desc->flags & VRING_DESC_F_NEXT) {
274                                         desc = &vq->desc[desc->next];
275                                         buff_addr = gpa_to_vva(dev, desc->addr);
276                                         vb_offset = 0;
277                                 } else {
278                                         /* Room in vring buffer is not enough */
279                                         uncompleted_pkt = 1;
280                                         break;
281                                 }
282                         }
283                         len_to_cpy = RTE_MIN(data_len - offset, desc->len - vb_offset);
284                 }
285
286                 /* Update used ring with desc information */
287                 idx = res_cur_idx & (vq->size - 1);
288                 vq->used->ring[idx].id = head[packet_success];
289
290                 /* Drop the packet if it is uncompleted */
291                 if (unlikely(uncompleted_pkt == 1))
292                         vq->used->ring[idx].len = vq->vhost_hlen;
293                 else
294                         vq->used->ring[idx].len = pkt_len + vq->vhost_hlen;
295
296                 vhost_log_used_vring(dev, vq,
297                         offsetof(struct vring_used, ring[idx]),
298                         sizeof(vq->used->ring[idx]));
299
300                 res_cur_idx++;
301                 packet_success++;
302
303                 if (unlikely(uncompleted_pkt == 1))
304                         continue;
305
306                 virtio_enqueue_offload(first_buff, &virtio_hdr.hdr);
307
308                 rte_memcpy((void *)(uintptr_t)buff_hdr_addr,
309                         (const void *)&virtio_hdr, vq->vhost_hlen);
310                 vhost_log_write(dev, hdr_desc->addr, vq->vhost_hlen);
311
312                 PRINT_PACKET(dev, (uintptr_t)buff_hdr_addr, vq->vhost_hlen, 1);
313
314                 if (res_cur_idx < res_end_idx) {
315                         /* Prefetch descriptor index. */
316                         rte_prefetch0(&vq->desc[head[packet_success]]);
317                 }
318         }
319
320         rte_compiler_barrier();
321
322         /* Wait until it's our turn to add our buffer to the used ring. */
323         while (unlikely(vq->last_used_idx != res_base_idx))
324                 rte_pause();
325
326         *(volatile uint16_t *)&vq->used->idx += count;
327         vq->last_used_idx = res_end_idx;
328         vhost_log_used_vring(dev, vq,
329                 offsetof(struct vring_used, idx),
330                 sizeof(vq->used->idx));
331
332         /* flush used->idx update before we read avail->flags. */
333         rte_mb();
334
335         /* Kick the guest if necessary. */
336         if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
337                 eventfd_write(vq->callfd, (eventfd_t)1);
338         return count;
339 }
340
341 static inline uint32_t __attribute__((always_inline))
342 copy_from_mbuf_to_vring(struct virtio_net *dev, uint32_t queue_id,
343                         uint16_t res_base_idx, uint16_t res_end_idx,
344                         struct rte_mbuf *pkt)
345 {
346         uint32_t vec_idx = 0;
347         uint32_t entry_success = 0;
348         struct vhost_virtqueue *vq;
349         /* The virtio_hdr is initialised to 0. */
350         struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {
351                 {0, 0, 0, 0, 0, 0}, 0};
352         uint16_t cur_idx = res_base_idx;
353         uint64_t vb_addr = 0;
354         uint64_t vb_hdr_addr = 0;
355         uint32_t seg_offset = 0;
356         uint32_t vb_offset = 0;
357         uint32_t seg_avail;
358         uint32_t vb_avail;
359         uint32_t cpy_len, entry_len;
360         uint16_t idx;
361
362         if (pkt == NULL)
363                 return 0;
364
365         LOG_DEBUG(VHOST_DATA, "(%"PRIu64") Current Index %d| "
366                 "End Index %d\n",
367                 dev->device_fh, cur_idx, res_end_idx);
368
369         /*
370          * Convert from gpa to vva
371          * (guest physical addr -> vhost virtual addr)
372          */
373         vq = dev->virtqueue[queue_id];
374
375         vb_addr = gpa_to_vva(dev, vq->buf_vec[vec_idx].buf_addr);
376         vb_hdr_addr = vb_addr;
377
378         /* Prefetch buffer address. */
379         rte_prefetch0((void *)(uintptr_t)vb_addr);
380
381         virtio_hdr.num_buffers = res_end_idx - res_base_idx;
382
383         LOG_DEBUG(VHOST_DATA, "(%"PRIu64") RX: Num merge buffers %d\n",
384                 dev->device_fh, virtio_hdr.num_buffers);
385
386         virtio_enqueue_offload(pkt, &virtio_hdr.hdr);
387
388         rte_memcpy((void *)(uintptr_t)vb_hdr_addr,
389                 (const void *)&virtio_hdr, vq->vhost_hlen);
390         vhost_log_write(dev, vq->buf_vec[vec_idx].buf_addr, vq->vhost_hlen);
391
392         PRINT_PACKET(dev, (uintptr_t)vb_hdr_addr, vq->vhost_hlen, 1);
393
394         seg_avail = rte_pktmbuf_data_len(pkt);
395         vb_offset = vq->vhost_hlen;
396         vb_avail = vq->buf_vec[vec_idx].buf_len - vq->vhost_hlen;
397
398         entry_len = vq->vhost_hlen;
399
400         if (vb_avail == 0) {
401                 uint32_t desc_idx = vq->buf_vec[vec_idx].desc_idx;
402
403                 if ((vq->desc[desc_idx].flags & VRING_DESC_F_NEXT) == 0) {
404                         idx = cur_idx & (vq->size - 1);
405
406                         /* Update used ring with desc information */
407                         vq->used->ring[idx].id = vq->buf_vec[vec_idx].desc_idx;
408                         vq->used->ring[idx].len = entry_len;
409
410                         vhost_log_used_vring(dev, vq,
411                                         offsetof(struct vring_used, ring[idx]),
412                                         sizeof(vq->used->ring[idx]));
413
414                         entry_len = 0;
415                         cur_idx++;
416                         entry_success++;
417                 }
418
419                 vec_idx++;
420                 vb_addr = gpa_to_vva(dev, vq->buf_vec[vec_idx].buf_addr);
421
422                 /* Prefetch buffer address. */
423                 rte_prefetch0((void *)(uintptr_t)vb_addr);
424                 vb_offset = 0;
425                 vb_avail = vq->buf_vec[vec_idx].buf_len;
426         }
427
428         cpy_len = RTE_MIN(vb_avail, seg_avail);
429
430         while (cpy_len > 0) {
431                 /* Copy mbuf data to vring buffer */
432                 rte_memcpy((void *)(uintptr_t)(vb_addr + vb_offset),
433                         rte_pktmbuf_mtod_offset(pkt, const void *, seg_offset),
434                         cpy_len);
435                 vhost_log_write(dev, vq->buf_vec[vec_idx].buf_addr + vb_offset,
436                         cpy_len);
437
438                 PRINT_PACKET(dev,
439                         (uintptr_t)(vb_addr + vb_offset),
440                         cpy_len, 0);
441
442                 seg_offset += cpy_len;
443                 vb_offset += cpy_len;
444                 seg_avail -= cpy_len;
445                 vb_avail -= cpy_len;
446                 entry_len += cpy_len;
447
448                 if (seg_avail != 0) {
449                         /*
450                          * The virtio buffer in this vring
451                          * entry reach to its end.
452                          * But the segment doesn't complete.
453                          */
454                         if ((vq->desc[vq->buf_vec[vec_idx].desc_idx].flags &
455                                 VRING_DESC_F_NEXT) == 0) {
456                                 /* Update used ring with desc information */
457                                 idx = cur_idx & (vq->size - 1);
458                                 vq->used->ring[idx].id
459                                         = vq->buf_vec[vec_idx].desc_idx;
460                                 vq->used->ring[idx].len = entry_len;
461                                 vhost_log_used_vring(dev, vq,
462                                         offsetof(struct vring_used, ring[idx]),
463                                         sizeof(vq->used->ring[idx]));
464                                 entry_len = 0;
465                                 cur_idx++;
466                                 entry_success++;
467                         }
468
469                         vec_idx++;
470                         vb_addr = gpa_to_vva(dev,
471                                 vq->buf_vec[vec_idx].buf_addr);
472                         vb_offset = 0;
473                         vb_avail = vq->buf_vec[vec_idx].buf_len;
474                         cpy_len = RTE_MIN(vb_avail, seg_avail);
475                 } else {
476                         /*
477                          * This current segment complete, need continue to
478                          * check if the whole packet complete or not.
479                          */
480                         pkt = pkt->next;
481                         if (pkt != NULL) {
482                                 /*
483                                  * There are more segments.
484                                  */
485                                 if (vb_avail == 0) {
486                                         /*
487                                          * This current buffer from vring is
488                                          * used up, need fetch next buffer
489                                          * from buf_vec.
490                                          */
491                                         uint32_t desc_idx =
492                                                 vq->buf_vec[vec_idx].desc_idx;
493
494                                         if ((vq->desc[desc_idx].flags &
495                                                 VRING_DESC_F_NEXT) == 0) {
496                                                 idx = cur_idx & (vq->size - 1);
497                                                 /*
498                                                  * Update used ring with the
499                                                  * descriptor information
500                                                  */
501                                                 vq->used->ring[idx].id
502                                                         = desc_idx;
503                                                 vq->used->ring[idx].len
504                                                         = entry_len;
505                                                 vhost_log_used_vring(dev, vq,
506                                                         offsetof(struct vring_used, ring[idx]),
507                                                         sizeof(vq->used->ring[idx]));
508                                                 entry_success++;
509                                                 entry_len = 0;
510                                                 cur_idx++;
511                                         }
512
513                                         /* Get next buffer from buf_vec. */
514                                         vec_idx++;
515                                         vb_addr = gpa_to_vva(dev,
516                                                 vq->buf_vec[vec_idx].buf_addr);
517                                         vb_avail =
518                                                 vq->buf_vec[vec_idx].buf_len;
519                                         vb_offset = 0;
520                                 }
521
522                                 seg_offset = 0;
523                                 seg_avail = rte_pktmbuf_data_len(pkt);
524                                 cpy_len = RTE_MIN(vb_avail, seg_avail);
525                         } else {
526                                 /*
527                                  * This whole packet completes.
528                                  */
529                                 /* Update used ring with desc information */
530                                 idx = cur_idx & (vq->size - 1);
531                                 vq->used->ring[idx].id
532                                         = vq->buf_vec[vec_idx].desc_idx;
533                                 vq->used->ring[idx].len = entry_len;
534                                 vhost_log_used_vring(dev, vq,
535                                         offsetof(struct vring_used, ring[idx]),
536                                         sizeof(vq->used->ring[idx]));
537                                 entry_success++;
538                                 break;
539                         }
540                 }
541         }
542
543         return entry_success;
544 }
545
546 static inline void __attribute__((always_inline))
547 update_secure_len(struct vhost_virtqueue *vq, uint32_t id,
548         uint32_t *secure_len, uint32_t *vec_idx)
549 {
550         uint16_t wrapped_idx = id & (vq->size - 1);
551         uint32_t idx = vq->avail->ring[wrapped_idx];
552         uint8_t next_desc;
553         uint32_t len = *secure_len;
554         uint32_t vec_id = *vec_idx;
555
556         do {
557                 next_desc = 0;
558                 len += vq->desc[idx].len;
559                 vq->buf_vec[vec_id].buf_addr = vq->desc[idx].addr;
560                 vq->buf_vec[vec_id].buf_len = vq->desc[idx].len;
561                 vq->buf_vec[vec_id].desc_idx = idx;
562                 vec_id++;
563
564                 if (vq->desc[idx].flags & VRING_DESC_F_NEXT) {
565                         idx = vq->desc[idx].next;
566                         next_desc = 1;
567                 }
568         } while (next_desc);
569
570         *secure_len = len;
571         *vec_idx = vec_id;
572 }
573
574 /*
575  * This function works for mergeable RX.
576  */
577 static inline uint32_t __attribute__((always_inline))
578 virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,
579         struct rte_mbuf **pkts, uint32_t count)
580 {
581         struct vhost_virtqueue *vq;
582         uint32_t pkt_idx = 0, entry_success = 0;
583         uint16_t avail_idx;
584         uint16_t res_base_idx, res_cur_idx;
585         uint8_t success = 0;
586
587         LOG_DEBUG(VHOST_DATA, "(%"PRIu64") virtio_dev_merge_rx()\n",
588                 dev->device_fh);
589         if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->virt_qp_nb))) {
590                 RTE_LOG(ERR, VHOST_DATA,
591                         "%s (%"PRIu64"): virtqueue idx:%d invalid.\n",
592                         __func__, dev->device_fh, queue_id);
593                 return 0;
594         }
595
596         vq = dev->virtqueue[queue_id];
597         if (unlikely(vq->enabled == 0))
598                 return 0;
599
600         count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
601
602         if (count == 0)
603                 return 0;
604
605         for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
606                 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + vq->vhost_hlen;
607
608                 do {
609                         /*
610                          * As many data cores may want access to available
611                          * buffers, they need to be reserved.
612                          */
613                         uint32_t secure_len = 0;
614                         uint32_t vec_idx = 0;
615
616                         res_base_idx = vq->last_used_idx_res;
617                         res_cur_idx = res_base_idx;
618
619                         do {
620                                 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
621                                 if (unlikely(res_cur_idx == avail_idx))
622                                         goto merge_rx_exit;
623
624                                 update_secure_len(vq, res_cur_idx,
625                                                   &secure_len, &vec_idx);
626                                 res_cur_idx++;
627                         } while (pkt_len > secure_len);
628
629                         /* vq->last_used_idx_res is atomically updated. */
630                         success = rte_atomic16_cmpset(&vq->last_used_idx_res,
631                                                         res_base_idx,
632                                                         res_cur_idx);
633                 } while (success == 0);
634
635                 entry_success = copy_from_mbuf_to_vring(dev, queue_id,
636                         res_base_idx, res_cur_idx, pkts[pkt_idx]);
637
638                 rte_compiler_barrier();
639
640                 /*
641                  * Wait until it's our turn to add our buffer
642                  * to the used ring.
643                  */
644                 while (unlikely(vq->last_used_idx != res_base_idx))
645                         rte_pause();
646
647                 *(volatile uint16_t *)&vq->used->idx += entry_success;
648                 vq->last_used_idx = res_cur_idx;
649         }
650
651 merge_rx_exit:
652         if (likely(pkt_idx)) {
653                 /* flush used->idx update before we read avail->flags. */
654                 rte_mb();
655
656                 /* Kick the guest if necessary. */
657                 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
658                         eventfd_write(vq->callfd, (eventfd_t)1);
659         }
660
661         return pkt_idx;
662 }
663
664 uint16_t
665 rte_vhost_enqueue_burst(struct virtio_net *dev, uint16_t queue_id,
666         struct rte_mbuf **pkts, uint16_t count)
667 {
668         if (unlikely(dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)))
669                 return virtio_dev_merge_rx(dev, queue_id, pkts, count);
670         else
671                 return virtio_dev_rx(dev, queue_id, pkts, count);
672 }
673
674 static void
675 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
676 {
677         struct ipv4_hdr *ipv4_hdr;
678         struct ipv6_hdr *ipv6_hdr;
679         void *l3_hdr = NULL;
680         struct ether_hdr *eth_hdr;
681         uint16_t ethertype;
682
683         eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
684
685         m->l2_len = sizeof(struct ether_hdr);
686         ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
687
688         if (ethertype == ETHER_TYPE_VLAN) {
689                 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
690
691                 m->l2_len += sizeof(struct vlan_hdr);
692                 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
693         }
694
695         l3_hdr = (char *)eth_hdr + m->l2_len;
696
697         switch (ethertype) {
698         case ETHER_TYPE_IPv4:
699                 ipv4_hdr = (struct ipv4_hdr *)l3_hdr;
700                 *l4_proto = ipv4_hdr->next_proto_id;
701                 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
702                 *l4_hdr = (char *)l3_hdr + m->l3_len;
703                 m->ol_flags |= PKT_TX_IPV4;
704                 break;
705         case ETHER_TYPE_IPv6:
706                 ipv6_hdr = (struct ipv6_hdr *)l3_hdr;
707                 *l4_proto = ipv6_hdr->proto;
708                 m->l3_len = sizeof(struct ipv6_hdr);
709                 *l4_hdr = (char *)l3_hdr + m->l3_len;
710                 m->ol_flags |= PKT_TX_IPV6;
711                 break;
712         default:
713                 m->l3_len = 0;
714                 *l4_proto = 0;
715                 break;
716         }
717 }
718
719 static inline void __attribute__((always_inline))
720 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
721 {
722         uint16_t l4_proto = 0;
723         void *l4_hdr = NULL;
724         struct tcp_hdr *tcp_hdr = NULL;
725
726         parse_ethernet(m, &l4_proto, &l4_hdr);
727         if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
728                 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
729                         switch (hdr->csum_offset) {
730                         case (offsetof(struct tcp_hdr, cksum)):
731                                 if (l4_proto == IPPROTO_TCP)
732                                         m->ol_flags |= PKT_TX_TCP_CKSUM;
733                                 break;
734                         case (offsetof(struct udp_hdr, dgram_cksum)):
735                                 if (l4_proto == IPPROTO_UDP)
736                                         m->ol_flags |= PKT_TX_UDP_CKSUM;
737                                 break;
738                         case (offsetof(struct sctp_hdr, cksum)):
739                                 if (l4_proto == IPPROTO_SCTP)
740                                         m->ol_flags |= PKT_TX_SCTP_CKSUM;
741                                 break;
742                         default:
743                                 break;
744                         }
745                 }
746         }
747
748         if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
749                 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
750                 case VIRTIO_NET_HDR_GSO_TCPV4:
751                 case VIRTIO_NET_HDR_GSO_TCPV6:
752                         tcp_hdr = (struct tcp_hdr *)l4_hdr;
753                         m->ol_flags |= PKT_TX_TCP_SEG;
754                         m->tso_segsz = hdr->gso_size;
755                         m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
756                         break;
757                 default:
758                         RTE_LOG(WARNING, VHOST_DATA,
759                                 "unsupported gso type %u.\n", hdr->gso_type);
760                         break;
761                 }
762         }
763 }
764
765 #define RARP_PKT_SIZE   64
766
767 static int
768 make_rarp_packet(struct rte_mbuf *rarp_mbuf, const struct ether_addr *mac)
769 {
770         struct ether_hdr *eth_hdr;
771         struct arp_hdr  *rarp;
772
773         if (rarp_mbuf->buf_len < 64) {
774                 RTE_LOG(WARNING, VHOST_DATA,
775                         "failed to make RARP; mbuf size too small %u (< %d)\n",
776                         rarp_mbuf->buf_len, RARP_PKT_SIZE);
777                 return -1;
778         }
779
780         /* Ethernet header. */
781         eth_hdr = rte_pktmbuf_mtod_offset(rarp_mbuf, struct ether_hdr *, 0);
782         memset(eth_hdr->d_addr.addr_bytes, 0xff, ETHER_ADDR_LEN);
783         ether_addr_copy(mac, &eth_hdr->s_addr);
784         eth_hdr->ether_type = htons(ETHER_TYPE_RARP);
785
786         /* RARP header. */
787         rarp = (struct arp_hdr *)(eth_hdr + 1);
788         rarp->arp_hrd = htons(ARP_HRD_ETHER);
789         rarp->arp_pro = htons(ETHER_TYPE_IPv4);
790         rarp->arp_hln = ETHER_ADDR_LEN;
791         rarp->arp_pln = 4;
792         rarp->arp_op  = htons(ARP_OP_REVREQUEST);
793
794         ether_addr_copy(mac, &rarp->arp_data.arp_sha);
795         ether_addr_copy(mac, &rarp->arp_data.arp_tha);
796         memset(&rarp->arp_data.arp_sip, 0x00, 4);
797         memset(&rarp->arp_data.arp_tip, 0x00, 4);
798
799         rarp_mbuf->pkt_len  = rarp_mbuf->data_len = RARP_PKT_SIZE;
800
801         return 0;
802 }
803
804 uint16_t
805 rte_vhost_dequeue_burst(struct virtio_net *dev, uint16_t queue_id,
806         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
807 {
808         struct rte_mbuf *m, *prev, *rarp_mbuf = NULL;
809         struct vhost_virtqueue *vq;
810         struct vring_desc *desc;
811         uint64_t vb_addr = 0;
812         uint64_t vb_net_hdr_addr = 0;
813         uint32_t head[MAX_PKT_BURST];
814         uint32_t used_idx;
815         uint32_t i;
816         uint16_t free_entries, entry_success = 0;
817         uint16_t avail_idx;
818         struct virtio_net_hdr *hdr = NULL;
819
820         if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->virt_qp_nb))) {
821                 RTE_LOG(ERR, VHOST_DATA,
822                         "%s (%"PRIu64"): virtqueue idx:%d invalid.\n",
823                         __func__, dev->device_fh, queue_id);
824                 return 0;
825         }
826
827         vq = dev->virtqueue[queue_id];
828         if (unlikely(vq->enabled == 0))
829                 return 0;
830
831         /*
832          * Construct a RARP broadcast packet, and inject it to the "pkts"
833          * array, to looks like that guest actually send such packet.
834          *
835          * Check user_send_rarp() for more information.
836          */
837         if (unlikely(rte_atomic16_cmpset((volatile uint16_t *)
838                                          &dev->broadcast_rarp.cnt, 1, 0))) {
839                 rarp_mbuf = rte_pktmbuf_alloc(mbuf_pool);
840                 if (rarp_mbuf == NULL) {
841                         RTE_LOG(ERR, VHOST_DATA,
842                                 "Failed to allocate memory for mbuf.\n");
843                         return 0;
844                 }
845
846                 if (make_rarp_packet(rarp_mbuf, &dev->mac)) {
847                         rte_pktmbuf_free(rarp_mbuf);
848                         rarp_mbuf = NULL;
849                 } else {
850                         count -= 1;
851                 }
852         }
853
854         avail_idx =  *((volatile uint16_t *)&vq->avail->idx);
855
856         /* If there are no available buffers then return. */
857         if (vq->last_used_idx == avail_idx)
858                 goto out;
859
860         LOG_DEBUG(VHOST_DATA, "%s (%"PRIu64")\n", __func__,
861                 dev->device_fh);
862
863         /* Prefetch available ring to retrieve head indexes. */
864         rte_prefetch0(&vq->avail->ring[vq->last_used_idx & (vq->size - 1)]);
865
866         /*get the number of free entries in the ring*/
867         free_entries = (avail_idx - vq->last_used_idx);
868
869         free_entries = RTE_MIN(free_entries, count);
870         /* Limit to MAX_PKT_BURST. */
871         free_entries = RTE_MIN(free_entries, MAX_PKT_BURST);
872
873         LOG_DEBUG(VHOST_DATA, "(%"PRIu64") Buffers available %d\n",
874                         dev->device_fh, free_entries);
875         /* Retrieve all of the head indexes first to avoid caching issues. */
876         for (i = 0; i < free_entries; i++)
877                 head[i] = vq->avail->ring[(vq->last_used_idx + i) & (vq->size - 1)];
878
879         /* Prefetch descriptor index. */
880         rte_prefetch0(&vq->desc[head[entry_success]]);
881         rte_prefetch0(&vq->used->ring[vq->last_used_idx & (vq->size - 1)]);
882
883         while (entry_success < free_entries) {
884                 uint32_t vb_avail, vb_offset;
885                 uint32_t seg_avail, seg_offset;
886                 uint32_t cpy_len;
887                 uint32_t seg_num = 0;
888                 struct rte_mbuf *cur;
889                 uint8_t alloc_err = 0;
890
891                 desc = &vq->desc[head[entry_success]];
892
893                 vb_net_hdr_addr = gpa_to_vva(dev, desc->addr);
894                 hdr = (struct virtio_net_hdr *)((uintptr_t)vb_net_hdr_addr);
895
896                 /* Discard first buffer as it is the virtio header */
897                 if (desc->flags & VRING_DESC_F_NEXT) {
898                         desc = &vq->desc[desc->next];
899                         vb_offset = 0;
900                         vb_avail = desc->len;
901                 } else {
902                         vb_offset = vq->vhost_hlen;
903                         vb_avail = desc->len - vb_offset;
904                 }
905
906                 /* Buffer address translation. */
907                 vb_addr = gpa_to_vva(dev, desc->addr);
908                 /* Prefetch buffer address. */
909                 rte_prefetch0((void *)(uintptr_t)vb_addr);
910
911                 used_idx = vq->last_used_idx & (vq->size - 1);
912
913                 if (entry_success < (free_entries - 1)) {
914                         /* Prefetch descriptor index. */
915                         rte_prefetch0(&vq->desc[head[entry_success+1]]);
916                         rte_prefetch0(&vq->used->ring[(used_idx + 1) & (vq->size - 1)]);
917                 }
918
919                 /* Update used index buffer information. */
920                 vq->used->ring[used_idx].id = head[entry_success];
921                 vq->used->ring[used_idx].len = 0;
922                 vhost_log_used_vring(dev, vq,
923                                 offsetof(struct vring_used, ring[used_idx]),
924                                 sizeof(vq->used->ring[used_idx]));
925
926                 /* Allocate an mbuf and populate the structure. */
927                 m = rte_pktmbuf_alloc(mbuf_pool);
928                 if (unlikely(m == NULL)) {
929                         RTE_LOG(ERR, VHOST_DATA,
930                                 "Failed to allocate memory for mbuf.\n");
931                         break;
932                 }
933                 seg_offset = 0;
934                 seg_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
935                 cpy_len = RTE_MIN(vb_avail, seg_avail);
936
937                 PRINT_PACKET(dev, (uintptr_t)vb_addr, desc->len, 0);
938
939                 seg_num++;
940                 cur = m;
941                 prev = m;
942                 while (cpy_len != 0) {
943                         rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *, seg_offset),
944                                 (void *)((uintptr_t)(vb_addr + vb_offset)),
945                                 cpy_len);
946
947                         seg_offset += cpy_len;
948                         vb_offset += cpy_len;
949                         vb_avail -= cpy_len;
950                         seg_avail -= cpy_len;
951
952                         if (vb_avail != 0) {
953                                 /*
954                                  * The segment reachs to its end,
955                                  * while the virtio buffer in TX vring has
956                                  * more data to be copied.
957                                  */
958                                 cur->data_len = seg_offset;
959                                 m->pkt_len += seg_offset;
960                                 /* Allocate mbuf and populate the structure. */
961                                 cur = rte_pktmbuf_alloc(mbuf_pool);
962                                 if (unlikely(cur == NULL)) {
963                                         RTE_LOG(ERR, VHOST_DATA, "Failed to "
964                                                 "allocate memory for mbuf.\n");
965                                         rte_pktmbuf_free(m);
966                                         alloc_err = 1;
967                                         break;
968                                 }
969
970                                 seg_num++;
971                                 prev->next = cur;
972                                 prev = cur;
973                                 seg_offset = 0;
974                                 seg_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
975                         } else {
976                                 if (desc->flags & VRING_DESC_F_NEXT) {
977                                         /*
978                                          * There are more virtio buffers in
979                                          * same vring entry need to be copied.
980                                          */
981                                         if (seg_avail == 0) {
982                                                 /*
983                                                  * The current segment hasn't
984                                                  * room to accomodate more
985                                                  * data.
986                                                  */
987                                                 cur->data_len = seg_offset;
988                                                 m->pkt_len += seg_offset;
989                                                 /*
990                                                  * Allocate an mbuf and
991                                                  * populate the structure.
992                                                  */
993                                                 cur = rte_pktmbuf_alloc(mbuf_pool);
994                                                 if (unlikely(cur == NULL)) {
995                                                         RTE_LOG(ERR,
996                                                                 VHOST_DATA,
997                                                                 "Failed to "
998                                                                 "allocate memory "
999                                                                 "for mbuf\n");
1000                                                         rte_pktmbuf_free(m);
1001                                                         alloc_err = 1;
1002                                                         break;
1003                                                 }
1004                                                 seg_num++;
1005                                                 prev->next = cur;
1006                                                 prev = cur;
1007                                                 seg_offset = 0;
1008                                                 seg_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
1009                                         }
1010
1011                                         desc = &vq->desc[desc->next];
1012
1013                                         /* Buffer address translation. */
1014                                         vb_addr = gpa_to_vva(dev, desc->addr);
1015                                         /* Prefetch buffer address. */
1016                                         rte_prefetch0((void *)(uintptr_t)vb_addr);
1017                                         vb_offset = 0;
1018                                         vb_avail = desc->len;
1019
1020                                         PRINT_PACKET(dev, (uintptr_t)vb_addr,
1021                                                 desc->len, 0);
1022                                 } else {
1023                                         /* The whole packet completes. */
1024                                         cur->data_len = seg_offset;
1025                                         m->pkt_len += seg_offset;
1026                                         vb_avail = 0;
1027                                 }
1028                         }
1029
1030                         cpy_len = RTE_MIN(vb_avail, seg_avail);
1031                 }
1032
1033                 if (unlikely(alloc_err == 1))
1034                         break;
1035
1036                 m->nb_segs = seg_num;
1037                 if ((hdr->flags != 0) || (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE))
1038                         vhost_dequeue_offload(hdr, m);
1039
1040                 pkts[entry_success] = m;
1041                 vq->last_used_idx++;
1042                 entry_success++;
1043         }
1044
1045         rte_compiler_barrier();
1046         vq->used->idx += entry_success;
1047         vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
1048                         sizeof(vq->used->idx));
1049
1050         /* Kick guest if required. */
1051         if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
1052                 eventfd_write(vq->callfd, (eventfd_t)1);
1053
1054 out:
1055         if (unlikely(rarp_mbuf != NULL)) {
1056                 /*
1057                  * Inject it to the head of "pkts" array, so that switch's mac
1058                  * learning table will get updated first.
1059                  */
1060                 memmove(&pkts[1], pkts, entry_success * sizeof(m));
1061                 pkts[0] = rarp_mbuf;
1062                 entry_success += 1;
1063         }
1064
1065         return entry_success;
1066 }