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_ether.h>
42 #include <rte_virtio_net.h>
47 #include "vhost-net.h"
49 #define MAX_PKT_BURST 32
52 is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t qp_nb)
54 return (is_tx ^ (idx & 1)) == 0 && idx < qp_nb * VIRTIO_QNUM;
58 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
60 memset(net_hdr, 0, sizeof(struct virtio_net_hdr));
62 if (m_buf->ol_flags & PKT_TX_L4_MASK) {
63 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
64 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
66 switch (m_buf->ol_flags & PKT_TX_L4_MASK) {
67 case PKT_TX_TCP_CKSUM:
68 net_hdr->csum_offset = (offsetof(struct tcp_hdr,
71 case PKT_TX_UDP_CKSUM:
72 net_hdr->csum_offset = (offsetof(struct udp_hdr,
75 case PKT_TX_SCTP_CKSUM:
76 net_hdr->csum_offset = (offsetof(struct sctp_hdr,
82 if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
83 if (m_buf->ol_flags & PKT_TX_IPV4)
84 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
86 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
87 net_hdr->gso_size = m_buf->tso_segsz;
88 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
96 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
97 * be received from the physical port or from another virtio device. A packet
98 * count is returned to indicate the number of packets that are succesfully
99 * added to the RX queue. This function works when the mbuf is scattered, but
100 * it doesn't support the mergeable feature.
102 static inline uint32_t __attribute__((always_inline))
103 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
104 struct rte_mbuf **pkts, uint32_t count)
106 struct vhost_virtqueue *vq;
107 struct vring_desc *desc;
108 struct rte_mbuf *buff, *first_buff;
109 /* The virtio_hdr is initialised to 0. */
110 struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0, 0, 0, 0, 0, 0}, 0};
111 uint64_t buff_addr = 0;
112 uint64_t buff_hdr_addr = 0;
113 uint32_t head[MAX_PKT_BURST];
114 uint32_t head_idx, packet_success = 0;
115 uint16_t avail_idx, res_cur_idx;
116 uint16_t res_base_idx, res_end_idx;
117 uint16_t free_entries;
120 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") virtio_dev_rx()\n", dev->device_fh);
121 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->virt_qp_nb))) {
122 RTE_LOG(ERR, VHOST_DATA,
123 "%s (%"PRIu64"): virtqueue idx:%d invalid.\n",
124 __func__, dev->device_fh, queue_id);
128 vq = dev->virtqueue[queue_id];
129 if (unlikely(vq->enabled == 0))
132 count = (count > MAX_PKT_BURST) ? MAX_PKT_BURST : count;
135 * As many data cores may want access to available buffers,
136 * they need to be reserved.
139 res_base_idx = vq->last_used_idx_res;
140 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
142 free_entries = (avail_idx - res_base_idx);
143 /*check that we have enough buffers*/
144 if (unlikely(count > free_entries))
145 count = free_entries;
150 res_end_idx = res_base_idx + count;
151 /* vq->last_used_idx_res is atomically updated. */
152 /* TODO: Allow to disable cmpset if no concurrency in application. */
153 success = rte_atomic16_cmpset(&vq->last_used_idx_res,
154 res_base_idx, res_end_idx);
155 } while (unlikely(success == 0));
156 res_cur_idx = res_base_idx;
157 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") Current Index %d| End Index %d\n",
158 dev->device_fh, res_cur_idx, res_end_idx);
160 /* Prefetch available ring to retrieve indexes. */
161 rte_prefetch0(&vq->avail->ring[res_cur_idx & (vq->size - 1)]);
163 /* Retrieve all of the head indexes first to avoid caching issues. */
164 for (head_idx = 0; head_idx < count; head_idx++)
165 head[head_idx] = vq->avail->ring[(res_cur_idx + head_idx) &
168 /*Prefetch descriptor index. */
169 rte_prefetch0(&vq->desc[head[packet_success]]);
171 while (res_cur_idx != res_end_idx) {
172 uint32_t offset = 0, vb_offset = 0;
173 uint32_t pkt_len, len_to_cpy, data_len, total_copied = 0;
174 uint8_t hdr = 0, uncompleted_pkt = 0;
176 /* Get descriptor from available ring */
177 desc = &vq->desc[head[packet_success]];
179 buff = pkts[packet_success];
182 /* Convert from gpa to vva (guest physical addr -> vhost virtual addr) */
183 buff_addr = gpa_to_vva(dev, desc->addr);
184 /* Prefetch buffer address. */
185 rte_prefetch0((void *)(uintptr_t)buff_addr);
187 /* Copy virtio_hdr to packet and increment buffer address */
188 buff_hdr_addr = buff_addr;
191 * If the descriptors are chained the header and data are
192 * placed in separate buffers.
194 if ((desc->flags & VRING_DESC_F_NEXT) &&
195 (desc->len == vq->vhost_hlen)) {
196 desc = &vq->desc[desc->next];
197 /* Buffer address translation. */
198 buff_addr = gpa_to_vva(dev, desc->addr);
200 vb_offset += vq->vhost_hlen;
204 pkt_len = rte_pktmbuf_pkt_len(buff);
205 data_len = rte_pktmbuf_data_len(buff);
206 len_to_cpy = RTE_MIN(data_len,
207 hdr ? desc->len - vq->vhost_hlen : desc->len);
208 while (total_copied < pkt_len) {
209 /* Copy mbuf data to buffer */
210 rte_memcpy((void *)(uintptr_t)(buff_addr + vb_offset),
211 rte_pktmbuf_mtod_offset(buff, const void *, offset),
213 PRINT_PACKET(dev, (uintptr_t)(buff_addr + vb_offset),
216 offset += len_to_cpy;
217 vb_offset += len_to_cpy;
218 total_copied += len_to_cpy;
220 /* The whole packet completes */
221 if (total_copied == pkt_len)
224 /* The current segment completes */
225 if (offset == data_len) {
228 data_len = rte_pktmbuf_data_len(buff);
231 /* The current vring descriptor done */
232 if (vb_offset == desc->len) {
233 if (desc->flags & VRING_DESC_F_NEXT) {
234 desc = &vq->desc[desc->next];
235 buff_addr = gpa_to_vva(dev, desc->addr);
238 /* Room in vring buffer is not enough */
243 len_to_cpy = RTE_MIN(data_len - offset, desc->len - vb_offset);
246 /* Update used ring with desc information */
247 vq->used->ring[res_cur_idx & (vq->size - 1)].id =
248 head[packet_success];
250 /* Drop the packet if it is uncompleted */
251 if (unlikely(uncompleted_pkt == 1))
252 vq->used->ring[res_cur_idx & (vq->size - 1)].len =
255 vq->used->ring[res_cur_idx & (vq->size - 1)].len =
256 pkt_len + vq->vhost_hlen;
261 if (unlikely(uncompleted_pkt == 1))
264 virtio_enqueue_offload(first_buff, &virtio_hdr.hdr);
266 rte_memcpy((void *)(uintptr_t)buff_hdr_addr,
267 (const void *)&virtio_hdr, vq->vhost_hlen);
269 PRINT_PACKET(dev, (uintptr_t)buff_hdr_addr, vq->vhost_hlen, 1);
271 if (res_cur_idx < res_end_idx) {
272 /* Prefetch descriptor index. */
273 rte_prefetch0(&vq->desc[head[packet_success]]);
277 rte_compiler_barrier();
279 /* Wait until it's our turn to add our buffer to the used ring. */
280 while (unlikely(vq->last_used_idx != res_base_idx))
283 *(volatile uint16_t *)&vq->used->idx += count;
284 vq->last_used_idx = res_end_idx;
286 /* flush used->idx update before we read avail->flags. */
289 /* Kick the guest if necessary. */
290 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
291 eventfd_write(vq->callfd, (eventfd_t)1);
295 static inline uint32_t __attribute__((always_inline))
296 copy_from_mbuf_to_vring(struct virtio_net *dev, uint32_t queue_id,
297 uint16_t res_base_idx, uint16_t res_end_idx,
298 struct rte_mbuf *pkt)
300 uint32_t vec_idx = 0;
301 uint32_t entry_success = 0;
302 struct vhost_virtqueue *vq;
303 /* The virtio_hdr is initialised to 0. */
304 struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {
305 {0, 0, 0, 0, 0, 0}, 0};
306 uint16_t cur_idx = res_base_idx;
307 uint64_t vb_addr = 0;
308 uint64_t vb_hdr_addr = 0;
309 uint32_t seg_offset = 0;
310 uint32_t vb_offset = 0;
313 uint32_t cpy_len, entry_len;
318 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") Current Index %d| "
320 dev->device_fh, cur_idx, res_end_idx);
323 * Convert from gpa to vva
324 * (guest physical addr -> vhost virtual addr)
326 vq = dev->virtqueue[queue_id];
328 vb_addr = gpa_to_vva(dev, vq->buf_vec[vec_idx].buf_addr);
329 vb_hdr_addr = vb_addr;
331 /* Prefetch buffer address. */
332 rte_prefetch0((void *)(uintptr_t)vb_addr);
334 virtio_hdr.num_buffers = res_end_idx - res_base_idx;
336 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") RX: Num merge buffers %d\n",
337 dev->device_fh, virtio_hdr.num_buffers);
339 virtio_enqueue_offload(pkt, &virtio_hdr.hdr);
341 rte_memcpy((void *)(uintptr_t)vb_hdr_addr,
342 (const void *)&virtio_hdr, vq->vhost_hlen);
344 PRINT_PACKET(dev, (uintptr_t)vb_hdr_addr, vq->vhost_hlen, 1);
346 seg_avail = rte_pktmbuf_data_len(pkt);
347 vb_offset = vq->vhost_hlen;
348 vb_avail = vq->buf_vec[vec_idx].buf_len - vq->vhost_hlen;
350 entry_len = vq->vhost_hlen;
354 vq->buf_vec[vec_idx].desc_idx;
356 if ((vq->desc[desc_idx].flags
357 & VRING_DESC_F_NEXT) == 0) {
358 /* Update used ring with desc information */
359 vq->used->ring[cur_idx & (vq->size - 1)].id
360 = vq->buf_vec[vec_idx].desc_idx;
361 vq->used->ring[cur_idx & (vq->size - 1)].len
370 vb_addr = gpa_to_vva(dev, vq->buf_vec[vec_idx].buf_addr);
372 /* Prefetch buffer address. */
373 rte_prefetch0((void *)(uintptr_t)vb_addr);
375 vb_avail = vq->buf_vec[vec_idx].buf_len;
378 cpy_len = RTE_MIN(vb_avail, seg_avail);
380 while (cpy_len > 0) {
381 /* Copy mbuf data to vring buffer */
382 rte_memcpy((void *)(uintptr_t)(vb_addr + vb_offset),
383 rte_pktmbuf_mtod_offset(pkt, const void *, seg_offset),
387 (uintptr_t)(vb_addr + vb_offset),
390 seg_offset += cpy_len;
391 vb_offset += cpy_len;
392 seg_avail -= cpy_len;
394 entry_len += cpy_len;
396 if (seg_avail != 0) {
398 * The virtio buffer in this vring
399 * entry reach to its end.
400 * But the segment doesn't complete.
402 if ((vq->desc[vq->buf_vec[vec_idx].desc_idx].flags &
403 VRING_DESC_F_NEXT) == 0) {
404 /* Update used ring with desc information */
405 vq->used->ring[cur_idx & (vq->size - 1)].id
406 = vq->buf_vec[vec_idx].desc_idx;
407 vq->used->ring[cur_idx & (vq->size - 1)].len
415 vb_addr = gpa_to_vva(dev,
416 vq->buf_vec[vec_idx].buf_addr);
418 vb_avail = vq->buf_vec[vec_idx].buf_len;
419 cpy_len = RTE_MIN(vb_avail, seg_avail);
422 * This current segment complete, need continue to
423 * check if the whole packet complete or not.
428 * There are more segments.
432 * This current buffer from vring is
433 * used up, need fetch next buffer
437 vq->buf_vec[vec_idx].desc_idx;
439 if ((vq->desc[desc_idx].flags &
440 VRING_DESC_F_NEXT) == 0) {
441 uint16_t wrapped_idx =
442 cur_idx & (vq->size - 1);
444 * Update used ring with the
445 * descriptor information
447 vq->used->ring[wrapped_idx].id
449 vq->used->ring[wrapped_idx].len
456 /* Get next buffer from buf_vec. */
458 vb_addr = gpa_to_vva(dev,
459 vq->buf_vec[vec_idx].buf_addr);
461 vq->buf_vec[vec_idx].buf_len;
466 seg_avail = rte_pktmbuf_data_len(pkt);
467 cpy_len = RTE_MIN(vb_avail, seg_avail);
470 * This whole packet completes.
472 /* Update used ring with desc information */
473 vq->used->ring[cur_idx & (vq->size - 1)].id
474 = vq->buf_vec[vec_idx].desc_idx;
475 vq->used->ring[cur_idx & (vq->size - 1)].len
483 return entry_success;
486 static inline void __attribute__((always_inline))
487 update_secure_len(struct vhost_virtqueue *vq, uint32_t id,
488 uint32_t *secure_len, uint32_t *vec_idx)
490 uint16_t wrapped_idx = id & (vq->size - 1);
491 uint32_t idx = vq->avail->ring[wrapped_idx];
493 uint32_t len = *secure_len;
494 uint32_t vec_id = *vec_idx;
498 len += vq->desc[idx].len;
499 vq->buf_vec[vec_id].buf_addr = vq->desc[idx].addr;
500 vq->buf_vec[vec_id].buf_len = vq->desc[idx].len;
501 vq->buf_vec[vec_id].desc_idx = idx;
504 if (vq->desc[idx].flags & VRING_DESC_F_NEXT) {
505 idx = vq->desc[idx].next;
515 * This function works for mergeable RX.
517 static inline uint32_t __attribute__((always_inline))
518 virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,
519 struct rte_mbuf **pkts, uint32_t count)
521 struct vhost_virtqueue *vq;
522 uint32_t pkt_idx = 0, entry_success = 0;
524 uint16_t res_base_idx, res_cur_idx;
527 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") virtio_dev_merge_rx()\n",
529 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->virt_qp_nb))) {
530 RTE_LOG(ERR, VHOST_DATA,
531 "%s (%"PRIu64"): virtqueue idx:%d invalid.\n",
532 __func__, dev->device_fh, queue_id);
536 vq = dev->virtqueue[queue_id];
537 if (unlikely(vq->enabled == 0))
540 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
545 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
546 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + vq->vhost_hlen;
550 * As many data cores may want access to available
551 * buffers, they need to be reserved.
553 uint32_t secure_len = 0;
554 uint32_t vec_idx = 0;
556 res_base_idx = vq->last_used_idx_res;
557 res_cur_idx = res_base_idx;
560 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
561 if (unlikely(res_cur_idx == avail_idx))
564 update_secure_len(vq, res_cur_idx,
565 &secure_len, &vec_idx);
567 } while (pkt_len > secure_len);
569 /* vq->last_used_idx_res is atomically updated. */
570 success = rte_atomic16_cmpset(&vq->last_used_idx_res,
573 } while (success == 0);
575 entry_success = copy_from_mbuf_to_vring(dev, queue_id,
576 res_base_idx, res_cur_idx, pkts[pkt_idx]);
578 rte_compiler_barrier();
581 * Wait until it's our turn to add our buffer
584 while (unlikely(vq->last_used_idx != res_base_idx))
587 *(volatile uint16_t *)&vq->used->idx += entry_success;
588 vq->last_used_idx = res_cur_idx;
592 if (likely(pkt_idx)) {
593 /* flush used->idx update before we read avail->flags. */
596 /* Kick the guest if necessary. */
597 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
598 eventfd_write(vq->callfd, (eventfd_t)1);
605 rte_vhost_enqueue_burst(struct virtio_net *dev, uint16_t queue_id,
606 struct rte_mbuf **pkts, uint16_t count)
608 if (unlikely(dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)))
609 return virtio_dev_merge_rx(dev, queue_id, pkts, count);
611 return virtio_dev_rx(dev, queue_id, pkts, count);
615 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
617 struct ipv4_hdr *ipv4_hdr;
618 struct ipv6_hdr *ipv6_hdr;
620 struct ether_hdr *eth_hdr;
623 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
625 m->l2_len = sizeof(struct ether_hdr);
626 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
628 if (ethertype == ETHER_TYPE_VLAN) {
629 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
631 m->l2_len += sizeof(struct vlan_hdr);
632 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
635 l3_hdr = (char *)eth_hdr + m->l2_len;
638 case ETHER_TYPE_IPv4:
639 ipv4_hdr = (struct ipv4_hdr *)l3_hdr;
640 *l4_proto = ipv4_hdr->next_proto_id;
641 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
642 *l4_hdr = (char *)l3_hdr + m->l3_len;
643 m->ol_flags |= PKT_TX_IPV4;
645 case ETHER_TYPE_IPv6:
646 ipv6_hdr = (struct ipv6_hdr *)l3_hdr;
647 *l4_proto = ipv6_hdr->proto;
648 m->l3_len = sizeof(struct ipv6_hdr);
649 *l4_hdr = (char *)l3_hdr + m->l3_len;
650 m->ol_flags |= PKT_TX_IPV6;
659 static inline void __attribute__((always_inline))
660 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
662 uint16_t l4_proto = 0;
664 struct tcp_hdr *tcp_hdr = NULL;
666 parse_ethernet(m, &l4_proto, &l4_hdr);
667 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
668 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
669 switch (hdr->csum_offset) {
670 case (offsetof(struct tcp_hdr, cksum)):
671 if (l4_proto == IPPROTO_TCP)
672 m->ol_flags |= PKT_TX_TCP_CKSUM;
674 case (offsetof(struct udp_hdr, dgram_cksum)):
675 if (l4_proto == IPPROTO_UDP)
676 m->ol_flags |= PKT_TX_UDP_CKSUM;
678 case (offsetof(struct sctp_hdr, cksum)):
679 if (l4_proto == IPPROTO_SCTP)
680 m->ol_flags |= PKT_TX_SCTP_CKSUM;
688 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
689 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
690 case VIRTIO_NET_HDR_GSO_TCPV4:
691 case VIRTIO_NET_HDR_GSO_TCPV6:
692 tcp_hdr = (struct tcp_hdr *)l4_hdr;
693 m->ol_flags |= PKT_TX_TCP_SEG;
694 m->tso_segsz = hdr->gso_size;
695 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
698 RTE_LOG(WARNING, VHOST_DATA,
699 "unsupported gso type %u.\n", hdr->gso_type);
706 rte_vhost_dequeue_burst(struct virtio_net *dev, uint16_t queue_id,
707 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
709 struct rte_mbuf *m, *prev;
710 struct vhost_virtqueue *vq;
711 struct vring_desc *desc;
712 uint64_t vb_addr = 0;
713 uint64_t vb_net_hdr_addr = 0;
714 uint32_t head[MAX_PKT_BURST];
717 uint16_t free_entries, entry_success = 0;
719 struct virtio_net_hdr *hdr = NULL;
721 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->virt_qp_nb))) {
722 RTE_LOG(ERR, VHOST_DATA,
723 "%s (%"PRIu64"): virtqueue idx:%d invalid.\n",
724 __func__, dev->device_fh, queue_id);
728 vq = dev->virtqueue[queue_id];
729 if (unlikely(vq->enabled == 0))
732 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
734 /* If there are no available buffers then return. */
735 if (vq->last_used_idx == avail_idx)
738 LOG_DEBUG(VHOST_DATA, "%s (%"PRIu64")\n", __func__,
741 /* Prefetch available ring to retrieve head indexes. */
742 rte_prefetch0(&vq->avail->ring[vq->last_used_idx & (vq->size - 1)]);
744 /*get the number of free entries in the ring*/
745 free_entries = (avail_idx - vq->last_used_idx);
747 free_entries = RTE_MIN(free_entries, count);
748 /* Limit to MAX_PKT_BURST. */
749 free_entries = RTE_MIN(free_entries, MAX_PKT_BURST);
751 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") Buffers available %d\n",
752 dev->device_fh, free_entries);
753 /* Retrieve all of the head indexes first to avoid caching issues. */
754 for (i = 0; i < free_entries; i++)
755 head[i] = vq->avail->ring[(vq->last_used_idx + i) & (vq->size - 1)];
757 /* Prefetch descriptor index. */
758 rte_prefetch0(&vq->desc[head[entry_success]]);
759 rte_prefetch0(&vq->used->ring[vq->last_used_idx & (vq->size - 1)]);
761 while (entry_success < free_entries) {
762 uint32_t vb_avail, vb_offset;
763 uint32_t seg_avail, seg_offset;
765 uint32_t seg_num = 0;
766 struct rte_mbuf *cur;
767 uint8_t alloc_err = 0;
769 desc = &vq->desc[head[entry_success]];
771 vb_net_hdr_addr = gpa_to_vva(dev, desc->addr);
772 hdr = (struct virtio_net_hdr *)((uintptr_t)vb_net_hdr_addr);
774 /* Discard first buffer as it is the virtio header */
775 if (desc->flags & VRING_DESC_F_NEXT) {
776 desc = &vq->desc[desc->next];
778 vb_avail = desc->len;
780 vb_offset = vq->vhost_hlen;
781 vb_avail = desc->len - vb_offset;
784 /* Buffer address translation. */
785 vb_addr = gpa_to_vva(dev, desc->addr);
786 /* Prefetch buffer address. */
787 rte_prefetch0((void *)(uintptr_t)vb_addr);
789 used_idx = vq->last_used_idx & (vq->size - 1);
791 if (entry_success < (free_entries - 1)) {
792 /* Prefetch descriptor index. */
793 rte_prefetch0(&vq->desc[head[entry_success+1]]);
794 rte_prefetch0(&vq->used->ring[(used_idx + 1) & (vq->size - 1)]);
797 /* Update used index buffer information. */
798 vq->used->ring[used_idx].id = head[entry_success];
799 vq->used->ring[used_idx].len = 0;
801 /* Allocate an mbuf and populate the structure. */
802 m = rte_pktmbuf_alloc(mbuf_pool);
803 if (unlikely(m == NULL)) {
804 RTE_LOG(ERR, VHOST_DATA,
805 "Failed to allocate memory for mbuf.\n");
809 seg_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
810 cpy_len = RTE_MIN(vb_avail, seg_avail);
812 PRINT_PACKET(dev, (uintptr_t)vb_addr, desc->len, 0);
817 while (cpy_len != 0) {
818 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *, seg_offset),
819 (void *)((uintptr_t)(vb_addr + vb_offset)),
822 seg_offset += cpy_len;
823 vb_offset += cpy_len;
825 seg_avail -= cpy_len;
829 * The segment reachs to its end,
830 * while the virtio buffer in TX vring has
831 * more data to be copied.
833 cur->data_len = seg_offset;
834 m->pkt_len += seg_offset;
835 /* Allocate mbuf and populate the structure. */
836 cur = rte_pktmbuf_alloc(mbuf_pool);
837 if (unlikely(cur == NULL)) {
838 RTE_LOG(ERR, VHOST_DATA, "Failed to "
839 "allocate memory for mbuf.\n");
849 seg_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
851 if (desc->flags & VRING_DESC_F_NEXT) {
853 * There are more virtio buffers in
854 * same vring entry need to be copied.
856 if (seg_avail == 0) {
858 * The current segment hasn't
859 * room to accomodate more
862 cur->data_len = seg_offset;
863 m->pkt_len += seg_offset;
865 * Allocate an mbuf and
866 * populate the structure.
868 cur = rte_pktmbuf_alloc(mbuf_pool);
869 if (unlikely(cur == NULL)) {
883 seg_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
886 desc = &vq->desc[desc->next];
888 /* Buffer address translation. */
889 vb_addr = gpa_to_vva(dev, desc->addr);
890 /* Prefetch buffer address. */
891 rte_prefetch0((void *)(uintptr_t)vb_addr);
893 vb_avail = desc->len;
895 PRINT_PACKET(dev, (uintptr_t)vb_addr,
898 /* The whole packet completes. */
899 cur->data_len = seg_offset;
900 m->pkt_len += seg_offset;
905 cpy_len = RTE_MIN(vb_avail, seg_avail);
908 if (unlikely(alloc_err == 1))
911 m->nb_segs = seg_num;
912 if ((hdr->flags != 0) || (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE))
913 vhost_dequeue_offload(hdr, m);
915 pkts[entry_success] = m;
920 rte_compiler_barrier();
921 vq->used->idx += entry_success;
922 /* Kick guest if required. */
923 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
924 eventfd_write(vq->callfd, (eventfd_t)1);
925 return entry_success;