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;
101 /* Split loop in two to save memory barriers */
102 for (i = 0; i < vq->shadow_used_idx; i++) {
103 vq->desc_packed[used_idx].id = vq->shadow_used_packed[i].id;
104 vq->desc_packed[used_idx].len = vq->shadow_used_packed[i].len;
106 used_idx += vq->shadow_used_packed[i].count;
107 if (used_idx >= vq->size)
108 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;
129 vq->desc_packed[vq->last_used_idx].flags = flags;
131 vhost_log_cache_used_vring(dev, vq,
133 sizeof(struct vring_packed_desc),
134 sizeof(struct vring_packed_desc));
136 vq->last_used_idx += vq->shadow_used_packed[i].count;
137 if (vq->last_used_idx >= vq->size) {
138 vq->used_wrap_counter ^= 1;
139 vq->last_used_idx -= vq->size;
144 vq->shadow_used_idx = 0;
145 vhost_log_cache_sync(dev, vq);
148 static __rte_always_inline void
149 update_shadow_used_ring_packed(struct vhost_virtqueue *vq,
150 uint16_t desc_idx, uint32_t len, uint16_t count)
152 uint16_t i = vq->shadow_used_idx++;
154 vq->shadow_used_packed[i].id = desc_idx;
155 vq->shadow_used_packed[i].len = len;
156 vq->shadow_used_packed[i].count = count;
160 do_data_copy_enqueue(struct virtio_net *dev, struct vhost_virtqueue *vq)
162 struct batch_copy_elem *elem = vq->batch_copy_elems;
163 uint16_t count = vq->batch_copy_nb_elems;
166 for (i = 0; i < count; i++) {
167 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
168 vhost_log_cache_write(dev, vq, elem[i].log_addr, elem[i].len);
169 PRINT_PACKET(dev, (uintptr_t)elem[i].dst, elem[i].len, 0);
172 vq->batch_copy_nb_elems = 0;
176 do_data_copy_dequeue(struct vhost_virtqueue *vq)
178 struct batch_copy_elem *elem = vq->batch_copy_elems;
179 uint16_t count = vq->batch_copy_nb_elems;
182 for (i = 0; i < count; i++)
183 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
185 vq->batch_copy_nb_elems = 0;
188 /* avoid write operation when necessary, to lessen cache issues */
189 #define ASSIGN_UNLESS_EQUAL(var, val) do { \
190 if ((var) != (val)) \
194 static __rte_always_inline void
195 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
197 uint64_t csum_l4 = m_buf->ol_flags & PKT_TX_L4_MASK;
199 if (m_buf->ol_flags & PKT_TX_TCP_SEG)
200 csum_l4 |= PKT_TX_TCP_CKSUM;
203 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
204 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
207 case PKT_TX_TCP_CKSUM:
208 net_hdr->csum_offset = (offsetof(struct tcp_hdr,
211 case PKT_TX_UDP_CKSUM:
212 net_hdr->csum_offset = (offsetof(struct udp_hdr,
215 case PKT_TX_SCTP_CKSUM:
216 net_hdr->csum_offset = (offsetof(struct sctp_hdr,
221 ASSIGN_UNLESS_EQUAL(net_hdr->csum_start, 0);
222 ASSIGN_UNLESS_EQUAL(net_hdr->csum_offset, 0);
223 ASSIGN_UNLESS_EQUAL(net_hdr->flags, 0);
226 /* IP cksum verification cannot be bypassed, then calculate here */
227 if (m_buf->ol_flags & PKT_TX_IP_CKSUM) {
228 struct ipv4_hdr *ipv4_hdr;
230 ipv4_hdr = rte_pktmbuf_mtod_offset(m_buf, struct ipv4_hdr *,
232 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
235 if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
236 if (m_buf->ol_flags & PKT_TX_IPV4)
237 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
239 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
240 net_hdr->gso_size = m_buf->tso_segsz;
241 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
243 } else if (m_buf->ol_flags & PKT_TX_UDP_SEG) {
244 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
245 net_hdr->gso_size = m_buf->tso_segsz;
246 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len +
249 ASSIGN_UNLESS_EQUAL(net_hdr->gso_type, 0);
250 ASSIGN_UNLESS_EQUAL(net_hdr->gso_size, 0);
251 ASSIGN_UNLESS_EQUAL(net_hdr->hdr_len, 0);
255 static __rte_always_inline int
256 map_one_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
257 struct buf_vector *buf_vec, uint16_t *vec_idx,
258 uint64_t desc_iova, uint64_t desc_len, uint8_t perm)
260 uint16_t vec_id = *vec_idx;
264 uint64_t desc_chunck_len = desc_len;
266 if (unlikely(vec_id >= BUF_VECTOR_MAX))
269 desc_addr = vhost_iova_to_vva(dev, vq,
273 if (unlikely(!desc_addr))
276 buf_vec[vec_id].buf_iova = desc_iova;
277 buf_vec[vec_id].buf_addr = desc_addr;
278 buf_vec[vec_id].buf_len = desc_chunck_len;
280 desc_len -= desc_chunck_len;
281 desc_iova += desc_chunck_len;
289 static __rte_always_inline int
290 fill_vec_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
291 uint32_t avail_idx, uint16_t *vec_idx,
292 struct buf_vector *buf_vec, uint16_t *desc_chain_head,
293 uint32_t *desc_chain_len, uint8_t perm)
295 uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
296 uint16_t vec_id = *vec_idx;
299 struct vring_desc *descs = vq->desc;
300 struct vring_desc *idesc = NULL;
302 *desc_chain_head = idx;
304 if (vq->desc[idx].flags & VRING_DESC_F_INDIRECT) {
305 dlen = vq->desc[idx].len;
306 descs = (struct vring_desc *)(uintptr_t)
307 vhost_iova_to_vva(dev, vq, vq->desc[idx].addr,
310 if (unlikely(!descs))
313 if (unlikely(dlen < vq->desc[idx].len)) {
315 * The indirect desc table is not contiguous
316 * in process VA space, we have to copy it.
318 idesc = alloc_copy_ind_table(dev, vq,
319 vq->desc[idx].addr, vq->desc[idx].len);
320 if (unlikely(!idesc))
330 if (unlikely(idx >= vq->size)) {
331 free_ind_table(idesc);
335 len += descs[idx].len;
337 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
338 descs[idx].addr, descs[idx].len,
340 free_ind_table(idesc);
344 if ((descs[idx].flags & VRING_DESC_F_NEXT) == 0)
347 idx = descs[idx].next;
350 *desc_chain_len = len;
353 if (unlikely(!!idesc))
354 free_ind_table(idesc);
360 * Returns -1 on fail, 0 on success
363 reserve_avail_buf_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
364 uint32_t size, struct buf_vector *buf_vec,
365 uint16_t *num_buffers, uint16_t avail_head,
369 uint16_t vec_idx = 0;
370 uint16_t max_tries, tries = 0;
372 uint16_t head_idx = 0;
376 cur_idx = vq->last_avail_idx;
378 if (rxvq_is_mergeable(dev))
379 max_tries = vq->size - 1;
384 if (unlikely(cur_idx == avail_head))
387 * if we tried all available ring items, and still
388 * can't get enough buf, it means something abnormal
391 if (unlikely(++tries > max_tries))
394 if (unlikely(fill_vec_buf_split(dev, vq, cur_idx,
397 VHOST_ACCESS_RW) < 0))
399 len = RTE_MIN(len, size);
400 update_shadow_used_ring_split(vq, head_idx, len);
412 static __rte_always_inline int
413 fill_vec_buf_packed_indirect(struct virtio_net *dev,
414 struct vhost_virtqueue *vq,
415 struct vring_packed_desc *desc, uint16_t *vec_idx,
416 struct buf_vector *buf_vec, uint32_t *len, uint8_t perm)
420 uint16_t vec_id = *vec_idx;
422 struct vring_packed_desc *descs, *idescs = NULL;
425 descs = (struct vring_packed_desc *)(uintptr_t)
426 vhost_iova_to_vva(dev, vq, desc->addr, &dlen, VHOST_ACCESS_RO);
427 if (unlikely(!descs))
430 if (unlikely(dlen < desc->len)) {
432 * The indirect desc table is not contiguous
433 * in process VA space, we have to copy it.
435 idescs = alloc_copy_ind_table(dev, vq, desc->addr, desc->len);
436 if (unlikely(!idescs))
442 nr_descs = desc->len / sizeof(struct vring_packed_desc);
443 if (unlikely(nr_descs >= vq->size)) {
444 free_ind_table(idescs);
448 for (i = 0; i < nr_descs; i++) {
449 if (unlikely(vec_id >= BUF_VECTOR_MAX)) {
450 free_ind_table(idescs);
454 *len += descs[i].len;
455 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
456 descs[i].addr, descs[i].len,
462 if (unlikely(!!idescs))
463 free_ind_table(idescs);
468 static __rte_always_inline int
469 fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
470 uint16_t avail_idx, uint16_t *desc_count,
471 struct buf_vector *buf_vec, uint16_t *vec_idx,
472 uint16_t *buf_id, uint32_t *len, uint8_t perm)
474 bool wrap_counter = vq->avail_wrap_counter;
475 struct vring_packed_desc *descs = vq->desc_packed;
476 uint16_t vec_id = *vec_idx;
478 if (avail_idx < vq->last_avail_idx)
481 if (unlikely(!desc_is_avail(&descs[avail_idx], wrap_counter)))
488 if (unlikely(vec_id >= BUF_VECTOR_MAX))
492 *buf_id = descs[avail_idx].id;
494 if (descs[avail_idx].flags & VRING_DESC_F_INDIRECT) {
495 if (unlikely(fill_vec_buf_packed_indirect(dev, vq,
501 *len += descs[avail_idx].len;
503 if (unlikely(map_one_desc(dev, vq, buf_vec, &vec_id,
504 descs[avail_idx].addr,
505 descs[avail_idx].len,
510 if ((descs[avail_idx].flags & VRING_DESC_F_NEXT) == 0)
513 if (++avail_idx >= vq->size) {
514 avail_idx -= vq->size;
525 * Returns -1 on fail, 0 on success
528 reserve_avail_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
529 uint32_t size, struct buf_vector *buf_vec,
530 uint16_t *nr_vec, uint16_t *num_buffers,
534 uint16_t vec_idx = 0;
535 uint16_t max_tries, tries = 0;
542 avail_idx = vq->last_avail_idx;
544 if (rxvq_is_mergeable(dev))
545 max_tries = vq->size - 1;
551 * if we tried all available ring items, and still
552 * can't get enough buf, it means something abnormal
555 if (unlikely(++tries > max_tries))
558 if (unlikely(fill_vec_buf_packed(dev, vq,
559 avail_idx, &desc_count,
562 VHOST_ACCESS_RW) < 0))
565 len = RTE_MIN(len, size);
566 update_shadow_used_ring_packed(vq, buf_id, len, desc_count);
569 avail_idx += desc_count;
570 if (avail_idx >= vq->size)
571 avail_idx -= vq->size;
573 *nr_descs += desc_count;
582 static __rte_always_inline int
583 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
584 struct rte_mbuf *m, struct buf_vector *buf_vec,
585 uint16_t nr_vec, uint16_t num_buffers)
587 uint32_t vec_idx = 0;
588 uint32_t mbuf_offset, mbuf_avail;
589 uint32_t buf_offset, buf_avail;
590 uint64_t buf_addr, buf_iova, buf_len;
593 struct rte_mbuf *hdr_mbuf;
594 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
595 struct virtio_net_hdr_mrg_rxbuf tmp_hdr, *hdr = NULL;
598 if (unlikely(m == NULL)) {
603 buf_addr = buf_vec[vec_idx].buf_addr;
604 buf_iova = buf_vec[vec_idx].buf_iova;
605 buf_len = buf_vec[vec_idx].buf_len;
608 rte_prefetch0((void *)(uintptr_t)buf_vec[1].buf_addr);
610 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
617 if (unlikely(buf_len < dev->vhost_hlen))
620 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)hdr_addr;
622 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) RX: num merge buffers %d\n",
623 dev->vid, num_buffers);
625 if (unlikely(buf_len < dev->vhost_hlen)) {
626 buf_offset = dev->vhost_hlen - buf_len;
628 buf_addr = buf_vec[vec_idx].buf_addr;
629 buf_iova = buf_vec[vec_idx].buf_iova;
630 buf_len = buf_vec[vec_idx].buf_len;
631 buf_avail = buf_len - buf_offset;
633 buf_offset = dev->vhost_hlen;
634 buf_avail = buf_len - dev->vhost_hlen;
637 mbuf_avail = rte_pktmbuf_data_len(m);
639 while (mbuf_avail != 0 || m->next != NULL) {
640 /* done with current buf, get the next one */
641 if (buf_avail == 0) {
643 if (unlikely(vec_idx >= nr_vec)) {
648 buf_addr = buf_vec[vec_idx].buf_addr;
649 buf_iova = buf_vec[vec_idx].buf_iova;
650 buf_len = buf_vec[vec_idx].buf_len;
652 /* Prefetch next buffer address. */
653 if (vec_idx + 1 < nr_vec)
654 rte_prefetch0((void *)(uintptr_t)
655 buf_vec[vec_idx + 1].buf_addr);
660 /* done with current mbuf, get the next one */
661 if (mbuf_avail == 0) {
665 mbuf_avail = rte_pktmbuf_data_len(m);
669 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
670 if (rxvq_is_mergeable(dev))
671 ASSIGN_UNLESS_EQUAL(hdr->num_buffers,
674 if (unlikely(hdr == &tmp_hdr)) {
676 uint64_t remain = dev->vhost_hlen;
677 uint64_t src = (uint64_t)(uintptr_t)hdr, dst;
678 uint64_t iova = buf_vec[0].buf_iova;
679 uint16_t hdr_vec_idx = 0;
682 len = RTE_MIN(remain,
683 buf_vec[hdr_vec_idx].buf_len);
684 dst = buf_vec[hdr_vec_idx].buf_addr;
685 rte_memcpy((void *)(uintptr_t)dst,
686 (void *)(uintptr_t)src,
689 PRINT_PACKET(dev, (uintptr_t)dst,
691 vhost_log_cache_write(dev, vq,
700 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
702 vhost_log_cache_write(dev, vq,
710 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
712 if (likely(cpy_len > MAX_BATCH_LEN ||
713 vq->batch_copy_nb_elems >= vq->size)) {
714 rte_memcpy((void *)((uintptr_t)(buf_addr + buf_offset)),
715 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
717 vhost_log_cache_write(dev, vq, buf_iova + buf_offset,
719 PRINT_PACKET(dev, (uintptr_t)(buf_addr + buf_offset),
722 batch_copy[vq->batch_copy_nb_elems].dst =
723 (void *)((uintptr_t)(buf_addr + buf_offset));
724 batch_copy[vq->batch_copy_nb_elems].src =
725 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
726 batch_copy[vq->batch_copy_nb_elems].log_addr =
727 buf_iova + buf_offset;
728 batch_copy[vq->batch_copy_nb_elems].len = cpy_len;
729 vq->batch_copy_nb_elems++;
732 mbuf_avail -= cpy_len;
733 mbuf_offset += cpy_len;
734 buf_avail -= cpy_len;
735 buf_offset += cpy_len;
743 static __rte_always_inline uint32_t
744 virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
745 struct rte_mbuf **pkts, uint32_t count)
747 uint32_t pkt_idx = 0;
748 uint16_t num_buffers;
749 struct buf_vector buf_vec[BUF_VECTOR_MAX];
752 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
753 avail_head = *((volatile uint16_t *)&vq->avail->idx);
756 * The ordering between avail index and
757 * desc reads needs to be enforced.
761 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
762 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
765 if (unlikely(reserve_avail_buf_split(dev, vq,
766 pkt_len, buf_vec, &num_buffers,
767 avail_head, &nr_vec) < 0)) {
768 VHOST_LOG_DEBUG(VHOST_DATA,
769 "(%d) failed to get enough desc from vring\n",
771 vq->shadow_used_idx -= num_buffers;
775 rte_prefetch0((void *)(uintptr_t)buf_vec[0].buf_addr);
777 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
778 dev->vid, vq->last_avail_idx,
779 vq->last_avail_idx + num_buffers);
781 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
784 vq->shadow_used_idx -= num_buffers;
788 vq->last_avail_idx += num_buffers;
791 do_data_copy_enqueue(dev, vq);
793 if (likely(vq->shadow_used_idx)) {
794 flush_shadow_used_ring_split(dev, vq);
795 vhost_vring_call_split(dev, vq);
801 static __rte_always_inline uint32_t
802 virtio_dev_rx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
803 struct rte_mbuf **pkts, uint32_t count)
805 uint32_t pkt_idx = 0;
806 uint16_t num_buffers;
807 struct buf_vector buf_vec[BUF_VECTOR_MAX];
809 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
810 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
812 uint16_t nr_descs = 0;
814 if (unlikely(reserve_avail_buf_packed(dev, vq,
815 pkt_len, buf_vec, &nr_vec,
816 &num_buffers, &nr_descs) < 0)) {
817 VHOST_LOG_DEBUG(VHOST_DATA,
818 "(%d) failed to get enough desc from vring\n",
820 vq->shadow_used_idx -= num_buffers;
824 rte_prefetch0((void *)(uintptr_t)buf_vec[0].buf_addr);
826 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
827 dev->vid, vq->last_avail_idx,
828 vq->last_avail_idx + num_buffers);
830 if (copy_mbuf_to_desc(dev, vq, pkts[pkt_idx],
833 vq->shadow_used_idx -= num_buffers;
837 vq->last_avail_idx += nr_descs;
838 if (vq->last_avail_idx >= vq->size) {
839 vq->last_avail_idx -= vq->size;
840 vq->avail_wrap_counter ^= 1;
844 do_data_copy_enqueue(dev, vq);
846 if (likely(vq->shadow_used_idx)) {
847 flush_shadow_used_ring_packed(dev, vq);
848 vhost_vring_call_packed(dev, vq);
854 static __rte_always_inline uint32_t
855 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
856 struct rte_mbuf **pkts, uint32_t count)
858 struct vhost_virtqueue *vq;
861 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
862 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
863 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
864 dev->vid, __func__, queue_id);
868 vq = dev->virtqueue[queue_id];
870 rte_spinlock_lock(&vq->access_lock);
872 if (unlikely(vq->enabled == 0))
873 goto out_access_unlock;
875 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
876 vhost_user_iotlb_rd_lock(vq);
878 if (unlikely(vq->access_ok == 0))
879 if (unlikely(vring_translate(dev, vq) < 0))
882 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
886 if (vq_is_packed(dev))
887 nb_tx = virtio_dev_rx_packed(dev, vq, pkts, count);
889 nb_tx = virtio_dev_rx_split(dev, vq, pkts, count);
892 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
893 vhost_user_iotlb_rd_unlock(vq);
896 rte_spinlock_unlock(&vq->access_lock);
902 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
903 struct rte_mbuf **pkts, uint16_t count)
905 struct virtio_net *dev = get_device(vid);
910 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
911 RTE_LOG(ERR, VHOST_DATA,
912 "(%d) %s: built-in vhost net backend is disabled.\n",
917 return virtio_dev_rx(dev, queue_id, pkts, count);
921 virtio_net_with_host_offload(struct virtio_net *dev)
924 ((1ULL << VIRTIO_NET_F_CSUM) |
925 (1ULL << VIRTIO_NET_F_HOST_ECN) |
926 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
927 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
928 (1ULL << VIRTIO_NET_F_HOST_UFO)))
935 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
937 struct ipv4_hdr *ipv4_hdr;
938 struct ipv6_hdr *ipv6_hdr;
940 struct ether_hdr *eth_hdr;
943 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
945 m->l2_len = sizeof(struct ether_hdr);
946 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
948 if (ethertype == ETHER_TYPE_VLAN) {
949 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
951 m->l2_len += sizeof(struct vlan_hdr);
952 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
955 l3_hdr = (char *)eth_hdr + m->l2_len;
958 case ETHER_TYPE_IPv4:
960 *l4_proto = ipv4_hdr->next_proto_id;
961 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
962 *l4_hdr = (char *)l3_hdr + m->l3_len;
963 m->ol_flags |= PKT_TX_IPV4;
965 case ETHER_TYPE_IPv6:
967 *l4_proto = ipv6_hdr->proto;
968 m->l3_len = sizeof(struct ipv6_hdr);
969 *l4_hdr = (char *)l3_hdr + m->l3_len;
970 m->ol_flags |= PKT_TX_IPV6;
980 static __rte_always_inline void
981 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
983 uint16_t l4_proto = 0;
985 struct tcp_hdr *tcp_hdr = NULL;
987 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
990 parse_ethernet(m, &l4_proto, &l4_hdr);
991 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
992 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
993 switch (hdr->csum_offset) {
994 case (offsetof(struct tcp_hdr, cksum)):
995 if (l4_proto == IPPROTO_TCP)
996 m->ol_flags |= PKT_TX_TCP_CKSUM;
998 case (offsetof(struct udp_hdr, dgram_cksum)):
999 if (l4_proto == IPPROTO_UDP)
1000 m->ol_flags |= PKT_TX_UDP_CKSUM;
1002 case (offsetof(struct sctp_hdr, cksum)):
1003 if (l4_proto == IPPROTO_SCTP)
1004 m->ol_flags |= PKT_TX_SCTP_CKSUM;
1012 if (l4_hdr && hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1013 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1014 case VIRTIO_NET_HDR_GSO_TCPV4:
1015 case VIRTIO_NET_HDR_GSO_TCPV6:
1017 m->ol_flags |= PKT_TX_TCP_SEG;
1018 m->tso_segsz = hdr->gso_size;
1019 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
1021 case VIRTIO_NET_HDR_GSO_UDP:
1022 m->ol_flags |= PKT_TX_UDP_SEG;
1023 m->tso_segsz = hdr->gso_size;
1024 m->l4_len = sizeof(struct udp_hdr);
1027 RTE_LOG(WARNING, VHOST_DATA,
1028 "unsupported gso type %u.\n", hdr->gso_type);
1034 static __rte_always_inline void
1035 put_zmbuf(struct zcopy_mbuf *zmbuf)
1040 static __rte_always_inline int
1041 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
1042 struct buf_vector *buf_vec, uint16_t nr_vec,
1043 struct rte_mbuf *m, struct rte_mempool *mbuf_pool)
1045 uint32_t buf_avail, buf_offset;
1046 uint64_t buf_addr, buf_iova, buf_len;
1047 uint32_t mbuf_avail, mbuf_offset;
1049 struct rte_mbuf *cur = m, *prev = m;
1050 struct virtio_net_hdr tmp_hdr;
1051 struct virtio_net_hdr *hdr = NULL;
1052 /* A counter to avoid desc dead loop chain */
1053 uint16_t vec_idx = 0;
1054 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
1057 buf_addr = buf_vec[vec_idx].buf_addr;
1058 buf_iova = buf_vec[vec_idx].buf_iova;
1059 buf_len = buf_vec[vec_idx].buf_len;
1061 if (unlikely(buf_len < dev->vhost_hlen && nr_vec <= 1)) {
1066 if (likely(nr_vec > 1))
1067 rte_prefetch0((void *)(uintptr_t)buf_vec[1].buf_addr);
1069 if (virtio_net_with_host_offload(dev)) {
1070 if (unlikely(buf_len < sizeof(struct virtio_net_hdr))) {
1072 uint64_t remain = sizeof(struct virtio_net_hdr);
1074 uint64_t dst = (uint64_t)(uintptr_t)&tmp_hdr;
1075 uint16_t hdr_vec_idx = 0;
1078 * No luck, the virtio-net header doesn't fit
1079 * in a contiguous virtual area.
1082 len = RTE_MIN(remain,
1083 buf_vec[hdr_vec_idx].buf_len);
1084 src = buf_vec[hdr_vec_idx].buf_addr;
1085 rte_memcpy((void *)(uintptr_t)dst,
1086 (void *)(uintptr_t)src, len);
1095 hdr = (struct virtio_net_hdr *)((uintptr_t)buf_addr);
1101 * A virtio driver normally uses at least 2 desc buffers
1102 * for Tx: the first for storing the header, and others
1103 * for storing the data.
1105 if (unlikely(buf_len < dev->vhost_hlen)) {
1106 buf_offset = dev->vhost_hlen - buf_len;
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;
1111 buf_avail = buf_len - buf_offset;
1112 } else if (buf_len == dev->vhost_hlen) {
1113 if (unlikely(++vec_idx >= nr_vec))
1115 buf_addr = buf_vec[vec_idx].buf_addr;
1116 buf_iova = buf_vec[vec_idx].buf_iova;
1117 buf_len = buf_vec[vec_idx].buf_len;
1120 buf_avail = buf_len;
1122 buf_offset = dev->vhost_hlen;
1123 buf_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
1126 rte_prefetch0((void *)(uintptr_t)
1127 (buf_addr + buf_offset));
1130 (uintptr_t)(buf_addr + buf_offset),
1131 (uint32_t)buf_avail, 0);
1134 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
1138 cpy_len = RTE_MIN(buf_avail, mbuf_avail);
1141 * A desc buf might across two host physical pages that are
1142 * not continuous. In such case (gpa_to_hpa returns 0), data
1143 * will be copied even though zero copy is enabled.
1145 if (unlikely(dev->dequeue_zero_copy && (hpa = gpa_to_hpa(dev,
1146 buf_iova + buf_offset, cpy_len)))) {
1147 cur->data_len = cpy_len;
1150 (void *)(uintptr_t)(buf_addr + buf_offset);
1151 cur->buf_iova = hpa;
1154 * In zero copy mode, one mbuf can only reference data
1155 * for one or partial of one desc buff.
1157 mbuf_avail = cpy_len;
1159 if (likely(cpy_len > MAX_BATCH_LEN ||
1160 vq->batch_copy_nb_elems >= vq->size ||
1161 (hdr && cur == m))) {
1162 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
1164 (void *)((uintptr_t)(buf_addr +
1168 batch_copy[vq->batch_copy_nb_elems].dst =
1169 rte_pktmbuf_mtod_offset(cur, void *,
1171 batch_copy[vq->batch_copy_nb_elems].src =
1172 (void *)((uintptr_t)(buf_addr +
1174 batch_copy[vq->batch_copy_nb_elems].len =
1176 vq->batch_copy_nb_elems++;
1180 mbuf_avail -= cpy_len;
1181 mbuf_offset += cpy_len;
1182 buf_avail -= cpy_len;
1183 buf_offset += cpy_len;
1185 /* This buf reaches to its end, get the next one */
1186 if (buf_avail == 0) {
1187 if (++vec_idx >= nr_vec)
1190 buf_addr = buf_vec[vec_idx].buf_addr;
1191 buf_iova = buf_vec[vec_idx].buf_iova;
1192 buf_len = buf_vec[vec_idx].buf_len;
1195 * Prefecth desc n + 1 buffer while
1196 * desc n buffer is processed.
1198 if (vec_idx + 1 < nr_vec)
1199 rte_prefetch0((void *)(uintptr_t)
1200 buf_vec[vec_idx + 1].buf_addr);
1203 buf_avail = buf_len;
1205 PRINT_PACKET(dev, (uintptr_t)buf_addr,
1206 (uint32_t)buf_avail, 0);
1210 * This mbuf reaches to its end, get a new one
1211 * to hold more data.
1213 if (mbuf_avail == 0) {
1214 cur = rte_pktmbuf_alloc(mbuf_pool);
1215 if (unlikely(cur == NULL)) {
1216 RTE_LOG(ERR, VHOST_DATA, "Failed to "
1217 "allocate memory for mbuf.\n");
1221 if (unlikely(dev->dequeue_zero_copy))
1222 rte_mbuf_refcnt_update(cur, 1);
1225 prev->data_len = mbuf_offset;
1227 m->pkt_len += mbuf_offset;
1231 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
1235 prev->data_len = mbuf_offset;
1236 m->pkt_len += mbuf_offset;
1239 vhost_dequeue_offload(hdr, m);
1246 static __rte_always_inline struct zcopy_mbuf *
1247 get_zmbuf(struct vhost_virtqueue *vq)
1253 /* search [last_zmbuf_idx, zmbuf_size) */
1254 i = vq->last_zmbuf_idx;
1255 last = vq->zmbuf_size;
1258 for (; i < last; i++) {
1259 if (vq->zmbufs[i].in_use == 0) {
1260 vq->last_zmbuf_idx = i + 1;
1261 vq->zmbufs[i].in_use = 1;
1262 return &vq->zmbufs[i];
1268 /* search [0, last_zmbuf_idx) */
1270 last = vq->last_zmbuf_idx;
1277 static __rte_always_inline bool
1278 mbuf_is_consumed(struct rte_mbuf *m)
1281 if (rte_mbuf_refcnt_read(m) > 1)
1289 static __rte_always_inline void
1290 restore_mbuf(struct rte_mbuf *m)
1292 uint32_t mbuf_size, priv_size;
1295 priv_size = rte_pktmbuf_priv_size(m->pool);
1296 mbuf_size = sizeof(struct rte_mbuf) + priv_size;
1297 /* start of buffer is after mbuf structure and priv data */
1299 m->buf_addr = (char *)m + mbuf_size;
1300 m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size;
1305 static __rte_always_inline uint16_t
1306 virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
1307 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1310 uint16_t free_entries;
1312 if (unlikely(dev->dequeue_zero_copy)) {
1313 struct zcopy_mbuf *zmbuf, *next;
1315 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1316 zmbuf != NULL; zmbuf = next) {
1317 next = TAILQ_NEXT(zmbuf, next);
1319 if (mbuf_is_consumed(zmbuf->mbuf)) {
1320 update_shadow_used_ring_split(vq,
1321 zmbuf->desc_idx, 0);
1322 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1323 restore_mbuf(zmbuf->mbuf);
1324 rte_pktmbuf_free(zmbuf->mbuf);
1330 if (likely(vq->shadow_used_idx)) {
1331 flush_shadow_used_ring_split(dev, vq);
1332 vhost_vring_call_split(dev, vq);
1336 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
1338 free_entries = *((volatile uint16_t *)&vq->avail->idx) -
1340 if (free_entries == 0)
1344 * The ordering between avail index and
1345 * desc reads needs to be enforced.
1349 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1351 count = RTE_MIN(count, MAX_PKT_BURST);
1352 count = RTE_MIN(count, free_entries);
1353 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1356 for (i = 0; i < count; i++) {
1357 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1360 uint16_t nr_vec = 0;
1363 if (unlikely(fill_vec_buf_split(dev, vq,
1364 vq->last_avail_idx + i,
1366 &head_idx, &dummy_len,
1367 VHOST_ACCESS_RO) < 0))
1370 if (likely(dev->dequeue_zero_copy == 0))
1371 update_shadow_used_ring_split(vq, head_idx, 0);
1373 rte_prefetch0((void *)(uintptr_t)buf_vec[0].buf_addr);
1375 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
1376 if (unlikely(pkts[i] == NULL)) {
1377 RTE_LOG(ERR, VHOST_DATA,
1378 "Failed to allocate memory for mbuf.\n");
1382 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
1384 if (unlikely(err)) {
1385 rte_pktmbuf_free(pkts[i]);
1389 if (unlikely(dev->dequeue_zero_copy)) {
1390 struct zcopy_mbuf *zmbuf;
1392 zmbuf = get_zmbuf(vq);
1394 rte_pktmbuf_free(pkts[i]);
1397 zmbuf->mbuf = pkts[i];
1398 zmbuf->desc_idx = head_idx;
1401 * Pin lock the mbuf; we will check later to see
1402 * whether the mbuf is freed (when we are the last
1403 * user) or not. If that's the case, we then could
1404 * update the used ring safely.
1406 rte_mbuf_refcnt_update(pkts[i], 1);
1409 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1412 vq->last_avail_idx += i;
1414 if (likely(dev->dequeue_zero_copy == 0)) {
1415 do_data_copy_dequeue(vq);
1416 if (unlikely(i < count))
1417 vq->shadow_used_idx = i;
1418 if (likely(vq->shadow_used_idx)) {
1419 flush_shadow_used_ring_split(dev, vq);
1420 vhost_vring_call_split(dev, vq);
1427 static __rte_always_inline uint16_t
1428 virtio_dev_tx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
1429 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1433 rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]);
1435 if (unlikely(dev->dequeue_zero_copy)) {
1436 struct zcopy_mbuf *zmbuf, *next;
1438 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1439 zmbuf != NULL; zmbuf = next) {
1440 next = TAILQ_NEXT(zmbuf, next);
1442 if (mbuf_is_consumed(zmbuf->mbuf)) {
1443 update_shadow_used_ring_packed(vq,
1448 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1449 restore_mbuf(zmbuf->mbuf);
1450 rte_pktmbuf_free(zmbuf->mbuf);
1456 if (likely(vq->shadow_used_idx)) {
1457 flush_shadow_used_ring_packed(dev, vq);
1458 vhost_vring_call_packed(dev, vq);
1462 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1464 count = RTE_MIN(count, MAX_PKT_BURST);
1465 VHOST_LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1468 for (i = 0; i < count; i++) {
1469 struct buf_vector buf_vec[BUF_VECTOR_MAX];
1472 uint16_t desc_count, nr_vec = 0;
1475 if (unlikely(fill_vec_buf_packed(dev, vq,
1476 vq->last_avail_idx, &desc_count,
1478 &buf_id, &dummy_len,
1479 VHOST_ACCESS_RO) < 0))
1482 if (likely(dev->dequeue_zero_copy == 0))
1483 update_shadow_used_ring_packed(vq, buf_id, 0,
1486 rte_prefetch0((void *)(uintptr_t)buf_vec[0].buf_addr);
1488 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
1489 if (unlikely(pkts[i] == NULL)) {
1490 RTE_LOG(ERR, VHOST_DATA,
1491 "Failed to allocate memory for mbuf.\n");
1495 err = copy_desc_to_mbuf(dev, vq, buf_vec, nr_vec, pkts[i],
1497 if (unlikely(err)) {
1498 rte_pktmbuf_free(pkts[i]);
1502 if (unlikely(dev->dequeue_zero_copy)) {
1503 struct zcopy_mbuf *zmbuf;
1505 zmbuf = get_zmbuf(vq);
1507 rte_pktmbuf_free(pkts[i]);
1510 zmbuf->mbuf = pkts[i];
1511 zmbuf->desc_idx = buf_id;
1512 zmbuf->desc_count = desc_count;
1515 * Pin lock the mbuf; we will check later to see
1516 * whether the mbuf is freed (when we are the last
1517 * user) or not. If that's the case, we then could
1518 * update the used ring safely.
1520 rte_mbuf_refcnt_update(pkts[i], 1);
1523 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1526 vq->last_avail_idx += desc_count;
1527 if (vq->last_avail_idx >= vq->size) {
1528 vq->last_avail_idx -= vq->size;
1529 vq->avail_wrap_counter ^= 1;
1533 if (likely(dev->dequeue_zero_copy == 0)) {
1534 do_data_copy_dequeue(vq);
1535 if (unlikely(i < count))
1536 vq->shadow_used_idx = i;
1537 if (likely(vq->shadow_used_idx)) {
1538 flush_shadow_used_ring_packed(dev, vq);
1539 vhost_vring_call_packed(dev, vq);
1547 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
1548 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1550 struct virtio_net *dev;
1551 struct rte_mbuf *rarp_mbuf = NULL;
1552 struct vhost_virtqueue *vq;
1554 dev = get_device(vid);
1558 if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
1559 RTE_LOG(ERR, VHOST_DATA,
1560 "(%d) %s: built-in vhost net backend is disabled.\n",
1561 dev->vid, __func__);
1565 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
1566 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
1567 dev->vid, __func__, queue_id);
1571 vq = dev->virtqueue[queue_id];
1573 if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
1576 if (unlikely(vq->enabled == 0)) {
1578 goto out_access_unlock;
1581 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1582 vhost_user_iotlb_rd_lock(vq);
1584 if (unlikely(vq->access_ok == 0))
1585 if (unlikely(vring_translate(dev, vq) < 0)) {
1591 * Construct a RARP broadcast packet, and inject it to the "pkts"
1592 * array, to looks like that guest actually send such packet.
1594 * Check user_send_rarp() for more information.
1596 * broadcast_rarp shares a cacheline in the virtio_net structure
1597 * with some fields that are accessed during enqueue and
1598 * rte_atomic16_cmpset() causes a write if using cmpxchg. This could
1599 * result in false sharing between enqueue and dequeue.
1601 * Prevent unnecessary false sharing by reading broadcast_rarp first
1602 * and only performing cmpset if the read indicates it is likely to
1605 if (unlikely(rte_atomic16_read(&dev->broadcast_rarp) &&
1606 rte_atomic16_cmpset((volatile uint16_t *)
1607 &dev->broadcast_rarp.cnt, 1, 0))) {
1609 rarp_mbuf = rte_net_make_rarp_packet(mbuf_pool, &dev->mac);
1610 if (rarp_mbuf == NULL) {
1611 RTE_LOG(ERR, VHOST_DATA,
1612 "Failed to make RARP packet.\n");
1619 if (vq_is_packed(dev))
1620 count = virtio_dev_tx_packed(dev, vq, mbuf_pool, pkts, count);
1622 count = virtio_dev_tx_split(dev, vq, mbuf_pool, pkts, count);
1625 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1626 vhost_user_iotlb_rd_unlock(vq);
1629 rte_spinlock_unlock(&vq->access_lock);
1631 if (unlikely(rarp_mbuf != NULL)) {
1633 * Inject it to the head of "pkts" array, so that switch's mac
1634 * learning table will get updated first.
1636 memmove(&pkts[1], pkts, count * sizeof(struct rte_mbuf *));
1637 pkts[0] = rarp_mbuf;