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)
507 * Perform a load-acquire barrier in desc_is_avail to
508 * enforce the ordering between desc flags and desc
511 if (unlikely(!desc_is_avail(&descs[avail_idx], wrap_counter)))
518 if (unlikely(vec_id >= BUF_VECTOR_MAX))
521 if (unlikely(*desc_count >= vq->size))
525 *buf_id = descs[avail_idx].id;
527 if (descs[avail_idx].flags & VRING_DESC_F_INDIRECT) {
528 if (unlikely(fill_vec_buf_packed_indirect(dev, vq,
534 *len += descs[avail_idx].len;
536 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
537 descs[avail_idx].addr,
538 descs[avail_idx].len,
543 if ((descs[avail_idx].flags & VRING_DESC_F_NEXT) == 0)
546 if (++avail_idx >= vq->size) {
547 avail_idx -= vq->size;
558 * Returns -1 on fail, 0 on success
561 reserve_avail_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
562 uint32_t size, struct buf_vector *buf_vec,
563 uint16_t *nr_vec, uint16_t *num_buffers,
567 uint16_t vec_idx = 0;
568 uint16_t max_tries, tries = 0;
575 avail_idx = vq->last_avail_idx;
577 if (rxvq_is_mergeable(dev))
578 max_tries = vq->size - 1;
584 * if we tried all available ring items, and still
585 * can't get enough buf, it means something abnormal
588 if (unlikely(++tries > max_tries))
591 if (unlikely(fill_vec_buf_packed(dev, vq,
592 avail_idx, &desc_count,
595 VHOST_ACCESS_RW) < 0))
598 len = RTE_MIN(len, size);
599 update_shadow_used_ring_packed(vq, buf_id, len, desc_count);
602 avail_idx += desc_count;
603 if (avail_idx >= vq->size)
604 avail_idx -= vq->size;
606 *nr_descs += desc_count;
615 static __rte_noinline void
616 copy_vnet_hdr_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
617 struct buf_vector *buf_vec,
618 struct virtio_net_hdr_mrg_rxbuf *hdr)
621 uint64_t remain = dev->vhost_hlen;
622 uint64_t src = (uint64_t)(uintptr_t)hdr, dst;
623 uint64_t iova = buf_vec->buf_iova;
626 len = RTE_MIN(remain,
628 dst = buf_vec->buf_addr;
629 rte_memcpy((void *)(uintptr_t)dst,
630 (void *)(uintptr_t)src,
633 PRINT_PACKET(dev, (uintptr_t)dst,
635 vhost_log_cache_write(dev, vq,
645 static __rte_always_inline int
646 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
647 struct rte_mbuf *m, struct buf_vector *buf_vec,
648 uint16_t nr_vec, uint16_t num_buffers)
650 uint32_t vec_idx = 0;
651 uint32_t mbuf_offset, mbuf_avail;
652 uint32_t buf_offset, buf_avail;
653 uint64_t buf_addr, buf_iova, buf_len;
656 struct rte_mbuf *hdr_mbuf;
657 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
658 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
661 if (unlikely(m == NULL)) {
666 buf_addr = buf_vec[vec_idx].buf_addr;
667 buf_iova = buf_vec[vec_idx].buf_iova;
668 buf_len = buf_vec[vec_idx].buf_len;
670 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
677 if (unlikely(buf_len < dev->vhost_hlen))
680 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
682 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) RX: num merge buffers %d\n",
683 dev->vid, num_buffers);
685 if (unlikely(buf_len < dev->vhost_hlen)) {
686 buf_offset = dev->vhost_hlen - buf_len;
688 buf_addr = buf_vec[vec_idx].buf_addr;
689 buf_iova = buf_vec[vec_idx].buf_iova;
690 buf_len = buf_vec[vec_idx].buf_len;
691 buf_avail = buf_len - buf_offset;
693 buf_offset = dev->vhost_hlen;
694 buf_avail = buf_len - dev->vhost_hlen;
697 mbuf_avail = rte_pktmbuf_data_len(m);
699 while (mbuf_avail != 0 || m->next != NULL) {
700 /* done with current buf, get the next one */
701 if (buf_avail == 0) {
703 if (unlikely(vec_idx >= nr_vec)) {
708 buf_addr = buf_vec[vec_idx].buf_addr;
709 buf_iova = buf_vec[vec_idx].buf_iova;
710 buf_len = buf_vec[vec_idx].buf_len;
716 /* done with current mbuf, get the next one */
717 if (mbuf_avail == 0) {
721 mbuf_avail = rte_pktmbuf_data_len(m);
725 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
726 if (rxvq_is_mergeable(dev))
727 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
730 if (unlikely(hdr == &tmp_hdr)) {
731 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
733 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
735 vhost_log_cache_write(dev, vq,
743 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
745 if (likely(cpy_len > MAX_BATCH_LEN ||
746 vq->batch_copy_nb_elems >= vq->size)) {
747 rte_memcpy((void *)((uintptr_t)(buf_addr + buf_offset)),
748 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
750 vhost_log_cache_write(dev, vq, buf_iova + buf_offset,
752 PRINT_PACKET(dev, (uintptr_t)(buf_addr + buf_offset),
755 batch_copy[vq->batch_copy_nb_elems].dst =
756 (void *)((uintptr_t)(buf_addr + buf_offset));
757 batch_copy[vq->batch_copy_nb_elems].src =
758 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
759 batch_copy[vq->batch_copy_nb_elems].log_addr =
760 buf_iova + buf_offset;
761 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
762 vq->batch_copy_nb_elems++;
765 mbuf_avail -= cpy_len;
766 mbuf_offset += cpy_len;
767 buf_avail -= cpy_len;
768 buf_offset += cpy_len;
776 static __rte_noinline uint32_t
777 virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
778 struct rte_mbuf **pkts, uint32_t count)
780 uint32_t pkt_idx = 0;
781 uint16_t num_buffers;
782 struct buf_vector buf_vec[BUF_VECTOR_MAX];
785 avail_head = *((volatile uint16_t *)&vq->avail->idx);
788 * The ordering between avail index and
789 * desc reads needs to be enforced.
793 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
795 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
796 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
799 if (unlikely(reserve_avail_buf_split(dev, vq,
800 pkt_len, buf_vec, &num_buffers,
801 avail_head, &nr_vec) < 0)) {
802 VHOST_LOG_DEBUG(VHOST_DATA,
803 "(%d) failed to get enough desc from vring\n",
805 vq->shadow_used_idx -= num_buffers;
809 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
810 dev->vid, vq->last_avail_idx,
811 vq->last_avail_idx + num_buffers);
813 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
816 vq->shadow_used_idx -= num_buffers;
820 vq->last_avail_idx += num_buffers;
823 do_data_copy_enqueue(dev, vq);
825 if (likely(vq->shadow_used_idx)) {
826 flush_shadow_used_ring_split(dev, vq);
827 vhost_vring_call_split(dev, vq);
833 static __rte_noinline uint32_t
834 virtio_dev_rx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
835 struct rte_mbuf **pkts, uint32_t count)
837 uint32_t pkt_idx = 0;
838 uint16_t num_buffers;
839 struct buf_vector buf_vec[BUF_VECTOR_MAX];
841 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
842 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
844 uint16_t nr_descs = 0;
846 if (unlikely(reserve_avail_buf_packed(dev, vq,
847 pkt_len, buf_vec, &nr_vec,
848 &num_buffers, &nr_descs) < 0)) {
849 VHOST_LOG_DEBUG(VHOST_DATA,
850 "(%d) failed to get enough desc from vring\n",
852 vq->shadow_used_idx -= num_buffers;
856 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
857 dev->vid, vq->last_avail_idx,
858 vq->last_avail_idx + num_buffers);
860 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
863 vq->shadow_used_idx -= num_buffers;
867 vq->last_avail_idx += nr_descs;
868 if (vq->last_avail_idx >= vq->size) {
869 vq->last_avail_idx -= vq->size;
870 vq->avail_wrap_counter ^= 1;
874 do_data_copy_enqueue(dev, vq);
876 if (likely(vq->shadow_used_idx)) {
877 flush_shadow_used_ring_packed(dev, vq);
878 vhost_vring_call_packed(dev, vq);
884 static __rte_always_inline uint32_t
885 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
886 struct rte_mbuf **pkts, uint32_t count)
888 struct vhost_virtqueue *vq;
891 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
892 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
893 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
894 dev->vid, __func__, queue_id);
898 vq = dev->virtqueue[queue_id];
900 rte_spinlock_lock(&vq->access_lock);
902 if (unlikely(vq->enabled == 0))
903 goto out_access_unlock;
905 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
906 vhost_user_iotlb_rd_lock(vq);
908 if (unlikely(vq->access_ok == 0))
909 if (unlikely(vring_translate(dev, vq) < 0))
912 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
916 if (vq_is_packed(dev))
917 nb_tx = virtio_dev_rx_packed(dev, vq, pkts, count);
919 nb_tx = virtio_dev_rx_split(dev, vq, pkts, count);
922 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
923 vhost_user_iotlb_rd_unlock(vq);
926 rte_spinlock_unlock(&vq->access_lock);
932 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
933 struct rte_mbuf **pkts, uint16_t count)
935 struct virtio_net *dev = get_device(vid);
940 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
941 RTE_LOG(ERR, VHOST_DATA,
942 "(%d) %s: built-in vhost net backend is disabled.\n",
947 return virtio_dev_rx(dev, queue_id, pkts, count);
951 virtio_net_with_host_offload(struct virtio_net *dev)
954 ((1ULL << VIRTIO_NET_F_CSUM) |
955 (1ULL << VIRTIO_NET_F_HOST_ECN) |
956 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
957 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
958 (1ULL << VIRTIO_NET_F_HOST_UFO)))
965 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
967 struct rte_ipv4_hdr *ipv4_hdr;
968 struct rte_ipv6_hdr *ipv6_hdr;
970 struct rte_ether_hdr *eth_hdr;
973 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
975 m->l2_len = sizeof(struct rte_ether_hdr);
976 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
978 if (ethertype == RTE_ETHER_TYPE_VLAN) {
979 struct rte_vlan_hdr *vlan_hdr =
980 (struct rte_vlan_hdr *)(eth_hdr + 1);
982 m->l2_len += sizeof(struct rte_vlan_hdr);
983 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
986 l3_hdr = (char *)eth_hdr + m->l2_len;
989 case RTE_ETHER_TYPE_IPV4:
991 *l4_proto = ipv4_hdr->next_proto_id;
992 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
993 *l4_hdr = (char *)l3_hdr + m->l3_len;
994 m->ol_flags |= PKT_TX_IPV4;
996 case RTE_ETHER_TYPE_IPV6:
998 *l4_proto = ipv6_hdr->proto;
999 m->l3_len = sizeof(struct rte_ipv6_hdr);
1000 *l4_hdr = (char *)l3_hdr + m->l3_len;
1001 m->ol_flags |= PKT_TX_IPV6;
1011 static __rte_always_inline void
1012 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
1014 uint16_t l4_proto = 0;
1015 void *l4_hdr = NULL;
1016 struct rte_tcp_hdr *tcp_hdr = NULL;
1018 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
1021 parse_ethernet(m, &l4_proto, &l4_hdr);
1022 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1023 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
1024 switch (hdr->csum_offset) {
1025 case (offsetof(struct rte_tcp_hdr, cksum)):
1026 if (l4_proto == IPPROTO_TCP)
1027 m->ol_flags |= PKT_TX_TCP_CKSUM;
1029 case (offsetof(struct rte_udp_hdr, dgram_cksum)):
1030 if (l4_proto == IPPROTO_UDP)
1031 m->ol_flags |= PKT_TX_UDP_CKSUM;
1033 case (offsetof(struct rte_sctp_hdr, cksum)):
1034 if (l4_proto == IPPROTO_SCTP)
1035 m->ol_flags |= PKT_TX_SCTP_CKSUM;
1043 if (l4_hdr && hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1044 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1045 case VIRTIO_NET_HDR_GSO_TCPV4:
1046 case VIRTIO_NET_HDR_GSO_TCPV6:
1048 m->ol_flags |= PKT_TX_TCP_SEG;
1049 m->tso_segsz = hdr->gso_size;
1050 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
1052 case VIRTIO_NET_HDR_GSO_UDP:
1053 m->ol_flags |= PKT_TX_UDP_SEG;
1054 m->tso_segsz = hdr->gso_size;
1055 m->l4_len = sizeof(struct rte_udp_hdr);
1058 RTE_LOG(WARNING, VHOST_DATA,
1059 "unsupported gso type %u.\n", hdr->gso_type);
1065 static __rte_noinline void
1066 copy_vnet_hdr_from_desc(struct virtio_net_hdr *hdr,
1067 struct buf_vector *buf_vec)
1070 uint64_t remain = sizeof(struct virtio_net_hdr);
1072 uint64_t dst = (uint64_t)(uintptr_t)hdr;
1075 len = RTE_MIN(remain, buf_vec->buf_len);
1076 src = buf_vec->buf_addr;
1077 rte_memcpy((void *)(uintptr_t)dst,
1078 (void *)(uintptr_t)src, len);
1086 static __rte_always_inline int
1087 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
1088 struct buf_vector *buf_vec, uint16_t nr_vec,
1089 struct rte_mbuf *m, struct rte_mempool *mbuf_pool)
1091 uint32_t buf_avail, buf_offset;
1092 uint64_t buf_addr, buf_iova, buf_len;
1093 uint32_t mbuf_avail, mbuf_offset;
1095 struct rte_mbuf *cur = m, *prev = m;
1096 struct virtio_net_hdr tmp_hdr;
1097 struct virtio_net_hdr *hdr = NULL;
1098 /* A counter to avoid desc dead loop chain */
1099 uint16_t vec_idx = 0;
1100 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
1103 buf_addr = buf_vec[vec_idx].buf_addr;
1104 buf_iova = buf_vec[vec_idx].buf_iova;
1105 buf_len = buf_vec[vec_idx].buf_len;
1107 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
1112 if (virtio_net_with_host_offload(dev)) {
1113 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
1115 * No luck, the virtio-net header doesn't fit
1116 * in a contiguous virtual area.
1118 copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
1121 hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
1126 * A virtio driver normally uses at least 2 desc buffers
1127 * for Tx: the first for storing the header, and others
1128 * for storing the data.
1130 if (unlikely(buf_len < dev->vhost_hlen)) {
1131 buf_offset = dev->vhost_hlen - buf_len;
1133 buf_addr = buf_vec[vec_idx].buf_addr;
1134 buf_iova = buf_vec[vec_idx].buf_iova;
1135 buf_len = buf_vec[vec_idx].buf_len;
1136 buf_avail = buf_len - buf_offset;
1137 } else if (buf_len == dev->vhost_hlen) {
1138 if (unlikely(++vec_idx >= nr_vec))
1140 buf_addr = buf_vec[vec_idx].buf_addr;
1141 buf_iova = buf_vec[vec_idx].buf_iova;
1142 buf_len = buf_vec[vec_idx].buf_len;
1145 buf_avail = buf_len;
1147 buf_offset = dev->vhost_hlen;
1148 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
1152 (uintptr_t)(buf_addr + buf_offset),
1153 (uint32_t)buf_avail, 0);
1156 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
1160 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1163 * A desc buf might across two host physical pages that are
1164 * not continuous. In such case (gpa_to_hpa returns 0), data
1165 * will be copied even though zero copy is enabled.
1167 if (unlikely(dev->dequeue_zero_copy && (hpa = gpa_to_hpa(dev,
1168 buf_iova + buf_offset, cpy_len)))) {
1169 cur->data_len = cpy_len;
1172 (void *)(uintptr_t)(buf_addr + buf_offset);
1173 cur->buf_iova = hpa;
1176 * In zero copy mode, one mbuf can only reference data
1177 * for one or partial of one desc buff.
1179 mbuf_avail = cpy_len;
1181 if (likely(cpy_len > MAX_BATCH_LEN ||
1182 vq->batch_copy_nb_elems >= vq->size ||
1183 (hdr && cur == m))) {
1184 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
1186 (void *)((uintptr_t)(buf_addr +
1190 batch_copy[vq->batch_copy_nb_elems].dst =
1191 rte_pktmbuf_mtod_offset(cur, void *,
1193 batch_copy[vq->batch_copy_nb_elems].src =
1194 (void *)((uintptr_t)(buf_addr +
1196 batch_copy[vq->batch_copy_nb_elems].len =
1198 vq->batch_copy_nb_elems++;
1202 mbuf_avail -= cpy_len;
1203 mbuf_offset += cpy_len;
1204 buf_avail -= cpy_len;
1205 buf_offset += cpy_len;
1207 /* This buf reaches to its end, get the next one */
1208 if (buf_avail == 0) {
1209 if (++vec_idx >= nr_vec)
1212 buf_addr = buf_vec[vec_idx].buf_addr;
1213 buf_iova = buf_vec[vec_idx].buf_iova;
1214 buf_len = buf_vec[vec_idx].buf_len;
1217 buf_avail = buf_len;
1219 PRINT_PACKET(dev, (uintptr_t)buf_addr,
1220 (uint32_t)buf_avail, 0);
1224 * This mbuf reaches to its end, get a new one
1225 * to hold more data.
1227 if (mbuf_avail == 0) {
1228 cur = rte_pktmbuf_alloc(mbuf_pool);
1229 if (unlikely(cur == NULL)) {
1230 RTE_LOG(ERR, VHOST_DATA, "Failed to "
1231 "allocate memory for mbuf.\n");
1235 if (unlikely(dev->dequeue_zero_copy))
1236 rte_mbuf_refcnt_update(cur, 1);
1239 prev->data_len = mbuf_offset;
1241 m->pkt_len += mbuf_offset;
1245 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
1249 prev->data_len = mbuf_offset;
1250 m->pkt_len += mbuf_offset;
1253 vhost_dequeue_offload(hdr, m);
1260 static __rte_always_inline struct zcopy_mbuf *
1261 get_zmbuf(struct vhost_virtqueue *vq)
1267 /* search [last_zmbuf_idx, zmbuf_size) */
1268 i = vq->last_zmbuf_idx;
1269 last = vq->zmbuf_size;
1272 for (; i < last; i++) {
1273 if (vq->zmbufs[i].in_use == 0) {
1274 vq->last_zmbuf_idx = i + 1;
1275 vq->zmbufs[i].in_use = 1;
1276 return &vq->zmbufs[i];
1282 /* search [0, last_zmbuf_idx) */
1284 last = vq->last_zmbuf_idx;
1291 static __rte_noinline uint16_t
1292 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1293 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1296 uint16_t free_entries;
1298 if (unlikely(dev->dequeue_zero_copy)) {
1299 struct zcopy_mbuf *zmbuf, *next;
1301 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1302 zmbuf != NULL; zmbuf = next) {
1303 next = TAILQ_NEXT(zmbuf, next);
1305 if (mbuf_is_consumed(zmbuf->mbuf)) {
1306 update_shadow_used_ring_split(vq,
1307 zmbuf->desc_idx, 0);
1308 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1309 restore_mbuf(zmbuf->mbuf);
1310 rte_pktmbuf_free(zmbuf->mbuf);
1316 if (likely(vq->shadow_used_idx)) {
1317 flush_shadow_used_ring_split(dev, vq);
1318 vhost_vring_call_split(dev, vq);
1322 free_entries = *((volatile uint16_t *)&vq->avail->idx) -
1324 if (free_entries == 0)
1328 * The ordering between avail index and
1329 * desc reads needs to be enforced.
1333 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1335 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1337 count = RTE_MIN(count, MAX_PKT_BURST);
1338 count = RTE_MIN(count, free_entries);
1339 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1342 for (i = 0; i < count; i++) {
1343 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1346 uint16_t nr_vec = 0;
1349 if (unlikely(fill_vec_buf_split(dev, vq,
1350 vq->last_avail_idx + i,
1352 &head_idx, &dummy_len,
1353 VHOST_ACCESS_RO) < 0))
1356 if (likely(dev->dequeue_zero_copy == 0))
1357 update_shadow_used_ring_split(vq, head_idx, 0);
1359 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
1360 if (unlikely(pkts[i] == NULL)) {
1361 RTE_LOG(ERR, VHOST_DATA,
1362 "Failed to allocate memory for mbuf.\n");
1366 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
1368 if (unlikely(err)) {
1369 rte_pktmbuf_free(pkts[i]);
1373 if (unlikely(dev->dequeue_zero_copy)) {
1374 struct zcopy_mbuf *zmbuf;
1376 zmbuf = get_zmbuf(vq);
1378 rte_pktmbuf_free(pkts[i]);
1381 zmbuf->mbuf = pkts[i];
1382 zmbuf->desc_idx = head_idx;
1385 * Pin lock the mbuf; we will check later to see
1386 * whether the mbuf is freed (when we are the last
1387 * user) or not. If that's the case, we then could
1388 * update the used ring safely.
1390 rte_mbuf_refcnt_update(pkts[i], 1);
1393 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1396 vq->last_avail_idx += i;
1398 if (likely(dev->dequeue_zero_copy == 0)) {
1399 do_data_copy_dequeue(vq);
1400 if (unlikely(i < count))
1401 vq->shadow_used_idx = i;
1402 if (likely(vq->shadow_used_idx)) {
1403 flush_shadow_used_ring_split(dev, vq);
1404 vhost_vring_call_split(dev, vq);
1411 static __rte_noinline uint16_t
1412 virtio_dev_tx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
1413 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1417 if (unlikely(dev->dequeue_zero_copy)) {
1418 struct zcopy_mbuf *zmbuf, *next;
1420 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1421 zmbuf != NULL; zmbuf = next) {
1422 next = TAILQ_NEXT(zmbuf, next);
1424 if (mbuf_is_consumed(zmbuf->mbuf)) {
1425 update_shadow_used_ring_packed(vq,
1430 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1431 restore_mbuf(zmbuf->mbuf);
1432 rte_pktmbuf_free(zmbuf->mbuf);
1438 if (likely(vq->shadow_used_idx)) {
1439 flush_shadow_used_ring_packed(dev, vq);
1440 vhost_vring_call_packed(dev, vq);
1444 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1446 count = RTE_MIN(count, MAX_PKT_BURST);
1447 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1450 for (i = 0; i < count; i++) {
1451 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1454 uint16_t desc_count, nr_vec = 0;
1457 if (unlikely(fill_vec_buf_packed(dev, vq,
1458 vq->last_avail_idx, &desc_count,
1460 &buf_id, &dummy_len,
1461 VHOST_ACCESS_RO) < 0))
1464 if (likely(dev->dequeue_zero_copy == 0))
1465 update_shadow_used_ring_packed(vq, buf_id, 0,
1468 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
1469 if (unlikely(pkts[i] == NULL)) {
1470 RTE_LOG(ERR, VHOST_DATA,
1471 "Failed to allocate memory for mbuf.\n");
1475 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
1477 if (unlikely(err)) {
1478 rte_pktmbuf_free(pkts[i]);
1482 if (unlikely(dev->dequeue_zero_copy)) {
1483 struct zcopy_mbuf *zmbuf;
1485 zmbuf = get_zmbuf(vq);
1487 rte_pktmbuf_free(pkts[i]);
1490 zmbuf->mbuf = pkts[i];
1491 zmbuf->desc_idx = buf_id;
1492 zmbuf->desc_count = desc_count;
1495 * Pin lock the mbuf; we will check later to see
1496 * whether the mbuf is freed (when we are the last
1497 * user) or not. If that's the case, we then could
1498 * update the used ring safely.
1500 rte_mbuf_refcnt_update(pkts[i], 1);
1503 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1506 vq->last_avail_idx += desc_count;
1507 if (vq->last_avail_idx >= vq->size) {
1508 vq->last_avail_idx -= vq->size;
1509 vq->avail_wrap_counter ^= 1;
1513 if (likely(dev->dequeue_zero_copy == 0)) {
1514 do_data_copy_dequeue(vq);
1515 if (unlikely(i < count))
1516 vq->shadow_used_idx = i;
1517 if (likely(vq->shadow_used_idx)) {
1518 flush_shadow_used_ring_packed(dev, vq);
1519 vhost_vring_call_packed(dev, vq);
1527 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
1528 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1530 struct virtio_net *dev;
1531 struct rte_mbuf *rarp_mbuf = NULL;
1532 struct vhost_virtqueue *vq;
1534 dev = get_device(vid);
1538 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1539 RTE_LOG(ERR, VHOST_DATA,
1540 "(%d) %s: built-in vhost net backend is disabled.\n",
1541 dev->vid, __func__);
1545 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
1546 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
1547 dev->vid, __func__, queue_id);
1551 vq = dev->virtqueue[queue_id];
1553 if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
1556 if (unlikely(vq->enabled == 0)) {
1558 goto out_access_unlock;
1561 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1562 vhost_user_iotlb_rd_lock(vq);
1564 if (unlikely(vq->access_ok == 0))
1565 if (unlikely(vring_translate(dev, vq) < 0)) {
1571 * Construct a RARP broadcast packet, and inject it to the "pkts"
1572 * array, to looks like that guest actually send such packet.
1574 * Check user_send_rarp() for more information.
1576 * broadcast_rarp shares a cacheline in the virtio_net structure
1577 * with some fields that are accessed during enqueue and
1578 * rte_atomic16_cmpset() causes a write if using cmpxchg. This could
1579 * result in false sharing between enqueue and dequeue.
1581 * Prevent unnecessary false sharing by reading broadcast_rarp first
1582 * and only performing cmpset if the read indicates it is likely to
1585 if (unlikely(rte_atomic16_read(&dev->broadcast_rarp) &&
1586 rte_atomic16_cmpset((volatile uint16_t *)
1587 &dev->broadcast_rarp.cnt, 1, 0))) {
1589 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
1590 if (rarp_mbuf == NULL) {
1591 RTE_LOG(ERR, VHOST_DATA,
1592 "Failed to make RARP packet.\n");
1599 if (vq_is_packed(dev))
1600 count = virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count);
1602 count = virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count);
1605 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1606 vhost_user_iotlb_rd_unlock(vq);
1609 rte_spinlock_unlock(&vq->access_lock);
1611 if (unlikely(rarp_mbuf != NULL)) {
1613 * Inject it to the head of "pkts" array, so that switch's mac
1614 * learning table will get updated first.
1616 memmove(&pkts[1], pkts, count * sizeof(struct rte_mbuf *));
1617 pkts[0] = rarp_mbuf;