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 vq->shadow_used_idx = 0;
540 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
541 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
543 if (unlikely(reserve_avail_buf_mergeable(vq, pkt_len, buf_vec,
544 &num_buffers) < 0)) {
545 LOG_DEBUG(VHOST_DATA,
546 "(%d) failed to get enough desc from vring\n",
548 vq->shadow_used_idx -= num_buffers;
552 LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
553 dev->vid, vq->last_avail_idx,
554 vq->last_avail_idx + num_buffers);
556 if (copy_mbuf_to_desc_mergeable(dev, pkts[pkt_idx],
557 buf_vec, num_buffers) < 0) {
558 vq->shadow_used_idx -= num_buffers;
562 vq->last_avail_idx += num_buffers;
565 if (likely(vq->shadow_used_idx)) {
566 flush_shadow_used_ring(dev, vq);
568 /* flush used->idx update before we read avail->flags. */
571 /* Kick the guest if necessary. */
572 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
573 && (vq->callfd >= 0))
574 eventfd_write(vq->callfd, (eventfd_t)1);
581 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
582 struct rte_mbuf **pkts, uint16_t count)
584 struct virtio_net *dev = get_device(vid);
589 if (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF))
590 return virtio_dev_merge_rx(dev, queue_id, pkts, count);
592 return virtio_dev_rx(dev, queue_id, pkts, count);
596 virtio_net_with_host_offload(struct virtio_net *dev)
599 (VIRTIO_NET_F_CSUM | VIRTIO_NET_F_HOST_ECN |
600 VIRTIO_NET_F_HOST_TSO4 | VIRTIO_NET_F_HOST_TSO6 |
601 VIRTIO_NET_F_HOST_UFO))
608 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
610 struct ipv4_hdr *ipv4_hdr;
611 struct ipv6_hdr *ipv6_hdr;
613 struct ether_hdr *eth_hdr;
616 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
618 m->l2_len = sizeof(struct ether_hdr);
619 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
621 if (ethertype == ETHER_TYPE_VLAN) {
622 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
624 m->l2_len += sizeof(struct vlan_hdr);
625 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
628 l3_hdr = (char *)eth_hdr + m->l2_len;
631 case ETHER_TYPE_IPv4:
632 ipv4_hdr = (struct ipv4_hdr *)l3_hdr;
633 *l4_proto = ipv4_hdr->next_proto_id;
634 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
635 *l4_hdr = (char *)l3_hdr + m->l3_len;
636 m->ol_flags |= PKT_TX_IPV4;
638 case ETHER_TYPE_IPv6:
639 ipv6_hdr = (struct ipv6_hdr *)l3_hdr;
640 *l4_proto = ipv6_hdr->proto;
641 m->l3_len = sizeof(struct ipv6_hdr);
642 *l4_hdr = (char *)l3_hdr + m->l3_len;
643 m->ol_flags |= PKT_TX_IPV6;
652 static inline void __attribute__((always_inline))
653 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
655 uint16_t l4_proto = 0;
657 struct tcp_hdr *tcp_hdr = NULL;
659 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
662 parse_ethernet(m, &l4_proto, &l4_hdr);
663 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
664 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
665 switch (hdr->csum_offset) {
666 case (offsetof(struct tcp_hdr, cksum)):
667 if (l4_proto == IPPROTO_TCP)
668 m->ol_flags |= PKT_TX_TCP_CKSUM;
670 case (offsetof(struct udp_hdr, dgram_cksum)):
671 if (l4_proto == IPPROTO_UDP)
672 m->ol_flags |= PKT_TX_UDP_CKSUM;
674 case (offsetof(struct sctp_hdr, cksum)):
675 if (l4_proto == IPPROTO_SCTP)
676 m->ol_flags |= PKT_TX_SCTP_CKSUM;
684 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
685 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
686 case VIRTIO_NET_HDR_GSO_TCPV4:
687 case VIRTIO_NET_HDR_GSO_TCPV6:
688 tcp_hdr = (struct tcp_hdr *)l4_hdr;
689 m->ol_flags |= PKT_TX_TCP_SEG;
690 m->tso_segsz = hdr->gso_size;
691 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
694 RTE_LOG(WARNING, VHOST_DATA,
695 "unsupported gso type %u.\n", hdr->gso_type);
701 #define RARP_PKT_SIZE 64
704 make_rarp_packet(struct rte_mbuf *rarp_mbuf, const struct ether_addr *mac)
706 struct ether_hdr *eth_hdr;
707 struct arp_hdr *rarp;
709 if (rarp_mbuf->buf_len < 64) {
710 RTE_LOG(WARNING, VHOST_DATA,
711 "failed to make RARP; mbuf size too small %u (< %d)\n",
712 rarp_mbuf->buf_len, RARP_PKT_SIZE);
716 /* Ethernet header. */
717 eth_hdr = rte_pktmbuf_mtod_offset(rarp_mbuf, struct ether_hdr *, 0);
718 memset(eth_hdr->d_addr.addr_bytes, 0xff, ETHER_ADDR_LEN);
719 ether_addr_copy(mac, ð_hdr->s_addr);
720 eth_hdr->ether_type = htons(ETHER_TYPE_RARP);
723 rarp = (struct arp_hdr *)(eth_hdr + 1);
724 rarp->arp_hrd = htons(ARP_HRD_ETHER);
725 rarp->arp_pro = htons(ETHER_TYPE_IPv4);
726 rarp->arp_hln = ETHER_ADDR_LEN;
728 rarp->arp_op = htons(ARP_OP_REVREQUEST);
730 ether_addr_copy(mac, &rarp->arp_data.arp_sha);
731 ether_addr_copy(mac, &rarp->arp_data.arp_tha);
732 memset(&rarp->arp_data.arp_sip, 0x00, 4);
733 memset(&rarp->arp_data.arp_tip, 0x00, 4);
735 rarp_mbuf->pkt_len = rarp_mbuf->data_len = RARP_PKT_SIZE;
740 static inline void __attribute__((always_inline))
741 put_zmbuf(struct zcopy_mbuf *zmbuf)
746 static inline int __attribute__((always_inline))
747 copy_desc_to_mbuf(struct virtio_net *dev, struct vring_desc *descs,
748 uint16_t max_desc, struct rte_mbuf *m, uint16_t desc_idx,
749 struct rte_mempool *mbuf_pool)
751 struct vring_desc *desc;
753 uint32_t desc_avail, desc_offset;
754 uint32_t mbuf_avail, mbuf_offset;
756 struct rte_mbuf *cur = m, *prev = m;
757 struct virtio_net_hdr *hdr = NULL;
758 /* A counter to avoid desc dead loop chain */
759 uint32_t nr_desc = 1;
761 desc = &descs[desc_idx];
762 if (unlikely((desc->len < dev->vhost_hlen)) ||
763 (desc->flags & VRING_DESC_F_INDIRECT))
766 desc_addr = gpa_to_vva(dev, desc->addr);
767 if (unlikely(!desc_addr))
770 if (virtio_net_with_host_offload(dev)) {
771 hdr = (struct virtio_net_hdr *)((uintptr_t)desc_addr);
776 * A virtio driver normally uses at least 2 desc buffers
777 * for Tx: the first for storing the header, and others
778 * for storing the data.
780 if (likely((desc->len == dev->vhost_hlen) &&
781 (desc->flags & VRING_DESC_F_NEXT) != 0)) {
782 desc = &descs[desc->next];
783 if (unlikely(desc->flags & VRING_DESC_F_INDIRECT))
786 desc_addr = gpa_to_vva(dev, desc->addr);
787 if (unlikely(!desc_addr))
791 desc_avail = desc->len;
794 desc_avail = desc->len - dev->vhost_hlen;
795 desc_offset = dev->vhost_hlen;
798 rte_prefetch0((void *)(uintptr_t)(desc_addr + desc_offset));
800 PRINT_PACKET(dev, (uintptr_t)(desc_addr + desc_offset), desc_avail, 0);
803 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
807 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
810 * A desc buf might across two host physical pages that are
811 * not continuous. In such case (gpa_to_hpa returns 0), data
812 * will be copied even though zero copy is enabled.
814 if (unlikely(dev->dequeue_zero_copy && (hpa = gpa_to_hpa(dev,
815 desc->addr + desc_offset, cpy_len)))) {
816 cur->data_len = cpy_len;
818 cur->buf_addr = (void *)(uintptr_t)desc_addr;
819 cur->buf_physaddr = hpa;
822 * In zero copy mode, one mbuf can only reference data
823 * for one or partial of one desc buff.
825 mbuf_avail = cpy_len;
827 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
829 (void *)((uintptr_t)(desc_addr + desc_offset)),
833 mbuf_avail -= cpy_len;
834 mbuf_offset += cpy_len;
835 desc_avail -= cpy_len;
836 desc_offset += cpy_len;
838 /* This desc reaches to its end, get the next one */
839 if (desc_avail == 0) {
840 if ((desc->flags & VRING_DESC_F_NEXT) == 0)
843 if (unlikely(desc->next >= max_desc ||
844 ++nr_desc > max_desc))
846 desc = &descs[desc->next];
847 if (unlikely(desc->flags & VRING_DESC_F_INDIRECT))
850 desc_addr = gpa_to_vva(dev, desc->addr);
851 if (unlikely(!desc_addr))
854 rte_prefetch0((void *)(uintptr_t)desc_addr);
857 desc_avail = desc->len;
859 PRINT_PACKET(dev, (uintptr_t)desc_addr, desc->len, 0);
863 * This mbuf reaches to its end, get a new one
866 if (mbuf_avail == 0) {
867 cur = rte_pktmbuf_alloc(mbuf_pool);
868 if (unlikely(cur == NULL)) {
869 RTE_LOG(ERR, VHOST_DATA, "Failed to "
870 "allocate memory for mbuf.\n");
875 prev->data_len = mbuf_offset;
877 m->pkt_len += mbuf_offset;
881 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
885 prev->data_len = mbuf_offset;
886 m->pkt_len += mbuf_offset;
889 vhost_dequeue_offload(hdr, m);
894 static inline void __attribute__((always_inline))
895 update_used_ring(struct virtio_net *dev, struct vhost_virtqueue *vq,
896 uint32_t used_idx, uint32_t desc_idx)
898 vq->used->ring[used_idx].id = desc_idx;
899 vq->used->ring[used_idx].len = 0;
900 vhost_log_used_vring(dev, vq,
901 offsetof(struct vring_used, ring[used_idx]),
902 sizeof(vq->used->ring[used_idx]));
905 static inline void __attribute__((always_inline))
906 update_used_idx(struct virtio_net *dev, struct vhost_virtqueue *vq,
909 if (unlikely(count == 0))
915 vq->used->idx += count;
916 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
917 sizeof(vq->used->idx));
919 /* Kick guest if required. */
920 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
921 && (vq->callfd >= 0))
922 eventfd_write(vq->callfd, (eventfd_t)1);
925 static inline struct zcopy_mbuf *__attribute__((always_inline))
926 get_zmbuf(struct vhost_virtqueue *vq)
932 /* search [last_zmbuf_idx, zmbuf_size) */
933 i = vq->last_zmbuf_idx;
934 last = vq->zmbuf_size;
937 for (; i < last; i++) {
938 if (vq->zmbufs[i].in_use == 0) {
939 vq->last_zmbuf_idx = i + 1;
940 vq->zmbufs[i].in_use = 1;
941 return &vq->zmbufs[i];
947 /* search [0, last_zmbuf_idx) */
949 last = vq->last_zmbuf_idx;
956 static inline bool __attribute__((always_inline))
957 mbuf_is_consumed(struct rte_mbuf *m)
960 if (rte_mbuf_refcnt_read(m) > 1)
969 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
970 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
972 struct virtio_net *dev;
973 struct rte_mbuf *rarp_mbuf = NULL;
974 struct vhost_virtqueue *vq;
975 uint32_t desc_indexes[MAX_PKT_BURST];
978 uint16_t free_entries;
981 dev = get_device(vid);
985 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->virt_qp_nb))) {
986 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
987 dev->vid, __func__, queue_id);
991 vq = dev->virtqueue[queue_id];
992 if (unlikely(vq->enabled == 0))
995 if (unlikely(dev->dequeue_zero_copy)) {
996 struct zcopy_mbuf *zmbuf, *next;
999 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1000 zmbuf != NULL; zmbuf = next) {
1001 next = TAILQ_NEXT(zmbuf, next);
1003 if (mbuf_is_consumed(zmbuf->mbuf)) {
1004 used_idx = vq->last_used_idx++ & (vq->size - 1);
1005 update_used_ring(dev, vq, used_idx,
1009 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1010 rte_pktmbuf_free(zmbuf->mbuf);
1016 update_used_idx(dev, vq, nr_updated);
1020 * Construct a RARP broadcast packet, and inject it to the "pkts"
1021 * array, to looks like that guest actually send such packet.
1023 * Check user_send_rarp() for more information.
1025 if (unlikely(rte_atomic16_cmpset((volatile uint16_t *)
1026 &dev->broadcast_rarp.cnt, 1, 0))) {
1027 rarp_mbuf = rte_pktmbuf_alloc(mbuf_pool);
1028 if (rarp_mbuf == NULL) {
1029 RTE_LOG(ERR, VHOST_DATA,
1030 "Failed to allocate memory for mbuf.\n");
1034 if (make_rarp_packet(rarp_mbuf, &dev->mac)) {
1035 rte_pktmbuf_free(rarp_mbuf);
1042 free_entries = *((volatile uint16_t *)&vq->avail->idx) -
1044 if (free_entries == 0)
1047 LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1049 /* Prefetch available and used ring */
1050 avail_idx = vq->last_avail_idx & (vq->size - 1);
1051 used_idx = vq->last_used_idx & (vq->size - 1);
1052 rte_prefetch0(&vq->avail->ring[avail_idx]);
1053 rte_prefetch0(&vq->used->ring[used_idx]);
1055 count = RTE_MIN(count, MAX_PKT_BURST);
1056 count = RTE_MIN(count, free_entries);
1057 LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1060 /* Retrieve all of the head indexes first to avoid caching issues. */
1061 for (i = 0; i < count; i++) {
1062 avail_idx = (vq->last_avail_idx + i) & (vq->size - 1);
1063 used_idx = (vq->last_used_idx + i) & (vq->size - 1);
1064 desc_indexes[i] = vq->avail->ring[avail_idx];
1066 if (likely(dev->dequeue_zero_copy == 0))
1067 update_used_ring(dev, vq, used_idx, desc_indexes[i]);
1070 /* Prefetch descriptor index. */
1071 rte_prefetch0(&vq->desc[desc_indexes[0]]);
1072 for (i = 0; i < count; i++) {
1073 struct vring_desc *desc;
1077 if (likely(i + 1 < count))
1078 rte_prefetch0(&vq->desc[desc_indexes[i + 1]]);
1080 if (vq->desc[desc_indexes[i]].flags & VRING_DESC_F_INDIRECT) {
1081 desc = (struct vring_desc *)(uintptr_t)gpa_to_vva(dev,
1082 vq->desc[desc_indexes[i]].addr);
1083 if (unlikely(!desc))
1086 rte_prefetch0(desc);
1087 sz = vq->desc[desc_indexes[i]].len / sizeof(*desc);
1092 idx = desc_indexes[i];
1095 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
1096 if (unlikely(pkts[i] == NULL)) {
1097 RTE_LOG(ERR, VHOST_DATA,
1098 "Failed to allocate memory for mbuf.\n");
1102 err = copy_desc_to_mbuf(dev, desc, sz, pkts[i], idx, mbuf_pool);
1103 if (unlikely(err)) {
1104 rte_pktmbuf_free(pkts[i]);
1108 if (unlikely(dev->dequeue_zero_copy)) {
1109 struct zcopy_mbuf *zmbuf;
1111 zmbuf = get_zmbuf(vq);
1113 rte_pktmbuf_free(pkts[i]);
1116 zmbuf->mbuf = pkts[i];
1117 zmbuf->desc_idx = desc_indexes[i];
1120 * Pin lock the mbuf; we will check later to see
1121 * whether the mbuf is freed (when we are the last
1122 * user) or not. If that's the case, we then could
1123 * update the used ring safely.
1125 rte_mbuf_refcnt_update(pkts[i], 1);
1128 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1131 vq->last_avail_idx += i;
1133 if (likely(dev->dequeue_zero_copy == 0)) {
1134 vq->last_used_idx += i;
1135 update_used_idx(dev, vq, i);
1139 if (unlikely(rarp_mbuf != NULL)) {
1141 * Inject it to the head of "pkts" array, so that switch's mac
1142 * learning table will get updated first.
1144 memmove(&pkts[1], pkts, i * sizeof(struct rte_mbuf *));
1145 pkts[0] = rarp_mbuf;