vhost: pre update used ring for Tx and Rx
[dpdk.git] / lib / librte_vhost / vhost_rxtx.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdint.h>
35 #include <stdbool.h>
36 #include <linux/virtio_net.h>
37
38 #include <rte_mbuf.h>
39 #include <rte_memcpy.h>
40 #include <rte_ether.h>
41 #include <rte_ip.h>
42 #include <rte_virtio_net.h>
43 #include <rte_tcp.h>
44 #include <rte_udp.h>
45 #include <rte_sctp.h>
46 #include <rte_arp.h>
47
48 #include "vhost-net.h"
49
50 #define MAX_PKT_BURST 32
51 #define VHOST_LOG_PAGE  4096
52
53 static inline void __attribute__((always_inline))
54 vhost_log_page(uint8_t *log_base, uint64_t page)
55 {
56         log_base[page / 8] |= 1 << (page % 8);
57 }
58
59 static inline void __attribute__((always_inline))
60 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
61 {
62         uint64_t page;
63
64         if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
65                    !dev->log_base || !len))
66                 return;
67
68         if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
69                 return;
70
71         /* To make sure guest memory updates are committed before logging */
72         rte_smp_wmb();
73
74         page = addr / VHOST_LOG_PAGE;
75         while (page * VHOST_LOG_PAGE < addr + len) {
76                 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
77                 page += 1;
78         }
79 }
80
81 static inline void __attribute__((always_inline))
82 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
83                      uint64_t offset, uint64_t len)
84 {
85         vhost_log_write(dev, vq->log_guest_addr + offset, len);
86 }
87
88 static bool
89 is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t qp_nb)
90 {
91         return (is_tx ^ (idx & 1)) == 0 && idx < qp_nb * VIRTIO_QNUM;
92 }
93
94 static void
95 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
96 {
97         if (m_buf->ol_flags & PKT_TX_L4_MASK) {
98                 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
99                 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
100
101                 switch (m_buf->ol_flags & PKT_TX_L4_MASK) {
102                 case PKT_TX_TCP_CKSUM:
103                         net_hdr->csum_offset = (offsetof(struct tcp_hdr,
104                                                 cksum));
105                         break;
106                 case PKT_TX_UDP_CKSUM:
107                         net_hdr->csum_offset = (offsetof(struct udp_hdr,
108                                                 dgram_cksum));
109                         break;
110                 case PKT_TX_SCTP_CKSUM:
111                         net_hdr->csum_offset = (offsetof(struct sctp_hdr,
112                                                 cksum));
113                         break;
114                 }
115         }
116
117         if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
118                 if (m_buf->ol_flags & PKT_TX_IPV4)
119                         net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
120                 else
121                         net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
122                 net_hdr->gso_size = m_buf->tso_segsz;
123                 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
124                                         + m_buf->l4_len;
125         }
126 }
127
128 static inline void
129 copy_virtio_net_hdr(struct virtio_net *dev, uint64_t desc_addr,
130                     struct virtio_net_hdr_mrg_rxbuf hdr)
131 {
132         if (dev->vhost_hlen == sizeof(struct virtio_net_hdr_mrg_rxbuf))
133                 *(struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)desc_addr = hdr;
134         else
135                 *(struct virtio_net_hdr *)(uintptr_t)desc_addr = hdr.hdr;
136 }
137
138 static inline int __attribute__((always_inline))
139 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
140                   struct rte_mbuf *m, uint16_t desc_idx)
141 {
142         uint32_t desc_avail, desc_offset;
143         uint32_t mbuf_avail, mbuf_offset;
144         uint32_t cpy_len;
145         struct vring_desc *desc;
146         uint64_t desc_addr;
147         struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0, 0, 0, 0, 0, 0}, 0};
148
149         desc = &vq->desc[desc_idx];
150         if (unlikely(desc->len < dev->vhost_hlen))
151                 return -1;
152
153         desc_addr = gpa_to_vva(dev, desc->addr);
154         rte_prefetch0((void *)(uintptr_t)desc_addr);
155
156         virtio_enqueue_offload(m, &virtio_hdr.hdr);
157         copy_virtio_net_hdr(dev, desc_addr, virtio_hdr);
158         vhost_log_write(dev, desc->addr, dev->vhost_hlen);
159         PRINT_PACKET(dev, (uintptr_t)desc_addr, dev->vhost_hlen, 0);
160
161         desc_offset = dev->vhost_hlen;
162         desc_avail  = desc->len - dev->vhost_hlen;
163
164         mbuf_avail  = rte_pktmbuf_data_len(m);
165         mbuf_offset = 0;
166         while (mbuf_avail != 0 || m->next != NULL) {
167                 /* done with current mbuf, fetch next */
168                 if (mbuf_avail == 0) {
169                         m = m->next;
170
171                         mbuf_offset = 0;
172                         mbuf_avail  = rte_pktmbuf_data_len(m);
173                 }
174
175                 /* done with current desc buf, fetch next */
176                 if (desc_avail == 0) {
177                         if ((desc->flags & VRING_DESC_F_NEXT) == 0) {
178                                 /* Room in vring buffer is not enough */
179                                 return -1;
180                         }
181                         if (unlikely(desc->next >= vq->size))
182                                 return -1;
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         uint16_t used_idx;
265         uint32_t i;
266
267         LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
268         if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->virt_qp_nb))) {
269                 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
270                         dev->vid, __func__, 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, "(%d) res_start_idx %d | res_end_idx Index %d\n",
283                 dev->vid, res_start_idx, res_end_idx);
284
285         /* Retrieve all of the desc indexes first to avoid caching issues. */
286         rte_prefetch0(&vq->avail->ring[res_start_idx & (vq->size - 1)]);
287         for (i = 0; i < count; i++) {
288                 used_idx = (res_start_idx + i) & (vq->size - 1);
289                 desc_indexes[i] = vq->avail->ring[used_idx];
290                 vq->used->ring[used_idx].id = desc_indexes[i];
291                 vq->used->ring[used_idx].len = pkts[i]->pkt_len +
292                                                dev->vhost_hlen;
293                 vhost_log_used_vring(dev, vq,
294                         offsetof(struct vring_used, ring[used_idx]),
295                         sizeof(vq->used->ring[used_idx]));
296         }
297
298         rte_prefetch0(&vq->desc[desc_indexes[0]]);
299         for (i = 0; i < count; i++) {
300                 uint16_t desc_idx = desc_indexes[i];
301                 int err;
302
303                 err = copy_mbuf_to_desc(dev, vq, pkts[i], desc_idx);
304                 if (unlikely(err)) {
305                         used_idx = (res_start_idx + i) & (vq->size - 1);
306                         vq->used->ring[used_idx].len = dev->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
312                 if (i + 1 < count)
313                         rte_prefetch0(&vq->desc[desc_indexes[i+1]]);
314         }
315
316         rte_smp_wmb();
317
318         /* Wait until it's our turn to add our buffer to the used ring. */
319         while (unlikely(vq->last_used_idx != res_start_idx))
320                 rte_pause();
321
322         *(volatile uint16_t *)&vq->used->idx += count;
323         vq->last_used_idx = res_end_idx;
324         vhost_log_used_vring(dev, vq,
325                 offsetof(struct vring_used, idx),
326                 sizeof(vq->used->idx));
327
328         /* flush used->idx update before we read avail->flags. */
329         rte_mb();
330
331         /* Kick the guest if necessary. */
332         if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
333                         && (vq->callfd >= 0))
334                 eventfd_write(vq->callfd, (eventfd_t)1);
335         return count;
336 }
337
338 static inline int
339 fill_vec_buf(struct vhost_virtqueue *vq, uint32_t avail_idx,
340              uint32_t *allocated, uint32_t *vec_idx,
341              struct buf_vector *buf_vec)
342 {
343         uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
344         uint32_t vec_id = *vec_idx;
345         uint32_t len    = *allocated;
346
347         while (1) {
348                 if (unlikely(vec_id >= BUF_VECTOR_MAX || idx >= vq->size))
349                         return -1;
350
351                 len += vq->desc[idx].len;
352                 buf_vec[vec_id].buf_addr = vq->desc[idx].addr;
353                 buf_vec[vec_id].buf_len  = vq->desc[idx].len;
354                 buf_vec[vec_id].desc_idx = idx;
355                 vec_id++;
356
357                 if ((vq->desc[idx].flags & VRING_DESC_F_NEXT) == 0)
358                         break;
359
360                 idx = vq->desc[idx].next;
361         }
362
363         *allocated = len;
364         *vec_idx   = vec_id;
365
366         return 0;
367 }
368
369 /*
370  * As many data cores may want to access available buffers concurrently,
371  * they need to be reserved.
372  *
373  * Returns -1 on fail, 0 on success
374  */
375 static inline int
376 reserve_avail_buf_mergeable(struct vhost_virtqueue *vq, uint32_t size,
377                             uint16_t *start, uint16_t *end,
378                             struct buf_vector *buf_vec)
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, buf_vec) < 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, struct buf_vector *buf_vec)
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, "(%d) current index %d | end index %d\n",
449                 dev->vid, cur_idx, res_end_idx);
450
451         if (buf_vec[vec_idx].buf_len < dev->vhost_hlen)
452                 return -1;
453
454         desc_addr = gpa_to_vva(dev, buf_vec[vec_idx].buf_addr);
455         rte_prefetch0((void *)(uintptr_t)desc_addr);
456
457         virtio_hdr.num_buffers = res_end_idx - res_start_idx;
458         LOG_DEBUG(VHOST_DATA, "(%d) RX: num merge buffers %d\n",
459                 dev->vid, virtio_hdr.num_buffers);
460
461         virtio_enqueue_offload(m, &virtio_hdr.hdr);
462         copy_virtio_net_hdr(dev, desc_addr, virtio_hdr);
463         vhost_log_write(dev, buf_vec[vec_idx].buf_addr, dev->vhost_hlen);
464         PRINT_PACKET(dev, (uintptr_t)desc_addr, dev->vhost_hlen, 0);
465
466         desc_avail  = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
467         desc_offset = dev->vhost_hlen;
468
469         mbuf_avail  = rte_pktmbuf_data_len(m);
470         mbuf_offset = 0;
471         while (mbuf_avail != 0 || m->next != NULL) {
472                 /* done with current desc buf, get the next one */
473                 if (desc_avail == 0) {
474                         desc_idx = buf_vec[vec_idx].desc_idx;
475
476                         if (!(vq->desc[desc_idx].flags & VRING_DESC_F_NEXT)) {
477                                 /* Update used ring with desc information */
478                                 used_idx = cur_idx++ & (vq->size - 1);
479                                 vq->used->ring[used_idx].id  = desc_idx;
480                                 vq->used->ring[used_idx].len = desc_offset;
481                                 vhost_log_used_vring(dev, vq,
482                                         offsetof(struct vring_used,
483                                                  ring[used_idx]),
484                                         sizeof(vq->used->ring[used_idx]));
485                         }
486
487                         vec_idx++;
488                         desc_addr = gpa_to_vva(dev, buf_vec[vec_idx].buf_addr);
489
490                         /* Prefetch buffer address. */
491                         rte_prefetch0((void *)(uintptr_t)desc_addr);
492                         desc_offset = 0;
493                         desc_avail  = buf_vec[vec_idx].buf_len;
494                 }
495
496                 /* done with current mbuf, get the next one */
497                 if (mbuf_avail == 0) {
498                         m = m->next;
499
500                         mbuf_offset = 0;
501                         mbuf_avail  = rte_pktmbuf_data_len(m);
502                 }
503
504                 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
505                 rte_memcpy((void *)((uintptr_t)(desc_addr + desc_offset)),
506                         rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
507                         cpy_len);
508                 vhost_log_write(dev, buf_vec[vec_idx].buf_addr + desc_offset,
509                         cpy_len);
510                 PRINT_PACKET(dev, (uintptr_t)(desc_addr + desc_offset),
511                         cpy_len, 0);
512
513                 mbuf_avail  -= cpy_len;
514                 mbuf_offset += cpy_len;
515                 desc_avail  -= cpy_len;
516                 desc_offset += cpy_len;
517         }
518
519         used_idx = cur_idx & (vq->size - 1);
520         vq->used->ring[used_idx].id = buf_vec[vec_idx].desc_idx;
521         vq->used->ring[used_idx].len = desc_offset;
522         vhost_log_used_vring(dev, vq,
523                 offsetof(struct vring_used, ring[used_idx]),
524                 sizeof(vq->used->ring[used_idx]));
525
526         return res_end_idx - res_start_idx;
527 }
528
529 static inline uint32_t __attribute__((always_inline))
530 virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,
531         struct rte_mbuf **pkts, uint32_t count)
532 {
533         struct vhost_virtqueue *vq;
534         uint32_t pkt_idx = 0, nr_used = 0;
535         uint16_t start, end;
536         struct buf_vector buf_vec[BUF_VECTOR_MAX];
537
538         LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
539         if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->virt_qp_nb))) {
540                 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
541                         dev->vid, __func__, queue_id);
542                 return 0;
543         }
544
545         vq = dev->virtqueue[queue_id];
546         if (unlikely(vq->enabled == 0))
547                 return 0;
548
549         count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
550         if (count == 0)
551                 return 0;
552
553         for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
554                 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
555
556                 if (unlikely(reserve_avail_buf_mergeable(vq, pkt_len, &start,
557                                                          &end, buf_vec) < 0)) {
558                         LOG_DEBUG(VHOST_DATA,
559                                 "(%d) failed to get enough desc from vring\n",
560                                 dev->vid);
561                         break;
562                 }
563
564                 nr_used = copy_mbuf_to_desc_mergeable(dev, vq, start, end,
565                                                       pkts[pkt_idx], buf_vec);
566                 rte_smp_wmb();
567
568                 /*
569                  * Wait until it's our turn to add our buffer
570                  * to the used ring.
571                  */
572                 while (unlikely(vq->last_used_idx != start))
573                         rte_pause();
574
575                 *(volatile uint16_t *)&vq->used->idx += nr_used;
576                 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
577                         sizeof(vq->used->idx));
578                 vq->last_used_idx = end;
579         }
580
581         if (likely(pkt_idx)) {
582                 /* flush used->idx update before we read avail->flags. */
583                 rte_mb();
584
585                 /* Kick the guest if necessary. */
586                 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
587                                 && (vq->callfd >= 0))
588                         eventfd_write(vq->callfd, (eventfd_t)1);
589         }
590
591         return pkt_idx;
592 }
593
594 uint16_t
595 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
596         struct rte_mbuf **pkts, uint16_t count)
597 {
598         struct virtio_net *dev = get_device(vid);
599
600         if (!dev)
601                 return 0;
602
603         if (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF))
604                 return virtio_dev_merge_rx(dev, queue_id, pkts, count);
605         else
606                 return virtio_dev_rx(dev, queue_id, pkts, count);
607 }
608
609 static void
610 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
611 {
612         struct ipv4_hdr *ipv4_hdr;
613         struct ipv6_hdr *ipv6_hdr;
614         void *l3_hdr = NULL;
615         struct ether_hdr *eth_hdr;
616         uint16_t ethertype;
617
618         eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
619
620         m->l2_len = sizeof(struct ether_hdr);
621         ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
622
623         if (ethertype == ETHER_TYPE_VLAN) {
624                 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
625
626                 m->l2_len += sizeof(struct vlan_hdr);
627                 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
628         }
629
630         l3_hdr = (char *)eth_hdr + m->l2_len;
631
632         switch (ethertype) {
633         case ETHER_TYPE_IPv4:
634                 ipv4_hdr = (struct ipv4_hdr *)l3_hdr;
635                 *l4_proto = ipv4_hdr->next_proto_id;
636                 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
637                 *l4_hdr = (char *)l3_hdr + m->l3_len;
638                 m->ol_flags |= PKT_TX_IPV4;
639                 break;
640         case ETHER_TYPE_IPv6:
641                 ipv6_hdr = (struct ipv6_hdr *)l3_hdr;
642                 *l4_proto = ipv6_hdr->proto;
643                 m->l3_len = sizeof(struct ipv6_hdr);
644                 *l4_hdr = (char *)l3_hdr + m->l3_len;
645                 m->ol_flags |= PKT_TX_IPV6;
646                 break;
647         default:
648                 m->l3_len = 0;
649                 *l4_proto = 0;
650                 break;
651         }
652 }
653
654 static inline void __attribute__((always_inline))
655 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
656 {
657         uint16_t l4_proto = 0;
658         void *l4_hdr = NULL;
659         struct tcp_hdr *tcp_hdr = NULL;
660
661         parse_ethernet(m, &l4_proto, &l4_hdr);
662         if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
663                 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
664                         switch (hdr->csum_offset) {
665                         case (offsetof(struct tcp_hdr, cksum)):
666                                 if (l4_proto == IPPROTO_TCP)
667                                         m->ol_flags |= PKT_TX_TCP_CKSUM;
668                                 break;
669                         case (offsetof(struct udp_hdr, dgram_cksum)):
670                                 if (l4_proto == IPPROTO_UDP)
671                                         m->ol_flags |= PKT_TX_UDP_CKSUM;
672                                 break;
673                         case (offsetof(struct sctp_hdr, cksum)):
674                                 if (l4_proto == IPPROTO_SCTP)
675                                         m->ol_flags |= PKT_TX_SCTP_CKSUM;
676                                 break;
677                         default:
678                                 break;
679                         }
680                 }
681         }
682
683         if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
684                 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
685                 case VIRTIO_NET_HDR_GSO_TCPV4:
686                 case VIRTIO_NET_HDR_GSO_TCPV6:
687                         tcp_hdr = (struct tcp_hdr *)l4_hdr;
688                         m->ol_flags |= PKT_TX_TCP_SEG;
689                         m->tso_segsz = hdr->gso_size;
690                         m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
691                         break;
692                 default:
693                         RTE_LOG(WARNING, VHOST_DATA,
694                                 "unsupported gso type %u.\n", hdr->gso_type);
695                         break;
696                 }
697         }
698 }
699
700 #define RARP_PKT_SIZE   64
701
702 static int
703 make_rarp_packet(struct rte_mbuf *rarp_mbuf, const struct ether_addr *mac)
704 {
705         struct ether_hdr *eth_hdr;
706         struct arp_hdr  *rarp;
707
708         if (rarp_mbuf->buf_len < 64) {
709                 RTE_LOG(WARNING, VHOST_DATA,
710                         "failed to make RARP; mbuf size too small %u (< %d)\n",
711                         rarp_mbuf->buf_len, RARP_PKT_SIZE);
712                 return -1;
713         }
714
715         /* Ethernet header. */
716         eth_hdr = rte_pktmbuf_mtod_offset(rarp_mbuf, struct ether_hdr *, 0);
717         memset(eth_hdr->d_addr.addr_bytes, 0xff, ETHER_ADDR_LEN);
718         ether_addr_copy(mac, &eth_hdr->s_addr);
719         eth_hdr->ether_type = htons(ETHER_TYPE_RARP);
720
721         /* RARP header. */
722         rarp = (struct arp_hdr *)(eth_hdr + 1);
723         rarp->arp_hrd = htons(ARP_HRD_ETHER);
724         rarp->arp_pro = htons(ETHER_TYPE_IPv4);
725         rarp->arp_hln = ETHER_ADDR_LEN;
726         rarp->arp_pln = 4;
727         rarp->arp_op  = htons(ARP_OP_REVREQUEST);
728
729         ether_addr_copy(mac, &rarp->arp_data.arp_sha);
730         ether_addr_copy(mac, &rarp->arp_data.arp_tha);
731         memset(&rarp->arp_data.arp_sip, 0x00, 4);
732         memset(&rarp->arp_data.arp_tip, 0x00, 4);
733
734         rarp_mbuf->pkt_len  = rarp_mbuf->data_len = RARP_PKT_SIZE;
735
736         return 0;
737 }
738
739 static inline int __attribute__((always_inline))
740 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
741                   struct rte_mbuf *m, uint16_t desc_idx,
742                   struct rte_mempool *mbuf_pool)
743 {
744         struct vring_desc *desc;
745         uint64_t desc_addr;
746         uint32_t desc_avail, desc_offset;
747         uint32_t mbuf_avail, mbuf_offset;
748         uint32_t cpy_len;
749         struct rte_mbuf *cur = m, *prev = m;
750         struct virtio_net_hdr *hdr;
751         /* A counter to avoid desc dead loop chain */
752         uint32_t nr_desc = 1;
753
754         desc = &vq->desc[desc_idx];
755         if (unlikely(desc->len < dev->vhost_hlen))
756                 return -1;
757
758         desc_addr = gpa_to_vva(dev, desc->addr);
759         rte_prefetch0((void *)(uintptr_t)desc_addr);
760
761         /* Retrieve virtio net header */
762         hdr = (struct virtio_net_hdr *)((uintptr_t)desc_addr);
763         desc_avail  = desc->len - dev->vhost_hlen;
764         desc_offset = dev->vhost_hlen;
765
766         mbuf_offset = 0;
767         mbuf_avail  = m->buf_len - RTE_PKTMBUF_HEADROOM;
768         while (desc_avail != 0 || (desc->flags & VRING_DESC_F_NEXT) != 0) {
769                 /* This desc reaches to its end, get the next one */
770                 if (desc_avail == 0) {
771                         if (unlikely(desc->next >= vq->size ||
772                                      ++nr_desc >= vq->size))
773                                 return -1;
774                         desc = &vq->desc[desc->next];
775
776                         desc_addr = gpa_to_vva(dev, desc->addr);
777                         rte_prefetch0((void *)(uintptr_t)desc_addr);
778
779                         desc_offset = 0;
780                         desc_avail  = desc->len;
781
782                         PRINT_PACKET(dev, (uintptr_t)desc_addr, desc->len, 0);
783                 }
784
785                 /*
786                  * This mbuf reaches to its end, get a new one
787                  * to hold more data.
788                  */
789                 if (mbuf_avail == 0) {
790                         cur = rte_pktmbuf_alloc(mbuf_pool);
791                         if (unlikely(cur == NULL)) {
792                                 RTE_LOG(ERR, VHOST_DATA, "Failed to "
793                                         "allocate memory for mbuf.\n");
794                                 return -1;
795                         }
796
797                         prev->next = cur;
798                         prev->data_len = mbuf_offset;
799                         m->nb_segs += 1;
800                         m->pkt_len += mbuf_offset;
801                         prev = cur;
802
803                         mbuf_offset = 0;
804                         mbuf_avail  = cur->buf_len - RTE_PKTMBUF_HEADROOM;
805                 }
806
807                 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
808                 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *, mbuf_offset),
809                         (void *)((uintptr_t)(desc_addr + desc_offset)),
810                         cpy_len);
811
812                 mbuf_avail  -= cpy_len;
813                 mbuf_offset += cpy_len;
814                 desc_avail  -= cpy_len;
815                 desc_offset += cpy_len;
816         }
817
818         prev->data_len = mbuf_offset;
819         m->pkt_len    += mbuf_offset;
820
821         if (hdr->flags != 0 || hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE)
822                 vhost_dequeue_offload(hdr, m);
823
824         return 0;
825 }
826
827 uint16_t
828 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
829         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
830 {
831         struct virtio_net *dev;
832         struct rte_mbuf *rarp_mbuf = NULL;
833         struct vhost_virtqueue *vq;
834         uint32_t desc_indexes[MAX_PKT_BURST];
835         uint32_t used_idx;
836         uint32_t i = 0;
837         uint16_t free_entries;
838         uint16_t avail_idx;
839
840         dev = get_device(vid);
841         if (!dev)
842                 return 0;
843
844         if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->virt_qp_nb))) {
845                 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
846                         dev->vid, __func__, queue_id);
847                 return 0;
848         }
849
850         vq = dev->virtqueue[queue_id];
851         if (unlikely(vq->enabled == 0))
852                 return 0;
853
854         /*
855          * Construct a RARP broadcast packet, and inject it to the "pkts"
856          * array, to looks like that guest actually send such packet.
857          *
858          * Check user_send_rarp() for more information.
859          */
860         if (unlikely(rte_atomic16_cmpset((volatile uint16_t *)
861                                          &dev->broadcast_rarp.cnt, 1, 0))) {
862                 rarp_mbuf = rte_pktmbuf_alloc(mbuf_pool);
863                 if (rarp_mbuf == NULL) {
864                         RTE_LOG(ERR, VHOST_DATA,
865                                 "Failed to allocate memory for mbuf.\n");
866                         return 0;
867                 }
868
869                 if (make_rarp_packet(rarp_mbuf, &dev->mac)) {
870                         rte_pktmbuf_free(rarp_mbuf);
871                         rarp_mbuf = NULL;
872                 } else {
873                         count -= 1;
874                 }
875         }
876
877         avail_idx =  *((volatile uint16_t *)&vq->avail->idx);
878         free_entries = avail_idx - vq->last_used_idx;
879         if (free_entries == 0)
880                 goto out;
881
882         LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
883
884         /* Prefetch available ring to retrieve head indexes. */
885         used_idx = vq->last_used_idx & (vq->size - 1);
886         rte_prefetch0(&vq->avail->ring[used_idx]);
887         rte_prefetch0(&vq->used->ring[used_idx]);
888
889         count = RTE_MIN(count, MAX_PKT_BURST);
890         count = RTE_MIN(count, free_entries);
891         LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
892                         dev->vid, count);
893
894         /* Retrieve all of the head indexes first to avoid caching issues. */
895         for (i = 0; i < count; i++) {
896                 used_idx = (vq->last_used_idx + i) & (vq->size - 1);
897                 desc_indexes[i] = vq->avail->ring[used_idx];
898
899                 vq->used->ring[used_idx].id  = desc_indexes[i];
900                 vq->used->ring[used_idx].len = 0;
901                 vhost_log_used_vring(dev, vq,
902                                 offsetof(struct vring_used, ring[used_idx]),
903                                 sizeof(vq->used->ring[used_idx]));
904         }
905
906         /* Prefetch descriptor index. */
907         rte_prefetch0(&vq->desc[desc_indexes[0]]);
908         for (i = 0; i < count; i++) {
909                 int err;
910
911                 if (likely(i + 1 < count))
912                         rte_prefetch0(&vq->desc[desc_indexes[i + 1]]);
913
914                 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
915                 if (unlikely(pkts[i] == NULL)) {
916                         RTE_LOG(ERR, VHOST_DATA,
917                                 "Failed to allocate memory for mbuf.\n");
918                         break;
919                 }
920                 err = copy_desc_to_mbuf(dev, vq, pkts[i], desc_indexes[i],
921                                         mbuf_pool);
922                 if (unlikely(err)) {
923                         rte_pktmbuf_free(pkts[i]);
924                         break;
925                 }
926         }
927
928         rte_smp_wmb();
929         rte_smp_rmb();
930         vq->used->idx += i;
931         vq->last_used_idx += i;
932         vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
933                         sizeof(vq->used->idx));
934
935         /* Kick guest if required. */
936         if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
937                         && (vq->callfd >= 0))
938                 eventfd_write(vq->callfd, (eventfd_t)1);
939
940 out:
941         if (unlikely(rarp_mbuf != NULL)) {
942                 /*
943                  * Inject it to the head of "pkts" array, so that switch's mac
944                  * learning table will get updated first.
945                  */
946                 memmove(&pkts[1], pkts, i * sizeof(struct rte_mbuf *));
947                 pkts[0] = rarp_mbuf;
948                 i += 1;
949         }
950
951         return i;
952 }