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_split(struct virtio_net *dev,
81 struct vhost_virtqueue *vq,
82 uint16_t to, uint16_t from, uint16_t size)
84 rte_memcpy(&vq->used->ring[to],
85 &vq->shadow_used_ring[from],
86 size * sizeof(struct vring_used_elem));
87 vhost_log_cache_used_vring(dev, vq,
88 offsetof(struct vring_used, ring[to]),
89 size * sizeof(struct vring_used_elem));
92 static __rte_always_inline void
93 flush_shadow_used_ring_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
95 uint16_t used_idx = vq->last_used_idx & (vq->size - 1);
97 if (used_idx + vq->shadow_used_idx <= vq->size) {
98 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0,
103 /* update used ring interval [used_idx, vq->size] */
104 size = vq->size - used_idx;
105 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0, size);
107 /* update the left half used ring interval [0, left_size] */
108 do_flush_shadow_used_ring_split(dev, vq, 0, size,
109 vq->shadow_used_idx - size);
111 vq->last_used_idx += vq->shadow_used_idx;
115 vhost_log_cache_sync(dev, vq);
117 *(volatile uint16_t *)&vq->used->idx += vq->shadow_used_idx;
118 vq->shadow_used_idx = 0;
119 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
120 sizeof(vq->used->idx));
123 static __rte_always_inline void
124 update_shadow_used_ring_split(struct vhost_virtqueue *vq,
125 uint16_t desc_idx, uint16_t len)
127 uint16_t i = vq->shadow_used_idx++;
129 vq->shadow_used_ring[i].id = desc_idx;
130 vq->shadow_used_ring[i].len = len;
134 do_data_copy_enqueue(struct virtio_net *dev, struct vhost_virtqueue *vq)
136 struct batch_copy_elem *elem = vq->batch_copy_elems;
137 uint16_t count = vq->batch_copy_nb_elems;
140 for (i = 0; i < count; i++) {
141 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
142 vhost_log_cache_write(dev, vq, elem[i].log_addr, elem[i].len);
143 PRINT_PACKET(dev, (uintptr_t)elem[i].dst, elem[i].len, 0);
146 vq->batch_copy_nb_elems = 0;
150 do_data_copy_dequeue(struct vhost_virtqueue *vq)
152 struct batch_copy_elem *elem = vq->batch_copy_elems;
153 uint16_t count = vq->batch_copy_nb_elems;
156 for (i = 0; i < count; i++)
157 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
159 vq->batch_copy_nb_elems = 0;
162 /* avoid write operation when necessary, to lessen cache issues */
163 #define ASSIGN_UNLESS_EQUAL(var, val) do { \
164 if ((var) != (val)) \
168 static __rte_always_inline void
169 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
171 uint64_t csum_l4 = m_buf->ol_flags & PKT_TX_L4_MASK;
173 if (m_buf->ol_flags & PKT_TX_TCP_SEG)
174 csum_l4 |= PKT_TX_TCP_CKSUM;
177 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
178 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
181 case PKT_TX_TCP_CKSUM:
182 net_hdr->csum_offset = (offsetof(struct tcp_hdr,
185 case PKT_TX_UDP_CKSUM:
186 net_hdr->csum_offset = (offsetof(struct udp_hdr,
189 case PKT_TX_SCTP_CKSUM:
190 net_hdr->csum_offset = (offsetof(struct sctp_hdr,
195 ASSIGN_UNLESS_EQUAL(net_hdr->csum_start, 0);
196 ASSIGN_UNLESS_EQUAL(net_hdr->csum_offset, 0);
197 ASSIGN_UNLESS_EQUAL(net_hdr->flags, 0);
200 /* IP cksum verification cannot be bypassed, then calculate here */
201 if (m_buf->ol_flags & PKT_TX_IP_CKSUM) {
202 struct ipv4_hdr *ipv4_hdr;
204 ipv4_hdr = rte_pktmbuf_mtod_offset(m_buf, struct ipv4_hdr *,
206 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
209 if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
210 if (m_buf->ol_flags & PKT_TX_IPV4)
211 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
213 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
214 net_hdr->gso_size = m_buf->tso_segsz;
215 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
217 } else if (m_buf->ol_flags & PKT_TX_UDP_SEG) {
218 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
219 net_hdr->gso_size = m_buf->tso_segsz;
220 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len +
223 ASSIGN_UNLESS_EQUAL(net_hdr->gso_type, 0);
224 ASSIGN_UNLESS_EQUAL(net_hdr->gso_size, 0);
225 ASSIGN_UNLESS_EQUAL(net_hdr->hdr_len, 0);
229 static __rte_always_inline int
230 fill_vec_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
231 uint32_t avail_idx, uint16_t *vec_idx,
232 struct buf_vector *buf_vec, uint16_t *desc_chain_head,
233 uint16_t *desc_chain_len, uint8_t perm)
235 uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
236 uint16_t vec_id = *vec_idx;
238 uint64_t dlen, desc_avail, desc_iova;
239 struct vring_desc *descs = vq->desc;
240 struct vring_desc *idesc = NULL;
242 *desc_chain_head = idx;
244 if (vq->desc[idx].flags & VRING_DESC_F_INDIRECT) {
245 dlen = vq->desc[idx].len;
246 descs = (struct vring_desc *)(uintptr_t)
247 vhost_iova_to_vva(dev, vq, vq->desc[idx].addr,
250 if (unlikely(!descs))
253 if (unlikely(dlen < vq->desc[idx].len)) {
255 * The indirect desc table is not contiguous
256 * in process VA space, we have to copy it.
258 idesc = alloc_copy_ind_table(dev, vq,
259 vq->desc[idx].addr, vq->desc[idx].len);
260 if (unlikely(!idesc))
270 if (unlikely(idx >= vq->size)) {
271 free_ind_table(idesc);
276 len += descs[idx].len;
277 desc_avail = descs[idx].len;
278 desc_iova = descs[idx].addr;
282 uint64_t desc_chunck_len = desc_avail;
284 if (unlikely(vec_id >= BUF_VECTOR_MAX)) {
285 free_ind_table(idesc);
289 desc_addr = vhost_iova_to_vva(dev, vq,
293 if (unlikely(!desc_addr)) {
294 free_ind_table(idesc);
298 buf_vec[vec_id].buf_iova = desc_iova;
299 buf_vec[vec_id].buf_addr = desc_addr;
300 buf_vec[vec_id].buf_len = desc_chunck_len;
301 buf_vec[vec_id].desc_idx = idx;
303 desc_avail -= desc_chunck_len;
304 desc_iova += desc_chunck_len;
308 if ((descs[idx].flags & VRING_DESC_F_NEXT) == 0)
311 idx = descs[idx].next;
314 *desc_chain_len = len;
317 if (unlikely(!!idesc))
318 free_ind_table(idesc);
324 * Returns -1 on fail, 0 on success
327 reserve_avail_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
328 uint32_t size, struct buf_vector *buf_vec,
329 uint16_t *num_buffers, uint16_t avail_head,
333 uint16_t vec_idx = 0;
334 uint16_t max_tries, tries = 0;
336 uint16_t head_idx = 0;
340 cur_idx = vq->last_avail_idx;
342 if (rxvq_is_mergeable(dev))
343 max_tries = vq->size;
348 if (unlikely(cur_idx == avail_head))
351 if (unlikely(fill_vec_buf_split(dev, vq, cur_idx,
354 VHOST_ACCESS_RW) < 0))
356 len = RTE_MIN(len, size);
357 update_shadow_used_ring_split(vq, head_idx, len);
365 * if we tried all available ring items, and still
366 * can't get enough buf, it means something abnormal
369 if (unlikely(tries > max_tries))
378 static __rte_always_inline int
379 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
380 struct rte_mbuf *m, struct buf_vector *buf_vec,
381 uint16_t nr_vec, uint16_t num_buffers)
383 uint32_t vec_idx = 0;
384 uint32_t mbuf_offset, mbuf_avail;
385 uint32_t buf_offset, buf_avail;
386 uint64_t buf_addr, buf_iova, buf_len;
389 struct rte_mbuf *hdr_mbuf;
390 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
391 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
394 if (unlikely(m == NULL)) {
399 buf_addr = buf_vec[vec_idx].buf_addr;
400 buf_iova = buf_vec[vec_idx].buf_iova;
401 buf_len = buf_vec[vec_idx].buf_len;
404 rte_prefetch0((void *)(uintptr_t)buf_vec[1].buf_addr);
406 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
413 if (unlikely(buf_len < dev->vhost_hlen))
416 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
418 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) RX: num merge buffers %d\n",
419 dev->vid, num_buffers);
421 if (unlikely(buf_len < dev->vhost_hlen)) {
422 buf_offset = dev->vhost_hlen - buf_len;
424 buf_addr = buf_vec[vec_idx].buf_addr;
425 buf_iova = buf_vec[vec_idx].buf_iova;
426 buf_len = buf_vec[vec_idx].buf_len;
427 buf_avail = buf_len - buf_offset;
429 buf_offset = dev->vhost_hlen;
430 buf_avail = buf_len - dev->vhost_hlen;
433 mbuf_avail = rte_pktmbuf_data_len(m);
435 while (mbuf_avail != 0 || m->next != NULL) {
436 /* done with current buf, get the next one */
437 if (buf_avail == 0) {
439 if (unlikely(vec_idx >= nr_vec)) {
444 buf_addr = buf_vec[vec_idx].buf_addr;
445 buf_iova = buf_vec[vec_idx].buf_iova;
446 buf_len = buf_vec[vec_idx].buf_len;
448 /* Prefetch next buffer address. */
449 if (vec_idx + 1 < nr_vec)
450 rte_prefetch0((void *)(uintptr_t)
451 buf_vec[vec_idx + 1].buf_addr);
456 /* done with current mbuf, get the next one */
457 if (mbuf_avail == 0) {
461 mbuf_avail = rte_pktmbuf_data_len(m);
465 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
466 if (rxvq_is_mergeable(dev))
467 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
470 if (unlikely(hdr == &tmp_hdr)) {
472 uint64_t remain = dev->vhost_hlen;
473 uint64_t src = (uint64_t)(uintptr_t)hdr, dst;
474 uint64_t iova = buf_vec[0].buf_iova;
475 uint16_t hdr_vec_idx = 0;
479 dst = buf_vec[hdr_vec_idx].buf_addr;
480 rte_memcpy((void *)(uintptr_t)dst,
481 (void *)(uintptr_t)src,
484 PRINT_PACKET(dev, (uintptr_t)dst,
486 vhost_log_cache_write(dev, vq,
495 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
497 vhost_log_cache_write(dev, vq,
505 cpy_len = RTE_MIN(buf_len, mbuf_avail);
507 if (likely(cpy_len > MAX_BATCH_LEN ||
508 vq->batch_copy_nb_elems >= vq->size)) {
509 rte_memcpy((void *)((uintptr_t)(buf_addr + buf_offset)),
510 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
512 vhost_log_cache_write(dev, vq, buf_iova + buf_offset,
514 PRINT_PACKET(dev, (uintptr_t)(buf_addr + buf_offset),
517 batch_copy[vq->batch_copy_nb_elems].dst =
518 (void *)((uintptr_t)(buf_addr + buf_offset));
519 batch_copy[vq->batch_copy_nb_elems].src =
520 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
521 batch_copy[vq->batch_copy_nb_elems].log_addr =
522 buf_iova + buf_offset;
523 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
524 vq->batch_copy_nb_elems++;
527 mbuf_avail -= cpy_len;
528 mbuf_offset += cpy_len;
529 buf_avail -= cpy_len;
530 buf_offset += cpy_len;
538 static __rte_always_inline uint32_t
539 virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
540 struct rte_mbuf **pkts, uint32_t count)
542 uint32_t pkt_idx = 0;
543 uint16_t num_buffers;
544 struct buf_vector buf_vec[BUF_VECTOR_MAX];
547 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
548 avail_head = *((volatile uint16_t *)&vq->avail->idx);
550 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
551 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
554 if (unlikely(reserve_avail_buf_split(dev, vq,
555 pkt_len, buf_vec, &num_buffers,
556 avail_head, &nr_vec) < 0)) {
557 VHOST_LOG_DEBUG(VHOST_DATA,
558 "(%d) failed to get enough desc from vring\n",
560 vq->shadow_used_idx -= num_buffers;
564 rte_prefetch0((void *)(uintptr_t)buf_vec[0].buf_addr);
566 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
567 dev->vid, vq->last_avail_idx,
568 vq->last_avail_idx + num_buffers);
570 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
573 vq->shadow_used_idx -= num_buffers;
577 vq->last_avail_idx += num_buffers;
580 do_data_copy_enqueue(dev, vq);
582 if (likely(vq->shadow_used_idx)) {
583 flush_shadow_used_ring_split(dev, vq);
584 vhost_vring_call(dev, vq);
590 static __rte_always_inline uint32_t
591 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
592 struct rte_mbuf **pkts, uint32_t count)
594 struct vhost_virtqueue *vq;
596 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
597 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
598 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
599 dev->vid, __func__, queue_id);
603 vq = dev->virtqueue[queue_id];
605 rte_spinlock_lock(&vq->access_lock);
607 if (unlikely(vq->enabled == 0))
608 goto out_access_unlock;
610 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
611 vhost_user_iotlb_rd_lock(vq);
613 if (unlikely(vq->access_ok == 0))
614 if (unlikely(vring_translate(dev, vq) < 0))
617 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
621 count = virtio_dev_rx_split(dev, vq, pkts, count);
624 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
625 vhost_user_iotlb_rd_unlock(vq);
628 rte_spinlock_unlock(&vq->access_lock);
634 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
635 struct rte_mbuf **pkts, uint16_t count)
637 struct virtio_net *dev = get_device(vid);
642 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
643 RTE_LOG(ERR, VHOST_DATA,
644 "(%d) %s: built-in vhost net backend is disabled.\n",
649 return virtio_dev_rx(dev, queue_id, pkts, count);
653 virtio_net_with_host_offload(struct virtio_net *dev)
656 ((1ULL << VIRTIO_NET_F_CSUM) |
657 (1ULL << VIRTIO_NET_F_HOST_ECN) |
658 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
659 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
660 (1ULL << VIRTIO_NET_F_HOST_UFO)))
667 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
669 struct ipv4_hdr *ipv4_hdr;
670 struct ipv6_hdr *ipv6_hdr;
672 struct ether_hdr *eth_hdr;
675 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
677 m->l2_len = sizeof(struct ether_hdr);
678 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
680 if (ethertype == ETHER_TYPE_VLAN) {
681 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
683 m->l2_len += sizeof(struct vlan_hdr);
684 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
687 l3_hdr = (char *)eth_hdr + m->l2_len;
690 case ETHER_TYPE_IPv4:
692 *l4_proto = ipv4_hdr->next_proto_id;
693 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
694 *l4_hdr = (char *)l3_hdr + m->l3_len;
695 m->ol_flags |= PKT_TX_IPV4;
697 case ETHER_TYPE_IPv6:
699 *l4_proto = ipv6_hdr->proto;
700 m->l3_len = sizeof(struct ipv6_hdr);
701 *l4_hdr = (char *)l3_hdr + m->l3_len;
702 m->ol_flags |= PKT_TX_IPV6;
712 static __rte_always_inline void
713 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
715 uint16_t l4_proto = 0;
717 struct tcp_hdr *tcp_hdr = NULL;
719 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
722 parse_ethernet(m, &l4_proto, &l4_hdr);
723 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
724 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
725 switch (hdr->csum_offset) {
726 case (offsetof(struct tcp_hdr, cksum)):
727 if (l4_proto == IPPROTO_TCP)
728 m->ol_flags |= PKT_TX_TCP_CKSUM;
730 case (offsetof(struct udp_hdr, dgram_cksum)):
731 if (l4_proto == IPPROTO_UDP)
732 m->ol_flags |= PKT_TX_UDP_CKSUM;
734 case (offsetof(struct sctp_hdr, cksum)):
735 if (l4_proto == IPPROTO_SCTP)
736 m->ol_flags |= PKT_TX_SCTP_CKSUM;
744 if (l4_hdr && hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
745 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
746 case VIRTIO_NET_HDR_GSO_TCPV4:
747 case VIRTIO_NET_HDR_GSO_TCPV6:
749 m->ol_flags |= PKT_TX_TCP_SEG;
750 m->tso_segsz = hdr->gso_size;
751 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
753 case VIRTIO_NET_HDR_GSO_UDP:
754 m->ol_flags |= PKT_TX_UDP_SEG;
755 m->tso_segsz = hdr->gso_size;
756 m->l4_len = sizeof(struct udp_hdr);
759 RTE_LOG(WARNING, VHOST_DATA,
760 "unsupported gso type %u.\n", hdr->gso_type);
766 static __rte_always_inline void
767 put_zmbuf(struct zcopy_mbuf *zmbuf)
772 static __rte_always_inline int
773 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
774 struct buf_vector *buf_vec, uint16_t nr_vec,
775 struct rte_mbuf *m, struct rte_mempool *mbuf_pool)
777 uint32_t buf_avail, buf_offset;
778 uint64_t buf_addr, buf_iova, buf_len;
779 uint32_t mbuf_avail, mbuf_offset;
781 struct rte_mbuf *cur = m, *prev = m;
782 struct virtio_net_hdr tmp_hdr;
783 struct virtio_net_hdr *hdr = NULL;
784 /* A counter to avoid desc dead loop chain */
785 uint16_t vec_idx = 0;
786 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
789 buf_addr = buf_vec[vec_idx].buf_addr;
790 buf_iova = buf_vec[vec_idx].buf_iova;
791 buf_len = buf_vec[vec_idx].buf_len;
793 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
798 if (likely(nr_vec > 1))
799 rte_prefetch0((void *)(uintptr_t)buf_vec[1].buf_addr);
801 if (virtio_net_with_host_offload(dev)) {
802 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
804 uint64_t remain = sizeof(struct virtio_net_hdr);
806 uint64_t dst = (uint64_t)(uintptr_t)&tmp_hdr;
807 uint16_t hdr_vec_idx = 0;
810 * No luck, the virtio-net header doesn't fit
811 * in a contiguous virtual area.
815 src = buf_vec[hdr_vec_idx].buf_addr;
816 rte_memcpy((void *)(uintptr_t)dst,
817 (void *)(uintptr_t)src, len);
826 hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
832 * A virtio driver normally uses at least 2 desc buffers
833 * for Tx: the first for storing the header, and others
834 * for storing the data.
836 if (unlikely(buf_len < dev->vhost_hlen)) {
837 buf_offset = dev->vhost_hlen - buf_len;
839 buf_addr = buf_vec[vec_idx].buf_addr;
840 buf_iova = buf_vec[vec_idx].buf_iova;
841 buf_len = buf_vec[vec_idx].buf_len;
842 buf_avail = buf_len - buf_offset;
843 } else if (buf_len == dev->vhost_hlen) {
844 if (unlikely(++vec_idx >= nr_vec))
846 buf_addr = buf_vec[vec_idx].buf_addr;
847 buf_iova = buf_vec[vec_idx].buf_iova;
848 buf_len = buf_vec[vec_idx].buf_len;
853 buf_offset = dev->vhost_hlen;
854 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
857 rte_prefetch0((void *)(uintptr_t)
858 (buf_addr + buf_offset));
861 (uintptr_t)(buf_addr + buf_offset),
862 (uint32_t)buf_avail, 0);
865 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
869 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
872 * A desc buf might across two host physical pages that are
873 * not continuous. In such case (gpa_to_hpa returns 0), data
874 * will be copied even though zero copy is enabled.
876 if (unlikely(dev->dequeue_zero_copy && (hpa = gpa_to_hpa(dev,
877 buf_iova + buf_offset, cpy_len)))) {
878 cur->data_len = cpy_len;
881 (void *)(uintptr_t)(buf_addr + buf_offset);
885 * In zero copy mode, one mbuf can only reference data
886 * for one or partial of one desc buff.
888 mbuf_avail = cpy_len;
890 if (likely(cpy_len > MAX_BATCH_LEN ||
891 vq->batch_copy_nb_elems >= vq->size ||
892 (hdr && cur == m))) {
893 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
895 (void *)((uintptr_t)(buf_addr +
899 batch_copy[vq->batch_copy_nb_elems].dst =
900 rte_pktmbuf_mtod_offset(cur, void *,
902 batch_copy[vq->batch_copy_nb_elems].src =
903 (void *)((uintptr_t)(buf_addr +
905 batch_copy[vq->batch_copy_nb_elems].len =
907 vq->batch_copy_nb_elems++;
911 mbuf_avail -= cpy_len;
912 mbuf_offset += cpy_len;
913 buf_avail -= cpy_len;
914 buf_offset += cpy_len;
916 /* This buf reaches to its end, get the next one */
917 if (buf_avail == 0) {
918 if (++vec_idx >= nr_vec)
921 buf_addr = buf_vec[vec_idx].buf_addr;
922 buf_iova = buf_vec[vec_idx].buf_iova;
923 buf_len = buf_vec[vec_idx].buf_len;
926 * Prefecth desc n + 1 buffer while
927 * desc n buffer is processed.
929 if (vec_idx + 1 < nr_vec)
930 rte_prefetch0((void *)(uintptr_t)
931 buf_vec[vec_idx + 1].buf_addr);
936 PRINT_PACKET(dev, (uintptr_t)buf_addr,
937 (uint32_t)buf_avail, 0);
941 * This mbuf reaches to its end, get a new one
944 if (mbuf_avail == 0) {
945 cur = rte_pktmbuf_alloc(mbuf_pool);
946 if (unlikely(cur == NULL)) {
947 RTE_LOG(ERR, VHOST_DATA, "Failed to "
948 "allocate memory for mbuf.\n");
952 if (unlikely(dev->dequeue_zero_copy))
953 rte_mbuf_refcnt_update(cur, 1);
956 prev->data_len = mbuf_offset;
958 m->pkt_len += mbuf_offset;
962 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
966 prev->data_len = mbuf_offset;
967 m->pkt_len += mbuf_offset;
970 vhost_dequeue_offload(hdr, m);
977 static __rte_always_inline struct zcopy_mbuf *
978 get_zmbuf(struct vhost_virtqueue *vq)
984 /* search [last_zmbuf_idx, zmbuf_size) */
985 i = vq->last_zmbuf_idx;
986 last = vq->zmbuf_size;
989 for (; i < last; i++) {
990 if (vq->zmbufs[i].in_use == 0) {
991 vq->last_zmbuf_idx = i + 1;
992 vq->zmbufs[i].in_use = 1;
993 return &vq->zmbufs[i];
999 /* search [0, last_zmbuf_idx) */
1001 last = vq->last_zmbuf_idx;
1008 static __rte_always_inline bool
1009 mbuf_is_consumed(struct rte_mbuf *m)
1012 if (rte_mbuf_refcnt_read(m) > 1)
1020 static __rte_always_inline void
1021 restore_mbuf(struct rte_mbuf *m)
1023 uint32_t mbuf_size, priv_size;
1026 priv_size = rte_pktmbuf_priv_size(m->pool);
1027 mbuf_size = sizeof(struct rte_mbuf) + priv_size;
1028 /* start of buffer is after mbuf structure and priv data */
1030 m->buf_addr = (char *)m + mbuf_size;
1031 m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size;
1036 static __rte_always_inline uint16_t
1037 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1038 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1041 uint16_t free_entries;
1043 if (unlikely(dev->dequeue_zero_copy)) {
1044 struct zcopy_mbuf *zmbuf, *next;
1047 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1048 zmbuf != NULL; zmbuf = next) {
1049 next = TAILQ_NEXT(zmbuf, next);
1051 if (mbuf_is_consumed(zmbuf->mbuf)) {
1052 update_shadow_used_ring_split(vq,
1053 zmbuf->desc_idx, 0);
1056 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1057 restore_mbuf(zmbuf->mbuf);
1058 rte_pktmbuf_free(zmbuf->mbuf);
1064 flush_shadow_used_ring_split(dev, vq);
1065 vhost_vring_call(dev, vq);
1068 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1070 free_entries = *((volatile uint16_t *)&vq->avail->idx) -
1072 if (free_entries == 0)
1075 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1077 count = RTE_MIN(count, MAX_PKT_BURST);
1078 count = RTE_MIN(count, free_entries);
1079 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1082 for (i = 0; i < count; i++) {
1083 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1084 uint16_t head_idx, dummy_len;
1085 uint16_t nr_vec = 0;
1088 if (unlikely(fill_vec_buf_split(dev, vq,
1089 vq->last_avail_idx + i,
1091 &head_idx, &dummy_len,
1092 VHOST_ACCESS_RO) < 0))
1095 if (likely(dev->dequeue_zero_copy == 0))
1096 update_shadow_used_ring_split(vq, head_idx, 0);
1098 rte_prefetch0((void *)(uintptr_t)buf_vec[0].buf_addr);
1100 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
1101 if (unlikely(pkts[i] == NULL)) {
1102 RTE_LOG(ERR, VHOST_DATA,
1103 "Failed to allocate memory for mbuf.\n");
1107 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
1109 if (unlikely(err)) {
1110 rte_pktmbuf_free(pkts[i]);
1114 if (unlikely(dev->dequeue_zero_copy)) {
1115 struct zcopy_mbuf *zmbuf;
1117 zmbuf = get_zmbuf(vq);
1119 rte_pktmbuf_free(pkts[i]);
1122 zmbuf->mbuf = pkts[i];
1123 zmbuf->desc_idx = head_idx;
1126 * Pin lock the mbuf; we will check later to see
1127 * whether the mbuf is freed (when we are the last
1128 * user) or not. If that's the case, we then could
1129 * update the used ring safely.
1131 rte_mbuf_refcnt_update(pkts[i], 1);
1134 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1137 vq->last_avail_idx += i;
1139 if (likely(dev->dequeue_zero_copy == 0)) {
1140 do_data_copy_dequeue(vq);
1141 if (unlikely(i < count))
1142 vq->shadow_used_idx = i;
1143 flush_shadow_used_ring_split(dev, vq);
1144 vhost_vring_call(dev, vq);
1151 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
1152 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1154 struct virtio_net *dev;
1155 struct rte_mbuf *rarp_mbuf = NULL;
1156 struct vhost_virtqueue *vq;
1158 dev = get_device(vid);
1162 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1163 RTE_LOG(ERR, VHOST_DATA,
1164 "(%d) %s: built-in vhost net backend is disabled.\n",
1165 dev->vid, __func__);
1169 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
1170 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
1171 dev->vid, __func__, queue_id);
1175 vq = dev->virtqueue[queue_id];
1177 if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
1180 if (unlikely(vq->enabled == 0))
1181 goto out_access_unlock;
1183 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1184 vhost_user_iotlb_rd_lock(vq);
1186 if (unlikely(vq->access_ok == 0))
1187 if (unlikely(vring_translate(dev, vq) < 0))
1191 * Construct a RARP broadcast packet, and inject it to the "pkts"
1192 * array, to looks like that guest actually send such packet.
1194 * Check user_send_rarp() for more information.
1196 * broadcast_rarp shares a cacheline in the virtio_net structure
1197 * with some fields that are accessed during enqueue and
1198 * rte_atomic16_cmpset() causes a write if using cmpxchg. This could
1199 * result in false sharing between enqueue and dequeue.
1201 * Prevent unnecessary false sharing by reading broadcast_rarp first
1202 * and only performing cmpset if the read indicates it is likely to
1205 if (unlikely(rte_atomic16_read(&dev->broadcast_rarp) &&
1206 rte_atomic16_cmpset((volatile uint16_t *)
1207 &dev->broadcast_rarp.cnt, 1, 0))) {
1209 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
1210 if (rarp_mbuf == NULL) {
1211 RTE_LOG(ERR, VHOST_DATA,
1212 "Failed to make RARP packet.\n");
1218 count = virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count);
1221 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1222 vhost_user_iotlb_rd_unlock(vq);
1225 rte_spinlock_unlock(&vq->access_lock);
1227 if (unlikely(rarp_mbuf != NULL)) {
1229 * Inject it to the head of "pkts" array, so that switch's mac
1230 * learning table will get updated first.
1232 memmove(&pkts[1], pkts, count * sizeof(struct rte_mbuf *));
1233 pkts[0] = rarp_mbuf;