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);
147 do_data_copy_dequeue(struct vhost_virtqueue *vq)
149 struct batch_copy_elem *elem = vq->batch_copy_elems;
150 uint16_t count = vq->batch_copy_nb_elems;
153 for (i = 0; i < count; i++)
154 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
157 /* avoid write operation when necessary, to lessen cache issues */
158 #define ASSIGN_UNLESS_EQUAL(var, val) do { \
159 if ((var) != (val)) \
163 static __rte_always_inline void
164 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
166 uint64_t csum_l4 = m_buf->ol_flags & PKT_TX_L4_MASK;
168 if (m_buf->ol_flags & PKT_TX_TCP_SEG)
169 csum_l4 |= PKT_TX_TCP_CKSUM;
172 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
173 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
176 case PKT_TX_TCP_CKSUM:
177 net_hdr->csum_offset = (offsetof(struct tcp_hdr,
180 case PKT_TX_UDP_CKSUM:
181 net_hdr->csum_offset = (offsetof(struct udp_hdr,
184 case PKT_TX_SCTP_CKSUM:
185 net_hdr->csum_offset = (offsetof(struct sctp_hdr,
190 ASSIGN_UNLESS_EQUAL(net_hdr->csum_start, 0);
191 ASSIGN_UNLESS_EQUAL(net_hdr->csum_offset, 0);
192 ASSIGN_UNLESS_EQUAL(net_hdr->flags, 0);
195 /* IP cksum verification cannot be bypassed, then calculate here */
196 if (m_buf->ol_flags & PKT_TX_IP_CKSUM) {
197 struct ipv4_hdr *ipv4_hdr;
199 ipv4_hdr = rte_pktmbuf_mtod_offset(m_buf, struct ipv4_hdr *,
201 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
204 if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
205 if (m_buf->ol_flags & PKT_TX_IPV4)
206 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
208 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
209 net_hdr->gso_size = m_buf->tso_segsz;
210 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
212 } else if (m_buf->ol_flags & PKT_TX_UDP_SEG) {
213 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
214 net_hdr->gso_size = m_buf->tso_segsz;
215 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len +
218 ASSIGN_UNLESS_EQUAL(net_hdr->gso_type, 0);
219 ASSIGN_UNLESS_EQUAL(net_hdr->gso_size, 0);
220 ASSIGN_UNLESS_EQUAL(net_hdr->hdr_len, 0);
224 static __rte_always_inline int
225 fill_vec_buf(struct virtio_net *dev, struct vhost_virtqueue *vq,
226 uint32_t avail_idx, uint32_t *vec_idx,
227 struct buf_vector *buf_vec, uint16_t *desc_chain_head,
228 uint16_t *desc_chain_len, uint8_t perm)
230 uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
231 uint32_t vec_id = *vec_idx;
233 uint64_t dlen, desc_avail, desc_iova;
234 struct vring_desc *descs = vq->desc;
235 struct vring_desc *idesc = NULL;
237 *desc_chain_head = idx;
239 if (vq->desc[idx].flags & VRING_DESC_F_INDIRECT) {
240 dlen = vq->desc[idx].len;
241 descs = (struct vring_desc *)(uintptr_t)
242 vhost_iova_to_vva(dev, vq, vq->desc[idx].addr,
245 if (unlikely(!descs))
248 if (unlikely(dlen < vq->desc[idx].len)) {
250 * The indirect desc table is not contiguous
251 * in process VA space, we have to copy it.
253 idesc = alloc_copy_ind_table(dev, vq,
254 vq->desc[idx].addr, vq->desc[idx].len);
255 if (unlikely(!idesc))
265 if (unlikely(idx >= vq->size)) {
266 free_ind_table(idesc);
271 len += descs[idx].len;
272 desc_avail = descs[idx].len;
273 desc_iova = descs[idx].addr;
277 uint64_t desc_chunck_len = desc_avail;
279 if (unlikely(vec_id >= BUF_VECTOR_MAX)) {
280 free_ind_table(idesc);
284 desc_addr = vhost_iova_to_vva(dev, vq,
288 if (unlikely(!desc_addr)) {
289 free_ind_table(idesc);
293 buf_vec[vec_id].buf_iova = desc_iova;
294 buf_vec[vec_id].buf_addr = desc_addr;
295 buf_vec[vec_id].buf_len = desc_chunck_len;
296 buf_vec[vec_id].desc_idx = idx;
298 desc_avail -= desc_chunck_len;
299 desc_iova += desc_chunck_len;
303 if ((descs[idx].flags & VRING_DESC_F_NEXT) == 0)
306 idx = descs[idx].next;
309 *desc_chain_len = len;
312 if (unlikely(!!idesc))
313 free_ind_table(idesc);
319 * Returns -1 on fail, 0 on success
322 reserve_avail_buf(struct virtio_net *dev, struct vhost_virtqueue *vq,
323 uint32_t size, struct buf_vector *buf_vec,
324 uint16_t *num_buffers, uint16_t avail_head,
328 uint32_t vec_idx = 0;
329 uint16_t max_tries, tries = 0;
331 uint16_t head_idx = 0;
335 cur_idx = vq->last_avail_idx;
337 if (rxvq_is_mergeable(dev))
338 max_tries = vq->size;
343 if (unlikely(cur_idx == avail_head))
346 if (unlikely(fill_vec_buf(dev, vq, cur_idx, &vec_idx, buf_vec,
348 VHOST_ACCESS_RW) < 0))
350 len = RTE_MIN(len, size);
351 update_shadow_used_ring(vq, head_idx, len);
359 * if we tried all available ring items, and still
360 * can't get enough buf, it means something abnormal
363 if (unlikely(tries > max_tries))
372 static __rte_always_inline int
373 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
374 struct rte_mbuf *m, struct buf_vector *buf_vec,
375 uint16_t nr_vec, uint16_t num_buffers)
377 uint32_t vec_idx = 0;
378 uint32_t mbuf_offset, mbuf_avail;
379 uint32_t buf_offset, buf_avail;
380 uint64_t buf_addr, buf_iova, buf_len;
383 struct rte_mbuf *hdr_mbuf;
384 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
385 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
388 if (unlikely(m == NULL)) {
393 buf_addr = buf_vec[vec_idx].buf_addr;
394 buf_iova = buf_vec[vec_idx].buf_iova;
395 buf_len = buf_vec[vec_idx].buf_len;
398 rte_prefetch0((void *)(uintptr_t)buf_vec[1].buf_addr);
400 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
407 if (unlikely(buf_len < dev->vhost_hlen))
410 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
412 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) RX: num merge buffers %d\n",
413 dev->vid, num_buffers);
415 if (unlikely(buf_len < dev->vhost_hlen)) {
416 buf_offset = dev->vhost_hlen - buf_len;
418 buf_addr = buf_vec[vec_idx].buf_addr;
419 buf_iova = buf_vec[vec_idx].buf_iova;
420 buf_len = buf_vec[vec_idx].buf_len;
421 buf_avail = buf_len - buf_offset;
423 buf_offset = dev->vhost_hlen;
424 buf_avail = buf_len - dev->vhost_hlen;
427 mbuf_avail = rte_pktmbuf_data_len(m);
429 while (mbuf_avail != 0 || m->next != NULL) {
430 /* done with current buf, get the next one */
431 if (buf_avail == 0) {
433 if (unlikely(vec_idx >= nr_vec)) {
438 buf_addr = buf_vec[vec_idx].buf_addr;
439 buf_iova = buf_vec[vec_idx].buf_iova;
440 buf_len = buf_vec[vec_idx].buf_len;
442 /* Prefetch next buffer address. */
443 if (vec_idx + 1 < nr_vec)
444 rte_prefetch0((void *)(uintptr_t)
445 buf_vec[vec_idx + 1].buf_addr);
450 /* done with current mbuf, get the next one */
451 if (mbuf_avail == 0) {
455 mbuf_avail = rte_pktmbuf_data_len(m);
459 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
460 if (rxvq_is_mergeable(dev))
461 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
464 if (unlikely(hdr == &tmp_hdr)) {
466 uint64_t remain = dev->vhost_hlen;
467 uint64_t src = (uint64_t)(uintptr_t)hdr, dst;
468 uint64_t iova = buf_vec[0].buf_iova;
469 uint16_t hdr_vec_idx = 0;
473 dst = buf_vec[hdr_vec_idx].buf_addr;
474 rte_memcpy((void *)(uintptr_t)dst,
475 (void *)(uintptr_t)src,
478 PRINT_PACKET(dev, (uintptr_t)dst,
480 vhost_log_cache_write(dev, vq,
489 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
491 vhost_log_cache_write(dev, vq,
499 cpy_len = RTE_MIN(buf_len, mbuf_avail);
501 if (likely(cpy_len > MAX_BATCH_LEN ||
502 vq->batch_copy_nb_elems >= vq->size)) {
503 rte_memcpy((void *)((uintptr_t)(buf_addr + buf_offset)),
504 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
506 vhost_log_cache_write(dev, vq, buf_iova + buf_offset,
508 PRINT_PACKET(dev, (uintptr_t)(buf_addr + buf_offset),
511 batch_copy[vq->batch_copy_nb_elems].dst =
512 (void *)((uintptr_t)(buf_addr + buf_offset));
513 batch_copy[vq->batch_copy_nb_elems].src =
514 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
515 batch_copy[vq->batch_copy_nb_elems].log_addr =
516 buf_iova + buf_offset;
517 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
518 vq->batch_copy_nb_elems++;
521 mbuf_avail -= cpy_len;
522 mbuf_offset += cpy_len;
523 buf_avail -= cpy_len;
524 buf_offset += cpy_len;
532 static __rte_always_inline uint32_t
533 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
534 struct rte_mbuf **pkts, uint32_t count)
536 struct vhost_virtqueue *vq;
537 uint32_t pkt_idx = 0;
538 uint16_t num_buffers;
539 struct buf_vector buf_vec[BUF_VECTOR_MAX];
542 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
543 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
544 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
545 dev->vid, __func__, queue_id);
549 vq = dev->virtqueue[queue_id];
551 rte_spinlock_lock(&vq->access_lock);
553 if (unlikely(vq->enabled == 0))
554 goto out_access_unlock;
556 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
557 vhost_user_iotlb_rd_lock(vq);
559 if (unlikely(vq->access_ok == 0))
560 if (unlikely(vring_translate(dev, vq) < 0))
563 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
567 vq->batch_copy_nb_elems = 0;
569 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
571 avail_head = *((volatile uint16_t *)&vq->avail->idx);
572 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
573 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
576 if (unlikely(reserve_avail_buf(dev, vq,
577 pkt_len, buf_vec, &num_buffers,
578 avail_head, &nr_vec) < 0)) {
579 VHOST_LOG_DEBUG(VHOST_DATA,
580 "(%d) failed to get enough desc from vring\n",
582 vq->shadow_used_idx -= num_buffers;
586 rte_prefetch0((void *)(uintptr_t)buf_vec[0].buf_addr);
588 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
589 dev->vid, vq->last_avail_idx,
590 vq->last_avail_idx + num_buffers);
592 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
595 vq->shadow_used_idx -= num_buffers;
599 vq->last_avail_idx += num_buffers;
602 do_data_copy_enqueue(dev, vq);
604 if (likely(vq->shadow_used_idx)) {
605 flush_shadow_used_ring(dev, vq);
606 vhost_vring_call(dev, vq);
610 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
611 vhost_user_iotlb_rd_unlock(vq);
614 rte_spinlock_unlock(&vq->access_lock);
620 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
621 struct rte_mbuf **pkts, uint16_t count)
623 struct virtio_net *dev = get_device(vid);
628 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
629 RTE_LOG(ERR, VHOST_DATA,
630 "(%d) %s: built-in vhost net backend is disabled.\n",
635 return virtio_dev_rx(dev, queue_id, pkts, count);
639 virtio_net_with_host_offload(struct virtio_net *dev)
642 ((1ULL << VIRTIO_NET_F_CSUM) |
643 (1ULL << VIRTIO_NET_F_HOST_ECN) |
644 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
645 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
646 (1ULL << VIRTIO_NET_F_HOST_UFO)))
653 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
655 struct ipv4_hdr *ipv4_hdr;
656 struct ipv6_hdr *ipv6_hdr;
658 struct ether_hdr *eth_hdr;
661 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
663 m->l2_len = sizeof(struct ether_hdr);
664 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
666 if (ethertype == ETHER_TYPE_VLAN) {
667 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
669 m->l2_len += sizeof(struct vlan_hdr);
670 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
673 l3_hdr = (char *)eth_hdr + m->l2_len;
676 case ETHER_TYPE_IPv4:
678 *l4_proto = ipv4_hdr->next_proto_id;
679 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
680 *l4_hdr = (char *)l3_hdr + m->l3_len;
681 m->ol_flags |= PKT_TX_IPV4;
683 case ETHER_TYPE_IPv6:
685 *l4_proto = ipv6_hdr->proto;
686 m->l3_len = sizeof(struct ipv6_hdr);
687 *l4_hdr = (char *)l3_hdr + m->l3_len;
688 m->ol_flags |= PKT_TX_IPV6;
698 static __rte_always_inline void
699 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
701 uint16_t l4_proto = 0;
703 struct tcp_hdr *tcp_hdr = NULL;
705 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
708 parse_ethernet(m, &l4_proto, &l4_hdr);
709 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
710 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
711 switch (hdr->csum_offset) {
712 case (offsetof(struct tcp_hdr, cksum)):
713 if (l4_proto == IPPROTO_TCP)
714 m->ol_flags |= PKT_TX_TCP_CKSUM;
716 case (offsetof(struct udp_hdr, dgram_cksum)):
717 if (l4_proto == IPPROTO_UDP)
718 m->ol_flags |= PKT_TX_UDP_CKSUM;
720 case (offsetof(struct sctp_hdr, cksum)):
721 if (l4_proto == IPPROTO_SCTP)
722 m->ol_flags |= PKT_TX_SCTP_CKSUM;
730 if (l4_hdr && hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
731 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
732 case VIRTIO_NET_HDR_GSO_TCPV4:
733 case VIRTIO_NET_HDR_GSO_TCPV6:
735 m->ol_flags |= PKT_TX_TCP_SEG;
736 m->tso_segsz = hdr->gso_size;
737 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
739 case VIRTIO_NET_HDR_GSO_UDP:
740 m->ol_flags |= PKT_TX_UDP_SEG;
741 m->tso_segsz = hdr->gso_size;
742 m->l4_len = sizeof(struct udp_hdr);
745 RTE_LOG(WARNING, VHOST_DATA,
746 "unsupported gso type %u.\n", hdr->gso_type);
752 static __rte_always_inline void
753 put_zmbuf(struct zcopy_mbuf *zmbuf)
758 static __rte_always_inline int
759 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
760 struct buf_vector *buf_vec, uint16_t nr_vec,
761 struct rte_mbuf *m, struct rte_mempool *mbuf_pool)
763 uint32_t buf_avail, buf_offset;
764 uint64_t buf_addr, buf_iova, buf_len;
765 uint32_t mbuf_avail, mbuf_offset;
767 struct rte_mbuf *cur = m, *prev = m;
768 struct virtio_net_hdr tmp_hdr;
769 struct virtio_net_hdr *hdr = NULL;
770 /* A counter to avoid desc dead loop chain */
771 uint16_t vec_idx = 0;
772 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
775 buf_addr = buf_vec[vec_idx].buf_addr;
776 buf_iova = buf_vec[vec_idx].buf_iova;
777 buf_len = buf_vec[vec_idx].buf_len;
779 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
784 if (likely(nr_vec > 1))
785 rte_prefetch0((void *)(uintptr_t)buf_vec[1].buf_addr);
787 if (virtio_net_with_host_offload(dev)) {
788 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
790 uint64_t remain = sizeof(struct virtio_net_hdr);
792 uint64_t dst = (uint64_t)(uintptr_t)&tmp_hdr;
793 uint16_t hdr_vec_idx = 0;
796 * No luck, the virtio-net header doesn't fit
797 * in a contiguous virtual area.
801 src = buf_vec[hdr_vec_idx].buf_addr;
802 rte_memcpy((void *)(uintptr_t)dst,
803 (void *)(uintptr_t)src, len);
812 hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
818 * A virtio driver normally uses at least 2 desc buffers
819 * for Tx: the first for storing the header, and others
820 * for storing the data.
822 if (unlikely(buf_len < dev->vhost_hlen)) {
823 buf_offset = dev->vhost_hlen - buf_len;
825 buf_addr = buf_vec[vec_idx].buf_addr;
826 buf_iova = buf_vec[vec_idx].buf_iova;
827 buf_len = buf_vec[vec_idx].buf_len;
828 buf_avail = buf_len - buf_offset;
829 } else if (buf_len == dev->vhost_hlen) {
830 if (unlikely(++vec_idx >= nr_vec))
832 buf_addr = buf_vec[vec_idx].buf_addr;
833 buf_iova = buf_vec[vec_idx].buf_iova;
834 buf_len = buf_vec[vec_idx].buf_len;
839 buf_offset = dev->vhost_hlen;
840 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
843 rte_prefetch0((void *)(uintptr_t)
844 (buf_addr + buf_offset));
847 (uintptr_t)(buf_addr + buf_offset),
848 (uint32_t)buf_avail, 0);
851 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
855 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
858 * A desc buf might across two host physical pages that are
859 * not continuous. In such case (gpa_to_hpa returns 0), data
860 * will be copied even though zero copy is enabled.
862 if (unlikely(dev->dequeue_zero_copy && (hpa = gpa_to_hpa(dev,
863 buf_iova + buf_offset, cpy_len)))) {
864 cur->data_len = cpy_len;
867 (void *)(uintptr_t)(buf_addr + buf_offset);
871 * In zero copy mode, one mbuf can only reference data
872 * for one or partial of one desc buff.
874 mbuf_avail = cpy_len;
876 if (likely(cpy_len > MAX_BATCH_LEN ||
877 vq->batch_copy_nb_elems >= vq->size ||
878 (hdr && cur == m))) {
879 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
881 (void *)((uintptr_t)(buf_addr +
885 batch_copy[vq->batch_copy_nb_elems].dst =
886 rte_pktmbuf_mtod_offset(cur, void *,
888 batch_copy[vq->batch_copy_nb_elems].src =
889 (void *)((uintptr_t)(buf_addr +
891 batch_copy[vq->batch_copy_nb_elems].len =
893 vq->batch_copy_nb_elems++;
897 mbuf_avail -= cpy_len;
898 mbuf_offset += cpy_len;
899 buf_avail -= cpy_len;
900 buf_offset += cpy_len;
902 /* This buf reaches to its end, get the next one */
903 if (buf_avail == 0) {
904 if (++vec_idx >= nr_vec)
907 buf_addr = buf_vec[vec_idx].buf_addr;
908 buf_iova = buf_vec[vec_idx].buf_iova;
909 buf_len = buf_vec[vec_idx].buf_len;
912 * Prefecth desc n + 1 buffer while
913 * desc n buffer is processed.
915 if (vec_idx + 1 < nr_vec)
916 rte_prefetch0((void *)(uintptr_t)
917 buf_vec[vec_idx + 1].buf_addr);
922 PRINT_PACKET(dev, (uintptr_t)buf_addr,
923 (uint32_t)buf_avail, 0);
927 * This mbuf reaches to its end, get a new one
930 if (mbuf_avail == 0) {
931 cur = rte_pktmbuf_alloc(mbuf_pool);
932 if (unlikely(cur == NULL)) {
933 RTE_LOG(ERR, VHOST_DATA, "Failed to "
934 "allocate memory for mbuf.\n");
938 if (unlikely(dev->dequeue_zero_copy))
939 rte_mbuf_refcnt_update(cur, 1);
942 prev->data_len = mbuf_offset;
944 m->pkt_len += mbuf_offset;
948 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
952 prev->data_len = mbuf_offset;
953 m->pkt_len += mbuf_offset;
956 vhost_dequeue_offload(hdr, m);
963 static __rte_always_inline struct zcopy_mbuf *
964 get_zmbuf(struct vhost_virtqueue *vq)
970 /* search [last_zmbuf_idx, zmbuf_size) */
971 i = vq->last_zmbuf_idx;
972 last = vq->zmbuf_size;
975 for (; i < last; i++) {
976 if (vq->zmbufs[i].in_use == 0) {
977 vq->last_zmbuf_idx = i + 1;
978 vq->zmbufs[i].in_use = 1;
979 return &vq->zmbufs[i];
985 /* search [0, last_zmbuf_idx) */
987 last = vq->last_zmbuf_idx;
994 static __rte_always_inline bool
995 mbuf_is_consumed(struct rte_mbuf *m)
998 if (rte_mbuf_refcnt_read(m) > 1)
1006 static __rte_always_inline void
1007 restore_mbuf(struct rte_mbuf *m)
1009 uint32_t mbuf_size, priv_size;
1012 priv_size = rte_pktmbuf_priv_size(m->pool);
1013 mbuf_size = sizeof(struct rte_mbuf) + priv_size;
1014 /* start of buffer is after mbuf structure and priv data */
1016 m->buf_addr = (char *)m + mbuf_size;
1017 m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size;
1023 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
1024 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1026 struct virtio_net *dev;
1027 struct rte_mbuf *rarp_mbuf = NULL;
1028 struct vhost_virtqueue *vq;
1030 uint16_t free_entries;
1032 dev = get_device(vid);
1036 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1037 RTE_LOG(ERR, VHOST_DATA,
1038 "(%d) %s: built-in vhost net backend is disabled.\n",
1039 dev->vid, __func__);
1043 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
1044 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
1045 dev->vid, __func__, queue_id);
1049 vq = dev->virtqueue[queue_id];
1051 if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
1054 if (unlikely(vq->enabled == 0))
1055 goto out_access_unlock;
1057 vq->batch_copy_nb_elems = 0;
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;