4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
35 #include <linux/virtio_net.h>
38 #include <rte_memcpy.h>
39 #include <rte_virtio_net.h>
41 #include "vhost-net.h"
43 #define MAX_PKT_BURST 32
46 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
47 * be received from the physical port or from another virtio device. A packet
48 * count is returned to indicate the number of packets that are succesfully
49 * added to the RX queue. This function works when the mbuf is scattered, but
50 * it doesn't support the mergeable feature.
52 static inline uint32_t __attribute__((always_inline))
53 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
54 struct rte_mbuf **pkts, uint32_t count)
56 struct vhost_virtqueue *vq;
57 struct vring_desc *desc;
58 struct rte_mbuf *buff;
59 /* The virtio_hdr is initialised to 0. */
60 struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0, 0, 0, 0, 0, 0}, 0};
61 uint64_t buff_addr = 0;
62 uint64_t buff_hdr_addr = 0;
63 uint32_t head[MAX_PKT_BURST];
64 uint32_t head_idx, packet_success = 0;
65 uint16_t avail_idx, res_cur_idx;
66 uint16_t res_base_idx, res_end_idx;
67 uint16_t free_entries;
70 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") virtio_dev_rx()\n", dev->device_fh);
71 if (unlikely(queue_id != VIRTIO_RXQ)) {
72 LOG_DEBUG(VHOST_DATA, "mq isn't supported in this version.\n");
76 vq = dev->virtqueue[VIRTIO_RXQ];
77 count = (count > MAX_PKT_BURST) ? MAX_PKT_BURST : count;
80 * As many data cores may want access to available buffers,
81 * they need to be reserved.
84 res_base_idx = vq->last_used_idx_res;
85 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
87 free_entries = (avail_idx - res_base_idx);
88 /*check that we have enough buffers*/
89 if (unlikely(count > free_entries))
95 res_end_idx = res_base_idx + count;
96 /* vq->last_used_idx_res is atomically updated. */
97 /* TODO: Allow to disable cmpset if no concurrency in application. */
98 success = rte_atomic16_cmpset(&vq->last_used_idx_res,
99 res_base_idx, res_end_idx);
100 } while (unlikely(success == 0));
101 res_cur_idx = res_base_idx;
102 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") Current Index %d| End Index %d\n",
103 dev->device_fh, res_cur_idx, res_end_idx);
105 /* Prefetch available ring to retrieve indexes. */
106 rte_prefetch0(&vq->avail->ring[res_cur_idx & (vq->size - 1)]);
108 /* Retrieve all of the head indexes first to avoid caching issues. */
109 for (head_idx = 0; head_idx < count; head_idx++)
110 head[head_idx] = vq->avail->ring[(res_cur_idx + head_idx) &
113 /*Prefetch descriptor index. */
114 rte_prefetch0(&vq->desc[head[packet_success]]);
116 while (res_cur_idx != res_end_idx) {
117 uint32_t offset = 0, vb_offset = 0;
118 uint32_t pkt_len, len_to_cpy, data_len, total_copied = 0;
119 uint8_t hdr = 0, uncompleted_pkt = 0;
121 /* Get descriptor from available ring */
122 desc = &vq->desc[head[packet_success]];
124 buff = pkts[packet_success];
126 /* Convert from gpa to vva (guest physical addr -> vhost virtual addr) */
127 buff_addr = gpa_to_vva(dev, desc->addr);
128 /* Prefetch buffer address. */
129 rte_prefetch0((void *)(uintptr_t)buff_addr);
131 /* Copy virtio_hdr to packet and increment buffer address */
132 buff_hdr_addr = buff_addr;
135 * If the descriptors are chained the header and data are
136 * placed in separate buffers.
138 if ((desc->flags & VRING_DESC_F_NEXT) &&
139 (desc->len == vq->vhost_hlen)) {
140 desc = &vq->desc[desc->next];
141 /* Buffer address translation. */
142 buff_addr = gpa_to_vva(dev, desc->addr);
144 vb_offset += vq->vhost_hlen;
148 pkt_len = rte_pktmbuf_pkt_len(buff);
149 data_len = rte_pktmbuf_data_len(buff);
150 len_to_cpy = RTE_MIN(data_len,
151 hdr ? desc->len - vq->vhost_hlen : desc->len);
152 while (total_copied < pkt_len) {
153 /* Copy mbuf data to buffer */
154 rte_memcpy((void *)(uintptr_t)(buff_addr + vb_offset),
155 rte_pktmbuf_mtod_offset(buff, const void *, offset),
157 PRINT_PACKET(dev, (uintptr_t)(buff_addr + vb_offset),
160 offset += len_to_cpy;
161 vb_offset += len_to_cpy;
162 total_copied += len_to_cpy;
164 /* The whole packet completes */
165 if (total_copied == pkt_len)
168 /* The current segment completes */
169 if (offset == data_len) {
172 data_len = rte_pktmbuf_data_len(buff);
175 /* The current vring descriptor done */
176 if (vb_offset == desc->len) {
177 if (desc->flags & VRING_DESC_F_NEXT) {
178 desc = &vq->desc[desc->next];
179 buff_addr = gpa_to_vva(dev, desc->addr);
182 /* Room in vring buffer is not enough */
187 len_to_cpy = RTE_MIN(data_len - offset, desc->len - vb_offset);
190 /* Update used ring with desc information */
191 vq->used->ring[res_cur_idx & (vq->size - 1)].id =
192 head[packet_success];
194 /* Drop the packet if it is uncompleted */
195 if (unlikely(uncompleted_pkt == 1))
196 vq->used->ring[res_cur_idx & (vq->size - 1)].len =
199 vq->used->ring[res_cur_idx & (vq->size - 1)].len =
200 pkt_len + vq->vhost_hlen;
205 if (unlikely(uncompleted_pkt == 1))
208 rte_memcpy((void *)(uintptr_t)buff_hdr_addr,
209 (const void *)&virtio_hdr, vq->vhost_hlen);
211 PRINT_PACKET(dev, (uintptr_t)buff_hdr_addr, vq->vhost_hlen, 1);
213 if (res_cur_idx < res_end_idx) {
214 /* Prefetch descriptor index. */
215 rte_prefetch0(&vq->desc[head[packet_success]]);
219 rte_compiler_barrier();
221 /* Wait until it's our turn to add our buffer to the used ring. */
222 while (unlikely(vq->last_used_idx != res_base_idx))
225 *(volatile uint16_t *)&vq->used->idx += count;
226 vq->last_used_idx = res_end_idx;
228 /* flush used->idx update before we read avail->flags. */
231 /* Kick the guest if necessary. */
232 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
233 eventfd_write((int)vq->callfd, 1);
237 static inline uint32_t __attribute__((always_inline))
238 copy_from_mbuf_to_vring(struct virtio_net *dev, uint16_t res_base_idx,
239 uint16_t res_end_idx, struct rte_mbuf *pkt)
241 uint32_t vec_idx = 0;
242 uint32_t entry_success = 0;
243 struct vhost_virtqueue *vq;
244 /* The virtio_hdr is initialised to 0. */
245 struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {
246 {0, 0, 0, 0, 0, 0}, 0};
247 uint16_t cur_idx = res_base_idx;
248 uint64_t vb_addr = 0;
249 uint64_t vb_hdr_addr = 0;
250 uint32_t seg_offset = 0;
251 uint32_t vb_offset = 0;
254 uint32_t cpy_len, entry_len;
259 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") Current Index %d| "
261 dev->device_fh, cur_idx, res_end_idx);
264 * Convert from gpa to vva
265 * (guest physical addr -> vhost virtual addr)
267 vq = dev->virtqueue[VIRTIO_RXQ];
268 vb_addr = gpa_to_vva(dev, vq->buf_vec[vec_idx].buf_addr);
269 vb_hdr_addr = vb_addr;
271 /* Prefetch buffer address. */
272 rte_prefetch0((void *)(uintptr_t)vb_addr);
274 virtio_hdr.num_buffers = res_end_idx - res_base_idx;
276 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") RX: Num merge buffers %d\n",
277 dev->device_fh, virtio_hdr.num_buffers);
279 rte_memcpy((void *)(uintptr_t)vb_hdr_addr,
280 (const void *)&virtio_hdr, vq->vhost_hlen);
282 PRINT_PACKET(dev, (uintptr_t)vb_hdr_addr, vq->vhost_hlen, 1);
284 seg_avail = rte_pktmbuf_data_len(pkt);
285 vb_offset = vq->vhost_hlen;
286 vb_avail = vq->buf_vec[vec_idx].buf_len - vq->vhost_hlen;
288 entry_len = vq->vhost_hlen;
292 vq->buf_vec[vec_idx].desc_idx;
294 if ((vq->desc[desc_idx].flags
295 & VRING_DESC_F_NEXT) == 0) {
296 /* Update used ring with desc information */
297 vq->used->ring[cur_idx & (vq->size - 1)].id
298 = vq->buf_vec[vec_idx].desc_idx;
299 vq->used->ring[cur_idx & (vq->size - 1)].len
308 vb_addr = gpa_to_vva(dev, vq->buf_vec[vec_idx].buf_addr);
310 /* Prefetch buffer address. */
311 rte_prefetch0((void *)(uintptr_t)vb_addr);
313 vb_avail = vq->buf_vec[vec_idx].buf_len;
316 cpy_len = RTE_MIN(vb_avail, seg_avail);
318 while (cpy_len > 0) {
319 /* Copy mbuf data to vring buffer */
320 rte_memcpy((void *)(uintptr_t)(vb_addr + vb_offset),
321 rte_pktmbuf_mtod_offset(pkt, const void *, seg_offset),
325 (uintptr_t)(vb_addr + vb_offset),
328 seg_offset += cpy_len;
329 vb_offset += cpy_len;
330 seg_avail -= cpy_len;
332 entry_len += cpy_len;
334 if (seg_avail != 0) {
336 * The virtio buffer in this vring
337 * entry reach to its end.
338 * But the segment doesn't complete.
340 if ((vq->desc[vq->buf_vec[vec_idx].desc_idx].flags &
341 VRING_DESC_F_NEXT) == 0) {
342 /* Update used ring with desc information */
343 vq->used->ring[cur_idx & (vq->size - 1)].id
344 = vq->buf_vec[vec_idx].desc_idx;
345 vq->used->ring[cur_idx & (vq->size - 1)].len
353 vb_addr = gpa_to_vva(dev,
354 vq->buf_vec[vec_idx].buf_addr);
356 vb_avail = vq->buf_vec[vec_idx].buf_len;
357 cpy_len = RTE_MIN(vb_avail, seg_avail);
360 * This current segment complete, need continue to
361 * check if the whole packet complete or not.
366 * There are more segments.
370 * This current buffer from vring is
371 * used up, need fetch next buffer
375 vq->buf_vec[vec_idx].desc_idx;
377 if ((vq->desc[desc_idx].flags &
378 VRING_DESC_F_NEXT) == 0) {
379 uint16_t wrapped_idx =
380 cur_idx & (vq->size - 1);
382 * Update used ring with the
383 * descriptor information
385 vq->used->ring[wrapped_idx].id
387 vq->used->ring[wrapped_idx].len
394 /* Get next buffer from buf_vec. */
396 vb_addr = gpa_to_vva(dev,
397 vq->buf_vec[vec_idx].buf_addr);
399 vq->buf_vec[vec_idx].buf_len;
404 seg_avail = rte_pktmbuf_data_len(pkt);
405 cpy_len = RTE_MIN(vb_avail, seg_avail);
408 * This whole packet completes.
410 /* Update used ring with desc information */
411 vq->used->ring[cur_idx & (vq->size - 1)].id
412 = vq->buf_vec[vec_idx].desc_idx;
413 vq->used->ring[cur_idx & (vq->size - 1)].len
421 return entry_success;
424 static inline void __attribute__((always_inline))
425 update_secure_len(struct vhost_virtqueue *vq, uint32_t id,
426 uint32_t *secure_len, uint32_t *vec_idx)
428 uint16_t wrapped_idx = id & (vq->size - 1);
429 uint32_t idx = vq->avail->ring[wrapped_idx];
431 uint32_t len = *secure_len;
432 uint32_t vec_id = *vec_idx;
436 len += vq->desc[idx].len;
437 vq->buf_vec[vec_id].buf_addr = vq->desc[idx].addr;
438 vq->buf_vec[vec_id].buf_len = vq->desc[idx].len;
439 vq->buf_vec[vec_id].desc_idx = idx;
442 if (vq->desc[idx].flags & VRING_DESC_F_NEXT) {
443 idx = vq->desc[idx].next;
453 * This function works for mergeable RX.
455 static inline uint32_t __attribute__((always_inline))
456 virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,
457 struct rte_mbuf **pkts, uint32_t count)
459 struct vhost_virtqueue *vq;
460 uint32_t pkt_idx = 0, entry_success = 0;
462 uint16_t res_base_idx, res_cur_idx;
465 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") virtio_dev_merge_rx()\n",
467 if (unlikely(queue_id != VIRTIO_RXQ)) {
468 LOG_DEBUG(VHOST_DATA, "mq isn't supported in this version.\n");
471 vq = dev->virtqueue[VIRTIO_RXQ];
472 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
477 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
478 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + vq->vhost_hlen;
482 * As many data cores may want access to available
483 * buffers, they need to be reserved.
485 uint32_t secure_len = 0;
486 uint32_t vec_idx = 0;
488 res_base_idx = vq->last_used_idx_res;
489 res_cur_idx = res_base_idx;
492 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
493 if (unlikely(res_cur_idx == avail_idx)) {
494 LOG_DEBUG(VHOST_DATA,
495 "(%"PRIu64") Failed "
496 "to get enough desc from "
501 update_secure_len(vq, res_cur_idx, &secure_len, &vec_idx);
504 } while (pkt_len > secure_len);
506 /* vq->last_used_idx_res is atomically updated. */
507 success = rte_atomic16_cmpset(&vq->last_used_idx_res,
510 } while (success == 0);
512 entry_success = copy_from_mbuf_to_vring(dev, res_base_idx,
513 res_cur_idx, pkts[pkt_idx]);
515 rte_compiler_barrier();
518 * Wait until it's our turn to add our buffer
521 while (unlikely(vq->last_used_idx != res_base_idx))
524 *(volatile uint16_t *)&vq->used->idx += entry_success;
525 vq->last_used_idx = res_cur_idx;
527 /* flush used->idx update before we read avail->flags. */
530 /* Kick the guest if necessary. */
531 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
532 eventfd_write((int)vq->callfd, 1);
539 rte_vhost_enqueue_burst(struct virtio_net *dev, uint16_t queue_id,
540 struct rte_mbuf **pkts, uint16_t count)
542 if (unlikely(dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)))
543 return virtio_dev_merge_rx(dev, queue_id, pkts, count);
545 return virtio_dev_rx(dev, queue_id, pkts, count);
549 rte_vhost_dequeue_burst(struct virtio_net *dev, uint16_t queue_id,
550 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
552 struct rte_mbuf *m, *prev;
553 struct vhost_virtqueue *vq;
554 struct vring_desc *desc;
555 uint64_t vb_addr = 0;
556 uint32_t head[MAX_PKT_BURST];
559 uint16_t free_entries, entry_success = 0;
562 if (unlikely(queue_id != VIRTIO_TXQ)) {
563 LOG_DEBUG(VHOST_DATA, "mq isn't supported in this version.\n");
567 vq = dev->virtqueue[VIRTIO_TXQ];
568 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
570 /* If there are no available buffers then return. */
571 if (vq->last_used_idx == avail_idx)
574 LOG_DEBUG(VHOST_DATA, "%s (%"PRIu64")\n", __func__,
577 /* Prefetch available ring to retrieve head indexes. */
578 rte_prefetch0(&vq->avail->ring[vq->last_used_idx & (vq->size - 1)]);
580 /*get the number of free entries in the ring*/
581 free_entries = (avail_idx - vq->last_used_idx);
583 free_entries = RTE_MIN(free_entries, count);
584 /* Limit to MAX_PKT_BURST. */
585 free_entries = RTE_MIN(free_entries, MAX_PKT_BURST);
587 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") Buffers available %d\n",
588 dev->device_fh, free_entries);
589 /* Retrieve all of the head indexes first to avoid caching issues. */
590 for (i = 0; i < free_entries; i++)
591 head[i] = vq->avail->ring[(vq->last_used_idx + i) & (vq->size - 1)];
593 /* Prefetch descriptor index. */
594 rte_prefetch0(&vq->desc[head[entry_success]]);
595 rte_prefetch0(&vq->used->ring[vq->last_used_idx & (vq->size - 1)]);
597 while (entry_success < free_entries) {
598 uint32_t vb_avail, vb_offset;
599 uint32_t seg_avail, seg_offset;
601 uint32_t seg_num = 0;
602 struct rte_mbuf *cur;
603 uint8_t alloc_err = 0;
605 desc = &vq->desc[head[entry_success]];
607 /* Discard first buffer as it is the virtio header */
608 if (desc->flags & VRING_DESC_F_NEXT) {
609 desc = &vq->desc[desc->next];
611 vb_avail = desc->len;
613 vb_offset = vq->vhost_hlen;
614 vb_avail = desc->len - vb_offset;
617 /* Buffer address translation. */
618 vb_addr = gpa_to_vva(dev, desc->addr);
619 /* Prefetch buffer address. */
620 rte_prefetch0((void *)(uintptr_t)vb_addr);
622 used_idx = vq->last_used_idx & (vq->size - 1);
624 if (entry_success < (free_entries - 1)) {
625 /* Prefetch descriptor index. */
626 rte_prefetch0(&vq->desc[head[entry_success+1]]);
627 rte_prefetch0(&vq->used->ring[(used_idx + 1) & (vq->size - 1)]);
630 /* Update used index buffer information. */
631 vq->used->ring[used_idx].id = head[entry_success];
632 vq->used->ring[used_idx].len = 0;
634 /* Allocate an mbuf and populate the structure. */
635 m = rte_pktmbuf_alloc(mbuf_pool);
636 if (unlikely(m == NULL)) {
637 RTE_LOG(ERR, VHOST_DATA,
638 "Failed to allocate memory for mbuf.\n");
642 seg_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
643 cpy_len = RTE_MIN(vb_avail, seg_avail);
645 PRINT_PACKET(dev, (uintptr_t)vb_addr, desc->len, 0);
650 while (cpy_len != 0) {
651 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *, seg_offset),
652 (void *)((uintptr_t)(vb_addr + vb_offset)),
655 seg_offset += cpy_len;
656 vb_offset += cpy_len;
658 seg_avail -= cpy_len;
662 * The segment reachs to its end,
663 * while the virtio buffer in TX vring has
664 * more data to be copied.
666 cur->data_len = seg_offset;
667 m->pkt_len += seg_offset;
668 /* Allocate mbuf and populate the structure. */
669 cur = rte_pktmbuf_alloc(mbuf_pool);
670 if (unlikely(cur == NULL)) {
671 RTE_LOG(ERR, VHOST_DATA, "Failed to "
672 "allocate memory for mbuf.\n");
682 seg_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
684 if (desc->flags & VRING_DESC_F_NEXT) {
686 * There are more virtio buffers in
687 * same vring entry need to be copied.
689 if (seg_avail == 0) {
691 * The current segment hasn't
692 * room to accomodate more
695 cur->data_len = seg_offset;
696 m->pkt_len += seg_offset;
698 * Allocate an mbuf and
699 * populate the structure.
701 cur = rte_pktmbuf_alloc(mbuf_pool);
702 if (unlikely(cur == NULL)) {
716 seg_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
719 desc = &vq->desc[desc->next];
721 /* Buffer address translation. */
722 vb_addr = gpa_to_vva(dev, desc->addr);
723 /* Prefetch buffer address. */
724 rte_prefetch0((void *)(uintptr_t)vb_addr);
726 vb_avail = desc->len;
728 PRINT_PACKET(dev, (uintptr_t)vb_addr,
731 /* The whole packet completes. */
732 cur->data_len = seg_offset;
733 m->pkt_len += seg_offset;
738 cpy_len = RTE_MIN(vb_avail, seg_avail);
741 if (unlikely(alloc_err == 1))
744 m->nb_segs = seg_num;
746 pkts[entry_success] = m;
751 rte_compiler_barrier();
752 vq->used->idx += entry_success;
753 /* Kick guest if required. */
754 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
755 eventfd_write((int)vq->callfd, 1);
756 return entry_success;