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 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
59 * be received from the physical port or from another virtio device. A packet
60 * count is returned to indicate the number of packets that are succesfully
61 * added to the RX queue. This function works when the mbuf is scattered, but
62 * it doesn't support the mergeable feature.
64 static inline uint32_t __attribute__((always_inline))
65 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
66 struct rte_mbuf **pkts, uint32_t count)
68 struct vhost_virtqueue *vq;
69 struct vring_desc *desc;
70 struct rte_mbuf *buff;
71 /* The virtio_hdr is initialised to 0. */
72 struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0, 0, 0, 0, 0, 0}, 0};
73 uint64_t buff_addr = 0;
74 uint64_t buff_hdr_addr = 0;
75 uint32_t head[MAX_PKT_BURST];
76 uint32_t head_idx, packet_success = 0;
77 uint16_t avail_idx, res_cur_idx;
78 uint16_t res_base_idx, res_end_idx;
79 uint16_t free_entries;
82 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") virtio_dev_rx()\n", dev->device_fh);
83 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->virt_qp_nb))) {
84 RTE_LOG(ERR, VHOST_DATA,
85 "%s (%"PRIu64"): virtqueue idx:%d invalid.\n",
86 __func__, dev->device_fh, queue_id);
90 vq = dev->virtqueue[queue_id];
91 if (unlikely(vq->enabled == 0))
94 count = (count > MAX_PKT_BURST) ? MAX_PKT_BURST : count;
97 * As many data cores may want access to available buffers,
98 * they need to be reserved.
101 res_base_idx = vq->last_used_idx_res;
102 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
104 free_entries = (avail_idx - res_base_idx);
105 /*check that we have enough buffers*/
106 if (unlikely(count > free_entries))
107 count = free_entries;
112 res_end_idx = res_base_idx + count;
113 /* vq->last_used_idx_res is atomically updated. */
114 /* TODO: Allow to disable cmpset if no concurrency in application. */
115 success = rte_atomic16_cmpset(&vq->last_used_idx_res,
116 res_base_idx, res_end_idx);
117 } while (unlikely(success == 0));
118 res_cur_idx = res_base_idx;
119 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") Current Index %d| End Index %d\n",
120 dev->device_fh, res_cur_idx, res_end_idx);
122 /* Prefetch available ring to retrieve indexes. */
123 rte_prefetch0(&vq->avail->ring[res_cur_idx & (vq->size - 1)]);
125 /* Retrieve all of the head indexes first to avoid caching issues. */
126 for (head_idx = 0; head_idx < count; head_idx++)
127 head[head_idx] = vq->avail->ring[(res_cur_idx + head_idx) &
130 /*Prefetch descriptor index. */
131 rte_prefetch0(&vq->desc[head[packet_success]]);
133 while (res_cur_idx != res_end_idx) {
134 uint32_t offset = 0, vb_offset = 0;
135 uint32_t pkt_len, len_to_cpy, data_len, total_copied = 0;
136 uint8_t hdr = 0, uncompleted_pkt = 0;
138 /* Get descriptor from available ring */
139 desc = &vq->desc[head[packet_success]];
141 buff = pkts[packet_success];
143 /* Convert from gpa to vva (guest physical addr -> vhost virtual addr) */
144 buff_addr = gpa_to_vva(dev, desc->addr);
145 /* Prefetch buffer address. */
146 rte_prefetch0((void *)(uintptr_t)buff_addr);
148 /* Copy virtio_hdr to packet and increment buffer address */
149 buff_hdr_addr = buff_addr;
152 * If the descriptors are chained the header and data are
153 * placed in separate buffers.
155 if ((desc->flags & VRING_DESC_F_NEXT) &&
156 (desc->len == vq->vhost_hlen)) {
157 desc = &vq->desc[desc->next];
158 /* Buffer address translation. */
159 buff_addr = gpa_to_vva(dev, desc->addr);
161 vb_offset += vq->vhost_hlen;
165 pkt_len = rte_pktmbuf_pkt_len(buff);
166 data_len = rte_pktmbuf_data_len(buff);
167 len_to_cpy = RTE_MIN(data_len,
168 hdr ? desc->len - vq->vhost_hlen : desc->len);
169 while (total_copied < pkt_len) {
170 /* Copy mbuf data to buffer */
171 rte_memcpy((void *)(uintptr_t)(buff_addr + vb_offset),
172 rte_pktmbuf_mtod_offset(buff, const void *, offset),
174 PRINT_PACKET(dev, (uintptr_t)(buff_addr + vb_offset),
177 offset += len_to_cpy;
178 vb_offset += len_to_cpy;
179 total_copied += len_to_cpy;
181 /* The whole packet completes */
182 if (total_copied == pkt_len)
185 /* The current segment completes */
186 if (offset == data_len) {
189 data_len = rte_pktmbuf_data_len(buff);
192 /* The current vring descriptor done */
193 if (vb_offset == desc->len) {
194 if (desc->flags & VRING_DESC_F_NEXT) {
195 desc = &vq->desc[desc->next];
196 buff_addr = gpa_to_vva(dev, desc->addr);
199 /* Room in vring buffer is not enough */
204 len_to_cpy = RTE_MIN(data_len - offset, desc->len - vb_offset);
207 /* Update used ring with desc information */
208 vq->used->ring[res_cur_idx & (vq->size - 1)].id =
209 head[packet_success];
211 /* Drop the packet if it is uncompleted */
212 if (unlikely(uncompleted_pkt == 1))
213 vq->used->ring[res_cur_idx & (vq->size - 1)].len =
216 vq->used->ring[res_cur_idx & (vq->size - 1)].len =
217 pkt_len + vq->vhost_hlen;
222 if (unlikely(uncompleted_pkt == 1))
225 rte_memcpy((void *)(uintptr_t)buff_hdr_addr,
226 (const void *)&virtio_hdr, vq->vhost_hlen);
228 PRINT_PACKET(dev, (uintptr_t)buff_hdr_addr, vq->vhost_hlen, 1);
230 if (res_cur_idx < res_end_idx) {
231 /* Prefetch descriptor index. */
232 rte_prefetch0(&vq->desc[head[packet_success]]);
236 rte_compiler_barrier();
238 /* Wait until it's our turn to add our buffer to the used ring. */
239 while (unlikely(vq->last_used_idx != res_base_idx))
242 *(volatile uint16_t *)&vq->used->idx += count;
243 vq->last_used_idx = res_end_idx;
245 /* flush used->idx update before we read avail->flags. */
248 /* Kick the guest if necessary. */
249 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
250 eventfd_write(vq->callfd, (eventfd_t)1);
254 static inline uint32_t __attribute__((always_inline))
255 copy_from_mbuf_to_vring(struct virtio_net *dev, uint32_t queue_id,
256 uint16_t res_base_idx, uint16_t res_end_idx,
257 struct rte_mbuf *pkt)
259 uint32_t vec_idx = 0;
260 uint32_t entry_success = 0;
261 struct vhost_virtqueue *vq;
262 /* The virtio_hdr is initialised to 0. */
263 struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {
264 {0, 0, 0, 0, 0, 0}, 0};
265 uint16_t cur_idx = res_base_idx;
266 uint64_t vb_addr = 0;
267 uint64_t vb_hdr_addr = 0;
268 uint32_t seg_offset = 0;
269 uint32_t vb_offset = 0;
272 uint32_t cpy_len, entry_len;
277 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") Current Index %d| "
279 dev->device_fh, cur_idx, res_end_idx);
282 * Convert from gpa to vva
283 * (guest physical addr -> vhost virtual addr)
285 vq = dev->virtqueue[queue_id];
287 vb_addr = gpa_to_vva(dev, vq->buf_vec[vec_idx].buf_addr);
288 vb_hdr_addr = vb_addr;
290 /* Prefetch buffer address. */
291 rte_prefetch0((void *)(uintptr_t)vb_addr);
293 virtio_hdr.num_buffers = res_end_idx - res_base_idx;
295 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") RX: Num merge buffers %d\n",
296 dev->device_fh, virtio_hdr.num_buffers);
298 rte_memcpy((void *)(uintptr_t)vb_hdr_addr,
299 (const void *)&virtio_hdr, vq->vhost_hlen);
301 PRINT_PACKET(dev, (uintptr_t)vb_hdr_addr, vq->vhost_hlen, 1);
303 seg_avail = rte_pktmbuf_data_len(pkt);
304 vb_offset = vq->vhost_hlen;
305 vb_avail = vq->buf_vec[vec_idx].buf_len - vq->vhost_hlen;
307 entry_len = vq->vhost_hlen;
311 vq->buf_vec[vec_idx].desc_idx;
313 if ((vq->desc[desc_idx].flags
314 & VRING_DESC_F_NEXT) == 0) {
315 /* Update used ring with desc information */
316 vq->used->ring[cur_idx & (vq->size - 1)].id
317 = vq->buf_vec[vec_idx].desc_idx;
318 vq->used->ring[cur_idx & (vq->size - 1)].len
327 vb_addr = gpa_to_vva(dev, vq->buf_vec[vec_idx].buf_addr);
329 /* Prefetch buffer address. */
330 rte_prefetch0((void *)(uintptr_t)vb_addr);
332 vb_avail = vq->buf_vec[vec_idx].buf_len;
335 cpy_len = RTE_MIN(vb_avail, seg_avail);
337 while (cpy_len > 0) {
338 /* Copy mbuf data to vring buffer */
339 rte_memcpy((void *)(uintptr_t)(vb_addr + vb_offset),
340 rte_pktmbuf_mtod_offset(pkt, const void *, seg_offset),
344 (uintptr_t)(vb_addr + vb_offset),
347 seg_offset += cpy_len;
348 vb_offset += cpy_len;
349 seg_avail -= cpy_len;
351 entry_len += cpy_len;
353 if (seg_avail != 0) {
355 * The virtio buffer in this vring
356 * entry reach to its end.
357 * But the segment doesn't complete.
359 if ((vq->desc[vq->buf_vec[vec_idx].desc_idx].flags &
360 VRING_DESC_F_NEXT) == 0) {
361 /* Update used ring with desc information */
362 vq->used->ring[cur_idx & (vq->size - 1)].id
363 = vq->buf_vec[vec_idx].desc_idx;
364 vq->used->ring[cur_idx & (vq->size - 1)].len
372 vb_addr = gpa_to_vva(dev,
373 vq->buf_vec[vec_idx].buf_addr);
375 vb_avail = vq->buf_vec[vec_idx].buf_len;
376 cpy_len = RTE_MIN(vb_avail, seg_avail);
379 * This current segment complete, need continue to
380 * check if the whole packet complete or not.
385 * There are more segments.
389 * This current buffer from vring is
390 * used up, need fetch next buffer
394 vq->buf_vec[vec_idx].desc_idx;
396 if ((vq->desc[desc_idx].flags &
397 VRING_DESC_F_NEXT) == 0) {
398 uint16_t wrapped_idx =
399 cur_idx & (vq->size - 1);
401 * Update used ring with the
402 * descriptor information
404 vq->used->ring[wrapped_idx].id
406 vq->used->ring[wrapped_idx].len
413 /* Get next buffer from buf_vec. */
415 vb_addr = gpa_to_vva(dev,
416 vq->buf_vec[vec_idx].buf_addr);
418 vq->buf_vec[vec_idx].buf_len;
423 seg_avail = rte_pktmbuf_data_len(pkt);
424 cpy_len = RTE_MIN(vb_avail, seg_avail);
427 * This whole packet completes.
429 /* Update used ring with desc information */
430 vq->used->ring[cur_idx & (vq->size - 1)].id
431 = vq->buf_vec[vec_idx].desc_idx;
432 vq->used->ring[cur_idx & (vq->size - 1)].len
440 return entry_success;
443 static inline void __attribute__((always_inline))
444 update_secure_len(struct vhost_virtqueue *vq, uint32_t id,
445 uint32_t *secure_len, uint32_t *vec_idx)
447 uint16_t wrapped_idx = id & (vq->size - 1);
448 uint32_t idx = vq->avail->ring[wrapped_idx];
450 uint32_t len = *secure_len;
451 uint32_t vec_id = *vec_idx;
455 len += vq->desc[idx].len;
456 vq->buf_vec[vec_id].buf_addr = vq->desc[idx].addr;
457 vq->buf_vec[vec_id].buf_len = vq->desc[idx].len;
458 vq->buf_vec[vec_id].desc_idx = idx;
461 if (vq->desc[idx].flags & VRING_DESC_F_NEXT) {
462 idx = vq->desc[idx].next;
472 * This function works for mergeable RX.
474 static inline uint32_t __attribute__((always_inline))
475 virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,
476 struct rte_mbuf **pkts, uint32_t count)
478 struct vhost_virtqueue *vq;
479 uint32_t pkt_idx = 0, entry_success = 0;
481 uint16_t res_base_idx, res_cur_idx;
484 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") virtio_dev_merge_rx()\n",
486 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->virt_qp_nb))) {
487 RTE_LOG(ERR, VHOST_DATA,
488 "%s (%"PRIu64"): virtqueue idx:%d invalid.\n",
489 __func__, dev->device_fh, queue_id);
493 vq = dev->virtqueue[queue_id];
494 if (unlikely(vq->enabled == 0))
497 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
502 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
503 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + vq->vhost_hlen;
507 * As many data cores may want access to available
508 * buffers, they need to be reserved.
510 uint32_t secure_len = 0;
511 uint32_t vec_idx = 0;
513 res_base_idx = vq->last_used_idx_res;
514 res_cur_idx = res_base_idx;
517 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
518 if (unlikely(res_cur_idx == avail_idx))
521 update_secure_len(vq, res_cur_idx,
522 &secure_len, &vec_idx);
524 } while (pkt_len > secure_len);
526 /* vq->last_used_idx_res is atomically updated. */
527 success = rte_atomic16_cmpset(&vq->last_used_idx_res,
530 } while (success == 0);
532 entry_success = copy_from_mbuf_to_vring(dev, queue_id,
533 res_base_idx, res_cur_idx, pkts[pkt_idx]);
535 rte_compiler_barrier();
538 * Wait until it's our turn to add our buffer
541 while (unlikely(vq->last_used_idx != res_base_idx))
544 *(volatile uint16_t *)&vq->used->idx += entry_success;
545 vq->last_used_idx = res_cur_idx;
549 if (likely(pkt_idx)) {
550 /* flush used->idx update before we read avail->flags. */
553 /* Kick the guest if necessary. */
554 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
555 eventfd_write(vq->callfd, (eventfd_t)1);
562 rte_vhost_enqueue_burst(struct virtio_net *dev, uint16_t queue_id,
563 struct rte_mbuf **pkts, uint16_t count)
565 if (unlikely(dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)))
566 return virtio_dev_merge_rx(dev, queue_id, pkts, count);
568 return virtio_dev_rx(dev, queue_id, pkts, count);
572 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
574 struct ipv4_hdr *ipv4_hdr;
575 struct ipv6_hdr *ipv6_hdr;
577 struct ether_hdr *eth_hdr;
580 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
582 m->l2_len = sizeof(struct ether_hdr);
583 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
585 if (ethertype == ETHER_TYPE_VLAN) {
586 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
588 m->l2_len += sizeof(struct vlan_hdr);
589 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
592 l3_hdr = (char *)eth_hdr + m->l2_len;
595 case ETHER_TYPE_IPv4:
596 ipv4_hdr = (struct ipv4_hdr *)l3_hdr;
597 *l4_proto = ipv4_hdr->next_proto_id;
598 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
599 *l4_hdr = (char *)l3_hdr + m->l3_len;
600 m->ol_flags |= PKT_TX_IPV4;
602 case ETHER_TYPE_IPv6:
603 ipv6_hdr = (struct ipv6_hdr *)l3_hdr;
604 *l4_proto = ipv6_hdr->proto;
605 m->l3_len = sizeof(struct ipv6_hdr);
606 *l4_hdr = (char *)l3_hdr + m->l3_len;
607 m->ol_flags |= PKT_TX_IPV6;
616 static inline void __attribute__((always_inline))
617 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
619 uint16_t l4_proto = 0;
621 struct tcp_hdr *tcp_hdr = NULL;
623 parse_ethernet(m, &l4_proto, &l4_hdr);
624 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
625 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
626 switch (hdr->csum_offset) {
627 case (offsetof(struct tcp_hdr, cksum)):
628 if (l4_proto == IPPROTO_TCP)
629 m->ol_flags |= PKT_TX_TCP_CKSUM;
631 case (offsetof(struct udp_hdr, dgram_cksum)):
632 if (l4_proto == IPPROTO_UDP)
633 m->ol_flags |= PKT_TX_UDP_CKSUM;
635 case (offsetof(struct sctp_hdr, cksum)):
636 if (l4_proto == IPPROTO_SCTP)
637 m->ol_flags |= PKT_TX_SCTP_CKSUM;
645 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
646 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
647 case VIRTIO_NET_HDR_GSO_TCPV4:
648 case VIRTIO_NET_HDR_GSO_TCPV6:
649 tcp_hdr = (struct tcp_hdr *)l4_hdr;
650 m->ol_flags |= PKT_TX_TCP_SEG;
651 m->tso_segsz = hdr->gso_size;
652 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
655 RTE_LOG(WARNING, VHOST_DATA,
656 "unsupported gso type %u.\n", hdr->gso_type);
663 rte_vhost_dequeue_burst(struct virtio_net *dev, uint16_t queue_id,
664 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
666 struct rte_mbuf *m, *prev;
667 struct vhost_virtqueue *vq;
668 struct vring_desc *desc;
669 uint64_t vb_addr = 0;
670 uint64_t vb_net_hdr_addr = 0;
671 uint32_t head[MAX_PKT_BURST];
674 uint16_t free_entries, entry_success = 0;
676 struct virtio_net_hdr *hdr = NULL;
678 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->virt_qp_nb))) {
679 RTE_LOG(ERR, VHOST_DATA,
680 "%s (%"PRIu64"): virtqueue idx:%d invalid.\n",
681 __func__, dev->device_fh, queue_id);
685 vq = dev->virtqueue[queue_id];
686 if (unlikely(vq->enabled == 0))
689 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
691 /* If there are no available buffers then return. */
692 if (vq->last_used_idx == avail_idx)
695 LOG_DEBUG(VHOST_DATA, "%s (%"PRIu64")\n", __func__,
698 /* Prefetch available ring to retrieve head indexes. */
699 rte_prefetch0(&vq->avail->ring[vq->last_used_idx & (vq->size - 1)]);
701 /*get the number of free entries in the ring*/
702 free_entries = (avail_idx - vq->last_used_idx);
704 free_entries = RTE_MIN(free_entries, count);
705 /* Limit to MAX_PKT_BURST. */
706 free_entries = RTE_MIN(free_entries, MAX_PKT_BURST);
708 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") Buffers available %d\n",
709 dev->device_fh, free_entries);
710 /* Retrieve all of the head indexes first to avoid caching issues. */
711 for (i = 0; i < free_entries; i++)
712 head[i] = vq->avail->ring[(vq->last_used_idx + i) & (vq->size - 1)];
714 /* Prefetch descriptor index. */
715 rte_prefetch0(&vq->desc[head[entry_success]]);
716 rte_prefetch0(&vq->used->ring[vq->last_used_idx & (vq->size - 1)]);
718 while (entry_success < free_entries) {
719 uint32_t vb_avail, vb_offset;
720 uint32_t seg_avail, seg_offset;
722 uint32_t seg_num = 0;
723 struct rte_mbuf *cur;
724 uint8_t alloc_err = 0;
726 desc = &vq->desc[head[entry_success]];
728 vb_net_hdr_addr = gpa_to_vva(dev, desc->addr);
729 hdr = (struct virtio_net_hdr *)((uintptr_t)vb_net_hdr_addr);
731 /* Discard first buffer as it is the virtio header */
732 if (desc->flags & VRING_DESC_F_NEXT) {
733 desc = &vq->desc[desc->next];
735 vb_avail = desc->len;
737 vb_offset = vq->vhost_hlen;
738 vb_avail = desc->len - vb_offset;
741 /* Buffer address translation. */
742 vb_addr = gpa_to_vva(dev, desc->addr);
743 /* Prefetch buffer address. */
744 rte_prefetch0((void *)(uintptr_t)vb_addr);
746 used_idx = vq->last_used_idx & (vq->size - 1);
748 if (entry_success < (free_entries - 1)) {
749 /* Prefetch descriptor index. */
750 rte_prefetch0(&vq->desc[head[entry_success+1]]);
751 rte_prefetch0(&vq->used->ring[(used_idx + 1) & (vq->size - 1)]);
754 /* Update used index buffer information. */
755 vq->used->ring[used_idx].id = head[entry_success];
756 vq->used->ring[used_idx].len = 0;
758 /* Allocate an mbuf and populate the structure. */
759 m = rte_pktmbuf_alloc(mbuf_pool);
760 if (unlikely(m == NULL)) {
761 RTE_LOG(ERR, VHOST_DATA,
762 "Failed to allocate memory for mbuf.\n");
766 seg_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
767 cpy_len = RTE_MIN(vb_avail, seg_avail);
769 PRINT_PACKET(dev, (uintptr_t)vb_addr, desc->len, 0);
774 while (cpy_len != 0) {
775 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *, seg_offset),
776 (void *)((uintptr_t)(vb_addr + vb_offset)),
779 seg_offset += cpy_len;
780 vb_offset += cpy_len;
782 seg_avail -= cpy_len;
786 * The segment reachs to its end,
787 * while the virtio buffer in TX vring has
788 * more data to be copied.
790 cur->data_len = seg_offset;
791 m->pkt_len += seg_offset;
792 /* Allocate mbuf and populate the structure. */
793 cur = rte_pktmbuf_alloc(mbuf_pool);
794 if (unlikely(cur == NULL)) {
795 RTE_LOG(ERR, VHOST_DATA, "Failed to "
796 "allocate memory for mbuf.\n");
806 seg_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
808 if (desc->flags & VRING_DESC_F_NEXT) {
810 * There are more virtio buffers in
811 * same vring entry need to be copied.
813 if (seg_avail == 0) {
815 * The current segment hasn't
816 * room to accomodate more
819 cur->data_len = seg_offset;
820 m->pkt_len += seg_offset;
822 * Allocate an mbuf and
823 * populate the structure.
825 cur = rte_pktmbuf_alloc(mbuf_pool);
826 if (unlikely(cur == NULL)) {
840 seg_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
843 desc = &vq->desc[desc->next];
845 /* Buffer address translation. */
846 vb_addr = gpa_to_vva(dev, desc->addr);
847 /* Prefetch buffer address. */
848 rte_prefetch0((void *)(uintptr_t)vb_addr);
850 vb_avail = desc->len;
852 PRINT_PACKET(dev, (uintptr_t)vb_addr,
855 /* The whole packet completes. */
856 cur->data_len = seg_offset;
857 m->pkt_len += seg_offset;
862 cpy_len = RTE_MIN(vb_avail, seg_avail);
865 if (unlikely(alloc_err == 1))
868 m->nb_segs = seg_num;
869 if ((hdr->flags != 0) || (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE))
870 vhost_dequeue_offload(hdr, m);
872 pkts[entry_success] = m;
877 rte_compiler_barrier();
878 vq->used->idx += entry_success;
879 /* Kick guest if required. */
880 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
881 eventfd_write(vq->callfd, (eventfd_t)1);
882 return entry_success;