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.
34 #include <arpa/inet.h>
36 #include <linux/if_ether.h>
37 #include <linux/if_vlan.h>
38 #include <linux/virtio_net.h>
39 #include <linux/virtio_ring.h>
42 #include <sys/eventfd.h>
43 #include <sys/param.h>
46 #include <rte_atomic.h>
47 #include <rte_cycles.h>
48 #include <rte_ethdev.h>
50 #include <rte_string_fns.h>
51 #include <rte_malloc.h>
54 #include "virtio-net.h"
55 #include "vhost-net-cdev.h"
57 #define MAX_PKT_BURST 32 /* Max burst size for RX/TX */
60 * Function to convert guest physical addresses to vhost virtual addresses. This
61 * is used to convert virtio buffer addresses.
63 static inline uint64_t __attribute__((always_inline))
64 gpa_to_vva(struct virtio_net *dev, uint64_t guest_pa)
66 struct virtio_memory_regions *region;
68 uint64_t vhost_va = 0;
70 for (regionidx = 0; regionidx < dev->mem->nregions; regionidx++) {
71 region = &dev->mem->regions[regionidx];
72 if ((guest_pa >= region->guest_phys_address) &&
73 (guest_pa <= region->guest_phys_address_end)) {
74 vhost_va = region->address_offset + guest_pa;
78 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") GPA %p| VVA %p\n",
79 dev->device_fh, (void*)(uintptr_t)guest_pa, (void*)(uintptr_t)vhost_va);
85 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
86 * be received from the physical port or from another virtio device. A packet
87 * count is returned to indicate the number of packets that were succesfully
88 * added to the RX queue. This function works when mergeable is disabled.
90 static inline uint32_t __attribute__((always_inline))
91 virtio_dev_rx(struct virtio_net *dev, struct rte_mbuf **pkts, uint32_t count)
93 struct vhost_virtqueue *vq;
94 struct vring_desc *desc;
95 struct rte_mbuf *buff;
96 /* The virtio_hdr is initialised to 0. */
97 struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0,0,0,0,0,0},0};
98 uint64_t buff_addr = 0;
99 uint64_t buff_hdr_addr = 0;
100 uint32_t head[MAX_PKT_BURST], packet_len = 0;
101 uint32_t head_idx, packet_success = 0;
102 uint16_t avail_idx, res_cur_idx;
103 uint16_t res_base_idx, res_end_idx;
104 uint16_t free_entries;
107 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") virtio_dev_rx()\n", dev->device_fh);
108 vq = dev->virtqueue[VIRTIO_RXQ];
109 count = (count > MAX_PKT_BURST) ? MAX_PKT_BURST : count;
111 /* As many data cores may want access to available buffers, they need to be reserved. */
113 res_base_idx = vq->last_used_idx_res;
114 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
116 free_entries = (avail_idx - res_base_idx);
117 /*check that we have enough buffers*/
118 if (unlikely(count > free_entries))
119 count = free_entries;
124 res_end_idx = res_base_idx + count;
125 /* vq->last_used_idx_res is atomically updated. */
126 success = rte_atomic16_cmpset(&vq->last_used_idx_res, res_base_idx,
128 } while (unlikely(success == 0));
129 res_cur_idx = res_base_idx;
130 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") Current Index %d| End Index %d\n", dev->device_fh, res_cur_idx, res_end_idx);
132 /* Prefetch available ring to retrieve indexes. */
133 rte_prefetch0(&vq->avail->ring[res_cur_idx & (vq->size - 1)]);
135 /* Retrieve all of the head indexes first to avoid caching issues. */
136 for (head_idx = 0; head_idx < count; head_idx++)
137 head[head_idx] = vq->avail->ring[(res_cur_idx + head_idx) & (vq->size - 1)];
139 /*Prefetch descriptor index. */
140 rte_prefetch0(&vq->desc[head[packet_success]]);
142 while (res_cur_idx != res_end_idx) {
143 /* Get descriptor from available ring */
144 desc = &vq->desc[head[packet_success]];
146 buff = pkts[packet_success];
148 /* Convert from gpa to vva (guest physical addr -> vhost virtual addr) */
149 buff_addr = gpa_to_vva(dev, desc->addr);
150 /* Prefetch buffer address. */
151 rte_prefetch0((void*)(uintptr_t)buff_addr);
153 /* Copy virtio_hdr to packet and increment buffer address */
154 buff_hdr_addr = buff_addr;
155 packet_len = rte_pktmbuf_data_len(buff) + vq->vhost_hlen;
158 * If the descriptors are chained the header and data are
159 * placed in separate buffers.
161 if (desc->flags & VRING_DESC_F_NEXT) {
162 desc->len = vq->vhost_hlen;
163 desc = &vq->desc[desc->next];
164 /* Buffer address translation. */
165 buff_addr = gpa_to_vva(dev, desc->addr);
166 desc->len = rte_pktmbuf_data_len(buff);
168 buff_addr += vq->vhost_hlen;
169 desc->len = packet_len;
172 /* Update used ring with desc information */
173 vq->used->ring[res_cur_idx & (vq->size - 1)].id = head[packet_success];
174 vq->used->ring[res_cur_idx & (vq->size - 1)].len = packet_len;
176 /* Copy mbuf data to buffer */
177 rte_memcpy((void *)(uintptr_t)buff_addr,
178 rte_pktmbuf_mtod(buff, const void *),
179 rte_pktmbuf_data_len(buff));
180 PRINT_PACKET(dev, (uintptr_t)buff_addr,
181 rte_pktmbuf_data_len(buff), 0);
186 rte_memcpy((void *)(uintptr_t)buff_hdr_addr,
187 (const void *)&virtio_hdr, vq->vhost_hlen);
189 PRINT_PACKET(dev, (uintptr_t)buff_hdr_addr, vq->vhost_hlen, 1);
191 if (res_cur_idx < res_end_idx) {
192 /* Prefetch descriptor index. */
193 rte_prefetch0(&vq->desc[head[packet_success]]);
197 rte_compiler_barrier();
199 /* Wait until it's our turn to add our buffer to the used ring. */
200 while (unlikely(vq->last_used_idx != res_base_idx))
203 *(volatile uint16_t *)&vq->used->idx += count;
204 vq->last_used_idx = res_end_idx;
206 /* Kick the guest if necessary. */
207 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
208 eventfd_write((int)vq->kickfd, 1);
212 static inline uint32_t __attribute__((always_inline))
213 copy_from_mbuf_to_vring(struct virtio_net *dev,
214 uint16_t res_base_idx, uint16_t res_end_idx,
215 struct rte_mbuf *pkt)
217 uint32_t vec_idx = 0;
218 uint32_t entry_success = 0;
219 struct vhost_virtqueue *vq;
220 /* The virtio_hdr is initialised to 0. */
221 struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {
222 {0, 0, 0, 0, 0, 0}, 0};
223 uint16_t cur_idx = res_base_idx;
224 uint64_t vb_addr = 0;
225 uint64_t vb_hdr_addr = 0;
226 uint32_t seg_offset = 0;
227 uint32_t vb_offset = 0;
230 uint32_t cpy_len, entry_len;
235 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") Current Index %d| "
237 dev->device_fh, cur_idx, res_end_idx);
240 * Convert from gpa to vva
241 * (guest physical addr -> vhost virtual addr)
243 vq = dev->virtqueue[VIRTIO_RXQ];
245 gpa_to_vva(dev, vq->buf_vec[vec_idx].buf_addr);
246 vb_hdr_addr = vb_addr;
248 /* Prefetch buffer address. */
249 rte_prefetch0((void *)(uintptr_t)vb_addr);
251 virtio_hdr.num_buffers = res_end_idx - res_base_idx;
253 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") RX: Num merge buffers %d\n",
254 dev->device_fh, virtio_hdr.num_buffers);
256 rte_memcpy((void *)(uintptr_t)vb_hdr_addr,
257 (const void *)&virtio_hdr, vq->vhost_hlen);
259 PRINT_PACKET(dev, (uintptr_t)vb_hdr_addr, vq->vhost_hlen, 1);
261 seg_avail = rte_pktmbuf_data_len(pkt);
262 vb_offset = vq->vhost_hlen;
264 vq->buf_vec[vec_idx].buf_len - vq->vhost_hlen;
266 entry_len = vq->vhost_hlen;
270 vq->buf_vec[vec_idx].desc_idx;
271 vq->desc[desc_idx].len = vq->vhost_hlen;
273 if ((vq->desc[desc_idx].flags
274 & VRING_DESC_F_NEXT) == 0) {
275 /* Update used ring with desc information */
276 vq->used->ring[cur_idx & (vq->size - 1)].id
277 = vq->buf_vec[vec_idx].desc_idx;
278 vq->used->ring[cur_idx & (vq->size - 1)].len
288 gpa_to_vva(dev, vq->buf_vec[vec_idx].buf_addr);
290 /* Prefetch buffer address. */
291 rte_prefetch0((void *)(uintptr_t)vb_addr);
293 vb_avail = vq->buf_vec[vec_idx].buf_len;
296 cpy_len = RTE_MIN(vb_avail, seg_avail);
298 while (cpy_len > 0) {
299 /* Copy mbuf data to vring buffer */
300 rte_memcpy((void *)(uintptr_t)(vb_addr + vb_offset),
301 (const void *)(rte_pktmbuf_mtod(pkt, char*) + seg_offset),
305 (uintptr_t)(vb_addr + vb_offset),
308 seg_offset += cpy_len;
309 vb_offset += cpy_len;
310 seg_avail -= cpy_len;
312 entry_len += cpy_len;
314 if (seg_avail != 0) {
316 * The virtio buffer in this vring
317 * entry reach to its end.
318 * But the segment doesn't complete.
320 if ((vq->desc[vq->buf_vec[vec_idx].desc_idx].flags &
321 VRING_DESC_F_NEXT) == 0) {
322 /* Update used ring with desc information */
323 vq->used->ring[cur_idx & (vq->size - 1)].id
324 = vq->buf_vec[vec_idx].desc_idx;
325 vq->used->ring[cur_idx & (vq->size - 1)].len
333 vb_addr = gpa_to_vva(dev,
334 vq->buf_vec[vec_idx].buf_addr);
336 vb_avail = vq->buf_vec[vec_idx].buf_len;
337 cpy_len = RTE_MIN(vb_avail, seg_avail);
340 * This current segment complete, need continue to
341 * check if the whole packet complete or not.
346 * There are more segments.
350 * This current buffer from vring is
351 * used up, need fetch next buffer
355 vq->buf_vec[vec_idx].desc_idx;
356 vq->desc[desc_idx].len = vb_offset;
358 if ((vq->desc[desc_idx].flags &
359 VRING_DESC_F_NEXT) == 0) {
360 uint16_t wrapped_idx =
361 cur_idx & (vq->size - 1);
363 * Update used ring with the
364 * descriptor information
366 vq->used->ring[wrapped_idx].id
368 vq->used->ring[wrapped_idx].len
375 /* Get next buffer from buf_vec. */
377 vb_addr = gpa_to_vva(dev,
378 vq->buf_vec[vec_idx].buf_addr);
380 vq->buf_vec[vec_idx].buf_len;
385 seg_avail = rte_pktmbuf_data_len(pkt);
386 cpy_len = RTE_MIN(vb_avail, seg_avail);
389 * This whole packet completes.
392 vq->buf_vec[vec_idx].desc_idx;
393 vq->desc[desc_idx].len = vb_offset;
395 while (vq->desc[desc_idx].flags &
397 desc_idx = vq->desc[desc_idx].next;
398 vq->desc[desc_idx].len = 0;
401 /* Update used ring with desc information */
402 vq->used->ring[cur_idx & (vq->size - 1)].id
403 = vq->buf_vec[vec_idx].desc_idx;
404 vq->used->ring[cur_idx & (vq->size - 1)].len
410 cpy_len = RTE_MIN(vb_avail, seg_avail);
415 return entry_success;
419 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
420 * be received from the physical port or from another virtio device. A packet
421 * count is returned to indicate the number of packets that were succesfully
422 * added to the RX queue. This function works for mergeable RX.
424 static inline uint32_t __attribute__((always_inline))
425 virtio_dev_merge_rx(struct virtio_net *dev, struct rte_mbuf **pkts,
428 struct vhost_virtqueue *vq;
429 uint32_t pkt_idx = 0, entry_success = 0;
430 uint16_t avail_idx, res_cur_idx;
431 uint16_t res_base_idx, res_end_idx;
434 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") virtio_dev_merge_rx()\n",
436 vq = dev->virtqueue[VIRTIO_RXQ];
437 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
442 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
443 uint32_t secure_len = 0;
445 uint32_t vec_idx = 0;
446 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + vq->vhost_hlen;
451 * As many data cores may want access to available
452 * buffers, they need to be reserved.
454 res_base_idx = vq->last_used_idx_res;
455 res_cur_idx = res_base_idx;
458 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
459 if (unlikely(res_cur_idx == avail_idx)) {
460 LOG_DEBUG(VHOST_DATA,
461 "(%"PRIu64") Failed "
462 "to get enough desc from "
467 uint16_t wrapped_idx =
468 (res_cur_idx) & (vq->size - 1);
470 vq->avail->ring[wrapped_idx];
475 secure_len += vq->desc[idx].len;
476 if (vq->desc[idx].flags &
478 idx = vq->desc[idx].next;
485 } while (pkt_len > secure_len);
487 /* vq->last_used_idx_res is atomically updated. */
488 success = rte_atomic16_cmpset(&vq->last_used_idx_res,
491 } while (success == 0);
494 need_cnt = res_cur_idx - res_base_idx;
496 for (i = 0; i < need_cnt; i++, id++) {
497 uint16_t wrapped_idx = id & (vq->size - 1);
498 uint32_t idx = vq->avail->ring[wrapped_idx];
502 vq->buf_vec[vec_idx].buf_addr =
504 vq->buf_vec[vec_idx].buf_len =
506 vq->buf_vec[vec_idx].desc_idx = idx;
509 if (vq->desc[idx].flags & VRING_DESC_F_NEXT) {
510 idx = vq->desc[idx].next;
516 res_end_idx = res_cur_idx;
518 entry_success = copy_from_mbuf_to_vring(dev, res_base_idx,
519 res_end_idx, pkts[pkt_idx]);
521 rte_compiler_barrier();
524 * Wait until it's our turn to add our buffer
527 while (unlikely(vq->last_used_idx != res_base_idx))
530 *(volatile uint16_t *)&vq->used->idx += entry_success;
531 vq->last_used_idx = res_end_idx;
533 /* Kick the guest if necessary. */
534 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
535 eventfd_write((int)vq->kickfd, 1);
541 /* This function works for TX packets with mergeable feature enabled. */
542 static inline void __attribute__((always_inline))
543 virtio_dev_merge_tx(struct virtio_net *dev, struct rte_mempool *mbuf_pool)
545 struct rte_mbuf *m, *prev;
546 struct vhost_virtqueue *vq;
547 struct vring_desc *desc;
548 uint64_t vb_addr = 0;
549 uint32_t head[MAX_PKT_BURST];
552 uint16_t free_entries, entry_success = 0;
554 uint32_t buf_size = MBUF_SIZE - (sizeof(struct rte_mbuf)
555 + RTE_PKTMBUF_HEADROOM);
557 vq = dev->virtqueue[VIRTIO_TXQ];
558 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
560 /* If there are no available buffers then return. */
561 if (vq->last_used_idx == avail_idx)
564 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") virtio_dev_merge_tx()\n",
567 /* Prefetch available ring to retrieve head indexes. */
568 rte_prefetch0(&vq->avail->ring[vq->last_used_idx & (vq->size - 1)]);
570 /*get the number of free entries in the ring*/
571 free_entries = (avail_idx - vq->last_used_idx);
573 /* Limit to MAX_PKT_BURST. */
574 free_entries = RTE_MIN(free_entries, MAX_PKT_BURST);
576 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") Buffers available %d\n",
577 dev->device_fh, free_entries);
578 /* Retrieve all of the head indexes first to avoid caching issues. */
579 for (i = 0; i < free_entries; i++)
580 head[i] = vq->avail->ring[(vq->last_used_idx + i) & (vq->size - 1)];
582 /* Prefetch descriptor index. */
583 rte_prefetch0(&vq->desc[head[entry_success]]);
584 rte_prefetch0(&vq->used->ring[vq->last_used_idx & (vq->size - 1)]);
586 while (entry_success < free_entries) {
587 uint32_t vb_avail, vb_offset;
588 uint32_t seg_avail, seg_offset;
590 uint32_t seg_num = 0;
591 struct rte_mbuf *cur;
592 uint8_t alloc_err = 0;
594 desc = &vq->desc[head[entry_success]];
596 /* Discard first buffer as it is the virtio header */
597 desc = &vq->desc[desc->next];
599 /* Buffer address translation. */
600 vb_addr = gpa_to_vva(dev, desc->addr);
601 /* Prefetch buffer address. */
602 rte_prefetch0((void *)(uintptr_t)vb_addr);
604 used_idx = vq->last_used_idx & (vq->size - 1);
606 if (entry_success < (free_entries - 1)) {
607 /* Prefetch descriptor index. */
608 rte_prefetch0(&vq->desc[head[entry_success+1]]);
609 rte_prefetch0(&vq->used->ring[(used_idx + 1) & (vq->size - 1)]);
612 /* Update used index buffer information. */
613 vq->used->ring[used_idx].id = head[entry_success];
614 vq->used->ring[used_idx].len = 0;
617 vb_avail = desc->len;
619 seg_avail = buf_size;
620 cpy_len = RTE_MIN(vb_avail, seg_avail);
622 PRINT_PACKET(dev, (uintptr_t)vb_addr, desc->len, 0);
624 /* Allocate an mbuf and populate the structure. */
625 m = rte_pktmbuf_alloc(mbuf_pool);
626 if (unlikely(m == NULL)) {
627 RTE_LOG(ERR, VHOST_DATA,
628 "Failed to allocate memory for mbuf.\n");
635 while (cpy_len != 0) {
636 rte_memcpy((void *)(rte_pktmbuf_mtod(cur, char *) + seg_offset),
637 (void *)((uintptr_t)(vb_addr + vb_offset)),
640 seg_offset += cpy_len;
641 vb_offset += cpy_len;
643 seg_avail -= cpy_len;
647 * The segment reachs to its end,
648 * while the virtio buffer in TX vring has
649 * more data to be copied.
651 cur->data_len = seg_offset;
652 m->pkt_len += seg_offset;
653 /* Allocate mbuf and populate the structure. */
654 cur = rte_pktmbuf_alloc(mbuf_pool);
655 if (unlikely(cur == NULL)) {
656 RTE_LOG(ERR, VHOST_DATA, "Failed to "
657 "allocate memory for mbuf.\n");
667 seg_avail = buf_size;
669 if (desc->flags & VRING_DESC_F_NEXT) {
671 * There are more virtio buffers in
672 * same vring entry need to be copied.
674 if (seg_avail == 0) {
676 * The current segment hasn't
677 * room to accomodate more
680 cur->data_len = seg_offset;
681 m->pkt_len += seg_offset;
683 * Allocate an mbuf and
684 * populate the structure.
686 cur = rte_pktmbuf_alloc(mbuf_pool);
687 if (unlikely(cur == NULL)) {
701 seg_avail = buf_size;
704 desc = &vq->desc[desc->next];
706 /* Buffer address translation. */
707 vb_addr = gpa_to_vva(dev, desc->addr);
708 /* Prefetch buffer address. */
709 rte_prefetch0((void *)(uintptr_t)vb_addr);
711 vb_avail = desc->len;
713 PRINT_PACKET(dev, (uintptr_t)vb_addr,
716 /* The whole packet completes. */
717 cur->data_len = seg_offset;
718 m->pkt_len += seg_offset;
723 cpy_len = RTE_MIN(vb_avail, seg_avail);
726 if (unlikely(alloc_err == 1))
729 m->nb_segs = seg_num;
736 rte_compiler_barrier();
737 vq->used->idx += entry_success;
738 /* Kick guest if required. */
739 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
740 eventfd_write((int)vq->kickfd, 1);