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 (const void *)(rte_pktmbuf_mtod(buff, const char *) + 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];
269 gpa_to_vva(dev, vq->buf_vec[vec_idx].buf_addr);
270 vb_hdr_addr = vb_addr;
272 /* Prefetch buffer address. */
273 rte_prefetch0((void *)(uintptr_t)vb_addr);
275 virtio_hdr.num_buffers = res_end_idx - res_base_idx;
277 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") RX: Num merge buffers %d\n",
278 dev->device_fh, virtio_hdr.num_buffers);
280 rte_memcpy((void *)(uintptr_t)vb_hdr_addr,
281 (const void *)&virtio_hdr, vq->vhost_hlen);
283 PRINT_PACKET(dev, (uintptr_t)vb_hdr_addr, vq->vhost_hlen, 1);
285 seg_avail = rte_pktmbuf_data_len(pkt);
286 vb_offset = vq->vhost_hlen;
288 vq->buf_vec[vec_idx].buf_len - vq->vhost_hlen;
290 entry_len = vq->vhost_hlen;
294 vq->buf_vec[vec_idx].desc_idx;
295 vq->desc[desc_idx].len = vq->vhost_hlen;
297 if ((vq->desc[desc_idx].flags
298 & VRING_DESC_F_NEXT) == 0) {
299 /* Update used ring with desc information */
300 vq->used->ring[cur_idx & (vq->size - 1)].id
301 = vq->buf_vec[vec_idx].desc_idx;
302 vq->used->ring[cur_idx & (vq->size - 1)].len
312 gpa_to_vva(dev, vq->buf_vec[vec_idx].buf_addr);
314 /* Prefetch buffer address. */
315 rte_prefetch0((void *)(uintptr_t)vb_addr);
317 vb_avail = vq->buf_vec[vec_idx].buf_len;
320 cpy_len = RTE_MIN(vb_avail, seg_avail);
322 while (cpy_len > 0) {
323 /* Copy mbuf data to vring buffer */
324 rte_memcpy((void *)(uintptr_t)(vb_addr + vb_offset),
325 (const void *)(rte_pktmbuf_mtod(pkt, char*) + seg_offset),
329 (uintptr_t)(vb_addr + vb_offset),
332 seg_offset += cpy_len;
333 vb_offset += cpy_len;
334 seg_avail -= cpy_len;
336 entry_len += cpy_len;
338 if (seg_avail != 0) {
340 * The virtio buffer in this vring
341 * entry reach to its end.
342 * But the segment doesn't complete.
344 if ((vq->desc[vq->buf_vec[vec_idx].desc_idx].flags &
345 VRING_DESC_F_NEXT) == 0) {
346 /* Update used ring with desc information */
347 vq->used->ring[cur_idx & (vq->size - 1)].id
348 = vq->buf_vec[vec_idx].desc_idx;
349 vq->used->ring[cur_idx & (vq->size - 1)].len
357 vb_addr = gpa_to_vva(dev,
358 vq->buf_vec[vec_idx].buf_addr);
360 vb_avail = vq->buf_vec[vec_idx].buf_len;
361 cpy_len = RTE_MIN(vb_avail, seg_avail);
364 * This current segment complete, need continue to
365 * check if the whole packet complete or not.
370 * There are more segments.
374 * This current buffer from vring is
375 * used up, need fetch next buffer
379 vq->buf_vec[vec_idx].desc_idx;
380 vq->desc[desc_idx].len = vb_offset;
382 if ((vq->desc[desc_idx].flags &
383 VRING_DESC_F_NEXT) == 0) {
384 uint16_t wrapped_idx =
385 cur_idx & (vq->size - 1);
387 * Update used ring with the
388 * descriptor information
390 vq->used->ring[wrapped_idx].id
392 vq->used->ring[wrapped_idx].len
399 /* Get next buffer from buf_vec. */
401 vb_addr = gpa_to_vva(dev,
402 vq->buf_vec[vec_idx].buf_addr);
404 vq->buf_vec[vec_idx].buf_len;
409 seg_avail = rte_pktmbuf_data_len(pkt);
410 cpy_len = RTE_MIN(vb_avail, seg_avail);
413 * This whole packet completes.
416 vq->buf_vec[vec_idx].desc_idx;
417 vq->desc[desc_idx].len = vb_offset;
419 while (vq->desc[desc_idx].flags &
421 desc_idx = vq->desc[desc_idx].next;
422 vq->desc[desc_idx].len = 0;
425 /* Update used ring with desc information */
426 vq->used->ring[cur_idx & (vq->size - 1)].id
427 = vq->buf_vec[vec_idx].desc_idx;
428 vq->used->ring[cur_idx & (vq->size - 1)].len
434 cpy_len = RTE_MIN(vb_avail, seg_avail);
439 return entry_success;
443 * This function works for mergeable RX.
445 static inline uint32_t __attribute__((always_inline))
446 virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,
447 struct rte_mbuf **pkts, uint32_t count)
449 struct vhost_virtqueue *vq;
450 uint32_t pkt_idx = 0, entry_success = 0;
451 uint16_t avail_idx, res_cur_idx;
452 uint16_t res_base_idx, res_end_idx;
455 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") virtio_dev_merge_rx()\n",
457 if (unlikely(queue_id != VIRTIO_RXQ)) {
458 LOG_DEBUG(VHOST_DATA, "mq isn't supported in this version.\n");
461 vq = dev->virtqueue[VIRTIO_RXQ];
462 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
467 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
468 uint32_t secure_len = 0;
470 uint32_t vec_idx = 0;
471 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + vq->vhost_hlen;
476 * As many data cores may want access to available
477 * buffers, they need to be reserved.
479 res_base_idx = vq->last_used_idx_res;
480 res_cur_idx = res_base_idx;
483 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
484 if (unlikely(res_cur_idx == avail_idx)) {
485 LOG_DEBUG(VHOST_DATA,
486 "(%"PRIu64") Failed "
487 "to get enough desc from "
492 uint16_t wrapped_idx =
493 (res_cur_idx) & (vq->size - 1);
495 vq->avail->ring[wrapped_idx];
500 secure_len += vq->desc[idx].len;
501 if (vq->desc[idx].flags &
503 idx = vq->desc[idx].next;
510 } while (pkt_len > secure_len);
512 /* vq->last_used_idx_res is atomically updated. */
513 success = rte_atomic16_cmpset(&vq->last_used_idx_res,
516 } while (success == 0);
519 need_cnt = res_cur_idx - res_base_idx;
521 for (i = 0; i < need_cnt; i++, id++) {
522 uint16_t wrapped_idx = id & (vq->size - 1);
523 uint32_t idx = vq->avail->ring[wrapped_idx];
527 vq->buf_vec[vec_idx].buf_addr =
529 vq->buf_vec[vec_idx].buf_len =
531 vq->buf_vec[vec_idx].desc_idx = idx;
534 if (vq->desc[idx].flags & VRING_DESC_F_NEXT) {
535 idx = vq->desc[idx].next;
541 res_end_idx = res_cur_idx;
543 entry_success = copy_from_mbuf_to_vring(dev, res_base_idx,
544 res_end_idx, pkts[pkt_idx]);
546 rte_compiler_barrier();
549 * Wait until it's our turn to add our buffer
552 while (unlikely(vq->last_used_idx != res_base_idx))
555 *(volatile uint16_t *)&vq->used->idx += entry_success;
556 vq->last_used_idx = res_end_idx;
558 /* flush used->idx update before we read avail->flags. */
561 /* Kick the guest if necessary. */
562 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
563 eventfd_write((int)vq->callfd, 1);
570 rte_vhost_enqueue_burst(struct virtio_net *dev, uint16_t queue_id,
571 struct rte_mbuf **pkts, uint16_t count)
573 if (unlikely(dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)))
574 return virtio_dev_merge_rx(dev, queue_id, pkts, count);
576 return virtio_dev_rx(dev, queue_id, pkts, count);
580 rte_vhost_dequeue_burst(struct virtio_net *dev, uint16_t queue_id,
581 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
583 struct rte_mbuf *m, *prev;
584 struct vhost_virtqueue *vq;
585 struct vring_desc *desc;
586 uint64_t vb_addr = 0;
587 uint32_t head[MAX_PKT_BURST];
590 uint16_t free_entries, entry_success = 0;
593 if (unlikely(queue_id != VIRTIO_TXQ)) {
594 LOG_DEBUG(VHOST_DATA, "mq isn't supported in this version.\n");
598 vq = dev->virtqueue[VIRTIO_TXQ];
599 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
601 /* If there are no available buffers then return. */
602 if (vq->last_used_idx == avail_idx)
605 LOG_DEBUG(VHOST_DATA, "%s (%"PRIu64")\n", __func__,
608 /* Prefetch available ring to retrieve head indexes. */
609 rte_prefetch0(&vq->avail->ring[vq->last_used_idx & (vq->size - 1)]);
611 /*get the number of free entries in the ring*/
612 free_entries = (avail_idx - vq->last_used_idx);
614 free_entries = RTE_MIN(free_entries, count);
615 /* Limit to MAX_PKT_BURST. */
616 free_entries = RTE_MIN(free_entries, MAX_PKT_BURST);
618 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") Buffers available %d\n",
619 dev->device_fh, free_entries);
620 /* Retrieve all of the head indexes first to avoid caching issues. */
621 for (i = 0; i < free_entries; i++)
622 head[i] = vq->avail->ring[(vq->last_used_idx + i) & (vq->size - 1)];
624 /* Prefetch descriptor index. */
625 rte_prefetch0(&vq->desc[head[entry_success]]);
626 rte_prefetch0(&vq->used->ring[vq->last_used_idx & (vq->size - 1)]);
628 while (entry_success < free_entries) {
629 uint32_t vb_avail, vb_offset;
630 uint32_t seg_avail, seg_offset;
632 uint32_t seg_num = 0;
633 struct rte_mbuf *cur;
634 uint8_t alloc_err = 0;
636 desc = &vq->desc[head[entry_success]];
638 /* Discard first buffer as it is the virtio header */
639 if (desc->flags & VRING_DESC_F_NEXT) {
640 desc = &vq->desc[desc->next];
642 vb_avail = desc->len;
644 vb_offset = vq->vhost_hlen;
645 vb_avail = desc->len - vb_offset;
648 /* Buffer address translation. */
649 vb_addr = gpa_to_vva(dev, desc->addr);
650 /* Prefetch buffer address. */
651 rte_prefetch0((void *)(uintptr_t)vb_addr);
653 used_idx = vq->last_used_idx & (vq->size - 1);
655 if (entry_success < (free_entries - 1)) {
656 /* Prefetch descriptor index. */
657 rte_prefetch0(&vq->desc[head[entry_success+1]]);
658 rte_prefetch0(&vq->used->ring[(used_idx + 1) & (vq->size - 1)]);
661 /* Update used index buffer information. */
662 vq->used->ring[used_idx].id = head[entry_success];
663 vq->used->ring[used_idx].len = 0;
665 /* Allocate an mbuf and populate the structure. */
666 m = rte_pktmbuf_alloc(mbuf_pool);
667 if (unlikely(m == NULL)) {
668 RTE_LOG(ERR, VHOST_DATA,
669 "Failed to allocate memory for mbuf.\n");
673 seg_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
674 cpy_len = RTE_MIN(vb_avail, seg_avail);
676 PRINT_PACKET(dev, (uintptr_t)vb_addr, desc->len, 0);
681 while (cpy_len != 0) {
682 rte_memcpy((void *)(rte_pktmbuf_mtod(cur, char *) + seg_offset),
683 (void *)((uintptr_t)(vb_addr + vb_offset)),
686 seg_offset += cpy_len;
687 vb_offset += cpy_len;
689 seg_avail -= cpy_len;
693 * The segment reachs to its end,
694 * while the virtio buffer in TX vring has
695 * more data to be copied.
697 cur->data_len = seg_offset;
698 m->pkt_len += seg_offset;
699 /* Allocate mbuf and populate the structure. */
700 cur = rte_pktmbuf_alloc(mbuf_pool);
701 if (unlikely(cur == NULL)) {
702 RTE_LOG(ERR, VHOST_DATA, "Failed to "
703 "allocate memory for mbuf.\n");
713 seg_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
715 if (desc->flags & VRING_DESC_F_NEXT) {
717 * There are more virtio buffers in
718 * same vring entry need to be copied.
720 if (seg_avail == 0) {
722 * The current segment hasn't
723 * room to accomodate more
726 cur->data_len = seg_offset;
727 m->pkt_len += seg_offset;
729 * Allocate an mbuf and
730 * populate the structure.
732 cur = rte_pktmbuf_alloc(mbuf_pool);
733 if (unlikely(cur == NULL)) {
747 seg_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
750 desc = &vq->desc[desc->next];
752 /* Buffer address translation. */
753 vb_addr = gpa_to_vva(dev, desc->addr);
754 /* Prefetch buffer address. */
755 rte_prefetch0((void *)(uintptr_t)vb_addr);
757 vb_avail = desc->len;
759 PRINT_PACKET(dev, (uintptr_t)vb_addr,
762 /* The whole packet completes. */
763 cur->data_len = seg_offset;
764 m->pkt_len += seg_offset;
769 cpy_len = RTE_MIN(vb_avail, seg_avail);
772 if (unlikely(alloc_err == 1))
775 m->nb_segs = seg_num;
777 pkts[entry_success] = m;
782 rte_compiler_barrier();
783 vq->used->idx += entry_success;
784 /* Kick guest if required. */
785 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
786 eventfd_write((int)vq->callfd, 1);
787 return entry_success;