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 do_flush_shadow_used_ring_split(struct virtio_net *dev,
42 struct vhost_virtqueue *vq,
43 uint16_t to, uint16_t from, uint16_t size)
45 rte_memcpy(&vq->used->ring[to],
46 &vq->shadow_used_split[from],
47 size * sizeof(struct vring_used_elem));
48 vhost_log_cache_used_vring(dev, vq,
49 offsetof(struct vring_used, ring[to]),
50 size * sizeof(struct vring_used_elem));
53 static __rte_always_inline void
54 flush_shadow_used_ring_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
56 uint16_t used_idx = vq->last_used_idx & (vq->size - 1);
58 if (used_idx + vq->shadow_used_idx <= vq->size) {
59 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0,
64 /* update used ring interval [used_idx, vq->size] */
65 size = vq->size - used_idx;
66 do_flush_shadow_used_ring_split(dev, vq, used_idx, 0, size);
68 /* update the left half used ring interval [0, left_size] */
69 do_flush_shadow_used_ring_split(dev, vq, 0, size,
70 vq->shadow_used_idx - size);
72 vq->last_used_idx += vq->shadow_used_idx;
76 vhost_log_cache_sync(dev, vq);
78 *(volatile uint16_t *)&vq->used->idx += vq->shadow_used_idx;
79 vq->shadow_used_idx = 0;
80 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
81 sizeof(vq->used->idx));
84 static __rte_always_inline void
85 update_shadow_used_ring_split(struct vhost_virtqueue *vq,
86 uint16_t desc_idx, uint32_t len)
88 uint16_t i = vq->shadow_used_idx++;
90 vq->shadow_used_split[i].id = desc_idx;
91 vq->shadow_used_split[i].len = len;
94 static __rte_always_inline void
95 flush_shadow_used_ring_packed(struct virtio_net *dev,
96 struct vhost_virtqueue *vq)
99 uint16_t used_idx = vq->last_used_idx;
100 uint16_t head_idx = vq->last_used_idx;
101 uint16_t head_flags = 0;
103 /* Split loop in two to save memory barriers */
104 for (i = 0; i < vq->shadow_used_idx; i++) {
105 vq->desc_packed[used_idx].id = vq->shadow_used_packed[i].id;
106 vq->desc_packed[used_idx].len = vq->shadow_used_packed[i].len;
108 used_idx += vq->shadow_used_packed[i].count;
109 if (used_idx >= vq->size)
110 used_idx -= vq->size;
115 for (i = 0; i < vq->shadow_used_idx; i++) {
118 if (vq->shadow_used_packed[i].len)
119 flags = VRING_DESC_F_WRITE;
123 if (vq->used_wrap_counter) {
124 flags |= VRING_DESC_F_USED;
125 flags |= VRING_DESC_F_AVAIL;
127 flags &= ~VRING_DESC_F_USED;
128 flags &= ~VRING_DESC_F_AVAIL;
132 vq->desc_packed[vq->last_used_idx].flags = flags;
134 vhost_log_cache_used_vring(dev, vq,
136 sizeof(struct vring_packed_desc),
137 sizeof(struct vring_packed_desc));
139 head_idx = vq->last_used_idx;
143 vq->last_used_idx += vq->shadow_used_packed[i].count;
144 if (vq->last_used_idx >= vq->size) {
145 vq->used_wrap_counter ^= 1;
146 vq->last_used_idx -= vq->size;
150 vq->desc_packed[head_idx].flags = head_flags;
152 vhost_log_cache_used_vring(dev, vq,
154 sizeof(struct vring_packed_desc),
155 sizeof(struct vring_packed_desc));
157 vq->shadow_used_idx = 0;
158 vhost_log_cache_sync(dev, vq);
161 static __rte_always_inline void
162 update_shadow_used_ring_packed(struct vhost_virtqueue *vq,
163 uint16_t desc_idx, uint32_t len, uint16_t count)
165 uint16_t i = vq->shadow_used_idx++;
167 vq->shadow_used_packed[i].id = desc_idx;
168 vq->shadow_used_packed[i].len = len;
169 vq->shadow_used_packed[i].count = count;
173 do_data_copy_enqueue(struct virtio_net *dev, struct vhost_virtqueue *vq)
175 struct batch_copy_elem *elem = vq->batch_copy_elems;
176 uint16_t count = vq->batch_copy_nb_elems;
179 for (i = 0; i < count; i++) {
180 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
181 vhost_log_cache_write(dev, vq, elem[i].log_addr, elem[i].len);
182 PRINT_PACKET(dev, (uintptr_t)elem[i].dst, elem[i].len, 0);
185 vq->batch_copy_nb_elems = 0;
189 do_data_copy_dequeue(struct vhost_virtqueue *vq)
191 struct batch_copy_elem *elem = vq->batch_copy_elems;
192 uint16_t count = vq->batch_copy_nb_elems;
195 for (i = 0; i < count; i++)
196 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
198 vq->batch_copy_nb_elems = 0;
201 /* avoid write operation when necessary, to lessen cache issues */
202 #define ASSIGN_UNLESS_EQUAL(var, val) do { \
203 if ((var) != (val)) \
207 static __rte_always_inline void
208 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
210 uint64_t csum_l4 = m_buf->ol_flags & PKT_TX_L4_MASK;
212 if (m_buf->ol_flags & PKT_TX_TCP_SEG)
213 csum_l4 |= PKT_TX_TCP_CKSUM;
216 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
217 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
220 case PKT_TX_TCP_CKSUM:
221 net_hdr->csum_offset = (offsetof(struct rte_tcp_hdr,
224 case PKT_TX_UDP_CKSUM:
225 net_hdr->csum_offset = (offsetof(struct rte_udp_hdr,
228 case PKT_TX_SCTP_CKSUM:
229 net_hdr->csum_offset = (offsetof(struct rte_sctp_hdr,
234 ASSIGN_UNLESS_EQUAL(net_hdr->csum_start, 0);
235 ASSIGN_UNLESS_EQUAL(net_hdr->csum_offset, 0);
236 ASSIGN_UNLESS_EQUAL(net_hdr->flags, 0);
239 /* IP cksum verification cannot be bypassed, then calculate here */
240 if (m_buf->ol_flags & PKT_TX_IP_CKSUM) {
241 struct rte_ipv4_hdr *ipv4_hdr;
243 ipv4_hdr = rte_pktmbuf_mtod_offset(m_buf, struct rte_ipv4_hdr *,
245 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
248 if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
249 if (m_buf->ol_flags & PKT_TX_IPV4)
250 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
252 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
253 net_hdr->gso_size = m_buf->tso_segsz;
254 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
256 } else if (m_buf->ol_flags & PKT_TX_UDP_SEG) {
257 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
258 net_hdr->gso_size = m_buf->tso_segsz;
259 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len +
262 ASSIGN_UNLESS_EQUAL(net_hdr->gso_type, 0);
263 ASSIGN_UNLESS_EQUAL(net_hdr->gso_size, 0);
264 ASSIGN_UNLESS_EQUAL(net_hdr->hdr_len, 0);
268 static __rte_always_inline int
269 map_one_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
270 struct buf_vector *buf_vec, uint16_t *vec_idx,
271 uint64_t desc_iova, uint64_t desc_len, uint8_t perm)
273 uint16_t vec_id = *vec_idx;
277 uint64_t desc_chunck_len = desc_len;
279 if (unlikely(vec_id >= BUF_VECTOR_MAX))
282 desc_addr = vhost_iova_to_vva(dev, vq,
286 if (unlikely(!desc_addr))
289 rte_prefetch0((void *)(uintptr_t)desc_addr);
291 buf_vec[vec_id].buf_iova = desc_iova;
292 buf_vec[vec_id].buf_addr = desc_addr;
293 buf_vec[vec_id].buf_len = desc_chunck_len;
295 desc_len -= desc_chunck_len;
296 desc_iova += desc_chunck_len;
304 static __rte_always_inline int
305 fill_vec_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
306 uint32_t avail_idx, uint16_t *vec_idx,
307 struct buf_vector *buf_vec, uint16_t *desc_chain_head,
308 uint32_t *desc_chain_len, uint8_t perm)
310 uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
311 uint16_t vec_id = *vec_idx;
314 uint32_t nr_descs = vq->size;
316 struct vring_desc *descs = vq->desc;
317 struct vring_desc *idesc = NULL;
319 if (unlikely(idx >= vq->size))
322 *desc_chain_head = idx;
324 if (vq->desc[idx].flags & VRING_DESC_F_INDIRECT) {
325 dlen = vq->desc[idx].len;
326 nr_descs = dlen / sizeof(struct vring_desc);
327 if (unlikely(nr_descs > vq->size))
330 descs = (struct vring_desc *)(uintptr_t)
331 vhost_iova_to_vva(dev, vq, vq->desc[idx].addr,
334 if (unlikely(!descs))
337 if (unlikely(dlen < vq->desc[idx].len)) {
339 * The indirect desc table is not contiguous
340 * in process VA space, we have to copy it.
342 idesc = vhost_alloc_copy_ind_table(dev, vq,
343 vq->desc[idx].addr, vq->desc[idx].len);
344 if (unlikely(!idesc))
354 if (unlikely(idx >= nr_descs || cnt++ >= nr_descs)) {
355 free_ind_table(idesc);
359 len += descs[idx].len;
361 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
362 descs[idx].addr, descs[idx].len,
364 free_ind_table(idesc);
368 if ((descs[idx].flags & VRING_DESC_F_NEXT) == 0)
371 idx = descs[idx].next;
374 *desc_chain_len = len;
377 if (unlikely(!!idesc))
378 free_ind_table(idesc);
384 * Returns -1 on fail, 0 on success
387 reserve_avail_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
388 uint32_t size, struct buf_vector *buf_vec,
389 uint16_t *num_buffers, uint16_t avail_head,
393 uint16_t vec_idx = 0;
394 uint16_t max_tries, tries = 0;
396 uint16_t head_idx = 0;
400 cur_idx = vq->last_avail_idx;
402 if (rxvq_is_mergeable(dev))
403 max_tries = vq->size - 1;
408 if (unlikely(cur_idx == avail_head))
411 * if we tried all available ring items, and still
412 * can't get enough buf, it means something abnormal
415 if (unlikely(++tries > max_tries))
418 if (unlikely(fill_vec_buf_split(dev, vq, cur_idx,
421 VHOST_ACCESS_RW) < 0))
423 len = RTE_MIN(len, size);
424 update_shadow_used_ring_split(vq, head_idx, len);
436 static __rte_always_inline int
437 fill_vec_buf_packed_indirect(struct virtio_net *dev,
438 struct vhost_virtqueue *vq,
439 struct vring_packed_desc *desc, uint16_t *vec_idx,
440 struct buf_vector *buf_vec, uint32_t *len, uint8_t perm)
444 uint16_t vec_id = *vec_idx;
446 struct vring_packed_desc *descs, *idescs = NULL;
449 descs = (struct vring_packed_desc *)(uintptr_t)
450 vhost_iova_to_vva(dev, vq, desc->addr, &dlen, VHOST_ACCESS_RO);
451 if (unlikely(!descs))
454 if (unlikely(dlen < desc->len)) {
456 * The indirect desc table is not contiguous
457 * in process VA space, we have to copy it.
459 idescs = vhost_alloc_copy_ind_table(dev,
460 vq, desc->addr, desc->len);
461 if (unlikely(!idescs))
467 nr_descs = desc->len / sizeof(struct vring_packed_desc);
468 if (unlikely(nr_descs >= vq->size)) {
469 free_ind_table(idescs);
473 for (i = 0; i < nr_descs; i++) {
474 if (unlikely(vec_id >= BUF_VECTOR_MAX)) {
475 free_ind_table(idescs);
479 *len += descs[i].len;
480 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
481 descs[i].addr, descs[i].len,
487 if (unlikely(!!idescs))
488 free_ind_table(idescs);
493 static __rte_always_inline int
494 fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
495 uint16_t avail_idx, uint16_t *desc_count,
496 struct buf_vector *buf_vec, uint16_t *vec_idx,
497 uint16_t *buf_id, uint32_t *len, uint8_t perm)
499 bool wrap_counter = vq->avail_wrap_counter;
500 struct vring_packed_desc *descs = vq->desc_packed;
501 uint16_t vec_id = *vec_idx;
503 if (avail_idx < vq->last_avail_idx)
506 if (unlikely(!desc_is_avail(&descs[avail_idx], wrap_counter)))
510 * The ordering between desc flags and desc
511 * content reads need to be enforced.
519 if (unlikely(vec_id >= BUF_VECTOR_MAX))
522 if (unlikely(*desc_count >= vq->size))
526 *buf_id = descs[avail_idx].id;
528 if (descs[avail_idx].flags & VRING_DESC_F_INDIRECT) {
529 if (unlikely(fill_vec_buf_packed_indirect(dev, vq,
535 *len += descs[avail_idx].len;
537 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
538 descs[avail_idx].addr,
539 descs[avail_idx].len,
544 if ((descs[avail_idx].flags & VRING_DESC_F_NEXT) == 0)
547 if (++avail_idx >= vq->size) {
548 avail_idx -= vq->size;
559 * Returns -1 on fail, 0 on success
562 reserve_avail_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
563 uint32_t size, struct buf_vector *buf_vec,
564 uint16_t *nr_vec, uint16_t *num_buffers,
568 uint16_t vec_idx = 0;
569 uint16_t max_tries, tries = 0;
576 avail_idx = vq->last_avail_idx;
578 if (rxvq_is_mergeable(dev))
579 max_tries = vq->size - 1;
585 * if we tried all available ring items, and still
586 * can't get enough buf, it means something abnormal
589 if (unlikely(++tries > max_tries))
592 if (unlikely(fill_vec_buf_packed(dev, vq,
593 avail_idx, &desc_count,
596 VHOST_ACCESS_RW) < 0))
599 len = RTE_MIN(len, size);
600 update_shadow_used_ring_packed(vq, buf_id, len, desc_count);
603 avail_idx += desc_count;
604 if (avail_idx >= vq->size)
605 avail_idx -= vq->size;
607 *nr_descs += desc_count;
616 static __rte_noinline void
617 copy_vnet_hdr_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
618 struct buf_vector *buf_vec,
619 struct virtio_net_hdr_mrg_rxbuf *hdr)
622 uint64_t remain = dev->vhost_hlen;
623 uint64_t src = (uint64_t)(uintptr_t)hdr, dst;
624 uint64_t iova = buf_vec->buf_iova;
627 len = RTE_MIN(remain,
629 dst = buf_vec->buf_addr;
630 rte_memcpy((void *)(uintptr_t)dst,
631 (void *)(uintptr_t)src,
634 PRINT_PACKET(dev, (uintptr_t)dst,
636 vhost_log_cache_write(dev, vq,
646 static __rte_always_inline int
647 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
648 struct rte_mbuf *m, struct buf_vector *buf_vec,
649 uint16_t nr_vec, uint16_t num_buffers)
651 uint32_t vec_idx = 0;
652 uint32_t mbuf_offset, mbuf_avail;
653 uint32_t buf_offset, buf_avail;
654 uint64_t buf_addr, buf_iova, buf_len;
657 struct rte_mbuf *hdr_mbuf;
658 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
659 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
662 if (unlikely(m == NULL)) {
667 buf_addr = buf_vec[vec_idx].buf_addr;
668 buf_iova = buf_vec[vec_idx].buf_iova;
669 buf_len = buf_vec[vec_idx].buf_len;
671 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
678 if (unlikely(buf_len < dev->vhost_hlen))
681 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
683 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) RX: num merge buffers %d\n",
684 dev->vid, num_buffers);
686 if (unlikely(buf_len < dev->vhost_hlen)) {
687 buf_offset = dev->vhost_hlen - buf_len;
689 buf_addr = buf_vec[vec_idx].buf_addr;
690 buf_iova = buf_vec[vec_idx].buf_iova;
691 buf_len = buf_vec[vec_idx].buf_len;
692 buf_avail = buf_len - buf_offset;
694 buf_offset = dev->vhost_hlen;
695 buf_avail = buf_len - dev->vhost_hlen;
698 mbuf_avail = rte_pktmbuf_data_len(m);
700 while (mbuf_avail != 0 || m->next != NULL) {
701 /* done with current buf, get the next one */
702 if (buf_avail == 0) {
704 if (unlikely(vec_idx >= nr_vec)) {
709 buf_addr = buf_vec[vec_idx].buf_addr;
710 buf_iova = buf_vec[vec_idx].buf_iova;
711 buf_len = buf_vec[vec_idx].buf_len;
717 /* done with current mbuf, get the next one */
718 if (mbuf_avail == 0) {
722 mbuf_avail = rte_pktmbuf_data_len(m);
726 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
727 if (rxvq_is_mergeable(dev))
728 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
731 if (unlikely(hdr == &tmp_hdr)) {
732 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
734 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
736 vhost_log_cache_write(dev, vq,
744 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
746 if (likely(cpy_len > MAX_BATCH_LEN ||
747 vq->batch_copy_nb_elems >= vq->size)) {
748 rte_memcpy((void *)((uintptr_t)(buf_addr + buf_offset)),
749 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
751 vhost_log_cache_write(dev, vq, buf_iova + buf_offset,
753 PRINT_PACKET(dev, (uintptr_t)(buf_addr + buf_offset),
756 batch_copy[vq->batch_copy_nb_elems].dst =
757 (void *)((uintptr_t)(buf_addr + buf_offset));
758 batch_copy[vq->batch_copy_nb_elems].src =
759 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
760 batch_copy[vq->batch_copy_nb_elems].log_addr =
761 buf_iova + buf_offset;
762 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
763 vq->batch_copy_nb_elems++;
766 mbuf_avail -= cpy_len;
767 mbuf_offset += cpy_len;
768 buf_avail -= cpy_len;
769 buf_offset += cpy_len;
777 static __rte_noinline uint32_t
778 virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
779 struct rte_mbuf **pkts, uint32_t count)
781 uint32_t pkt_idx = 0;
782 uint16_t num_buffers;
783 struct buf_vector buf_vec[BUF_VECTOR_MAX];
786 avail_head = *((volatile uint16_t *)&vq->avail->idx);
789 * The ordering between avail index and
790 * desc reads needs to be enforced.
794 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
796 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
797 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
800 if (unlikely(reserve_avail_buf_split(dev, vq,
801 pkt_len, buf_vec, &num_buffers,
802 avail_head, &nr_vec) < 0)) {
803 VHOST_LOG_DEBUG(VHOST_DATA,
804 "(%d) failed to get enough desc from vring\n",
806 vq->shadow_used_idx -= num_buffers;
810 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
811 dev->vid, vq->last_avail_idx,
812 vq->last_avail_idx + num_buffers);
814 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
817 vq->shadow_used_idx -= num_buffers;
821 vq->last_avail_idx += num_buffers;
824 do_data_copy_enqueue(dev, vq);
826 if (likely(vq->shadow_used_idx)) {
827 flush_shadow_used_ring_split(dev, vq);
828 vhost_vring_call_split(dev, vq);
834 static __rte_noinline uint32_t
835 virtio_dev_rx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
836 struct rte_mbuf **pkts, uint32_t count)
838 uint32_t pkt_idx = 0;
839 uint16_t num_buffers;
840 struct buf_vector buf_vec[BUF_VECTOR_MAX];
842 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
843 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
845 uint16_t nr_descs = 0;
847 if (unlikely(reserve_avail_buf_packed(dev, vq,
848 pkt_len, buf_vec, &nr_vec,
849 &num_buffers, &nr_descs) < 0)) {
850 VHOST_LOG_DEBUG(VHOST_DATA,
851 "(%d) failed to get enough desc from vring\n",
853 vq->shadow_used_idx -= num_buffers;
857 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
858 dev->vid, vq->last_avail_idx,
859 vq->last_avail_idx + num_buffers);
861 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
864 vq->shadow_used_idx -= num_buffers;
868 vq->last_avail_idx += nr_descs;
869 if (vq->last_avail_idx >= vq->size) {
870 vq->last_avail_idx -= vq->size;
871 vq->avail_wrap_counter ^= 1;
875 do_data_copy_enqueue(dev, vq);
877 if (likely(vq->shadow_used_idx)) {
878 flush_shadow_used_ring_packed(dev, vq);
879 vhost_vring_call_packed(dev, vq);
885 static __rte_always_inline uint32_t
886 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
887 struct rte_mbuf **pkts, uint32_t count)
889 struct vhost_virtqueue *vq;
892 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
893 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
894 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
895 dev->vid, __func__, queue_id);
899 vq = dev->virtqueue[queue_id];
901 rte_spinlock_lock(&vq->access_lock);
903 if (unlikely(vq->enabled == 0))
904 goto out_access_unlock;
906 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
907 vhost_user_iotlb_rd_lock(vq);
909 if (unlikely(vq->access_ok == 0))
910 if (unlikely(vring_translate(dev, vq) < 0))
913 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
917 if (vq_is_packed(dev))
918 nb_tx = virtio_dev_rx_packed(dev, vq, pkts, count);
920 nb_tx = virtio_dev_rx_split(dev, vq, pkts, count);
923 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
924 vhost_user_iotlb_rd_unlock(vq);
927 rte_spinlock_unlock(&vq->access_lock);
933 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
934 struct rte_mbuf **pkts, uint16_t count)
936 struct virtio_net *dev = get_device(vid);
941 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
942 RTE_LOG(ERR, VHOST_DATA,
943 "(%d) %s: built-in vhost net backend is disabled.\n",
948 return virtio_dev_rx(dev, queue_id, pkts, count);
952 virtio_net_with_host_offload(struct virtio_net *dev)
955 ((1ULL << VIRTIO_NET_F_CSUM) |
956 (1ULL << VIRTIO_NET_F_HOST_ECN) |
957 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
958 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
959 (1ULL << VIRTIO_NET_F_HOST_UFO)))
966 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
968 struct rte_ipv4_hdr *ipv4_hdr;
969 struct rte_ipv6_hdr *ipv6_hdr;
971 struct rte_ether_hdr *eth_hdr;
974 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
976 m->l2_len = sizeof(struct rte_ether_hdr);
977 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
979 if (ethertype == RTE_ETHER_TYPE_VLAN) {
980 struct rte_vlan_hdr *vlan_hdr =
981 (struct rte_vlan_hdr *)(eth_hdr + 1);
983 m->l2_len += sizeof(struct rte_vlan_hdr);
984 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
987 l3_hdr = (char *)eth_hdr + m->l2_len;
990 case RTE_ETHER_TYPE_IPV4:
992 *l4_proto = ipv4_hdr->next_proto_id;
993 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
994 *l4_hdr = (char *)l3_hdr + m->l3_len;
995 m->ol_flags |= PKT_TX_IPV4;
997 case RTE_ETHER_TYPE_IPV6:
999 *l4_proto = ipv6_hdr->proto;
1000 m->l3_len = sizeof(struct rte_ipv6_hdr);
1001 *l4_hdr = (char *)l3_hdr + m->l3_len;
1002 m->ol_flags |= PKT_TX_IPV6;
1012 static __rte_always_inline void
1013 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
1015 uint16_t l4_proto = 0;
1016 void *l4_hdr = NULL;
1017 struct rte_tcp_hdr *tcp_hdr = NULL;
1019 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
1022 parse_ethernet(m, &l4_proto, &l4_hdr);
1023 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1024 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
1025 switch (hdr->csum_offset) {
1026 case (offsetof(struct rte_tcp_hdr, cksum)):
1027 if (l4_proto == IPPROTO_TCP)
1028 m->ol_flags |= PKT_TX_TCP_CKSUM;
1030 case (offsetof(struct rte_udp_hdr, dgram_cksum)):
1031 if (l4_proto == IPPROTO_UDP)
1032 m->ol_flags |= PKT_TX_UDP_CKSUM;
1034 case (offsetof(struct rte_sctp_hdr, cksum)):
1035 if (l4_proto == IPPROTO_SCTP)
1036 m->ol_flags |= PKT_TX_SCTP_CKSUM;
1044 if (l4_hdr && hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1045 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1046 case VIRTIO_NET_HDR_GSO_TCPV4:
1047 case VIRTIO_NET_HDR_GSO_TCPV6:
1049 m->ol_flags |= PKT_TX_TCP_SEG;
1050 m->tso_segsz = hdr->gso_size;
1051 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
1053 case VIRTIO_NET_HDR_GSO_UDP:
1054 m->ol_flags |= PKT_TX_UDP_SEG;
1055 m->tso_segsz = hdr->gso_size;
1056 m->l4_len = sizeof(struct rte_udp_hdr);
1059 RTE_LOG(WARNING, VHOST_DATA,
1060 "unsupported gso type %u.\n", hdr->gso_type);
1066 static __rte_noinline void
1067 copy_vnet_hdr_from_desc(struct virtio_net_hdr *hdr,
1068 struct buf_vector *buf_vec)
1071 uint64_t remain = sizeof(struct virtio_net_hdr);
1073 uint64_t dst = (uint64_t)(uintptr_t)hdr;
1076 len = RTE_MIN(remain, buf_vec->buf_len);
1077 src = buf_vec->buf_addr;
1078 rte_memcpy((void *)(uintptr_t)dst,
1079 (void *)(uintptr_t)src, len);
1087 static __rte_always_inline int
1088 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
1089 struct buf_vector *buf_vec, uint16_t nr_vec,
1090 struct rte_mbuf *m, struct rte_mempool *mbuf_pool)
1092 uint32_t buf_avail, buf_offset;
1093 uint64_t buf_addr, buf_iova, buf_len;
1094 uint32_t mbuf_avail, mbuf_offset;
1096 struct rte_mbuf *cur = m, *prev = m;
1097 struct virtio_net_hdr tmp_hdr;
1098 struct virtio_net_hdr *hdr = NULL;
1099 /* A counter to avoid desc dead loop chain */
1100 uint16_t vec_idx = 0;
1101 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
1104 buf_addr = buf_vec[vec_idx].buf_addr;
1105 buf_iova = buf_vec[vec_idx].buf_iova;
1106 buf_len = buf_vec[vec_idx].buf_len;
1108 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
1113 if (virtio_net_with_host_offload(dev)) {
1114 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
1116 * No luck, the virtio-net header doesn't fit
1117 * in a contiguous virtual area.
1119 copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
1122 hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
1127 * A virtio driver normally uses at least 2 desc buffers
1128 * for Tx: the first for storing the header, and others
1129 * for storing the data.
1131 if (unlikely(buf_len < dev->vhost_hlen)) {
1132 buf_offset = dev->vhost_hlen - buf_len;
1134 buf_addr = buf_vec[vec_idx].buf_addr;
1135 buf_iova = buf_vec[vec_idx].buf_iova;
1136 buf_len = buf_vec[vec_idx].buf_len;
1137 buf_avail = buf_len - buf_offset;
1138 } else if (buf_len == dev->vhost_hlen) {
1139 if (unlikely(++vec_idx >= nr_vec))
1141 buf_addr = buf_vec[vec_idx].buf_addr;
1142 buf_iova = buf_vec[vec_idx].buf_iova;
1143 buf_len = buf_vec[vec_idx].buf_len;
1146 buf_avail = buf_len;
1148 buf_offset = dev->vhost_hlen;
1149 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
1153 (uintptr_t)(buf_addr + buf_offset),
1154 (uint32_t)buf_avail, 0);
1157 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
1161 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1164 * A desc buf might across two host physical pages that are
1165 * not continuous. In such case (gpa_to_hpa returns 0), data
1166 * will be copied even though zero copy is enabled.
1168 if (unlikely(dev->dequeue_zero_copy && (hpa = gpa_to_hpa(dev,
1169 buf_iova + buf_offset, cpy_len)))) {
1170 cur->data_len = cpy_len;
1173 (void *)(uintptr_t)(buf_addr + buf_offset);
1174 cur->buf_iova = hpa;
1177 * In zero copy mode, one mbuf can only reference data
1178 * for one or partial of one desc buff.
1180 mbuf_avail = cpy_len;
1182 if (likely(cpy_len > MAX_BATCH_LEN ||
1183 vq->batch_copy_nb_elems >= vq->size ||
1184 (hdr && cur == m))) {
1185 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
1187 (void *)((uintptr_t)(buf_addr +
1191 batch_copy[vq->batch_copy_nb_elems].dst =
1192 rte_pktmbuf_mtod_offset(cur, void *,
1194 batch_copy[vq->batch_copy_nb_elems].src =
1195 (void *)((uintptr_t)(buf_addr +
1197 batch_copy[vq->batch_copy_nb_elems].len =
1199 vq->batch_copy_nb_elems++;
1203 mbuf_avail -= cpy_len;
1204 mbuf_offset += cpy_len;
1205 buf_avail -= cpy_len;
1206 buf_offset += cpy_len;
1208 /* This buf reaches to its end, get the next one */
1209 if (buf_avail == 0) {
1210 if (++vec_idx >= nr_vec)
1213 buf_addr = buf_vec[vec_idx].buf_addr;
1214 buf_iova = buf_vec[vec_idx].buf_iova;
1215 buf_len = buf_vec[vec_idx].buf_len;
1218 buf_avail = buf_len;
1220 PRINT_PACKET(dev, (uintptr_t)buf_addr,
1221 (uint32_t)buf_avail, 0);
1225 * This mbuf reaches to its end, get a new one
1226 * to hold more data.
1228 if (mbuf_avail == 0) {
1229 cur = rte_pktmbuf_alloc(mbuf_pool);
1230 if (unlikely(cur == NULL)) {
1231 RTE_LOG(ERR, VHOST_DATA, "Failed to "
1232 "allocate memory for mbuf.\n");
1236 if (unlikely(dev->dequeue_zero_copy))
1237 rte_mbuf_refcnt_update(cur, 1);
1240 prev->data_len = mbuf_offset;
1242 m->pkt_len += mbuf_offset;
1246 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
1250 prev->data_len = mbuf_offset;
1251 m->pkt_len += mbuf_offset;
1254 vhost_dequeue_offload(hdr, m);
1261 static __rte_always_inline struct zcopy_mbuf *
1262 get_zmbuf(struct vhost_virtqueue *vq)
1268 /* search [last_zmbuf_idx, zmbuf_size) */
1269 i = vq->last_zmbuf_idx;
1270 last = vq->zmbuf_size;
1273 for (; i < last; i++) {
1274 if (vq->zmbufs[i].in_use == 0) {
1275 vq->last_zmbuf_idx = i + 1;
1276 vq->zmbufs[i].in_use = 1;
1277 return &vq->zmbufs[i];
1283 /* search [0, last_zmbuf_idx) */
1285 last = vq->last_zmbuf_idx;
1292 static __rte_noinline uint16_t
1293 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1294 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1297 uint16_t free_entries;
1299 if (unlikely(dev->dequeue_zero_copy)) {
1300 struct zcopy_mbuf *zmbuf, *next;
1302 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1303 zmbuf != NULL; zmbuf = next) {
1304 next = TAILQ_NEXT(zmbuf, next);
1306 if (mbuf_is_consumed(zmbuf->mbuf)) {
1307 update_shadow_used_ring_split(vq,
1308 zmbuf->desc_idx, 0);
1309 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1310 restore_mbuf(zmbuf->mbuf);
1311 rte_pktmbuf_free(zmbuf->mbuf);
1317 if (likely(vq->shadow_used_idx)) {
1318 flush_shadow_used_ring_split(dev, vq);
1319 vhost_vring_call_split(dev, vq);
1323 free_entries = *((volatile uint16_t *)&vq->avail->idx) -
1325 if (free_entries == 0)
1329 * The ordering between avail index and
1330 * desc reads needs to be enforced.
1334 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1336 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1338 count = RTE_MIN(count, MAX_PKT_BURST);
1339 count = RTE_MIN(count, free_entries);
1340 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1343 for (i = 0; i < count; i++) {
1344 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1347 uint16_t nr_vec = 0;
1350 if (unlikely(fill_vec_buf_split(dev, vq,
1351 vq->last_avail_idx + i,
1353 &head_idx, &dummy_len,
1354 VHOST_ACCESS_RO) < 0))
1357 if (likely(dev->dequeue_zero_copy == 0))
1358 update_shadow_used_ring_split(vq, head_idx, 0);
1360 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
1361 if (unlikely(pkts[i] == NULL)) {
1362 RTE_LOG(ERR, VHOST_DATA,
1363 "Failed to allocate memory for mbuf.\n");
1367 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
1369 if (unlikely(err)) {
1370 rte_pktmbuf_free(pkts[i]);
1374 if (unlikely(dev->dequeue_zero_copy)) {
1375 struct zcopy_mbuf *zmbuf;
1377 zmbuf = get_zmbuf(vq);
1379 rte_pktmbuf_free(pkts[i]);
1382 zmbuf->mbuf = pkts[i];
1383 zmbuf->desc_idx = head_idx;
1386 * Pin lock the mbuf; we will check later to see
1387 * whether the mbuf is freed (when we are the last
1388 * user) or not. If that's the case, we then could
1389 * update the used ring safely.
1391 rte_mbuf_refcnt_update(pkts[i], 1);
1394 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1397 vq->last_avail_idx += i;
1399 if (likely(dev->dequeue_zero_copy == 0)) {
1400 do_data_copy_dequeue(vq);
1401 if (unlikely(i < count))
1402 vq->shadow_used_idx = i;
1403 if (likely(vq->shadow_used_idx)) {
1404 flush_shadow_used_ring_split(dev, vq);
1405 vhost_vring_call_split(dev, vq);
1412 static __rte_noinline uint16_t
1413 virtio_dev_tx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
1414 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1418 if (unlikely(dev->dequeue_zero_copy)) {
1419 struct zcopy_mbuf *zmbuf, *next;
1421 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1422 zmbuf != NULL; zmbuf = next) {
1423 next = TAILQ_NEXT(zmbuf, next);
1425 if (mbuf_is_consumed(zmbuf->mbuf)) {
1426 update_shadow_used_ring_packed(vq,
1431 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1432 restore_mbuf(zmbuf->mbuf);
1433 rte_pktmbuf_free(zmbuf->mbuf);
1439 if (likely(vq->shadow_used_idx)) {
1440 flush_shadow_used_ring_packed(dev, vq);
1441 vhost_vring_call_packed(dev, vq);
1445 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1447 count = RTE_MIN(count, MAX_PKT_BURST);
1448 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1451 for (i = 0; i < count; i++) {
1452 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1455 uint16_t desc_count, nr_vec = 0;
1458 if (unlikely(fill_vec_buf_packed(dev, vq,
1459 vq->last_avail_idx, &desc_count,
1461 &buf_id, &dummy_len,
1462 VHOST_ACCESS_RO) < 0))
1465 if (likely(dev->dequeue_zero_copy == 0))
1466 update_shadow_used_ring_packed(vq, buf_id, 0,
1469 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
1470 if (unlikely(pkts[i] == NULL)) {
1471 RTE_LOG(ERR, VHOST_DATA,
1472 "Failed to allocate memory for mbuf.\n");
1476 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
1478 if (unlikely(err)) {
1479 rte_pktmbuf_free(pkts[i]);
1483 if (unlikely(dev->dequeue_zero_copy)) {
1484 struct zcopy_mbuf *zmbuf;
1486 zmbuf = get_zmbuf(vq);
1488 rte_pktmbuf_free(pkts[i]);
1491 zmbuf->mbuf = pkts[i];
1492 zmbuf->desc_idx = buf_id;
1493 zmbuf->desc_count = desc_count;
1496 * Pin lock the mbuf; we will check later to see
1497 * whether the mbuf is freed (when we are the last
1498 * user) or not. If that's the case, we then could
1499 * update the used ring safely.
1501 rte_mbuf_refcnt_update(pkts[i], 1);
1504 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1507 vq->last_avail_idx += desc_count;
1508 if (vq->last_avail_idx >= vq->size) {
1509 vq->last_avail_idx -= vq->size;
1510 vq->avail_wrap_counter ^= 1;
1514 if (likely(dev->dequeue_zero_copy == 0)) {
1515 do_data_copy_dequeue(vq);
1516 if (unlikely(i < count))
1517 vq->shadow_used_idx = i;
1518 if (likely(vq->shadow_used_idx)) {
1519 flush_shadow_used_ring_packed(dev, vq);
1520 vhost_vring_call_packed(dev, vq);
1528 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
1529 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1531 struct virtio_net *dev;
1532 struct rte_mbuf *rarp_mbuf = NULL;
1533 struct vhost_virtqueue *vq;
1535 dev = get_device(vid);
1539 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1540 RTE_LOG(ERR, VHOST_DATA,
1541 "(%d) %s: built-in vhost net backend is disabled.\n",
1542 dev->vid, __func__);
1546 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
1547 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
1548 dev->vid, __func__, queue_id);
1552 vq = dev->virtqueue[queue_id];
1554 if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
1557 if (unlikely(vq->enabled == 0)) {
1559 goto out_access_unlock;
1562 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1563 vhost_user_iotlb_rd_lock(vq);
1565 if (unlikely(vq->access_ok == 0))
1566 if (unlikely(vring_translate(dev, vq) < 0)) {
1572 * Construct a RARP broadcast packet, and inject it to the "pkts"
1573 * array, to looks like that guest actually send such packet.
1575 * Check user_send_rarp() for more information.
1577 * broadcast_rarp shares a cacheline in the virtio_net structure
1578 * with some fields that are accessed during enqueue and
1579 * rte_atomic16_cmpset() causes a write if using cmpxchg. This could
1580 * result in false sharing between enqueue and dequeue.
1582 * Prevent unnecessary false sharing by reading broadcast_rarp first
1583 * and only performing cmpset if the read indicates it is likely to
1586 if (unlikely(rte_atomic16_read(&dev->broadcast_rarp) &&
1587 rte_atomic16_cmpset((volatile uint16_t *)
1588 &dev->broadcast_rarp.cnt, 1, 0))) {
1590 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
1591 if (rarp_mbuf == NULL) {
1592 RTE_LOG(ERR, VHOST_DATA,
1593 "Failed to make RARP packet.\n");
1600 if (vq_is_packed(dev))
1601 count = virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count);
1603 count = virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count);
1606 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1607 vhost_user_iotlb_rd_unlock(vq);
1610 rte_spinlock_unlock(&vq->access_lock);
1612 if (unlikely(rarp_mbuf != NULL)) {
1614 * Inject it to the head of "pkts" array, so that switch's mac
1615 * learning table will get updated first.
1617 memmove(&pkts[1], pkts, count * sizeof(struct rte_mbuf *));
1618 pkts[0] = rarp_mbuf;