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>
48 #include "vhost-net.h"
50 #define MAX_PKT_BURST 32
51 #define VHOST_LOG_PAGE 4096
53 static inline void __attribute__((always_inline))
54 vhost_log_page(uint8_t *log_base, uint64_t page)
56 log_base[page / 8] |= 1 << (page % 8);
59 static inline void __attribute__((always_inline))
60 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
64 if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
65 !dev->log_base || !len))
68 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
71 /* To make sure guest memory updates are committed before logging */
74 page = addr / VHOST_LOG_PAGE;
75 while (page * VHOST_LOG_PAGE < addr + len) {
76 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
81 static inline void __attribute__((always_inline))
82 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
83 uint64_t offset, uint64_t len)
85 vhost_log_write(dev, vq->log_guest_addr + offset, len);
89 is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t qp_nb)
91 return (is_tx ^ (idx & 1)) == 0 && idx < qp_nb * VIRTIO_QNUM;
95 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
97 if (m_buf->ol_flags & PKT_TX_L4_MASK) {
98 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
99 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
101 switch (m_buf->ol_flags & PKT_TX_L4_MASK) {
102 case PKT_TX_TCP_CKSUM:
103 net_hdr->csum_offset = (offsetof(struct tcp_hdr,
106 case PKT_TX_UDP_CKSUM:
107 net_hdr->csum_offset = (offsetof(struct udp_hdr,
110 case PKT_TX_SCTP_CKSUM:
111 net_hdr->csum_offset = (offsetof(struct sctp_hdr,
117 if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
118 if (m_buf->ol_flags & PKT_TX_IPV4)
119 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
121 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
122 net_hdr->gso_size = m_buf->tso_segsz;
123 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
129 copy_virtio_net_hdr(struct virtio_net *dev, uint64_t desc_addr,
130 struct virtio_net_hdr_mrg_rxbuf hdr)
132 if (dev->vhost_hlen == sizeof(struct virtio_net_hdr_mrg_rxbuf))
133 *(struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)desc_addr = hdr;
135 *(struct virtio_net_hdr *)(uintptr_t)desc_addr = hdr.hdr;
138 static inline int __attribute__((always_inline))
139 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
140 struct rte_mbuf *m, uint16_t desc_idx)
142 uint32_t desc_avail, desc_offset;
143 uint32_t mbuf_avail, mbuf_offset;
145 struct vring_desc *desc;
147 struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0, 0, 0, 0, 0, 0}, 0};
149 desc = &vq->desc[desc_idx];
150 if (unlikely(desc->len < dev->vhost_hlen))
153 desc_addr = gpa_to_vva(dev, desc->addr);
154 rte_prefetch0((void *)(uintptr_t)desc_addr);
156 virtio_enqueue_offload(m, &virtio_hdr.hdr);
157 copy_virtio_net_hdr(dev, desc_addr, virtio_hdr);
158 vhost_log_write(dev, desc->addr, dev->vhost_hlen);
159 PRINT_PACKET(dev, (uintptr_t)desc_addr, dev->vhost_hlen, 0);
161 desc_offset = dev->vhost_hlen;
162 desc_avail = desc->len - dev->vhost_hlen;
164 mbuf_avail = rte_pktmbuf_data_len(m);
166 while (mbuf_avail != 0 || m->next != NULL) {
167 /* done with current mbuf, fetch next */
168 if (mbuf_avail == 0) {
172 mbuf_avail = rte_pktmbuf_data_len(m);
175 /* done with current desc buf, fetch next */
176 if (desc_avail == 0) {
177 if ((desc->flags & VRING_DESC_F_NEXT) == 0) {
178 /* Room in vring buffer is not enough */
181 if (unlikely(desc->next >= vq->size))
184 desc = &vq->desc[desc->next];
185 desc_addr = gpa_to_vva(dev, desc->addr);
187 desc_avail = desc->len;
190 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
191 rte_memcpy((void *)((uintptr_t)(desc_addr + desc_offset)),
192 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
194 vhost_log_write(dev, desc->addr + desc_offset, cpy_len);
195 PRINT_PACKET(dev, (uintptr_t)(desc_addr + desc_offset),
198 mbuf_avail -= cpy_len;
199 mbuf_offset += cpy_len;
200 desc_avail -= cpy_len;
201 desc_offset += cpy_len;
208 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
209 * be received from the physical port or from another virtio device. A packet
210 * count is returned to indicate the number of packets that are succesfully
211 * added to the RX queue. This function works when the mbuf is scattered, but
212 * it doesn't support the mergeable feature.
214 static inline uint32_t __attribute__((always_inline))
215 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
216 struct rte_mbuf **pkts, uint32_t count)
218 struct vhost_virtqueue *vq;
219 uint16_t avail_idx, free_entries, start_idx;
220 uint16_t desc_indexes[MAX_PKT_BURST];
224 LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
225 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->virt_qp_nb))) {
226 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
227 dev->vid, __func__, queue_id);
231 vq = dev->virtqueue[queue_id];
232 if (unlikely(vq->enabled == 0))
235 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
236 start_idx = vq->last_used_idx;
237 free_entries = avail_idx - start_idx;
238 count = RTE_MIN(count, free_entries);
239 count = RTE_MIN(count, (uint32_t)MAX_PKT_BURST);
243 LOG_DEBUG(VHOST_DATA, "(%d) start_idx %d | end_idx %d\n",
244 dev->vid, start_idx, start_idx + count);
246 /* Retrieve all of the desc indexes first to avoid caching issues. */
247 rte_prefetch0(&vq->avail->ring[start_idx & (vq->size - 1)]);
248 for (i = 0; i < count; i++) {
249 used_idx = (start_idx + i) & (vq->size - 1);
250 desc_indexes[i] = vq->avail->ring[used_idx];
251 vq->used->ring[used_idx].id = desc_indexes[i];
252 vq->used->ring[used_idx].len = pkts[i]->pkt_len +
254 vhost_log_used_vring(dev, vq,
255 offsetof(struct vring_used, ring[used_idx]),
256 sizeof(vq->used->ring[used_idx]));
259 rte_prefetch0(&vq->desc[desc_indexes[0]]);
260 for (i = 0; i < count; i++) {
261 uint16_t desc_idx = desc_indexes[i];
264 err = copy_mbuf_to_desc(dev, vq, pkts[i], desc_idx);
266 used_idx = (start_idx + i) & (vq->size - 1);
267 vq->used->ring[used_idx].len = dev->vhost_hlen;
268 vhost_log_used_vring(dev, vq,
269 offsetof(struct vring_used, ring[used_idx]),
270 sizeof(vq->used->ring[used_idx]));
274 rte_prefetch0(&vq->desc[desc_indexes[i+1]]);
279 *(volatile uint16_t *)&vq->used->idx += count;
280 vq->last_used_idx += count;
281 vhost_log_used_vring(dev, vq,
282 offsetof(struct vring_used, idx),
283 sizeof(vq->used->idx));
285 /* flush used->idx update before we read avail->flags. */
288 /* Kick the guest if necessary. */
289 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
290 && (vq->callfd >= 0))
291 eventfd_write(vq->callfd, (eventfd_t)1);
296 fill_vec_buf(struct vhost_virtqueue *vq, uint32_t avail_idx,
297 uint32_t *allocated, uint32_t *vec_idx,
298 struct buf_vector *buf_vec)
300 uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
301 uint32_t vec_id = *vec_idx;
302 uint32_t len = *allocated;
305 if (unlikely(vec_id >= BUF_VECTOR_MAX || idx >= vq->size))
308 len += vq->desc[idx].len;
309 buf_vec[vec_id].buf_addr = vq->desc[idx].addr;
310 buf_vec[vec_id].buf_len = vq->desc[idx].len;
311 buf_vec[vec_id].desc_idx = idx;
314 if ((vq->desc[idx].flags & VRING_DESC_F_NEXT) == 0)
317 idx = vq->desc[idx].next;
327 * Returns -1 on fail, 0 on success
330 reserve_avail_buf_mergeable(struct vhost_virtqueue *vq, uint32_t size,
331 uint16_t *end, struct buf_vector *buf_vec)
335 uint32_t allocated = 0;
336 uint32_t vec_idx = 0;
339 cur_idx = vq->last_used_idx;
342 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
343 if (unlikely(cur_idx == avail_idx))
346 if (unlikely(fill_vec_buf(vq, cur_idx, &allocated,
347 &vec_idx, buf_vec) < 0))
353 if (allocated >= size)
357 * if we tried all available ring items, and still
358 * can't get enough buf, it means something abnormal
361 if (unlikely(tries >= vq->size))
369 static inline uint32_t __attribute__((always_inline))
370 copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,
371 uint16_t end_idx, struct rte_mbuf *m,
372 struct buf_vector *buf_vec)
374 struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0, 0, 0, 0, 0, 0}, 0};
375 uint32_t vec_idx = 0;
376 uint16_t start_idx = vq->last_used_idx;
377 uint16_t cur_idx = start_idx;
379 uint32_t mbuf_offset, mbuf_avail;
380 uint32_t desc_offset, desc_avail;
382 uint16_t desc_idx, used_idx;
384 if (unlikely(m == NULL))
387 LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
388 dev->vid, cur_idx, end_idx);
390 if (buf_vec[vec_idx].buf_len < dev->vhost_hlen)
393 desc_addr = gpa_to_vva(dev, buf_vec[vec_idx].buf_addr);
394 rte_prefetch0((void *)(uintptr_t)desc_addr);
396 virtio_hdr.num_buffers = end_idx - start_idx;
397 LOG_DEBUG(VHOST_DATA, "(%d) RX: num merge buffers %d\n",
398 dev->vid, virtio_hdr.num_buffers);
400 virtio_enqueue_offload(m, &virtio_hdr.hdr);
401 copy_virtio_net_hdr(dev, desc_addr, virtio_hdr);
402 vhost_log_write(dev, buf_vec[vec_idx].buf_addr, dev->vhost_hlen);
403 PRINT_PACKET(dev, (uintptr_t)desc_addr, dev->vhost_hlen, 0);
405 desc_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
406 desc_offset = dev->vhost_hlen;
408 mbuf_avail = rte_pktmbuf_data_len(m);
410 while (mbuf_avail != 0 || m->next != NULL) {
411 /* done with current desc buf, get the next one */
412 if (desc_avail == 0) {
413 desc_idx = buf_vec[vec_idx].desc_idx;
415 if (!(vq->desc[desc_idx].flags & VRING_DESC_F_NEXT)) {
416 /* Update used ring with desc information */
417 used_idx = cur_idx++ & (vq->size - 1);
418 vq->used->ring[used_idx].id = desc_idx;
419 vq->used->ring[used_idx].len = desc_offset;
420 vhost_log_used_vring(dev, vq,
421 offsetof(struct vring_used,
423 sizeof(vq->used->ring[used_idx]));
427 desc_addr = gpa_to_vva(dev, buf_vec[vec_idx].buf_addr);
429 /* Prefetch buffer address. */
430 rte_prefetch0((void *)(uintptr_t)desc_addr);
432 desc_avail = buf_vec[vec_idx].buf_len;
435 /* done with current mbuf, get the next one */
436 if (mbuf_avail == 0) {
440 mbuf_avail = rte_pktmbuf_data_len(m);
443 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
444 rte_memcpy((void *)((uintptr_t)(desc_addr + desc_offset)),
445 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
447 vhost_log_write(dev, buf_vec[vec_idx].buf_addr + desc_offset,
449 PRINT_PACKET(dev, (uintptr_t)(desc_addr + desc_offset),
452 mbuf_avail -= cpy_len;
453 mbuf_offset += cpy_len;
454 desc_avail -= cpy_len;
455 desc_offset += cpy_len;
458 used_idx = cur_idx & (vq->size - 1);
459 vq->used->ring[used_idx].id = buf_vec[vec_idx].desc_idx;
460 vq->used->ring[used_idx].len = desc_offset;
461 vhost_log_used_vring(dev, vq,
462 offsetof(struct vring_used, ring[used_idx]),
463 sizeof(vq->used->ring[used_idx]));
465 return end_idx - start_idx;
468 static inline uint32_t __attribute__((always_inline))
469 virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,
470 struct rte_mbuf **pkts, uint32_t count)
472 struct vhost_virtqueue *vq;
473 uint32_t pkt_idx = 0, nr_used = 0;
475 struct buf_vector buf_vec[BUF_VECTOR_MAX];
477 LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
478 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->virt_qp_nb))) {
479 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
480 dev->vid, __func__, queue_id);
484 vq = dev->virtqueue[queue_id];
485 if (unlikely(vq->enabled == 0))
488 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
492 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
493 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
495 if (unlikely(reserve_avail_buf_mergeable(vq, pkt_len,
496 &end, buf_vec) < 0)) {
497 LOG_DEBUG(VHOST_DATA,
498 "(%d) failed to get enough desc from vring\n",
503 nr_used = copy_mbuf_to_desc_mergeable(dev, vq, end,
504 pkts[pkt_idx], buf_vec);
507 *(volatile uint16_t *)&vq->used->idx += nr_used;
508 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
509 sizeof(vq->used->idx));
510 vq->last_used_idx = end;
513 if (likely(pkt_idx)) {
514 /* flush used->idx update before we read avail->flags. */
517 /* Kick the guest if necessary. */
518 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
519 && (vq->callfd >= 0))
520 eventfd_write(vq->callfd, (eventfd_t)1);
527 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
528 struct rte_mbuf **pkts, uint16_t count)
530 struct virtio_net *dev = get_device(vid);
535 if (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF))
536 return virtio_dev_merge_rx(dev, queue_id, pkts, count);
538 return virtio_dev_rx(dev, queue_id, pkts, count);
542 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
544 struct ipv4_hdr *ipv4_hdr;
545 struct ipv6_hdr *ipv6_hdr;
547 struct ether_hdr *eth_hdr;
550 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
552 m->l2_len = sizeof(struct ether_hdr);
553 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
555 if (ethertype == ETHER_TYPE_VLAN) {
556 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
558 m->l2_len += sizeof(struct vlan_hdr);
559 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
562 l3_hdr = (char *)eth_hdr + m->l2_len;
565 case ETHER_TYPE_IPv4:
566 ipv4_hdr = (struct ipv4_hdr *)l3_hdr;
567 *l4_proto = ipv4_hdr->next_proto_id;
568 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
569 *l4_hdr = (char *)l3_hdr + m->l3_len;
570 m->ol_flags |= PKT_TX_IPV4;
572 case ETHER_TYPE_IPv6:
573 ipv6_hdr = (struct ipv6_hdr *)l3_hdr;
574 *l4_proto = ipv6_hdr->proto;
575 m->l3_len = sizeof(struct ipv6_hdr);
576 *l4_hdr = (char *)l3_hdr + m->l3_len;
577 m->ol_flags |= PKT_TX_IPV6;
586 static inline void __attribute__((always_inline))
587 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
589 uint16_t l4_proto = 0;
591 struct tcp_hdr *tcp_hdr = NULL;
593 parse_ethernet(m, &l4_proto, &l4_hdr);
594 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
595 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
596 switch (hdr->csum_offset) {
597 case (offsetof(struct tcp_hdr, cksum)):
598 if (l4_proto == IPPROTO_TCP)
599 m->ol_flags |= PKT_TX_TCP_CKSUM;
601 case (offsetof(struct udp_hdr, dgram_cksum)):
602 if (l4_proto == IPPROTO_UDP)
603 m->ol_flags |= PKT_TX_UDP_CKSUM;
605 case (offsetof(struct sctp_hdr, cksum)):
606 if (l4_proto == IPPROTO_SCTP)
607 m->ol_flags |= PKT_TX_SCTP_CKSUM;
615 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
616 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
617 case VIRTIO_NET_HDR_GSO_TCPV4:
618 case VIRTIO_NET_HDR_GSO_TCPV6:
619 tcp_hdr = (struct tcp_hdr *)l4_hdr;
620 m->ol_flags |= PKT_TX_TCP_SEG;
621 m->tso_segsz = hdr->gso_size;
622 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
625 RTE_LOG(WARNING, VHOST_DATA,
626 "unsupported gso type %u.\n", hdr->gso_type);
632 #define RARP_PKT_SIZE 64
635 make_rarp_packet(struct rte_mbuf *rarp_mbuf, const struct ether_addr *mac)
637 struct ether_hdr *eth_hdr;
638 struct arp_hdr *rarp;
640 if (rarp_mbuf->buf_len < 64) {
641 RTE_LOG(WARNING, VHOST_DATA,
642 "failed to make RARP; mbuf size too small %u (< %d)\n",
643 rarp_mbuf->buf_len, RARP_PKT_SIZE);
647 /* Ethernet header. */
648 eth_hdr = rte_pktmbuf_mtod_offset(rarp_mbuf, struct ether_hdr *, 0);
649 memset(eth_hdr->d_addr.addr_bytes, 0xff, ETHER_ADDR_LEN);
650 ether_addr_copy(mac, ð_hdr->s_addr);
651 eth_hdr->ether_type = htons(ETHER_TYPE_RARP);
654 rarp = (struct arp_hdr *)(eth_hdr + 1);
655 rarp->arp_hrd = htons(ARP_HRD_ETHER);
656 rarp->arp_pro = htons(ETHER_TYPE_IPv4);
657 rarp->arp_hln = ETHER_ADDR_LEN;
659 rarp->arp_op = htons(ARP_OP_REVREQUEST);
661 ether_addr_copy(mac, &rarp->arp_data.arp_sha);
662 ether_addr_copy(mac, &rarp->arp_data.arp_tha);
663 memset(&rarp->arp_data.arp_sip, 0x00, 4);
664 memset(&rarp->arp_data.arp_tip, 0x00, 4);
666 rarp_mbuf->pkt_len = rarp_mbuf->data_len = RARP_PKT_SIZE;
671 static inline int __attribute__((always_inline))
672 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
673 struct rte_mbuf *m, uint16_t desc_idx,
674 struct rte_mempool *mbuf_pool)
676 struct vring_desc *desc;
678 uint32_t desc_avail, desc_offset;
679 uint32_t mbuf_avail, mbuf_offset;
681 struct rte_mbuf *cur = m, *prev = m;
682 struct virtio_net_hdr *hdr;
683 /* A counter to avoid desc dead loop chain */
684 uint32_t nr_desc = 1;
686 desc = &vq->desc[desc_idx];
687 if (unlikely(desc->len < dev->vhost_hlen))
690 desc_addr = gpa_to_vva(dev, desc->addr);
691 hdr = (struct virtio_net_hdr *)((uintptr_t)desc_addr);
695 * A virtio driver normally uses at least 2 desc buffers
696 * for Tx: the first for storing the header, and others
697 * for storing the data.
699 if (likely((desc->len == dev->vhost_hlen) &&
700 (desc->flags & VRING_DESC_F_NEXT) != 0)) {
701 desc = &vq->desc[desc->next];
703 desc_addr = gpa_to_vva(dev, desc->addr);
704 rte_prefetch0((void *)(uintptr_t)desc_addr);
707 desc_avail = desc->len;
710 PRINT_PACKET(dev, (uintptr_t)desc_addr, desc->len, 0);
712 desc_avail = desc->len - dev->vhost_hlen;
713 desc_offset = dev->vhost_hlen;
717 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
719 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
720 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *, mbuf_offset),
721 (void *)((uintptr_t)(desc_addr + desc_offset)),
724 mbuf_avail -= cpy_len;
725 mbuf_offset += cpy_len;
726 desc_avail -= cpy_len;
727 desc_offset += cpy_len;
729 /* This desc reaches to its end, get the next one */
730 if (desc_avail == 0) {
731 if ((desc->flags & VRING_DESC_F_NEXT) == 0)
734 if (unlikely(desc->next >= vq->size ||
735 ++nr_desc >= vq->size))
737 desc = &vq->desc[desc->next];
739 desc_addr = gpa_to_vva(dev, desc->addr);
740 rte_prefetch0((void *)(uintptr_t)desc_addr);
743 desc_avail = desc->len;
745 PRINT_PACKET(dev, (uintptr_t)desc_addr, desc->len, 0);
749 * This mbuf reaches to its end, get a new one
752 if (mbuf_avail == 0) {
753 cur = rte_pktmbuf_alloc(mbuf_pool);
754 if (unlikely(cur == NULL)) {
755 RTE_LOG(ERR, VHOST_DATA, "Failed to "
756 "allocate memory for mbuf.\n");
761 prev->data_len = mbuf_offset;
763 m->pkt_len += mbuf_offset;
767 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
771 prev->data_len = mbuf_offset;
772 m->pkt_len += mbuf_offset;
774 if (hdr->flags != 0 || hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE)
775 vhost_dequeue_offload(hdr, m);
781 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
782 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
784 struct virtio_net *dev;
785 struct rte_mbuf *rarp_mbuf = NULL;
786 struct vhost_virtqueue *vq;
787 uint32_t desc_indexes[MAX_PKT_BURST];
790 uint16_t free_entries;
793 dev = get_device(vid);
797 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->virt_qp_nb))) {
798 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
799 dev->vid, __func__, queue_id);
803 vq = dev->virtqueue[queue_id];
804 if (unlikely(vq->enabled == 0))
808 * Construct a RARP broadcast packet, and inject it to the "pkts"
809 * array, to looks like that guest actually send such packet.
811 * Check user_send_rarp() for more information.
813 if (unlikely(rte_atomic16_cmpset((volatile uint16_t *)
814 &dev->broadcast_rarp.cnt, 1, 0))) {
815 rarp_mbuf = rte_pktmbuf_alloc(mbuf_pool);
816 if (rarp_mbuf == NULL) {
817 RTE_LOG(ERR, VHOST_DATA,
818 "Failed to allocate memory for mbuf.\n");
822 if (make_rarp_packet(rarp_mbuf, &dev->mac)) {
823 rte_pktmbuf_free(rarp_mbuf);
830 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
831 free_entries = avail_idx - vq->last_used_idx;
832 if (free_entries == 0)
835 LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
837 /* Prefetch available ring to retrieve head indexes. */
838 used_idx = vq->last_used_idx & (vq->size - 1);
839 rte_prefetch0(&vq->avail->ring[used_idx]);
840 rte_prefetch0(&vq->used->ring[used_idx]);
842 count = RTE_MIN(count, MAX_PKT_BURST);
843 count = RTE_MIN(count, free_entries);
844 LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
847 /* Retrieve all of the head indexes first to avoid caching issues. */
848 for (i = 0; i < count; i++) {
849 used_idx = (vq->last_used_idx + i) & (vq->size - 1);
850 desc_indexes[i] = vq->avail->ring[used_idx];
852 vq->used->ring[used_idx].id = desc_indexes[i];
853 vq->used->ring[used_idx].len = 0;
854 vhost_log_used_vring(dev, vq,
855 offsetof(struct vring_used, ring[used_idx]),
856 sizeof(vq->used->ring[used_idx]));
859 /* Prefetch descriptor index. */
860 rte_prefetch0(&vq->desc[desc_indexes[0]]);
861 for (i = 0; i < count; i++) {
864 if (likely(i + 1 < count))
865 rte_prefetch0(&vq->desc[desc_indexes[i + 1]]);
867 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
868 if (unlikely(pkts[i] == NULL)) {
869 RTE_LOG(ERR, VHOST_DATA,
870 "Failed to allocate memory for mbuf.\n");
873 err = copy_desc_to_mbuf(dev, vq, pkts[i], desc_indexes[i],
876 rte_pktmbuf_free(pkts[i]);
884 vq->last_used_idx += i;
885 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
886 sizeof(vq->used->idx));
888 /* Kick guest if required. */
889 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
890 && (vq->callfd >= 0))
891 eventfd_write(vq->callfd, (eventfd_t)1);
894 if (unlikely(rarp_mbuf != NULL)) {
896 * Inject it to the head of "pkts" array, so that switch's mac
897 * learning table will get updated first.
899 memmove(&pkts[1], pkts, i * sizeof(struct rte_mbuf *));