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-cdev.h"
43 #define MAX_PKT_BURST 32 /* Max burst size for RX/TX */
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 were succesfully
49 * added to the RX queue. This function works when mergeable is disabled.
51 static inline uint32_t __attribute__((always_inline))
52 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id, struct rte_mbuf **pkts, uint32_t count)
54 struct vhost_virtqueue *vq;
55 struct vring_desc *desc;
56 struct rte_mbuf *buff;
57 /* The virtio_hdr is initialised to 0. */
58 struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0,0,0,0,0,0},0};
59 uint64_t buff_addr = 0;
60 uint64_t buff_hdr_addr = 0;
61 uint32_t head[MAX_PKT_BURST], packet_len = 0;
62 uint32_t head_idx, packet_success = 0;
63 uint16_t avail_idx, res_cur_idx;
64 uint16_t res_base_idx, res_end_idx;
65 uint16_t free_entries;
68 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") virtio_dev_rx()\n", dev->device_fh);
69 if (unlikely(queue_id != VIRTIO_RXQ)) {
70 LOG_DEBUG(VHOST_DATA, "mq isn't supported in this version.\n");
74 vq = dev->virtqueue[VIRTIO_RXQ];
75 count = (count > MAX_PKT_BURST) ? MAX_PKT_BURST : count;
77 /* As many data cores may want access to available buffers, they need to be reserved. */
79 res_base_idx = vq->last_used_idx_res;
80 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
82 free_entries = (avail_idx - res_base_idx);
83 /*check that we have enough buffers*/
84 if (unlikely(count > free_entries))
90 res_end_idx = res_base_idx + count;
91 /* vq->last_used_idx_res is atomically updated. */
92 success = rte_atomic16_cmpset(&vq->last_used_idx_res, res_base_idx,
94 } while (unlikely(success == 0));
95 res_cur_idx = res_base_idx;
96 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") Current Index %d| End Index %d\n", dev->device_fh, res_cur_idx, res_end_idx);
98 /* Prefetch available ring to retrieve indexes. */
99 rte_prefetch0(&vq->avail->ring[res_cur_idx & (vq->size - 1)]);
101 /* Retrieve all of the head indexes first to avoid caching issues. */
102 for (head_idx = 0; head_idx < count; head_idx++)
103 head[head_idx] = vq->avail->ring[(res_cur_idx + head_idx) & (vq->size - 1)];
105 /*Prefetch descriptor index. */
106 rte_prefetch0(&vq->desc[head[packet_success]]);
108 while (res_cur_idx != res_end_idx) {
109 /* Get descriptor from available ring */
110 desc = &vq->desc[head[packet_success]];
112 buff = pkts[packet_success];
114 /* Convert from gpa to vva (guest physical addr -> vhost virtual addr) */
115 buff_addr = gpa_to_vva(dev, desc->addr);
116 /* Prefetch buffer address. */
117 rte_prefetch0((void*)(uintptr_t)buff_addr);
119 /* Copy virtio_hdr to packet and increment buffer address */
120 buff_hdr_addr = buff_addr;
121 packet_len = rte_pktmbuf_data_len(buff) + vq->vhost_hlen;
124 * If the descriptors are chained the header and data are
125 * placed in separate buffers.
127 if (desc->flags & VRING_DESC_F_NEXT) {
128 desc->len = vq->vhost_hlen;
129 desc = &vq->desc[desc->next];
130 /* Buffer address translation. */
131 buff_addr = gpa_to_vva(dev, desc->addr);
132 desc->len = rte_pktmbuf_data_len(buff);
134 buff_addr += vq->vhost_hlen;
135 desc->len = packet_len;
138 /* Update used ring with desc information */
139 vq->used->ring[res_cur_idx & (vq->size - 1)].id = head[packet_success];
140 vq->used->ring[res_cur_idx & (vq->size - 1)].len = packet_len;
142 /* Copy mbuf data to buffer */
143 rte_memcpy((void *)(uintptr_t)buff_addr,
144 rte_pktmbuf_mtod(buff, const void *),
145 rte_pktmbuf_data_len(buff));
146 PRINT_PACKET(dev, (uintptr_t)buff_addr,
147 rte_pktmbuf_data_len(buff), 0);
152 rte_memcpy((void *)(uintptr_t)buff_hdr_addr,
153 (const void *)&virtio_hdr, vq->vhost_hlen);
155 PRINT_PACKET(dev, (uintptr_t)buff_hdr_addr, vq->vhost_hlen, 1);
157 if (res_cur_idx < res_end_idx) {
158 /* Prefetch descriptor index. */
159 rte_prefetch0(&vq->desc[head[packet_success]]);
163 rte_compiler_barrier();
165 /* Wait until it's our turn to add our buffer to the used ring. */
166 while (unlikely(vq->last_used_idx != res_base_idx))
169 *(volatile uint16_t *)&vq->used->idx += count;
170 vq->last_used_idx = res_end_idx;
172 /* Kick the guest if necessary. */
173 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
174 eventfd_write((int)vq->kickfd, 1);
178 static inline uint32_t __attribute__((always_inline))
179 copy_from_mbuf_to_vring(struct virtio_net *dev,
180 uint16_t res_base_idx, uint16_t res_end_idx,
181 struct rte_mbuf *pkt)
183 uint32_t vec_idx = 0;
184 uint32_t entry_success = 0;
185 struct vhost_virtqueue *vq;
186 /* The virtio_hdr is initialised to 0. */
187 struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {
188 {0, 0, 0, 0, 0, 0}, 0};
189 uint16_t cur_idx = res_base_idx;
190 uint64_t vb_addr = 0;
191 uint64_t vb_hdr_addr = 0;
192 uint32_t seg_offset = 0;
193 uint32_t vb_offset = 0;
196 uint32_t cpy_len, entry_len;
201 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") Current Index %d| "
203 dev->device_fh, cur_idx, res_end_idx);
206 * Convert from gpa to vva
207 * (guest physical addr -> vhost virtual addr)
209 vq = dev->virtqueue[VIRTIO_RXQ];
211 gpa_to_vva(dev, vq->buf_vec[vec_idx].buf_addr);
212 vb_hdr_addr = vb_addr;
214 /* Prefetch buffer address. */
215 rte_prefetch0((void *)(uintptr_t)vb_addr);
217 virtio_hdr.num_buffers = res_end_idx - res_base_idx;
219 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") RX: Num merge buffers %d\n",
220 dev->device_fh, virtio_hdr.num_buffers);
222 rte_memcpy((void *)(uintptr_t)vb_hdr_addr,
223 (const void *)&virtio_hdr, vq->vhost_hlen);
225 PRINT_PACKET(dev, (uintptr_t)vb_hdr_addr, vq->vhost_hlen, 1);
227 seg_avail = rte_pktmbuf_data_len(pkt);
228 vb_offset = vq->vhost_hlen;
230 vq->buf_vec[vec_idx].buf_len - vq->vhost_hlen;
232 entry_len = vq->vhost_hlen;
236 vq->buf_vec[vec_idx].desc_idx;
237 vq->desc[desc_idx].len = vq->vhost_hlen;
239 if ((vq->desc[desc_idx].flags
240 & VRING_DESC_F_NEXT) == 0) {
241 /* Update used ring with desc information */
242 vq->used->ring[cur_idx & (vq->size - 1)].id
243 = vq->buf_vec[vec_idx].desc_idx;
244 vq->used->ring[cur_idx & (vq->size - 1)].len
254 gpa_to_vva(dev, vq->buf_vec[vec_idx].buf_addr);
256 /* Prefetch buffer address. */
257 rte_prefetch0((void *)(uintptr_t)vb_addr);
259 vb_avail = vq->buf_vec[vec_idx].buf_len;
262 cpy_len = RTE_MIN(vb_avail, seg_avail);
264 while (cpy_len > 0) {
265 /* Copy mbuf data to vring buffer */
266 rte_memcpy((void *)(uintptr_t)(vb_addr + vb_offset),
267 (const void *)(rte_pktmbuf_mtod(pkt, char*) + seg_offset),
271 (uintptr_t)(vb_addr + vb_offset),
274 seg_offset += cpy_len;
275 vb_offset += cpy_len;
276 seg_avail -= cpy_len;
278 entry_len += cpy_len;
280 if (seg_avail != 0) {
282 * The virtio buffer in this vring
283 * entry reach to its end.
284 * But the segment doesn't complete.
286 if ((vq->desc[vq->buf_vec[vec_idx].desc_idx].flags &
287 VRING_DESC_F_NEXT) == 0) {
288 /* Update used ring with desc information */
289 vq->used->ring[cur_idx & (vq->size - 1)].id
290 = vq->buf_vec[vec_idx].desc_idx;
291 vq->used->ring[cur_idx & (vq->size - 1)].len
299 vb_addr = gpa_to_vva(dev,
300 vq->buf_vec[vec_idx].buf_addr);
302 vb_avail = vq->buf_vec[vec_idx].buf_len;
303 cpy_len = RTE_MIN(vb_avail, seg_avail);
306 * This current segment complete, need continue to
307 * check if the whole packet complete or not.
312 * There are more segments.
316 * This current buffer from vring is
317 * used up, need fetch next buffer
321 vq->buf_vec[vec_idx].desc_idx;
322 vq->desc[desc_idx].len = vb_offset;
324 if ((vq->desc[desc_idx].flags &
325 VRING_DESC_F_NEXT) == 0) {
326 uint16_t wrapped_idx =
327 cur_idx & (vq->size - 1);
329 * Update used ring with the
330 * descriptor information
332 vq->used->ring[wrapped_idx].id
334 vq->used->ring[wrapped_idx].len
341 /* Get next buffer from buf_vec. */
343 vb_addr = gpa_to_vva(dev,
344 vq->buf_vec[vec_idx].buf_addr);
346 vq->buf_vec[vec_idx].buf_len;
351 seg_avail = rte_pktmbuf_data_len(pkt);
352 cpy_len = RTE_MIN(vb_avail, seg_avail);
355 * This whole packet completes.
358 vq->buf_vec[vec_idx].desc_idx;
359 vq->desc[desc_idx].len = vb_offset;
361 while (vq->desc[desc_idx].flags &
363 desc_idx = vq->desc[desc_idx].next;
364 vq->desc[desc_idx].len = 0;
367 /* Update used ring with desc information */
368 vq->used->ring[cur_idx & (vq->size - 1)].id
369 = vq->buf_vec[vec_idx].desc_idx;
370 vq->used->ring[cur_idx & (vq->size - 1)].len
376 cpy_len = RTE_MIN(vb_avail, seg_avail);
381 return entry_success;
385 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
386 * be received from the physical port or from another virtio device. A packet
387 * count is returned to indicate the number of packets that were succesfully
388 * added to the RX queue. This function works for mergeable RX.
390 static inline uint32_t __attribute__((always_inline))
391 virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id, struct rte_mbuf **pkts,
394 struct vhost_virtqueue *vq;
395 uint32_t pkt_idx = 0, entry_success = 0;
396 uint16_t avail_idx, res_cur_idx;
397 uint16_t res_base_idx, res_end_idx;
400 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") virtio_dev_merge_rx()\n",
402 if (unlikely(queue_id != VIRTIO_RXQ)) {
403 LOG_DEBUG(VHOST_DATA, "mq isn't supported in this version.\n");
406 vq = dev->virtqueue[VIRTIO_RXQ];
407 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
412 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
413 uint32_t secure_len = 0;
415 uint32_t vec_idx = 0;
416 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + vq->vhost_hlen;
421 * As many data cores may want access to available
422 * buffers, they need to be reserved.
424 res_base_idx = vq->last_used_idx_res;
425 res_cur_idx = res_base_idx;
428 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
429 if (unlikely(res_cur_idx == avail_idx)) {
430 LOG_DEBUG(VHOST_DATA,
431 "(%"PRIu64") Failed "
432 "to get enough desc from "
437 uint16_t wrapped_idx =
438 (res_cur_idx) & (vq->size - 1);
440 vq->avail->ring[wrapped_idx];
445 secure_len += vq->desc[idx].len;
446 if (vq->desc[idx].flags &
448 idx = vq->desc[idx].next;
455 } while (pkt_len > secure_len);
457 /* vq->last_used_idx_res is atomically updated. */
458 success = rte_atomic16_cmpset(&vq->last_used_idx_res,
461 } while (success == 0);
464 need_cnt = res_cur_idx - res_base_idx;
466 for (i = 0; i < need_cnt; i++, id++) {
467 uint16_t wrapped_idx = id & (vq->size - 1);
468 uint32_t idx = vq->avail->ring[wrapped_idx];
472 vq->buf_vec[vec_idx].buf_addr =
474 vq->buf_vec[vec_idx].buf_len =
476 vq->buf_vec[vec_idx].desc_idx = idx;
479 if (vq->desc[idx].flags & VRING_DESC_F_NEXT) {
480 idx = vq->desc[idx].next;
486 res_end_idx = res_cur_idx;
488 entry_success = copy_from_mbuf_to_vring(dev, res_base_idx,
489 res_end_idx, pkts[pkt_idx]);
491 rte_compiler_barrier();
494 * Wait until it's our turn to add our buffer
497 while (unlikely(vq->last_used_idx != res_base_idx))
500 *(volatile uint16_t *)&vq->used->idx += entry_success;
501 vq->last_used_idx = res_end_idx;
503 /* Kick the guest if necessary. */
504 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
505 eventfd_write((int)vq->kickfd, 1);
512 rte_vhost_enqueue_burst(struct virtio_net *dev, uint16_t queue_id, struct rte_mbuf **pkts, uint16_t count)
514 if (unlikely(dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)))
515 return virtio_dev_merge_rx(dev, queue_id, pkts, count);
517 return virtio_dev_rx(dev, queue_id, pkts, count);
521 rte_vhost_dequeue_burst(struct virtio_net *dev, uint16_t queue_id, struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
523 struct rte_mbuf *m, *prev;
524 struct vhost_virtqueue *vq;
525 struct vring_desc *desc;
526 uint64_t vb_addr = 0;
527 uint32_t head[MAX_PKT_BURST];
530 uint16_t free_entries, entry_success = 0;
533 if (unlikely(queue_id != VIRTIO_TXQ)) {
534 LOG_DEBUG(VHOST_DATA, "mq isn't supported in this version.\n");
538 vq = dev->virtqueue[VIRTIO_TXQ];
539 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
541 /* If there are no available buffers then return. */
542 if (vq->last_used_idx == avail_idx)
545 LOG_DEBUG(VHOST_DATA, "%s (%"PRIu64")\n", __func__,
548 /* Prefetch available ring to retrieve head indexes. */
549 rte_prefetch0(&vq->avail->ring[vq->last_used_idx & (vq->size - 1)]);
551 /*get the number of free entries in the ring*/
552 free_entries = (avail_idx - vq->last_used_idx);
554 free_entries = RTE_MIN(free_entries, count);
555 /* Limit to MAX_PKT_BURST. */
556 free_entries = RTE_MIN(free_entries, MAX_PKT_BURST);
558 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") Buffers available %d\n",
559 dev->device_fh, free_entries);
560 /* Retrieve all of the head indexes first to avoid caching issues. */
561 for (i = 0; i < free_entries; i++)
562 head[i] = vq->avail->ring[(vq->last_used_idx + i) & (vq->size - 1)];
564 /* Prefetch descriptor index. */
565 rte_prefetch0(&vq->desc[head[entry_success]]);
566 rte_prefetch0(&vq->used->ring[vq->last_used_idx & (vq->size - 1)]);
568 while (entry_success < free_entries) {
569 uint32_t vb_avail, vb_offset;
570 uint32_t seg_avail, seg_offset;
572 uint32_t seg_num = 0;
573 struct rte_mbuf *cur;
574 uint8_t alloc_err = 0;
576 desc = &vq->desc[head[entry_success]];
578 /* Discard first buffer as it is the virtio header */
579 desc = &vq->desc[desc->next];
581 /* Buffer address translation. */
582 vb_addr = gpa_to_vva(dev, desc->addr);
583 /* Prefetch buffer address. */
584 rte_prefetch0((void *)(uintptr_t)vb_addr);
586 used_idx = vq->last_used_idx & (vq->size - 1);
588 if (entry_success < (free_entries - 1)) {
589 /* Prefetch descriptor index. */
590 rte_prefetch0(&vq->desc[head[entry_success+1]]);
591 rte_prefetch0(&vq->used->ring[(used_idx + 1) & (vq->size - 1)]);
594 /* Update used index buffer information. */
595 vq->used->ring[used_idx].id = head[entry_success];
596 vq->used->ring[used_idx].len = 0;
599 vb_avail = desc->len;
600 /* Allocate an mbuf and populate the structure. */
601 m = rte_pktmbuf_alloc(mbuf_pool);
602 if (unlikely(m == NULL)) {
603 RTE_LOG(ERR, VHOST_DATA,
604 "Failed to allocate memory for mbuf.\n");
605 return entry_success;
608 seg_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
609 cpy_len = RTE_MIN(vb_avail, seg_avail);
611 PRINT_PACKET(dev, (uintptr_t)vb_addr, desc->len, 0);
616 while (cpy_len != 0) {
617 rte_memcpy((void *)(rte_pktmbuf_mtod(cur, char *) + seg_offset),
618 (void *)((uintptr_t)(vb_addr + vb_offset)),
621 seg_offset += cpy_len;
622 vb_offset += cpy_len;
624 seg_avail -= cpy_len;
628 * The segment reachs to its end,
629 * while the virtio buffer in TX vring has
630 * more data to be copied.
632 cur->data_len = seg_offset;
633 m->pkt_len += seg_offset;
634 /* Allocate mbuf and populate the structure. */
635 cur = rte_pktmbuf_alloc(mbuf_pool);
636 if (unlikely(cur == NULL)) {
637 RTE_LOG(ERR, VHOST_DATA, "Failed to "
638 "allocate memory for mbuf.\n");
648 seg_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
650 if (desc->flags & VRING_DESC_F_NEXT) {
652 * There are more virtio buffers in
653 * same vring entry need to be copied.
655 if (seg_avail == 0) {
657 * The current segment hasn't
658 * room to accomodate more
661 cur->data_len = seg_offset;
662 m->pkt_len += seg_offset;
664 * Allocate an mbuf and
665 * populate the structure.
667 cur = rte_pktmbuf_alloc(mbuf_pool);
668 if (unlikely(cur == NULL)) {
682 seg_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
685 desc = &vq->desc[desc->next];
687 /* Buffer address translation. */
688 vb_addr = gpa_to_vva(dev, desc->addr);
689 /* Prefetch buffer address. */
690 rte_prefetch0((void *)(uintptr_t)vb_addr);
692 vb_avail = desc->len;
694 PRINT_PACKET(dev, (uintptr_t)vb_addr,
697 /* The whole packet completes. */
698 cur->data_len = seg_offset;
699 m->pkt_len += seg_offset;
704 cpy_len = RTE_MIN(vb_avail, seg_avail);
707 if (unlikely(alloc_err == 1))
710 m->nb_segs = seg_num;
712 pkts[entry_success] = m;
717 rte_compiler_barrier();
718 vq->used->idx += entry_success;
719 /* Kick guest if required. */
720 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
721 eventfd_write((int)vq->kickfd, 1);
722 return entry_success;