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;
95 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
97 if (m_buf->ol_flags & PKT_TX_L4_MASK) {
98 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
99 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
101 switch (m_buf->ol_flags & PKT_TX_L4_MASK) {
102 case PKT_TX_TCP_CKSUM:
103 net_hdr->csum_offset = (offsetof(struct tcp_hdr,
106 case PKT_TX_UDP_CKSUM:
107 net_hdr->csum_offset = (offsetof(struct udp_hdr,
110 case PKT_TX_SCTP_CKSUM:
111 net_hdr->csum_offset = (offsetof(struct sctp_hdr,
117 if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
118 if (m_buf->ol_flags & PKT_TX_IPV4)
119 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
121 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
122 net_hdr->gso_size = m_buf->tso_segsz;
123 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
129 copy_virtio_net_hdr(struct virtio_net *dev, uint64_t desc_addr,
130 struct virtio_net_hdr_mrg_rxbuf hdr)
132 if (dev->vhost_hlen == sizeof(struct virtio_net_hdr_mrg_rxbuf))
133 *(struct virtio_net_hdr_mrg_rxbuf *)(uintptr_t)desc_addr = hdr;
135 *(struct virtio_net_hdr *)(uintptr_t)desc_addr = hdr.hdr;
138 static inline int __attribute__((always_inline))
139 copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,
140 struct rte_mbuf *m, uint16_t desc_idx)
142 uint32_t desc_avail, desc_offset;
143 uint32_t mbuf_avail, mbuf_offset;
145 struct vring_desc *desc;
147 struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0, 0, 0, 0, 0, 0}, 0};
149 desc = &vq->desc[desc_idx];
150 desc_addr = gpa_to_vva(dev, desc->addr);
152 * Checking of 'desc_addr' placed outside of 'unlikely' macro to avoid
153 * performance issue with some versions of gcc (4.8.4 and 5.3.0) which
154 * otherwise stores offset on the stack instead of in a register.
156 if (unlikely(desc->len < dev->vhost_hlen) || !desc_addr)
159 rte_prefetch0((void *)(uintptr_t)desc_addr);
161 virtio_enqueue_offload(m, &virtio_hdr.hdr);
162 copy_virtio_net_hdr(dev, desc_addr, virtio_hdr);
163 vhost_log_write(dev, desc->addr, dev->vhost_hlen);
164 PRINT_PACKET(dev, (uintptr_t)desc_addr, dev->vhost_hlen, 0);
166 desc_offset = dev->vhost_hlen;
167 desc_avail = desc->len - dev->vhost_hlen;
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 */
186 if (unlikely(desc->next >= vq->size))
189 desc = &vq->desc[desc->next];
190 desc_addr = gpa_to_vva(dev, desc->addr);
191 if (unlikely(!desc_addr))
195 desc_avail = desc->len;
198 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
199 rte_memcpy((void *)((uintptr_t)(desc_addr + desc_offset)),
200 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
202 vhost_log_write(dev, desc->addr + desc_offset, cpy_len);
203 PRINT_PACKET(dev, (uintptr_t)(desc_addr + desc_offset),
206 mbuf_avail -= cpy_len;
207 mbuf_offset += cpy_len;
208 desc_avail -= cpy_len;
209 desc_offset += cpy_len;
216 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
217 * be received from the physical port or from another virtio device. A packet
218 * count is returned to indicate the number of packets that are succesfully
219 * added to the RX queue. This function works when the mbuf is scattered, but
220 * it doesn't support the mergeable feature.
222 static inline uint32_t __attribute__((always_inline))
223 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
224 struct rte_mbuf **pkts, uint32_t count)
226 struct vhost_virtqueue *vq;
227 uint16_t avail_idx, free_entries, start_idx;
228 uint16_t desc_indexes[MAX_PKT_BURST];
232 LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
233 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->virt_qp_nb))) {
234 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
235 dev->vid, __func__, queue_id);
239 vq = dev->virtqueue[queue_id];
240 if (unlikely(vq->enabled == 0))
243 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
244 start_idx = vq->last_used_idx;
245 free_entries = avail_idx - start_idx;
246 count = RTE_MIN(count, free_entries);
247 count = RTE_MIN(count, (uint32_t)MAX_PKT_BURST);
251 LOG_DEBUG(VHOST_DATA, "(%d) start_idx %d | end_idx %d\n",
252 dev->vid, start_idx, start_idx + count);
254 /* Retrieve all of the desc indexes first to avoid caching issues. */
255 rte_prefetch0(&vq->avail->ring[start_idx & (vq->size - 1)]);
256 for (i = 0; i < count; i++) {
257 used_idx = (start_idx + i) & (vq->size - 1);
258 desc_indexes[i] = vq->avail->ring[used_idx];
259 vq->used->ring[used_idx].id = desc_indexes[i];
260 vq->used->ring[used_idx].len = pkts[i]->pkt_len +
262 vhost_log_used_vring(dev, vq,
263 offsetof(struct vring_used, ring[used_idx]),
264 sizeof(vq->used->ring[used_idx]));
267 rte_prefetch0(&vq->desc[desc_indexes[0]]);
268 for (i = 0; i < count; i++) {
269 uint16_t desc_idx = desc_indexes[i];
272 err = copy_mbuf_to_desc(dev, vq, pkts[i], desc_idx);
274 used_idx = (start_idx + i) & (vq->size - 1);
275 vq->used->ring[used_idx].len = dev->vhost_hlen;
276 vhost_log_used_vring(dev, vq,
277 offsetof(struct vring_used, ring[used_idx]),
278 sizeof(vq->used->ring[used_idx]));
282 rte_prefetch0(&vq->desc[desc_indexes[i+1]]);
287 *(volatile uint16_t *)&vq->used->idx += count;
288 vq->last_used_idx += count;
289 vhost_log_used_vring(dev, vq,
290 offsetof(struct vring_used, idx),
291 sizeof(vq->used->idx));
293 /* flush used->idx update before we read avail->flags. */
296 /* Kick the guest if necessary. */
297 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
298 && (vq->callfd >= 0))
299 eventfd_write(vq->callfd, (eventfd_t)1);
304 fill_vec_buf(struct vhost_virtqueue *vq, uint32_t avail_idx,
305 uint32_t *allocated, uint32_t *vec_idx,
306 struct buf_vector *buf_vec)
308 uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
309 uint32_t vec_id = *vec_idx;
310 uint32_t len = *allocated;
313 if (unlikely(vec_id >= BUF_VECTOR_MAX || idx >= vq->size))
316 len += vq->desc[idx].len;
317 buf_vec[vec_id].buf_addr = vq->desc[idx].addr;
318 buf_vec[vec_id].buf_len = vq->desc[idx].len;
319 buf_vec[vec_id].desc_idx = idx;
322 if ((vq->desc[idx].flags & VRING_DESC_F_NEXT) == 0)
325 idx = vq->desc[idx].next;
335 * Returns -1 on fail, 0 on success
338 reserve_avail_buf_mergeable(struct vhost_virtqueue *vq, uint32_t size,
339 uint16_t *end, struct buf_vector *buf_vec)
343 uint32_t allocated = 0;
344 uint32_t vec_idx = 0;
347 cur_idx = vq->last_used_idx;
350 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
351 if (unlikely(cur_idx == avail_idx))
354 if (unlikely(fill_vec_buf(vq, cur_idx, &allocated,
355 &vec_idx, buf_vec) < 0))
361 if (allocated >= size)
365 * if we tried all available ring items, and still
366 * can't get enough buf, it means something abnormal
369 if (unlikely(tries >= vq->size))
377 static inline uint32_t __attribute__((always_inline))
378 copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,
379 uint16_t end_idx, struct rte_mbuf *m,
380 struct buf_vector *buf_vec)
382 struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0, 0, 0, 0, 0, 0}, 0};
383 uint32_t vec_idx = 0;
384 uint16_t start_idx = vq->last_used_idx;
385 uint16_t cur_idx = start_idx;
387 uint32_t desc_chain_head;
388 uint32_t desc_chain_len;
389 uint32_t mbuf_offset, mbuf_avail;
390 uint32_t desc_offset, desc_avail;
392 uint16_t desc_idx, used_idx;
394 if (unlikely(m == NULL))
397 LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
398 dev->vid, cur_idx, end_idx);
400 desc_addr = gpa_to_vva(dev, buf_vec[vec_idx].buf_addr);
401 if (buf_vec[vec_idx].buf_len < dev->vhost_hlen || !desc_addr)
404 rte_prefetch0((void *)(uintptr_t)desc_addr);
406 virtio_hdr.num_buffers = end_idx - start_idx;
407 LOG_DEBUG(VHOST_DATA, "(%d) RX: num merge buffers %d\n",
408 dev->vid, virtio_hdr.num_buffers);
410 virtio_enqueue_offload(m, &virtio_hdr.hdr);
411 copy_virtio_net_hdr(dev, desc_addr, virtio_hdr);
412 vhost_log_write(dev, buf_vec[vec_idx].buf_addr, dev->vhost_hlen);
413 PRINT_PACKET(dev, (uintptr_t)desc_addr, dev->vhost_hlen, 0);
415 desc_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
416 desc_offset = dev->vhost_hlen;
417 desc_chain_head = buf_vec[vec_idx].desc_idx;
418 desc_chain_len = desc_offset;
420 mbuf_avail = rte_pktmbuf_data_len(m);
422 while (mbuf_avail != 0 || m->next != NULL) {
423 /* done with current desc buf, get the next one */
424 if (desc_avail == 0) {
425 desc_idx = buf_vec[vec_idx].desc_idx;
428 if (!(vq->desc[desc_idx].flags & VRING_DESC_F_NEXT)) {
429 /* Update used ring with desc information */
430 used_idx = cur_idx++ & (vq->size - 1);
431 vq->used->ring[used_idx].id = desc_chain_head;
432 vq->used->ring[used_idx].len = desc_chain_len;
433 vhost_log_used_vring(dev, vq,
434 offsetof(struct vring_used,
436 sizeof(vq->used->ring[used_idx]));
437 desc_chain_head = buf_vec[vec_idx].desc_idx;
441 desc_addr = gpa_to_vva(dev, buf_vec[vec_idx].buf_addr);
442 if (unlikely(!desc_addr))
445 /* Prefetch buffer address. */
446 rte_prefetch0((void *)(uintptr_t)desc_addr);
448 desc_avail = buf_vec[vec_idx].buf_len;
451 /* done with current mbuf, get the next one */
452 if (mbuf_avail == 0) {
456 mbuf_avail = rte_pktmbuf_data_len(m);
459 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
460 rte_memcpy((void *)((uintptr_t)(desc_addr + desc_offset)),
461 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
463 vhost_log_write(dev, buf_vec[vec_idx].buf_addr + desc_offset,
465 PRINT_PACKET(dev, (uintptr_t)(desc_addr + desc_offset),
468 mbuf_avail -= cpy_len;
469 mbuf_offset += cpy_len;
470 desc_avail -= cpy_len;
471 desc_offset += cpy_len;
472 desc_chain_len += cpy_len;
475 used_idx = cur_idx & (vq->size - 1);
476 vq->used->ring[used_idx].id = desc_chain_head;
477 vq->used->ring[used_idx].len = desc_chain_len;
478 vhost_log_used_vring(dev, vq,
479 offsetof(struct vring_used, ring[used_idx]),
480 sizeof(vq->used->ring[used_idx]));
482 return end_idx - start_idx;
485 static inline uint32_t __attribute__((always_inline))
486 virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,
487 struct rte_mbuf **pkts, uint32_t count)
489 struct vhost_virtqueue *vq;
490 uint32_t pkt_idx = 0, nr_used = 0;
492 struct buf_vector buf_vec[BUF_VECTOR_MAX];
494 LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
495 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->virt_qp_nb))) {
496 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
497 dev->vid, __func__, queue_id);
501 vq = dev->virtqueue[queue_id];
502 if (unlikely(vq->enabled == 0))
505 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
509 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
510 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
512 if (unlikely(reserve_avail_buf_mergeable(vq, pkt_len,
513 &end, buf_vec) < 0)) {
514 LOG_DEBUG(VHOST_DATA,
515 "(%d) failed to get enough desc from vring\n",
520 nr_used = copy_mbuf_to_desc_mergeable(dev, vq, end,
521 pkts[pkt_idx], buf_vec);
524 *(volatile uint16_t *)&vq->used->idx += nr_used;
525 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
526 sizeof(vq->used->idx));
527 vq->last_used_idx += nr_used;
530 if (likely(pkt_idx)) {
531 /* flush used->idx update before we read avail->flags. */
534 /* Kick the guest if necessary. */
535 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
536 && (vq->callfd >= 0))
537 eventfd_write(vq->callfd, (eventfd_t)1);
544 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
545 struct rte_mbuf **pkts, uint16_t count)
547 struct virtio_net *dev = get_device(vid);
552 if (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF))
553 return virtio_dev_merge_rx(dev, queue_id, pkts, count);
555 return virtio_dev_rx(dev, queue_id, pkts, count);
559 virtio_net_with_host_offload(struct virtio_net *dev)
562 (VIRTIO_NET_F_CSUM | VIRTIO_NET_F_HOST_ECN |
563 VIRTIO_NET_F_HOST_TSO4 | VIRTIO_NET_F_HOST_TSO6 |
564 VIRTIO_NET_F_HOST_UFO))
571 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
573 struct ipv4_hdr *ipv4_hdr;
574 struct ipv6_hdr *ipv6_hdr;
576 struct ether_hdr *eth_hdr;
579 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
581 m->l2_len = sizeof(struct ether_hdr);
582 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
584 if (ethertype == ETHER_TYPE_VLAN) {
585 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
587 m->l2_len += sizeof(struct vlan_hdr);
588 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
591 l3_hdr = (char *)eth_hdr + m->l2_len;
594 case ETHER_TYPE_IPv4:
595 ipv4_hdr = (struct ipv4_hdr *)l3_hdr;
596 *l4_proto = ipv4_hdr->next_proto_id;
597 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
598 *l4_hdr = (char *)l3_hdr + m->l3_len;
599 m->ol_flags |= PKT_TX_IPV4;
601 case ETHER_TYPE_IPv6:
602 ipv6_hdr = (struct ipv6_hdr *)l3_hdr;
603 *l4_proto = ipv6_hdr->proto;
604 m->l3_len = sizeof(struct ipv6_hdr);
605 *l4_hdr = (char *)l3_hdr + m->l3_len;
606 m->ol_flags |= PKT_TX_IPV6;
615 static inline void __attribute__((always_inline))
616 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
618 uint16_t l4_proto = 0;
620 struct tcp_hdr *tcp_hdr = NULL;
622 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
625 parse_ethernet(m, &l4_proto, &l4_hdr);
626 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
627 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
628 switch (hdr->csum_offset) {
629 case (offsetof(struct tcp_hdr, cksum)):
630 if (l4_proto == IPPROTO_TCP)
631 m->ol_flags |= PKT_TX_TCP_CKSUM;
633 case (offsetof(struct udp_hdr, dgram_cksum)):
634 if (l4_proto == IPPROTO_UDP)
635 m->ol_flags |= PKT_TX_UDP_CKSUM;
637 case (offsetof(struct sctp_hdr, cksum)):
638 if (l4_proto == IPPROTO_SCTP)
639 m->ol_flags |= PKT_TX_SCTP_CKSUM;
647 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
648 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
649 case VIRTIO_NET_HDR_GSO_TCPV4:
650 case VIRTIO_NET_HDR_GSO_TCPV6:
651 tcp_hdr = (struct tcp_hdr *)l4_hdr;
652 m->ol_flags |= PKT_TX_TCP_SEG;
653 m->tso_segsz = hdr->gso_size;
654 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
657 RTE_LOG(WARNING, VHOST_DATA,
658 "unsupported gso type %u.\n", hdr->gso_type);
664 #define RARP_PKT_SIZE 64
667 make_rarp_packet(struct rte_mbuf *rarp_mbuf, const struct ether_addr *mac)
669 struct ether_hdr *eth_hdr;
670 struct arp_hdr *rarp;
672 if (rarp_mbuf->buf_len < 64) {
673 RTE_LOG(WARNING, VHOST_DATA,
674 "failed to make RARP; mbuf size too small %u (< %d)\n",
675 rarp_mbuf->buf_len, RARP_PKT_SIZE);
679 /* Ethernet header. */
680 eth_hdr = rte_pktmbuf_mtod_offset(rarp_mbuf, struct ether_hdr *, 0);
681 memset(eth_hdr->d_addr.addr_bytes, 0xff, ETHER_ADDR_LEN);
682 ether_addr_copy(mac, ð_hdr->s_addr);
683 eth_hdr->ether_type = htons(ETHER_TYPE_RARP);
686 rarp = (struct arp_hdr *)(eth_hdr + 1);
687 rarp->arp_hrd = htons(ARP_HRD_ETHER);
688 rarp->arp_pro = htons(ETHER_TYPE_IPv4);
689 rarp->arp_hln = ETHER_ADDR_LEN;
691 rarp->arp_op = htons(ARP_OP_REVREQUEST);
693 ether_addr_copy(mac, &rarp->arp_data.arp_sha);
694 ether_addr_copy(mac, &rarp->arp_data.arp_tha);
695 memset(&rarp->arp_data.arp_sip, 0x00, 4);
696 memset(&rarp->arp_data.arp_tip, 0x00, 4);
698 rarp_mbuf->pkt_len = rarp_mbuf->data_len = RARP_PKT_SIZE;
703 static inline void __attribute__((always_inline))
704 put_zmbuf(struct zcopy_mbuf *zmbuf)
709 static inline int __attribute__((always_inline))
710 copy_desc_to_mbuf(struct virtio_net *dev, struct vring_desc *descs,
711 uint16_t max_desc, struct rte_mbuf *m, uint16_t desc_idx,
712 struct rte_mempool *mbuf_pool)
714 struct vring_desc *desc;
716 uint32_t desc_avail, desc_offset;
717 uint32_t mbuf_avail, mbuf_offset;
719 struct rte_mbuf *cur = m, *prev = m;
720 struct virtio_net_hdr *hdr = NULL;
721 /* A counter to avoid desc dead loop chain */
722 uint32_t nr_desc = 1;
724 desc = &descs[desc_idx];
725 if (unlikely((desc->len < dev->vhost_hlen)) ||
726 (desc->flags & VRING_DESC_F_INDIRECT))
729 desc_addr = gpa_to_vva(dev, desc->addr);
730 if (unlikely(!desc_addr))
733 if (virtio_net_with_host_offload(dev)) {
734 hdr = (struct virtio_net_hdr *)((uintptr_t)desc_addr);
739 * A virtio driver normally uses at least 2 desc buffers
740 * for Tx: the first for storing the header, and others
741 * for storing the data.
743 if (likely((desc->len == dev->vhost_hlen) &&
744 (desc->flags & VRING_DESC_F_NEXT) != 0)) {
745 desc = &descs[desc->next];
746 if (unlikely(desc->flags & VRING_DESC_F_INDIRECT))
749 desc_addr = gpa_to_vva(dev, desc->addr);
750 if (unlikely(!desc_addr))
754 desc_avail = desc->len;
757 desc_avail = desc->len - dev->vhost_hlen;
758 desc_offset = dev->vhost_hlen;
761 rte_prefetch0((void *)(uintptr_t)(desc_addr + desc_offset));
763 PRINT_PACKET(dev, (uintptr_t)(desc_addr + desc_offset), desc_avail, 0);
766 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
770 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
773 * A desc buf might across two host physical pages that are
774 * not continuous. In such case (gpa_to_hpa returns 0), data
775 * will be copied even though zero copy is enabled.
777 if (unlikely(dev->dequeue_zero_copy && (hpa = gpa_to_hpa(dev,
778 desc->addr + desc_offset, cpy_len)))) {
779 cur->data_len = cpy_len;
781 cur->buf_addr = (void *)(uintptr_t)desc_addr;
782 cur->buf_physaddr = hpa;
785 * In zero copy mode, one mbuf can only reference data
786 * for one or partial of one desc buff.
788 mbuf_avail = cpy_len;
790 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
792 (void *)((uintptr_t)(desc_addr + desc_offset)),
796 mbuf_avail -= cpy_len;
797 mbuf_offset += cpy_len;
798 desc_avail -= cpy_len;
799 desc_offset += cpy_len;
801 /* This desc reaches to its end, get the next one */
802 if (desc_avail == 0) {
803 if ((desc->flags & VRING_DESC_F_NEXT) == 0)
806 if (unlikely(desc->next >= max_desc ||
807 ++nr_desc > max_desc))
809 desc = &descs[desc->next];
810 if (unlikely(desc->flags & VRING_DESC_F_INDIRECT))
813 desc_addr = gpa_to_vva(dev, desc->addr);
814 if (unlikely(!desc_addr))
817 rte_prefetch0((void *)(uintptr_t)desc_addr);
820 desc_avail = desc->len;
822 PRINT_PACKET(dev, (uintptr_t)desc_addr, desc->len, 0);
826 * This mbuf reaches to its end, get a new one
829 if (mbuf_avail == 0) {
830 cur = rte_pktmbuf_alloc(mbuf_pool);
831 if (unlikely(cur == NULL)) {
832 RTE_LOG(ERR, VHOST_DATA, "Failed to "
833 "allocate memory for mbuf.\n");
838 prev->data_len = mbuf_offset;
840 m->pkt_len += mbuf_offset;
844 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
848 prev->data_len = mbuf_offset;
849 m->pkt_len += mbuf_offset;
852 vhost_dequeue_offload(hdr, m);
857 static inline void __attribute__((always_inline))
858 update_used_ring(struct virtio_net *dev, struct vhost_virtqueue *vq,
859 uint32_t used_idx, uint32_t desc_idx)
861 vq->used->ring[used_idx].id = desc_idx;
862 vq->used->ring[used_idx].len = 0;
863 vhost_log_used_vring(dev, vq,
864 offsetof(struct vring_used, ring[used_idx]),
865 sizeof(vq->used->ring[used_idx]));
868 static inline void __attribute__((always_inline))
869 update_used_idx(struct virtio_net *dev, struct vhost_virtqueue *vq,
872 if (unlikely(count == 0))
878 vq->used->idx += count;
879 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
880 sizeof(vq->used->idx));
882 /* Kick guest if required. */
883 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
884 && (vq->callfd >= 0))
885 eventfd_write(vq->callfd, (eventfd_t)1);
888 static inline struct zcopy_mbuf *__attribute__((always_inline))
889 get_zmbuf(struct vhost_virtqueue *vq)
895 /* search [last_zmbuf_idx, zmbuf_size) */
896 i = vq->last_zmbuf_idx;
897 last = vq->zmbuf_size;
900 for (; i < last; i++) {
901 if (vq->zmbufs[i].in_use == 0) {
902 vq->last_zmbuf_idx = i + 1;
903 vq->zmbufs[i].in_use = 1;
904 return &vq->zmbufs[i];
910 /* search [0, last_zmbuf_idx) */
912 last = vq->last_zmbuf_idx;
919 static inline bool __attribute__((always_inline))
920 mbuf_is_consumed(struct rte_mbuf *m)
923 if (rte_mbuf_refcnt_read(m) > 1)
932 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
933 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
935 struct virtio_net *dev;
936 struct rte_mbuf *rarp_mbuf = NULL;
937 struct vhost_virtqueue *vq;
938 uint32_t desc_indexes[MAX_PKT_BURST];
941 uint16_t free_entries;
944 dev = get_device(vid);
948 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->virt_qp_nb))) {
949 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
950 dev->vid, __func__, queue_id);
954 vq = dev->virtqueue[queue_id];
955 if (unlikely(vq->enabled == 0))
958 if (unlikely(dev->dequeue_zero_copy)) {
959 struct zcopy_mbuf *zmbuf, *next;
962 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
963 zmbuf != NULL; zmbuf = next) {
964 next = TAILQ_NEXT(zmbuf, next);
966 if (mbuf_is_consumed(zmbuf->mbuf)) {
967 used_idx = vq->last_used_idx++ & (vq->size - 1);
968 update_used_ring(dev, vq, used_idx,
972 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
973 rte_pktmbuf_free(zmbuf->mbuf);
979 update_used_idx(dev, vq, nr_updated);
983 * Construct a RARP broadcast packet, and inject it to the "pkts"
984 * array, to looks like that guest actually send such packet.
986 * Check user_send_rarp() for more information.
988 if (unlikely(rte_atomic16_cmpset((volatile uint16_t *)
989 &dev->broadcast_rarp.cnt, 1, 0))) {
990 rarp_mbuf = rte_pktmbuf_alloc(mbuf_pool);
991 if (rarp_mbuf == NULL) {
992 RTE_LOG(ERR, VHOST_DATA,
993 "Failed to allocate memory for mbuf.\n");
997 if (make_rarp_packet(rarp_mbuf, &dev->mac)) {
998 rte_pktmbuf_free(rarp_mbuf);
1005 free_entries = *((volatile uint16_t *)&vq->avail->idx) -
1007 if (free_entries == 0)
1010 LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1012 /* Prefetch available and used ring */
1013 avail_idx = vq->last_avail_idx & (vq->size - 1);
1014 used_idx = vq->last_used_idx & (vq->size - 1);
1015 rte_prefetch0(&vq->avail->ring[avail_idx]);
1016 rte_prefetch0(&vq->used->ring[used_idx]);
1018 count = RTE_MIN(count, MAX_PKT_BURST);
1019 count = RTE_MIN(count, free_entries);
1020 LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1023 /* Retrieve all of the head indexes first to avoid caching issues. */
1024 for (i = 0; i < count; i++) {
1025 avail_idx = (vq->last_avail_idx + i) & (vq->size - 1);
1026 used_idx = (vq->last_used_idx + i) & (vq->size - 1);
1027 desc_indexes[i] = vq->avail->ring[avail_idx];
1029 if (likely(dev->dequeue_zero_copy == 0))
1030 update_used_ring(dev, vq, used_idx, desc_indexes[i]);
1033 /* Prefetch descriptor index. */
1034 rte_prefetch0(&vq->desc[desc_indexes[0]]);
1035 for (i = 0; i < count; i++) {
1036 struct vring_desc *desc;
1040 if (likely(i + 1 < count))
1041 rte_prefetch0(&vq->desc[desc_indexes[i + 1]]);
1043 if (vq->desc[desc_indexes[i]].flags & VRING_DESC_F_INDIRECT) {
1044 desc = (struct vring_desc *)(uintptr_t)gpa_to_vva(dev,
1045 vq->desc[desc_indexes[i]].addr);
1046 if (unlikely(!desc))
1049 rte_prefetch0(desc);
1050 sz = vq->desc[desc_indexes[i]].len / sizeof(*desc);
1055 idx = desc_indexes[i];
1058 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
1059 if (unlikely(pkts[i] == NULL)) {
1060 RTE_LOG(ERR, VHOST_DATA,
1061 "Failed to allocate memory for mbuf.\n");
1065 err = copy_desc_to_mbuf(dev, desc, sz, pkts[i], idx, mbuf_pool);
1066 if (unlikely(err)) {
1067 rte_pktmbuf_free(pkts[i]);
1071 if (unlikely(dev->dequeue_zero_copy)) {
1072 struct zcopy_mbuf *zmbuf;
1074 zmbuf = get_zmbuf(vq);
1076 rte_pktmbuf_free(pkts[i]);
1079 zmbuf->mbuf = pkts[i];
1080 zmbuf->desc_idx = desc_indexes[i];
1083 * Pin lock the mbuf; we will check later to see
1084 * whether the mbuf is freed (when we are the last
1085 * user) or not. If that's the case, we then could
1086 * update the used ring safely.
1088 rte_mbuf_refcnt_update(pkts[i], 1);
1091 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1094 vq->last_avail_idx += i;
1096 if (likely(dev->dequeue_zero_copy == 0)) {
1097 vq->last_used_idx += i;
1098 update_used_idx(dev, vq, i);
1102 if (unlikely(rarp_mbuf != NULL)) {
1104 * Inject it to the head of "pkts" array, so that switch's mac
1105 * learning table will get updated first.
1107 memmove(&pkts[1], pkts, i * sizeof(struct rte_mbuf *));
1108 pkts[0] = rarp_mbuf;