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;
113 for (i = 0; i < vq->shadow_used_idx; i++) {
116 if (vq->shadow_used_packed[i].len)
117 flags = VRING_DESC_F_WRITE;
121 if (vq->used_wrap_counter) {
122 flags |= VRING_DESC_F_USED;
123 flags |= VRING_DESC_F_AVAIL;
125 flags &= ~VRING_DESC_F_USED;
126 flags &= ~VRING_DESC_F_AVAIL;
130 vq->desc_packed[vq->last_used_idx].flags = flags;
132 vhost_log_cache_used_vring(dev, vq,
134 sizeof(struct vring_packed_desc),
135 sizeof(struct vring_packed_desc));
137 head_idx = vq->last_used_idx;
141 vq->last_used_idx += vq->shadow_used_packed[i].count;
142 if (vq->last_used_idx >= vq->size) {
143 vq->used_wrap_counter ^= 1;
144 vq->last_used_idx -= vq->size;
148 __atomic_store_n(&vq->desc_packed[head_idx].flags, head_flags,
151 vhost_log_cache_used_vring(dev, vq,
153 sizeof(struct vring_packed_desc),
154 sizeof(struct vring_packed_desc));
156 vq->shadow_used_idx = 0;
157 vhost_log_cache_sync(dev, vq);
160 static __rte_always_inline void
161 update_shadow_used_ring_packed(struct vhost_virtqueue *vq,
162 uint16_t desc_idx, uint32_t len, uint16_t count)
164 uint16_t i = vq->shadow_used_idx++;
166 vq->shadow_used_packed[i].id = desc_idx;
167 vq->shadow_used_packed[i].len = len;
168 vq->shadow_used_packed[i].count = count;
172 do_data_copy_enqueue(struct virtio_net *dev, struct vhost_virtqueue *vq)
174 struct batch_copy_elem *elem = vq->batch_copy_elems;
175 uint16_t count = vq->batch_copy_nb_elems;
178 for (i = 0; i < count; i++) {
179 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
180 vhost_log_cache_write(dev, vq, elem[i].log_addr, elem[i].len);
181 PRINT_PACKET(dev, (uintptr_t)elem[i].dst, elem[i].len, 0);
184 vq->batch_copy_nb_elems = 0;
188 do_data_copy_dequeue(struct vhost_virtqueue *vq)
190 struct batch_copy_elem *elem = vq->batch_copy_elems;
191 uint16_t count = vq->batch_copy_nb_elems;
194 for (i = 0; i < count; i++)
195 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
197 vq->batch_copy_nb_elems = 0;
200 /* avoid write operation when necessary, to lessen cache issues */
201 #define ASSIGN_UNLESS_EQUAL(var, val) do { \
202 if ((var) != (val)) \
206 static __rte_always_inline void
207 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
209 uint64_t csum_l4 = m_buf->ol_flags & PKT_TX_L4_MASK;
211 if (m_buf->ol_flags & PKT_TX_TCP_SEG)
212 csum_l4 |= PKT_TX_TCP_CKSUM;
215 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
216 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
219 case PKT_TX_TCP_CKSUM:
220 net_hdr->csum_offset = (offsetof(struct rte_tcp_hdr,
223 case PKT_TX_UDP_CKSUM:
224 net_hdr->csum_offset = (offsetof(struct rte_udp_hdr,
227 case PKT_TX_SCTP_CKSUM:
228 net_hdr->csum_offset = (offsetof(struct rte_sctp_hdr,
233 ASSIGN_UNLESS_EQUAL(net_hdr->csum_start, 0);
234 ASSIGN_UNLESS_EQUAL(net_hdr->csum_offset, 0);
235 ASSIGN_UNLESS_EQUAL(net_hdr->flags, 0);
238 /* IP cksum verification cannot be bypassed, then calculate here */
239 if (m_buf->ol_flags & PKT_TX_IP_CKSUM) {
240 struct rte_ipv4_hdr *ipv4_hdr;
242 ipv4_hdr = rte_pktmbuf_mtod_offset(m_buf, struct rte_ipv4_hdr *,
244 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
247 if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
248 if (m_buf->ol_flags & PKT_TX_IPV4)
249 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
251 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
252 net_hdr->gso_size = m_buf->tso_segsz;
253 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
255 } else if (m_buf->ol_flags & PKT_TX_UDP_SEG) {
256 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
257 net_hdr->gso_size = m_buf->tso_segsz;
258 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len +
261 ASSIGN_UNLESS_EQUAL(net_hdr->gso_type, 0);
262 ASSIGN_UNLESS_EQUAL(net_hdr->gso_size, 0);
263 ASSIGN_UNLESS_EQUAL(net_hdr->hdr_len, 0);
267 static __rte_always_inline int
268 map_one_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
269 struct buf_vector *buf_vec, uint16_t *vec_idx,
270 uint64_t desc_iova, uint64_t desc_len, uint8_t perm)
272 uint16_t vec_id = *vec_idx;
276 uint64_t desc_chunck_len = desc_len;
278 if (unlikely(vec_id >= BUF_VECTOR_MAX))
281 desc_addr = vhost_iova_to_vva(dev, vq,
285 if (unlikely(!desc_addr))
288 rte_prefetch0((void *)(uintptr_t)desc_addr);
290 buf_vec[vec_id].buf_iova = desc_iova;
291 buf_vec[vec_id].buf_addr = desc_addr;
292 buf_vec[vec_id].buf_len = desc_chunck_len;
294 desc_len -= desc_chunck_len;
295 desc_iova += desc_chunck_len;
303 static __rte_always_inline int
304 fill_vec_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
305 uint32_t avail_idx, uint16_t *vec_idx,
306 struct buf_vector *buf_vec, uint16_t *desc_chain_head,
307 uint32_t *desc_chain_len, uint8_t perm)
309 uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
310 uint16_t vec_id = *vec_idx;
313 uint32_t nr_descs = vq->size;
315 struct vring_desc *descs = vq->desc;
316 struct vring_desc *idesc = NULL;
318 if (unlikely(idx >= vq->size))
321 *desc_chain_head = idx;
323 if (vq->desc[idx].flags & VRING_DESC_F_INDIRECT) {
324 dlen = vq->desc[idx].len;
325 nr_descs = dlen / sizeof(struct vring_desc);
326 if (unlikely(nr_descs > vq->size))
329 descs = (struct vring_desc *)(uintptr_t)
330 vhost_iova_to_vva(dev, vq, vq->desc[idx].addr,
333 if (unlikely(!descs))
336 if (unlikely(dlen < vq->desc[idx].len)) {
338 * The indirect desc table is not contiguous
339 * in process VA space, we have to copy it.
341 idesc = vhost_alloc_copy_ind_table(dev, vq,
342 vq->desc[idx].addr, vq->desc[idx].len);
343 if (unlikely(!idesc))
353 if (unlikely(idx >= nr_descs || cnt++ >= nr_descs)) {
354 free_ind_table(idesc);
358 len += descs[idx].len;
360 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
361 descs[idx].addr, descs[idx].len,
363 free_ind_table(idesc);
367 if ((descs[idx].flags & VRING_DESC_F_NEXT) == 0)
370 idx = descs[idx].next;
373 *desc_chain_len = len;
376 if (unlikely(!!idesc))
377 free_ind_table(idesc);
383 * Returns -1 on fail, 0 on success
386 reserve_avail_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
387 uint32_t size, struct buf_vector *buf_vec,
388 uint16_t *num_buffers, uint16_t avail_head,
392 uint16_t vec_idx = 0;
393 uint16_t max_tries, tries = 0;
395 uint16_t head_idx = 0;
399 cur_idx = vq->last_avail_idx;
401 if (rxvq_is_mergeable(dev))
402 max_tries = vq->size - 1;
407 if (unlikely(cur_idx == avail_head))
410 * if we tried all available ring items, and still
411 * can't get enough buf, it means something abnormal
414 if (unlikely(++tries > max_tries))
417 if (unlikely(fill_vec_buf_split(dev, vq, cur_idx,
420 VHOST_ACCESS_RW) < 0))
422 len = RTE_MIN(len, size);
423 update_shadow_used_ring_split(vq, head_idx, len);
435 static __rte_always_inline int
436 fill_vec_buf_packed_indirect(struct virtio_net *dev,
437 struct vhost_virtqueue *vq,
438 struct vring_packed_desc *desc, uint16_t *vec_idx,
439 struct buf_vector *buf_vec, uint32_t *len, uint8_t perm)
443 uint16_t vec_id = *vec_idx;
445 struct vring_packed_desc *descs, *idescs = NULL;
448 descs = (struct vring_packed_desc *)(uintptr_t)
449 vhost_iova_to_vva(dev, vq, desc->addr, &dlen, VHOST_ACCESS_RO);
450 if (unlikely(!descs))
453 if (unlikely(dlen < desc->len)) {
455 * The indirect desc table is not contiguous
456 * in process VA space, we have to copy it.
458 idescs = vhost_alloc_copy_ind_table(dev,
459 vq, desc->addr, desc->len);
460 if (unlikely(!idescs))
466 nr_descs = desc->len / sizeof(struct vring_packed_desc);
467 if (unlikely(nr_descs >= vq->size)) {
468 free_ind_table(idescs);
472 for (i = 0; i < nr_descs; i++) {
473 if (unlikely(vec_id >= BUF_VECTOR_MAX)) {
474 free_ind_table(idescs);
478 *len += descs[i].len;
479 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
480 descs[i].addr, descs[i].len,
486 if (unlikely(!!idescs))
487 free_ind_table(idescs);
492 static __rte_always_inline int
493 fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
494 uint16_t avail_idx, uint16_t *desc_count,
495 struct buf_vector *buf_vec, uint16_t *vec_idx,
496 uint16_t *buf_id, uint32_t *len, uint8_t perm)
498 bool wrap_counter = vq->avail_wrap_counter;
499 struct vring_packed_desc *descs = vq->desc_packed;
500 uint16_t vec_id = *vec_idx;
502 if (avail_idx < vq->last_avail_idx)
506 * Perform a load-acquire barrier in desc_is_avail to
507 * enforce the ordering between desc flags and desc
510 if (unlikely(!desc_is_avail(&descs[avail_idx], wrap_counter)))
517 if (unlikely(vec_id >= BUF_VECTOR_MAX))
520 if (unlikely(*desc_count >= vq->size))
524 *buf_id = descs[avail_idx].id;
526 if (descs[avail_idx].flags & VRING_DESC_F_INDIRECT) {
527 if (unlikely(fill_vec_buf_packed_indirect(dev, vq,
533 *len += descs[avail_idx].len;
535 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
536 descs[avail_idx].addr,
537 descs[avail_idx].len,
542 if ((descs[avail_idx].flags & VRING_DESC_F_NEXT) == 0)
545 if (++avail_idx >= vq->size) {
546 avail_idx -= vq->size;
557 * Returns -1 on fail, 0 on success
560 reserve_avail_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
561 uint32_t size, struct buf_vector *buf_vec,
562 uint16_t *nr_vec, uint16_t *num_buffers,
566 uint16_t vec_idx = 0;
567 uint16_t max_tries, tries = 0;
574 avail_idx = vq->last_avail_idx;
576 if (rxvq_is_mergeable(dev))
577 max_tries = vq->size - 1;
583 * if we tried all available ring items, and still
584 * can't get enough buf, it means something abnormal
587 if (unlikely(++tries > max_tries))
590 if (unlikely(fill_vec_buf_packed(dev, vq,
591 avail_idx, &desc_count,
594 VHOST_ACCESS_RW) < 0))
597 len = RTE_MIN(len, size);
598 update_shadow_used_ring_packed(vq, buf_id, len, desc_count);
601 avail_idx += desc_count;
602 if (avail_idx >= vq->size)
603 avail_idx -= vq->size;
605 *nr_descs += desc_count;
614 static __rte_noinline void
615 copy_vnet_hdr_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
616 struct buf_vector *buf_vec,
617 struct virtio_net_hdr_mrg_rxbuf *hdr)
620 uint64_t remain = dev->vhost_hlen;
621 uint64_t src = (uint64_t)(uintptr_t)hdr, dst;
622 uint64_t iova = buf_vec->buf_iova;
625 len = RTE_MIN(remain,
627 dst = buf_vec->buf_addr;
628 rte_memcpy((void *)(uintptr_t)dst,
629 (void *)(uintptr_t)src,
632 PRINT_PACKET(dev, (uintptr_t)dst,
634 vhost_log_cache_write(dev, vq,
644 static __rte_always_inline int
645 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
646 struct rte_mbuf *m, struct buf_vector *buf_vec,
647 uint16_t nr_vec, uint16_t num_buffers)
649 uint32_t vec_idx = 0;
650 uint32_t mbuf_offset, mbuf_avail;
651 uint32_t buf_offset, buf_avail;
652 uint64_t buf_addr, buf_iova, buf_len;
655 struct rte_mbuf *hdr_mbuf;
656 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
657 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
660 if (unlikely(m == NULL)) {
665 buf_addr = buf_vec[vec_idx].buf_addr;
666 buf_iova = buf_vec[vec_idx].buf_iova;
667 buf_len = buf_vec[vec_idx].buf_len;
669 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
676 if (unlikely(buf_len < dev->vhost_hlen))
679 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
681 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) RX: num merge buffers %d\n",
682 dev->vid, num_buffers);
684 if (unlikely(buf_len < dev->vhost_hlen)) {
685 buf_offset = dev->vhost_hlen - buf_len;
687 buf_addr = buf_vec[vec_idx].buf_addr;
688 buf_iova = buf_vec[vec_idx].buf_iova;
689 buf_len = buf_vec[vec_idx].buf_len;
690 buf_avail = buf_len - buf_offset;
692 buf_offset = dev->vhost_hlen;
693 buf_avail = buf_len - dev->vhost_hlen;
696 mbuf_avail = rte_pktmbuf_data_len(m);
698 while (mbuf_avail != 0 || m->next != NULL) {
699 /* done with current buf, get the next one */
700 if (buf_avail == 0) {
702 if (unlikely(vec_idx >= nr_vec)) {
707 buf_addr = buf_vec[vec_idx].buf_addr;
708 buf_iova = buf_vec[vec_idx].buf_iova;
709 buf_len = buf_vec[vec_idx].buf_len;
715 /* done with current mbuf, get the next one */
716 if (mbuf_avail == 0) {
720 mbuf_avail = rte_pktmbuf_data_len(m);
724 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
725 if (rxvq_is_mergeable(dev))
726 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
729 if (unlikely(hdr == &tmp_hdr)) {
730 copy_vnet_hdr_to_desc(dev, vq, buf_vec, hdr);
732 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
734 vhost_log_cache_write(dev, vq,
742 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
744 if (likely(cpy_len > MAX_BATCH_LEN ||
745 vq->batch_copy_nb_elems >= vq->size)) {
746 rte_memcpy((void *)((uintptr_t)(buf_addr + buf_offset)),
747 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
749 vhost_log_cache_write(dev, vq, buf_iova + buf_offset,
751 PRINT_PACKET(dev, (uintptr_t)(buf_addr + buf_offset),
754 batch_copy[vq->batch_copy_nb_elems].dst =
755 (void *)((uintptr_t)(buf_addr + buf_offset));
756 batch_copy[vq->batch_copy_nb_elems].src =
757 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
758 batch_copy[vq->batch_copy_nb_elems].log_addr =
759 buf_iova + buf_offset;
760 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
761 vq->batch_copy_nb_elems++;
764 mbuf_avail -= cpy_len;
765 mbuf_offset += cpy_len;
766 buf_avail -= cpy_len;
767 buf_offset += cpy_len;
775 static __rte_noinline uint32_t
776 virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
777 struct rte_mbuf **pkts, uint32_t count)
779 uint32_t pkt_idx = 0;
780 uint16_t num_buffers;
781 struct buf_vector buf_vec[BUF_VECTOR_MAX];
784 avail_head = *((volatile uint16_t *)&vq->avail->idx);
787 * The ordering between avail index and
788 * desc reads needs to be enforced.
792 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
794 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
795 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
798 if (unlikely(reserve_avail_buf_split(dev, vq,
799 pkt_len, buf_vec, &num_buffers,
800 avail_head, &nr_vec) < 0)) {
801 VHOST_LOG_DEBUG(VHOST_DATA,
802 "(%d) failed to get enough desc from vring\n",
804 vq->shadow_used_idx -= num_buffers;
808 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
809 dev->vid, vq->last_avail_idx,
810 vq->last_avail_idx + num_buffers);
812 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
815 vq->shadow_used_idx -= num_buffers;
819 vq->last_avail_idx += num_buffers;
822 do_data_copy_enqueue(dev, vq);
824 if (likely(vq->shadow_used_idx)) {
825 flush_shadow_used_ring_split(dev, vq);
826 vhost_vring_call_split(dev, vq);
832 static __rte_noinline uint32_t
833 virtio_dev_rx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
834 struct rte_mbuf **pkts, uint32_t count)
836 uint32_t pkt_idx = 0;
837 uint16_t num_buffers;
838 struct buf_vector buf_vec[BUF_VECTOR_MAX];
840 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
841 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
843 uint16_t nr_descs = 0;
845 if (unlikely(reserve_avail_buf_packed(dev, vq,
846 pkt_len, buf_vec, &nr_vec,
847 &num_buffers, &nr_descs) < 0)) {
848 VHOST_LOG_DEBUG(VHOST_DATA,
849 "(%d) failed to get enough desc from vring\n",
851 vq->shadow_used_idx -= num_buffers;
855 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
856 dev->vid, vq->last_avail_idx,
857 vq->last_avail_idx + num_buffers);
859 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
862 vq->shadow_used_idx -= num_buffers;
866 vq->last_avail_idx += nr_descs;
867 if (vq->last_avail_idx >= vq->size) {
868 vq->last_avail_idx -= vq->size;
869 vq->avail_wrap_counter ^= 1;
873 do_data_copy_enqueue(dev, vq);
875 if (likely(vq->shadow_used_idx)) {
876 flush_shadow_used_ring_packed(dev, vq);
877 vhost_vring_call_packed(dev, vq);
883 static __rte_always_inline uint32_t
884 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
885 struct rte_mbuf **pkts, uint32_t count)
887 struct vhost_virtqueue *vq;
890 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
891 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
892 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
893 dev->vid, __func__, queue_id);
897 vq = dev->virtqueue[queue_id];
899 rte_spinlock_lock(&vq->access_lock);
901 if (unlikely(vq->enabled == 0))
902 goto out_access_unlock;
904 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
905 vhost_user_iotlb_rd_lock(vq);
907 if (unlikely(vq->access_ok == 0))
908 if (unlikely(vring_translate(dev, vq) < 0))
911 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
915 if (vq_is_packed(dev))
916 nb_tx = virtio_dev_rx_packed(dev, vq, pkts, count);
918 nb_tx = virtio_dev_rx_split(dev, vq, pkts, count);
921 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
922 vhost_user_iotlb_rd_unlock(vq);
925 rte_spinlock_unlock(&vq->access_lock);
931 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
932 struct rte_mbuf **pkts, uint16_t count)
934 struct virtio_net *dev = get_device(vid);
939 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
940 RTE_LOG(ERR, VHOST_DATA,
941 "(%d) %s: built-in vhost net backend is disabled.\n",
946 return virtio_dev_rx(dev, queue_id, pkts, count);
950 virtio_net_with_host_offload(struct virtio_net *dev)
953 ((1ULL << VIRTIO_NET_F_CSUM) |
954 (1ULL << VIRTIO_NET_F_HOST_ECN) |
955 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
956 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
957 (1ULL << VIRTIO_NET_F_HOST_UFO)))
964 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
966 struct rte_ipv4_hdr *ipv4_hdr;
967 struct rte_ipv6_hdr *ipv6_hdr;
969 struct rte_ether_hdr *eth_hdr;
972 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
974 m->l2_len = sizeof(struct rte_ether_hdr);
975 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
977 if (ethertype == RTE_ETHER_TYPE_VLAN) {
978 struct rte_vlan_hdr *vlan_hdr =
979 (struct rte_vlan_hdr *)(eth_hdr + 1);
981 m->l2_len += sizeof(struct rte_vlan_hdr);
982 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
985 l3_hdr = (char *)eth_hdr + m->l2_len;
988 case RTE_ETHER_TYPE_IPV4:
990 *l4_proto = ipv4_hdr->next_proto_id;
991 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
992 *l4_hdr = (char *)l3_hdr + m->l3_len;
993 m->ol_flags |= PKT_TX_IPV4;
995 case RTE_ETHER_TYPE_IPV6:
997 *l4_proto = ipv6_hdr->proto;
998 m->l3_len = sizeof(struct rte_ipv6_hdr);
999 *l4_hdr = (char *)l3_hdr + m->l3_len;
1000 m->ol_flags |= PKT_TX_IPV6;
1010 static __rte_always_inline void
1011 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
1013 uint16_t l4_proto = 0;
1014 void *l4_hdr = NULL;
1015 struct rte_tcp_hdr *tcp_hdr = NULL;
1017 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
1020 parse_ethernet(m, &l4_proto, &l4_hdr);
1021 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1022 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
1023 switch (hdr->csum_offset) {
1024 case (offsetof(struct rte_tcp_hdr, cksum)):
1025 if (l4_proto == IPPROTO_TCP)
1026 m->ol_flags |= PKT_TX_TCP_CKSUM;
1028 case (offsetof(struct rte_udp_hdr, dgram_cksum)):
1029 if (l4_proto == IPPROTO_UDP)
1030 m->ol_flags |= PKT_TX_UDP_CKSUM;
1032 case (offsetof(struct rte_sctp_hdr, cksum)):
1033 if (l4_proto == IPPROTO_SCTP)
1034 m->ol_flags |= PKT_TX_SCTP_CKSUM;
1042 if (l4_hdr && hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1043 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1044 case VIRTIO_NET_HDR_GSO_TCPV4:
1045 case VIRTIO_NET_HDR_GSO_TCPV6:
1047 m->ol_flags |= PKT_TX_TCP_SEG;
1048 m->tso_segsz = hdr->gso_size;
1049 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
1051 case VIRTIO_NET_HDR_GSO_UDP:
1052 m->ol_flags |= PKT_TX_UDP_SEG;
1053 m->tso_segsz = hdr->gso_size;
1054 m->l4_len = sizeof(struct rte_udp_hdr);
1057 RTE_LOG(WARNING, VHOST_DATA,
1058 "unsupported gso type %u.\n", hdr->gso_type);
1064 static __rte_noinline void
1065 copy_vnet_hdr_from_desc(struct virtio_net_hdr *hdr,
1066 struct buf_vector *buf_vec)
1069 uint64_t remain = sizeof(struct virtio_net_hdr);
1071 uint64_t dst = (uint64_t)(uintptr_t)hdr;
1074 len = RTE_MIN(remain, buf_vec->buf_len);
1075 src = buf_vec->buf_addr;
1076 rte_memcpy((void *)(uintptr_t)dst,
1077 (void *)(uintptr_t)src, len);
1085 static __rte_always_inline int
1086 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
1087 struct buf_vector *buf_vec, uint16_t nr_vec,
1088 struct rte_mbuf *m, struct rte_mempool *mbuf_pool)
1090 uint32_t buf_avail, buf_offset;
1091 uint64_t buf_addr, buf_iova, buf_len;
1092 uint32_t mbuf_avail, mbuf_offset;
1094 struct rte_mbuf *cur = m, *prev = m;
1095 struct virtio_net_hdr tmp_hdr;
1096 struct virtio_net_hdr *hdr = NULL;
1097 /* A counter to avoid desc dead loop chain */
1098 uint16_t vec_idx = 0;
1099 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
1102 buf_addr = buf_vec[vec_idx].buf_addr;
1103 buf_iova = buf_vec[vec_idx].buf_iova;
1104 buf_len = buf_vec[vec_idx].buf_len;
1106 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
1111 if (virtio_net_with_host_offload(dev)) {
1112 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
1114 * No luck, the virtio-net header doesn't fit
1115 * in a contiguous virtual area.
1117 copy_vnet_hdr_from_desc(&tmp_hdr, buf_vec);
1120 hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
1125 * A virtio driver normally uses at least 2 desc buffers
1126 * for Tx: the first for storing the header, and others
1127 * for storing the data.
1129 if (unlikely(buf_len < dev->vhost_hlen)) {
1130 buf_offset = dev->vhost_hlen - buf_len;
1132 buf_addr = buf_vec[vec_idx].buf_addr;
1133 buf_iova = buf_vec[vec_idx].buf_iova;
1134 buf_len = buf_vec[vec_idx].buf_len;
1135 buf_avail = buf_len - buf_offset;
1136 } else if (buf_len == dev->vhost_hlen) {
1137 if (unlikely(++vec_idx >= nr_vec))
1139 buf_addr = buf_vec[vec_idx].buf_addr;
1140 buf_iova = buf_vec[vec_idx].buf_iova;
1141 buf_len = buf_vec[vec_idx].buf_len;
1144 buf_avail = buf_len;
1146 buf_offset = dev->vhost_hlen;
1147 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
1151 (uintptr_t)(buf_addr + buf_offset),
1152 (uint32_t)buf_avail, 0);
1155 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
1159 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1162 * A desc buf might across two host physical pages that are
1163 * not continuous. In such case (gpa_to_hpa returns 0), data
1164 * will be copied even though zero copy is enabled.
1166 if (unlikely(dev->dequeue_zero_copy && (hpa = gpa_to_hpa(dev,
1167 buf_iova + buf_offset, cpy_len)))) {
1168 cur->data_len = cpy_len;
1171 (void *)(uintptr_t)(buf_addr + buf_offset);
1172 cur->buf_iova = hpa;
1175 * In zero copy mode, one mbuf can only reference data
1176 * for one or partial of one desc buff.
1178 mbuf_avail = cpy_len;
1180 if (likely(cpy_len > MAX_BATCH_LEN ||
1181 vq->batch_copy_nb_elems >= vq->size ||
1182 (hdr && cur == m))) {
1183 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
1185 (void *)((uintptr_t)(buf_addr +
1189 batch_copy[vq->batch_copy_nb_elems].dst =
1190 rte_pktmbuf_mtod_offset(cur, void *,
1192 batch_copy[vq->batch_copy_nb_elems].src =
1193 (void *)((uintptr_t)(buf_addr +
1195 batch_copy[vq->batch_copy_nb_elems].len =
1197 vq->batch_copy_nb_elems++;
1201 mbuf_avail -= cpy_len;
1202 mbuf_offset += cpy_len;
1203 buf_avail -= cpy_len;
1204 buf_offset += cpy_len;
1206 /* This buf reaches to its end, get the next one */
1207 if (buf_avail == 0) {
1208 if (++vec_idx >= nr_vec)
1211 buf_addr = buf_vec[vec_idx].buf_addr;
1212 buf_iova = buf_vec[vec_idx].buf_iova;
1213 buf_len = buf_vec[vec_idx].buf_len;
1216 buf_avail = buf_len;
1218 PRINT_PACKET(dev, (uintptr_t)buf_addr,
1219 (uint32_t)buf_avail, 0);
1223 * This mbuf reaches to its end, get a new one
1224 * to hold more data.
1226 if (mbuf_avail == 0) {
1227 cur = rte_pktmbuf_alloc(mbuf_pool);
1228 if (unlikely(cur == NULL)) {
1229 RTE_LOG(ERR, VHOST_DATA, "Failed to "
1230 "allocate memory for mbuf.\n");
1234 if (unlikely(dev->dequeue_zero_copy))
1235 rte_mbuf_refcnt_update(cur, 1);
1238 prev->data_len = mbuf_offset;
1240 m->pkt_len += mbuf_offset;
1244 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
1248 prev->data_len = mbuf_offset;
1249 m->pkt_len += mbuf_offset;
1252 vhost_dequeue_offload(hdr, m);
1259 static __rte_always_inline struct zcopy_mbuf *
1260 get_zmbuf(struct vhost_virtqueue *vq)
1266 /* search [last_zmbuf_idx, zmbuf_size) */
1267 i = vq->last_zmbuf_idx;
1268 last = vq->zmbuf_size;
1271 for (; i < last; i++) {
1272 if (vq->zmbufs[i].in_use == 0) {
1273 vq->last_zmbuf_idx = i + 1;
1274 vq->zmbufs[i].in_use = 1;
1275 return &vq->zmbufs[i];
1281 /* search [0, last_zmbuf_idx) */
1283 last = vq->last_zmbuf_idx;
1290 static __rte_noinline uint16_t
1291 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1292 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1295 uint16_t free_entries;
1297 if (unlikely(dev->dequeue_zero_copy)) {
1298 struct zcopy_mbuf *zmbuf, *next;
1300 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1301 zmbuf != NULL; zmbuf = next) {
1302 next = TAILQ_NEXT(zmbuf, next);
1304 if (mbuf_is_consumed(zmbuf->mbuf)) {
1305 update_shadow_used_ring_split(vq,
1306 zmbuf->desc_idx, 0);
1307 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1308 restore_mbuf(zmbuf->mbuf);
1309 rte_pktmbuf_free(zmbuf->mbuf);
1315 if (likely(vq->shadow_used_idx)) {
1316 flush_shadow_used_ring_split(dev, vq);
1317 vhost_vring_call_split(dev, vq);
1321 free_entries = *((volatile uint16_t *)&vq->avail->idx) -
1323 if (free_entries == 0)
1327 * The ordering between avail index and
1328 * desc reads needs to be enforced.
1332 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1334 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1336 count = RTE_MIN(count, MAX_PKT_BURST);
1337 count = RTE_MIN(count, free_entries);
1338 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1341 for (i = 0; i < count; i++) {
1342 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1345 uint16_t nr_vec = 0;
1348 if (unlikely(fill_vec_buf_split(dev, vq,
1349 vq->last_avail_idx + i,
1351 &head_idx, &dummy_len,
1352 VHOST_ACCESS_RO) < 0))
1355 if (likely(dev->dequeue_zero_copy == 0))
1356 update_shadow_used_ring_split(vq, head_idx, 0);
1358 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
1359 if (unlikely(pkts[i] == NULL)) {
1360 RTE_LOG(ERR, VHOST_DATA,
1361 "Failed to allocate memory for mbuf.\n");
1365 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
1367 if (unlikely(err)) {
1368 rte_pktmbuf_free(pkts[i]);
1372 if (unlikely(dev->dequeue_zero_copy)) {
1373 struct zcopy_mbuf *zmbuf;
1375 zmbuf = get_zmbuf(vq);
1377 rte_pktmbuf_free(pkts[i]);
1380 zmbuf->mbuf = pkts[i];
1381 zmbuf->desc_idx = head_idx;
1384 * Pin lock the mbuf; we will check later to see
1385 * whether the mbuf is freed (when we are the last
1386 * user) or not. If that's the case, we then could
1387 * update the used ring safely.
1389 rte_mbuf_refcnt_update(pkts[i], 1);
1392 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1395 vq->last_avail_idx += i;
1397 if (likely(dev->dequeue_zero_copy == 0)) {
1398 do_data_copy_dequeue(vq);
1399 if (unlikely(i < count))
1400 vq->shadow_used_idx = i;
1401 if (likely(vq->shadow_used_idx)) {
1402 flush_shadow_used_ring_split(dev, vq);
1403 vhost_vring_call_split(dev, vq);
1410 static __rte_noinline uint16_t
1411 virtio_dev_tx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
1412 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1416 if (unlikely(dev->dequeue_zero_copy)) {
1417 struct zcopy_mbuf *zmbuf, *next;
1419 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1420 zmbuf != NULL; zmbuf = next) {
1421 next = TAILQ_NEXT(zmbuf, next);
1423 if (mbuf_is_consumed(zmbuf->mbuf)) {
1424 update_shadow_used_ring_packed(vq,
1429 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1430 restore_mbuf(zmbuf->mbuf);
1431 rte_pktmbuf_free(zmbuf->mbuf);
1437 if (likely(vq->shadow_used_idx)) {
1438 flush_shadow_used_ring_packed(dev, vq);
1439 vhost_vring_call_packed(dev, vq);
1443 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1445 count = RTE_MIN(count, MAX_PKT_BURST);
1446 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1449 for (i = 0; i < count; i++) {
1450 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1453 uint16_t desc_count, nr_vec = 0;
1456 if (unlikely(fill_vec_buf_packed(dev, vq,
1457 vq->last_avail_idx, &desc_count,
1459 &buf_id, &dummy_len,
1460 VHOST_ACCESS_RO) < 0))
1463 if (likely(dev->dequeue_zero_copy == 0))
1464 update_shadow_used_ring_packed(vq, buf_id, 0,
1467 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
1468 if (unlikely(pkts[i] == NULL)) {
1469 RTE_LOG(ERR, VHOST_DATA,
1470 "Failed to allocate memory for mbuf.\n");
1474 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
1476 if (unlikely(err)) {
1477 rte_pktmbuf_free(pkts[i]);
1481 if (unlikely(dev->dequeue_zero_copy)) {
1482 struct zcopy_mbuf *zmbuf;
1484 zmbuf = get_zmbuf(vq);
1486 rte_pktmbuf_free(pkts[i]);
1489 zmbuf->mbuf = pkts[i];
1490 zmbuf->desc_idx = buf_id;
1491 zmbuf->desc_count = desc_count;
1494 * Pin lock the mbuf; we will check later to see
1495 * whether the mbuf is freed (when we are the last
1496 * user) or not. If that's the case, we then could
1497 * update the used ring safely.
1499 rte_mbuf_refcnt_update(pkts[i], 1);
1502 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1505 vq->last_avail_idx += desc_count;
1506 if (vq->last_avail_idx >= vq->size) {
1507 vq->last_avail_idx -= vq->size;
1508 vq->avail_wrap_counter ^= 1;
1512 if (likely(dev->dequeue_zero_copy == 0)) {
1513 do_data_copy_dequeue(vq);
1514 if (unlikely(i < count))
1515 vq->shadow_used_idx = i;
1516 if (likely(vq->shadow_used_idx)) {
1517 flush_shadow_used_ring_packed(dev, vq);
1518 vhost_vring_call_packed(dev, vq);
1526 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
1527 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1529 struct virtio_net *dev;
1530 struct rte_mbuf *rarp_mbuf = NULL;
1531 struct vhost_virtqueue *vq;
1533 dev = get_device(vid);
1537 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1538 RTE_LOG(ERR, VHOST_DATA,
1539 "(%d) %s: built-in vhost net backend is disabled.\n",
1540 dev->vid, __func__);
1544 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
1545 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
1546 dev->vid, __func__, queue_id);
1550 vq = dev->virtqueue[queue_id];
1552 if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
1555 if (unlikely(vq->enabled == 0)) {
1557 goto out_access_unlock;
1560 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1561 vhost_user_iotlb_rd_lock(vq);
1563 if (unlikely(vq->access_ok == 0))
1564 if (unlikely(vring_translate(dev, vq) < 0)) {
1570 * Construct a RARP broadcast packet, and inject it to the "pkts"
1571 * array, to looks like that guest actually send such packet.
1573 * Check user_send_rarp() for more information.
1575 * broadcast_rarp shares a cacheline in the virtio_net structure
1576 * with some fields that are accessed during enqueue and
1577 * rte_atomic16_cmpset() causes a write if using cmpxchg. This could
1578 * result in false sharing between enqueue and dequeue.
1580 * Prevent unnecessary false sharing by reading broadcast_rarp first
1581 * and only performing cmpset if the read indicates it is likely to
1584 if (unlikely(rte_atomic16_read(&dev->broadcast_rarp) &&
1585 rte_atomic16_cmpset((volatile uint16_t *)
1586 &dev->broadcast_rarp.cnt, 1, 0))) {
1588 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
1589 if (rarp_mbuf == NULL) {
1590 RTE_LOG(ERR, VHOST_DATA,
1591 "Failed to make RARP packet.\n");
1598 if (vq_is_packed(dev))
1599 count = virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count);
1601 count = virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count);
1604 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1605 vhost_user_iotlb_rd_unlock(vq);
1608 rte_spinlock_unlock(&vq->access_lock);
1610 if (unlikely(rarp_mbuf != NULL)) {
1612 * Inject it to the head of "pkts" array, so that switch's mac
1613 * learning table will get updated first.
1615 memmove(&pkts[1], pkts, count * sizeof(struct rte_mbuf *));
1616 pkts[0] = rarp_mbuf;