4 * Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <linux/virtio_net.h>
39 #include <rte_memcpy.h>
40 #include <rte_ether.h>
42 #include <rte_vhost.h>
47 #include <rte_spinlock.h>
52 #define MAX_PKT_BURST 32
54 #define MAX_BATCH_LEN 256
57 is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t nr_vring)
59 return (is_tx ^ (idx & 1)) == 0 && idx < nr_vring;
62 static __rte_always_inline void
63 do_flush_shadow_used_ring(struct virtio_net *dev, struct vhost_virtqueue *vq,
64 uint16_t to, uint16_t from, uint16_t size)
66 rte_memcpy(&vq->used->ring[to],
67 &vq->shadow_used_ring[from],
68 size * sizeof(struct vring_used_elem));
69 vhost_log_used_vring(dev, vq,
70 offsetof(struct vring_used, ring[to]),
71 size * sizeof(struct vring_used_elem));
74 static __rte_always_inline void
75 flush_shadow_used_ring(struct virtio_net *dev, struct vhost_virtqueue *vq)
77 uint16_t used_idx = vq->last_used_idx & (vq->size - 1);
79 if (used_idx + vq->shadow_used_idx <= vq->size) {
80 do_flush_shadow_used_ring(dev, vq, used_idx, 0,
85 /* update used ring interval [used_idx, vq->size] */
86 size = vq->size - used_idx;
87 do_flush_shadow_used_ring(dev, vq, used_idx, 0, size);
89 /* update the left half used ring interval [0, left_size] */
90 do_flush_shadow_used_ring(dev, vq, 0, size,
91 vq->shadow_used_idx - size);
93 vq->last_used_idx += vq->shadow_used_idx;
97 *(volatile uint16_t *)&vq->used->idx += vq->shadow_used_idx;
98 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
99 sizeof(vq->used->idx));
102 static __rte_always_inline void
103 update_shadow_used_ring(struct vhost_virtqueue *vq,
104 uint16_t desc_idx, uint16_t len)
106 uint16_t i = vq->shadow_used_idx++;
108 vq->shadow_used_ring[i].id = desc_idx;
109 vq->shadow_used_ring[i].len = len;
113 do_data_copy_enqueue(struct virtio_net *dev, struct vhost_virtqueue *vq)
115 struct batch_copy_elem *elem = vq->batch_copy_elems;
116 uint16_t count = vq->batch_copy_nb_elems;
119 for (i = 0; i < count; i++) {
120 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
121 vhost_log_write(dev, elem[i].log_addr, elem[i].len);
122 PRINT_PACKET(dev, (uintptr_t)elem[i].dst, elem[i].len, 0);
127 do_data_copy_dequeue(struct vhost_virtqueue *vq)
129 struct batch_copy_elem *elem = vq->batch_copy_elems;
130 uint16_t count = vq->batch_copy_nb_elems;
133 for (i = 0; i < count; i++)
134 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
137 /* avoid write operation when necessary, to lessen cache issues */
138 #define ASSIGN_UNLESS_EQUAL(var, val) do { \
139 if ((var) != (val)) \
144 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
146 uint64_t csum_l4 = m_buf->ol_flags & PKT_TX_L4_MASK;
148 if (m_buf->ol_flags & PKT_TX_TCP_SEG)
149 csum_l4 |= PKT_TX_TCP_CKSUM;
152 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
153 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
156 case PKT_TX_TCP_CKSUM:
157 net_hdr->csum_offset = (offsetof(struct tcp_hdr,
160 case PKT_TX_UDP_CKSUM:
161 net_hdr->csum_offset = (offsetof(struct udp_hdr,
164 case PKT_TX_SCTP_CKSUM:
165 net_hdr->csum_offset = (offsetof(struct sctp_hdr,
170 ASSIGN_UNLESS_EQUAL(net_hdr->csum_start, 0);
171 ASSIGN_UNLESS_EQUAL(net_hdr->csum_offset, 0);
172 ASSIGN_UNLESS_EQUAL(net_hdr->flags, 0);
175 /* IP cksum verification cannot be bypassed, then calculate here */
176 if (m_buf->ol_flags & PKT_TX_IP_CKSUM) {
177 struct ipv4_hdr *ipv4_hdr;
179 ipv4_hdr = rte_pktmbuf_mtod_offset(m_buf, struct ipv4_hdr *,
181 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
184 if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
185 if (m_buf->ol_flags & PKT_TX_IPV4)
186 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
188 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
189 net_hdr->gso_size = m_buf->tso_segsz;
190 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
193 ASSIGN_UNLESS_EQUAL(net_hdr->gso_type, 0);
194 ASSIGN_UNLESS_EQUAL(net_hdr->gso_size, 0);
195 ASSIGN_UNLESS_EQUAL(net_hdr->hdr_len, 0);
199 static __rte_always_inline int
200 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
201 struct vring_desc *descs, struct rte_mbuf *m,
202 uint16_t desc_idx, uint32_t size)
204 uint32_t desc_avail, desc_offset;
205 uint32_t mbuf_avail, mbuf_offset;
207 struct vring_desc *desc;
209 /* A counter to avoid desc dead loop chain */
210 uint16_t nr_desc = 1;
211 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
212 uint16_t copy_nb = vq->batch_copy_nb_elems;
215 desc = &descs[desc_idx];
216 desc_addr = vhost_iova_to_vva(dev, vq, desc->addr,
217 desc->len, VHOST_ACCESS_RW);
219 * Checking of 'desc_addr' placed outside of 'unlikely' macro to avoid
220 * performance issue with some versions of gcc (4.8.4 and 5.3.0) which
221 * otherwise stores offset on the stack instead of in a register.
223 if (unlikely(desc->len < dev->vhost_hlen) || !desc_addr) {
228 rte_prefetch0((void *)(uintptr_t)desc_addr);
230 virtio_enqueue_offload(m, (struct virtio_net_hdr *)(uintptr_t)desc_addr);
231 vhost_log_write(dev, desc->addr, dev->vhost_hlen);
232 PRINT_PACKET(dev, (uintptr_t)desc_addr, dev->vhost_hlen, 0);
234 desc_offset = dev->vhost_hlen;
235 desc_avail = desc->len - dev->vhost_hlen;
237 mbuf_avail = rte_pktmbuf_data_len(m);
239 while (mbuf_avail != 0 || m->next != NULL) {
240 /* done with current mbuf, fetch next */
241 if (mbuf_avail == 0) {
245 mbuf_avail = rte_pktmbuf_data_len(m);
248 /* done with current desc buf, fetch next */
249 if (desc_avail == 0) {
250 if ((desc->flags & VRING_DESC_F_NEXT) == 0) {
251 /* Room in vring buffer is not enough */
255 if (unlikely(desc->next >= size || ++nr_desc > size)) {
260 desc = &descs[desc->next];
261 desc_addr = vhost_iova_to_vva(dev, vq, desc->addr,
264 if (unlikely(!desc_addr)) {
270 desc_avail = desc->len;
273 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
274 if (likely(cpy_len > MAX_BATCH_LEN || copy_nb >= vq->size)) {
275 rte_memcpy((void *)((uintptr_t)(desc_addr +
277 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
279 vhost_log_write(dev, desc->addr + desc_offset, cpy_len);
280 PRINT_PACKET(dev, (uintptr_t)(desc_addr + desc_offset),
283 batch_copy[copy_nb].dst =
284 (void *)((uintptr_t)(desc_addr + desc_offset));
285 batch_copy[copy_nb].src =
286 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
287 batch_copy[copy_nb].log_addr = desc->addr + desc_offset;
288 batch_copy[copy_nb].len = cpy_len;
292 mbuf_avail -= cpy_len;
293 mbuf_offset += cpy_len;
294 desc_avail -= cpy_len;
295 desc_offset += cpy_len;
299 vq->batch_copy_nb_elems = copy_nb;
305 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
306 * be received from the physical port or from another virtio device. A packet
307 * count is returned to indicate the number of packets that are successfully
308 * added to the RX queue. This function works when the mbuf is scattered, but
309 * it doesn't support the mergeable feature.
311 static __rte_always_inline uint32_t
312 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
313 struct rte_mbuf **pkts, uint32_t count)
315 struct vhost_virtqueue *vq;
316 uint16_t avail_idx, free_entries, start_idx;
317 uint16_t desc_indexes[MAX_PKT_BURST];
318 struct vring_desc *descs;
322 LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
323 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
324 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
325 dev->vid, __func__, queue_id);
329 vq = dev->virtqueue[queue_id];
331 rte_spinlock_lock(&vq->access_lock);
333 if (unlikely(vq->enabled == 0))
334 goto out_access_unlock;
336 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
337 vhost_user_iotlb_rd_lock(vq);
339 if (unlikely(vq->access_ok == 0)) {
340 if (unlikely(vring_translate(dev, vq) < 0)) {
346 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
347 start_idx = vq->last_used_idx;
348 free_entries = avail_idx - start_idx;
349 count = RTE_MIN(count, free_entries);
350 count = RTE_MIN(count, (uint32_t)MAX_PKT_BURST);
354 LOG_DEBUG(VHOST_DATA, "(%d) start_idx %d | end_idx %d\n",
355 dev->vid, start_idx, start_idx + count);
357 vq->batch_copy_nb_elems = 0;
359 /* Retrieve all of the desc indexes first to avoid caching issues. */
360 rte_prefetch0(&vq->avail->ring[start_idx & (vq->size - 1)]);
361 for (i = 0; i < count; i++) {
362 used_idx = (start_idx + i) & (vq->size - 1);
363 desc_indexes[i] = vq->avail->ring[used_idx];
364 vq->used->ring[used_idx].id = desc_indexes[i];
365 vq->used->ring[used_idx].len = pkts[i]->pkt_len +
367 vhost_log_used_vring(dev, vq,
368 offsetof(struct vring_used, ring[used_idx]),
369 sizeof(vq->used->ring[used_idx]));
372 rte_prefetch0(&vq->desc[desc_indexes[0]]);
373 for (i = 0; i < count; i++) {
374 uint16_t desc_idx = desc_indexes[i];
377 if (vq->desc[desc_idx].flags & VRING_DESC_F_INDIRECT) {
378 descs = (struct vring_desc *)(uintptr_t)
379 vhost_iova_to_vva(dev,
380 vq, vq->desc[desc_idx].addr,
381 vq->desc[desc_idx].len,
383 if (unlikely(!descs)) {
389 sz = vq->desc[desc_idx].len / sizeof(*descs);
395 err = copy_mbuf_to_desc(dev, vq, descs, pkts[i], desc_idx, sz);
402 rte_prefetch0(&vq->desc[desc_indexes[i+1]]);
405 do_data_copy_enqueue(dev, vq);
409 *(volatile uint16_t *)&vq->used->idx += count;
410 vq->last_used_idx += count;
411 vhost_log_used_vring(dev, vq,
412 offsetof(struct vring_used, idx),
413 sizeof(vq->used->idx));
415 /* flush used->idx update before we read avail->flags. */
418 /* Kick the guest if necessary. */
419 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
420 && (vq->callfd >= 0))
421 eventfd_write(vq->callfd, (eventfd_t)1);
423 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
424 vhost_user_iotlb_rd_unlock(vq);
427 rte_spinlock_unlock(&vq->access_lock);
432 static __rte_always_inline int
433 fill_vec_buf(struct virtio_net *dev, struct vhost_virtqueue *vq,
434 uint32_t avail_idx, uint32_t *vec_idx,
435 struct buf_vector *buf_vec, uint16_t *desc_chain_head,
436 uint16_t *desc_chain_len)
438 uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
439 uint32_t vec_id = *vec_idx;
441 struct vring_desc *descs = vq->desc;
443 *desc_chain_head = idx;
445 if (vq->desc[idx].flags & VRING_DESC_F_INDIRECT) {
446 descs = (struct vring_desc *)(uintptr_t)
447 vhost_iova_to_vva(dev, vq, vq->desc[idx].addr,
450 if (unlikely(!descs))
457 if (unlikely(vec_id >= BUF_VECTOR_MAX || idx >= vq->size))
460 len += descs[idx].len;
461 buf_vec[vec_id].buf_addr = descs[idx].addr;
462 buf_vec[vec_id].buf_len = descs[idx].len;
463 buf_vec[vec_id].desc_idx = idx;
466 if ((descs[idx].flags & VRING_DESC_F_NEXT) == 0)
469 idx = descs[idx].next;
472 *desc_chain_len = len;
479 * Returns -1 on fail, 0 on success
482 reserve_avail_buf_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,
483 uint32_t size, struct buf_vector *buf_vec,
484 uint16_t *num_buffers, uint16_t avail_head)
487 uint32_t vec_idx = 0;
490 uint16_t head_idx = 0;
494 cur_idx = vq->last_avail_idx;
497 if (unlikely(cur_idx == avail_head))
500 if (unlikely(fill_vec_buf(dev, vq, cur_idx, &vec_idx, buf_vec,
501 &head_idx, &len) < 0))
503 len = RTE_MIN(len, size);
504 update_shadow_used_ring(vq, head_idx, len);
512 * if we tried all available ring items, and still
513 * can't get enough buf, it means something abnormal
516 if (unlikely(tries >= vq->size))
523 static __rte_always_inline int
524 copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,
525 struct rte_mbuf *m, struct buf_vector *buf_vec,
526 uint16_t num_buffers)
528 uint32_t vec_idx = 0;
530 uint32_t mbuf_offset, mbuf_avail;
531 uint32_t desc_offset, desc_avail;
533 uint64_t hdr_addr, hdr_phys_addr;
534 struct rte_mbuf *hdr_mbuf;
535 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
536 uint16_t copy_nb = vq->batch_copy_nb_elems;
539 if (unlikely(m == NULL)) {
544 desc_addr = vhost_iova_to_vva(dev, vq, buf_vec[vec_idx].buf_addr,
545 buf_vec[vec_idx].buf_len,
547 if (buf_vec[vec_idx].buf_len < dev->vhost_hlen || !desc_addr) {
553 hdr_addr = desc_addr;
554 hdr_phys_addr = buf_vec[vec_idx].buf_addr;
555 rte_prefetch0((void *)(uintptr_t)hdr_addr);
557 LOG_DEBUG(VHOST_DATA, "(%d) RX: num merge buffers %d\n",
558 dev->vid, num_buffers);
560 desc_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
561 desc_offset = dev->vhost_hlen;
563 mbuf_avail = rte_pktmbuf_data_len(m);
565 while (mbuf_avail != 0 || m->next != NULL) {
566 /* done with current desc buf, get the next one */
567 if (desc_avail == 0) {
570 vhost_iova_to_vva(dev, vq,
571 buf_vec[vec_idx].buf_addr,
572 buf_vec[vec_idx].buf_len,
574 if (unlikely(!desc_addr)) {
579 /* Prefetch buffer address. */
580 rte_prefetch0((void *)(uintptr_t)desc_addr);
582 desc_avail = buf_vec[vec_idx].buf_len;
585 /* done with current mbuf, get the next one */
586 if (mbuf_avail == 0) {
590 mbuf_avail = rte_pktmbuf_data_len(m);
594 struct virtio_net_hdr_mrg_rxbuf *hdr;
596 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)
598 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
599 ASSIGN_UNLESS_EQUAL(hdr->num_buffers, num_buffers);
601 vhost_log_write(dev, hdr_phys_addr, dev->vhost_hlen);
602 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
608 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
610 if (likely(cpy_len > MAX_BATCH_LEN || copy_nb >= vq->size)) {
611 rte_memcpy((void *)((uintptr_t)(desc_addr +
613 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
616 buf_vec[vec_idx].buf_addr + desc_offset,
618 PRINT_PACKET(dev, (uintptr_t)(desc_addr + desc_offset),
621 batch_copy[copy_nb].dst =
622 (void *)((uintptr_t)(desc_addr + desc_offset));
623 batch_copy[copy_nb].src =
624 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
625 batch_copy[copy_nb].log_addr =
626 buf_vec[vec_idx].buf_addr + desc_offset;
627 batch_copy[copy_nb].len = cpy_len;
631 mbuf_avail -= cpy_len;
632 mbuf_offset += cpy_len;
633 desc_avail -= cpy_len;
634 desc_offset += cpy_len;
638 vq->batch_copy_nb_elems = copy_nb;
643 static __rte_always_inline uint32_t
644 virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,
645 struct rte_mbuf **pkts, uint32_t count)
647 struct vhost_virtqueue *vq;
648 uint32_t pkt_idx = 0;
649 uint16_t num_buffers;
650 struct buf_vector buf_vec[BUF_VECTOR_MAX];
653 LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
654 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
655 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
656 dev->vid, __func__, queue_id);
660 vq = dev->virtqueue[queue_id];
662 rte_spinlock_lock(&vq->access_lock);
664 if (unlikely(vq->enabled == 0))
665 goto out_access_unlock;
667 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
668 vhost_user_iotlb_rd_lock(vq);
670 if (unlikely(vq->access_ok == 0))
671 if (unlikely(vring_translate(dev, vq) < 0))
674 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
678 vq->batch_copy_nb_elems = 0;
680 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
682 vq->shadow_used_idx = 0;
683 avail_head = *((volatile uint16_t *)&vq->avail->idx);
684 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
685 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
687 if (unlikely(reserve_avail_buf_mergeable(dev, vq,
688 pkt_len, buf_vec, &num_buffers,
690 LOG_DEBUG(VHOST_DATA,
691 "(%d) failed to get enough desc from vring\n",
693 vq->shadow_used_idx -= num_buffers;
697 LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
698 dev->vid, vq->last_avail_idx,
699 vq->last_avail_idx + num_buffers);
701 if (copy_mbuf_to_desc_mergeable(dev, vq, pkts[pkt_idx],
702 buf_vec, num_buffers) < 0) {
703 vq->shadow_used_idx -= num_buffers;
707 vq->last_avail_idx += num_buffers;
710 do_data_copy_enqueue(dev, vq);
712 if (likely(vq->shadow_used_idx)) {
713 flush_shadow_used_ring(dev, vq);
715 /* flush used->idx update before we read avail->flags. */
718 /* Kick the guest if necessary. */
719 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
720 && (vq->callfd >= 0))
721 eventfd_write(vq->callfd, (eventfd_t)1);
725 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
726 vhost_user_iotlb_rd_unlock(vq);
729 rte_spinlock_unlock(&vq->access_lock);
735 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
736 struct rte_mbuf **pkts, uint16_t count)
738 struct virtio_net *dev = get_device(vid);
743 if (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF))
744 return virtio_dev_merge_rx(dev, queue_id, pkts, count);
746 return virtio_dev_rx(dev, queue_id, pkts, count);
750 virtio_net_with_host_offload(struct virtio_net *dev)
753 ((1ULL << VIRTIO_NET_F_CSUM) |
754 (1ULL << VIRTIO_NET_F_HOST_ECN) |
755 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
756 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
757 (1ULL << VIRTIO_NET_F_HOST_UFO)))
764 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
766 struct ipv4_hdr *ipv4_hdr;
767 struct ipv6_hdr *ipv6_hdr;
769 struct ether_hdr *eth_hdr;
772 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
774 m->l2_len = sizeof(struct ether_hdr);
775 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
777 if (ethertype == ETHER_TYPE_VLAN) {
778 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
780 m->l2_len += sizeof(struct vlan_hdr);
781 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
784 l3_hdr = (char *)eth_hdr + m->l2_len;
787 case ETHER_TYPE_IPv4:
789 *l4_proto = ipv4_hdr->next_proto_id;
790 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
791 *l4_hdr = (char *)l3_hdr + m->l3_len;
792 m->ol_flags |= PKT_TX_IPV4;
794 case ETHER_TYPE_IPv6:
796 *l4_proto = ipv6_hdr->proto;
797 m->l3_len = sizeof(struct ipv6_hdr);
798 *l4_hdr = (char *)l3_hdr + m->l3_len;
799 m->ol_flags |= PKT_TX_IPV6;
809 static __rte_always_inline void
810 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
812 uint16_t l4_proto = 0;
814 struct tcp_hdr *tcp_hdr = NULL;
816 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
819 parse_ethernet(m, &l4_proto, &l4_hdr);
820 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
821 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
822 switch (hdr->csum_offset) {
823 case (offsetof(struct tcp_hdr, cksum)):
824 if (l4_proto == IPPROTO_TCP)
825 m->ol_flags |= PKT_TX_TCP_CKSUM;
827 case (offsetof(struct udp_hdr, dgram_cksum)):
828 if (l4_proto == IPPROTO_UDP)
829 m->ol_flags |= PKT_TX_UDP_CKSUM;
831 case (offsetof(struct sctp_hdr, cksum)):
832 if (l4_proto == IPPROTO_SCTP)
833 m->ol_flags |= PKT_TX_SCTP_CKSUM;
841 if (l4_hdr && hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
842 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
843 case VIRTIO_NET_HDR_GSO_TCPV4:
844 case VIRTIO_NET_HDR_GSO_TCPV6:
846 m->ol_flags |= PKT_TX_TCP_SEG;
847 m->tso_segsz = hdr->gso_size;
848 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
851 RTE_LOG(WARNING, VHOST_DATA,
852 "unsupported gso type %u.\n", hdr->gso_type);
858 #define RARP_PKT_SIZE 64
861 make_rarp_packet(struct rte_mbuf *rarp_mbuf, const struct ether_addr *mac)
863 struct ether_hdr *eth_hdr;
864 struct arp_hdr *rarp;
866 if (rarp_mbuf->buf_len < 64) {
867 RTE_LOG(WARNING, VHOST_DATA,
868 "failed to make RARP; mbuf size too small %u (< %d)\n",
869 rarp_mbuf->buf_len, RARP_PKT_SIZE);
873 /* Ethernet header. */
874 eth_hdr = rte_pktmbuf_mtod_offset(rarp_mbuf, struct ether_hdr *, 0);
875 memset(eth_hdr->d_addr.addr_bytes, 0xff, ETHER_ADDR_LEN);
876 ether_addr_copy(mac, ð_hdr->s_addr);
877 eth_hdr->ether_type = htons(ETHER_TYPE_RARP);
880 rarp = (struct arp_hdr *)(eth_hdr + 1);
881 rarp->arp_hrd = htons(ARP_HRD_ETHER);
882 rarp->arp_pro = htons(ETHER_TYPE_IPv4);
883 rarp->arp_hln = ETHER_ADDR_LEN;
885 rarp->arp_op = htons(ARP_OP_REVREQUEST);
887 ether_addr_copy(mac, &rarp->arp_data.arp_sha);
888 ether_addr_copy(mac, &rarp->arp_data.arp_tha);
889 memset(&rarp->arp_data.arp_sip, 0x00, 4);
890 memset(&rarp->arp_data.arp_tip, 0x00, 4);
892 rarp_mbuf->pkt_len = rarp_mbuf->data_len = RARP_PKT_SIZE;
897 static __rte_always_inline void
898 put_zmbuf(struct zcopy_mbuf *zmbuf)
903 static __rte_always_inline int
904 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
905 struct vring_desc *descs, uint16_t max_desc,
906 struct rte_mbuf *m, uint16_t desc_idx,
907 struct rte_mempool *mbuf_pool)
909 struct vring_desc *desc;
911 uint32_t desc_avail, desc_offset;
912 uint32_t mbuf_avail, mbuf_offset;
914 struct rte_mbuf *cur = m, *prev = m;
915 struct virtio_net_hdr *hdr = NULL;
916 /* A counter to avoid desc dead loop chain */
917 uint32_t nr_desc = 1;
918 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
919 uint16_t copy_nb = vq->batch_copy_nb_elems;
922 desc = &descs[desc_idx];
923 if (unlikely((desc->len < dev->vhost_hlen)) ||
924 (desc->flags & VRING_DESC_F_INDIRECT)) {
929 desc_addr = vhost_iova_to_vva(dev,
933 if (unlikely(!desc_addr)) {
938 if (virtio_net_with_host_offload(dev)) {
939 hdr = (struct virtio_net_hdr *)((uintptr_t)desc_addr);
944 * A virtio driver normally uses at least 2 desc buffers
945 * for Tx: the first for storing the header, and others
946 * for storing the data.
948 if (likely((desc->len == dev->vhost_hlen) &&
949 (desc->flags & VRING_DESC_F_NEXT) != 0)) {
950 desc = &descs[desc->next];
951 if (unlikely(desc->flags & VRING_DESC_F_INDIRECT)) {
956 desc_addr = vhost_iova_to_vva(dev,
960 if (unlikely(!desc_addr)) {
966 desc_avail = desc->len;
969 desc_avail = desc->len - dev->vhost_hlen;
970 desc_offset = dev->vhost_hlen;
973 rte_prefetch0((void *)(uintptr_t)(desc_addr + desc_offset));
975 PRINT_PACKET(dev, (uintptr_t)(desc_addr + desc_offset), desc_avail, 0);
978 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
982 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
985 * A desc buf might across two host physical pages that are
986 * not continuous. In such case (gpa_to_hpa returns 0), data
987 * will be copied even though zero copy is enabled.
989 if (unlikely(dev->dequeue_zero_copy && (hpa = gpa_to_hpa(dev,
990 desc->addr + desc_offset, cpy_len)))) {
991 cur->data_len = cpy_len;
993 cur->buf_addr = (void *)(uintptr_t)(desc_addr
998 * In zero copy mode, one mbuf can only reference data
999 * for one or partial of one desc buff.
1001 mbuf_avail = cpy_len;
1003 if (likely(cpy_len > MAX_BATCH_LEN ||
1004 copy_nb >= vq->size ||
1005 (hdr && cur == m))) {
1006 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
1008 (void *)((uintptr_t)(desc_addr +
1012 batch_copy[copy_nb].dst =
1013 rte_pktmbuf_mtod_offset(cur, void *,
1015 batch_copy[copy_nb].src =
1016 (void *)((uintptr_t)(desc_addr +
1018 batch_copy[copy_nb].len = cpy_len;
1023 mbuf_avail -= cpy_len;
1024 mbuf_offset += cpy_len;
1025 desc_avail -= cpy_len;
1026 desc_offset += cpy_len;
1028 /* This desc reaches to its end, get the next one */
1029 if (desc_avail == 0) {
1030 if ((desc->flags & VRING_DESC_F_NEXT) == 0)
1033 if (unlikely(desc->next >= max_desc ||
1034 ++nr_desc > max_desc)) {
1038 desc = &descs[desc->next];
1039 if (unlikely(desc->flags & VRING_DESC_F_INDIRECT)) {
1044 desc_addr = vhost_iova_to_vva(dev,
1048 if (unlikely(!desc_addr)) {
1053 rte_prefetch0((void *)(uintptr_t)desc_addr);
1056 desc_avail = desc->len;
1058 PRINT_PACKET(dev, (uintptr_t)desc_addr, desc->len, 0);
1062 * This mbuf reaches to its end, get a new one
1063 * to hold more data.
1065 if (mbuf_avail == 0) {
1066 cur = rte_pktmbuf_alloc(mbuf_pool);
1067 if (unlikely(cur == NULL)) {
1068 RTE_LOG(ERR, VHOST_DATA, "Failed to "
1069 "allocate memory for mbuf.\n");
1073 if (unlikely(dev->dequeue_zero_copy))
1074 rte_mbuf_refcnt_update(cur, 1);
1077 prev->data_len = mbuf_offset;
1079 m->pkt_len += mbuf_offset;
1083 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
1087 prev->data_len = mbuf_offset;
1088 m->pkt_len += mbuf_offset;
1091 vhost_dequeue_offload(hdr, m);
1094 vq->batch_copy_nb_elems = copy_nb;
1099 static __rte_always_inline void
1100 update_used_ring(struct virtio_net *dev, struct vhost_virtqueue *vq,
1101 uint32_t used_idx, uint32_t desc_idx)
1103 vq->used->ring[used_idx].id = desc_idx;
1104 vq->used->ring[used_idx].len = 0;
1105 vhost_log_used_vring(dev, vq,
1106 offsetof(struct vring_used, ring[used_idx]),
1107 sizeof(vq->used->ring[used_idx]));
1110 static __rte_always_inline void
1111 update_used_idx(struct virtio_net *dev, struct vhost_virtqueue *vq,
1114 if (unlikely(count == 0))
1120 vq->used->idx += count;
1121 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
1122 sizeof(vq->used->idx));
1124 /* Kick guest if required. */
1125 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
1126 && (vq->callfd >= 0))
1127 eventfd_write(vq->callfd, (eventfd_t)1);
1130 static __rte_always_inline struct zcopy_mbuf *
1131 get_zmbuf(struct vhost_virtqueue *vq)
1137 /* search [last_zmbuf_idx, zmbuf_size) */
1138 i = vq->last_zmbuf_idx;
1139 last = vq->zmbuf_size;
1142 for (; i < last; i++) {
1143 if (vq->zmbufs[i].in_use == 0) {
1144 vq->last_zmbuf_idx = i + 1;
1145 vq->zmbufs[i].in_use = 1;
1146 return &vq->zmbufs[i];
1152 /* search [0, last_zmbuf_idx) */
1154 last = vq->last_zmbuf_idx;
1161 static __rte_always_inline bool
1162 mbuf_is_consumed(struct rte_mbuf *m)
1165 if (rte_mbuf_refcnt_read(m) > 1)
1173 static __rte_always_inline void
1174 restore_mbuf(struct rte_mbuf *m)
1176 uint32_t mbuf_size, priv_size;
1179 priv_size = rte_pktmbuf_priv_size(m->pool);
1180 mbuf_size = sizeof(struct rte_mbuf) + priv_size;
1181 /* start of buffer is after mbuf structure and priv data */
1183 m->buf_addr = (char *)m + mbuf_size;
1184 m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size;
1190 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
1191 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1193 struct virtio_net *dev;
1194 struct rte_mbuf *rarp_mbuf = NULL;
1195 struct vhost_virtqueue *vq;
1196 uint32_t desc_indexes[MAX_PKT_BURST];
1199 uint16_t free_entries;
1202 dev = get_device(vid);
1206 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
1207 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
1208 dev->vid, __func__, queue_id);
1212 vq = dev->virtqueue[queue_id];
1214 if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
1217 if (unlikely(vq->enabled == 0))
1218 goto out_access_unlock;
1220 vq->batch_copy_nb_elems = 0;
1222 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1223 vhost_user_iotlb_rd_lock(vq);
1225 if (unlikely(vq->access_ok == 0))
1226 if (unlikely(vring_translate(dev, vq) < 0))
1229 if (unlikely(dev->dequeue_zero_copy)) {
1230 struct zcopy_mbuf *zmbuf, *next;
1233 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1234 zmbuf != NULL; zmbuf = next) {
1235 next = TAILQ_NEXT(zmbuf, next);
1237 if (mbuf_is_consumed(zmbuf->mbuf)) {
1238 used_idx = vq->last_used_idx++ & (vq->size - 1);
1239 update_used_ring(dev, vq, used_idx,
1243 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1244 restore_mbuf(zmbuf->mbuf);
1245 rte_pktmbuf_free(zmbuf->mbuf);
1251 update_used_idx(dev, vq, nr_updated);
1255 * Construct a RARP broadcast packet, and inject it to the "pkts"
1256 * array, to looks like that guest actually send such packet.
1258 * Check user_send_rarp() for more information.
1260 * broadcast_rarp shares a cacheline in the virtio_net structure
1261 * with some fields that are accessed during enqueue and
1262 * rte_atomic16_cmpset() causes a write if using cmpxchg. This could
1263 * result in false sharing between enqueue and dequeue.
1265 * Prevent unnecessary false sharing by reading broadcast_rarp first
1266 * and only performing cmpset if the read indicates it is likely to
1270 if (unlikely(rte_atomic16_read(&dev->broadcast_rarp) &&
1271 rte_atomic16_cmpset((volatile uint16_t *)
1272 &dev->broadcast_rarp.cnt, 1, 0))) {
1274 rarp_mbuf = rte_pktmbuf_alloc(mbuf_pool);
1275 if (rarp_mbuf == NULL) {
1276 RTE_LOG(ERR, VHOST_DATA,
1277 "Failed to allocate memory for mbuf.\n");
1281 if (make_rarp_packet(rarp_mbuf, &dev->mac)) {
1282 rte_pktmbuf_free(rarp_mbuf);
1289 free_entries = *((volatile uint16_t *)&vq->avail->idx) -
1291 if (free_entries == 0)
1294 LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1296 /* Prefetch available and used ring */
1297 avail_idx = vq->last_avail_idx & (vq->size - 1);
1298 used_idx = vq->last_used_idx & (vq->size - 1);
1299 rte_prefetch0(&vq->avail->ring[avail_idx]);
1300 rte_prefetch0(&vq->used->ring[used_idx]);
1302 count = RTE_MIN(count, MAX_PKT_BURST);
1303 count = RTE_MIN(count, free_entries);
1304 LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1307 /* Retrieve all of the head indexes first to avoid caching issues. */
1308 for (i = 0; i < count; i++) {
1309 avail_idx = (vq->last_avail_idx + i) & (vq->size - 1);
1310 used_idx = (vq->last_used_idx + i) & (vq->size - 1);
1311 desc_indexes[i] = vq->avail->ring[avail_idx];
1313 if (likely(dev->dequeue_zero_copy == 0))
1314 update_used_ring(dev, vq, used_idx, desc_indexes[i]);
1317 /* Prefetch descriptor index. */
1318 rte_prefetch0(&vq->desc[desc_indexes[0]]);
1319 for (i = 0; i < count; i++) {
1320 struct vring_desc *desc;
1324 if (likely(i + 1 < count))
1325 rte_prefetch0(&vq->desc[desc_indexes[i + 1]]);
1327 if (vq->desc[desc_indexes[i]].flags & VRING_DESC_F_INDIRECT) {
1328 desc = (struct vring_desc *)(uintptr_t)
1329 vhost_iova_to_vva(dev, vq,
1330 vq->desc[desc_indexes[i]].addr,
1333 if (unlikely(!desc))
1336 rte_prefetch0(desc);
1337 sz = vq->desc[desc_indexes[i]].len / sizeof(*desc);
1342 idx = desc_indexes[i];
1345 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
1346 if (unlikely(pkts[i] == NULL)) {
1347 RTE_LOG(ERR, VHOST_DATA,
1348 "Failed to allocate memory for mbuf.\n");
1352 err = copy_desc_to_mbuf(dev, vq, desc, sz, pkts[i], idx,
1354 if (unlikely(err)) {
1355 rte_pktmbuf_free(pkts[i]);
1359 if (unlikely(dev->dequeue_zero_copy)) {
1360 struct zcopy_mbuf *zmbuf;
1362 zmbuf = get_zmbuf(vq);
1364 rte_pktmbuf_free(pkts[i]);
1367 zmbuf->mbuf = pkts[i];
1368 zmbuf->desc_idx = desc_indexes[i];
1371 * Pin lock the mbuf; we will check later to see
1372 * whether the mbuf is freed (when we are the last
1373 * user) or not. If that's the case, we then could
1374 * update the used ring safely.
1376 rte_mbuf_refcnt_update(pkts[i], 1);
1379 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1382 vq->last_avail_idx += i;
1384 if (likely(dev->dequeue_zero_copy == 0)) {
1385 do_data_copy_dequeue(vq);
1386 vq->last_used_idx += i;
1387 update_used_idx(dev, vq, i);
1391 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1392 vhost_user_iotlb_rd_unlock(vq);
1395 rte_spinlock_unlock(&vq->access_lock);
1397 if (unlikely(rarp_mbuf != NULL)) {
1399 * Inject it to the head of "pkts" array, so that switch's mac
1400 * learning table will get updated first.
1402 memmove(&pkts[1], pkts, i * sizeof(struct rte_mbuf *));
1403 pkts[0] = rarp_mbuf;