4 * Copyright(c) 2010-2014 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>
48 #include "vhost-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;
95 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
97 memset(net_hdr, 0, sizeof(struct virtio_net_hdr));
99 if (m_buf->ol_flags & PKT_TX_L4_MASK) {
100 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
101 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
103 switch (m_buf->ol_flags & PKT_TX_L4_MASK) {
104 case PKT_TX_TCP_CKSUM:
105 net_hdr->csum_offset = (offsetof(struct tcp_hdr,
108 case PKT_TX_UDP_CKSUM:
109 net_hdr->csum_offset = (offsetof(struct udp_hdr,
112 case PKT_TX_SCTP_CKSUM:
113 net_hdr->csum_offset = (offsetof(struct sctp_hdr,
119 if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
120 if (m_buf->ol_flags & PKT_TX_IPV4)
121 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
123 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
124 net_hdr->gso_size = m_buf->tso_segsz;
125 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
133 copy_virtio_net_hdr(struct vhost_virtqueue *vq, uint64_t desc_addr,
134 struct virtio_net_hdr_mrg_rxbuf hdr)
136 if (vq->vhost_hlen == sizeof(struct virtio_net_hdr_mrg_rxbuf))
137 *(struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)desc_addr = hdr;
139 *(struct virtio_net_hdr *)(uintptr_t)desc_addr = hdr.hdr;
142 static inline int __attribute__((always_inline))
143 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
144 struct rte_mbuf *m, uint16_t desc_idx, uint32_t *copied)
146 uint32_t desc_avail, desc_offset;
147 uint32_t mbuf_avail, mbuf_offset;
149 struct vring_desc *desc;
151 struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0, 0, 0, 0, 0, 0}, 0};
153 desc = &vq->desc[desc_idx];
154 if (unlikely(desc->len < vq->vhost_hlen))
157 desc_addr = gpa_to_vva(dev, desc->addr);
158 rte_prefetch0((void *)(uintptr_t)desc_addr);
160 virtio_enqueue_offload(m, &virtio_hdr.hdr);
161 copy_virtio_net_hdr(vq, desc_addr, virtio_hdr);
162 vhost_log_write(dev, desc->addr, vq->vhost_hlen);
163 PRINT_PACKET(dev, (uintptr_t)desc_addr, vq->vhost_hlen, 0);
165 desc_offset = vq->vhost_hlen;
166 desc_avail = desc->len - vq->vhost_hlen;
168 *copied = rte_pktmbuf_pkt_len(m);
169 mbuf_avail = rte_pktmbuf_data_len(m);
171 while (mbuf_avail != 0 || m->next != NULL) {
172 /* done with current mbuf, fetch next */
173 if (mbuf_avail == 0) {
177 mbuf_avail = rte_pktmbuf_data_len(m);
180 /* done with current desc buf, fetch next */
181 if (desc_avail == 0) {
182 if ((desc->flags & VRING_DESC_F_NEXT) == 0) {
183 /* Room in vring buffer is not enough */
187 desc = &vq->desc[desc->next];
188 desc_addr = gpa_to_vva(dev, desc->addr);
190 desc_avail = desc->len;
193 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
194 rte_memcpy((void *)((uintptr_t)(desc_addr + desc_offset)),
195 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
197 vhost_log_write(dev, desc->addr + desc_offset, cpy_len);
198 PRINT_PACKET(dev, (uintptr_t)(desc_addr + desc_offset),
201 mbuf_avail -= cpy_len;
202 mbuf_offset += cpy_len;
203 desc_avail -= cpy_len;
204 desc_offset += cpy_len;
211 * As many data cores may want to access available buffers
212 * they need to be reserved.
214 static inline uint32_t
215 reserve_avail_buf(struct vhost_virtqueue *vq, uint32_t count,
216 uint16_t *start, uint16_t *end)
218 uint16_t res_start_idx;
219 uint16_t res_end_idx;
221 uint16_t free_entries;
224 count = RTE_MIN(count, (uint32_t)MAX_PKT_BURST);
227 res_start_idx = vq->last_used_idx_res;
228 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
230 free_entries = avail_idx - res_start_idx;
231 count = RTE_MIN(count, free_entries);
235 res_end_idx = res_start_idx + count;
238 * update vq->last_used_idx_res atomically; try again if failed.
240 * TODO: Allow to disable cmpset if no concurrency in application.
242 success = rte_atomic16_cmpset(&vq->last_used_idx_res,
243 res_start_idx, res_end_idx);
244 if (unlikely(!success))
247 *start = res_start_idx;
254 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
255 * be received from the physical port or from another virtio device. A packet
256 * count is returned to indicate the number of packets that are succesfully
257 * added to the RX queue. This function works when the mbuf is scattered, but
258 * it doesn't support the mergeable feature.
260 static inline uint32_t __attribute__((always_inline))
261 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
262 struct rte_mbuf **pkts, uint32_t count)
264 struct vhost_virtqueue *vq;
265 uint16_t res_start_idx, res_end_idx;
266 uint16_t desc_indexes[MAX_PKT_BURST];
269 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") virtio_dev_rx()\n", dev->device_fh);
270 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->virt_qp_nb))) {
271 RTE_LOG(ERR, VHOST_DATA,
272 "%s (%"PRIu64"): virtqueue idx:%d invalid.\n",
273 __func__, dev->device_fh, queue_id);
277 vq = dev->virtqueue[queue_id];
278 if (unlikely(vq->enabled == 0))
281 count = reserve_avail_buf(vq, count, &res_start_idx, &res_end_idx);
285 LOG_DEBUG(VHOST_DATA,
286 "(%"PRIu64") res_start_idx %d| res_end_idx Index %d\n",
287 dev->device_fh, res_start_idx, res_end_idx);
289 /* Retrieve all of the desc indexes first to avoid caching issues. */
290 rte_prefetch0(&vq->avail->ring[res_start_idx & (vq->size - 1)]);
291 for (i = 0; i < count; i++) {
292 desc_indexes[i] = vq->avail->ring[(res_start_idx + i) &
296 rte_prefetch0(&vq->desc[desc_indexes[0]]);
297 for (i = 0; i < count; i++) {
298 uint16_t desc_idx = desc_indexes[i];
299 uint16_t used_idx = (res_start_idx + i) & (vq->size - 1);
303 err = copy_mbuf_to_desc(dev, vq, pkts[i], desc_idx, &copied);
305 vq->used->ring[used_idx].id = desc_idx;
307 vq->used->ring[used_idx].len = vq->vhost_hlen;
309 vq->used->ring[used_idx].len = copied + vq->vhost_hlen;
310 vhost_log_used_vring(dev, vq,
311 offsetof(struct vring_used, ring[used_idx]),
312 sizeof(vq->used->ring[used_idx]));
315 rte_prefetch0(&vq->desc[desc_indexes[i+1]]);
318 rte_compiler_barrier();
320 /* Wait until it's our turn to add our buffer to the used ring. */
321 while (unlikely(vq->last_used_idx != res_start_idx))
324 *(volatile uint16_t *)&vq->used->idx += count;
325 vq->last_used_idx = res_end_idx;
326 vhost_log_used_vring(dev, vq,
327 offsetof(struct vring_used, idx),
328 sizeof(vq->used->idx));
330 /* flush used->idx update before we read avail->flags. */
333 /* Kick the guest if necessary. */
334 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
335 eventfd_write(vq->callfd, (eventfd_t)1);
340 fill_vec_buf(struct vhost_virtqueue *vq, uint32_t avail_idx,
341 uint32_t *allocated, uint32_t *vec_idx)
343 uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
344 uint32_t vec_id = *vec_idx;
345 uint32_t len = *allocated;
348 if (vec_id >= BUF_VECTOR_MAX)
351 len += vq->desc[idx].len;
352 vq->buf_vec[vec_id].buf_addr = vq->desc[idx].addr;
353 vq->buf_vec[vec_id].buf_len = vq->desc[idx].len;
354 vq->buf_vec[vec_id].desc_idx = idx;
357 if ((vq->desc[idx].flags & VRING_DESC_F_NEXT) == 0)
360 idx = vq->desc[idx].next;
370 * As many data cores may want to access available buffers concurrently,
371 * they need to be reserved.
373 * Returns -1 on fail, 0 on success
376 reserve_avail_buf_mergeable(struct vhost_virtqueue *vq, uint32_t size,
377 uint16_t *start, uint16_t *end)
379 uint16_t res_start_idx;
380 uint16_t res_cur_idx;
387 res_start_idx = vq->last_used_idx_res;
388 res_cur_idx = res_start_idx;
394 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
395 if (unlikely(res_cur_idx == avail_idx))
398 if (unlikely(fill_vec_buf(vq, res_cur_idx, &allocated,
405 if (allocated >= size)
409 * if we tried all available ring items, and still
410 * can't get enough buf, it means something abnormal
413 if (unlikely(tries >= vq->size))
418 * update vq->last_used_idx_res atomically.
419 * retry again if failed.
421 if (rte_atomic16_cmpset(&vq->last_used_idx_res,
422 res_start_idx, res_cur_idx) == 0)
425 *start = res_start_idx;
430 static inline uint32_t __attribute__((always_inline))
431 copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,
432 uint16_t res_start_idx, uint16_t res_end_idx,
435 struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0, 0, 0, 0, 0, 0}, 0};
436 uint32_t vec_idx = 0;
437 uint16_t cur_idx = res_start_idx;
439 uint32_t mbuf_offset, mbuf_avail;
440 uint32_t desc_offset, desc_avail;
442 uint16_t desc_idx, used_idx;
444 if (unlikely(m == NULL))
447 LOG_DEBUG(VHOST_DATA,
448 "(%"PRIu64") Current Index %d| End Index %d\n",
449 dev->device_fh, cur_idx, res_end_idx);
451 if (vq->buf_vec[vec_idx].buf_len < vq->vhost_hlen)
454 desc_addr = gpa_to_vva(dev, vq->buf_vec[vec_idx].buf_addr);
455 rte_prefetch0((void *)(uintptr_t)desc_addr);
457 virtio_hdr.num_buffers = res_end_idx - res_start_idx;
458 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") RX: Num merge buffers %d\n",
459 dev->device_fh, virtio_hdr.num_buffers);
461 virtio_enqueue_offload(m, &virtio_hdr.hdr);
462 copy_virtio_net_hdr(vq, desc_addr, virtio_hdr);
463 vhost_log_write(dev, vq->buf_vec[vec_idx].buf_addr, vq->vhost_hlen);
464 PRINT_PACKET(dev, (uintptr_t)desc_addr, vq->vhost_hlen, 0);
466 desc_avail = vq->buf_vec[vec_idx].buf_len - vq->vhost_hlen;
467 desc_offset = vq->vhost_hlen;
469 mbuf_avail = rte_pktmbuf_data_len(m);
471 while (mbuf_avail != 0 || m->next != NULL) {
472 /* done with current desc buf, get the next one */
473 if (desc_avail == 0) {
474 desc_idx = vq->buf_vec[vec_idx].desc_idx;
476 if (!(vq->desc[desc_idx].flags & VRING_DESC_F_NEXT)) {
477 /* Update used ring with desc information */
478 used_idx = cur_idx++ & (vq->size - 1);
479 vq->used->ring[used_idx].id = desc_idx;
480 vq->used->ring[used_idx].len = desc_offset;
481 vhost_log_used_vring(dev, vq,
482 offsetof(struct vring_used,
484 sizeof(vq->used->ring[used_idx]));
488 desc_addr = gpa_to_vva(dev, vq->buf_vec[vec_idx].buf_addr);
490 /* Prefetch buffer address. */
491 rte_prefetch0((void *)(uintptr_t)desc_addr);
493 desc_avail = vq->buf_vec[vec_idx].buf_len;
496 /* done with current mbuf, get the next one */
497 if (mbuf_avail == 0) {
501 mbuf_avail = rte_pktmbuf_data_len(m);
504 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
505 rte_memcpy((void *)((uintptr_t)(desc_addr + desc_offset)),
506 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
508 vhost_log_write(dev, vq->buf_vec[vec_idx].buf_addr + desc_offset,
510 PRINT_PACKET(dev, (uintptr_t)(desc_addr + desc_offset),
513 mbuf_avail -= cpy_len;
514 mbuf_offset += cpy_len;
515 desc_avail -= cpy_len;
516 desc_offset += cpy_len;
519 used_idx = cur_idx & (vq->size - 1);
520 vq->used->ring[used_idx].id = vq->buf_vec[vec_idx].desc_idx;
521 vq->used->ring[used_idx].len = desc_offset;
522 vhost_log_used_vring(dev, vq,
523 offsetof(struct vring_used, ring[used_idx]),
524 sizeof(vq->used->ring[used_idx]));
526 return res_end_idx - res_start_idx;
529 static inline uint32_t __attribute__((always_inline))
530 virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,
531 struct rte_mbuf **pkts, uint32_t count)
533 struct vhost_virtqueue *vq;
534 uint32_t pkt_idx = 0, nr_used = 0;
537 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") virtio_dev_merge_rx()\n",
539 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->virt_qp_nb))) {
540 RTE_LOG(ERR, VHOST_DATA,
541 "%s (%"PRIu64"): virtqueue idx:%d invalid.\n",
542 __func__, dev->device_fh, queue_id);
546 vq = dev->virtqueue[queue_id];
547 if (unlikely(vq->enabled == 0))
550 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
554 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
555 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + vq->vhost_hlen;
557 if (unlikely(reserve_avail_buf_mergeable(vq, pkt_len,
558 &start, &end) < 0)) {
559 LOG_DEBUG(VHOST_DATA,
560 "(%" PRIu64 ") Failed to get enough desc from vring\n",
565 nr_used = copy_mbuf_to_desc_mergeable(dev, vq, start, end,
567 rte_compiler_barrier();
570 * Wait until it's our turn to add our buffer
573 while (unlikely(vq->last_used_idx != start))
576 *(volatile uint16_t *)&vq->used->idx += nr_used;
577 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
578 sizeof(vq->used->idx));
579 vq->last_used_idx = end;
582 if (likely(pkt_idx)) {
583 /* flush used->idx update before we read avail->flags. */
586 /* Kick the guest if necessary. */
587 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
588 eventfd_write(vq->callfd, (eventfd_t)1);
595 rte_vhost_enqueue_burst(struct virtio_net *dev, uint16_t queue_id,
596 struct rte_mbuf **pkts, uint16_t count)
598 if (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF))
599 return virtio_dev_merge_rx(dev, queue_id, pkts, count);
601 return virtio_dev_rx(dev, queue_id, pkts, count);
605 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
607 struct ipv4_hdr *ipv4_hdr;
608 struct ipv6_hdr *ipv6_hdr;
610 struct ether_hdr *eth_hdr;
613 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
615 m->l2_len = sizeof(struct ether_hdr);
616 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
618 if (ethertype == ETHER_TYPE_VLAN) {
619 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
621 m->l2_len += sizeof(struct vlan_hdr);
622 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
625 l3_hdr = (char *)eth_hdr + m->l2_len;
628 case ETHER_TYPE_IPv4:
629 ipv4_hdr = (struct ipv4_hdr *)l3_hdr;
630 *l4_proto = ipv4_hdr->next_proto_id;
631 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
632 *l4_hdr = (char *)l3_hdr + m->l3_len;
633 m->ol_flags |= PKT_TX_IPV4;
635 case ETHER_TYPE_IPv6:
636 ipv6_hdr = (struct ipv6_hdr *)l3_hdr;
637 *l4_proto = ipv6_hdr->proto;
638 m->l3_len = sizeof(struct ipv6_hdr);
639 *l4_hdr = (char *)l3_hdr + m->l3_len;
640 m->ol_flags |= PKT_TX_IPV6;
649 static inline void __attribute__((always_inline))
650 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
652 uint16_t l4_proto = 0;
654 struct tcp_hdr *tcp_hdr = NULL;
656 parse_ethernet(m, &l4_proto, &l4_hdr);
657 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
658 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
659 switch (hdr->csum_offset) {
660 case (offsetof(struct tcp_hdr, cksum)):
661 if (l4_proto == IPPROTO_TCP)
662 m->ol_flags |= PKT_TX_TCP_CKSUM;
664 case (offsetof(struct udp_hdr, dgram_cksum)):
665 if (l4_proto == IPPROTO_UDP)
666 m->ol_flags |= PKT_TX_UDP_CKSUM;
668 case (offsetof(struct sctp_hdr, cksum)):
669 if (l4_proto == IPPROTO_SCTP)
670 m->ol_flags |= PKT_TX_SCTP_CKSUM;
678 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
679 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
680 case VIRTIO_NET_HDR_GSO_TCPV4:
681 case VIRTIO_NET_HDR_GSO_TCPV6:
682 tcp_hdr = (struct tcp_hdr *)l4_hdr;
683 m->ol_flags |= PKT_TX_TCP_SEG;
684 m->tso_segsz = hdr->gso_size;
685 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
688 RTE_LOG(WARNING, VHOST_DATA,
689 "unsupported gso type %u.\n", hdr->gso_type);
695 #define RARP_PKT_SIZE 64
698 make_rarp_packet(struct rte_mbuf *rarp_mbuf, const struct ether_addr *mac)
700 struct ether_hdr *eth_hdr;
701 struct arp_hdr *rarp;
703 if (rarp_mbuf->buf_len < 64) {
704 RTE_LOG(WARNING, VHOST_DATA,
705 "failed to make RARP; mbuf size too small %u (< %d)\n",
706 rarp_mbuf->buf_len, RARP_PKT_SIZE);
710 /* Ethernet header. */
711 eth_hdr = rte_pktmbuf_mtod_offset(rarp_mbuf, struct ether_hdr *, 0);
712 memset(eth_hdr->d_addr.addr_bytes, 0xff, ETHER_ADDR_LEN);
713 ether_addr_copy(mac, ð_hdr->s_addr);
714 eth_hdr->ether_type = htons(ETHER_TYPE_RARP);
717 rarp = (struct arp_hdr *)(eth_hdr + 1);
718 rarp->arp_hrd = htons(ARP_HRD_ETHER);
719 rarp->arp_pro = htons(ETHER_TYPE_IPv4);
720 rarp->arp_hln = ETHER_ADDR_LEN;
722 rarp->arp_op = htons(ARP_OP_REVREQUEST);
724 ether_addr_copy(mac, &rarp->arp_data.arp_sha);
725 ether_addr_copy(mac, &rarp->arp_data.arp_tha);
726 memset(&rarp->arp_data.arp_sip, 0x00, 4);
727 memset(&rarp->arp_data.arp_tip, 0x00, 4);
729 rarp_mbuf->pkt_len = rarp_mbuf->data_len = RARP_PKT_SIZE;
734 static inline int __attribute__((always_inline))
735 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
736 struct rte_mbuf *m, uint16_t desc_idx,
737 struct rte_mempool *mbuf_pool)
739 struct vring_desc *desc;
741 uint32_t desc_avail, desc_offset;
742 uint32_t mbuf_avail, mbuf_offset;
744 struct rte_mbuf *cur = m, *prev = m;
745 struct virtio_net_hdr *hdr;
747 desc = &vq->desc[desc_idx];
748 if (unlikely(desc->len < vq->vhost_hlen))
751 desc_addr = gpa_to_vva(dev, desc->addr);
752 rte_prefetch0((void *)(uintptr_t)desc_addr);
754 /* Retrieve virtio net header */
755 hdr = (struct virtio_net_hdr *)((uintptr_t)desc_addr);
756 desc_avail = desc->len - vq->vhost_hlen;
757 desc_offset = vq->vhost_hlen;
760 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
761 while (desc_avail != 0 || (desc->flags & VRING_DESC_F_NEXT) != 0) {
762 /* This desc reaches to its end, get the next one */
763 if (desc_avail == 0) {
764 desc = &vq->desc[desc->next];
766 desc_addr = gpa_to_vva(dev, desc->addr);
767 rte_prefetch0((void *)(uintptr_t)desc_addr);
770 desc_avail = desc->len;
772 PRINT_PACKET(dev, (uintptr_t)desc_addr, desc->len, 0);
776 * This mbuf reaches to its end, get a new one
779 if (mbuf_avail == 0) {
780 cur = rte_pktmbuf_alloc(mbuf_pool);
781 if (unlikely(cur == NULL)) {
782 RTE_LOG(ERR, VHOST_DATA, "Failed to "
783 "allocate memory for mbuf.\n");
788 prev->data_len = mbuf_offset;
790 m->pkt_len += mbuf_offset;
794 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
797 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
798 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *, mbuf_offset),
799 (void *)((uintptr_t)(desc_addr + desc_offset)),
802 mbuf_avail -= cpy_len;
803 mbuf_offset += cpy_len;
804 desc_avail -= cpy_len;
805 desc_offset += cpy_len;
808 prev->data_len = mbuf_offset;
809 m->pkt_len += mbuf_offset;
811 if (hdr->flags != 0 || hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE)
812 vhost_dequeue_offload(hdr, m);
818 rte_vhost_dequeue_burst(struct virtio_net *dev, uint16_t queue_id,
819 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
821 struct rte_mbuf *rarp_mbuf = NULL;
822 struct vhost_virtqueue *vq;
823 uint32_t desc_indexes[MAX_PKT_BURST];
826 uint16_t free_entries;
829 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->virt_qp_nb))) {
830 RTE_LOG(ERR, VHOST_DATA,
831 "%s (%"PRIu64"): virtqueue idx:%d invalid.\n",
832 __func__, dev->device_fh, queue_id);
836 vq = dev->virtqueue[queue_id];
837 if (unlikely(vq->enabled == 0))
841 * Construct a RARP broadcast packet, and inject it to the "pkts"
842 * array, to looks like that guest actually send such packet.
844 * Check user_send_rarp() for more information.
846 if (unlikely(rte_atomic16_cmpset((volatile uint16_t *)
847 &dev->broadcast_rarp.cnt, 1, 0))) {
848 rarp_mbuf = rte_pktmbuf_alloc(mbuf_pool);
849 if (rarp_mbuf == NULL) {
850 RTE_LOG(ERR, VHOST_DATA,
851 "Failed to allocate memory for mbuf.\n");
855 if (make_rarp_packet(rarp_mbuf, &dev->mac)) {
856 rte_pktmbuf_free(rarp_mbuf);
863 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
864 free_entries = avail_idx - vq->last_used_idx;
865 if (free_entries == 0)
868 LOG_DEBUG(VHOST_DATA, "%s (%"PRIu64")\n", __func__, dev->device_fh);
870 /* Prefetch available ring to retrieve head indexes. */
871 used_idx = vq->last_used_idx & (vq->size - 1);
872 rte_prefetch0(&vq->avail->ring[used_idx]);
874 count = RTE_MIN(count, MAX_PKT_BURST);
875 count = RTE_MIN(count, free_entries);
876 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") about to dequeue %u buffers\n",
877 dev->device_fh, count);
879 /* Retrieve all of the head indexes first to avoid caching issues. */
880 for (i = 0; i < count; i++) {
881 desc_indexes[i] = vq->avail->ring[(vq->last_used_idx + i) &
885 /* Prefetch descriptor index. */
886 rte_prefetch0(&vq->desc[desc_indexes[0]]);
887 rte_prefetch0(&vq->used->ring[vq->last_used_idx & (vq->size - 1)]);
889 for (i = 0; i < count; i++) {
892 if (likely(i + 1 < count)) {
893 rte_prefetch0(&vq->desc[desc_indexes[i + 1]]);
894 rte_prefetch0(&vq->used->ring[(used_idx + 1) &
898 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
899 if (unlikely(pkts[i] == NULL)) {
900 RTE_LOG(ERR, VHOST_DATA,
901 "Failed to allocate memory for mbuf.\n");
904 err = copy_desc_to_mbuf(dev, vq, pkts[i], desc_indexes[i],
907 rte_pktmbuf_free(pkts[i]);
911 used_idx = vq->last_used_idx++ & (vq->size - 1);
912 vq->used->ring[used_idx].id = desc_indexes[i];
913 vq->used->ring[used_idx].len = 0;
914 vhost_log_used_vring(dev, vq,
915 offsetof(struct vring_used, ring[used_idx]),
916 sizeof(vq->used->ring[used_idx]));
919 rte_compiler_barrier();
921 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
922 sizeof(vq->used->idx));
924 /* Kick guest if required. */
925 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
926 eventfd_write(vq->callfd, (eventfd_t)1);
929 if (unlikely(rarp_mbuf != NULL)) {
931 * Inject it to the head of "pkts" array, so that switch's mac
932 * learning table will get updated first.
934 memmove(&pkts[1], pkts, i * sizeof(struct rte_mbuf *));