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 desc_addr = gpa_to_vva(dev, desc->addr);
152 * Checking of 'desc_addr' placed outside of 'unlikely' macro to avoid
153 * performance issue with some versions of gcc (4.8.4 and 5.3.0) which
154 * otherwise stores offset on the stack instead of in a register.
156 if (unlikely(desc->len < dev->vhost_hlen) || !desc_addr)
159 rte_prefetch0((void *)(uintptr_t)desc_addr);
161 virtio_enqueue_offload(m, &virtio_hdr.hdr);
162 copy_virtio_net_hdr(dev, desc_addr, virtio_hdr);
163 vhost_log_write(dev, desc->addr, dev->vhost_hlen);
164 PRINT_PACKET(dev, (uintptr_t)desc_addr, dev->vhost_hlen, 0);
166 desc_offset = dev->vhost_hlen;
167 desc_avail = desc->len - dev->vhost_hlen;
169 mbuf_avail = rte_pktmbuf_data_len(m);
171 while (mbuf_avail != 0 || m->next != NULL) {
172 /* done with current mbuf, fetch next */
173 if (mbuf_avail == 0) {
177 mbuf_avail = rte_pktmbuf_data_len(m);
180 /* done with current desc buf, fetch next */
181 if (desc_avail == 0) {
182 if ((desc->flags & VRING_DESC_F_NEXT) == 0) {
183 /* Room in vring buffer is not enough */
186 if (unlikely(desc->next >= vq->size))
189 desc = &vq->desc[desc->next];
190 desc_addr = gpa_to_vva(dev, desc->addr);
191 if (unlikely(!desc_addr))
195 desc_avail = desc->len;
198 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
199 rte_memcpy((void *)((uintptr_t)(desc_addr + desc_offset)),
200 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
202 vhost_log_write(dev, desc->addr + desc_offset, cpy_len);
203 PRINT_PACKET(dev, (uintptr_t)(desc_addr + desc_offset),
206 mbuf_avail -= cpy_len;
207 mbuf_offset += cpy_len;
208 desc_avail -= cpy_len;
209 desc_offset += cpy_len;
216 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
217 * be received from the physical port or from another virtio device. A packet
218 * count is returned to indicate the number of packets that are succesfully
219 * added to the RX queue. This function works when the mbuf is scattered, but
220 * it doesn't support the mergeable feature.
222 static inline uint32_t __attribute__((always_inline))
223 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
224 struct rte_mbuf **pkts, uint32_t count)
226 struct vhost_virtqueue *vq;
227 uint16_t avail_idx, free_entries, start_idx;
228 uint16_t desc_indexes[MAX_PKT_BURST];
232 LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
233 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->virt_qp_nb))) {
234 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
235 dev->vid, __func__, queue_id);
239 vq = dev->virtqueue[queue_id];
240 if (unlikely(vq->enabled == 0))
243 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
244 start_idx = vq->last_used_idx;
245 free_entries = avail_idx - start_idx;
246 count = RTE_MIN(count, free_entries);
247 count = RTE_MIN(count, (uint32_t)MAX_PKT_BURST);
251 LOG_DEBUG(VHOST_DATA, "(%d) start_idx %d | end_idx %d\n",
252 dev->vid, start_idx, start_idx + count);
254 /* Retrieve all of the desc indexes first to avoid caching issues. */
255 rte_prefetch0(&vq->avail->ring[start_idx & (vq->size - 1)]);
256 for (i = 0; i < count; i++) {
257 used_idx = (start_idx + i) & (vq->size - 1);
258 desc_indexes[i] = vq->avail->ring[used_idx];
259 vq->used->ring[used_idx].id = desc_indexes[i];
260 vq->used->ring[used_idx].len = pkts[i]->pkt_len +
262 vhost_log_used_vring(dev, vq,
263 offsetof(struct vring_used, ring[used_idx]),
264 sizeof(vq->used->ring[used_idx]));
267 rte_prefetch0(&vq->desc[desc_indexes[0]]);
268 for (i = 0; i < count; i++) {
269 uint16_t desc_idx = desc_indexes[i];
272 err = copy_mbuf_to_desc(dev, vq, pkts[i], desc_idx);
274 used_idx = (start_idx + i) & (vq->size - 1);
275 vq->used->ring[used_idx].len = dev->vhost_hlen;
276 vhost_log_used_vring(dev, vq,
277 offsetof(struct vring_used, ring[used_idx]),
278 sizeof(vq->used->ring[used_idx]));
282 rte_prefetch0(&vq->desc[desc_indexes[i+1]]);
287 *(volatile uint16_t *)&vq->used->idx += count;
288 vq->last_used_idx += count;
289 vhost_log_used_vring(dev, vq,
290 offsetof(struct vring_used, idx),
291 sizeof(vq->used->idx));
293 /* flush used->idx update before we read avail->flags. */
296 /* Kick the guest if necessary. */
297 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
298 && (vq->callfd >= 0))
299 eventfd_write(vq->callfd, (eventfd_t)1);
304 fill_vec_buf(struct vhost_virtqueue *vq, uint32_t avail_idx,
305 uint32_t *allocated, uint32_t *vec_idx,
306 struct buf_vector *buf_vec)
308 uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
309 uint32_t vec_id = *vec_idx;
310 uint32_t len = *allocated;
313 if (unlikely(vec_id >= BUF_VECTOR_MAX || idx >= vq->size))
316 len += vq->desc[idx].len;
317 buf_vec[vec_id].buf_addr = vq->desc[idx].addr;
318 buf_vec[vec_id].buf_len = vq->desc[idx].len;
319 buf_vec[vec_id].desc_idx = idx;
322 if ((vq->desc[idx].flags & VRING_DESC_F_NEXT) == 0)
325 idx = vq->desc[idx].next;
335 * Returns -1 on fail, 0 on success
338 reserve_avail_buf_mergeable(struct vhost_virtqueue *vq, uint32_t size,
339 uint16_t *end, struct buf_vector *buf_vec)
343 uint32_t allocated = 0;
344 uint32_t vec_idx = 0;
347 cur_idx = vq->last_used_idx;
350 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
351 if (unlikely(cur_idx == avail_idx))
354 if (unlikely(fill_vec_buf(vq, cur_idx, &allocated,
355 &vec_idx, buf_vec) < 0))
361 if (allocated >= size)
365 * if we tried all available ring items, and still
366 * can't get enough buf, it means something abnormal
369 if (unlikely(tries >= vq->size))
377 static inline uint32_t __attribute__((always_inline))
378 copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,
379 uint16_t end_idx, struct rte_mbuf *m,
380 struct buf_vector *buf_vec)
382 struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0, 0, 0, 0, 0, 0}, 0};
383 uint32_t vec_idx = 0;
384 uint16_t start_idx = vq->last_used_idx;
385 uint16_t cur_idx = start_idx;
387 uint32_t desc_chain_head;
388 uint32_t desc_chain_len;
389 uint32_t mbuf_offset, mbuf_avail;
390 uint32_t desc_offset, desc_avail;
392 uint16_t desc_idx, used_idx;
394 if (unlikely(m == NULL))
397 LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
398 dev->vid, cur_idx, end_idx);
400 desc_addr = gpa_to_vva(dev, buf_vec[vec_idx].buf_addr);
401 if (buf_vec[vec_idx].buf_len < dev->vhost_hlen || !desc_addr)
404 rte_prefetch0((void *)(uintptr_t)desc_addr);
406 virtio_hdr.num_buffers = end_idx - start_idx;
407 LOG_DEBUG(VHOST_DATA, "(%d) RX: num merge buffers %d\n",
408 dev->vid, virtio_hdr.num_buffers);
410 virtio_enqueue_offload(m, &virtio_hdr.hdr);
411 copy_virtio_net_hdr(dev, desc_addr, virtio_hdr);
412 vhost_log_write(dev, buf_vec[vec_idx].buf_addr, dev->vhost_hlen);
413 PRINT_PACKET(dev, (uintptr_t)desc_addr, dev->vhost_hlen, 0);
415 desc_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
416 desc_offset = dev->vhost_hlen;
417 desc_chain_head = buf_vec[vec_idx].desc_idx;
418 desc_chain_len = desc_offset;
420 mbuf_avail = rte_pktmbuf_data_len(m);
422 while (mbuf_avail != 0 || m->next != NULL) {
423 /* done with current desc buf, get the next one */
424 if (desc_avail == 0) {
425 desc_idx = buf_vec[vec_idx].desc_idx;
428 if (!(vq->desc[desc_idx].flags & VRING_DESC_F_NEXT)) {
429 /* Update used ring with desc information */
430 used_idx = cur_idx++ & (vq->size - 1);
431 vq->used->ring[used_idx].id = desc_chain_head;
432 vq->used->ring[used_idx].len = desc_chain_len;
433 vhost_log_used_vring(dev, vq,
434 offsetof(struct vring_used,
436 sizeof(vq->used->ring[used_idx]));
437 desc_chain_head = buf_vec[vec_idx].desc_idx;
441 desc_addr = gpa_to_vva(dev, buf_vec[vec_idx].buf_addr);
442 if (unlikely(!desc_addr))
445 /* Prefetch buffer address. */
446 rte_prefetch0((void *)(uintptr_t)desc_addr);
448 desc_avail = buf_vec[vec_idx].buf_len;
451 /* done with current mbuf, get the next one */
452 if (mbuf_avail == 0) {
456 mbuf_avail = rte_pktmbuf_data_len(m);
459 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
460 rte_memcpy((void *)((uintptr_t)(desc_addr + desc_offset)),
461 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
463 vhost_log_write(dev, buf_vec[vec_idx].buf_addr + desc_offset,
465 PRINT_PACKET(dev, (uintptr_t)(desc_addr + desc_offset),
468 mbuf_avail -= cpy_len;
469 mbuf_offset += cpy_len;
470 desc_avail -= cpy_len;
471 desc_offset += cpy_len;
472 desc_chain_len += cpy_len;
475 used_idx = cur_idx & (vq->size - 1);
476 vq->used->ring[used_idx].id = desc_chain_head;
477 vq->used->ring[used_idx].len = desc_chain_len;
478 vhost_log_used_vring(dev, vq,
479 offsetof(struct vring_used, ring[used_idx]),
480 sizeof(vq->used->ring[used_idx]));
482 return end_idx - start_idx;
485 static inline uint32_t __attribute__((always_inline))
486 virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,
487 struct rte_mbuf **pkts, uint32_t count)
489 struct vhost_virtqueue *vq;
490 uint32_t pkt_idx = 0, nr_used = 0;
492 struct buf_vector buf_vec[BUF_VECTOR_MAX];
494 LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
495 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->virt_qp_nb))) {
496 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
497 dev->vid, __func__, queue_id);
501 vq = dev->virtqueue[queue_id];
502 if (unlikely(vq->enabled == 0))
505 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
509 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
510 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
512 if (unlikely(reserve_avail_buf_mergeable(vq, pkt_len,
513 &end, buf_vec) < 0)) {
514 LOG_DEBUG(VHOST_DATA,
515 "(%d) failed to get enough desc from vring\n",
520 nr_used = copy_mbuf_to_desc_mergeable(dev, vq, end,
521 pkts[pkt_idx], buf_vec);
524 *(volatile uint16_t *)&vq->used->idx += nr_used;
525 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
526 sizeof(vq->used->idx));
527 vq->last_used_idx += nr_used;
530 if (likely(pkt_idx)) {
531 /* flush used->idx update before we read avail->flags. */
534 /* Kick the guest if necessary. */
535 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
536 && (vq->callfd >= 0))
537 eventfd_write(vq->callfd, (eventfd_t)1);
544 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
545 struct rte_mbuf **pkts, uint16_t count)
547 struct virtio_net *dev = get_device(vid);
552 if (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF))
553 return virtio_dev_merge_rx(dev, queue_id, pkts, count);
555 return virtio_dev_rx(dev, queue_id, pkts, count);
559 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
561 struct ipv4_hdr *ipv4_hdr;
562 struct ipv6_hdr *ipv6_hdr;
564 struct ether_hdr *eth_hdr;
567 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
569 m->l2_len = sizeof(struct ether_hdr);
570 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
572 if (ethertype == ETHER_TYPE_VLAN) {
573 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
575 m->l2_len += sizeof(struct vlan_hdr);
576 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
579 l3_hdr = (char *)eth_hdr + m->l2_len;
582 case ETHER_TYPE_IPv4:
583 ipv4_hdr = (struct ipv4_hdr *)l3_hdr;
584 *l4_proto = ipv4_hdr->next_proto_id;
585 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
586 *l4_hdr = (char *)l3_hdr + m->l3_len;
587 m->ol_flags |= PKT_TX_IPV4;
589 case ETHER_TYPE_IPv6:
590 ipv6_hdr = (struct ipv6_hdr *)l3_hdr;
591 *l4_proto = ipv6_hdr->proto;
592 m->l3_len = sizeof(struct ipv6_hdr);
593 *l4_hdr = (char *)l3_hdr + m->l3_len;
594 m->ol_flags |= PKT_TX_IPV6;
603 static inline void __attribute__((always_inline))
604 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
606 uint16_t l4_proto = 0;
608 struct tcp_hdr *tcp_hdr = NULL;
610 parse_ethernet(m, &l4_proto, &l4_hdr);
611 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
612 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
613 switch (hdr->csum_offset) {
614 case (offsetof(struct tcp_hdr, cksum)):
615 if (l4_proto == IPPROTO_TCP)
616 m->ol_flags |= PKT_TX_TCP_CKSUM;
618 case (offsetof(struct udp_hdr, dgram_cksum)):
619 if (l4_proto == IPPROTO_UDP)
620 m->ol_flags |= PKT_TX_UDP_CKSUM;
622 case (offsetof(struct sctp_hdr, cksum)):
623 if (l4_proto == IPPROTO_SCTP)
624 m->ol_flags |= PKT_TX_SCTP_CKSUM;
632 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
633 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
634 case VIRTIO_NET_HDR_GSO_TCPV4:
635 case VIRTIO_NET_HDR_GSO_TCPV6:
636 tcp_hdr = (struct tcp_hdr *)l4_hdr;
637 m->ol_flags |= PKT_TX_TCP_SEG;
638 m->tso_segsz = hdr->gso_size;
639 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
642 RTE_LOG(WARNING, VHOST_DATA,
643 "unsupported gso type %u.\n", hdr->gso_type);
649 #define RARP_PKT_SIZE 64
652 make_rarp_packet(struct rte_mbuf *rarp_mbuf, const struct ether_addr *mac)
654 struct ether_hdr *eth_hdr;
655 struct arp_hdr *rarp;
657 if (rarp_mbuf->buf_len < 64) {
658 RTE_LOG(WARNING, VHOST_DATA,
659 "failed to make RARP; mbuf size too small %u (< %d)\n",
660 rarp_mbuf->buf_len, RARP_PKT_SIZE);
664 /* Ethernet header. */
665 eth_hdr = rte_pktmbuf_mtod_offset(rarp_mbuf, struct ether_hdr *, 0);
666 memset(eth_hdr->d_addr.addr_bytes, 0xff, ETHER_ADDR_LEN);
667 ether_addr_copy(mac, ð_hdr->s_addr);
668 eth_hdr->ether_type = htons(ETHER_TYPE_RARP);
671 rarp = (struct arp_hdr *)(eth_hdr + 1);
672 rarp->arp_hrd = htons(ARP_HRD_ETHER);
673 rarp->arp_pro = htons(ETHER_TYPE_IPv4);
674 rarp->arp_hln = ETHER_ADDR_LEN;
676 rarp->arp_op = htons(ARP_OP_REVREQUEST);
678 ether_addr_copy(mac, &rarp->arp_data.arp_sha);
679 ether_addr_copy(mac, &rarp->arp_data.arp_tha);
680 memset(&rarp->arp_data.arp_sip, 0x00, 4);
681 memset(&rarp->arp_data.arp_tip, 0x00, 4);
683 rarp_mbuf->pkt_len = rarp_mbuf->data_len = RARP_PKT_SIZE;
688 static inline int __attribute__((always_inline))
689 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
690 struct rte_mbuf *m, uint16_t desc_idx,
691 struct rte_mempool *mbuf_pool)
693 struct vring_desc *desc;
695 uint32_t desc_avail, desc_offset;
696 uint32_t mbuf_avail, mbuf_offset;
698 struct rte_mbuf *cur = m, *prev = m;
699 struct virtio_net_hdr *hdr;
700 /* A counter to avoid desc dead loop chain */
701 uint32_t nr_desc = 1;
703 desc = &vq->desc[desc_idx];
704 if (unlikely(desc->len < dev->vhost_hlen))
707 desc_addr = gpa_to_vva(dev, desc->addr);
708 if (unlikely(!desc_addr))
711 hdr = (struct virtio_net_hdr *)((uintptr_t)desc_addr);
715 * A virtio driver normally uses at least 2 desc buffers
716 * for Tx: the first for storing the header, and others
717 * for storing the data.
719 if (likely((desc->len == dev->vhost_hlen) &&
720 (desc->flags & VRING_DESC_F_NEXT) != 0)) {
721 desc = &vq->desc[desc->next];
723 desc_addr = gpa_to_vva(dev, desc->addr);
724 if (unlikely(!desc_addr))
727 rte_prefetch0((void *)(uintptr_t)desc_addr);
730 desc_avail = desc->len;
733 PRINT_PACKET(dev, (uintptr_t)desc_addr, desc->len, 0);
735 desc_avail = desc->len - dev->vhost_hlen;
736 desc_offset = dev->vhost_hlen;
740 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
742 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
743 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *, mbuf_offset),
744 (void *)((uintptr_t)(desc_addr + desc_offset)),
747 mbuf_avail -= cpy_len;
748 mbuf_offset += cpy_len;
749 desc_avail -= cpy_len;
750 desc_offset += cpy_len;
752 /* This desc reaches to its end, get the next one */
753 if (desc_avail == 0) {
754 if ((desc->flags & VRING_DESC_F_NEXT) == 0)
757 if (unlikely(desc->next >= vq->size ||
758 ++nr_desc > vq->size))
760 desc = &vq->desc[desc->next];
762 desc_addr = gpa_to_vva(dev, desc->addr);
763 if (unlikely(!desc_addr))
766 rte_prefetch0((void *)(uintptr_t)desc_addr);
769 desc_avail = desc->len;
771 PRINT_PACKET(dev, (uintptr_t)desc_addr, desc->len, 0);
775 * This mbuf reaches to its end, get a new one
778 if (mbuf_avail == 0) {
779 cur = rte_pktmbuf_alloc(mbuf_pool);
780 if (unlikely(cur == NULL)) {
781 RTE_LOG(ERR, VHOST_DATA, "Failed to "
782 "allocate memory for mbuf.\n");
787 prev->data_len = mbuf_offset;
789 m->pkt_len += mbuf_offset;
793 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
797 prev->data_len = mbuf_offset;
798 m->pkt_len += mbuf_offset;
800 if (hdr->flags != 0 || hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE)
801 vhost_dequeue_offload(hdr, m);
807 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
808 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
810 struct virtio_net *dev;
811 struct rte_mbuf *rarp_mbuf = NULL;
812 struct vhost_virtqueue *vq;
813 uint32_t desc_indexes[MAX_PKT_BURST];
816 uint16_t free_entries;
819 dev = get_device(vid);
823 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->virt_qp_nb))) {
824 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
825 dev->vid, __func__, queue_id);
829 vq = dev->virtqueue[queue_id];
830 if (unlikely(vq->enabled == 0))
834 * Construct a RARP broadcast packet, and inject it to the "pkts"
835 * array, to looks like that guest actually send such packet.
837 * Check user_send_rarp() for more information.
839 if (unlikely(rte_atomic16_cmpset((volatile uint16_t *)
840 &dev->broadcast_rarp.cnt, 1, 0))) {
841 rarp_mbuf = rte_pktmbuf_alloc(mbuf_pool);
842 if (rarp_mbuf == NULL) {
843 RTE_LOG(ERR, VHOST_DATA,
844 "Failed to allocate memory for mbuf.\n");
848 if (make_rarp_packet(rarp_mbuf, &dev->mac)) {
849 rte_pktmbuf_free(rarp_mbuf);
856 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
857 free_entries = avail_idx - vq->last_used_idx;
858 if (free_entries == 0)
861 LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
863 /* Prefetch available ring to retrieve head indexes. */
864 used_idx = vq->last_used_idx & (vq->size - 1);
865 rte_prefetch0(&vq->avail->ring[used_idx]);
866 rte_prefetch0(&vq->used->ring[used_idx]);
868 count = RTE_MIN(count, MAX_PKT_BURST);
869 count = RTE_MIN(count, free_entries);
870 LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
873 /* Retrieve all of the head indexes first to avoid caching issues. */
874 for (i = 0; i < count; i++) {
875 used_idx = (vq->last_used_idx + i) & (vq->size - 1);
876 desc_indexes[i] = vq->avail->ring[used_idx];
878 vq->used->ring[used_idx].id = desc_indexes[i];
879 vq->used->ring[used_idx].len = 0;
880 vhost_log_used_vring(dev, vq,
881 offsetof(struct vring_used, ring[used_idx]),
882 sizeof(vq->used->ring[used_idx]));
885 /* Prefetch descriptor index. */
886 rte_prefetch0(&vq->desc[desc_indexes[0]]);
887 for (i = 0; i < count; i++) {
890 if (likely(i + 1 < count))
891 rte_prefetch0(&vq->desc[desc_indexes[i + 1]]);
893 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
894 if (unlikely(pkts[i] == NULL)) {
895 RTE_LOG(ERR, VHOST_DATA,
896 "Failed to allocate memory for mbuf.\n");
899 err = copy_desc_to_mbuf(dev, vq, pkts[i], desc_indexes[i],
902 rte_pktmbuf_free(pkts[i]);
910 vq->last_used_idx += i;
911 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
912 sizeof(vq->used->idx));
914 /* Kick guest if required. */
915 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
916 && (vq->callfd >= 0))
917 eventfd_write(vq->callfd, (eventfd_t)1);
920 if (unlikely(rarp_mbuf != NULL)) {
922 * Inject it to the head of "pkts" array, so that switch's mac
923 * learning table will get updated first.
925 memmove(&pkts[1], pkts, i * sizeof(struct rte_mbuf *));