1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
7 #include <linux/virtio_net.h>
10 #include <rte_memcpy.h>
11 #include <rte_ether.h>
13 #include <rte_vhost.h>
18 #include <rte_spinlock.h>
19 #include <rte_malloc.h>
24 #define MAX_PKT_BURST 32
26 #define MAX_BATCH_LEN 256
28 static __rte_always_inline bool
29 rxvq_is_mergeable(struct virtio_net *dev)
31 return dev->features & (1ULL << VIRTIO_NET_F_MRG_RXBUF);
35 is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t nr_vring)
37 return (is_tx ^ (idx & 1)) == 0 && idx < nr_vring;
40 static __rte_always_inline void *
41 alloc_copy_ind_table(struct virtio_net *dev, struct vhost_virtqueue *vq,
42 uint64_t desc_addr, uint64_t desc_len)
46 uint64_t len, remain = desc_len;
48 idesc = rte_malloc(__func__, desc_len, 0);
52 dst = (uint64_t)(uintptr_t)idesc;
56 src = vhost_iova_to_vva(dev, vq, desc_addr, &len,
58 if (unlikely(!src || !len)) {
63 rte_memcpy((void *)(uintptr_t)dst, (void *)(uintptr_t)src, len);
73 static __rte_always_inline void
74 free_ind_table(void *idesc)
79 static __rte_always_inline void
80 do_flush_shadow_used_ring(struct virtio_net *dev, struct vhost_virtqueue *vq,
81 uint16_t to, uint16_t from, uint16_t size)
83 rte_memcpy(&vq->used->ring[to],
84 &vq->shadow_used_ring[from],
85 size * sizeof(struct vring_used_elem));
86 vhost_log_cache_used_vring(dev, vq,
87 offsetof(struct vring_used, ring[to]),
88 size * sizeof(struct vring_used_elem));
91 static __rte_always_inline void
92 flush_shadow_used_ring(struct virtio_net *dev, struct vhost_virtqueue *vq)
94 uint16_t used_idx = vq->last_used_idx & (vq->size - 1);
96 if (used_idx + vq->shadow_used_idx <= vq->size) {
97 do_flush_shadow_used_ring(dev, vq, used_idx, 0,
102 /* update used ring interval [used_idx, vq->size] */
103 size = vq->size - used_idx;
104 do_flush_shadow_used_ring(dev, vq, used_idx, 0, size);
106 /* update the left half used ring interval [0, left_size] */
107 do_flush_shadow_used_ring(dev, vq, 0, size,
108 vq->shadow_used_idx - size);
110 vq->last_used_idx += vq->shadow_used_idx;
114 vhost_log_cache_sync(dev, vq);
116 *(volatile uint16_t *)&vq->used->idx += vq->shadow_used_idx;
117 vq->shadow_used_idx = 0;
118 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
119 sizeof(vq->used->idx));
122 static __rte_always_inline void
123 update_shadow_used_ring(struct vhost_virtqueue *vq,
124 uint16_t desc_idx, uint16_t len)
126 uint16_t i = vq->shadow_used_idx++;
128 vq->shadow_used_ring[i].id = desc_idx;
129 vq->shadow_used_ring[i].len = len;
133 do_data_copy_enqueue(struct virtio_net *dev, struct vhost_virtqueue *vq)
135 struct batch_copy_elem *elem = vq->batch_copy_elems;
136 uint16_t count = vq->batch_copy_nb_elems;
139 for (i = 0; i < count; i++) {
140 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
141 vhost_log_cache_write(dev, vq, elem[i].log_addr, elem[i].len);
142 PRINT_PACKET(dev, (uintptr_t)elem[i].dst, elem[i].len, 0);
145 vq->batch_copy_nb_elems = 0;
149 do_data_copy_dequeue(struct vhost_virtqueue *vq)
151 struct batch_copy_elem *elem = vq->batch_copy_elems;
152 uint16_t count = vq->batch_copy_nb_elems;
155 for (i = 0; i < count; i++)
156 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
158 vq->batch_copy_nb_elems = 0;
161 /* avoid write operation when necessary, to lessen cache issues */
162 #define ASSIGN_UNLESS_EQUAL(var, val) do { \
163 if ((var) != (val)) \
167 static __rte_always_inline void
168 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
170 uint64_t csum_l4 = m_buf->ol_flags & PKT_TX_L4_MASK;
172 if (m_buf->ol_flags & PKT_TX_TCP_SEG)
173 csum_l4 |= PKT_TX_TCP_CKSUM;
176 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
177 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
180 case PKT_TX_TCP_CKSUM:
181 net_hdr->csum_offset = (offsetof(struct tcp_hdr,
184 case PKT_TX_UDP_CKSUM:
185 net_hdr->csum_offset = (offsetof(struct udp_hdr,
188 case PKT_TX_SCTP_CKSUM:
189 net_hdr->csum_offset = (offsetof(struct sctp_hdr,
194 ASSIGN_UNLESS_EQUAL(net_hdr->csum_start, 0);
195 ASSIGN_UNLESS_EQUAL(net_hdr->csum_offset, 0);
196 ASSIGN_UNLESS_EQUAL(net_hdr->flags, 0);
199 /* IP cksum verification cannot be bypassed, then calculate here */
200 if (m_buf->ol_flags & PKT_TX_IP_CKSUM) {
201 struct ipv4_hdr *ipv4_hdr;
203 ipv4_hdr = rte_pktmbuf_mtod_offset(m_buf, struct ipv4_hdr *,
205 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
208 if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
209 if (m_buf->ol_flags & PKT_TX_IPV4)
210 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
212 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
213 net_hdr->gso_size = m_buf->tso_segsz;
214 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
216 } else if (m_buf->ol_flags & PKT_TX_UDP_SEG) {
217 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
218 net_hdr->gso_size = m_buf->tso_segsz;
219 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len +
222 ASSIGN_UNLESS_EQUAL(net_hdr->gso_type, 0);
223 ASSIGN_UNLESS_EQUAL(net_hdr->gso_size, 0);
224 ASSIGN_UNLESS_EQUAL(net_hdr->hdr_len, 0);
228 static __rte_always_inline int
229 fill_vec_buf(struct virtio_net *dev, struct vhost_virtqueue *vq,
230 uint32_t avail_idx, uint32_t *vec_idx,
231 struct buf_vector *buf_vec, uint16_t *desc_chain_head,
232 uint16_t *desc_chain_len, uint8_t perm)
234 uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
235 uint32_t vec_id = *vec_idx;
237 uint64_t dlen, desc_avail, desc_iova;
238 struct vring_desc *descs = vq->desc;
239 struct vring_desc *idesc = NULL;
241 *desc_chain_head = idx;
243 if (vq->desc[idx].flags & VRING_DESC_F_INDIRECT) {
244 dlen = vq->desc[idx].len;
245 descs = (struct vring_desc *)(uintptr_t)
246 vhost_iova_to_vva(dev, vq, vq->desc[idx].addr,
249 if (unlikely(!descs))
252 if (unlikely(dlen < vq->desc[idx].len)) {
254 * The indirect desc table is not contiguous
255 * in process VA space, we have to copy it.
257 idesc = alloc_copy_ind_table(dev, vq,
258 vq->desc[idx].addr, vq->desc[idx].len);
259 if (unlikely(!idesc))
269 if (unlikely(idx >= vq->size)) {
270 free_ind_table(idesc);
275 len += descs[idx].len;
276 desc_avail = descs[idx].len;
277 desc_iova = descs[idx].addr;
281 uint64_t desc_chunck_len = desc_avail;
283 if (unlikely(vec_id >= BUF_VECTOR_MAX)) {
284 free_ind_table(idesc);
288 desc_addr = vhost_iova_to_vva(dev, vq,
292 if (unlikely(!desc_addr)) {
293 free_ind_table(idesc);
297 buf_vec[vec_id].buf_iova = desc_iova;
298 buf_vec[vec_id].buf_addr = desc_addr;
299 buf_vec[vec_id].buf_len = desc_chunck_len;
300 buf_vec[vec_id].desc_idx = idx;
302 desc_avail -= desc_chunck_len;
303 desc_iova += desc_chunck_len;
307 if ((descs[idx].flags & VRING_DESC_F_NEXT) == 0)
310 idx = descs[idx].next;
313 *desc_chain_len = len;
316 if (unlikely(!!idesc))
317 free_ind_table(idesc);
323 * Returns -1 on fail, 0 on success
326 reserve_avail_buf(struct virtio_net *dev, struct vhost_virtqueue *vq,
327 uint32_t size, struct buf_vector *buf_vec,
328 uint16_t *num_buffers, uint16_t avail_head,
332 uint32_t vec_idx = 0;
333 uint16_t max_tries, tries = 0;
335 uint16_t head_idx = 0;
339 cur_idx = vq->last_avail_idx;
341 if (rxvq_is_mergeable(dev))
342 max_tries = vq->size;
347 if (unlikely(cur_idx == avail_head))
350 if (unlikely(fill_vec_buf(dev, vq, cur_idx, &vec_idx, buf_vec,
352 VHOST_ACCESS_RW) < 0))
354 len = RTE_MIN(len, size);
355 update_shadow_used_ring(vq, head_idx, len);
363 * if we tried all available ring items, and still
364 * can't get enough buf, it means something abnormal
367 if (unlikely(tries > max_tries))
376 static __rte_always_inline int
377 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
378 struct rte_mbuf *m, struct buf_vector *buf_vec,
379 uint16_t nr_vec, uint16_t num_buffers)
381 uint32_t vec_idx = 0;
382 uint32_t mbuf_offset, mbuf_avail;
383 uint32_t buf_offset, buf_avail;
384 uint64_t buf_addr, buf_iova, buf_len;
387 struct rte_mbuf *hdr_mbuf;
388 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
389 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
392 if (unlikely(m == NULL)) {
397 buf_addr = buf_vec[vec_idx].buf_addr;
398 buf_iova = buf_vec[vec_idx].buf_iova;
399 buf_len = buf_vec[vec_idx].buf_len;
402 rte_prefetch0((void *)(uintptr_t)buf_vec[1].buf_addr);
404 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
411 if (unlikely(buf_len < dev->vhost_hlen))
414 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
416 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) RX: num merge buffers %d\n",
417 dev->vid, num_buffers);
419 if (unlikely(buf_len < dev->vhost_hlen)) {
420 buf_offset = dev->vhost_hlen - buf_len;
422 buf_addr = buf_vec[vec_idx].buf_addr;
423 buf_iova = buf_vec[vec_idx].buf_iova;
424 buf_len = buf_vec[vec_idx].buf_len;
425 buf_avail = buf_len - buf_offset;
427 buf_offset = dev->vhost_hlen;
428 buf_avail = buf_len - dev->vhost_hlen;
431 mbuf_avail = rte_pktmbuf_data_len(m);
433 while (mbuf_avail != 0 || m->next != NULL) {
434 /* done with current buf, get the next one */
435 if (buf_avail == 0) {
437 if (unlikely(vec_idx >= nr_vec)) {
442 buf_addr = buf_vec[vec_idx].buf_addr;
443 buf_iova = buf_vec[vec_idx].buf_iova;
444 buf_len = buf_vec[vec_idx].buf_len;
446 /* Prefetch next buffer address. */
447 if (vec_idx + 1 < nr_vec)
448 rte_prefetch0((void *)(uintptr_t)
449 buf_vec[vec_idx + 1].buf_addr);
454 /* done with current mbuf, get the next one */
455 if (mbuf_avail == 0) {
459 mbuf_avail = rte_pktmbuf_data_len(m);
463 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
464 if (rxvq_is_mergeable(dev))
465 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
468 if (unlikely(hdr == &tmp_hdr)) {
470 uint64_t remain = dev->vhost_hlen;
471 uint64_t src = (uint64_t)(uintptr_t)hdr, dst;
472 uint64_t iova = buf_vec[0].buf_iova;
473 uint16_t hdr_vec_idx = 0;
477 dst = buf_vec[hdr_vec_idx].buf_addr;
478 rte_memcpy((void *)(uintptr_t)dst,
479 (void *)(uintptr_t)src,
482 PRINT_PACKET(dev, (uintptr_t)dst,
484 vhost_log_cache_write(dev, vq,
493 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
495 vhost_log_cache_write(dev, vq,
503 cpy_len = RTE_MIN(buf_len, mbuf_avail);
505 if (likely(cpy_len > MAX_BATCH_LEN ||
506 vq->batch_copy_nb_elems >= vq->size)) {
507 rte_memcpy((void *)((uintptr_t)(buf_addr + buf_offset)),
508 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
510 vhost_log_cache_write(dev, vq, buf_iova + buf_offset,
512 PRINT_PACKET(dev, (uintptr_t)(buf_addr + buf_offset),
515 batch_copy[vq->batch_copy_nb_elems].dst =
516 (void *)((uintptr_t)(buf_addr + buf_offset));
517 batch_copy[vq->batch_copy_nb_elems].src =
518 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
519 batch_copy[vq->batch_copy_nb_elems].log_addr =
520 buf_iova + buf_offset;
521 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
522 vq->batch_copy_nb_elems++;
525 mbuf_avail -= cpy_len;
526 mbuf_offset += cpy_len;
527 buf_avail -= cpy_len;
528 buf_offset += cpy_len;
536 static __rte_always_inline uint32_t
537 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
538 struct rte_mbuf **pkts, uint32_t count)
540 struct vhost_virtqueue *vq;
541 uint32_t pkt_idx = 0;
542 uint16_t num_buffers;
543 struct buf_vector buf_vec[BUF_VECTOR_MAX];
546 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
547 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
548 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
549 dev->vid, __func__, queue_id);
553 vq = dev->virtqueue[queue_id];
555 rte_spinlock_lock(&vq->access_lock);
557 if (unlikely(vq->enabled == 0))
558 goto out_access_unlock;
560 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
561 vhost_user_iotlb_rd_lock(vq);
563 if (unlikely(vq->access_ok == 0))
564 if (unlikely(vring_translate(dev, vq) < 0))
567 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
571 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
573 avail_head = *((volatile uint16_t *)&vq->avail->idx);
574 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
575 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
578 if (unlikely(reserve_avail_buf(dev, vq,
579 pkt_len, buf_vec, &num_buffers,
580 avail_head, &nr_vec) < 0)) {
581 VHOST_LOG_DEBUG(VHOST_DATA,
582 "(%d) failed to get enough desc from vring\n",
584 vq->shadow_used_idx -= num_buffers;
588 rte_prefetch0((void *)(uintptr_t)buf_vec[0].buf_addr);
590 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
591 dev->vid, vq->last_avail_idx,
592 vq->last_avail_idx + num_buffers);
594 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
597 vq->shadow_used_idx -= num_buffers;
601 vq->last_avail_idx += num_buffers;
604 do_data_copy_enqueue(dev, vq);
606 if (likely(vq->shadow_used_idx)) {
607 flush_shadow_used_ring(dev, vq);
608 vhost_vring_call(dev, vq);
612 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
613 vhost_user_iotlb_rd_unlock(vq);
616 rte_spinlock_unlock(&vq->access_lock);
622 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
623 struct rte_mbuf **pkts, uint16_t count)
625 struct virtio_net *dev = get_device(vid);
630 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
631 RTE_LOG(ERR, VHOST_DATA,
632 "(%d) %s: built-in vhost net backend is disabled.\n",
637 return virtio_dev_rx(dev, queue_id, pkts, count);
641 virtio_net_with_host_offload(struct virtio_net *dev)
644 ((1ULL << VIRTIO_NET_F_CSUM) |
645 (1ULL << VIRTIO_NET_F_HOST_ECN) |
646 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
647 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
648 (1ULL << VIRTIO_NET_F_HOST_UFO)))
655 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
657 struct ipv4_hdr *ipv4_hdr;
658 struct ipv6_hdr *ipv6_hdr;
660 struct ether_hdr *eth_hdr;
663 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
665 m->l2_len = sizeof(struct ether_hdr);
666 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
668 if (ethertype == ETHER_TYPE_VLAN) {
669 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
671 m->l2_len += sizeof(struct vlan_hdr);
672 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
675 l3_hdr = (char *)eth_hdr + m->l2_len;
678 case ETHER_TYPE_IPv4:
680 *l4_proto = ipv4_hdr->next_proto_id;
681 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
682 *l4_hdr = (char *)l3_hdr + m->l3_len;
683 m->ol_flags |= PKT_TX_IPV4;
685 case ETHER_TYPE_IPv6:
687 *l4_proto = ipv6_hdr->proto;
688 m->l3_len = sizeof(struct ipv6_hdr);
689 *l4_hdr = (char *)l3_hdr + m->l3_len;
690 m->ol_flags |= PKT_TX_IPV6;
700 static __rte_always_inline void
701 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
703 uint16_t l4_proto = 0;
705 struct tcp_hdr *tcp_hdr = NULL;
707 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
710 parse_ethernet(m, &l4_proto, &l4_hdr);
711 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
712 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
713 switch (hdr->csum_offset) {
714 case (offsetof(struct tcp_hdr, cksum)):
715 if (l4_proto == IPPROTO_TCP)
716 m->ol_flags |= PKT_TX_TCP_CKSUM;
718 case (offsetof(struct udp_hdr, dgram_cksum)):
719 if (l4_proto == IPPROTO_UDP)
720 m->ol_flags |= PKT_TX_UDP_CKSUM;
722 case (offsetof(struct sctp_hdr, cksum)):
723 if (l4_proto == IPPROTO_SCTP)
724 m->ol_flags |= PKT_TX_SCTP_CKSUM;
732 if (l4_hdr && hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
733 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
734 case VIRTIO_NET_HDR_GSO_TCPV4:
735 case VIRTIO_NET_HDR_GSO_TCPV6:
737 m->ol_flags |= PKT_TX_TCP_SEG;
738 m->tso_segsz = hdr->gso_size;
739 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
741 case VIRTIO_NET_HDR_GSO_UDP:
742 m->ol_flags |= PKT_TX_UDP_SEG;
743 m->tso_segsz = hdr->gso_size;
744 m->l4_len = sizeof(struct udp_hdr);
747 RTE_LOG(WARNING, VHOST_DATA,
748 "unsupported gso type %u.\n", hdr->gso_type);
754 static __rte_always_inline void
755 put_zmbuf(struct zcopy_mbuf *zmbuf)
760 static __rte_always_inline int
761 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
762 struct buf_vector *buf_vec, uint16_t nr_vec,
763 struct rte_mbuf *m, struct rte_mempool *mbuf_pool)
765 uint32_t buf_avail, buf_offset;
766 uint64_t buf_addr, buf_iova, buf_len;
767 uint32_t mbuf_avail, mbuf_offset;
769 struct rte_mbuf *cur = m, *prev = m;
770 struct virtio_net_hdr tmp_hdr;
771 struct virtio_net_hdr *hdr = NULL;
772 /* A counter to avoid desc dead loop chain */
773 uint16_t vec_idx = 0;
774 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
777 buf_addr = buf_vec[vec_idx].buf_addr;
778 buf_iova = buf_vec[vec_idx].buf_iova;
779 buf_len = buf_vec[vec_idx].buf_len;
781 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
786 if (likely(nr_vec > 1))
787 rte_prefetch0((void *)(uintptr_t)buf_vec[1].buf_addr);
789 if (virtio_net_with_host_offload(dev)) {
790 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
792 uint64_t remain = sizeof(struct virtio_net_hdr);
794 uint64_t dst = (uint64_t)(uintptr_t)&tmp_hdr;
795 uint16_t hdr_vec_idx = 0;
798 * No luck, the virtio-net header doesn't fit
799 * in a contiguous virtual area.
803 src = buf_vec[hdr_vec_idx].buf_addr;
804 rte_memcpy((void *)(uintptr_t)dst,
805 (void *)(uintptr_t)src, len);
814 hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
820 * A virtio driver normally uses at least 2 desc buffers
821 * for Tx: the first for storing the header, and others
822 * for storing the data.
824 if (unlikely(buf_len < dev->vhost_hlen)) {
825 buf_offset = dev->vhost_hlen - buf_len;
827 buf_addr = buf_vec[vec_idx].buf_addr;
828 buf_iova = buf_vec[vec_idx].buf_iova;
829 buf_len = buf_vec[vec_idx].buf_len;
830 buf_avail = buf_len - buf_offset;
831 } else if (buf_len == dev->vhost_hlen) {
832 if (unlikely(++vec_idx >= nr_vec))
834 buf_addr = buf_vec[vec_idx].buf_addr;
835 buf_iova = buf_vec[vec_idx].buf_iova;
836 buf_len = buf_vec[vec_idx].buf_len;
841 buf_offset = dev->vhost_hlen;
842 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
845 rte_prefetch0((void *)(uintptr_t)
846 (buf_addr + buf_offset));
849 (uintptr_t)(buf_addr + buf_offset),
850 (uint32_t)buf_avail, 0);
853 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
857 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
860 * A desc buf might across two host physical pages that are
861 * not continuous. In such case (gpa_to_hpa returns 0), data
862 * will be copied even though zero copy is enabled.
864 if (unlikely(dev->dequeue_zero_copy && (hpa = gpa_to_hpa(dev,
865 buf_iova + buf_offset, cpy_len)))) {
866 cur->data_len = cpy_len;
869 (void *)(uintptr_t)(buf_addr + buf_offset);
873 * In zero copy mode, one mbuf can only reference data
874 * for one or partial of one desc buff.
876 mbuf_avail = cpy_len;
878 if (likely(cpy_len > MAX_BATCH_LEN ||
879 vq->batch_copy_nb_elems >= vq->size ||
880 (hdr && cur == m))) {
881 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
883 (void *)((uintptr_t)(buf_addr +
887 batch_copy[vq->batch_copy_nb_elems].dst =
888 rte_pktmbuf_mtod_offset(cur, void *,
890 batch_copy[vq->batch_copy_nb_elems].src =
891 (void *)((uintptr_t)(buf_addr +
893 batch_copy[vq->batch_copy_nb_elems].len =
895 vq->batch_copy_nb_elems++;
899 mbuf_avail -= cpy_len;
900 mbuf_offset += cpy_len;
901 buf_avail -= cpy_len;
902 buf_offset += cpy_len;
904 /* This buf reaches to its end, get the next one */
905 if (buf_avail == 0) {
906 if (++vec_idx >= nr_vec)
909 buf_addr = buf_vec[vec_idx].buf_addr;
910 buf_iova = buf_vec[vec_idx].buf_iova;
911 buf_len = buf_vec[vec_idx].buf_len;
914 * Prefecth desc n + 1 buffer while
915 * desc n buffer is processed.
917 if (vec_idx + 1 < nr_vec)
918 rte_prefetch0((void *)(uintptr_t)
919 buf_vec[vec_idx + 1].buf_addr);
924 PRINT_PACKET(dev, (uintptr_t)buf_addr,
925 (uint32_t)buf_avail, 0);
929 * This mbuf reaches to its end, get a new one
932 if (mbuf_avail == 0) {
933 cur = rte_pktmbuf_alloc(mbuf_pool);
934 if (unlikely(cur == NULL)) {
935 RTE_LOG(ERR, VHOST_DATA, "Failed to "
936 "allocate memory for mbuf.\n");
940 if (unlikely(dev->dequeue_zero_copy))
941 rte_mbuf_refcnt_update(cur, 1);
944 prev->data_len = mbuf_offset;
946 m->pkt_len += mbuf_offset;
950 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
954 prev->data_len = mbuf_offset;
955 m->pkt_len += mbuf_offset;
958 vhost_dequeue_offload(hdr, m);
965 static __rte_always_inline struct zcopy_mbuf *
966 get_zmbuf(struct vhost_virtqueue *vq)
972 /* search [last_zmbuf_idx, zmbuf_size) */
973 i = vq->last_zmbuf_idx;
974 last = vq->zmbuf_size;
977 for (; i < last; i++) {
978 if (vq->zmbufs[i].in_use == 0) {
979 vq->last_zmbuf_idx = i + 1;
980 vq->zmbufs[i].in_use = 1;
981 return &vq->zmbufs[i];
987 /* search [0, last_zmbuf_idx) */
989 last = vq->last_zmbuf_idx;
996 static __rte_always_inline bool
997 mbuf_is_consumed(struct rte_mbuf *m)
1000 if (rte_mbuf_refcnt_read(m) > 1)
1008 static __rte_always_inline void
1009 restore_mbuf(struct rte_mbuf *m)
1011 uint32_t mbuf_size, priv_size;
1014 priv_size = rte_pktmbuf_priv_size(m->pool);
1015 mbuf_size = sizeof(struct rte_mbuf) + priv_size;
1016 /* start of buffer is after mbuf structure and priv data */
1018 m->buf_addr = (char *)m + mbuf_size;
1019 m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size;
1025 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
1026 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1028 struct virtio_net *dev;
1029 struct rte_mbuf *rarp_mbuf = NULL;
1030 struct vhost_virtqueue *vq;
1032 uint16_t free_entries;
1034 dev = get_device(vid);
1038 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1039 RTE_LOG(ERR, VHOST_DATA,
1040 "(%d) %s: built-in vhost net backend is disabled.\n",
1041 dev->vid, __func__);
1045 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
1046 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
1047 dev->vid, __func__, queue_id);
1051 vq = dev->virtqueue[queue_id];
1053 if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
1056 if (unlikely(vq->enabled == 0))
1057 goto out_access_unlock;
1059 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1060 vhost_user_iotlb_rd_lock(vq);
1062 if (unlikely(vq->access_ok == 0))
1063 if (unlikely(vring_translate(dev, vq) < 0))
1066 if (unlikely(dev->dequeue_zero_copy)) {
1067 struct zcopy_mbuf *zmbuf, *next;
1070 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1071 zmbuf != NULL; zmbuf = next) {
1072 next = TAILQ_NEXT(zmbuf, next);
1074 if (mbuf_is_consumed(zmbuf->mbuf)) {
1075 update_shadow_used_ring(vq, zmbuf->desc_idx, 0);
1078 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1079 restore_mbuf(zmbuf->mbuf);
1080 rte_pktmbuf_free(zmbuf->mbuf);
1086 flush_shadow_used_ring(dev, vq);
1087 vhost_vring_call(dev, vq);
1090 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1093 * Construct a RARP broadcast packet, and inject it to the "pkts"
1094 * array, to looks like that guest actually send such packet.
1096 * Check user_send_rarp() for more information.
1098 * broadcast_rarp shares a cacheline in the virtio_net structure
1099 * with some fields that are accessed during enqueue and
1100 * rte_atomic16_cmpset() causes a write if using cmpxchg. This could
1101 * result in false sharing between enqueue and dequeue.
1103 * Prevent unnecessary false sharing by reading broadcast_rarp first
1104 * and only performing cmpset if the read indicates it is likely to
1108 if (unlikely(rte_atomic16_read(&dev->broadcast_rarp) &&
1109 rte_atomic16_cmpset((volatile uint16_t *)
1110 &dev->broadcast_rarp.cnt, 1, 0))) {
1112 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
1113 if (rarp_mbuf == NULL) {
1114 RTE_LOG(ERR, VHOST_DATA,
1115 "Failed to make RARP packet.\n");
1121 free_entries = *((volatile uint16_t *)&vq->avail->idx) -
1123 if (free_entries == 0)
1126 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1128 count = RTE_MIN(count, MAX_PKT_BURST);
1129 count = RTE_MIN(count, free_entries);
1130 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1133 for (i = 0; i < count; i++) {
1134 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1135 uint16_t head_idx, dummy_len;
1136 uint32_t nr_vec = 0;
1139 if (unlikely(fill_vec_buf(dev, vq,
1140 vq->last_avail_idx + i,
1142 &head_idx, &dummy_len,
1143 VHOST_ACCESS_RO) < 0))
1146 if (likely(dev->dequeue_zero_copy == 0))
1147 update_shadow_used_ring(vq, head_idx, 0);
1149 rte_prefetch0((void *)(uintptr_t)buf_vec[0].buf_addr);
1151 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
1152 if (unlikely(pkts[i] == NULL)) {
1153 RTE_LOG(ERR, VHOST_DATA,
1154 "Failed to allocate memory for mbuf.\n");
1158 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
1160 if (unlikely(err)) {
1161 rte_pktmbuf_free(pkts[i]);
1165 if (unlikely(dev->dequeue_zero_copy)) {
1166 struct zcopy_mbuf *zmbuf;
1168 zmbuf = get_zmbuf(vq);
1170 rte_pktmbuf_free(pkts[i]);
1173 zmbuf->mbuf = pkts[i];
1174 zmbuf->desc_idx = head_idx;
1177 * Pin lock the mbuf; we will check later to see
1178 * whether the mbuf is freed (when we are the last
1179 * user) or not. If that's the case, we then could
1180 * update the used ring safely.
1182 rte_mbuf_refcnt_update(pkts[i], 1);
1185 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1188 vq->last_avail_idx += i;
1190 if (likely(dev->dequeue_zero_copy == 0)) {
1191 do_data_copy_dequeue(vq);
1192 if (unlikely(i < count))
1193 vq->shadow_used_idx = i;
1194 flush_shadow_used_ring(dev, vq);
1195 vhost_vring_call(dev, vq);
1199 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1200 vhost_user_iotlb_rd_unlock(vq);
1203 rte_spinlock_unlock(&vq->access_lock);
1205 if (unlikely(rarp_mbuf != NULL)) {
1207 * Inject it to the head of "pkts" array, so that switch's mac
1208 * learning table will get updated first.
1210 memmove(&pkts[1], pkts, i * sizeof(struct rte_mbuf *));
1211 pkts[0] = rarp_mbuf;