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