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