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;
103 uint16_t avail_idx, res_cur_idx;
104 uint16_t res_base_idx, res_end_idx;
105 uint16_t free_entries;
108 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") virtio_dev_rx()\n", dev->device_fh);
109 vq = dev->virtqueue[VIRTIO_RXQ];
110 count = (count > MAX_PKT_BURST) ? MAX_PKT_BURST : count;
112 /* As many data cores may want access to available buffers, they need to be reserved. */
114 res_base_idx = vq->last_used_idx_res;
115 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
117 free_entries = (avail_idx - res_base_idx);
118 /* If retry is enabled and the queue is full then we wait and retry to avoid packet loss. */
119 if (enable_retry && unlikely(count > free_entries)) {
120 for (retry = 0; retry < burst_rx_retry_num; retry++) {
121 rte_delay_us(burst_rx_delay_time);
123 *((volatile uint16_t *)&vq->avail->idx);
124 free_entries = (avail_idx - res_base_idx);
125 if (count <= free_entries)
130 /*check that we have enough buffers*/
131 if (unlikely(count > free_entries))
132 count = free_entries;
137 res_end_idx = res_base_idx + count;
138 /* vq->last_used_idx_res is atomically updated. */
139 success = rte_atomic16_cmpset(&vq->last_used_idx_res, res_base_idx,
141 } while (unlikely(success == 0));
142 res_cur_idx = res_base_idx;
143 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") Current Index %d| End Index %d\n", dev->device_fh, res_cur_idx, res_end_idx);
145 /* Prefetch available ring to retrieve indexes. */
146 rte_prefetch0(&vq->avail->ring[res_cur_idx & (vq->size - 1)]);
148 /* Retrieve all of the head indexes first to avoid caching issues. */
149 for (head_idx = 0; head_idx < count; head_idx++)
150 head[head_idx] = vq->avail->ring[(res_cur_idx + head_idx) & (vq->size - 1)];
152 /*Prefetch descriptor index. */
153 rte_prefetch0(&vq->desc[head[packet_success]]);
155 while (res_cur_idx != res_end_idx) {
156 /* Get descriptor from available ring */
157 desc = &vq->desc[head[packet_success]];
159 buff = pkts[packet_success];
161 /* Convert from gpa to vva (guest physical addr -> vhost virtual addr) */
162 buff_addr = gpa_to_vva(dev, desc->addr);
163 /* Prefetch buffer address. */
164 rte_prefetch0((void*)(uintptr_t)buff_addr);
166 /* Copy virtio_hdr to packet and increment buffer address */
167 buff_hdr_addr = buff_addr;
168 packet_len = rte_pktmbuf_data_len(buff) + vq->vhost_hlen;
171 * If the descriptors are chained the header and data are
172 * placed in separate buffers.
174 if (desc->flags & VRING_DESC_F_NEXT) {
175 desc->len = vq->vhost_hlen;
176 desc = &vq->desc[desc->next];
177 /* Buffer address translation. */
178 buff_addr = gpa_to_vva(dev, desc->addr);
179 desc->len = rte_pktmbuf_data_len(buff);
181 buff_addr += vq->vhost_hlen;
182 desc->len = packet_len;
185 /* Update used ring with desc information */
186 vq->used->ring[res_cur_idx & (vq->size - 1)].id = head[packet_success];
187 vq->used->ring[res_cur_idx & (vq->size - 1)].len = packet_len;
189 /* Copy mbuf data to buffer */
190 rte_memcpy((void *)(uintptr_t)buff_addr,
191 rte_pktmbuf_mtod(buff, const void *),
192 rte_pktmbuf_data_len(buff));
193 PRINT_PACKET(dev, (uintptr_t)buff_addr,
194 rte_pktmbuf_data_len(buff), 0);
199 rte_memcpy((void *)(uintptr_t)buff_hdr_addr,
200 (const void *)&virtio_hdr, vq->vhost_hlen);
202 PRINT_PACKET(dev, (uintptr_t)buff_hdr_addr, vq->vhost_hlen, 1);
204 if (res_cur_idx < res_end_idx) {
205 /* Prefetch descriptor index. */
206 rte_prefetch0(&vq->desc[head[packet_success]]);
210 rte_compiler_barrier();
212 /* Wait until it's our turn to add our buffer to the used ring. */
213 while (unlikely(vq->last_used_idx != res_base_idx))
216 *(volatile uint16_t *)&vq->used->idx += count;
217 vq->last_used_idx = res_end_idx;
219 /* Kick the guest if necessary. */
220 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
221 eventfd_write((int)vq->kickfd, 1);
225 static inline uint32_t __attribute__((always_inline))
226 copy_from_mbuf_to_vring(struct virtio_net *dev,
227 uint16_t res_base_idx, uint16_t res_end_idx,
228 struct rte_mbuf *pkt)
230 uint32_t vec_idx = 0;
231 uint32_t entry_success = 0;
232 struct vhost_virtqueue *vq;
233 /* The virtio_hdr is initialised to 0. */
234 struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {
235 {0, 0, 0, 0, 0, 0}, 0};
236 uint16_t cur_idx = res_base_idx;
237 uint64_t vb_addr = 0;
238 uint64_t vb_hdr_addr = 0;
239 uint32_t seg_offset = 0;
240 uint32_t vb_offset = 0;
243 uint32_t cpy_len, entry_len;
248 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") Current Index %d| "
250 dev->device_fh, cur_idx, res_end_idx);
253 * Convert from gpa to vva
254 * (guest physical addr -> vhost virtual addr)
256 vq = dev->virtqueue[VIRTIO_RXQ];
258 gpa_to_vva(dev, vq->buf_vec[vec_idx].buf_addr);
259 vb_hdr_addr = vb_addr;
261 /* Prefetch buffer address. */
262 rte_prefetch0((void *)(uintptr_t)vb_addr);
264 virtio_hdr.num_buffers = res_end_idx - res_base_idx;
266 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") RX: Num merge buffers %d\n",
267 dev->device_fh, virtio_hdr.num_buffers);
269 rte_memcpy((void *)(uintptr_t)vb_hdr_addr,
270 (const void *)&virtio_hdr, vq->vhost_hlen);
272 PRINT_PACKET(dev, (uintptr_t)vb_hdr_addr, vq->vhost_hlen, 1);
274 seg_avail = rte_pktmbuf_data_len(pkt);
275 vb_offset = vq->vhost_hlen;
277 vq->buf_vec[vec_idx].buf_len - vq->vhost_hlen;
279 entry_len = vq->vhost_hlen;
283 vq->buf_vec[vec_idx].desc_idx;
284 vq->desc[desc_idx].len = vq->vhost_hlen;
286 if ((vq->desc[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
301 gpa_to_vva(dev, vq->buf_vec[vec_idx].buf_addr);
303 /* Prefetch buffer address. */
304 rte_prefetch0((void *)(uintptr_t)vb_addr);
306 vb_avail = vq->buf_vec[vec_idx].buf_len;
309 cpy_len = RTE_MIN(vb_avail, seg_avail);
311 while (cpy_len > 0) {
312 /* Copy mbuf data to vring buffer */
313 rte_memcpy((void *)(uintptr_t)(vb_addr + vb_offset),
314 (const void *)(rte_pktmbuf_mtod(pkt, char*) + seg_offset),
318 (uintptr_t)(vb_addr + vb_offset),
321 seg_offset += cpy_len;
322 vb_offset += cpy_len;
323 seg_avail -= cpy_len;
325 entry_len += cpy_len;
327 if (seg_avail != 0) {
329 * The virtio buffer in this vring
330 * entry reach to its end.
331 * But the segment doesn't complete.
333 if ((vq->desc[vq->buf_vec[vec_idx].desc_idx].flags &
334 VRING_DESC_F_NEXT) == 0) {
335 /* Update used ring with desc information */
336 vq->used->ring[cur_idx & (vq->size - 1)].id
337 = vq->buf_vec[vec_idx].desc_idx;
338 vq->used->ring[cur_idx & (vq->size - 1)].len
346 vb_addr = gpa_to_vva(dev,
347 vq->buf_vec[vec_idx].buf_addr);
349 vb_avail = vq->buf_vec[vec_idx].buf_len;
350 cpy_len = RTE_MIN(vb_avail, seg_avail);
353 * This current segment complete, need continue to
354 * check if the whole packet complete or not.
359 * There are more segments.
363 * This current buffer from vring is
364 * used up, need fetch next buffer
368 vq->buf_vec[vec_idx].desc_idx;
369 vq->desc[desc_idx].len = vb_offset;
371 if ((vq->desc[desc_idx].flags &
372 VRING_DESC_F_NEXT) == 0) {
373 uint16_t wrapped_idx =
374 cur_idx & (vq->size - 1);
376 * Update used ring with the
377 * descriptor information
379 vq->used->ring[wrapped_idx].id
381 vq->used->ring[wrapped_idx].len
388 /* Get next buffer from buf_vec. */
390 vb_addr = gpa_to_vva(dev,
391 vq->buf_vec[vec_idx].buf_addr);
393 vq->buf_vec[vec_idx].buf_len;
398 seg_avail = rte_pktmbuf_data_len(pkt);
399 cpy_len = RTE_MIN(vb_avail, seg_avail);
402 * This whole packet completes.
405 vq->buf_vec[vec_idx].desc_idx;
406 vq->desc[desc_idx].len = vb_offset;
408 while (vq->desc[desc_idx].flags &
410 desc_idx = vq->desc[desc_idx].next;
411 vq->desc[desc_idx].len = 0;
414 /* Update used ring with desc information */
415 vq->used->ring[cur_idx & (vq->size - 1)].id
416 = vq->buf_vec[vec_idx].desc_idx;
417 vq->used->ring[cur_idx & (vq->size - 1)].len
423 cpy_len = RTE_MIN(vb_avail, seg_avail);
428 return entry_success;
432 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
433 * be received from the physical port or from another virtio device. A packet
434 * count is returned to indicate the number of packets that were succesfully
435 * added to the RX queue. This function works for mergeable RX.
437 static inline uint32_t __attribute__((always_inline))
438 virtio_dev_merge_rx(struct virtio_net *dev, struct rte_mbuf **pkts,
441 struct vhost_virtqueue *vq;
442 uint32_t pkt_idx = 0, entry_success = 0;
444 uint16_t avail_idx, res_cur_idx;
445 uint16_t res_base_idx, res_end_idx;
448 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") virtio_dev_merge_rx()\n",
450 vq = dev->virtqueue[VIRTIO_RXQ];
451 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
456 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
457 uint32_t secure_len = 0;
459 uint32_t vec_idx = 0;
460 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + vq->vhost_hlen;
465 * As many data cores may want access to available
466 * buffers, they need to be reserved.
468 res_base_idx = vq->last_used_idx_res;
469 res_cur_idx = res_base_idx;
472 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
473 if (unlikely(res_cur_idx == avail_idx)) {
475 * If retry is enabled and the queue is
476 * full then we wait and retry to avoid
481 for (retry = 0; retry < burst_rx_retry_num; retry++) {
482 rte_delay_us(burst_rx_delay_time);
484 *((volatile uint16_t *)&vq->avail->idx);
485 if (likely(res_cur_idx != avail_idx)) {
494 LOG_DEBUG(VHOST_DATA,
495 "(%"PRIu64") Failed "
496 "to get enough desc from "
501 uint16_t wrapped_idx =
502 (res_cur_idx) & (vq->size - 1);
504 vq->avail->ring[wrapped_idx];
509 secure_len += vq->desc[idx].len;
510 if (vq->desc[idx].flags &
512 idx = vq->desc[idx].next;
519 } while (pkt_len > secure_len);
521 /* vq->last_used_idx_res is atomically updated. */
522 success = rte_atomic16_cmpset(&vq->last_used_idx_res,
525 } while (success == 0);
528 need_cnt = res_cur_idx - res_base_idx;
530 for (i = 0; i < need_cnt; i++, id++) {
531 uint16_t wrapped_idx = id & (vq->size - 1);
532 uint32_t idx = vq->avail->ring[wrapped_idx];
536 vq->buf_vec[vec_idx].buf_addr =
538 vq->buf_vec[vec_idx].buf_len =
540 vq->buf_vec[vec_idx].desc_idx = idx;
543 if (vq->desc[idx].flags & VRING_DESC_F_NEXT) {
544 idx = vq->desc[idx].next;
550 res_end_idx = res_cur_idx;
552 entry_success = copy_from_mbuf_to_vring(dev, res_base_idx,
553 res_end_idx, pkts[pkt_idx]);
555 rte_compiler_barrier();
558 * Wait until it's our turn to add our buffer
561 while (unlikely(vq->last_used_idx != res_base_idx))
564 *(volatile uint16_t *)&vq->used->idx += entry_success;
565 vq->last_used_idx = res_end_idx;
567 /* Kick the guest if necessary. */
568 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
569 eventfd_write((int)vq->kickfd, 1);
575 /* This function works for TX packets with mergeable feature enabled. */
576 static inline void __attribute__((always_inline))
577 virtio_dev_merge_tx(struct virtio_net *dev, struct rte_mempool *mbuf_pool)
579 struct rte_mbuf *m, *prev;
580 struct vhost_virtqueue *vq;
581 struct vring_desc *desc;
582 uint64_t vb_addr = 0;
583 uint32_t head[MAX_PKT_BURST];
586 uint16_t free_entries, entry_success = 0;
588 uint32_t buf_size = MBUF_SIZE - (sizeof(struct rte_mbuf)
589 + RTE_PKTMBUF_HEADROOM);
591 vq = dev->virtqueue[VIRTIO_TXQ];
592 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
594 /* If there are no available buffers then return. */
595 if (vq->last_used_idx == avail_idx)
598 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") virtio_dev_merge_tx()\n",
601 /* Prefetch available ring to retrieve head indexes. */
602 rte_prefetch0(&vq->avail->ring[vq->last_used_idx & (vq->size - 1)]);
604 /*get the number of free entries in the ring*/
605 free_entries = (avail_idx - vq->last_used_idx);
607 /* Limit to MAX_PKT_BURST. */
608 free_entries = RTE_MIN(free_entries, MAX_PKT_BURST);
610 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") Buffers available %d\n",
611 dev->device_fh, free_entries);
612 /* Retrieve all of the head indexes first to avoid caching issues. */
613 for (i = 0; i < free_entries; i++)
614 head[i] = vq->avail->ring[(vq->last_used_idx + i) & (vq->size - 1)];
616 /* Prefetch descriptor index. */
617 rte_prefetch0(&vq->desc[head[entry_success]]);
618 rte_prefetch0(&vq->used->ring[vq->last_used_idx & (vq->size - 1)]);
620 while (entry_success < free_entries) {
621 uint32_t vb_avail, vb_offset;
622 uint32_t seg_avail, seg_offset;
624 uint32_t seg_num = 0;
625 struct rte_mbuf *cur;
626 uint8_t alloc_err = 0;
628 desc = &vq->desc[head[entry_success]];
630 /* Discard first buffer as it is the virtio header */
631 desc = &vq->desc[desc->next];
633 /* Buffer address translation. */
634 vb_addr = gpa_to_vva(dev, desc->addr);
635 /* Prefetch buffer address. */
636 rte_prefetch0((void *)(uintptr_t)vb_addr);
638 used_idx = vq->last_used_idx & (vq->size - 1);
640 if (entry_success < (free_entries - 1)) {
641 /* Prefetch descriptor index. */
642 rte_prefetch0(&vq->desc[head[entry_success+1]]);
643 rte_prefetch0(&vq->used->ring[(used_idx + 1) & (vq->size - 1)]);
646 /* Update used index buffer information. */
647 vq->used->ring[used_idx].id = head[entry_success];
648 vq->used->ring[used_idx].len = 0;
651 vb_avail = desc->len;
653 seg_avail = buf_size;
654 cpy_len = RTE_MIN(vb_avail, seg_avail);
656 PRINT_PACKET(dev, (uintptr_t)vb_addr, desc->len, 0);
658 /* Allocate an mbuf and populate the structure. */
659 m = rte_pktmbuf_alloc(mbuf_pool);
660 if (unlikely(m == NULL)) {
661 RTE_LOG(ERR, VHOST_DATA,
662 "Failed to allocate memory for mbuf.\n");
669 while (cpy_len != 0) {
670 rte_memcpy((void *)(rte_pktmbuf_mtod(cur, char *) + seg_offset),
671 (void *)((uintptr_t)(vb_addr + vb_offset)),
674 seg_offset += cpy_len;
675 vb_offset += cpy_len;
677 seg_avail -= cpy_len;
681 * The segment reachs to its end,
682 * while the virtio buffer in TX vring has
683 * more data to be copied.
685 cur->data_len = seg_offset;
686 m->pkt_len += seg_offset;
687 /* Allocate mbuf and populate the structure. */
688 cur = rte_pktmbuf_alloc(mbuf_pool);
689 if (unlikely(cur == NULL)) {
690 RTE_LOG(ERR, VHOST_DATA, "Failed to "
691 "allocate memory for mbuf.\n");
701 seg_avail = buf_size;
703 if (desc->flags & VRING_DESC_F_NEXT) {
705 * There are more virtio buffers in
706 * same vring entry need to be copied.
708 if (seg_avail == 0) {
710 * The current segment hasn't
711 * room to accomodate more
714 cur->data_len = seg_offset;
715 m->pkt_len += seg_offset;
717 * Allocate an mbuf and
718 * populate the structure.
720 cur = rte_pktmbuf_alloc(mbuf_pool);
721 if (unlikely(cur == NULL)) {
735 seg_avail = buf_size;
738 desc = &vq->desc[desc->next];
740 /* Buffer address translation. */
741 vb_addr = gpa_to_vva(dev, desc->addr);
742 /* Prefetch buffer address. */
743 rte_prefetch0((void *)(uintptr_t)vb_addr);
745 vb_avail = desc->len;
747 PRINT_PACKET(dev, (uintptr_t)vb_addr,
750 /* The whole packet completes. */
751 cur->data_len = seg_offset;
752 m->pkt_len += seg_offset;
757 cpy_len = RTE_MIN(vb_avail, seg_avail);
760 if (unlikely(alloc_err == 1))
763 m->nb_segs = seg_num;
766 * If this is the first received packet we need to learn
767 * the MAC and setup VMDQ
769 if (dev->ready == DEVICE_MAC_LEARNING) {
770 if (dev->remove || (link_vmdq(dev, m) == -1)) {
772 * Discard frame if device is scheduled for
773 * removal or a duplicate MAC address is found.
775 entry_success = free_entries;
776 vq->last_used_idx += entry_success;
782 virtio_tx_route(dev, m, mbuf_pool, (uint16_t)dev->device_fh);
788 rte_compiler_barrier();
789 vq->used->idx += entry_success;
790 /* Kick guest if required. */
791 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
792 eventfd_write((int)vq->kickfd, 1);