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_virtio_net.h>
47 #include <rte_spinlock.h>
51 #define MAX_PKT_BURST 32
52 #define VHOST_LOG_PAGE 4096
55 * Atomically set a bit in memory.
57 static inline void __attribute__((always_inline))
58 vhost_set_bit(unsigned int nr, volatile uint8_t *addr)
60 __sync_fetch_and_or_8(addr, (1U << nr));
63 static inline void __attribute__((always_inline))
64 vhost_log_page(uint8_t *log_base, uint64_t page)
66 vhost_set_bit(page % 8, &log_base[page / 8]);
69 static inline void __attribute__((always_inline))
70 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
74 if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
75 !dev->log_base || !len))
78 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
81 /* To make sure guest memory updates are committed before logging */
84 page = addr / VHOST_LOG_PAGE;
85 while (page * VHOST_LOG_PAGE < addr + len) {
86 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
91 static inline void __attribute__((always_inline))
92 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
93 uint64_t offset, uint64_t len)
95 vhost_log_write(dev, vq->log_guest_addr + offset, len);
99 is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t qp_nb)
101 return (is_tx ^ (idx & 1)) == 0 && idx < qp_nb * VIRTIO_QNUM;
104 static inline void __attribute__((always_inline))
105 do_flush_shadow_used_ring(struct virtio_net *dev, struct vhost_virtqueue *vq,
106 uint16_t to, uint16_t from, uint16_t size)
108 rte_memcpy(&vq->used->ring[to],
109 &vq->shadow_used_ring[from],
110 size * sizeof(struct vring_used_elem));
111 vhost_log_used_vring(dev, vq,
112 offsetof(struct vring_used, ring[to]),
113 size * sizeof(struct vring_used_elem));
116 static inline void __attribute__((always_inline))
117 flush_shadow_used_ring(struct virtio_net *dev, struct vhost_virtqueue *vq)
119 uint16_t used_idx = vq->last_used_idx & (vq->size - 1);
121 if (used_idx + vq->shadow_used_idx <= vq->size) {
122 do_flush_shadow_used_ring(dev, vq, used_idx, 0,
123 vq->shadow_used_idx);
127 /* update used ring interval [used_idx, vq->size] */
128 size = vq->size - used_idx;
129 do_flush_shadow_used_ring(dev, vq, used_idx, 0, size);
131 /* update the left half used ring interval [0, left_size] */
132 do_flush_shadow_used_ring(dev, vq, 0, size,
133 vq->shadow_used_idx - size);
135 vq->last_used_idx += vq->shadow_used_idx;
139 *(volatile uint16_t *)&vq->used->idx += vq->shadow_used_idx;
140 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
141 sizeof(vq->used->idx));
144 static inline void __attribute__((always_inline))
145 update_shadow_used_ring(struct vhost_virtqueue *vq,
146 uint16_t desc_idx, uint16_t len)
148 uint16_t i = vq->shadow_used_idx++;
150 vq->shadow_used_ring[i].id = desc_idx;
151 vq->shadow_used_ring[i].len = len;
155 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
157 uint64_t csum_l4 = m_buf->ol_flags & PKT_TX_L4_MASK;
159 if (m_buf->ol_flags & PKT_TX_TCP_SEG)
160 csum_l4 |= PKT_TX_TCP_CKSUM;
163 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
164 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
167 case PKT_TX_TCP_CKSUM:
168 net_hdr->csum_offset = (offsetof(struct tcp_hdr,
171 case PKT_TX_UDP_CKSUM:
172 net_hdr->csum_offset = (offsetof(struct udp_hdr,
175 case PKT_TX_SCTP_CKSUM:
176 net_hdr->csum_offset = (offsetof(struct sctp_hdr,
182 /* IP cksum verification cannot be bypassed, then calculate here */
183 if (m_buf->ol_flags & PKT_TX_IP_CKSUM) {
184 struct ipv4_hdr *ipv4_hdr;
186 ipv4_hdr = rte_pktmbuf_mtod_offset(m_buf, struct ipv4_hdr *,
188 ipv4_hdr->hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
191 if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
192 if (m_buf->ol_flags & PKT_TX_IPV4)
193 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
195 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
196 net_hdr->gso_size = m_buf->tso_segsz;
197 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
203 copy_virtio_net_hdr(struct virtio_net *dev, uint64_t desc_addr,
204 struct virtio_net_hdr_mrg_rxbuf hdr)
206 if (dev->vhost_hlen == sizeof(struct virtio_net_hdr_mrg_rxbuf))
207 *(struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)desc_addr = hdr;
209 *(struct virtio_net_hdr *)(uintptr_t)desc_addr = hdr.hdr;
212 static inline int __attribute__((always_inline))
213 copy_mbuf_to_desc(struct virtio_net *dev, struct vring_desc *descs,
214 struct rte_mbuf *m, uint16_t desc_idx, uint32_t size)
216 uint32_t desc_avail, desc_offset;
217 uint32_t mbuf_avail, mbuf_offset;
219 struct vring_desc *desc;
221 struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0, 0, 0, 0, 0, 0}, 0};
222 /* A counter to avoid desc dead loop chain */
223 uint16_t nr_desc = 1;
225 desc = &descs[desc_idx];
226 desc_addr = gpa_to_vva(dev, desc->addr);
228 * Checking of 'desc_addr' placed outside of 'unlikely' macro to avoid
229 * performance issue with some versions of gcc (4.8.4 and 5.3.0) which
230 * otherwise stores offset on the stack instead of in a register.
232 if (unlikely(desc->len < dev->vhost_hlen) || !desc_addr)
235 rte_prefetch0((void *)(uintptr_t)desc_addr);
237 virtio_enqueue_offload(m, &virtio_hdr.hdr);
238 copy_virtio_net_hdr(dev, desc_addr, virtio_hdr);
239 vhost_log_write(dev, desc->addr, dev->vhost_hlen);
240 PRINT_PACKET(dev, (uintptr_t)desc_addr, dev->vhost_hlen, 0);
242 desc_offset = dev->vhost_hlen;
243 desc_avail = desc->len - dev->vhost_hlen;
245 mbuf_avail = rte_pktmbuf_data_len(m);
247 while (mbuf_avail != 0 || m->next != NULL) {
248 /* done with current mbuf, fetch next */
249 if (mbuf_avail == 0) {
253 mbuf_avail = rte_pktmbuf_data_len(m);
256 /* done with current desc buf, fetch next */
257 if (desc_avail == 0) {
258 if ((desc->flags & VRING_DESC_F_NEXT) == 0) {
259 /* Room in vring buffer is not enough */
262 if (unlikely(desc->next >= size || ++nr_desc > size))
265 desc = &descs[desc->next];
266 desc_addr = gpa_to_vva(dev, desc->addr);
267 if (unlikely(!desc_addr))
271 desc_avail = desc->len;
274 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
275 rte_memcpy((void *)((uintptr_t)(desc_addr + desc_offset)),
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 mbuf_avail -= cpy_len;
283 mbuf_offset += cpy_len;
284 desc_avail -= cpy_len;
285 desc_offset += cpy_len;
292 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
293 * be received from the physical port or from another virtio device. A packet
294 * count is returned to indicate the number of packets that are succesfully
295 * added to the RX queue. This function works when the mbuf is scattered, but
296 * it doesn't support the mergeable feature.
298 static inline uint32_t __attribute__((always_inline))
299 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
300 struct rte_mbuf **pkts, uint32_t count)
302 struct vhost_virtqueue *vq;
303 uint16_t avail_idx, free_entries, start_idx;
304 uint16_t desc_indexes[MAX_PKT_BURST];
305 struct vring_desc *descs;
309 LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
310 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->virt_qp_nb))) {
311 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
312 dev->vid, __func__, queue_id);
316 vq = dev->virtqueue[queue_id];
318 rte_spinlock_lock(&vq->access_lock);
320 if (unlikely(vq->enabled == 0))
321 goto out_access_unlock;
323 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
324 start_idx = vq->last_used_idx;
325 free_entries = avail_idx - start_idx;
326 count = RTE_MIN(count, free_entries);
327 count = RTE_MIN(count, (uint32_t)MAX_PKT_BURST);
329 goto out_access_unlock;
331 LOG_DEBUG(VHOST_DATA, "(%d) start_idx %d | end_idx %d\n",
332 dev->vid, start_idx, start_idx + count);
334 /* Retrieve all of the desc indexes first to avoid caching issues. */
335 rte_prefetch0(&vq->avail->ring[start_idx & (vq->size - 1)]);
336 for (i = 0; i < count; i++) {
337 used_idx = (start_idx + i) & (vq->size - 1);
338 desc_indexes[i] = vq->avail->ring[used_idx];
339 vq->used->ring[used_idx].id = desc_indexes[i];
340 vq->used->ring[used_idx].len = pkts[i]->pkt_len +
342 vhost_log_used_vring(dev, vq,
343 offsetof(struct vring_used, ring[used_idx]),
344 sizeof(vq->used->ring[used_idx]));
347 rte_prefetch0(&vq->desc[desc_indexes[0]]);
348 for (i = 0; i < count; i++) {
349 uint16_t desc_idx = desc_indexes[i];
352 if (vq->desc[desc_idx].flags & VRING_DESC_F_INDIRECT) {
353 descs = (struct vring_desc *)(uintptr_t)gpa_to_vva(dev,
354 vq->desc[desc_idx].addr);
355 if (unlikely(!descs)) {
361 sz = vq->desc[desc_idx].len / sizeof(*descs);
367 err = copy_mbuf_to_desc(dev, descs, pkts[i], desc_idx, sz);
369 used_idx = (start_idx + i) & (vq->size - 1);
370 vq->used->ring[used_idx].len = dev->vhost_hlen;
371 vhost_log_used_vring(dev, vq,
372 offsetof(struct vring_used, ring[used_idx]),
373 sizeof(vq->used->ring[used_idx]));
377 rte_prefetch0(&vq->desc[desc_indexes[i+1]]);
382 *(volatile uint16_t *)&vq->used->idx += count;
383 vq->last_used_idx += count;
384 vhost_log_used_vring(dev, vq,
385 offsetof(struct vring_used, idx),
386 sizeof(vq->used->idx));
388 /* flush used->idx update before we read avail->flags. */
391 /* Kick the guest if necessary. */
392 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
393 && (vq->callfd >= 0))
394 eventfd_write(vq->callfd, (eventfd_t)1);
397 rte_spinlock_unlock(&vq->access_lock);
402 static inline int __attribute__((always_inline))
403 fill_vec_buf(struct virtio_net *dev, struct vhost_virtqueue *vq,
404 uint32_t avail_idx, uint32_t *vec_idx,
405 struct buf_vector *buf_vec, uint16_t *desc_chain_head,
406 uint16_t *desc_chain_len)
408 uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
409 uint32_t vec_id = *vec_idx;
411 struct vring_desc *descs = vq->desc;
413 *desc_chain_head = idx;
415 if (vq->desc[idx].flags & VRING_DESC_F_INDIRECT) {
416 descs = (struct vring_desc *)(uintptr_t)
417 gpa_to_vva(dev, vq->desc[idx].addr);
418 if (unlikely(!descs))
425 if (unlikely(vec_id >= BUF_VECTOR_MAX || idx >= vq->size))
428 len += descs[idx].len;
429 buf_vec[vec_id].buf_addr = descs[idx].addr;
430 buf_vec[vec_id].buf_len = descs[idx].len;
431 buf_vec[vec_id].desc_idx = idx;
434 if ((descs[idx].flags & VRING_DESC_F_NEXT) == 0)
437 idx = descs[idx].next;
440 *desc_chain_len = len;
447 * Returns -1 on fail, 0 on success
450 reserve_avail_buf_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,
451 uint32_t size, struct buf_vector *buf_vec,
452 uint16_t *num_buffers, uint16_t avail_head)
455 uint32_t vec_idx = 0;
458 uint16_t head_idx = 0;
462 cur_idx = vq->last_avail_idx;
465 if (unlikely(cur_idx == avail_head))
468 if (unlikely(fill_vec_buf(dev, vq, cur_idx, &vec_idx, buf_vec,
469 &head_idx, &len) < 0))
471 len = RTE_MIN(len, size);
472 update_shadow_used_ring(vq, head_idx, len);
480 * if we tried all available ring items, and still
481 * can't get enough buf, it means something abnormal
484 if (unlikely(tries >= vq->size))
491 static inline int __attribute__((always_inline))
492 copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct rte_mbuf *m,
493 struct buf_vector *buf_vec, uint16_t num_buffers)
495 struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0, 0, 0, 0, 0, 0}, 0};
496 uint32_t vec_idx = 0;
498 uint32_t mbuf_offset, mbuf_avail;
499 uint32_t desc_offset, desc_avail;
501 uint64_t hdr_addr, hdr_phys_addr;
502 struct rte_mbuf *hdr_mbuf;
504 if (unlikely(m == NULL))
507 desc_addr = gpa_to_vva(dev, buf_vec[vec_idx].buf_addr);
508 if (buf_vec[vec_idx].buf_len < dev->vhost_hlen || !desc_addr)
512 hdr_addr = desc_addr;
513 hdr_phys_addr = buf_vec[vec_idx].buf_addr;
514 rte_prefetch0((void *)(uintptr_t)hdr_addr);
516 virtio_hdr.num_buffers = num_buffers;
517 LOG_DEBUG(VHOST_DATA, "(%d) RX: num merge buffers %d\n",
518 dev->vid, num_buffers);
520 desc_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
521 desc_offset = dev->vhost_hlen;
523 mbuf_avail = rte_pktmbuf_data_len(m);
525 while (mbuf_avail != 0 || m->next != NULL) {
526 /* done with current desc buf, get the next one */
527 if (desc_avail == 0) {
529 desc_addr = gpa_to_vva(dev, buf_vec[vec_idx].buf_addr);
530 if (unlikely(!desc_addr))
533 /* Prefetch buffer address. */
534 rte_prefetch0((void *)(uintptr_t)desc_addr);
536 desc_avail = buf_vec[vec_idx].buf_len;
539 /* done with current mbuf, get the next one */
540 if (mbuf_avail == 0) {
544 mbuf_avail = rte_pktmbuf_data_len(m);
548 virtio_enqueue_offload(hdr_mbuf, &virtio_hdr.hdr);
549 copy_virtio_net_hdr(dev, hdr_addr, virtio_hdr);
550 vhost_log_write(dev, hdr_phys_addr, dev->vhost_hlen);
551 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
557 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
558 rte_memcpy((void *)((uintptr_t)(desc_addr + desc_offset)),
559 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
561 vhost_log_write(dev, buf_vec[vec_idx].buf_addr + desc_offset,
563 PRINT_PACKET(dev, (uintptr_t)(desc_addr + desc_offset),
566 mbuf_avail -= cpy_len;
567 mbuf_offset += cpy_len;
568 desc_avail -= cpy_len;
569 desc_offset += cpy_len;
575 static inline uint32_t __attribute__((always_inline))
576 virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,
577 struct rte_mbuf **pkts, uint32_t count)
579 struct vhost_virtqueue *vq;
580 uint32_t pkt_idx = 0;
581 uint16_t num_buffers;
582 struct buf_vector buf_vec[BUF_VECTOR_MAX];
585 LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
586 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->virt_qp_nb))) {
587 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
588 dev->vid, __func__, queue_id);
592 vq = dev->virtqueue[queue_id];
594 rte_spinlock_lock(&vq->access_lock);
596 if (unlikely(vq->enabled == 0))
597 goto out_access_unlock;
599 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
601 goto out_access_unlock;
603 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
605 vq->shadow_used_idx = 0;
606 avail_head = *((volatile uint16_t *)&vq->avail->idx);
607 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
608 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
610 if (unlikely(reserve_avail_buf_mergeable(dev, vq,
611 pkt_len, buf_vec, &num_buffers,
613 LOG_DEBUG(VHOST_DATA,
614 "(%d) failed to get enough desc from vring\n",
616 vq->shadow_used_idx -= num_buffers;
620 LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
621 dev->vid, vq->last_avail_idx,
622 vq->last_avail_idx + num_buffers);
624 if (copy_mbuf_to_desc_mergeable(dev, pkts[pkt_idx],
625 buf_vec, num_buffers) < 0) {
626 vq->shadow_used_idx -= num_buffers;
630 vq->last_avail_idx += num_buffers;
633 if (likely(vq->shadow_used_idx)) {
634 flush_shadow_used_ring(dev, vq);
636 /* flush used->idx update before we read avail->flags. */
639 /* Kick the guest if necessary. */
640 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
641 && (vq->callfd >= 0))
642 eventfd_write(vq->callfd, (eventfd_t)1);
646 rte_spinlock_unlock(&vq->access_lock);
652 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
653 struct rte_mbuf **pkts, uint16_t count)
655 struct virtio_net *dev = get_device(vid);
660 if (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF))
661 return virtio_dev_merge_rx(dev, queue_id, pkts, count);
663 return virtio_dev_rx(dev, queue_id, pkts, count);
667 virtio_net_with_host_offload(struct virtio_net *dev)
670 ((1ULL << VIRTIO_NET_F_CSUM) |
671 (1ULL << VIRTIO_NET_F_HOST_ECN) |
672 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
673 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
674 (1ULL << VIRTIO_NET_F_HOST_UFO)))
681 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
683 struct ipv4_hdr *ipv4_hdr;
684 struct ipv6_hdr *ipv6_hdr;
686 struct ether_hdr *eth_hdr;
689 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
691 m->l2_len = sizeof(struct ether_hdr);
692 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
694 if (ethertype == ETHER_TYPE_VLAN) {
695 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
697 m->l2_len += sizeof(struct vlan_hdr);
698 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
701 l3_hdr = (char *)eth_hdr + m->l2_len;
704 case ETHER_TYPE_IPv4:
705 ipv4_hdr = (struct ipv4_hdr *)l3_hdr;
706 *l4_proto = ipv4_hdr->next_proto_id;
707 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
708 *l4_hdr = (char *)l3_hdr + m->l3_len;
709 m->ol_flags |= PKT_TX_IPV4;
711 case ETHER_TYPE_IPv6:
712 ipv6_hdr = (struct ipv6_hdr *)l3_hdr;
713 *l4_proto = ipv6_hdr->proto;
714 m->l3_len = sizeof(struct ipv6_hdr);
715 *l4_hdr = (char *)l3_hdr + m->l3_len;
716 m->ol_flags |= PKT_TX_IPV6;
726 static inline void __attribute__((always_inline))
727 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
729 uint16_t l4_proto = 0;
731 struct tcp_hdr *tcp_hdr = NULL;
733 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
736 parse_ethernet(m, &l4_proto, &l4_hdr);
737 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
738 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
739 switch (hdr->csum_offset) {
740 case (offsetof(struct tcp_hdr, cksum)):
741 if (l4_proto == IPPROTO_TCP)
742 m->ol_flags |= PKT_TX_TCP_CKSUM;
744 case (offsetof(struct udp_hdr, dgram_cksum)):
745 if (l4_proto == IPPROTO_UDP)
746 m->ol_flags |= PKT_TX_UDP_CKSUM;
748 case (offsetof(struct sctp_hdr, cksum)):
749 if (l4_proto == IPPROTO_SCTP)
750 m->ol_flags |= PKT_TX_SCTP_CKSUM;
758 if (l4_hdr && hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
759 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
760 case VIRTIO_NET_HDR_GSO_TCPV4:
761 case VIRTIO_NET_HDR_GSO_TCPV6:
762 tcp_hdr = (struct tcp_hdr *)l4_hdr;
763 m->ol_flags |= PKT_TX_TCP_SEG;
764 m->tso_segsz = hdr->gso_size;
765 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
768 RTE_LOG(WARNING, VHOST_DATA,
769 "unsupported gso type %u.\n", hdr->gso_type);
775 #define RARP_PKT_SIZE 64
778 make_rarp_packet(struct rte_mbuf *rarp_mbuf, const struct ether_addr *mac)
780 struct ether_hdr *eth_hdr;
781 struct arp_hdr *rarp;
783 if (rarp_mbuf->buf_len < 64) {
784 RTE_LOG(WARNING, VHOST_DATA,
785 "failed to make RARP; mbuf size too small %u (< %d)\n",
786 rarp_mbuf->buf_len, RARP_PKT_SIZE);
790 /* Ethernet header. */
791 eth_hdr = rte_pktmbuf_mtod_offset(rarp_mbuf, struct ether_hdr *, 0);
792 memset(eth_hdr->d_addr.addr_bytes, 0xff, ETHER_ADDR_LEN);
793 ether_addr_copy(mac, ð_hdr->s_addr);
794 eth_hdr->ether_type = htons(ETHER_TYPE_RARP);
797 rarp = (struct arp_hdr *)(eth_hdr + 1);
798 rarp->arp_hrd = htons(ARP_HRD_ETHER);
799 rarp->arp_pro = htons(ETHER_TYPE_IPv4);
800 rarp->arp_hln = ETHER_ADDR_LEN;
802 rarp->arp_op = htons(ARP_OP_REVREQUEST);
804 ether_addr_copy(mac, &rarp->arp_data.arp_sha);
805 ether_addr_copy(mac, &rarp->arp_data.arp_tha);
806 memset(&rarp->arp_data.arp_sip, 0x00, 4);
807 memset(&rarp->arp_data.arp_tip, 0x00, 4);
809 rarp_mbuf->pkt_len = rarp_mbuf->data_len = RARP_PKT_SIZE;
814 static inline void __attribute__((always_inline))
815 put_zmbuf(struct zcopy_mbuf *zmbuf)
820 static inline int __attribute__((always_inline))
821 copy_desc_to_mbuf(struct virtio_net *dev, struct vring_desc *descs,
822 uint16_t max_desc, struct rte_mbuf *m, uint16_t desc_idx,
823 struct rte_mempool *mbuf_pool)
825 struct vring_desc *desc;
827 uint32_t desc_avail, desc_offset;
828 uint32_t mbuf_avail, mbuf_offset;
830 struct rte_mbuf *cur = m, *prev = m;
831 struct virtio_net_hdr *hdr = NULL;
832 /* A counter to avoid desc dead loop chain */
833 uint32_t nr_desc = 1;
835 desc = &descs[desc_idx];
836 if (unlikely((desc->len < dev->vhost_hlen)) ||
837 (desc->flags & VRING_DESC_F_INDIRECT))
840 desc_addr = gpa_to_vva(dev, desc->addr);
841 if (unlikely(!desc_addr))
844 if (virtio_net_with_host_offload(dev)) {
845 hdr = (struct virtio_net_hdr *)((uintptr_t)desc_addr);
850 * A virtio driver normally uses at least 2 desc buffers
851 * for Tx: the first for storing the header, and others
852 * for storing the data.
854 if (likely((desc->len == dev->vhost_hlen) &&
855 (desc->flags & VRING_DESC_F_NEXT) != 0)) {
856 desc = &descs[desc->next];
857 if (unlikely(desc->flags & VRING_DESC_F_INDIRECT))
860 desc_addr = gpa_to_vva(dev, desc->addr);
861 if (unlikely(!desc_addr))
865 desc_avail = desc->len;
868 desc_avail = desc->len - dev->vhost_hlen;
869 desc_offset = dev->vhost_hlen;
872 rte_prefetch0((void *)(uintptr_t)(desc_addr + desc_offset));
874 PRINT_PACKET(dev, (uintptr_t)(desc_addr + desc_offset), desc_avail, 0);
877 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
881 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
884 * A desc buf might across two host physical pages that are
885 * not continuous. In such case (gpa_to_hpa returns 0), data
886 * will be copied even though zero copy is enabled.
888 if (unlikely(dev->dequeue_zero_copy && (hpa = gpa_to_hpa(dev,
889 desc->addr + desc_offset, cpy_len)))) {
890 cur->data_len = cpy_len;
892 cur->buf_addr = (void *)(uintptr_t)(desc_addr
894 cur->buf_physaddr = hpa;
897 * In zero copy mode, one mbuf can only reference data
898 * for one or partial of one desc buff.
900 mbuf_avail = cpy_len;
902 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
904 (void *)((uintptr_t)(desc_addr + desc_offset)),
908 mbuf_avail -= cpy_len;
909 mbuf_offset += cpy_len;
910 desc_avail -= cpy_len;
911 desc_offset += cpy_len;
913 /* This desc reaches to its end, get the next one */
914 if (desc_avail == 0) {
915 if ((desc->flags & VRING_DESC_F_NEXT) == 0)
918 if (unlikely(desc->next >= max_desc ||
919 ++nr_desc > max_desc))
921 desc = &descs[desc->next];
922 if (unlikely(desc->flags & VRING_DESC_F_INDIRECT))
925 desc_addr = gpa_to_vva(dev, desc->addr);
926 if (unlikely(!desc_addr))
929 rte_prefetch0((void *)(uintptr_t)desc_addr);
932 desc_avail = desc->len;
934 PRINT_PACKET(dev, (uintptr_t)desc_addr, desc->len, 0);
938 * This mbuf reaches to its end, get a new one
941 if (mbuf_avail == 0) {
942 cur = rte_pktmbuf_alloc(mbuf_pool);
943 if (unlikely(cur == NULL)) {
944 RTE_LOG(ERR, VHOST_DATA, "Failed to "
945 "allocate memory for mbuf.\n");
948 if (unlikely(dev->dequeue_zero_copy))
949 rte_mbuf_refcnt_update(cur, 1);
952 prev->data_len = mbuf_offset;
954 m->pkt_len += mbuf_offset;
958 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
962 prev->data_len = mbuf_offset;
963 m->pkt_len += mbuf_offset;
966 vhost_dequeue_offload(hdr, m);
971 static inline void __attribute__((always_inline))
972 update_used_ring(struct virtio_net *dev, struct vhost_virtqueue *vq,
973 uint32_t used_idx, uint32_t desc_idx)
975 vq->used->ring[used_idx].id = desc_idx;
976 vq->used->ring[used_idx].len = 0;
977 vhost_log_used_vring(dev, vq,
978 offsetof(struct vring_used, ring[used_idx]),
979 sizeof(vq->used->ring[used_idx]));
982 static inline void __attribute__((always_inline))
983 update_used_idx(struct virtio_net *dev, struct vhost_virtqueue *vq,
986 if (unlikely(count == 0))
992 vq->used->idx += count;
993 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
994 sizeof(vq->used->idx));
996 /* Kick guest if required. */
997 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
998 && (vq->callfd >= 0))
999 eventfd_write(vq->callfd, (eventfd_t)1);
1002 static inline struct zcopy_mbuf *__attribute__((always_inline))
1003 get_zmbuf(struct vhost_virtqueue *vq)
1009 /* search [last_zmbuf_idx, zmbuf_size) */
1010 i = vq->last_zmbuf_idx;
1011 last = vq->zmbuf_size;
1014 for (; i < last; i++) {
1015 if (vq->zmbufs[i].in_use == 0) {
1016 vq->last_zmbuf_idx = i + 1;
1017 vq->zmbufs[i].in_use = 1;
1018 return &vq->zmbufs[i];
1024 /* search [0, last_zmbuf_idx) */
1026 last = vq->last_zmbuf_idx;
1033 static inline bool __attribute__((always_inline))
1034 mbuf_is_consumed(struct rte_mbuf *m)
1037 if (rte_mbuf_refcnt_read(m) > 1)
1045 static inline void __attribute__((always_inline))
1046 restore_mbuf(struct rte_mbuf *m)
1048 uint32_t mbuf_size, priv_size;
1051 priv_size = rte_pktmbuf_priv_size(m->pool);
1052 mbuf_size = sizeof(struct rte_mbuf) + priv_size;
1053 /* start of buffer is after mbuf structure and priv data */
1055 m->buf_addr = (char *)m + mbuf_size;
1056 m->buf_physaddr = rte_mempool_virt2phy(NULL, m) + mbuf_size;
1062 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
1063 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1065 struct virtio_net *dev;
1066 struct rte_mbuf *rarp_mbuf = NULL;
1067 struct vhost_virtqueue *vq;
1068 uint32_t desc_indexes[MAX_PKT_BURST];
1071 uint16_t free_entries;
1074 dev = get_device(vid);
1078 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->virt_qp_nb))) {
1079 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
1080 dev->vid, __func__, queue_id);
1084 vq = dev->virtqueue[queue_id];
1086 if (unlikely(rte_spinlock_trylock(&vq->access_lock) == 0))
1089 if (unlikely(vq->enabled == 0))
1090 goto out_access_unlock;
1092 if (unlikely(dev->dequeue_zero_copy)) {
1093 struct zcopy_mbuf *zmbuf, *next;
1096 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1097 zmbuf != NULL; zmbuf = next) {
1098 next = TAILQ_NEXT(zmbuf, next);
1100 if (mbuf_is_consumed(zmbuf->mbuf)) {
1101 used_idx = vq->last_used_idx++ & (vq->size - 1);
1102 update_used_ring(dev, vq, used_idx,
1106 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1107 restore_mbuf(zmbuf->mbuf);
1108 rte_pktmbuf_free(zmbuf->mbuf);
1114 update_used_idx(dev, vq, nr_updated);
1118 * Construct a RARP broadcast packet, and inject it to the "pkts"
1119 * array, to looks like that guest actually send such packet.
1121 * Check user_send_rarp() for more information.
1123 * broadcast_rarp shares a cacheline in the virtio_net structure
1124 * with some fields that are accessed during enqueue and
1125 * rte_atomic16_cmpset() causes a write if using cmpxchg. This could
1126 * result in false sharing between enqueue and dequeue.
1128 * Prevent unnecessary false sharing by reading broadcast_rarp first
1129 * and only performing cmpset if the read indicates it is likely to
1133 if (unlikely(rte_atomic16_read(&dev->broadcast_rarp) &&
1134 rte_atomic16_cmpset((volatile uint16_t *)
1135 &dev->broadcast_rarp.cnt, 1, 0))) {
1137 rarp_mbuf = rte_pktmbuf_alloc(mbuf_pool);
1138 if (rarp_mbuf == NULL) {
1139 RTE_LOG(ERR, VHOST_DATA,
1140 "Failed to allocate memory for mbuf.\n");
1141 goto out_access_unlock;
1144 if (make_rarp_packet(rarp_mbuf, &dev->mac)) {
1145 rte_pktmbuf_free(rarp_mbuf);
1152 free_entries = *((volatile uint16_t *)&vq->avail->idx) -
1154 if (free_entries == 0)
1155 goto out_access_unlock;
1157 LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1159 /* Prefetch available and used ring */
1160 avail_idx = vq->last_avail_idx & (vq->size - 1);
1161 used_idx = vq->last_used_idx & (vq->size - 1);
1162 rte_prefetch0(&vq->avail->ring[avail_idx]);
1163 rte_prefetch0(&vq->used->ring[used_idx]);
1165 count = RTE_MIN(count, MAX_PKT_BURST);
1166 count = RTE_MIN(count, free_entries);
1167 LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1170 /* Retrieve all of the head indexes first to avoid caching issues. */
1171 for (i = 0; i < count; i++) {
1172 avail_idx = (vq->last_avail_idx + i) & (vq->size - 1);
1173 used_idx = (vq->last_used_idx + i) & (vq->size - 1);
1174 desc_indexes[i] = vq->avail->ring[avail_idx];
1176 if (likely(dev->dequeue_zero_copy == 0))
1177 update_used_ring(dev, vq, used_idx, desc_indexes[i]);
1180 /* Prefetch descriptor index. */
1181 rte_prefetch0(&vq->desc[desc_indexes[0]]);
1182 for (i = 0; i < count; i++) {
1183 struct vring_desc *desc;
1187 if (likely(i + 1 < count))
1188 rte_prefetch0(&vq->desc[desc_indexes[i + 1]]);
1190 if (vq->desc[desc_indexes[i]].flags & VRING_DESC_F_INDIRECT) {
1191 desc = (struct vring_desc *)(uintptr_t)gpa_to_vva(dev,
1192 vq->desc[desc_indexes[i]].addr);
1193 if (unlikely(!desc))
1196 rte_prefetch0(desc);
1197 sz = vq->desc[desc_indexes[i]].len / sizeof(*desc);
1202 idx = desc_indexes[i];
1205 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
1206 if (unlikely(pkts[i] == NULL)) {
1207 RTE_LOG(ERR, VHOST_DATA,
1208 "Failed to allocate memory for mbuf.\n");
1212 err = copy_desc_to_mbuf(dev, desc, sz, pkts[i], idx, mbuf_pool);
1213 if (unlikely(err)) {
1214 rte_pktmbuf_free(pkts[i]);
1218 if (unlikely(dev->dequeue_zero_copy)) {
1219 struct zcopy_mbuf *zmbuf;
1221 zmbuf = get_zmbuf(vq);
1223 rte_pktmbuf_free(pkts[i]);
1226 zmbuf->mbuf = pkts[i];
1227 zmbuf->desc_idx = desc_indexes[i];
1230 * Pin lock the mbuf; we will check later to see
1231 * whether the mbuf is freed (when we are the last
1232 * user) or not. If that's the case, we then could
1233 * update the used ring safely.
1235 rte_mbuf_refcnt_update(pkts[i], 1);
1238 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1241 vq->last_avail_idx += i;
1243 if (likely(dev->dequeue_zero_copy == 0)) {
1244 vq->last_used_idx += i;
1245 update_used_idx(dev, vq, i);
1249 rte_spinlock_unlock(&vq->access_lock);
1251 if (unlikely(rarp_mbuf != NULL)) {
1253 * Inject it to the head of "pkts" array, so that switch's mac
1254 * learning table will get updated first.
1256 memmove(&pkts[1], pkts, i * sizeof(struct rte_mbuf *));
1257 pkts[0] = rarp_mbuf;