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_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
230 uint32_t avail_idx, uint16_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 uint16_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_split(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 uint16_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_split(dev, vq, cur_idx,
353 VHOST_ACCESS_RW) < 0))
355 len = RTE_MIN(len, size);
356 update_shadow_used_ring(vq, head_idx, len);
364 * if we tried all available ring items, and still
365 * can't get enough buf, it means something abnormal
368 if (unlikely(tries > max_tries))
377 static __rte_always_inline int
378 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
379 struct rte_mbuf *m, struct buf_vector *buf_vec,
380 uint16_t nr_vec, uint16_t num_buffers)
382 uint32_t vec_idx = 0;
383 uint32_t mbuf_offset, mbuf_avail;
384 uint32_t buf_offset, buf_avail;
385 uint64_t buf_addr, buf_iova, buf_len;
388 struct rte_mbuf *hdr_mbuf;
389 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
390 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
393 if (unlikely(m == NULL)) {
398 buf_addr = buf_vec[vec_idx].buf_addr;
399 buf_iova = buf_vec[vec_idx].buf_iova;
400 buf_len = buf_vec[vec_idx].buf_len;
403 rte_prefetch0((void *)(uintptr_t)buf_vec[1].buf_addr);
405 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
412 if (unlikely(buf_len < dev->vhost_hlen))
415 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
417 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) RX: num merge buffers %d\n",
418 dev->vid, num_buffers);
420 if (unlikely(buf_len < dev->vhost_hlen)) {
421 buf_offset = dev->vhost_hlen - buf_len;
423 buf_addr = buf_vec[vec_idx].buf_addr;
424 buf_iova = buf_vec[vec_idx].buf_iova;
425 buf_len = buf_vec[vec_idx].buf_len;
426 buf_avail = buf_len - buf_offset;
428 buf_offset = dev->vhost_hlen;
429 buf_avail = buf_len - dev->vhost_hlen;
432 mbuf_avail = rte_pktmbuf_data_len(m);
434 while (mbuf_avail != 0 || m->next != NULL) {
435 /* done with current buf, get the next one */
436 if (buf_avail == 0) {
438 if (unlikely(vec_idx >= nr_vec)) {
443 buf_addr = buf_vec[vec_idx].buf_addr;
444 buf_iova = buf_vec[vec_idx].buf_iova;
445 buf_len = buf_vec[vec_idx].buf_len;
447 /* Prefetch next buffer address. */
448 if (vec_idx + 1 < nr_vec)
449 rte_prefetch0((void *)(uintptr_t)
450 buf_vec[vec_idx + 1].buf_addr);
455 /* done with current mbuf, get the next one */
456 if (mbuf_avail == 0) {
460 mbuf_avail = rte_pktmbuf_data_len(m);
464 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
465 if (rxvq_is_mergeable(dev))
466 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
469 if (unlikely(hdr == &tmp_hdr)) {
471 uint64_t remain = dev->vhost_hlen;
472 uint64_t src = (uint64_t)(uintptr_t)hdr, dst;
473 uint64_t iova = buf_vec[0].buf_iova;
474 uint16_t hdr_vec_idx = 0;
478 dst = buf_vec[hdr_vec_idx].buf_addr;
479 rte_memcpy((void *)(uintptr_t)dst,
480 (void *)(uintptr_t)src,
483 PRINT_PACKET(dev, (uintptr_t)dst,
485 vhost_log_cache_write(dev, vq,
494 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
496 vhost_log_cache_write(dev, vq,
504 cpy_len = RTE_MIN(buf_len, mbuf_avail);
506 if (likely(cpy_len > MAX_BATCH_LEN ||
507 vq->batch_copy_nb_elems >= vq->size)) {
508 rte_memcpy((void *)((uintptr_t)(buf_addr + buf_offset)),
509 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
511 vhost_log_cache_write(dev, vq, buf_iova + buf_offset,
513 PRINT_PACKET(dev, (uintptr_t)(buf_addr + buf_offset),
516 batch_copy[vq->batch_copy_nb_elems].dst =
517 (void *)((uintptr_t)(buf_addr + buf_offset));
518 batch_copy[vq->batch_copy_nb_elems].src =
519 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
520 batch_copy[vq->batch_copy_nb_elems].log_addr =
521 buf_iova + buf_offset;
522 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
523 vq->batch_copy_nb_elems++;
526 mbuf_avail -= cpy_len;
527 mbuf_offset += cpy_len;
528 buf_avail -= cpy_len;
529 buf_offset += cpy_len;
537 static __rte_always_inline uint32_t
538 virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
539 struct rte_mbuf **pkts, uint32_t count)
541 uint32_t pkt_idx = 0;
542 uint16_t num_buffers;
543 struct buf_vector buf_vec[BUF_VECTOR_MAX];
546 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
547 avail_head = *((volatile uint16_t *)&vq->avail->idx);
549 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
550 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
553 if (unlikely(reserve_avail_buf_split(dev, vq,
554 pkt_len, buf_vec, &num_buffers,
555 avail_head, &nr_vec) < 0)) {
556 VHOST_LOG_DEBUG(VHOST_DATA,
557 "(%d) failed to get enough desc from vring\n",
559 vq->shadow_used_idx -= num_buffers;
563 rte_prefetch0((void *)(uintptr_t)buf_vec[0].buf_addr);
565 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
566 dev->vid, vq->last_avail_idx,
567 vq->last_avail_idx + num_buffers);
569 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
572 vq->shadow_used_idx -= num_buffers;
576 vq->last_avail_idx += num_buffers;
579 do_data_copy_enqueue(dev, vq);
581 if (likely(vq->shadow_used_idx)) {
582 flush_shadow_used_ring(dev, vq);
583 vhost_vring_call(dev, vq);
589 static __rte_always_inline uint32_t
590 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
591 struct rte_mbuf **pkts, uint32_t count)
593 struct vhost_virtqueue *vq;
595 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
596 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
597 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
598 dev->vid, __func__, queue_id);
602 vq = dev->virtqueue[queue_id];
604 rte_spinlock_lock(&vq->access_lock);
606 if (unlikely(vq->enabled == 0))
607 goto out_access_unlock;
609 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
610 vhost_user_iotlb_rd_lock(vq);
612 if (unlikely(vq->access_ok == 0))
613 if (unlikely(vring_translate(dev, vq) < 0))
616 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
620 count = virtio_dev_rx_split(dev, vq, pkts, count);
623 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
624 vhost_user_iotlb_rd_unlock(vq);
627 rte_spinlock_unlock(&vq->access_lock);
633 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
634 struct rte_mbuf **pkts, uint16_t count)
636 struct virtio_net *dev = get_device(vid);
641 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
642 RTE_LOG(ERR, VHOST_DATA,
643 "(%d) %s: built-in vhost net backend is disabled.\n",
648 return virtio_dev_rx(dev, queue_id, pkts, count);
652 virtio_net_with_host_offload(struct virtio_net *dev)
655 ((1ULL << VIRTIO_NET_F_CSUM) |
656 (1ULL << VIRTIO_NET_F_HOST_ECN) |
657 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
658 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
659 (1ULL << VIRTIO_NET_F_HOST_UFO)))
666 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
668 struct ipv4_hdr *ipv4_hdr;
669 struct ipv6_hdr *ipv6_hdr;
671 struct ether_hdr *eth_hdr;
674 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
676 m->l2_len = sizeof(struct ether_hdr);
677 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
679 if (ethertype == ETHER_TYPE_VLAN) {
680 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
682 m->l2_len += sizeof(struct vlan_hdr);
683 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
686 l3_hdr = (char *)eth_hdr + m->l2_len;
689 case ETHER_TYPE_IPv4:
691 *l4_proto = ipv4_hdr->next_proto_id;
692 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
693 *l4_hdr = (char *)l3_hdr + m->l3_len;
694 m->ol_flags |= PKT_TX_IPV4;
696 case ETHER_TYPE_IPv6:
698 *l4_proto = ipv6_hdr->proto;
699 m->l3_len = sizeof(struct ipv6_hdr);
700 *l4_hdr = (char *)l3_hdr + m->l3_len;
701 m->ol_flags |= PKT_TX_IPV6;
711 static __rte_always_inline void
712 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
714 uint16_t l4_proto = 0;
716 struct tcp_hdr *tcp_hdr = NULL;
718 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
721 parse_ethernet(m, &l4_proto, &l4_hdr);
722 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
723 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
724 switch (hdr->csum_offset) {
725 case (offsetof(struct tcp_hdr, cksum)):
726 if (l4_proto == IPPROTO_TCP)
727 m->ol_flags |= PKT_TX_TCP_CKSUM;
729 case (offsetof(struct udp_hdr, dgram_cksum)):
730 if (l4_proto == IPPROTO_UDP)
731 m->ol_flags |= PKT_TX_UDP_CKSUM;
733 case (offsetof(struct sctp_hdr, cksum)):
734 if (l4_proto == IPPROTO_SCTP)
735 m->ol_flags |= PKT_TX_SCTP_CKSUM;
743 if (l4_hdr && hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
744 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
745 case VIRTIO_NET_HDR_GSO_TCPV4:
746 case VIRTIO_NET_HDR_GSO_TCPV6:
748 m->ol_flags |= PKT_TX_TCP_SEG;
749 m->tso_segsz = hdr->gso_size;
750 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
752 case VIRTIO_NET_HDR_GSO_UDP:
753 m->ol_flags |= PKT_TX_UDP_SEG;
754 m->tso_segsz = hdr->gso_size;
755 m->l4_len = sizeof(struct udp_hdr);
758 RTE_LOG(WARNING, VHOST_DATA,
759 "unsupported gso type %u.\n", hdr->gso_type);
765 static __rte_always_inline void
766 put_zmbuf(struct zcopy_mbuf *zmbuf)
771 static __rte_always_inline int
772 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
773 struct buf_vector *buf_vec, uint16_t nr_vec,
774 struct rte_mbuf *m, struct rte_mempool *mbuf_pool)
776 uint32_t buf_avail, buf_offset;
777 uint64_t buf_addr, buf_iova, buf_len;
778 uint32_t mbuf_avail, mbuf_offset;
780 struct rte_mbuf *cur = m, *prev = m;
781 struct virtio_net_hdr tmp_hdr;
782 struct virtio_net_hdr *hdr = NULL;
783 /* A counter to avoid desc dead loop chain */
784 uint16_t vec_idx = 0;
785 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
788 buf_addr = buf_vec[vec_idx].buf_addr;
789 buf_iova = buf_vec[vec_idx].buf_iova;
790 buf_len = buf_vec[vec_idx].buf_len;
792 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
797 if (likely(nr_vec > 1))
798 rte_prefetch0((void *)(uintptr_t)buf_vec[1].buf_addr);
800 if (virtio_net_with_host_offload(dev)) {
801 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
803 uint64_t remain = sizeof(struct virtio_net_hdr);
805 uint64_t dst = (uint64_t)(uintptr_t)&tmp_hdr;
806 uint16_t hdr_vec_idx = 0;
809 * No luck, the virtio-net header doesn't fit
810 * in a contiguous virtual area.
814 src = buf_vec[hdr_vec_idx].buf_addr;
815 rte_memcpy((void *)(uintptr_t)dst,
816 (void *)(uintptr_t)src, len);
825 hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
831 * A virtio driver normally uses at least 2 desc buffers
832 * for Tx: the first for storing the header, and others
833 * for storing the data.
835 if (unlikely(buf_len < dev->vhost_hlen)) {
836 buf_offset = dev->vhost_hlen - buf_len;
838 buf_addr = buf_vec[vec_idx].buf_addr;
839 buf_iova = buf_vec[vec_idx].buf_iova;
840 buf_len = buf_vec[vec_idx].buf_len;
841 buf_avail = buf_len - buf_offset;
842 } else if (buf_len == dev->vhost_hlen) {
843 if (unlikely(++vec_idx >= nr_vec))
845 buf_addr = buf_vec[vec_idx].buf_addr;
846 buf_iova = buf_vec[vec_idx].buf_iova;
847 buf_len = buf_vec[vec_idx].buf_len;
852 buf_offset = dev->vhost_hlen;
853 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
856 rte_prefetch0((void *)(uintptr_t)
857 (buf_addr + buf_offset));
860 (uintptr_t)(buf_addr + buf_offset),
861 (uint32_t)buf_avail, 0);
864 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
868 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
871 * A desc buf might across two host physical pages that are
872 * not continuous. In such case (gpa_to_hpa returns 0), data
873 * will be copied even though zero copy is enabled.
875 if (unlikely(dev->dequeue_zero_copy && (hpa = gpa_to_hpa(dev,
876 buf_iova + buf_offset, cpy_len)))) {
877 cur->data_len = cpy_len;
880 (void *)(uintptr_t)(buf_addr + buf_offset);
884 * In zero copy mode, one mbuf can only reference data
885 * for one or partial of one desc buff.
887 mbuf_avail = cpy_len;
889 if (likely(cpy_len > MAX_BATCH_LEN ||
890 vq->batch_copy_nb_elems >= vq->size ||
891 (hdr && cur == m))) {
892 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
894 (void *)((uintptr_t)(buf_addr +
898 batch_copy[vq->batch_copy_nb_elems].dst =
899 rte_pktmbuf_mtod_offset(cur, void *,
901 batch_copy[vq->batch_copy_nb_elems].src =
902 (void *)((uintptr_t)(buf_addr +
904 batch_copy[vq->batch_copy_nb_elems].len =
906 vq->batch_copy_nb_elems++;
910 mbuf_avail -= cpy_len;
911 mbuf_offset += cpy_len;
912 buf_avail -= cpy_len;
913 buf_offset += cpy_len;
915 /* This buf reaches to its end, get the next one */
916 if (buf_avail == 0) {
917 if (++vec_idx >= nr_vec)
920 buf_addr = buf_vec[vec_idx].buf_addr;
921 buf_iova = buf_vec[vec_idx].buf_iova;
922 buf_len = buf_vec[vec_idx].buf_len;
925 * Prefecth desc n + 1 buffer while
926 * desc n buffer is processed.
928 if (vec_idx + 1 < nr_vec)
929 rte_prefetch0((void *)(uintptr_t)
930 buf_vec[vec_idx + 1].buf_addr);
935 PRINT_PACKET(dev, (uintptr_t)buf_addr,
936 (uint32_t)buf_avail, 0);
940 * This mbuf reaches to its end, get a new one
943 if (mbuf_avail == 0) {
944 cur = rte_pktmbuf_alloc(mbuf_pool);
945 if (unlikely(cur == NULL)) {
946 RTE_LOG(ERR, VHOST_DATA, "Failed to "
947 "allocate memory for mbuf.\n");
951 if (unlikely(dev->dequeue_zero_copy))
952 rte_mbuf_refcnt_update(cur, 1);
955 prev->data_len = mbuf_offset;
957 m->pkt_len += mbuf_offset;
961 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
965 prev->data_len = mbuf_offset;
966 m->pkt_len += mbuf_offset;
969 vhost_dequeue_offload(hdr, m);
976 static __rte_always_inline struct zcopy_mbuf *
977 get_zmbuf(struct vhost_virtqueue *vq)
983 /* search [last_zmbuf_idx, zmbuf_size) */
984 i = vq->last_zmbuf_idx;
985 last = vq->zmbuf_size;
988 for (; i < last; i++) {
989 if (vq->zmbufs[i].in_use == 0) {
990 vq->last_zmbuf_idx = i + 1;
991 vq->zmbufs[i].in_use = 1;
992 return &vq->zmbufs[i];
998 /* search [0, last_zmbuf_idx) */
1000 last = vq->last_zmbuf_idx;
1007 static __rte_always_inline bool
1008 mbuf_is_consumed(struct rte_mbuf *m)
1011 if (rte_mbuf_refcnt_read(m) > 1)
1019 static __rte_always_inline void
1020 restore_mbuf(struct rte_mbuf *m)
1022 uint32_t mbuf_size, priv_size;
1025 priv_size = rte_pktmbuf_priv_size(m->pool);
1026 mbuf_size = sizeof(struct rte_mbuf) + priv_size;
1027 /* start of buffer is after mbuf structure and priv data */
1029 m->buf_addr = (char *)m + mbuf_size;
1030 m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size;
1035 static __rte_always_inline uint16_t
1036 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1037 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1040 uint16_t free_entries;
1042 if (unlikely(dev->dequeue_zero_copy)) {
1043 struct zcopy_mbuf *zmbuf, *next;
1046 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1047 zmbuf != NULL; zmbuf = next) {
1048 next = TAILQ_NEXT(zmbuf, next);
1050 if (mbuf_is_consumed(zmbuf->mbuf)) {
1051 update_shadow_used_ring(vq, zmbuf->desc_idx, 0);
1054 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1055 restore_mbuf(zmbuf->mbuf);
1056 rte_pktmbuf_free(zmbuf->mbuf);
1062 flush_shadow_used_ring(dev, vq);
1063 vhost_vring_call(dev, vq);
1066 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1068 free_entries = *((volatile uint16_t *)&vq->avail->idx) -
1070 if (free_entries == 0)
1073 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1075 count = RTE_MIN(count, MAX_PKT_BURST);
1076 count = RTE_MIN(count, free_entries);
1077 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1080 for (i = 0; i < count; i++) {
1081 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1082 uint16_t head_idx, dummy_len;
1083 uint16_t nr_vec = 0;
1086 if (unlikely(fill_vec_buf_split(dev, vq,
1087 vq->last_avail_idx + i,
1089 &head_idx, &dummy_len,
1090 VHOST_ACCESS_RO) < 0))
1093 if (likely(dev->dequeue_zero_copy == 0))
1094 update_shadow_used_ring(vq, head_idx, 0);
1096 rte_prefetch0((void *)(uintptr_t)buf_vec[0].buf_addr);
1098 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
1099 if (unlikely(pkts[i] == NULL)) {
1100 RTE_LOG(ERR, VHOST_DATA,
1101 "Failed to allocate memory for mbuf.\n");
1105 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
1107 if (unlikely(err)) {
1108 rte_pktmbuf_free(pkts[i]);
1112 if (unlikely(dev->dequeue_zero_copy)) {
1113 struct zcopy_mbuf *zmbuf;
1115 zmbuf = get_zmbuf(vq);
1117 rte_pktmbuf_free(pkts[i]);
1120 zmbuf->mbuf = pkts[i];
1121 zmbuf->desc_idx = head_idx;
1124 * Pin lock the mbuf; we will check later to see
1125 * whether the mbuf is freed (when we are the last
1126 * user) or not. If that's the case, we then could
1127 * update the used ring safely.
1129 rte_mbuf_refcnt_update(pkts[i], 1);
1132 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1135 vq->last_avail_idx += i;
1137 if (likely(dev->dequeue_zero_copy == 0)) {
1138 do_data_copy_dequeue(vq);
1139 if (unlikely(i < count))
1140 vq->shadow_used_idx = i;
1141 flush_shadow_used_ring(dev, vq);
1142 vhost_vring_call(dev, vq);
1149 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
1150 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1152 struct virtio_net *dev;
1153 struct rte_mbuf *rarp_mbuf = NULL;
1154 struct vhost_virtqueue *vq;
1156 dev = get_device(vid);
1160 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1161 RTE_LOG(ERR, VHOST_DATA,
1162 "(%d) %s: built-in vhost net backend is disabled.\n",
1163 dev->vid, __func__);
1167 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
1168 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
1169 dev->vid, __func__, queue_id);
1173 vq = dev->virtqueue[queue_id];
1175 if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
1178 if (unlikely(vq->enabled == 0))
1179 goto out_access_unlock;
1181 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1182 vhost_user_iotlb_rd_lock(vq);
1184 if (unlikely(vq->access_ok == 0))
1185 if (unlikely(vring_translate(dev, vq) < 0))
1189 * Construct a RARP broadcast packet, and inject it to the "pkts"
1190 * array, to looks like that guest actually send such packet.
1192 * Check user_send_rarp() for more information.
1194 * broadcast_rarp shares a cacheline in the virtio_net structure
1195 * with some fields that are accessed during enqueue and
1196 * rte_atomic16_cmpset() causes a write if using cmpxchg. This could
1197 * result in false sharing between enqueue and dequeue.
1199 * Prevent unnecessary false sharing by reading broadcast_rarp first
1200 * and only performing cmpset if the read indicates it is likely to
1203 if (unlikely(rte_atomic16_read(&dev->broadcast_rarp) &&
1204 rte_atomic16_cmpset((volatile uint16_t *)
1205 &dev->broadcast_rarp.cnt, 1, 0))) {
1207 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
1208 if (rarp_mbuf == NULL) {
1209 RTE_LOG(ERR, VHOST_DATA,
1210 "Failed to make RARP packet.\n");
1216 count = virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count);
1219 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1220 vhost_user_iotlb_rd_unlock(vq);
1223 rte_spinlock_unlock(&vq->access_lock);
1225 if (unlikely(rarp_mbuf != NULL)) {
1227 * Inject it to the head of "pkts" array, so that switch's mac
1228 * learning table will get updated first.
1230 memmove(&pkts[1], pkts, count * sizeof(struct rte_mbuf *));
1231 pkts[0] = rarp_mbuf;