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.
36 #include <linux/virtio_net.h>
39 #include <rte_memcpy.h>
40 #include <rte_virtio_net.h>
42 #include "vhost-net.h"
44 #define MAX_PKT_BURST 32
47 is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t qp_nb)
49 return (is_tx ^ (idx & 1)) == 0 && idx < qp_nb * VIRTIO_QNUM;
53 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
54 * be received from the physical port or from another virtio device. A packet
55 * count is returned to indicate the number of packets that are succesfully
56 * added to the RX queue. This function works when the mbuf is scattered, but
57 * it doesn't support the mergeable feature.
59 static inline uint32_t __attribute__((always_inline))
60 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
61 struct rte_mbuf **pkts, uint32_t count)
63 struct vhost_virtqueue *vq;
64 struct vring_desc *desc;
65 struct rte_mbuf *buff;
66 /* The virtio_hdr is initialised to 0. */
67 struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0, 0, 0, 0, 0, 0}, 0};
68 uint64_t buff_addr = 0;
69 uint64_t buff_hdr_addr = 0;
70 uint32_t head[MAX_PKT_BURST];
71 uint32_t head_idx, packet_success = 0;
72 uint16_t avail_idx, res_cur_idx;
73 uint16_t res_base_idx, res_end_idx;
74 uint16_t free_entries;
77 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") virtio_dev_rx()\n", dev->device_fh);
78 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->virt_qp_nb))) {
79 RTE_LOG(ERR, VHOST_DATA,
80 "%s (%"PRIu64"): virtqueue idx:%d invalid.\n",
81 __func__, dev->device_fh, queue_id);
85 vq = dev->virtqueue[queue_id];
86 count = (count > MAX_PKT_BURST) ? MAX_PKT_BURST : count;
89 * As many data cores may want access to available buffers,
90 * they need to be reserved.
93 res_base_idx = vq->last_used_idx_res;
94 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
96 free_entries = (avail_idx - res_base_idx);
97 /*check that we have enough buffers*/
98 if (unlikely(count > free_entries))
104 res_end_idx = res_base_idx + count;
105 /* vq->last_used_idx_res is atomically updated. */
106 /* TODO: Allow to disable cmpset if no concurrency in application. */
107 success = rte_atomic16_cmpset(&vq->last_used_idx_res,
108 res_base_idx, res_end_idx);
109 } while (unlikely(success == 0));
110 res_cur_idx = res_base_idx;
111 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") Current Index %d| End Index %d\n",
112 dev->device_fh, res_cur_idx, res_end_idx);
114 /* Prefetch available ring to retrieve indexes. */
115 rte_prefetch0(&vq->avail->ring[res_cur_idx & (vq->size - 1)]);
117 /* Retrieve all of the head indexes first to avoid caching issues. */
118 for (head_idx = 0; head_idx < count; head_idx++)
119 head[head_idx] = vq->avail->ring[(res_cur_idx + head_idx) &
122 /*Prefetch descriptor index. */
123 rte_prefetch0(&vq->desc[head[packet_success]]);
125 while (res_cur_idx != res_end_idx) {
126 uint32_t offset = 0, vb_offset = 0;
127 uint32_t pkt_len, len_to_cpy, data_len, total_copied = 0;
128 uint8_t hdr = 0, uncompleted_pkt = 0;
130 /* Get descriptor from available ring */
131 desc = &vq->desc[head[packet_success]];
133 buff = pkts[packet_success];
135 /* Convert from gpa to vva (guest physical addr -> vhost virtual addr) */
136 buff_addr = gpa_to_vva(dev, desc->addr);
137 /* Prefetch buffer address. */
138 rte_prefetch0((void *)(uintptr_t)buff_addr);
140 /* Copy virtio_hdr to packet and increment buffer address */
141 buff_hdr_addr = buff_addr;
144 * If the descriptors are chained the header and data are
145 * placed in separate buffers.
147 if ((desc->flags & VRING_DESC_F_NEXT) &&
148 (desc->len == vq->vhost_hlen)) {
149 desc = &vq->desc[desc->next];
150 /* Buffer address translation. */
151 buff_addr = gpa_to_vva(dev, desc->addr);
153 vb_offset += vq->vhost_hlen;
157 pkt_len = rte_pktmbuf_pkt_len(buff);
158 data_len = rte_pktmbuf_data_len(buff);
159 len_to_cpy = RTE_MIN(data_len,
160 hdr ? desc->len - vq->vhost_hlen : desc->len);
161 while (total_copied < pkt_len) {
162 /* Copy mbuf data to buffer */
163 rte_memcpy((void *)(uintptr_t)(buff_addr + vb_offset),
164 rte_pktmbuf_mtod_offset(buff, const void *, offset),
166 PRINT_PACKET(dev, (uintptr_t)(buff_addr + vb_offset),
169 offset += len_to_cpy;
170 vb_offset += len_to_cpy;
171 total_copied += len_to_cpy;
173 /* The whole packet completes */
174 if (total_copied == pkt_len)
177 /* The current segment completes */
178 if (offset == data_len) {
181 data_len = rte_pktmbuf_data_len(buff);
184 /* The current vring descriptor done */
185 if (vb_offset == desc->len) {
186 if (desc->flags & VRING_DESC_F_NEXT) {
187 desc = &vq->desc[desc->next];
188 buff_addr = gpa_to_vva(dev, desc->addr);
191 /* Room in vring buffer is not enough */
196 len_to_cpy = RTE_MIN(data_len - offset, desc->len - vb_offset);
199 /* Update used ring with desc information */
200 vq->used->ring[res_cur_idx & (vq->size - 1)].id =
201 head[packet_success];
203 /* Drop the packet if it is uncompleted */
204 if (unlikely(uncompleted_pkt == 1))
205 vq->used->ring[res_cur_idx & (vq->size - 1)].len =
208 vq->used->ring[res_cur_idx & (vq->size - 1)].len =
209 pkt_len + vq->vhost_hlen;
214 if (unlikely(uncompleted_pkt == 1))
217 rte_memcpy((void *)(uintptr_t)buff_hdr_addr,
218 (const void *)&virtio_hdr, vq->vhost_hlen);
220 PRINT_PACKET(dev, (uintptr_t)buff_hdr_addr, vq->vhost_hlen, 1);
222 if (res_cur_idx < res_end_idx) {
223 /* Prefetch descriptor index. */
224 rte_prefetch0(&vq->desc[head[packet_success]]);
228 rte_compiler_barrier();
230 /* Wait until it's our turn to add our buffer to the used ring. */
231 while (unlikely(vq->last_used_idx != res_base_idx))
234 *(volatile uint16_t *)&vq->used->idx += count;
235 vq->last_used_idx = res_end_idx;
237 /* flush used->idx update before we read avail->flags. */
240 /* Kick the guest if necessary. */
241 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
242 eventfd_write(vq->callfd, (eventfd_t)1);
246 static inline uint32_t __attribute__((always_inline))
247 copy_from_mbuf_to_vring(struct virtio_net *dev, uint32_t queue_id,
248 uint16_t res_base_idx, uint16_t res_end_idx,
249 struct rte_mbuf *pkt)
251 uint32_t vec_idx = 0;
252 uint32_t entry_success = 0;
253 struct vhost_virtqueue *vq;
254 /* The virtio_hdr is initialised to 0. */
255 struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {
256 {0, 0, 0, 0, 0, 0}, 0};
257 uint16_t cur_idx = res_base_idx;
258 uint64_t vb_addr = 0;
259 uint64_t vb_hdr_addr = 0;
260 uint32_t seg_offset = 0;
261 uint32_t vb_offset = 0;
264 uint32_t cpy_len, entry_len;
269 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") Current Index %d| "
271 dev->device_fh, cur_idx, res_end_idx);
274 * Convert from gpa to vva
275 * (guest physical addr -> vhost virtual addr)
277 vq = dev->virtqueue[queue_id];
278 vb_addr = gpa_to_vva(dev, vq->buf_vec[vec_idx].buf_addr);
279 vb_hdr_addr = vb_addr;
281 /* Prefetch buffer address. */
282 rte_prefetch0((void *)(uintptr_t)vb_addr);
284 virtio_hdr.num_buffers = res_end_idx - res_base_idx;
286 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") RX: Num merge buffers %d\n",
287 dev->device_fh, virtio_hdr.num_buffers);
289 rte_memcpy((void *)(uintptr_t)vb_hdr_addr,
290 (const void *)&virtio_hdr, vq->vhost_hlen);
292 PRINT_PACKET(dev, (uintptr_t)vb_hdr_addr, vq->vhost_hlen, 1);
294 seg_avail = rte_pktmbuf_data_len(pkt);
295 vb_offset = vq->vhost_hlen;
296 vb_avail = vq->buf_vec[vec_idx].buf_len - vq->vhost_hlen;
298 entry_len = vq->vhost_hlen;
302 vq->buf_vec[vec_idx].desc_idx;
304 if ((vq->desc[desc_idx].flags
305 & VRING_DESC_F_NEXT) == 0) {
306 /* Update used ring with desc information */
307 vq->used->ring[cur_idx & (vq->size - 1)].id
308 = vq->buf_vec[vec_idx].desc_idx;
309 vq->used->ring[cur_idx & (vq->size - 1)].len
318 vb_addr = gpa_to_vva(dev, vq->buf_vec[vec_idx].buf_addr);
320 /* Prefetch buffer address. */
321 rte_prefetch0((void *)(uintptr_t)vb_addr);
323 vb_avail = vq->buf_vec[vec_idx].buf_len;
326 cpy_len = RTE_MIN(vb_avail, seg_avail);
328 while (cpy_len > 0) {
329 /* Copy mbuf data to vring buffer */
330 rte_memcpy((void *)(uintptr_t)(vb_addr + vb_offset),
331 rte_pktmbuf_mtod_offset(pkt, const void *, seg_offset),
335 (uintptr_t)(vb_addr + vb_offset),
338 seg_offset += cpy_len;
339 vb_offset += cpy_len;
340 seg_avail -= cpy_len;
342 entry_len += cpy_len;
344 if (seg_avail != 0) {
346 * The virtio buffer in this vring
347 * entry reach to its end.
348 * But the segment doesn't complete.
350 if ((vq->desc[vq->buf_vec[vec_idx].desc_idx].flags &
351 VRING_DESC_F_NEXT) == 0) {
352 /* Update used ring with desc information */
353 vq->used->ring[cur_idx & (vq->size - 1)].id
354 = vq->buf_vec[vec_idx].desc_idx;
355 vq->used->ring[cur_idx & (vq->size - 1)].len
363 vb_addr = gpa_to_vva(dev,
364 vq->buf_vec[vec_idx].buf_addr);
366 vb_avail = vq->buf_vec[vec_idx].buf_len;
367 cpy_len = RTE_MIN(vb_avail, seg_avail);
370 * This current segment complete, need continue to
371 * check if the whole packet complete or not.
376 * There are more segments.
380 * This current buffer from vring is
381 * used up, need fetch next buffer
385 vq->buf_vec[vec_idx].desc_idx;
387 if ((vq->desc[desc_idx].flags &
388 VRING_DESC_F_NEXT) == 0) {
389 uint16_t wrapped_idx =
390 cur_idx & (vq->size - 1);
392 * Update used ring with the
393 * descriptor information
395 vq->used->ring[wrapped_idx].id
397 vq->used->ring[wrapped_idx].len
404 /* Get next buffer from buf_vec. */
406 vb_addr = gpa_to_vva(dev,
407 vq->buf_vec[vec_idx].buf_addr);
409 vq->buf_vec[vec_idx].buf_len;
414 seg_avail = rte_pktmbuf_data_len(pkt);
415 cpy_len = RTE_MIN(vb_avail, seg_avail);
418 * This whole packet completes.
420 /* Update used ring with desc information */
421 vq->used->ring[cur_idx & (vq->size - 1)].id
422 = vq->buf_vec[vec_idx].desc_idx;
423 vq->used->ring[cur_idx & (vq->size - 1)].len
431 return entry_success;
434 static inline void __attribute__((always_inline))
435 update_secure_len(struct vhost_virtqueue *vq, uint32_t id,
436 uint32_t *secure_len, uint32_t *vec_idx)
438 uint16_t wrapped_idx = id & (vq->size - 1);
439 uint32_t idx = vq->avail->ring[wrapped_idx];
441 uint32_t len = *secure_len;
442 uint32_t vec_id = *vec_idx;
446 len += vq->desc[idx].len;
447 vq->buf_vec[vec_id].buf_addr = vq->desc[idx].addr;
448 vq->buf_vec[vec_id].buf_len = vq->desc[idx].len;
449 vq->buf_vec[vec_id].desc_idx = idx;
452 if (vq->desc[idx].flags & VRING_DESC_F_NEXT) {
453 idx = vq->desc[idx].next;
463 * This function works for mergeable RX.
465 static inline uint32_t __attribute__((always_inline))
466 virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,
467 struct rte_mbuf **pkts, uint32_t count)
469 struct vhost_virtqueue *vq;
470 uint32_t pkt_idx = 0, entry_success = 0;
472 uint16_t res_base_idx, res_cur_idx;
475 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") virtio_dev_merge_rx()\n",
477 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->virt_qp_nb))) {
478 RTE_LOG(ERR, VHOST_DATA,
479 "%s (%"PRIu64"): virtqueue idx:%d invalid.\n",
480 __func__, dev->device_fh, queue_id);
484 vq = dev->virtqueue[queue_id];
485 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
490 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
491 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + vq->vhost_hlen;
495 * As many data cores may want access to available
496 * buffers, they need to be reserved.
498 uint32_t secure_len = 0;
499 uint32_t vec_idx = 0;
501 res_base_idx = vq->last_used_idx_res;
502 res_cur_idx = res_base_idx;
505 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
506 if (unlikely(res_cur_idx == avail_idx)) {
507 LOG_DEBUG(VHOST_DATA,
508 "(%"PRIu64") Failed "
509 "to get enough desc from "
514 update_secure_len(vq, res_cur_idx, &secure_len, &vec_idx);
517 } while (pkt_len > secure_len);
519 /* vq->last_used_idx_res is atomically updated. */
520 success = rte_atomic16_cmpset(&vq->last_used_idx_res,
523 } while (success == 0);
525 entry_success = copy_from_mbuf_to_vring(dev, queue_id,
526 res_base_idx, res_cur_idx, pkts[pkt_idx]);
528 rte_compiler_barrier();
531 * Wait until it's our turn to add our buffer
534 while (unlikely(vq->last_used_idx != res_base_idx))
537 *(volatile uint16_t *)&vq->used->idx += entry_success;
538 vq->last_used_idx = res_cur_idx;
542 if (likely(pkt_idx)) {
543 /* flush used->idx update before we read avail->flags. */
546 /* Kick the guest if necessary. */
547 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
548 eventfd_write(vq->callfd, (eventfd_t)1);
555 rte_vhost_enqueue_burst(struct virtio_net *dev, uint16_t queue_id,
556 struct rte_mbuf **pkts, uint16_t count)
558 if (unlikely(dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)))
559 return virtio_dev_merge_rx(dev, queue_id, pkts, count);
561 return virtio_dev_rx(dev, queue_id, pkts, count);
565 rte_vhost_dequeue_burst(struct virtio_net *dev, uint16_t queue_id,
566 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
568 struct rte_mbuf *m, *prev;
569 struct vhost_virtqueue *vq;
570 struct vring_desc *desc;
571 uint64_t vb_addr = 0;
572 uint32_t head[MAX_PKT_BURST];
575 uint16_t free_entries, entry_success = 0;
578 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->virt_qp_nb))) {
579 RTE_LOG(ERR, VHOST_DATA,
580 "%s (%"PRIu64"): virtqueue idx:%d invalid.\n",
581 __func__, dev->device_fh, queue_id);
585 vq = dev->virtqueue[queue_id];
586 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
588 /* If there are no available buffers then return. */
589 if (vq->last_used_idx == avail_idx)
592 LOG_DEBUG(VHOST_DATA, "%s (%"PRIu64")\n", __func__,
595 /* Prefetch available ring to retrieve head indexes. */
596 rte_prefetch0(&vq->avail->ring[vq->last_used_idx & (vq->size - 1)]);
598 /*get the number of free entries in the ring*/
599 free_entries = (avail_idx - vq->last_used_idx);
601 free_entries = RTE_MIN(free_entries, count);
602 /* Limit to MAX_PKT_BURST. */
603 free_entries = RTE_MIN(free_entries, MAX_PKT_BURST);
605 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") Buffers available %d\n",
606 dev->device_fh, free_entries);
607 /* Retrieve all of the head indexes first to avoid caching issues. */
608 for (i = 0; i < free_entries; i++)
609 head[i] = vq->avail->ring[(vq->last_used_idx + i) & (vq->size - 1)];
611 /* Prefetch descriptor index. */
612 rte_prefetch0(&vq->desc[head[entry_success]]);
613 rte_prefetch0(&vq->used->ring[vq->last_used_idx & (vq->size - 1)]);
615 while (entry_success < free_entries) {
616 uint32_t vb_avail, vb_offset;
617 uint32_t seg_avail, seg_offset;
619 uint32_t seg_num = 0;
620 struct rte_mbuf *cur;
621 uint8_t alloc_err = 0;
623 desc = &vq->desc[head[entry_success]];
625 /* Discard first buffer as it is the virtio header */
626 if (desc->flags & VRING_DESC_F_NEXT) {
627 desc = &vq->desc[desc->next];
629 vb_avail = desc->len;
631 vb_offset = vq->vhost_hlen;
632 vb_avail = desc->len - vb_offset;
635 /* Buffer address translation. */
636 vb_addr = gpa_to_vva(dev, desc->addr);
637 /* Prefetch buffer address. */
638 rte_prefetch0((void *)(uintptr_t)vb_addr);
640 used_idx = vq->last_used_idx & (vq->size - 1);
642 if (entry_success < (free_entries - 1)) {
643 /* Prefetch descriptor index. */
644 rte_prefetch0(&vq->desc[head[entry_success+1]]);
645 rte_prefetch0(&vq->used->ring[(used_idx + 1) & (vq->size - 1)]);
648 /* Update used index buffer information. */
649 vq->used->ring[used_idx].id = head[entry_success];
650 vq->used->ring[used_idx].len = 0;
652 /* Allocate an mbuf and populate the structure. */
653 m = rte_pktmbuf_alloc(mbuf_pool);
654 if (unlikely(m == NULL)) {
655 RTE_LOG(ERR, VHOST_DATA,
656 "Failed to allocate memory for mbuf.\n");
660 seg_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
661 cpy_len = RTE_MIN(vb_avail, seg_avail);
663 PRINT_PACKET(dev, (uintptr_t)vb_addr, desc->len, 0);
668 while (cpy_len != 0) {
669 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *, seg_offset),
670 (void *)((uintptr_t)(vb_addr + vb_offset)),
673 seg_offset += cpy_len;
674 vb_offset += cpy_len;
676 seg_avail -= cpy_len;
680 * The segment reachs to its end,
681 * while the virtio buffer in TX vring has
682 * more data to be copied.
684 cur->data_len = seg_offset;
685 m->pkt_len += seg_offset;
686 /* Allocate mbuf and populate the structure. */
687 cur = rte_pktmbuf_alloc(mbuf_pool);
688 if (unlikely(cur == NULL)) {
689 RTE_LOG(ERR, VHOST_DATA, "Failed to "
690 "allocate memory for mbuf.\n");
700 seg_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
702 if (desc->flags & VRING_DESC_F_NEXT) {
704 * There are more virtio buffers in
705 * same vring entry need to be copied.
707 if (seg_avail == 0) {
709 * The current segment hasn't
710 * room to accomodate more
713 cur->data_len = seg_offset;
714 m->pkt_len += seg_offset;
716 * Allocate an mbuf and
717 * populate the structure.
719 cur = rte_pktmbuf_alloc(mbuf_pool);
720 if (unlikely(cur == NULL)) {
734 seg_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
737 desc = &vq->desc[desc->next];
739 /* Buffer address translation. */
740 vb_addr = gpa_to_vva(dev, desc->addr);
741 /* Prefetch buffer address. */
742 rte_prefetch0((void *)(uintptr_t)vb_addr);
744 vb_avail = desc->len;
746 PRINT_PACKET(dev, (uintptr_t)vb_addr,
749 /* The whole packet completes. */
750 cur->data_len = seg_offset;
751 m->pkt_len += seg_offset;
756 cpy_len = RTE_MIN(vb_avail, seg_avail);
759 if (unlikely(alloc_err == 1))
762 m->nb_segs = seg_num;
764 pkts[entry_success] = m;
769 rte_compiler_barrier();
770 vq->used->idx += entry_success;
771 /* Kick guest if required. */
772 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
773 eventfd_write(vq->callfd, (eventfd_t)1);
774 return entry_success;