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_split[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, uint32_t len)
127 uint16_t i = vq->shadow_used_idx++;
129 vq->shadow_used_split[i].id = desc_idx;
130 vq->shadow_used_split[i].len = len;
133 static __rte_always_inline void
134 flush_shadow_used_ring_packed(struct virtio_net *dev,
135 struct vhost_virtqueue *vq)
138 uint16_t used_idx = vq->last_used_idx;
140 /* Split loop in two to save memory barriers */
141 for (i = 0; i < vq->shadow_used_idx; i++) {
142 vq->desc_packed[used_idx].id = vq->shadow_used_packed[i].id;
143 vq->desc_packed[used_idx].len = vq->shadow_used_packed[i].len;
145 used_idx += vq->shadow_used_packed[i].count;
146 if (used_idx >= vq->size)
147 used_idx -= vq->size;
152 for (i = 0; i < vq->shadow_used_idx; i++) {
155 if (vq->shadow_used_packed[i].len)
156 flags = VRING_DESC_F_WRITE;
160 if (vq->used_wrap_counter) {
161 flags |= VRING_DESC_F_USED;
162 flags |= VRING_DESC_F_AVAIL;
164 flags &= ~VRING_DESC_F_USED;
165 flags &= ~VRING_DESC_F_AVAIL;
168 vq->desc_packed[vq->last_used_idx].flags = flags;
170 vhost_log_cache_used_vring(dev, vq,
172 sizeof(struct vring_packed_desc),
173 sizeof(struct vring_packed_desc));
175 vq->last_used_idx += vq->shadow_used_packed[i].count;
176 if (vq->last_used_idx >= vq->size) {
177 vq->used_wrap_counter ^= 1;
178 vq->last_used_idx -= vq->size;
183 vq->shadow_used_idx = 0;
184 vhost_log_cache_sync(dev, vq);
187 static __rte_always_inline void
188 update_shadow_used_ring_packed(struct vhost_virtqueue *vq,
189 uint16_t desc_idx, uint32_t len, uint16_t count)
191 uint16_t i = vq->shadow_used_idx++;
193 vq->shadow_used_packed[i].id = desc_idx;
194 vq->shadow_used_packed[i].len = len;
195 vq->shadow_used_packed[i].count = count;
199 do_data_copy_enqueue(struct virtio_net *dev, struct vhost_virtqueue *vq)
201 struct batch_copy_elem *elem = vq->batch_copy_elems;
202 uint16_t count = vq->batch_copy_nb_elems;
205 for (i = 0; i < count; i++) {
206 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
207 vhost_log_cache_write(dev, vq, elem[i].log_addr, elem[i].len);
208 PRINT_PACKET(dev, (uintptr_t)elem[i].dst, elem[i].len, 0);
211 vq->batch_copy_nb_elems = 0;
215 do_data_copy_dequeue(struct vhost_virtqueue *vq)
217 struct batch_copy_elem *elem = vq->batch_copy_elems;
218 uint16_t count = vq->batch_copy_nb_elems;
221 for (i = 0; i < count; i++)
222 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
224 vq->batch_copy_nb_elems = 0;
227 /* avoid write operation when necessary, to lessen cache issues */
228 #define ASSIGN_UNLESS_EQUAL(var, val) do { \
229 if ((var) != (val)) \
233 static __rte_always_inline void
234 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
236 uint64_t csum_l4 = m_buf->ol_flags & PKT_TX_L4_MASK;
238 if (m_buf->ol_flags & PKT_TX_TCP_SEG)
239 csum_l4 |= PKT_TX_TCP_CKSUM;
242 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
243 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
246 case PKT_TX_TCP_CKSUM:
247 net_hdr->csum_offset = (offsetof(struct tcp_hdr,
250 case PKT_TX_UDP_CKSUM:
251 net_hdr->csum_offset = (offsetof(struct udp_hdr,
254 case PKT_TX_SCTP_CKSUM:
255 net_hdr->csum_offset = (offsetof(struct sctp_hdr,
260 ASSIGN_UNLESS_EQUAL(net_hdr->csum_start, 0);
261 ASSIGN_UNLESS_EQUAL(net_hdr->csum_offset, 0);
262 ASSIGN_UNLESS_EQUAL(net_hdr->flags, 0);
265 /* IP cksum verification cannot be bypassed, then calculate here */
266 if (m_buf->ol_flags & PKT_TX_IP_CKSUM) {
267 struct ipv4_hdr *ipv4_hdr;
269 ipv4_hdr = rte_pktmbuf_mtod_offset(m_buf, struct ipv4_hdr *,
271 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
274 if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
275 if (m_buf->ol_flags & PKT_TX_IPV4)
276 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
278 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
279 net_hdr->gso_size = m_buf->tso_segsz;
280 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
282 } else if (m_buf->ol_flags & PKT_TX_UDP_SEG) {
283 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
284 net_hdr->gso_size = m_buf->tso_segsz;
285 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len +
288 ASSIGN_UNLESS_EQUAL(net_hdr->gso_type, 0);
289 ASSIGN_UNLESS_EQUAL(net_hdr->gso_size, 0);
290 ASSIGN_UNLESS_EQUAL(net_hdr->hdr_len, 0);
294 static __rte_always_inline int
295 map_one_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
296 struct buf_vector *buf_vec, uint16_t *vec_idx,
297 uint64_t desc_iova, uint64_t desc_len, uint8_t perm)
299 uint16_t vec_id = *vec_idx;
303 uint64_t desc_chunck_len = desc_len;
305 if (unlikely(vec_id >= BUF_VECTOR_MAX))
308 desc_addr = vhost_iova_to_vva(dev, vq,
312 if (unlikely(!desc_addr))
315 buf_vec[vec_id].buf_iova = desc_iova;
316 buf_vec[vec_id].buf_addr = desc_addr;
317 buf_vec[vec_id].buf_len = desc_chunck_len;
319 desc_len -= desc_chunck_len;
320 desc_iova += desc_chunck_len;
328 static __rte_always_inline int
329 fill_vec_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
330 uint32_t avail_idx, uint16_t *vec_idx,
331 struct buf_vector *buf_vec, uint16_t *desc_chain_head,
332 uint32_t *desc_chain_len, uint8_t perm)
334 uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
335 uint16_t vec_id = *vec_idx;
338 uint32_t nr_descs = vq->size;
340 struct vring_desc *descs = vq->desc;
341 struct vring_desc *idesc = NULL;
343 if (unlikely(idx >= vq->size))
346 *desc_chain_head = idx;
348 if (vq->desc[idx].flags & VRING_DESC_F_INDIRECT) {
349 dlen = vq->desc[idx].len;
350 nr_descs = dlen / sizeof(struct vring_desc);
351 if (unlikely(nr_descs > vq->size))
354 descs = (struct vring_desc *)(uintptr_t)
355 vhost_iova_to_vva(dev, vq, vq->desc[idx].addr,
358 if (unlikely(!descs))
361 if (unlikely(dlen < vq->desc[idx].len)) {
363 * The indirect desc table is not contiguous
364 * in process VA space, we have to copy it.
366 idesc = alloc_copy_ind_table(dev, vq,
367 vq->desc[idx].addr, vq->desc[idx].len);
368 if (unlikely(!idesc))
378 if (unlikely(idx >= nr_descs || cnt++ >= nr_descs)) {
379 free_ind_table(idesc);
383 len += descs[idx].len;
385 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
386 descs[idx].addr, descs[idx].len,
388 free_ind_table(idesc);
392 if ((descs[idx].flags & VRING_DESC_F_NEXT) == 0)
395 idx = descs[idx].next;
398 *desc_chain_len = len;
401 if (unlikely(!!idesc))
402 free_ind_table(idesc);
408 * Returns -1 on fail, 0 on success
411 reserve_avail_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
412 uint32_t size, struct buf_vector *buf_vec,
413 uint16_t *num_buffers, uint16_t avail_head,
417 uint16_t vec_idx = 0;
418 uint16_t max_tries, tries = 0;
420 uint16_t head_idx = 0;
424 cur_idx = vq->last_avail_idx;
426 if (rxvq_is_mergeable(dev))
427 max_tries = vq->size - 1;
432 if (unlikely(cur_idx == avail_head))
435 * if we tried all available ring items, and still
436 * can't get enough buf, it means something abnormal
439 if (unlikely(++tries > max_tries))
442 if (unlikely(fill_vec_buf_split(dev, vq, cur_idx,
445 VHOST_ACCESS_RW) < 0))
447 len = RTE_MIN(len, size);
448 update_shadow_used_ring_split(vq, head_idx, len);
460 static __rte_always_inline int
461 fill_vec_buf_packed_indirect(struct virtio_net *dev,
462 struct vhost_virtqueue *vq,
463 struct vring_packed_desc *desc, uint16_t *vec_idx,
464 struct buf_vector *buf_vec, uint32_t *len, uint8_t perm)
468 uint16_t vec_id = *vec_idx;
470 struct vring_packed_desc *descs, *idescs = NULL;
473 descs = (struct vring_packed_desc *)(uintptr_t)
474 vhost_iova_to_vva(dev, vq, desc->addr, &dlen, VHOST_ACCESS_RO);
475 if (unlikely(!descs))
478 if (unlikely(dlen < desc->len)) {
480 * The indirect desc table is not contiguous
481 * in process VA space, we have to copy it.
483 idescs = alloc_copy_ind_table(dev, vq, desc->addr, desc->len);
484 if (unlikely(!idescs))
490 nr_descs = desc->len / sizeof(struct vring_packed_desc);
491 if (unlikely(nr_descs >= vq->size)) {
492 free_ind_table(idescs);
496 for (i = 0; i < nr_descs; i++) {
497 if (unlikely(vec_id >= BUF_VECTOR_MAX)) {
498 free_ind_table(idescs);
502 *len += descs[i].len;
503 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
504 descs[i].addr, descs[i].len,
510 if (unlikely(!!idescs))
511 free_ind_table(idescs);
516 static __rte_always_inline int
517 fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
518 uint16_t avail_idx, uint16_t *desc_count,
519 struct buf_vector *buf_vec, uint16_t *vec_idx,
520 uint16_t *buf_id, uint32_t *len, uint8_t perm)
522 bool wrap_counter = vq->avail_wrap_counter;
523 struct vring_packed_desc *descs = vq->desc_packed;
524 uint16_t vec_id = *vec_idx;
526 if (avail_idx < vq->last_avail_idx)
529 if (unlikely(!desc_is_avail(&descs[avail_idx], wrap_counter)))
533 * The ordering between desc flags and desc
534 * content reads need to be enforced.
542 if (unlikely(vec_id >= BUF_VECTOR_MAX))
545 if (unlikely(*desc_count >= vq->size))
549 *buf_id = descs[avail_idx].id;
551 if (descs[avail_idx].flags & VRING_DESC_F_INDIRECT) {
552 if (unlikely(fill_vec_buf_packed_indirect(dev, vq,
558 *len += descs[avail_idx].len;
560 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
561 descs[avail_idx].addr,
562 descs[avail_idx].len,
567 if ((descs[avail_idx].flags & VRING_DESC_F_NEXT) == 0)
570 if (++avail_idx >= vq->size) {
571 avail_idx -= vq->size;
582 * Returns -1 on fail, 0 on success
585 reserve_avail_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
586 uint32_t size, struct buf_vector *buf_vec,
587 uint16_t *nr_vec, uint16_t *num_buffers,
591 uint16_t vec_idx = 0;
592 uint16_t max_tries, tries = 0;
599 avail_idx = vq->last_avail_idx;
601 if (rxvq_is_mergeable(dev))
602 max_tries = vq->size - 1;
608 * if we tried all available ring items, and still
609 * can't get enough buf, it means something abnormal
612 if (unlikely(++tries > max_tries))
615 if (unlikely(fill_vec_buf_packed(dev, vq,
616 avail_idx, &desc_count,
619 VHOST_ACCESS_RW) < 0))
622 len = RTE_MIN(len, size);
623 update_shadow_used_ring_packed(vq, buf_id, len, desc_count);
626 avail_idx += desc_count;
627 if (avail_idx >= vq->size)
628 avail_idx -= vq->size;
630 *nr_descs += desc_count;
639 static __rte_always_inline int
640 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
641 struct rte_mbuf *m, struct buf_vector *buf_vec,
642 uint16_t nr_vec, uint16_t num_buffers)
644 uint32_t vec_idx = 0;
645 uint32_t mbuf_offset, mbuf_avail;
646 uint32_t buf_offset, buf_avail;
647 uint64_t buf_addr, buf_iova, buf_len;
650 struct rte_mbuf *hdr_mbuf;
651 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
652 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
655 if (unlikely(m == NULL)) {
660 buf_addr = buf_vec[vec_idx].buf_addr;
661 buf_iova = buf_vec[vec_idx].buf_iova;
662 buf_len = buf_vec[vec_idx].buf_len;
665 rte_prefetch0((void *)(uintptr_t)buf_vec[1].buf_addr);
667 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
674 if (unlikely(buf_len < dev->vhost_hlen))
677 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
679 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) RX: num merge buffers %d\n",
680 dev->vid, num_buffers);
682 if (unlikely(buf_len < dev->vhost_hlen)) {
683 buf_offset = dev->vhost_hlen - buf_len;
685 buf_addr = buf_vec[vec_idx].buf_addr;
686 buf_iova = buf_vec[vec_idx].buf_iova;
687 buf_len = buf_vec[vec_idx].buf_len;
688 buf_avail = buf_len - buf_offset;
690 buf_offset = dev->vhost_hlen;
691 buf_avail = buf_len - dev->vhost_hlen;
694 mbuf_avail = rte_pktmbuf_data_len(m);
696 while (mbuf_avail != 0 || m->next != NULL) {
697 /* done with current buf, get the next one */
698 if (buf_avail == 0) {
700 if (unlikely(vec_idx >= nr_vec)) {
705 buf_addr = buf_vec[vec_idx].buf_addr;
706 buf_iova = buf_vec[vec_idx].buf_iova;
707 buf_len = buf_vec[vec_idx].buf_len;
709 /* Prefetch next buffer address. */
710 if (vec_idx + 1 < nr_vec)
711 rte_prefetch0((void *)(uintptr_t)
712 buf_vec[vec_idx + 1].buf_addr);
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)) {
733 uint64_t remain = dev->vhost_hlen;
734 uint64_t src = (uint64_t)(uintptr_t)hdr, dst;
735 uint64_t iova = buf_vec[0].buf_iova;
736 uint16_t hdr_vec_idx = 0;
739 len = RTE_MIN(remain,
740 buf_vec[hdr_vec_idx].buf_len);
741 dst = buf_vec[hdr_vec_idx].buf_addr;
742 rte_memcpy((void *)(uintptr_t)dst,
743 (void *)(uintptr_t)src,
746 PRINT_PACKET(dev, (uintptr_t)dst,
748 vhost_log_cache_write(dev, vq,
757 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
759 vhost_log_cache_write(dev, vq,
767 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
769 if (likely(cpy_len > MAX_BATCH_LEN ||
770 vq->batch_copy_nb_elems >= vq->size)) {
771 rte_memcpy((void *)((uintptr_t)(buf_addr + buf_offset)),
772 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
774 vhost_log_cache_write(dev, vq, buf_iova + buf_offset,
776 PRINT_PACKET(dev, (uintptr_t)(buf_addr + buf_offset),
779 batch_copy[vq->batch_copy_nb_elems].dst =
780 (void *)((uintptr_t)(buf_addr + buf_offset));
781 batch_copy[vq->batch_copy_nb_elems].src =
782 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
783 batch_copy[vq->batch_copy_nb_elems].log_addr =
784 buf_iova + buf_offset;
785 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
786 vq->batch_copy_nb_elems++;
789 mbuf_avail -= cpy_len;
790 mbuf_offset += cpy_len;
791 buf_avail -= cpy_len;
792 buf_offset += cpy_len;
800 static __rte_always_inline uint32_t
801 virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
802 struct rte_mbuf **pkts, uint32_t count)
804 uint32_t pkt_idx = 0;
805 uint16_t num_buffers;
806 struct buf_vector buf_vec[BUF_VECTOR_MAX];
809 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
810 avail_head = *((volatile uint16_t *)&vq->avail->idx);
813 * The ordering between avail index and
814 * desc reads needs to be enforced.
818 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
819 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
822 if (unlikely(reserve_avail_buf_split(dev, vq,
823 pkt_len, buf_vec, &num_buffers,
824 avail_head, &nr_vec) < 0)) {
825 VHOST_LOG_DEBUG(VHOST_DATA,
826 "(%d) failed to get enough desc from vring\n",
828 vq->shadow_used_idx -= num_buffers;
832 rte_prefetch0((void *)(uintptr_t)buf_vec[0].buf_addr);
834 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
835 dev->vid, vq->last_avail_idx,
836 vq->last_avail_idx + num_buffers);
838 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
841 vq->shadow_used_idx -= num_buffers;
845 vq->last_avail_idx += num_buffers;
848 do_data_copy_enqueue(dev, vq);
850 if (likely(vq->shadow_used_idx)) {
851 flush_shadow_used_ring_split(dev, vq);
852 vhost_vring_call_split(dev, vq);
858 static __rte_always_inline uint32_t
859 virtio_dev_rx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
860 struct rte_mbuf **pkts, uint32_t count)
862 uint32_t pkt_idx = 0;
863 uint16_t num_buffers;
864 struct buf_vector buf_vec[BUF_VECTOR_MAX];
866 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
867 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
869 uint16_t nr_descs = 0;
871 if (unlikely(reserve_avail_buf_packed(dev, vq,
872 pkt_len, buf_vec, &nr_vec,
873 &num_buffers, &nr_descs) < 0)) {
874 VHOST_LOG_DEBUG(VHOST_DATA,
875 "(%d) failed to get enough desc from vring\n",
877 vq->shadow_used_idx -= num_buffers;
881 rte_prefetch0((void *)(uintptr_t)buf_vec[0].buf_addr);
883 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
884 dev->vid, vq->last_avail_idx,
885 vq->last_avail_idx + num_buffers);
887 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
890 vq->shadow_used_idx -= num_buffers;
894 vq->last_avail_idx += nr_descs;
895 if (vq->last_avail_idx >= vq->size) {
896 vq->last_avail_idx -= vq->size;
897 vq->avail_wrap_counter ^= 1;
901 do_data_copy_enqueue(dev, vq);
903 if (likely(vq->shadow_used_idx)) {
904 flush_shadow_used_ring_packed(dev, vq);
905 vhost_vring_call_packed(dev, vq);
911 static __rte_always_inline uint32_t
912 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
913 struct rte_mbuf **pkts, uint32_t count)
915 struct vhost_virtqueue *vq;
918 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
919 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
920 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
921 dev->vid, __func__, queue_id);
925 vq = dev->virtqueue[queue_id];
927 rte_spinlock_lock(&vq->access_lock);
929 if (unlikely(vq->enabled == 0))
930 goto out_access_unlock;
932 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
933 vhost_user_iotlb_rd_lock(vq);
935 if (unlikely(vq->access_ok == 0))
936 if (unlikely(vring_translate(dev, vq) < 0))
939 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
943 if (vq_is_packed(dev))
944 nb_tx = virtio_dev_rx_packed(dev, vq, pkts, count);
946 nb_tx = virtio_dev_rx_split(dev, vq, pkts, count);
949 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
950 vhost_user_iotlb_rd_unlock(vq);
953 rte_spinlock_unlock(&vq->access_lock);
959 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
960 struct rte_mbuf **pkts, uint16_t count)
962 struct virtio_net *dev = get_device(vid);
967 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
968 RTE_LOG(ERR, VHOST_DATA,
969 "(%d) %s: built-in vhost net backend is disabled.\n",
974 return virtio_dev_rx(dev, queue_id, pkts, count);
978 virtio_net_with_host_offload(struct virtio_net *dev)
981 ((1ULL << VIRTIO_NET_F_CSUM) |
982 (1ULL << VIRTIO_NET_F_HOST_ECN) |
983 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
984 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
985 (1ULL << VIRTIO_NET_F_HOST_UFO)))
992 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
994 struct ipv4_hdr *ipv4_hdr;
995 struct ipv6_hdr *ipv6_hdr;
997 struct ether_hdr *eth_hdr;
1000 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
1002 m->l2_len = sizeof(struct ether_hdr);
1003 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
1005 if (ethertype == ETHER_TYPE_VLAN) {
1006 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
1008 m->l2_len += sizeof(struct vlan_hdr);
1009 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
1012 l3_hdr = (char *)eth_hdr + m->l2_len;
1014 switch (ethertype) {
1015 case ETHER_TYPE_IPv4:
1017 *l4_proto = ipv4_hdr->next_proto_id;
1018 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
1019 *l4_hdr = (char *)l3_hdr + m->l3_len;
1020 m->ol_flags |= PKT_TX_IPV4;
1022 case ETHER_TYPE_IPv6:
1024 *l4_proto = ipv6_hdr->proto;
1025 m->l3_len = sizeof(struct ipv6_hdr);
1026 *l4_hdr = (char *)l3_hdr + m->l3_len;
1027 m->ol_flags |= PKT_TX_IPV6;
1037 static __rte_always_inline void
1038 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
1040 uint16_t l4_proto = 0;
1041 void *l4_hdr = NULL;
1042 struct tcp_hdr *tcp_hdr = NULL;
1044 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
1047 parse_ethernet(m, &l4_proto, &l4_hdr);
1048 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1049 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
1050 switch (hdr->csum_offset) {
1051 case (offsetof(struct tcp_hdr, cksum)):
1052 if (l4_proto == IPPROTO_TCP)
1053 m->ol_flags |= PKT_TX_TCP_CKSUM;
1055 case (offsetof(struct udp_hdr, dgram_cksum)):
1056 if (l4_proto == IPPROTO_UDP)
1057 m->ol_flags |= PKT_TX_UDP_CKSUM;
1059 case (offsetof(struct sctp_hdr, cksum)):
1060 if (l4_proto == IPPROTO_SCTP)
1061 m->ol_flags |= PKT_TX_SCTP_CKSUM;
1069 if (l4_hdr && hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1070 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1071 case VIRTIO_NET_HDR_GSO_TCPV4:
1072 case VIRTIO_NET_HDR_GSO_TCPV6:
1074 m->ol_flags |= PKT_TX_TCP_SEG;
1075 m->tso_segsz = hdr->gso_size;
1076 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
1078 case VIRTIO_NET_HDR_GSO_UDP:
1079 m->ol_flags |= PKT_TX_UDP_SEG;
1080 m->tso_segsz = hdr->gso_size;
1081 m->l4_len = sizeof(struct udp_hdr);
1084 RTE_LOG(WARNING, VHOST_DATA,
1085 "unsupported gso type %u.\n", hdr->gso_type);
1091 static __rte_always_inline int
1092 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
1093 struct buf_vector *buf_vec, uint16_t nr_vec,
1094 struct rte_mbuf *m, struct rte_mempool *mbuf_pool)
1096 uint32_t buf_avail, buf_offset;
1097 uint64_t buf_addr, buf_iova, buf_len;
1098 uint32_t mbuf_avail, mbuf_offset;
1100 struct rte_mbuf *cur = m, *prev = m;
1101 struct virtio_net_hdr tmp_hdr;
1102 struct virtio_net_hdr *hdr = NULL;
1103 /* A counter to avoid desc dead loop chain */
1104 uint16_t vec_idx = 0;
1105 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
1108 buf_addr = buf_vec[vec_idx].buf_addr;
1109 buf_iova = buf_vec[vec_idx].buf_iova;
1110 buf_len = buf_vec[vec_idx].buf_len;
1112 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
1117 if (likely(nr_vec > 1))
1118 rte_prefetch0((void *)(uintptr_t)buf_vec[1].buf_addr);
1120 if (virtio_net_with_host_offload(dev)) {
1121 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
1123 uint64_t remain = sizeof(struct virtio_net_hdr);
1125 uint64_t dst = (uint64_t)(uintptr_t)&tmp_hdr;
1126 uint16_t hdr_vec_idx = 0;
1129 * No luck, the virtio-net header doesn't fit
1130 * in a contiguous virtual area.
1133 len = RTE_MIN(remain,
1134 buf_vec[hdr_vec_idx].buf_len);
1135 src = buf_vec[hdr_vec_idx].buf_addr;
1136 rte_memcpy((void *)(uintptr_t)dst,
1137 (void *)(uintptr_t)src, len);
1146 hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
1152 * A virtio driver normally uses at least 2 desc buffers
1153 * for Tx: the first for storing the header, and others
1154 * for storing the data.
1156 if (unlikely(buf_len < dev->vhost_hlen)) {
1157 buf_offset = dev->vhost_hlen - buf_len;
1159 buf_addr = buf_vec[vec_idx].buf_addr;
1160 buf_iova = buf_vec[vec_idx].buf_iova;
1161 buf_len = buf_vec[vec_idx].buf_len;
1162 buf_avail = buf_len - buf_offset;
1163 } else if (buf_len == dev->vhost_hlen) {
1164 if (unlikely(++vec_idx >= nr_vec))
1166 buf_addr = buf_vec[vec_idx].buf_addr;
1167 buf_iova = buf_vec[vec_idx].buf_iova;
1168 buf_len = buf_vec[vec_idx].buf_len;
1171 buf_avail = buf_len;
1173 buf_offset = dev->vhost_hlen;
1174 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
1177 rte_prefetch0((void *)(uintptr_t)
1178 (buf_addr + buf_offset));
1181 (uintptr_t)(buf_addr + buf_offset),
1182 (uint32_t)buf_avail, 0);
1185 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
1189 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1192 * A desc buf might across two host physical pages that are
1193 * not continuous. In such case (gpa_to_hpa returns 0), data
1194 * will be copied even though zero copy is enabled.
1196 if (unlikely(dev->dequeue_zero_copy && (hpa = gpa_to_hpa(dev,
1197 buf_iova + buf_offset, cpy_len)))) {
1198 cur->data_len = cpy_len;
1201 (void *)(uintptr_t)(buf_addr + buf_offset);
1202 cur->buf_iova = hpa;
1205 * In zero copy mode, one mbuf can only reference data
1206 * for one or partial of one desc buff.
1208 mbuf_avail = cpy_len;
1210 if (likely(cpy_len > MAX_BATCH_LEN ||
1211 vq->batch_copy_nb_elems >= vq->size ||
1212 (hdr && cur == m))) {
1213 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
1215 (void *)((uintptr_t)(buf_addr +
1219 batch_copy[vq->batch_copy_nb_elems].dst =
1220 rte_pktmbuf_mtod_offset(cur, void *,
1222 batch_copy[vq->batch_copy_nb_elems].src =
1223 (void *)((uintptr_t)(buf_addr +
1225 batch_copy[vq->batch_copy_nb_elems].len =
1227 vq->batch_copy_nb_elems++;
1231 mbuf_avail -= cpy_len;
1232 mbuf_offset += cpy_len;
1233 buf_avail -= cpy_len;
1234 buf_offset += cpy_len;
1236 /* This buf reaches to its end, get the next one */
1237 if (buf_avail == 0) {
1238 if (++vec_idx >= nr_vec)
1241 buf_addr = buf_vec[vec_idx].buf_addr;
1242 buf_iova = buf_vec[vec_idx].buf_iova;
1243 buf_len = buf_vec[vec_idx].buf_len;
1246 * Prefecth desc n + 1 buffer while
1247 * desc n buffer is processed.
1249 if (vec_idx + 1 < nr_vec)
1250 rte_prefetch0((void *)(uintptr_t)
1251 buf_vec[vec_idx + 1].buf_addr);
1254 buf_avail = buf_len;
1256 PRINT_PACKET(dev, (uintptr_t)buf_addr,
1257 (uint32_t)buf_avail, 0);
1261 * This mbuf reaches to its end, get a new one
1262 * to hold more data.
1264 if (mbuf_avail == 0) {
1265 cur = rte_pktmbuf_alloc(mbuf_pool);
1266 if (unlikely(cur == NULL)) {
1267 RTE_LOG(ERR, VHOST_DATA, "Failed to "
1268 "allocate memory for mbuf.\n");
1272 if (unlikely(dev->dequeue_zero_copy))
1273 rte_mbuf_refcnt_update(cur, 1);
1276 prev->data_len = mbuf_offset;
1278 m->pkt_len += mbuf_offset;
1282 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
1286 prev->data_len = mbuf_offset;
1287 m->pkt_len += mbuf_offset;
1290 vhost_dequeue_offload(hdr, m);
1297 static __rte_always_inline struct zcopy_mbuf *
1298 get_zmbuf(struct vhost_virtqueue *vq)
1304 /* search [last_zmbuf_idx, zmbuf_size) */
1305 i = vq->last_zmbuf_idx;
1306 last = vq->zmbuf_size;
1309 for (; i < last; i++) {
1310 if (vq->zmbufs[i].in_use == 0) {
1311 vq->last_zmbuf_idx = i + 1;
1312 vq->zmbufs[i].in_use = 1;
1313 return &vq->zmbufs[i];
1319 /* search [0, last_zmbuf_idx) */
1321 last = vq->last_zmbuf_idx;
1328 static __rte_always_inline uint16_t
1329 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1330 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1333 uint16_t free_entries;
1335 if (unlikely(dev->dequeue_zero_copy)) {
1336 struct zcopy_mbuf *zmbuf, *next;
1338 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1339 zmbuf != NULL; zmbuf = next) {
1340 next = TAILQ_NEXT(zmbuf, next);
1342 if (mbuf_is_consumed(zmbuf->mbuf)) {
1343 update_shadow_used_ring_split(vq,
1344 zmbuf->desc_idx, 0);
1345 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1346 restore_mbuf(zmbuf->mbuf);
1347 rte_pktmbuf_free(zmbuf->mbuf);
1353 if (likely(vq->shadow_used_idx)) {
1354 flush_shadow_used_ring_split(dev, vq);
1355 vhost_vring_call_split(dev, vq);
1359 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1361 free_entries = *((volatile uint16_t *)&vq->avail->idx) -
1363 if (free_entries == 0)
1367 * The ordering between avail index and
1368 * desc reads needs to be enforced.
1372 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1374 count = RTE_MIN(count, MAX_PKT_BURST);
1375 count = RTE_MIN(count, free_entries);
1376 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1379 for (i = 0; i < count; i++) {
1380 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1383 uint16_t nr_vec = 0;
1386 if (unlikely(fill_vec_buf_split(dev, vq,
1387 vq->last_avail_idx + i,
1389 &head_idx, &dummy_len,
1390 VHOST_ACCESS_RO) < 0))
1393 if (likely(dev->dequeue_zero_copy == 0))
1394 update_shadow_used_ring_split(vq, head_idx, 0);
1396 rte_prefetch0((void *)(uintptr_t)buf_vec[0].buf_addr);
1398 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
1399 if (unlikely(pkts[i] == NULL)) {
1400 RTE_LOG(ERR, VHOST_DATA,
1401 "Failed to allocate memory for mbuf.\n");
1405 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
1407 if (unlikely(err)) {
1408 rte_pktmbuf_free(pkts[i]);
1412 if (unlikely(dev->dequeue_zero_copy)) {
1413 struct zcopy_mbuf *zmbuf;
1415 zmbuf = get_zmbuf(vq);
1417 rte_pktmbuf_free(pkts[i]);
1420 zmbuf->mbuf = pkts[i];
1421 zmbuf->desc_idx = head_idx;
1424 * Pin lock the mbuf; we will check later to see
1425 * whether the mbuf is freed (when we are the last
1426 * user) or not. If that's the case, we then could
1427 * update the used ring safely.
1429 rte_mbuf_refcnt_update(pkts[i], 1);
1432 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1435 vq->last_avail_idx += i;
1437 if (likely(dev->dequeue_zero_copy == 0)) {
1438 do_data_copy_dequeue(vq);
1439 if (unlikely(i < count))
1440 vq->shadow_used_idx = i;
1441 if (likely(vq->shadow_used_idx)) {
1442 flush_shadow_used_ring_split(dev, vq);
1443 vhost_vring_call_split(dev, vq);
1450 static __rte_always_inline uint16_t
1451 virtio_dev_tx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
1452 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1456 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1458 if (unlikely(dev->dequeue_zero_copy)) {
1459 struct zcopy_mbuf *zmbuf, *next;
1461 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1462 zmbuf != NULL; zmbuf = next) {
1463 next = TAILQ_NEXT(zmbuf, next);
1465 if (mbuf_is_consumed(zmbuf->mbuf)) {
1466 update_shadow_used_ring_packed(vq,
1471 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1472 restore_mbuf(zmbuf->mbuf);
1473 rte_pktmbuf_free(zmbuf->mbuf);
1479 if (likely(vq->shadow_used_idx)) {
1480 flush_shadow_used_ring_packed(dev, vq);
1481 vhost_vring_call_packed(dev, vq);
1485 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1487 count = RTE_MIN(count, MAX_PKT_BURST);
1488 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1491 for (i = 0; i < count; i++) {
1492 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1495 uint16_t desc_count, nr_vec = 0;
1498 if (unlikely(fill_vec_buf_packed(dev, vq,
1499 vq->last_avail_idx, &desc_count,
1501 &buf_id, &dummy_len,
1502 VHOST_ACCESS_RO) < 0))
1505 if (likely(dev->dequeue_zero_copy == 0))
1506 update_shadow_used_ring_packed(vq, buf_id, 0,
1509 rte_prefetch0((void *)(uintptr_t)buf_vec[0].buf_addr);
1511 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
1512 if (unlikely(pkts[i] == NULL)) {
1513 RTE_LOG(ERR, VHOST_DATA,
1514 "Failed to allocate memory for mbuf.\n");
1518 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
1520 if (unlikely(err)) {
1521 rte_pktmbuf_free(pkts[i]);
1525 if (unlikely(dev->dequeue_zero_copy)) {
1526 struct zcopy_mbuf *zmbuf;
1528 zmbuf = get_zmbuf(vq);
1530 rte_pktmbuf_free(pkts[i]);
1533 zmbuf->mbuf = pkts[i];
1534 zmbuf->desc_idx = buf_id;
1535 zmbuf->desc_count = desc_count;
1538 * Pin lock the mbuf; we will check later to see
1539 * whether the mbuf is freed (when we are the last
1540 * user) or not. If that's the case, we then could
1541 * update the used ring safely.
1543 rte_mbuf_refcnt_update(pkts[i], 1);
1546 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1549 vq->last_avail_idx += desc_count;
1550 if (vq->last_avail_idx >= vq->size) {
1551 vq->last_avail_idx -= vq->size;
1552 vq->avail_wrap_counter ^= 1;
1556 if (likely(dev->dequeue_zero_copy == 0)) {
1557 do_data_copy_dequeue(vq);
1558 if (unlikely(i < count))
1559 vq->shadow_used_idx = i;
1560 if (likely(vq->shadow_used_idx)) {
1561 flush_shadow_used_ring_packed(dev, vq);
1562 vhost_vring_call_packed(dev, vq);
1570 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
1571 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1573 struct virtio_net *dev;
1574 struct rte_mbuf *rarp_mbuf = NULL;
1575 struct vhost_virtqueue *vq;
1577 dev = get_device(vid);
1581 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1582 RTE_LOG(ERR, VHOST_DATA,
1583 "(%d) %s: built-in vhost net backend is disabled.\n",
1584 dev->vid, __func__);
1588 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
1589 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
1590 dev->vid, __func__, queue_id);
1594 vq = dev->virtqueue[queue_id];
1596 if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
1599 if (unlikely(vq->enabled == 0)) {
1601 goto out_access_unlock;
1604 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1605 vhost_user_iotlb_rd_lock(vq);
1607 if (unlikely(vq->access_ok == 0))
1608 if (unlikely(vring_translate(dev, vq) < 0)) {
1614 * Construct a RARP broadcast packet, and inject it to the "pkts"
1615 * array, to looks like that guest actually send such packet.
1617 * Check user_send_rarp() for more information.
1619 * broadcast_rarp shares a cacheline in the virtio_net structure
1620 * with some fields that are accessed during enqueue and
1621 * rte_atomic16_cmpset() causes a write if using cmpxchg. This could
1622 * result in false sharing between enqueue and dequeue.
1624 * Prevent unnecessary false sharing by reading broadcast_rarp first
1625 * and only performing cmpset if the read indicates it is likely to
1628 if (unlikely(rte_atomic16_read(&dev->broadcast_rarp) &&
1629 rte_atomic16_cmpset((volatile uint16_t *)
1630 &dev->broadcast_rarp.cnt, 1, 0))) {
1632 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
1633 if (rarp_mbuf == NULL) {
1634 RTE_LOG(ERR, VHOST_DATA,
1635 "Failed to make RARP packet.\n");
1642 if (vq_is_packed(dev))
1643 count = virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count);
1645 count = virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count);
1648 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1649 vhost_user_iotlb_rd_unlock(vq);
1652 rte_spinlock_unlock(&vq->access_lock);
1654 if (unlikely(rarp_mbuf != NULL)) {
1656 * Inject it to the head of "pkts" array, so that switch's mac
1657 * learning table will get updated first.
1659 memmove(&pkts[1], pkts, count * sizeof(struct rte_mbuf *));
1660 pkts[0] = rarp_mbuf;