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>
51 #define MAX_PKT_BURST 32
53 #define MAX_BATCH_LEN 256
56 is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t nr_vring)
58 return (is_tx ^ (idx & 1)) == 0 && idx < nr_vring;
61 static __rte_always_inline void
62 do_flush_shadow_used_ring(struct virtio_net *dev, struct vhost_virtqueue *vq,
63 uint16_t to, uint16_t from, uint16_t size)
65 rte_memcpy(&vq->used->ring[to],
66 &vq->shadow_used_ring[from],
67 size * sizeof(struct vring_used_elem));
68 vhost_log_used_vring(dev, vq,
69 offsetof(struct vring_used, ring[to]),
70 size * sizeof(struct vring_used_elem));
73 static __rte_always_inline void
74 flush_shadow_used_ring(struct virtio_net *dev, struct vhost_virtqueue *vq)
76 uint16_t used_idx = vq->last_used_idx & (vq->size - 1);
78 if (used_idx + vq->shadow_used_idx <= vq->size) {
79 do_flush_shadow_used_ring(dev, vq, used_idx, 0,
84 /* update used ring interval [used_idx, vq->size] */
85 size = vq->size - used_idx;
86 do_flush_shadow_used_ring(dev, vq, used_idx, 0, size);
88 /* update the left half used ring interval [0, left_size] */
89 do_flush_shadow_used_ring(dev, vq, 0, size,
90 vq->shadow_used_idx - size);
92 vq->last_used_idx += vq->shadow_used_idx;
96 *(volatile uint16_t *)&vq->used->idx += vq->shadow_used_idx;
97 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
98 sizeof(vq->used->idx));
101 static __rte_always_inline void
102 update_shadow_used_ring(struct vhost_virtqueue *vq,
103 uint16_t desc_idx, uint16_t len)
105 uint16_t i = vq->shadow_used_idx++;
107 vq->shadow_used_ring[i].id = desc_idx;
108 vq->shadow_used_ring[i].len = len;
112 do_data_copy_enqueue(struct virtio_net *dev, struct vhost_virtqueue *vq)
114 struct batch_copy_elem *elem = vq->batch_copy_elems;
115 uint16_t count = vq->batch_copy_nb_elems;
118 for (i = 0; i < count; i++) {
119 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
120 vhost_log_write(dev, elem[i].log_addr, elem[i].len);
121 PRINT_PACKET(dev, (uintptr_t)elem[i].dst, elem[i].len, 0);
126 do_data_copy_dequeue(struct vhost_virtqueue *vq)
128 struct batch_copy_elem *elem = vq->batch_copy_elems;
129 uint16_t count = vq->batch_copy_nb_elems;
132 for (i = 0; i < count; i++)
133 rte_memcpy(elem[i].dst, elem[i].src, elem[i].len);
136 /* avoid write operation when necessary, to lessen cache issues */
137 #define ASSIGN_UNLESS_EQUAL(var, val) do { \
138 if ((var) != (val)) \
143 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
145 uint64_t csum_l4 = m_buf->ol_flags & PKT_TX_L4_MASK;
147 if (m_buf->ol_flags & PKT_TX_TCP_SEG)
148 csum_l4 |= PKT_TX_TCP_CKSUM;
151 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
152 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
155 case PKT_TX_TCP_CKSUM:
156 net_hdr->csum_offset = (offsetof(struct tcp_hdr,
159 case PKT_TX_UDP_CKSUM:
160 net_hdr->csum_offset = (offsetof(struct udp_hdr,
163 case PKT_TX_SCTP_CKSUM:
164 net_hdr->csum_offset = (offsetof(struct sctp_hdr,
169 ASSIGN_UNLESS_EQUAL(net_hdr->csum_start, 0);
170 ASSIGN_UNLESS_EQUAL(net_hdr->csum_offset, 0);
171 ASSIGN_UNLESS_EQUAL(net_hdr->flags, 0);
174 /* IP cksum verification cannot be bypassed, then calculate here */
175 if (m_buf->ol_flags & PKT_TX_IP_CKSUM) {
176 struct ipv4_hdr *ipv4_hdr;
178 ipv4_hdr = rte_pktmbuf_mtod_offset(m_buf, struct ipv4_hdr *,
180 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
183 if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
184 if (m_buf->ol_flags & PKT_TX_IPV4)
185 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
187 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
188 net_hdr->gso_size = m_buf->tso_segsz;
189 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
192 ASSIGN_UNLESS_EQUAL(net_hdr->gso_type, 0);
193 ASSIGN_UNLESS_EQUAL(net_hdr->gso_size, 0);
194 ASSIGN_UNLESS_EQUAL(net_hdr->hdr_len, 0);
198 static __rte_always_inline int
199 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
200 struct vring_desc *descs, struct rte_mbuf *m,
201 uint16_t desc_idx, uint32_t size)
203 uint32_t desc_avail, desc_offset;
204 uint32_t mbuf_avail, mbuf_offset;
206 struct vring_desc *desc;
208 /* A counter to avoid desc dead loop chain */
209 uint16_t nr_desc = 1;
210 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
211 uint16_t copy_nb = vq->batch_copy_nb_elems;
214 desc = &descs[desc_idx];
215 desc_addr = vhost_iova_to_vva(dev, vq, desc->addr,
216 desc->len, VHOST_ACCESS_RW);
218 * Checking of 'desc_addr' placed outside of 'unlikely' macro to avoid
219 * performance issue with some versions of gcc (4.8.4 and 5.3.0) which
220 * otherwise stores offset on the stack instead of in a register.
222 if (unlikely(desc->len < dev->vhost_hlen) || !desc_addr) {
227 rte_prefetch0((void *)(uintptr_t)desc_addr);
229 virtio_enqueue_offload(m, (struct virtio_net_hdr *)(uintptr_t)desc_addr);
230 vhost_log_write(dev, desc->addr, dev->vhost_hlen);
231 PRINT_PACKET(dev, (uintptr_t)desc_addr, dev->vhost_hlen, 0);
233 desc_offset = dev->vhost_hlen;
234 desc_avail = desc->len - dev->vhost_hlen;
236 mbuf_avail = rte_pktmbuf_data_len(m);
238 while (mbuf_avail != 0 || m->next != NULL) {
239 /* done with current mbuf, fetch next */
240 if (mbuf_avail == 0) {
244 mbuf_avail = rte_pktmbuf_data_len(m);
247 /* done with current desc buf, fetch next */
248 if (desc_avail == 0) {
249 if ((desc->flags & VRING_DESC_F_NEXT) == 0) {
250 /* Room in vring buffer is not enough */
254 if (unlikely(desc->next >= size || ++nr_desc > size)) {
259 desc = &descs[desc->next];
260 desc_addr = vhost_iova_to_vva(dev, vq, desc->addr,
263 if (unlikely(!desc_addr)) {
269 desc_avail = desc->len;
272 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
273 if (likely(cpy_len > MAX_BATCH_LEN || copy_nb >= vq->size)) {
274 rte_memcpy((void *)((uintptr_t)(desc_addr +
276 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
278 vhost_log_write(dev, desc->addr + desc_offset, cpy_len);
279 PRINT_PACKET(dev, (uintptr_t)(desc_addr + desc_offset),
282 batch_copy[copy_nb].dst =
283 (void *)((uintptr_t)(desc_addr + desc_offset));
284 batch_copy[copy_nb].src =
285 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
286 batch_copy[copy_nb].log_addr = desc->addr + desc_offset;
287 batch_copy[copy_nb].len = cpy_len;
291 mbuf_avail -= cpy_len;
292 mbuf_offset += cpy_len;
293 desc_avail -= cpy_len;
294 desc_offset += cpy_len;
298 vq->batch_copy_nb_elems = copy_nb;
304 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
305 * be received from the physical port or from another virtio device. A packet
306 * count is returned to indicate the number of packets that are successfully
307 * added to the RX queue. This function works when the mbuf is scattered, but
308 * it doesn't support the mergeable feature.
310 static __rte_always_inline uint32_t
311 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
312 struct rte_mbuf **pkts, uint32_t count)
314 struct vhost_virtqueue *vq;
315 uint16_t avail_idx, free_entries, start_idx;
316 uint16_t desc_indexes[MAX_PKT_BURST];
317 struct vring_desc *descs;
321 LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
322 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
323 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
324 dev->vid, __func__, queue_id);
328 vq = dev->virtqueue[queue_id];
329 if (unlikely(vq->enabled == 0))
332 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
333 vhost_user_iotlb_rd_lock(vq);
335 if (unlikely(vq->access_ok == 0)) {
336 if (unlikely(vring_translate(dev, vq) < 0)) {
342 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
343 start_idx = vq->last_used_idx;
344 free_entries = avail_idx - start_idx;
345 count = RTE_MIN(count, free_entries);
346 count = RTE_MIN(count, (uint32_t)MAX_PKT_BURST);
350 LOG_DEBUG(VHOST_DATA, "(%d) start_idx %d | end_idx %d\n",
351 dev->vid, start_idx, start_idx + count);
353 vq->batch_copy_nb_elems = 0;
355 /* Retrieve all of the desc indexes first to avoid caching issues. */
356 rte_prefetch0(&vq->avail->ring[start_idx & (vq->size - 1)]);
357 for (i = 0; i < count; i++) {
358 used_idx = (start_idx + i) & (vq->size - 1);
359 desc_indexes[i] = vq->avail->ring[used_idx];
360 vq->used->ring[used_idx].id = desc_indexes[i];
361 vq->used->ring[used_idx].len = pkts[i]->pkt_len +
363 vhost_log_used_vring(dev, vq,
364 offsetof(struct vring_used, ring[used_idx]),
365 sizeof(vq->used->ring[used_idx]));
368 rte_prefetch0(&vq->desc[desc_indexes[0]]);
369 for (i = 0; i < count; i++) {
370 uint16_t desc_idx = desc_indexes[i];
373 if (vq->desc[desc_idx].flags & VRING_DESC_F_INDIRECT) {
374 descs = (struct vring_desc *)(uintptr_t)
375 vhost_iova_to_vva(dev,
376 vq, vq->desc[desc_idx].addr,
377 vq->desc[desc_idx].len,
379 if (unlikely(!descs)) {
385 sz = vq->desc[desc_idx].len / sizeof(*descs);
391 err = copy_mbuf_to_desc(dev, vq, descs, pkts[i], desc_idx, sz);
398 rte_prefetch0(&vq->desc[desc_indexes[i+1]]);
401 do_data_copy_enqueue(dev, vq);
405 *(volatile uint16_t *)&vq->used->idx += count;
406 vq->last_used_idx += count;
407 vhost_log_used_vring(dev, vq,
408 offsetof(struct vring_used, idx),
409 sizeof(vq->used->idx));
411 /* flush used->idx update before we read avail->flags. */
414 /* Kick the guest if necessary. */
415 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
416 && (vq->callfd >= 0))
417 eventfd_write(vq->callfd, (eventfd_t)1);
419 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
420 vhost_user_iotlb_rd_unlock(vq);
425 static __rte_always_inline int
426 fill_vec_buf(struct virtio_net *dev, struct vhost_virtqueue *vq,
427 uint32_t avail_idx, uint32_t *vec_idx,
428 struct buf_vector *buf_vec, uint16_t *desc_chain_head,
429 uint16_t *desc_chain_len)
431 uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
432 uint32_t vec_id = *vec_idx;
434 struct vring_desc *descs = vq->desc;
436 *desc_chain_head = idx;
438 if (vq->desc[idx].flags & VRING_DESC_F_INDIRECT) {
439 descs = (struct vring_desc *)(uintptr_t)
440 vhost_iova_to_vva(dev, vq, vq->desc[idx].addr,
443 if (unlikely(!descs))
450 if (unlikely(vec_id >= BUF_VECTOR_MAX || idx >= vq->size))
453 len += descs[idx].len;
454 buf_vec[vec_id].buf_addr = descs[idx].addr;
455 buf_vec[vec_id].buf_len = descs[idx].len;
456 buf_vec[vec_id].desc_idx = idx;
459 if ((descs[idx].flags & VRING_DESC_F_NEXT) == 0)
462 idx = descs[idx].next;
465 *desc_chain_len = len;
472 * Returns -1 on fail, 0 on success
475 reserve_avail_buf_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,
476 uint32_t size, struct buf_vector *buf_vec,
477 uint16_t *num_buffers, uint16_t avail_head)
480 uint32_t vec_idx = 0;
483 uint16_t head_idx = 0;
487 cur_idx = vq->last_avail_idx;
490 if (unlikely(cur_idx == avail_head))
493 if (unlikely(fill_vec_buf(dev, vq, cur_idx, &vec_idx, buf_vec,
494 &head_idx, &len) < 0))
496 len = RTE_MIN(len, size);
497 update_shadow_used_ring(vq, head_idx, len);
505 * if we tried all available ring items, and still
506 * can't get enough buf, it means something abnormal
509 if (unlikely(tries >= vq->size))
516 static __rte_always_inline int
517 copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,
518 struct rte_mbuf *m, struct buf_vector *buf_vec,
519 uint16_t num_buffers)
521 uint32_t vec_idx = 0;
523 uint32_t mbuf_offset, mbuf_avail;
524 uint32_t desc_offset, desc_avail;
526 uint64_t hdr_addr, hdr_phys_addr;
527 struct rte_mbuf *hdr_mbuf;
528 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
529 uint16_t copy_nb = vq->batch_copy_nb_elems;
532 if (unlikely(m == NULL)) {
537 desc_addr = vhost_iova_to_vva(dev, vq, buf_vec[vec_idx].buf_addr,
538 buf_vec[vec_idx].buf_len,
540 if (buf_vec[vec_idx].buf_len < dev->vhost_hlen || !desc_addr) {
546 hdr_addr = desc_addr;
547 hdr_phys_addr = buf_vec[vec_idx].buf_addr;
548 rte_prefetch0((void *)(uintptr_t)hdr_addr);
550 LOG_DEBUG(VHOST_DATA, "(%d) RX: num merge buffers %d\n",
551 dev->vid, num_buffers);
553 desc_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
554 desc_offset = dev->vhost_hlen;
556 mbuf_avail = rte_pktmbuf_data_len(m);
558 while (mbuf_avail != 0 || m->next != NULL) {
559 /* done with current desc buf, get the next one */
560 if (desc_avail == 0) {
563 vhost_iova_to_vva(dev, vq,
564 buf_vec[vec_idx].buf_addr,
565 buf_vec[vec_idx].buf_len,
567 if (unlikely(!desc_addr)) {
572 /* Prefetch buffer address. */
573 rte_prefetch0((void *)(uintptr_t)desc_addr);
575 desc_avail = buf_vec[vec_idx].buf_len;
578 /* done with current mbuf, get the next one */
579 if (mbuf_avail == 0) {
583 mbuf_avail = rte_pktmbuf_data_len(m);
587 struct virtio_net_hdr_mrg_rxbuf *hdr;
589 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)
591 virtio_enqueue_offload(hdr_mbuf, &hdr->hdr);
592 ASSIGN_UNLESS_EQUAL(hdr->num_buffers, num_buffers);
594 vhost_log_write(dev, hdr_phys_addr, dev->vhost_hlen);
595 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
601 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
603 if (likely(cpy_len > MAX_BATCH_LEN || copy_nb >= vq->size)) {
604 rte_memcpy((void *)((uintptr_t)(desc_addr +
606 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
609 buf_vec[vec_idx].buf_addr + desc_offset,
611 PRINT_PACKET(dev, (uintptr_t)(desc_addr + desc_offset),
614 batch_copy[copy_nb].dst =
615 (void *)((uintptr_t)(desc_addr + desc_offset));
616 batch_copy[copy_nb].src =
617 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset);
618 batch_copy[copy_nb].log_addr =
619 buf_vec[vec_idx].buf_addr + desc_offset;
620 batch_copy[copy_nb].len = cpy_len;
624 mbuf_avail -= cpy_len;
625 mbuf_offset += cpy_len;
626 desc_avail -= cpy_len;
627 desc_offset += cpy_len;
631 vq->batch_copy_nb_elems = copy_nb;
636 static __rte_always_inline uint32_t
637 virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,
638 struct rte_mbuf **pkts, uint32_t count)
640 struct vhost_virtqueue *vq;
641 uint32_t pkt_idx = 0;
642 uint16_t num_buffers;
643 struct buf_vector buf_vec[BUF_VECTOR_MAX];
646 LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
647 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->nr_vring))) {
648 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
649 dev->vid, __func__, queue_id);
653 vq = dev->virtqueue[queue_id];
654 if (unlikely(vq->enabled == 0))
657 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
658 vhost_user_iotlb_rd_lock(vq);
660 if (unlikely(vq->access_ok == 0))
661 if (unlikely(vring_translate(dev, vq) < 0))
664 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
668 vq->batch_copy_nb_elems = 0;
670 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
672 vq->shadow_used_idx = 0;
673 avail_head = *((volatile uint16_t *)&vq->avail->idx);
674 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
675 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
677 if (unlikely(reserve_avail_buf_mergeable(dev, vq,
678 pkt_len, buf_vec, &num_buffers,
680 LOG_DEBUG(VHOST_DATA,
681 "(%d) failed to get enough desc from vring\n",
683 vq->shadow_used_idx -= num_buffers;
687 LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
688 dev->vid, vq->last_avail_idx,
689 vq->last_avail_idx + num_buffers);
691 if (copy_mbuf_to_desc_mergeable(dev, vq, pkts[pkt_idx],
692 buf_vec, num_buffers) < 0) {
693 vq->shadow_used_idx -= num_buffers;
697 vq->last_avail_idx += num_buffers;
700 do_data_copy_enqueue(dev, vq);
702 if (likely(vq->shadow_used_idx)) {
703 flush_shadow_used_ring(dev, vq);
705 /* flush used->idx update before we read avail->flags. */
708 /* Kick the guest if necessary. */
709 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
710 && (vq->callfd >= 0))
711 eventfd_write(vq->callfd, (eventfd_t)1);
715 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
716 vhost_user_iotlb_rd_unlock(vq);
722 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
723 struct rte_mbuf **pkts, uint16_t count)
725 struct virtio_net *dev = get_device(vid);
730 if (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF))
731 return virtio_dev_merge_rx(dev, queue_id, pkts, count);
733 return virtio_dev_rx(dev, queue_id, pkts, count);
737 virtio_net_with_host_offload(struct virtio_net *dev)
740 ((1ULL << VIRTIO_NET_F_CSUM) |
741 (1ULL << VIRTIO_NET_F_HOST_ECN) |
742 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
743 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
744 (1ULL << VIRTIO_NET_F_HOST_UFO)))
751 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
753 struct ipv4_hdr *ipv4_hdr;
754 struct ipv6_hdr *ipv6_hdr;
756 struct ether_hdr *eth_hdr;
759 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
761 m->l2_len = sizeof(struct ether_hdr);
762 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
764 if (ethertype == ETHER_TYPE_VLAN) {
765 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
767 m->l2_len += sizeof(struct vlan_hdr);
768 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
771 l3_hdr = (char *)eth_hdr + m->l2_len;
774 case ETHER_TYPE_IPv4:
776 *l4_proto = ipv4_hdr->next_proto_id;
777 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
778 *l4_hdr = (char *)l3_hdr + m->l3_len;
779 m->ol_flags |= PKT_TX_IPV4;
781 case ETHER_TYPE_IPv6:
783 *l4_proto = ipv6_hdr->proto;
784 m->l3_len = sizeof(struct ipv6_hdr);
785 *l4_hdr = (char *)l3_hdr + m->l3_len;
786 m->ol_flags |= PKT_TX_IPV6;
796 static __rte_always_inline void
797 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
799 uint16_t l4_proto = 0;
801 struct tcp_hdr *tcp_hdr = NULL;
803 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
806 parse_ethernet(m, &l4_proto, &l4_hdr);
807 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
808 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
809 switch (hdr->csum_offset) {
810 case (offsetof(struct tcp_hdr, cksum)):
811 if (l4_proto == IPPROTO_TCP)
812 m->ol_flags |= PKT_TX_TCP_CKSUM;
814 case (offsetof(struct udp_hdr, dgram_cksum)):
815 if (l4_proto == IPPROTO_UDP)
816 m->ol_flags |= PKT_TX_UDP_CKSUM;
818 case (offsetof(struct sctp_hdr, cksum)):
819 if (l4_proto == IPPROTO_SCTP)
820 m->ol_flags |= PKT_TX_SCTP_CKSUM;
828 if (l4_hdr && hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
829 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
830 case VIRTIO_NET_HDR_GSO_TCPV4:
831 case VIRTIO_NET_HDR_GSO_TCPV6:
833 m->ol_flags |= PKT_TX_TCP_SEG;
834 m->tso_segsz = hdr->gso_size;
835 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
838 RTE_LOG(WARNING, VHOST_DATA,
839 "unsupported gso type %u.\n", hdr->gso_type);
845 #define RARP_PKT_SIZE 64
848 make_rarp_packet(struct rte_mbuf *rarp_mbuf, const struct ether_addr *mac)
850 struct ether_hdr *eth_hdr;
851 struct arp_hdr *rarp;
853 if (rarp_mbuf->buf_len < 64) {
854 RTE_LOG(WARNING, VHOST_DATA,
855 "failed to make RARP; mbuf size too small %u (< %d)\n",
856 rarp_mbuf->buf_len, RARP_PKT_SIZE);
860 /* Ethernet header. */
861 eth_hdr = rte_pktmbuf_mtod_offset(rarp_mbuf, struct ether_hdr *, 0);
862 memset(eth_hdr->d_addr.addr_bytes, 0xff, ETHER_ADDR_LEN);
863 ether_addr_copy(mac, ð_hdr->s_addr);
864 eth_hdr->ether_type = htons(ETHER_TYPE_RARP);
867 rarp = (struct arp_hdr *)(eth_hdr + 1);
868 rarp->arp_hrd = htons(ARP_HRD_ETHER);
869 rarp->arp_pro = htons(ETHER_TYPE_IPv4);
870 rarp->arp_hln = ETHER_ADDR_LEN;
872 rarp->arp_op = htons(ARP_OP_REVREQUEST);
874 ether_addr_copy(mac, &rarp->arp_data.arp_sha);
875 ether_addr_copy(mac, &rarp->arp_data.arp_tha);
876 memset(&rarp->arp_data.arp_sip, 0x00, 4);
877 memset(&rarp->arp_data.arp_tip, 0x00, 4);
879 rarp_mbuf->pkt_len = rarp_mbuf->data_len = RARP_PKT_SIZE;
884 static __rte_always_inline void
885 put_zmbuf(struct zcopy_mbuf *zmbuf)
890 static __rte_always_inline int
891 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
892 struct vring_desc *descs, uint16_t max_desc,
893 struct rte_mbuf *m, uint16_t desc_idx,
894 struct rte_mempool *mbuf_pool)
896 struct vring_desc *desc;
898 uint32_t desc_avail, desc_offset;
899 uint32_t mbuf_avail, mbuf_offset;
901 struct rte_mbuf *cur = m, *prev = m;
902 struct virtio_net_hdr *hdr = NULL;
903 /* A counter to avoid desc dead loop chain */
904 uint32_t nr_desc = 1;
905 struct batch_copy_elem *batch_copy = vq->batch_copy_elems;
906 uint16_t copy_nb = vq->batch_copy_nb_elems;
909 desc = &descs[desc_idx];
910 if (unlikely((desc->len < dev->vhost_hlen)) ||
911 (desc->flags & VRING_DESC_F_INDIRECT)) {
916 desc_addr = vhost_iova_to_vva(dev,
920 if (unlikely(!desc_addr)) {
925 if (virtio_net_with_host_offload(dev)) {
926 hdr = (struct virtio_net_hdr *)((uintptr_t)desc_addr);
931 * A virtio driver normally uses at least 2 desc buffers
932 * for Tx: the first for storing the header, and others
933 * for storing the data.
935 if (likely((desc->len == dev->vhost_hlen) &&
936 (desc->flags & VRING_DESC_F_NEXT) != 0)) {
937 desc = &descs[desc->next];
938 if (unlikely(desc->flags & VRING_DESC_F_INDIRECT)) {
943 desc_addr = vhost_iova_to_vva(dev,
947 if (unlikely(!desc_addr)) {
953 desc_avail = desc->len;
956 desc_avail = desc->len - dev->vhost_hlen;
957 desc_offset = dev->vhost_hlen;
960 rte_prefetch0((void *)(uintptr_t)(desc_addr + desc_offset));
962 PRINT_PACKET(dev, (uintptr_t)(desc_addr + desc_offset), desc_avail, 0);
965 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
969 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
972 * A desc buf might across two host physical pages that are
973 * not continuous. In such case (gpa_to_hpa returns 0), data
974 * will be copied even though zero copy is enabled.
976 if (unlikely(dev->dequeue_zero_copy && (hpa = gpa_to_hpa(dev,
977 desc->addr + desc_offset, cpy_len)))) {
978 cur->data_len = cpy_len;
980 cur->buf_addr = (void *)(uintptr_t)desc_addr;
981 cur->buf_physaddr = hpa;
984 * In zero copy mode, one mbuf can only reference data
985 * for one or partial of one desc buff.
987 mbuf_avail = cpy_len;
989 if (likely(cpy_len > MAX_BATCH_LEN ||
990 copy_nb >= vq->size)) {
991 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
993 (void *)((uintptr_t)(desc_addr +
997 batch_copy[copy_nb].dst =
998 rte_pktmbuf_mtod_offset(cur, void *,
1000 batch_copy[copy_nb].src =
1001 (void *)((uintptr_t)(desc_addr +
1003 batch_copy[copy_nb].len = cpy_len;
1008 mbuf_avail -= cpy_len;
1009 mbuf_offset += cpy_len;
1010 desc_avail -= cpy_len;
1011 desc_offset += cpy_len;
1013 /* This desc reaches to its end, get the next one */
1014 if (desc_avail == 0) {
1015 if ((desc->flags & VRING_DESC_F_NEXT) == 0)
1018 if (unlikely(desc->next >= max_desc ||
1019 ++nr_desc > max_desc)) {
1023 desc = &descs[desc->next];
1024 if (unlikely(desc->flags & VRING_DESC_F_INDIRECT)) {
1029 desc_addr = vhost_iova_to_vva(dev,
1033 if (unlikely(!desc_addr)) {
1038 rte_prefetch0((void *)(uintptr_t)desc_addr);
1041 desc_avail = desc->len;
1043 PRINT_PACKET(dev, (uintptr_t)desc_addr, desc->len, 0);
1047 * This mbuf reaches to its end, get a new one
1048 * to hold more data.
1050 if (mbuf_avail == 0) {
1051 cur = rte_pktmbuf_alloc(mbuf_pool);
1052 if (unlikely(cur == NULL)) {
1053 RTE_LOG(ERR, VHOST_DATA, "Failed to "
1054 "allocate memory for mbuf.\n");
1058 if (unlikely(dev->dequeue_zero_copy))
1059 rte_mbuf_refcnt_update(cur, 1);
1062 prev->data_len = mbuf_offset;
1064 m->pkt_len += mbuf_offset;
1068 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
1072 prev->data_len = mbuf_offset;
1073 m->pkt_len += mbuf_offset;
1076 vhost_dequeue_offload(hdr, m);
1079 vq->batch_copy_nb_elems = copy_nb;
1084 static __rte_always_inline void
1085 update_used_ring(struct virtio_net *dev, struct vhost_virtqueue *vq,
1086 uint32_t used_idx, uint32_t desc_idx)
1088 vq->used->ring[used_idx].id = desc_idx;
1089 vq->used->ring[used_idx].len = 0;
1090 vhost_log_used_vring(dev, vq,
1091 offsetof(struct vring_used, ring[used_idx]),
1092 sizeof(vq->used->ring[used_idx]));
1095 static __rte_always_inline void
1096 update_used_idx(struct virtio_net *dev, struct vhost_virtqueue *vq,
1099 if (unlikely(count == 0))
1105 vq->used->idx += count;
1106 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
1107 sizeof(vq->used->idx));
1109 /* Kick guest if required. */
1110 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
1111 && (vq->callfd >= 0))
1112 eventfd_write(vq->callfd, (eventfd_t)1);
1115 static __rte_always_inline struct zcopy_mbuf *
1116 get_zmbuf(struct vhost_virtqueue *vq)
1122 /* search [last_zmbuf_idx, zmbuf_size) */
1123 i = vq->last_zmbuf_idx;
1124 last = vq->zmbuf_size;
1127 for (; i < last; i++) {
1128 if (vq->zmbufs[i].in_use == 0) {
1129 vq->last_zmbuf_idx = i + 1;
1130 vq->zmbufs[i].in_use = 1;
1131 return &vq->zmbufs[i];
1137 /* search [0, last_zmbuf_idx) */
1139 last = vq->last_zmbuf_idx;
1146 static __rte_always_inline bool
1147 mbuf_is_consumed(struct rte_mbuf *m)
1150 if (rte_mbuf_refcnt_read(m) > 1)
1159 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
1160 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1162 struct virtio_net *dev;
1163 struct rte_mbuf *rarp_mbuf = NULL;
1164 struct vhost_virtqueue *vq;
1165 uint32_t desc_indexes[MAX_PKT_BURST];
1168 uint16_t free_entries;
1171 dev = get_device(vid);
1175 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->nr_vring))) {
1176 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
1177 dev->vid, __func__, queue_id);
1181 vq = dev->virtqueue[queue_id];
1182 if (unlikely(vq->enabled == 0))
1185 vq->batch_copy_nb_elems = 0;
1187 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1188 vhost_user_iotlb_rd_lock(vq);
1190 if (unlikely(vq->access_ok == 0))
1191 if (unlikely(vring_translate(dev, vq) < 0))
1194 if (unlikely(dev->dequeue_zero_copy)) {
1195 struct zcopy_mbuf *zmbuf, *next;
1198 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1199 zmbuf != NULL; zmbuf = next) {
1200 next = TAILQ_NEXT(zmbuf, next);
1202 if (mbuf_is_consumed(zmbuf->mbuf)) {
1203 used_idx = vq->last_used_idx++ & (vq->size - 1);
1204 update_used_ring(dev, vq, used_idx,
1208 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1209 rte_pktmbuf_free(zmbuf->mbuf);
1215 update_used_idx(dev, vq, nr_updated);
1219 * Construct a RARP broadcast packet, and inject it to the "pkts"
1220 * array, to looks like that guest actually send such packet.
1222 * Check user_send_rarp() for more information.
1224 * broadcast_rarp shares a cacheline in the virtio_net structure
1225 * with some fields that are accessed during enqueue and
1226 * rte_atomic16_cmpset() causes a write if using cmpxchg. This could
1227 * result in false sharing between enqueue and dequeue.
1229 * Prevent unnecessary false sharing by reading broadcast_rarp first
1230 * and only performing cmpset if the read indicates it is likely to
1234 if (unlikely(rte_atomic16_read(&dev->broadcast_rarp) &&
1235 rte_atomic16_cmpset((volatile uint16_t *)
1236 &dev->broadcast_rarp.cnt, 1, 0))) {
1238 rarp_mbuf = rte_pktmbuf_alloc(mbuf_pool);
1239 if (rarp_mbuf == NULL) {
1240 RTE_LOG(ERR, VHOST_DATA,
1241 "Failed to allocate memory for mbuf.\n");
1245 if (make_rarp_packet(rarp_mbuf, &dev->mac)) {
1246 rte_pktmbuf_free(rarp_mbuf);
1253 free_entries = *((volatile uint16_t *)&vq->avail->idx) -
1255 if (free_entries == 0)
1258 LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1260 /* Prefetch available and used ring */
1261 avail_idx = vq->last_avail_idx & (vq->size - 1);
1262 used_idx = vq->last_used_idx & (vq->size - 1);
1263 rte_prefetch0(&vq->avail->ring[avail_idx]);
1264 rte_prefetch0(&vq->used->ring[used_idx]);
1266 count = RTE_MIN(count, MAX_PKT_BURST);
1267 count = RTE_MIN(count, free_entries);
1268 LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1271 /* Retrieve all of the head indexes first to avoid caching issues. */
1272 for (i = 0; i < count; i++) {
1273 avail_idx = (vq->last_avail_idx + i) & (vq->size - 1);
1274 used_idx = (vq->last_used_idx + i) & (vq->size - 1);
1275 desc_indexes[i] = vq->avail->ring[avail_idx];
1277 if (likely(dev->dequeue_zero_copy == 0))
1278 update_used_ring(dev, vq, used_idx, desc_indexes[i]);
1281 /* Prefetch descriptor index. */
1282 rte_prefetch0(&vq->desc[desc_indexes[0]]);
1283 for (i = 0; i < count; i++) {
1284 struct vring_desc *desc;
1288 if (likely(i + 1 < count))
1289 rte_prefetch0(&vq->desc[desc_indexes[i + 1]]);
1291 if (vq->desc[desc_indexes[i]].flags & VRING_DESC_F_INDIRECT) {
1292 desc = (struct vring_desc *)(uintptr_t)
1293 vhost_iova_to_vva(dev, vq,
1294 vq->desc[desc_indexes[i]].addr,
1297 if (unlikely(!desc))
1300 rte_prefetch0(desc);
1301 sz = vq->desc[desc_indexes[i]].len / sizeof(*desc);
1306 idx = desc_indexes[i];
1309 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
1310 if (unlikely(pkts[i] == NULL)) {
1311 RTE_LOG(ERR, VHOST_DATA,
1312 "Failed to allocate memory for mbuf.\n");
1316 err = copy_desc_to_mbuf(dev, vq, desc, sz, pkts[i], idx,
1318 if (unlikely(err)) {
1319 rte_pktmbuf_free(pkts[i]);
1323 if (unlikely(dev->dequeue_zero_copy)) {
1324 struct zcopy_mbuf *zmbuf;
1326 zmbuf = get_zmbuf(vq);
1328 rte_pktmbuf_free(pkts[i]);
1331 zmbuf->mbuf = pkts[i];
1332 zmbuf->desc_idx = desc_indexes[i];
1335 * Pin lock the mbuf; we will check later to see
1336 * whether the mbuf is freed (when we are the last
1337 * user) or not. If that's the case, we then could
1338 * update the used ring safely.
1340 rte_mbuf_refcnt_update(pkts[i], 1);
1343 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1346 vq->last_avail_idx += i;
1348 if (likely(dev->dequeue_zero_copy == 0)) {
1349 do_data_copy_dequeue(vq);
1350 vq->last_used_idx += i;
1351 update_used_idx(dev, vq, i);
1355 if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
1356 vhost_user_iotlb_rd_unlock(vq);
1358 if (unlikely(rarp_mbuf != NULL)) {
1360 * Inject it to the head of "pkts" array, so that switch's mac
1361 * learning table will get updated first.
1363 memmove(&pkts[1], pkts, i * sizeof(struct rte_mbuf *));
1364 pkts[0] = rarp_mbuf;