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