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"
49 #include "virtio-net.h"
51 #define MAX_PKT_BURST 32
52 #define VHOST_LOG_PAGE 4096
54 static inline void __attribute__((always_inline))
55 vhost_log_page(uint8_t *log_base, uint64_t page)
57 log_base[page / 8] |= 1 << (page % 8);
60 static inline void __attribute__((always_inline))
61 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
65 if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
66 !dev->log_base || !len))
69 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
72 /* To make sure guest memory updates are committed before logging */
75 page = addr / VHOST_LOG_PAGE;
76 while (page * VHOST_LOG_PAGE < addr + len) {
77 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
82 static inline void __attribute__((always_inline))
83 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
84 uint64_t offset, uint64_t len)
86 vhost_log_write(dev, vq->log_guest_addr + offset, len);
90 is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t qp_nb)
92 return (is_tx ^ (idx & 1)) == 0 && idx < qp_nb * VIRTIO_QNUM;
96 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
98 if (m_buf->ol_flags & PKT_TX_L4_MASK) {
99 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
100 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
102 switch (m_buf->ol_flags & PKT_TX_L4_MASK) {
103 case PKT_TX_TCP_CKSUM:
104 net_hdr->csum_offset = (offsetof(struct tcp_hdr,
107 case PKT_TX_UDP_CKSUM:
108 net_hdr->csum_offset = (offsetof(struct udp_hdr,
111 case PKT_TX_SCTP_CKSUM:
112 net_hdr->csum_offset = (offsetof(struct sctp_hdr,
118 if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
119 if (m_buf->ol_flags & PKT_TX_IPV4)
120 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
122 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
123 net_hdr->gso_size = m_buf->tso_segsz;
124 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
130 copy_virtio_net_hdr(struct vhost_virtqueue *vq, uint64_t desc_addr,
131 struct virtio_net_hdr_mrg_rxbuf hdr)
133 if (vq->vhost_hlen == sizeof(struct virtio_net_hdr_mrg_rxbuf))
134 *(struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)desc_addr = hdr;
136 *(struct virtio_net_hdr *)(uintptr_t)desc_addr = hdr.hdr;
139 static inline int __attribute__((always_inline))
140 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
141 struct rte_mbuf *m, uint16_t desc_idx, uint32_t *copied)
143 uint32_t desc_avail, desc_offset;
144 uint32_t mbuf_avail, mbuf_offset;
146 struct vring_desc *desc;
148 struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0, 0, 0, 0, 0, 0}, 0};
150 desc = &vq->desc[desc_idx];
151 if (unlikely(desc->len < vq->vhost_hlen))
154 desc_addr = gpa_to_vva(dev, desc->addr);
155 rte_prefetch0((void *)(uintptr_t)desc_addr);
157 virtio_enqueue_offload(m, &virtio_hdr.hdr);
158 copy_virtio_net_hdr(vq, desc_addr, virtio_hdr);
159 vhost_log_write(dev, desc->addr, vq->vhost_hlen);
160 PRINT_PACKET(dev, (uintptr_t)desc_addr, vq->vhost_hlen, 0);
162 desc_offset = vq->vhost_hlen;
163 desc_avail = desc->len - vq->vhost_hlen;
165 *copied = rte_pktmbuf_pkt_len(m);
166 mbuf_avail = rte_pktmbuf_data_len(m);
168 while (mbuf_avail != 0 || m->next != NULL) {
169 /* done with current mbuf, fetch next */
170 if (mbuf_avail == 0) {
174 mbuf_avail = rte_pktmbuf_data_len(m);
177 /* done with current desc buf, fetch next */
178 if (desc_avail == 0) {
179 if ((desc->flags & VRING_DESC_F_NEXT) == 0) {
180 /* Room in vring buffer is not enough */
183 if (unlikely(desc->next >= vq->size))
186 desc = &vq->desc[desc->next];
187 desc_addr = gpa_to_vva(dev, desc->addr);
189 desc_avail = desc->len;
192 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
193 rte_memcpy((void *)((uintptr_t)(desc_addr + desc_offset)),
194 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
196 vhost_log_write(dev, desc->addr + desc_offset, cpy_len);
197 PRINT_PACKET(dev, (uintptr_t)(desc_addr + desc_offset),
200 mbuf_avail -= cpy_len;
201 mbuf_offset += cpy_len;
202 desc_avail -= cpy_len;
203 desc_offset += cpy_len;
210 * As many data cores may want to access available buffers
211 * they need to be reserved.
213 static inline uint32_t
214 reserve_avail_buf(struct vhost_virtqueue *vq, uint32_t count,
215 uint16_t *start, uint16_t *end)
217 uint16_t res_start_idx;
218 uint16_t res_end_idx;
220 uint16_t free_entries;
223 count = RTE_MIN(count, (uint32_t)MAX_PKT_BURST);
226 res_start_idx = vq->last_used_idx_res;
227 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
229 free_entries = avail_idx - res_start_idx;
230 count = RTE_MIN(count, free_entries);
234 res_end_idx = res_start_idx + count;
237 * update vq->last_used_idx_res atomically; try again if failed.
239 * TODO: Allow to disable cmpset if no concurrency in application.
241 success = rte_atomic16_cmpset(&vq->last_used_idx_res,
242 res_start_idx, res_end_idx);
243 if (unlikely(!success))
246 *start = res_start_idx;
253 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
254 * be received from the physical port or from another virtio device. A packet
255 * count is returned to indicate the number of packets that are succesfully
256 * added to the RX queue. This function works when the mbuf is scattered, but
257 * it doesn't support the mergeable feature.
259 static inline uint32_t __attribute__((always_inline))
260 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
261 struct rte_mbuf **pkts, uint32_t count)
263 struct vhost_virtqueue *vq;
264 uint16_t res_start_idx, res_end_idx;
265 uint16_t desc_indexes[MAX_PKT_BURST];
268 LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
269 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->virt_qp_nb))) {
270 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
271 dev->vid, __func__, queue_id);
275 vq = dev->virtqueue[queue_id];
276 if (unlikely(vq->enabled == 0))
279 count = reserve_avail_buf(vq, count, &res_start_idx, &res_end_idx);
283 LOG_DEBUG(VHOST_DATA, "(%d) res_start_idx %d | res_end_idx Index %d\n",
284 dev->vid, res_start_idx, res_end_idx);
286 /* Retrieve all of the desc indexes first to avoid caching issues. */
287 rte_prefetch0(&vq->avail->ring[res_start_idx & (vq->size - 1)]);
288 for (i = 0; i < count; i++) {
289 desc_indexes[i] = vq->avail->ring[(res_start_idx + i) &
293 rte_prefetch0(&vq->desc[desc_indexes[0]]);
294 for (i = 0; i < count; i++) {
295 uint16_t desc_idx = desc_indexes[i];
296 uint16_t used_idx = (res_start_idx + i) & (vq->size - 1);
300 err = copy_mbuf_to_desc(dev, vq, pkts[i], desc_idx, &copied);
302 vq->used->ring[used_idx].id = desc_idx;
304 vq->used->ring[used_idx].len = vq->vhost_hlen;
306 vq->used->ring[used_idx].len = copied + vq->vhost_hlen;
307 vhost_log_used_vring(dev, vq,
308 offsetof(struct vring_used, ring[used_idx]),
309 sizeof(vq->used->ring[used_idx]));
312 rte_prefetch0(&vq->desc[desc_indexes[i+1]]);
317 /* Wait until it's our turn to add our buffer to the used ring. */
318 while (unlikely(vq->last_used_idx != res_start_idx))
321 *(volatile uint16_t *)&vq->used->idx += count;
322 vq->last_used_idx = res_end_idx;
323 vhost_log_used_vring(dev, vq,
324 offsetof(struct vring_used, idx),
325 sizeof(vq->used->idx));
327 /* flush used->idx update before we read avail->flags. */
330 /* Kick the guest if necessary. */
331 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
332 && (vq->callfd >= 0))
333 eventfd_write(vq->callfd, (eventfd_t)1);
338 fill_vec_buf(struct vhost_virtqueue *vq, uint32_t avail_idx,
339 uint32_t *allocated, uint32_t *vec_idx)
341 uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
342 uint32_t vec_id = *vec_idx;
343 uint32_t len = *allocated;
346 if (unlikely(vec_id >= BUF_VECTOR_MAX || idx >= vq->size))
349 len += vq->desc[idx].len;
350 vq->buf_vec[vec_id].buf_addr = vq->desc[idx].addr;
351 vq->buf_vec[vec_id].buf_len = vq->desc[idx].len;
352 vq->buf_vec[vec_id].desc_idx = idx;
355 if ((vq->desc[idx].flags & VRING_DESC_F_NEXT) == 0)
358 idx = vq->desc[idx].next;
368 * As many data cores may want to access available buffers concurrently,
369 * they need to be reserved.
371 * Returns -1 on fail, 0 on success
374 reserve_avail_buf_mergeable(struct vhost_virtqueue *vq, uint32_t size,
375 uint16_t *start, uint16_t *end)
377 uint16_t res_start_idx;
378 uint16_t res_cur_idx;
385 res_start_idx = vq->last_used_idx_res;
386 res_cur_idx = res_start_idx;
392 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
393 if (unlikely(res_cur_idx == avail_idx))
396 if (unlikely(fill_vec_buf(vq, res_cur_idx, &allocated,
403 if (allocated >= size)
407 * if we tried all available ring items, and still
408 * can't get enough buf, it means something abnormal
411 if (unlikely(tries >= vq->size))
416 * update vq->last_used_idx_res atomically.
417 * retry again if failed.
419 if (rte_atomic16_cmpset(&vq->last_used_idx_res,
420 res_start_idx, res_cur_idx) == 0)
423 *start = res_start_idx;
428 static inline uint32_t __attribute__((always_inline))
429 copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,
430 uint16_t res_start_idx, uint16_t res_end_idx,
433 struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0, 0, 0, 0, 0, 0}, 0};
434 uint32_t vec_idx = 0;
435 uint16_t cur_idx = res_start_idx;
437 uint32_t mbuf_offset, mbuf_avail;
438 uint32_t desc_offset, desc_avail;
440 uint16_t desc_idx, used_idx;
442 if (unlikely(m == NULL))
445 LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
446 dev->vid, cur_idx, res_end_idx);
448 if (vq->buf_vec[vec_idx].buf_len < vq->vhost_hlen)
451 desc_addr = gpa_to_vva(dev, vq->buf_vec[vec_idx].buf_addr);
452 rte_prefetch0((void *)(uintptr_t)desc_addr);
454 virtio_hdr.num_buffers = res_end_idx - res_start_idx;
455 LOG_DEBUG(VHOST_DATA, "(%d) RX: num merge buffers %d\n",
456 dev->vid, virtio_hdr.num_buffers);
458 virtio_enqueue_offload(m, &virtio_hdr.hdr);
459 copy_virtio_net_hdr(vq, desc_addr, virtio_hdr);
460 vhost_log_write(dev, vq->buf_vec[vec_idx].buf_addr, vq->vhost_hlen);
461 PRINT_PACKET(dev, (uintptr_t)desc_addr, vq->vhost_hlen, 0);
463 desc_avail = vq->buf_vec[vec_idx].buf_len - vq->vhost_hlen;
464 desc_offset = vq->vhost_hlen;
466 mbuf_avail = rte_pktmbuf_data_len(m);
468 while (mbuf_avail != 0 || m->next != NULL) {
469 /* done with current desc buf, get the next one */
470 if (desc_avail == 0) {
471 desc_idx = vq->buf_vec[vec_idx].desc_idx;
473 if (!(vq->desc[desc_idx].flags & VRING_DESC_F_NEXT)) {
474 /* Update used ring with desc information */
475 used_idx = cur_idx++ & (vq->size - 1);
476 vq->used->ring[used_idx].id = desc_idx;
477 vq->used->ring[used_idx].len = desc_offset;
478 vhost_log_used_vring(dev, vq,
479 offsetof(struct vring_used,
481 sizeof(vq->used->ring[used_idx]));
485 desc_addr = gpa_to_vva(dev, vq->buf_vec[vec_idx].buf_addr);
487 /* Prefetch buffer address. */
488 rte_prefetch0((void *)(uintptr_t)desc_addr);
490 desc_avail = vq->buf_vec[vec_idx].buf_len;
493 /* done with current mbuf, get the next one */
494 if (mbuf_avail == 0) {
498 mbuf_avail = rte_pktmbuf_data_len(m);
501 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
502 rte_memcpy((void *)((uintptr_t)(desc_addr + desc_offset)),
503 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
505 vhost_log_write(dev, vq->buf_vec[vec_idx].buf_addr + desc_offset,
507 PRINT_PACKET(dev, (uintptr_t)(desc_addr + desc_offset),
510 mbuf_avail -= cpy_len;
511 mbuf_offset += cpy_len;
512 desc_avail -= cpy_len;
513 desc_offset += cpy_len;
516 used_idx = cur_idx & (vq->size - 1);
517 vq->used->ring[used_idx].id = vq->buf_vec[vec_idx].desc_idx;
518 vq->used->ring[used_idx].len = desc_offset;
519 vhost_log_used_vring(dev, vq,
520 offsetof(struct vring_used, ring[used_idx]),
521 sizeof(vq->used->ring[used_idx]));
523 return res_end_idx - res_start_idx;
526 static inline uint32_t __attribute__((always_inline))
527 virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,
528 struct rte_mbuf **pkts, uint32_t count)
530 struct vhost_virtqueue *vq;
531 uint32_t pkt_idx = 0, nr_used = 0;
534 LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
535 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->virt_qp_nb))) {
536 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
537 dev->vid, __func__, queue_id);
541 vq = dev->virtqueue[queue_id];
542 if (unlikely(vq->enabled == 0))
545 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
549 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
550 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + vq->vhost_hlen;
552 if (unlikely(reserve_avail_buf_mergeable(vq, pkt_len,
553 &start, &end) < 0)) {
554 LOG_DEBUG(VHOST_DATA,
555 "(%d) failed to get enough desc from vring\n",
560 nr_used = copy_mbuf_to_desc_mergeable(dev, vq, start, end,
565 * Wait until it's our turn to add our buffer
568 while (unlikely(vq->last_used_idx != start))
571 *(volatile uint16_t *)&vq->used->idx += nr_used;
572 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
573 sizeof(vq->used->idx));
574 vq->last_used_idx = end;
577 if (likely(pkt_idx)) {
578 /* flush used->idx update before we read avail->flags. */
581 /* Kick the guest if necessary. */
582 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
583 && (vq->callfd >= 0))
584 eventfd_write(vq->callfd, (eventfd_t)1);
591 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
592 struct rte_mbuf **pkts, uint16_t count)
594 struct virtio_net *dev = get_device(vid);
599 if (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF))
600 return virtio_dev_merge_rx(dev, queue_id, pkts, count);
602 return virtio_dev_rx(dev, queue_id, pkts, count);
606 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
608 struct ipv4_hdr *ipv4_hdr;
609 struct ipv6_hdr *ipv6_hdr;
611 struct ether_hdr *eth_hdr;
614 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
616 m->l2_len = sizeof(struct ether_hdr);
617 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
619 if (ethertype == ETHER_TYPE_VLAN) {
620 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
622 m->l2_len += sizeof(struct vlan_hdr);
623 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
626 l3_hdr = (char *)eth_hdr + m->l2_len;
629 case ETHER_TYPE_IPv4:
630 ipv4_hdr = (struct ipv4_hdr *)l3_hdr;
631 *l4_proto = ipv4_hdr->next_proto_id;
632 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
633 *l4_hdr = (char *)l3_hdr + m->l3_len;
634 m->ol_flags |= PKT_TX_IPV4;
636 case ETHER_TYPE_IPv6:
637 ipv6_hdr = (struct ipv6_hdr *)l3_hdr;
638 *l4_proto = ipv6_hdr->proto;
639 m->l3_len = sizeof(struct ipv6_hdr);
640 *l4_hdr = (char *)l3_hdr + m->l3_len;
641 m->ol_flags |= PKT_TX_IPV6;
650 static inline void __attribute__((always_inline))
651 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
653 uint16_t l4_proto = 0;
655 struct tcp_hdr *tcp_hdr = NULL;
657 parse_ethernet(m, &l4_proto, &l4_hdr);
658 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
659 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
660 switch (hdr->csum_offset) {
661 case (offsetof(struct tcp_hdr, cksum)):
662 if (l4_proto == IPPROTO_TCP)
663 m->ol_flags |= PKT_TX_TCP_CKSUM;
665 case (offsetof(struct udp_hdr, dgram_cksum)):
666 if (l4_proto == IPPROTO_UDP)
667 m->ol_flags |= PKT_TX_UDP_CKSUM;
669 case (offsetof(struct sctp_hdr, cksum)):
670 if (l4_proto == IPPROTO_SCTP)
671 m->ol_flags |= PKT_TX_SCTP_CKSUM;
679 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
680 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
681 case VIRTIO_NET_HDR_GSO_TCPV4:
682 case VIRTIO_NET_HDR_GSO_TCPV6:
683 tcp_hdr = (struct tcp_hdr *)l4_hdr;
684 m->ol_flags |= PKT_TX_TCP_SEG;
685 m->tso_segsz = hdr->gso_size;
686 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
689 RTE_LOG(WARNING, VHOST_DATA,
690 "unsupported gso type %u.\n", hdr->gso_type);
696 #define RARP_PKT_SIZE 64
699 make_rarp_packet(struct rte_mbuf *rarp_mbuf, const struct ether_addr *mac)
701 struct ether_hdr *eth_hdr;
702 struct arp_hdr *rarp;
704 if (rarp_mbuf->buf_len < 64) {
705 RTE_LOG(WARNING, VHOST_DATA,
706 "failed to make RARP; mbuf size too small %u (< %d)\n",
707 rarp_mbuf->buf_len, RARP_PKT_SIZE);
711 /* Ethernet header. */
712 eth_hdr = rte_pktmbuf_mtod_offset(rarp_mbuf, struct ether_hdr *, 0);
713 memset(eth_hdr->d_addr.addr_bytes, 0xff, ETHER_ADDR_LEN);
714 ether_addr_copy(mac, ð_hdr->s_addr);
715 eth_hdr->ether_type = htons(ETHER_TYPE_RARP);
718 rarp = (struct arp_hdr *)(eth_hdr + 1);
719 rarp->arp_hrd = htons(ARP_HRD_ETHER);
720 rarp->arp_pro = htons(ETHER_TYPE_IPv4);
721 rarp->arp_hln = ETHER_ADDR_LEN;
723 rarp->arp_op = htons(ARP_OP_REVREQUEST);
725 ether_addr_copy(mac, &rarp->arp_data.arp_sha);
726 ether_addr_copy(mac, &rarp->arp_data.arp_tha);
727 memset(&rarp->arp_data.arp_sip, 0x00, 4);
728 memset(&rarp->arp_data.arp_tip, 0x00, 4);
730 rarp_mbuf->pkt_len = rarp_mbuf->data_len = RARP_PKT_SIZE;
735 static inline int __attribute__((always_inline))
736 copy_desc_to_mbuf(struct virtio_net *dev, struct vhost_virtqueue *vq,
737 struct rte_mbuf *m, uint16_t desc_idx,
738 struct rte_mempool *mbuf_pool)
740 struct vring_desc *desc;
742 uint32_t desc_avail, desc_offset;
743 uint32_t mbuf_avail, mbuf_offset;
745 struct rte_mbuf *cur = m, *prev = m;
746 struct virtio_net_hdr *hdr;
747 /* A counter to avoid desc dead loop chain */
748 uint32_t nr_desc = 1;
750 desc = &vq->desc[desc_idx];
751 if (unlikely(desc->len < vq->vhost_hlen))
754 desc_addr = gpa_to_vva(dev, desc->addr);
755 rte_prefetch0((void *)(uintptr_t)desc_addr);
757 /* Retrieve virtio net header */
758 hdr = (struct virtio_net_hdr *)((uintptr_t)desc_addr);
759 desc_avail = desc->len - vq->vhost_hlen;
760 desc_offset = vq->vhost_hlen;
763 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
764 while (desc_avail != 0 || (desc->flags & VRING_DESC_F_NEXT) != 0) {
765 /* This desc reaches to its end, get the next one */
766 if (desc_avail == 0) {
767 if (unlikely(desc->next >= vq->size ||
768 ++nr_desc >= vq->size))
770 desc = &vq->desc[desc->next];
772 desc_addr = gpa_to_vva(dev, desc->addr);
773 rte_prefetch0((void *)(uintptr_t)desc_addr);
776 desc_avail = desc->len;
778 PRINT_PACKET(dev, (uintptr_t)desc_addr, desc->len, 0);
782 * This mbuf reaches to its end, get a new one
785 if (mbuf_avail == 0) {
786 cur = rte_pktmbuf_alloc(mbuf_pool);
787 if (unlikely(cur == NULL)) {
788 RTE_LOG(ERR, VHOST_DATA, "Failed to "
789 "allocate memory for mbuf.\n");
794 prev->data_len = mbuf_offset;
796 m->pkt_len += mbuf_offset;
800 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
803 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
804 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *, mbuf_offset),
805 (void *)((uintptr_t)(desc_addr + desc_offset)),
808 mbuf_avail -= cpy_len;
809 mbuf_offset += cpy_len;
810 desc_avail -= cpy_len;
811 desc_offset += cpy_len;
814 prev->data_len = mbuf_offset;
815 m->pkt_len += mbuf_offset;
817 if (hdr->flags != 0 || hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE)
818 vhost_dequeue_offload(hdr, m);
824 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
825 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
827 struct virtio_net *dev;
828 struct rte_mbuf *rarp_mbuf = NULL;
829 struct vhost_virtqueue *vq;
830 uint32_t desc_indexes[MAX_PKT_BURST];
833 uint16_t free_entries;
836 dev = get_device(vid);
840 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->virt_qp_nb))) {
841 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
842 dev->vid, __func__, queue_id);
846 vq = dev->virtqueue[queue_id];
847 if (unlikely(vq->enabled == 0))
851 * Construct a RARP broadcast packet, and inject it to the "pkts"
852 * array, to looks like that guest actually send such packet.
854 * Check user_send_rarp() for more information.
856 if (unlikely(rte_atomic16_cmpset((volatile uint16_t *)
857 &dev->broadcast_rarp.cnt, 1, 0))) {
858 rarp_mbuf = rte_pktmbuf_alloc(mbuf_pool);
859 if (rarp_mbuf == NULL) {
860 RTE_LOG(ERR, VHOST_DATA,
861 "Failed to allocate memory for mbuf.\n");
865 if (make_rarp_packet(rarp_mbuf, &dev->mac)) {
866 rte_pktmbuf_free(rarp_mbuf);
873 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
874 free_entries = avail_idx - vq->last_used_idx;
875 if (free_entries == 0)
878 LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
880 /* Prefetch available ring to retrieve head indexes. */
881 used_idx = vq->last_used_idx & (vq->size - 1);
882 rte_prefetch0(&vq->avail->ring[used_idx]);
884 count = RTE_MIN(count, MAX_PKT_BURST);
885 count = RTE_MIN(count, free_entries);
886 LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
889 /* Retrieve all of the head indexes first to avoid caching issues. */
890 for (i = 0; i < count; i++) {
891 desc_indexes[i] = vq->avail->ring[(vq->last_used_idx + i) &
895 /* Prefetch descriptor index. */
896 rte_prefetch0(&vq->desc[desc_indexes[0]]);
897 rte_prefetch0(&vq->used->ring[vq->last_used_idx & (vq->size - 1)]);
899 for (i = 0; i < count; i++) {
902 if (likely(i + 1 < count)) {
903 rte_prefetch0(&vq->desc[desc_indexes[i + 1]]);
904 rte_prefetch0(&vq->used->ring[(used_idx + 1) &
908 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
909 if (unlikely(pkts[i] == NULL)) {
910 RTE_LOG(ERR, VHOST_DATA,
911 "Failed to allocate memory for mbuf.\n");
914 err = copy_desc_to_mbuf(dev, vq, pkts[i], desc_indexes[i],
917 rte_pktmbuf_free(pkts[i]);
921 used_idx = vq->last_used_idx++ & (vq->size - 1);
922 vq->used->ring[used_idx].id = desc_indexes[i];
923 vq->used->ring[used_idx].len = 0;
924 vhost_log_used_vring(dev, vq,
925 offsetof(struct vring_used, ring[used_idx]),
926 sizeof(vq->used->ring[used_idx]));
932 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
933 sizeof(vq->used->idx));
935 /* Kick guest if required. */
936 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
937 && (vq->callfd >= 0))
938 eventfd_write(vq->callfd, (eventfd_t)1);
941 if (unlikely(rarp_mbuf != NULL)) {
943 * Inject it to the head of "pkts" array, so that switch's mac
944 * learning table will get updated first.
946 memmove(&pkts[1], pkts, i * sizeof(struct rte_mbuf *));