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>
50 #define MAX_PKT_BURST 32
51 #define VHOST_LOG_PAGE 4096
53 static inline void __attribute__((always_inline))
54 vhost_log_page(uint8_t *log_base, uint64_t page)
56 log_base[page / 8] |= 1 << (page % 8);
59 static inline void __attribute__((always_inline))
60 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
64 if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
65 !dev->log_base || !len))
68 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
71 /* To make sure guest memory updates are committed before logging */
74 page = addr / VHOST_LOG_PAGE;
75 while (page * VHOST_LOG_PAGE < addr + len) {
76 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
81 static inline void __attribute__((always_inline))
82 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
83 uint64_t offset, uint64_t len)
85 vhost_log_write(dev, vq->log_guest_addr + offset, len);
89 is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t qp_nb)
91 return (is_tx ^ (idx & 1)) == 0 && idx < qp_nb * VIRTIO_QNUM;
94 static inline void __attribute__((always_inline))
95 do_flush_shadow_used_ring(struct virtio_net *dev, struct vhost_virtqueue *vq,
96 uint16_t to, uint16_t from, uint16_t size)
98 rte_memcpy(&vq->used->ring[to],
99 &vq->shadow_used_ring[from],
100 size * sizeof(struct vring_used_elem));
101 vhost_log_used_vring(dev, vq,
102 offsetof(struct vring_used, ring[to]),
103 size * sizeof(struct vring_used_elem));
106 static inline void __attribute__((always_inline))
107 flush_shadow_used_ring(struct virtio_net *dev, struct vhost_virtqueue *vq)
109 uint16_t used_idx = vq->last_used_idx & (vq->size - 1);
111 if (used_idx + vq->shadow_used_idx <= vq->size) {
112 do_flush_shadow_used_ring(dev, vq, used_idx, 0,
113 vq->shadow_used_idx);
117 /* update used ring interval [used_idx, vq->size] */
118 size = vq->size - used_idx;
119 do_flush_shadow_used_ring(dev, vq, used_idx, 0, size);
121 /* update the left half used ring interval [0, left_size] */
122 do_flush_shadow_used_ring(dev, vq, 0, size,
123 vq->shadow_used_idx - size);
125 vq->last_used_idx += vq->shadow_used_idx;
129 *(volatile uint16_t *)&vq->used->idx += vq->shadow_used_idx;
130 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
131 sizeof(vq->used->idx));
134 static inline void __attribute__((always_inline))
135 update_shadow_used_ring(struct vhost_virtqueue *vq,
136 uint16_t desc_idx, uint16_t len)
138 uint16_t i = vq->shadow_used_idx++;
140 vq->shadow_used_ring[i].id = desc_idx;
141 vq->shadow_used_ring[i].len = len;
145 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
147 if (m_buf->ol_flags & PKT_TX_L4_MASK) {
148 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
149 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
151 switch (m_buf->ol_flags & PKT_TX_L4_MASK) {
152 case PKT_TX_TCP_CKSUM:
153 net_hdr->csum_offset = (offsetof(struct tcp_hdr,
156 case PKT_TX_UDP_CKSUM:
157 net_hdr->csum_offset = (offsetof(struct udp_hdr,
160 case PKT_TX_SCTP_CKSUM:
161 net_hdr->csum_offset = (offsetof(struct sctp_hdr,
167 if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
168 if (m_buf->ol_flags & PKT_TX_IPV4)
169 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
171 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
172 net_hdr->gso_size = m_buf->tso_segsz;
173 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
179 copy_virtio_net_hdr(struct virtio_net *dev, uint64_t desc_addr,
180 struct virtio_net_hdr_mrg_rxbuf hdr)
182 if (dev->vhost_hlen == sizeof(struct virtio_net_hdr_mrg_rxbuf))
183 *(struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)desc_addr = hdr;
185 *(struct virtio_net_hdr *)(uintptr_t)desc_addr = hdr.hdr;
188 static inline int __attribute__((always_inline))
189 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
190 struct rte_mbuf *m, uint16_t desc_idx)
192 uint32_t desc_avail, desc_offset;
193 uint32_t mbuf_avail, mbuf_offset;
195 struct vring_desc *desc;
197 struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0, 0, 0, 0, 0, 0}, 0};
199 desc = &vq->desc[desc_idx];
200 desc_addr = gpa_to_vva(dev, desc->addr);
202 * Checking of 'desc_addr' placed outside of 'unlikely' macro to avoid
203 * performance issue with some versions of gcc (4.8.4 and 5.3.0) which
204 * otherwise stores offset on the stack instead of in a register.
206 if (unlikely(desc->len < dev->vhost_hlen) || !desc_addr)
209 rte_prefetch0((void *)(uintptr_t)desc_addr);
211 virtio_enqueue_offload(m, &virtio_hdr.hdr);
212 copy_virtio_net_hdr(dev, desc_addr, virtio_hdr);
213 vhost_log_write(dev, desc->addr, dev->vhost_hlen);
214 PRINT_PACKET(dev, (uintptr_t)desc_addr, dev->vhost_hlen, 0);
216 desc_offset = dev->vhost_hlen;
217 desc_avail = desc->len - dev->vhost_hlen;
219 mbuf_avail = rte_pktmbuf_data_len(m);
221 while (mbuf_avail != 0 || m->next != NULL) {
222 /* done with current mbuf, fetch next */
223 if (mbuf_avail == 0) {
227 mbuf_avail = rte_pktmbuf_data_len(m);
230 /* done with current desc buf, fetch next */
231 if (desc_avail == 0) {
232 if ((desc->flags & VRING_DESC_F_NEXT) == 0) {
233 /* Room in vring buffer is not enough */
236 if (unlikely(desc->next >= vq->size))
239 desc = &vq->desc[desc->next];
240 desc_addr = gpa_to_vva(dev, desc->addr);
241 if (unlikely(!desc_addr))
245 desc_avail = desc->len;
248 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
249 rte_memcpy((void *)((uintptr_t)(desc_addr + desc_offset)),
250 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
252 vhost_log_write(dev, desc->addr + desc_offset, cpy_len);
253 PRINT_PACKET(dev, (uintptr_t)(desc_addr + desc_offset),
256 mbuf_avail -= cpy_len;
257 mbuf_offset += cpy_len;
258 desc_avail -= cpy_len;
259 desc_offset += cpy_len;
266 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
267 * be received from the physical port or from another virtio device. A packet
268 * count is returned to indicate the number of packets that are succesfully
269 * added to the RX queue. This function works when the mbuf is scattered, but
270 * it doesn't support the mergeable feature.
272 static inline uint32_t __attribute__((always_inline))
273 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
274 struct rte_mbuf **pkts, uint32_t count)
276 struct vhost_virtqueue *vq;
277 uint16_t avail_idx, free_entries, start_idx;
278 uint16_t desc_indexes[MAX_PKT_BURST];
282 LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
283 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->virt_qp_nb))) {
284 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
285 dev->vid, __func__, queue_id);
289 vq = dev->virtqueue[queue_id];
290 if (unlikely(vq->enabled == 0))
293 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
294 start_idx = vq->last_used_idx;
295 free_entries = avail_idx - start_idx;
296 count = RTE_MIN(count, free_entries);
297 count = RTE_MIN(count, (uint32_t)MAX_PKT_BURST);
301 LOG_DEBUG(VHOST_DATA, "(%d) start_idx %d | end_idx %d\n",
302 dev->vid, start_idx, start_idx + count);
304 /* Retrieve all of the desc indexes first to avoid caching issues. */
305 rte_prefetch0(&vq->avail->ring[start_idx & (vq->size - 1)]);
306 for (i = 0; i < count; i++) {
307 used_idx = (start_idx + i) & (vq->size - 1);
308 desc_indexes[i] = vq->avail->ring[used_idx];
309 vq->used->ring[used_idx].id = desc_indexes[i];
310 vq->used->ring[used_idx].len = pkts[i]->pkt_len +
312 vhost_log_used_vring(dev, vq,
313 offsetof(struct vring_used, ring[used_idx]),
314 sizeof(vq->used->ring[used_idx]));
317 rte_prefetch0(&vq->desc[desc_indexes[0]]);
318 for (i = 0; i < count; i++) {
319 uint16_t desc_idx = desc_indexes[i];
322 err = copy_mbuf_to_desc(dev, vq, pkts[i], desc_idx);
324 used_idx = (start_idx + i) & (vq->size - 1);
325 vq->used->ring[used_idx].len = dev->vhost_hlen;
326 vhost_log_used_vring(dev, vq,
327 offsetof(struct vring_used, ring[used_idx]),
328 sizeof(vq->used->ring[used_idx]));
332 rte_prefetch0(&vq->desc[desc_indexes[i+1]]);
337 *(volatile uint16_t *)&vq->used->idx += count;
338 vq->last_used_idx += count;
339 vhost_log_used_vring(dev, vq,
340 offsetof(struct vring_used, idx),
341 sizeof(vq->used->idx));
343 /* flush used->idx update before we read avail->flags. */
346 /* Kick the guest if necessary. */
347 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
348 && (vq->callfd >= 0))
349 eventfd_write(vq->callfd, (eventfd_t)1);
353 static inline int __attribute__((always_inline))
354 fill_vec_buf(struct vhost_virtqueue *vq, uint32_t avail_idx,
355 uint32_t *vec_idx, struct buf_vector *buf_vec,
356 uint16_t *desc_chain_head, uint16_t *desc_chain_len)
358 uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
359 uint32_t vec_id = *vec_idx;
362 *desc_chain_head = idx;
364 if (unlikely(vec_id >= BUF_VECTOR_MAX || idx >= vq->size))
367 len += vq->desc[idx].len;
368 buf_vec[vec_id].buf_addr = vq->desc[idx].addr;
369 buf_vec[vec_id].buf_len = vq->desc[idx].len;
370 buf_vec[vec_id].desc_idx = idx;
373 if ((vq->desc[idx].flags & VRING_DESC_F_NEXT) == 0)
376 idx = vq->desc[idx].next;
379 *desc_chain_len = len;
386 * Returns -1 on fail, 0 on success
389 reserve_avail_buf_mergeable(struct vhost_virtqueue *vq, uint32_t size,
390 struct buf_vector *buf_vec, uint16_t *num_buffers)
394 uint32_t vec_idx = 0;
397 uint16_t head_idx = 0;
401 cur_idx = vq->last_avail_idx;
404 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
405 if (unlikely(cur_idx == avail_idx))
408 if (unlikely(fill_vec_buf(vq, cur_idx, &vec_idx, buf_vec,
409 &head_idx, &len) < 0))
411 len = RTE_MIN(len, size);
412 update_shadow_used_ring(vq, head_idx, len);
420 * if we tried all available ring items, and still
421 * can't get enough buf, it means something abnormal
424 if (unlikely(tries >= vq->size))
431 static inline int __attribute__((always_inline))
432 copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct rte_mbuf *m,
433 struct buf_vector *buf_vec, uint16_t num_buffers)
435 struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0, 0, 0, 0, 0, 0}, 0};
436 uint32_t vec_idx = 0;
438 uint32_t mbuf_offset, mbuf_avail;
439 uint32_t desc_offset, desc_avail;
441 uint64_t hdr_addr, hdr_phys_addr;
442 struct rte_mbuf *hdr_mbuf;
444 if (unlikely(m == NULL))
447 desc_addr = gpa_to_vva(dev, buf_vec[vec_idx].buf_addr);
448 if (buf_vec[vec_idx].buf_len < dev->vhost_hlen || !desc_addr)
452 hdr_addr = desc_addr;
453 hdr_phys_addr = buf_vec[vec_idx].buf_addr;
454 rte_prefetch0((void *)(uintptr_t)hdr_addr);
456 virtio_hdr.num_buffers = num_buffers;
457 LOG_DEBUG(VHOST_DATA, "(%d) RX: num merge buffers %d\n",
458 dev->vid, num_buffers);
460 desc_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
461 desc_offset = dev->vhost_hlen;
463 mbuf_avail = rte_pktmbuf_data_len(m);
465 while (mbuf_avail != 0 || m->next != NULL) {
466 /* done with current desc buf, get the next one */
467 if (desc_avail == 0) {
469 desc_addr = gpa_to_vva(dev, buf_vec[vec_idx].buf_addr);
470 if (unlikely(!desc_addr))
473 /* Prefetch buffer address. */
474 rte_prefetch0((void *)(uintptr_t)desc_addr);
476 desc_avail = buf_vec[vec_idx].buf_len;
479 /* done with current mbuf, get the next one */
480 if (mbuf_avail == 0) {
484 mbuf_avail = rte_pktmbuf_data_len(m);
488 virtio_enqueue_offload(hdr_mbuf, &virtio_hdr.hdr);
489 copy_virtio_net_hdr(dev, hdr_addr, virtio_hdr);
490 vhost_log_write(dev, hdr_phys_addr, dev->vhost_hlen);
491 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
497 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
498 rte_memcpy((void *)((uintptr_t)(desc_addr + desc_offset)),
499 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
501 vhost_log_write(dev, buf_vec[vec_idx].buf_addr + desc_offset,
503 PRINT_PACKET(dev, (uintptr_t)(desc_addr + desc_offset),
506 mbuf_avail -= cpy_len;
507 mbuf_offset += cpy_len;
508 desc_avail -= cpy_len;
509 desc_offset += cpy_len;
515 static inline uint32_t __attribute__((always_inline))
516 virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,
517 struct rte_mbuf **pkts, uint32_t count)
519 struct vhost_virtqueue *vq;
520 uint32_t pkt_idx = 0;
521 uint16_t num_buffers;
522 struct buf_vector buf_vec[BUF_VECTOR_MAX];
524 LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
525 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->virt_qp_nb))) {
526 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
527 dev->vid, __func__, queue_id);
531 vq = dev->virtqueue[queue_id];
532 if (unlikely(vq->enabled == 0))
535 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
539 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
541 vq->shadow_used_idx = 0;
542 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
543 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
545 if (unlikely(reserve_avail_buf_mergeable(vq, pkt_len, buf_vec,
546 &num_buffers) < 0)) {
547 LOG_DEBUG(VHOST_DATA,
548 "(%d) failed to get enough desc from vring\n",
550 vq->shadow_used_idx -= num_buffers;
554 LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
555 dev->vid, vq->last_avail_idx,
556 vq->last_avail_idx + num_buffers);
558 if (copy_mbuf_to_desc_mergeable(dev, pkts[pkt_idx],
559 buf_vec, num_buffers) < 0) {
560 vq->shadow_used_idx -= num_buffers;
564 vq->last_avail_idx += num_buffers;
567 if (likely(vq->shadow_used_idx)) {
568 flush_shadow_used_ring(dev, vq);
570 /* flush used->idx update before we read avail->flags. */
573 /* Kick the guest if necessary. */
574 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
575 && (vq->callfd >= 0))
576 eventfd_write(vq->callfd, (eventfd_t)1);
583 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
584 struct rte_mbuf **pkts, uint16_t count)
586 struct virtio_net *dev = get_device(vid);
591 if (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF))
592 return virtio_dev_merge_rx(dev, queue_id, pkts, count);
594 return virtio_dev_rx(dev, queue_id, pkts, count);
598 virtio_net_with_host_offload(struct virtio_net *dev)
601 (VIRTIO_NET_F_CSUM | VIRTIO_NET_F_HOST_ECN |
602 VIRTIO_NET_F_HOST_TSO4 | VIRTIO_NET_F_HOST_TSO6 |
603 VIRTIO_NET_F_HOST_UFO))
610 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
612 struct ipv4_hdr *ipv4_hdr;
613 struct ipv6_hdr *ipv6_hdr;
615 struct ether_hdr *eth_hdr;
618 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
620 m->l2_len = sizeof(struct ether_hdr);
621 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
623 if (ethertype == ETHER_TYPE_VLAN) {
624 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
626 m->l2_len += sizeof(struct vlan_hdr);
627 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
630 l3_hdr = (char *)eth_hdr + m->l2_len;
633 case ETHER_TYPE_IPv4:
634 ipv4_hdr = (struct ipv4_hdr *)l3_hdr;
635 *l4_proto = ipv4_hdr->next_proto_id;
636 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
637 *l4_hdr = (char *)l3_hdr + m->l3_len;
638 m->ol_flags |= PKT_TX_IPV4;
640 case ETHER_TYPE_IPv6:
641 ipv6_hdr = (struct ipv6_hdr *)l3_hdr;
642 *l4_proto = ipv6_hdr->proto;
643 m->l3_len = sizeof(struct ipv6_hdr);
644 *l4_hdr = (char *)l3_hdr + m->l3_len;
645 m->ol_flags |= PKT_TX_IPV6;
654 static inline void __attribute__((always_inline))
655 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
657 uint16_t l4_proto = 0;
659 struct tcp_hdr *tcp_hdr = NULL;
661 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
664 parse_ethernet(m, &l4_proto, &l4_hdr);
665 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
666 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
667 switch (hdr->csum_offset) {
668 case (offsetof(struct tcp_hdr, cksum)):
669 if (l4_proto == IPPROTO_TCP)
670 m->ol_flags |= PKT_TX_TCP_CKSUM;
672 case (offsetof(struct udp_hdr, dgram_cksum)):
673 if (l4_proto == IPPROTO_UDP)
674 m->ol_flags |= PKT_TX_UDP_CKSUM;
676 case (offsetof(struct sctp_hdr, cksum)):
677 if (l4_proto == IPPROTO_SCTP)
678 m->ol_flags |= PKT_TX_SCTP_CKSUM;
686 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
687 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
688 case VIRTIO_NET_HDR_GSO_TCPV4:
689 case VIRTIO_NET_HDR_GSO_TCPV6:
690 tcp_hdr = (struct tcp_hdr *)l4_hdr;
691 m->ol_flags |= PKT_TX_TCP_SEG;
692 m->tso_segsz = hdr->gso_size;
693 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
696 RTE_LOG(WARNING, VHOST_DATA,
697 "unsupported gso type %u.\n", hdr->gso_type);
703 #define RARP_PKT_SIZE 64
706 make_rarp_packet(struct rte_mbuf *rarp_mbuf, const struct ether_addr *mac)
708 struct ether_hdr *eth_hdr;
709 struct arp_hdr *rarp;
711 if (rarp_mbuf->buf_len < 64) {
712 RTE_LOG(WARNING, VHOST_DATA,
713 "failed to make RARP; mbuf size too small %u (< %d)\n",
714 rarp_mbuf->buf_len, RARP_PKT_SIZE);
718 /* Ethernet header. */
719 eth_hdr = rte_pktmbuf_mtod_offset(rarp_mbuf, struct ether_hdr *, 0);
720 memset(eth_hdr->d_addr.addr_bytes, 0xff, ETHER_ADDR_LEN);
721 ether_addr_copy(mac, ð_hdr->s_addr);
722 eth_hdr->ether_type = htons(ETHER_TYPE_RARP);
725 rarp = (struct arp_hdr *)(eth_hdr + 1);
726 rarp->arp_hrd = htons(ARP_HRD_ETHER);
727 rarp->arp_pro = htons(ETHER_TYPE_IPv4);
728 rarp->arp_hln = ETHER_ADDR_LEN;
730 rarp->arp_op = htons(ARP_OP_REVREQUEST);
732 ether_addr_copy(mac, &rarp->arp_data.arp_sha);
733 ether_addr_copy(mac, &rarp->arp_data.arp_tha);
734 memset(&rarp->arp_data.arp_sip, 0x00, 4);
735 memset(&rarp->arp_data.arp_tip, 0x00, 4);
737 rarp_mbuf->pkt_len = rarp_mbuf->data_len = RARP_PKT_SIZE;
742 static inline void __attribute__((always_inline))
743 put_zmbuf(struct zcopy_mbuf *zmbuf)
748 static inline int __attribute__((always_inline))
749 copy_desc_to_mbuf(struct virtio_net *dev, struct vring_desc *descs,
750 uint16_t max_desc, struct rte_mbuf *m, uint16_t desc_idx,
751 struct rte_mempool *mbuf_pool)
753 struct vring_desc *desc;
755 uint32_t desc_avail, desc_offset;
756 uint32_t mbuf_avail, mbuf_offset;
758 struct rte_mbuf *cur = m, *prev = m;
759 struct virtio_net_hdr *hdr = NULL;
760 /* A counter to avoid desc dead loop chain */
761 uint32_t nr_desc = 1;
763 desc = &descs[desc_idx];
764 if (unlikely((desc->len < dev->vhost_hlen)) ||
765 (desc->flags & VRING_DESC_F_INDIRECT))
768 desc_addr = gpa_to_vva(dev, desc->addr);
769 if (unlikely(!desc_addr))
772 if (virtio_net_with_host_offload(dev)) {
773 hdr = (struct virtio_net_hdr *)((uintptr_t)desc_addr);
778 * A virtio driver normally uses at least 2 desc buffers
779 * for Tx: the first for storing the header, and others
780 * for storing the data.
782 if (likely((desc->len == dev->vhost_hlen) &&
783 (desc->flags & VRING_DESC_F_NEXT) != 0)) {
784 desc = &descs[desc->next];
785 if (unlikely(desc->flags & VRING_DESC_F_INDIRECT))
788 desc_addr = gpa_to_vva(dev, desc->addr);
789 if (unlikely(!desc_addr))
793 desc_avail = desc->len;
796 desc_avail = desc->len - dev->vhost_hlen;
797 desc_offset = dev->vhost_hlen;
800 rte_prefetch0((void *)(uintptr_t)(desc_addr + desc_offset));
802 PRINT_PACKET(dev, (uintptr_t)(desc_addr + desc_offset), desc_avail, 0);
805 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
809 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
812 * A desc buf might across two host physical pages that are
813 * not continuous. In such case (gpa_to_hpa returns 0), data
814 * will be copied even though zero copy is enabled.
816 if (unlikely(dev->dequeue_zero_copy && (hpa = gpa_to_hpa(dev,
817 desc->addr + desc_offset, cpy_len)))) {
818 cur->data_len = cpy_len;
820 cur->buf_addr = (void *)(uintptr_t)desc_addr;
821 cur->buf_physaddr = hpa;
824 * In zero copy mode, one mbuf can only reference data
825 * for one or partial of one desc buff.
827 mbuf_avail = cpy_len;
829 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
831 (void *)((uintptr_t)(desc_addr + desc_offset)),
835 mbuf_avail -= cpy_len;
836 mbuf_offset += cpy_len;
837 desc_avail -= cpy_len;
838 desc_offset += cpy_len;
840 /* This desc reaches to its end, get the next one */
841 if (desc_avail == 0) {
842 if ((desc->flags & VRING_DESC_F_NEXT) == 0)
845 if (unlikely(desc->next >= max_desc ||
846 ++nr_desc > max_desc))
848 desc = &descs[desc->next];
849 if (unlikely(desc->flags & VRING_DESC_F_INDIRECT))
852 desc_addr = gpa_to_vva(dev, desc->addr);
853 if (unlikely(!desc_addr))
856 rte_prefetch0((void *)(uintptr_t)desc_addr);
859 desc_avail = desc->len;
861 PRINT_PACKET(dev, (uintptr_t)desc_addr, desc->len, 0);
865 * This mbuf reaches to its end, get a new one
868 if (mbuf_avail == 0) {
869 cur = rte_pktmbuf_alloc(mbuf_pool);
870 if (unlikely(cur == NULL)) {
871 RTE_LOG(ERR, VHOST_DATA, "Failed to "
872 "allocate memory for mbuf.\n");
877 prev->data_len = mbuf_offset;
879 m->pkt_len += mbuf_offset;
883 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
887 prev->data_len = mbuf_offset;
888 m->pkt_len += mbuf_offset;
891 vhost_dequeue_offload(hdr, m);
896 static inline void __attribute__((always_inline))
897 update_used_ring(struct virtio_net *dev, struct vhost_virtqueue *vq,
898 uint32_t used_idx, uint32_t desc_idx)
900 vq->used->ring[used_idx].id = desc_idx;
901 vq->used->ring[used_idx].len = 0;
902 vhost_log_used_vring(dev, vq,
903 offsetof(struct vring_used, ring[used_idx]),
904 sizeof(vq->used->ring[used_idx]));
907 static inline void __attribute__((always_inline))
908 update_used_idx(struct virtio_net *dev, struct vhost_virtqueue *vq,
911 if (unlikely(count == 0))
917 vq->used->idx += count;
918 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
919 sizeof(vq->used->idx));
921 /* Kick guest if required. */
922 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
923 && (vq->callfd >= 0))
924 eventfd_write(vq->callfd, (eventfd_t)1);
927 static inline struct zcopy_mbuf *__attribute__((always_inline))
928 get_zmbuf(struct vhost_virtqueue *vq)
934 /* search [last_zmbuf_idx, zmbuf_size) */
935 i = vq->last_zmbuf_idx;
936 last = vq->zmbuf_size;
939 for (; i < last; i++) {
940 if (vq->zmbufs[i].in_use == 0) {
941 vq->last_zmbuf_idx = i + 1;
942 vq->zmbufs[i].in_use = 1;
943 return &vq->zmbufs[i];
949 /* search [0, last_zmbuf_idx) */
951 last = vq->last_zmbuf_idx;
958 static inline bool __attribute__((always_inline))
959 mbuf_is_consumed(struct rte_mbuf *m)
962 if (rte_mbuf_refcnt_read(m) > 1)
971 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
972 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
974 struct virtio_net *dev;
975 struct rte_mbuf *rarp_mbuf = NULL;
976 struct vhost_virtqueue *vq;
977 uint32_t desc_indexes[MAX_PKT_BURST];
980 uint16_t free_entries;
983 dev = get_device(vid);
987 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->virt_qp_nb))) {
988 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
989 dev->vid, __func__, queue_id);
993 vq = dev->virtqueue[queue_id];
994 if (unlikely(vq->enabled == 0))
997 if (unlikely(dev->dequeue_zero_copy)) {
998 struct zcopy_mbuf *zmbuf, *next;
1001 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1002 zmbuf != NULL; zmbuf = next) {
1003 next = TAILQ_NEXT(zmbuf, next);
1005 if (mbuf_is_consumed(zmbuf->mbuf)) {
1006 used_idx = vq->last_used_idx++ & (vq->size - 1);
1007 update_used_ring(dev, vq, used_idx,
1011 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1012 rte_pktmbuf_free(zmbuf->mbuf);
1018 update_used_idx(dev, vq, nr_updated);
1022 * Construct a RARP broadcast packet, and inject it to the "pkts"
1023 * array, to looks like that guest actually send such packet.
1025 * Check user_send_rarp() for more information.
1027 if (unlikely(rte_atomic16_cmpset((volatile uint16_t *)
1028 &dev->broadcast_rarp.cnt, 1, 0))) {
1029 rarp_mbuf = rte_pktmbuf_alloc(mbuf_pool);
1030 if (rarp_mbuf == NULL) {
1031 RTE_LOG(ERR, VHOST_DATA,
1032 "Failed to allocate memory for mbuf.\n");
1036 if (make_rarp_packet(rarp_mbuf, &dev->mac)) {
1037 rte_pktmbuf_free(rarp_mbuf);
1044 free_entries = *((volatile uint16_t *)&vq->avail->idx) -
1046 if (free_entries == 0)
1049 LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1051 /* Prefetch available and used ring */
1052 avail_idx = vq->last_avail_idx & (vq->size - 1);
1053 used_idx = vq->last_used_idx & (vq->size - 1);
1054 rte_prefetch0(&vq->avail->ring[avail_idx]);
1055 rte_prefetch0(&vq->used->ring[used_idx]);
1057 count = RTE_MIN(count, MAX_PKT_BURST);
1058 count = RTE_MIN(count, free_entries);
1059 LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1062 /* Retrieve all of the head indexes first to avoid caching issues. */
1063 for (i = 0; i < count; i++) {
1064 avail_idx = (vq->last_avail_idx + i) & (vq->size - 1);
1065 used_idx = (vq->last_used_idx + i) & (vq->size - 1);
1066 desc_indexes[i] = vq->avail->ring[avail_idx];
1068 if (likely(dev->dequeue_zero_copy == 0))
1069 update_used_ring(dev, vq, used_idx, desc_indexes[i]);
1072 /* Prefetch descriptor index. */
1073 rte_prefetch0(&vq->desc[desc_indexes[0]]);
1074 for (i = 0; i < count; i++) {
1075 struct vring_desc *desc;
1079 if (likely(i + 1 < count))
1080 rte_prefetch0(&vq->desc[desc_indexes[i + 1]]);
1082 if (vq->desc[desc_indexes[i]].flags & VRING_DESC_F_INDIRECT) {
1083 desc = (struct vring_desc *)(uintptr_t)gpa_to_vva(dev,
1084 vq->desc[desc_indexes[i]].addr);
1085 if (unlikely(!desc))
1088 rte_prefetch0(desc);
1089 sz = vq->desc[desc_indexes[i]].len / sizeof(*desc);
1094 idx = desc_indexes[i];
1097 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
1098 if (unlikely(pkts[i] == NULL)) {
1099 RTE_LOG(ERR, VHOST_DATA,
1100 "Failed to allocate memory for mbuf.\n");
1104 err = copy_desc_to_mbuf(dev, desc, sz, pkts[i], idx, mbuf_pool);
1105 if (unlikely(err)) {
1106 rte_pktmbuf_free(pkts[i]);
1110 if (unlikely(dev->dequeue_zero_copy)) {
1111 struct zcopy_mbuf *zmbuf;
1113 zmbuf = get_zmbuf(vq);
1115 rte_pktmbuf_free(pkts[i]);
1118 zmbuf->mbuf = pkts[i];
1119 zmbuf->desc_idx = desc_indexes[i];
1122 * Pin lock the mbuf; we will check later to see
1123 * whether the mbuf is freed (when we are the last
1124 * user) or not. If that's the case, we then could
1125 * update the used ring safely.
1127 rte_mbuf_refcnt_update(pkts[i], 1);
1130 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1133 vq->last_avail_idx += i;
1135 if (likely(dev->dequeue_zero_copy == 0)) {
1136 vq->last_used_idx += i;
1137 update_used_idx(dev, vq, i);
1141 if (unlikely(rarp_mbuf != NULL)) {
1143 * Inject it to the head of "pkts" array, so that switch's mac
1144 * learning table will get updated first.
1146 memmove(&pkts[1], pkts, i * sizeof(struct rte_mbuf *));
1147 pkts[0] = rarp_mbuf;