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 vring_desc *descs,
190 struct rte_mbuf *m, uint16_t desc_idx, uint32_t size)
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};
198 /* A counter to avoid desc dead loop chain */
199 uint16_t nr_desc = 1;
201 desc = &descs[desc_idx];
202 desc_addr = gpa_to_vva(dev, desc->addr);
204 * Checking of 'desc_addr' placed outside of 'unlikely' macro to avoid
205 * performance issue with some versions of gcc (4.8.4 and 5.3.0) which
206 * otherwise stores offset on the stack instead of in a register.
208 if (unlikely(desc->len < dev->vhost_hlen) || !desc_addr)
211 rte_prefetch0((void *)(uintptr_t)desc_addr);
213 virtio_enqueue_offload(m, &virtio_hdr.hdr);
214 copy_virtio_net_hdr(dev, desc_addr, virtio_hdr);
215 vhost_log_write(dev, desc->addr, dev->vhost_hlen);
216 PRINT_PACKET(dev, (uintptr_t)desc_addr, dev->vhost_hlen, 0);
218 desc_offset = dev->vhost_hlen;
219 desc_avail = desc->len - dev->vhost_hlen;
221 mbuf_avail = rte_pktmbuf_data_len(m);
223 while (mbuf_avail != 0 || m->next != NULL) {
224 /* done with current mbuf, fetch next */
225 if (mbuf_avail == 0) {
229 mbuf_avail = rte_pktmbuf_data_len(m);
232 /* done with current desc buf, fetch next */
233 if (desc_avail == 0) {
234 if ((desc->flags & VRING_DESC_F_NEXT) == 0) {
235 /* Room in vring buffer is not enough */
238 if (unlikely(desc->next >= size || ++nr_desc > size))
241 desc = &descs[desc->next];
242 desc_addr = gpa_to_vva(dev, desc->addr);
243 if (unlikely(!desc_addr))
247 desc_avail = desc->len;
250 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
251 rte_memcpy((void *)((uintptr_t)(desc_addr + desc_offset)),
252 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
254 vhost_log_write(dev, desc->addr + desc_offset, cpy_len);
255 PRINT_PACKET(dev, (uintptr_t)(desc_addr + desc_offset),
258 mbuf_avail -= cpy_len;
259 mbuf_offset += cpy_len;
260 desc_avail -= cpy_len;
261 desc_offset += cpy_len;
268 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
269 * be received from the physical port or from another virtio device. A packet
270 * count is returned to indicate the number of packets that are succesfully
271 * added to the RX queue. This function works when the mbuf is scattered, but
272 * it doesn't support the mergeable feature.
274 static inline uint32_t __attribute__((always_inline))
275 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
276 struct rte_mbuf **pkts, uint32_t count)
278 struct vhost_virtqueue *vq;
279 uint16_t avail_idx, free_entries, start_idx;
280 uint16_t desc_indexes[MAX_PKT_BURST];
281 struct vring_desc *descs;
285 LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
286 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->virt_qp_nb))) {
287 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
288 dev->vid, __func__, queue_id);
292 vq = dev->virtqueue[queue_id];
293 if (unlikely(vq->enabled == 0))
296 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
297 start_idx = vq->last_used_idx;
298 free_entries = avail_idx - start_idx;
299 count = RTE_MIN(count, free_entries);
300 count = RTE_MIN(count, (uint32_t)MAX_PKT_BURST);
304 LOG_DEBUG(VHOST_DATA, "(%d) start_idx %d | end_idx %d\n",
305 dev->vid, start_idx, start_idx + count);
307 /* Retrieve all of the desc indexes first to avoid caching issues. */
308 rte_prefetch0(&vq->avail->ring[start_idx & (vq->size - 1)]);
309 for (i = 0; i < count; i++) {
310 used_idx = (start_idx + i) & (vq->size - 1);
311 desc_indexes[i] = vq->avail->ring[used_idx];
312 vq->used->ring[used_idx].id = desc_indexes[i];
313 vq->used->ring[used_idx].len = pkts[i]->pkt_len +
315 vhost_log_used_vring(dev, vq,
316 offsetof(struct vring_used, ring[used_idx]),
317 sizeof(vq->used->ring[used_idx]));
320 rte_prefetch0(&vq->desc[desc_indexes[0]]);
321 for (i = 0; i < count; i++) {
322 uint16_t desc_idx = desc_indexes[i];
325 if (vq->desc[desc_idx].flags & VRING_DESC_F_INDIRECT) {
326 descs = (struct vring_desc *)(uintptr_t)gpa_to_vva(dev,
327 vq->desc[desc_idx].addr);
328 if (unlikely(!descs)) {
334 sz = vq->desc[desc_idx].len / sizeof(*descs);
340 err = copy_mbuf_to_desc(dev, descs, pkts[i], desc_idx, sz);
342 used_idx = (start_idx + i) & (vq->size - 1);
343 vq->used->ring[used_idx].len = dev->vhost_hlen;
344 vhost_log_used_vring(dev, vq,
345 offsetof(struct vring_used, ring[used_idx]),
346 sizeof(vq->used->ring[used_idx]));
350 rte_prefetch0(&vq->desc[desc_indexes[i+1]]);
355 *(volatile uint16_t *)&vq->used->idx += count;
356 vq->last_used_idx += count;
357 vhost_log_used_vring(dev, vq,
358 offsetof(struct vring_used, idx),
359 sizeof(vq->used->idx));
361 /* flush used->idx update before we read avail->flags. */
364 /* Kick the guest if necessary. */
365 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
366 && (vq->callfd >= 0))
367 eventfd_write(vq->callfd, (eventfd_t)1);
371 static inline int __attribute__((always_inline))
372 fill_vec_buf(struct virtio_net *dev, struct vhost_virtqueue *vq,
373 uint32_t avail_idx, uint32_t *vec_idx,
374 struct buf_vector *buf_vec, uint16_t *desc_chain_head,
375 uint16_t *desc_chain_len)
377 uint16_t idx = vq->avail->ring[avail_idx & (vq->size - 1)];
378 uint32_t vec_id = *vec_idx;
380 struct vring_desc *descs = vq->desc;
382 *desc_chain_head = idx;
384 if (vq->desc[idx].flags & VRING_DESC_F_INDIRECT) {
385 descs = (struct vring_desc *)(uintptr_t)
386 gpa_to_vva(dev, vq->desc[idx].addr);
387 if (unlikely(!descs))
394 if (unlikely(vec_id >= BUF_VECTOR_MAX || idx >= vq->size))
397 len += descs[idx].len;
398 buf_vec[vec_id].buf_addr = descs[idx].addr;
399 buf_vec[vec_id].buf_len = descs[idx].len;
400 buf_vec[vec_id].desc_idx = idx;
403 if ((descs[idx].flags & VRING_DESC_F_NEXT) == 0)
406 idx = descs[idx].next;
409 *desc_chain_len = len;
416 * Returns -1 on fail, 0 on success
419 reserve_avail_buf_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,
420 uint32_t size, struct buf_vector *buf_vec,
421 uint16_t *num_buffers, uint16_t avail_head)
424 uint32_t vec_idx = 0;
427 uint16_t head_idx = 0;
431 cur_idx = vq->last_avail_idx;
434 if (unlikely(cur_idx == avail_head))
437 if (unlikely(fill_vec_buf(dev, vq, cur_idx, &vec_idx, buf_vec,
438 &head_idx, &len) < 0))
440 len = RTE_MIN(len, size);
441 update_shadow_used_ring(vq, head_idx, len);
449 * if we tried all available ring items, and still
450 * can't get enough buf, it means something abnormal
453 if (unlikely(tries >= vq->size))
460 static inline int __attribute__((always_inline))
461 copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct rte_mbuf *m,
462 struct buf_vector *buf_vec, uint16_t num_buffers)
464 struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0, 0, 0, 0, 0, 0}, 0};
465 uint32_t vec_idx = 0;
467 uint32_t mbuf_offset, mbuf_avail;
468 uint32_t desc_offset, desc_avail;
470 uint64_t hdr_addr, hdr_phys_addr;
471 struct rte_mbuf *hdr_mbuf;
473 if (unlikely(m == NULL))
476 desc_addr = gpa_to_vva(dev, buf_vec[vec_idx].buf_addr);
477 if (buf_vec[vec_idx].buf_len < dev->vhost_hlen || !desc_addr)
481 hdr_addr = desc_addr;
482 hdr_phys_addr = buf_vec[vec_idx].buf_addr;
483 rte_prefetch0((void *)(uintptr_t)hdr_addr);
485 virtio_hdr.num_buffers = num_buffers;
486 LOG_DEBUG(VHOST_DATA, "(%d) RX: num merge buffers %d\n",
487 dev->vid, num_buffers);
489 desc_avail = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
490 desc_offset = dev->vhost_hlen;
492 mbuf_avail = rte_pktmbuf_data_len(m);
494 while (mbuf_avail != 0 || m->next != NULL) {
495 /* done with current desc buf, get the next one */
496 if (desc_avail == 0) {
498 desc_addr = gpa_to_vva(dev, buf_vec[vec_idx].buf_addr);
499 if (unlikely(!desc_addr))
502 /* Prefetch buffer address. */
503 rte_prefetch0((void *)(uintptr_t)desc_addr);
505 desc_avail = buf_vec[vec_idx].buf_len;
508 /* done with current mbuf, get the next one */
509 if (mbuf_avail == 0) {
513 mbuf_avail = rte_pktmbuf_data_len(m);
517 virtio_enqueue_offload(hdr_mbuf, &virtio_hdr.hdr);
518 copy_virtio_net_hdr(dev, hdr_addr, virtio_hdr);
519 vhost_log_write(dev, hdr_phys_addr, dev->vhost_hlen);
520 PRINT_PACKET(dev, (uintptr_t)hdr_addr,
526 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
527 rte_memcpy((void *)((uintptr_t)(desc_addr + desc_offset)),
528 rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
530 vhost_log_write(dev, buf_vec[vec_idx].buf_addr + desc_offset,
532 PRINT_PACKET(dev, (uintptr_t)(desc_addr + desc_offset),
535 mbuf_avail -= cpy_len;
536 mbuf_offset += cpy_len;
537 desc_avail -= cpy_len;
538 desc_offset += cpy_len;
544 static inline uint32_t __attribute__((always_inline))
545 virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,
546 struct rte_mbuf **pkts, uint32_t count)
548 struct vhost_virtqueue *vq;
549 uint32_t pkt_idx = 0;
550 uint16_t num_buffers;
551 struct buf_vector buf_vec[BUF_VECTOR_MAX];
554 LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
555 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->virt_qp_nb))) {
556 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
557 dev->vid, __func__, queue_id);
561 vq = dev->virtqueue[queue_id];
562 if (unlikely(vq->enabled == 0))
565 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
569 rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
571 vq->shadow_used_idx = 0;
572 avail_head = *((volatile uint16_t *)&vq->avail->idx);
573 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
574 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
576 if (unlikely(reserve_avail_buf_mergeable(dev, vq,
577 pkt_len, buf_vec, &num_buffers,
579 LOG_DEBUG(VHOST_DATA,
580 "(%d) failed to get enough desc from vring\n",
582 vq->shadow_used_idx -= num_buffers;
586 LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
587 dev->vid, vq->last_avail_idx,
588 vq->last_avail_idx + num_buffers);
590 if (copy_mbuf_to_desc_mergeable(dev, pkts[pkt_idx],
591 buf_vec, num_buffers) < 0) {
592 vq->shadow_used_idx -= num_buffers;
596 vq->last_avail_idx += num_buffers;
599 if (likely(vq->shadow_used_idx)) {
600 flush_shadow_used_ring(dev, vq);
602 /* flush used->idx update before we read avail->flags. */
605 /* Kick the guest if necessary. */
606 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
607 && (vq->callfd >= 0))
608 eventfd_write(vq->callfd, (eventfd_t)1);
615 rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
616 struct rte_mbuf **pkts, uint16_t count)
618 struct virtio_net *dev = get_device(vid);
623 if (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF))
624 return virtio_dev_merge_rx(dev, queue_id, pkts, count);
626 return virtio_dev_rx(dev, queue_id, pkts, count);
630 virtio_net_with_host_offload(struct virtio_net *dev)
633 (VIRTIO_NET_F_CSUM | VIRTIO_NET_F_HOST_ECN |
634 VIRTIO_NET_F_HOST_TSO4 | VIRTIO_NET_F_HOST_TSO6 |
635 VIRTIO_NET_F_HOST_UFO))
642 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
644 struct ipv4_hdr *ipv4_hdr;
645 struct ipv6_hdr *ipv6_hdr;
647 struct ether_hdr *eth_hdr;
650 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
652 m->l2_len = sizeof(struct ether_hdr);
653 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
655 if (ethertype == ETHER_TYPE_VLAN) {
656 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
658 m->l2_len += sizeof(struct vlan_hdr);
659 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
662 l3_hdr = (char *)eth_hdr + m->l2_len;
665 case ETHER_TYPE_IPv4:
666 ipv4_hdr = (struct ipv4_hdr *)l3_hdr;
667 *l4_proto = ipv4_hdr->next_proto_id;
668 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
669 *l4_hdr = (char *)l3_hdr + m->l3_len;
670 m->ol_flags |= PKT_TX_IPV4;
672 case ETHER_TYPE_IPv6:
673 ipv6_hdr = (struct ipv6_hdr *)l3_hdr;
674 *l4_proto = ipv6_hdr->proto;
675 m->l3_len = sizeof(struct ipv6_hdr);
676 *l4_hdr = (char *)l3_hdr + m->l3_len;
677 m->ol_flags |= PKT_TX_IPV6;
686 static inline void __attribute__((always_inline))
687 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
689 uint16_t l4_proto = 0;
691 struct tcp_hdr *tcp_hdr = NULL;
693 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
696 parse_ethernet(m, &l4_proto, &l4_hdr);
697 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
698 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
699 switch (hdr->csum_offset) {
700 case (offsetof(struct tcp_hdr, cksum)):
701 if (l4_proto == IPPROTO_TCP)
702 m->ol_flags |= PKT_TX_TCP_CKSUM;
704 case (offsetof(struct udp_hdr, dgram_cksum)):
705 if (l4_proto == IPPROTO_UDP)
706 m->ol_flags |= PKT_TX_UDP_CKSUM;
708 case (offsetof(struct sctp_hdr, cksum)):
709 if (l4_proto == IPPROTO_SCTP)
710 m->ol_flags |= PKT_TX_SCTP_CKSUM;
718 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
719 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
720 case VIRTIO_NET_HDR_GSO_TCPV4:
721 case VIRTIO_NET_HDR_GSO_TCPV6:
722 tcp_hdr = (struct tcp_hdr *)l4_hdr;
723 m->ol_flags |= PKT_TX_TCP_SEG;
724 m->tso_segsz = hdr->gso_size;
725 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
728 RTE_LOG(WARNING, VHOST_DATA,
729 "unsupported gso type %u.\n", hdr->gso_type);
735 #define RARP_PKT_SIZE 64
738 make_rarp_packet(struct rte_mbuf *rarp_mbuf, const struct ether_addr *mac)
740 struct ether_hdr *eth_hdr;
741 struct arp_hdr *rarp;
743 if (rarp_mbuf->buf_len < 64) {
744 RTE_LOG(WARNING, VHOST_DATA,
745 "failed to make RARP; mbuf size too small %u (< %d)\n",
746 rarp_mbuf->buf_len, RARP_PKT_SIZE);
750 /* Ethernet header. */
751 eth_hdr = rte_pktmbuf_mtod_offset(rarp_mbuf, struct ether_hdr *, 0);
752 memset(eth_hdr->d_addr.addr_bytes, 0xff, ETHER_ADDR_LEN);
753 ether_addr_copy(mac, ð_hdr->s_addr);
754 eth_hdr->ether_type = htons(ETHER_TYPE_RARP);
757 rarp = (struct arp_hdr *)(eth_hdr + 1);
758 rarp->arp_hrd = htons(ARP_HRD_ETHER);
759 rarp->arp_pro = htons(ETHER_TYPE_IPv4);
760 rarp->arp_hln = ETHER_ADDR_LEN;
762 rarp->arp_op = htons(ARP_OP_REVREQUEST);
764 ether_addr_copy(mac, &rarp->arp_data.arp_sha);
765 ether_addr_copy(mac, &rarp->arp_data.arp_tha);
766 memset(&rarp->arp_data.arp_sip, 0x00, 4);
767 memset(&rarp->arp_data.arp_tip, 0x00, 4);
769 rarp_mbuf->pkt_len = rarp_mbuf->data_len = RARP_PKT_SIZE;
774 static inline void __attribute__((always_inline))
775 put_zmbuf(struct zcopy_mbuf *zmbuf)
780 static inline int __attribute__((always_inline))
781 copy_desc_to_mbuf(struct virtio_net *dev, struct vring_desc *descs,
782 uint16_t max_desc, struct rte_mbuf *m, uint16_t desc_idx,
783 struct rte_mempool *mbuf_pool)
785 struct vring_desc *desc;
787 uint32_t desc_avail, desc_offset;
788 uint32_t mbuf_avail, mbuf_offset;
790 struct rte_mbuf *cur = m, *prev = m;
791 struct virtio_net_hdr *hdr = NULL;
792 /* A counter to avoid desc dead loop chain */
793 uint32_t nr_desc = 1;
795 desc = &descs[desc_idx];
796 if (unlikely((desc->len < dev->vhost_hlen)) ||
797 (desc->flags & VRING_DESC_F_INDIRECT))
800 desc_addr = gpa_to_vva(dev, desc->addr);
801 if (unlikely(!desc_addr))
804 if (virtio_net_with_host_offload(dev)) {
805 hdr = (struct virtio_net_hdr *)((uintptr_t)desc_addr);
810 * A virtio driver normally uses at least 2 desc buffers
811 * for Tx: the first for storing the header, and others
812 * for storing the data.
814 if (likely((desc->len == dev->vhost_hlen) &&
815 (desc->flags & VRING_DESC_F_NEXT) != 0)) {
816 desc = &descs[desc->next];
817 if (unlikely(desc->flags & VRING_DESC_F_INDIRECT))
820 desc_addr = gpa_to_vva(dev, desc->addr);
821 if (unlikely(!desc_addr))
825 desc_avail = desc->len;
828 desc_avail = desc->len - dev->vhost_hlen;
829 desc_offset = dev->vhost_hlen;
832 rte_prefetch0((void *)(uintptr_t)(desc_addr + desc_offset));
834 PRINT_PACKET(dev, (uintptr_t)(desc_addr + desc_offset), desc_avail, 0);
837 mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
841 cpy_len = RTE_MIN(desc_avail, mbuf_avail);
844 * A desc buf might across two host physical pages that are
845 * not continuous. In such case (gpa_to_hpa returns 0), data
846 * will be copied even though zero copy is enabled.
848 if (unlikely(dev->dequeue_zero_copy && (hpa = gpa_to_hpa(dev,
849 desc->addr + desc_offset, cpy_len)))) {
850 cur->data_len = cpy_len;
852 cur->buf_addr = (void *)(uintptr_t)desc_addr;
853 cur->buf_physaddr = hpa;
856 * In zero copy mode, one mbuf can only reference data
857 * for one or partial of one desc buff.
859 mbuf_avail = cpy_len;
861 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *,
863 (void *)((uintptr_t)(desc_addr + desc_offset)),
867 mbuf_avail -= cpy_len;
868 mbuf_offset += cpy_len;
869 desc_avail -= cpy_len;
870 desc_offset += cpy_len;
872 /* This desc reaches to its end, get the next one */
873 if (desc_avail == 0) {
874 if ((desc->flags & VRING_DESC_F_NEXT) == 0)
877 if (unlikely(desc->next >= max_desc ||
878 ++nr_desc > max_desc))
880 desc = &descs[desc->next];
881 if (unlikely(desc->flags & VRING_DESC_F_INDIRECT))
884 desc_addr = gpa_to_vva(dev, desc->addr);
885 if (unlikely(!desc_addr))
888 rte_prefetch0((void *)(uintptr_t)desc_addr);
891 desc_avail = desc->len;
893 PRINT_PACKET(dev, (uintptr_t)desc_addr, desc->len, 0);
897 * This mbuf reaches to its end, get a new one
900 if (mbuf_avail == 0) {
901 cur = rte_pktmbuf_alloc(mbuf_pool);
902 if (unlikely(cur == NULL)) {
903 RTE_LOG(ERR, VHOST_DATA, "Failed to "
904 "allocate memory for mbuf.\n");
909 prev->data_len = mbuf_offset;
911 m->pkt_len += mbuf_offset;
915 mbuf_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
919 prev->data_len = mbuf_offset;
920 m->pkt_len += mbuf_offset;
923 vhost_dequeue_offload(hdr, m);
928 static inline void __attribute__((always_inline))
929 update_used_ring(struct virtio_net *dev, struct vhost_virtqueue *vq,
930 uint32_t used_idx, uint32_t desc_idx)
932 vq->used->ring[used_idx].id = desc_idx;
933 vq->used->ring[used_idx].len = 0;
934 vhost_log_used_vring(dev, vq,
935 offsetof(struct vring_used, ring[used_idx]),
936 sizeof(vq->used->ring[used_idx]));
939 static inline void __attribute__((always_inline))
940 update_used_idx(struct virtio_net *dev, struct vhost_virtqueue *vq,
943 if (unlikely(count == 0))
949 vq->used->idx += count;
950 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
951 sizeof(vq->used->idx));
953 /* Kick guest if required. */
954 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
955 && (vq->callfd >= 0))
956 eventfd_write(vq->callfd, (eventfd_t)1);
959 static inline struct zcopy_mbuf *__attribute__((always_inline))
960 get_zmbuf(struct vhost_virtqueue *vq)
966 /* search [last_zmbuf_idx, zmbuf_size) */
967 i = vq->last_zmbuf_idx;
968 last = vq->zmbuf_size;
971 for (; i < last; i++) {
972 if (vq->zmbufs[i].in_use == 0) {
973 vq->last_zmbuf_idx = i + 1;
974 vq->zmbufs[i].in_use = 1;
975 return &vq->zmbufs[i];
981 /* search [0, last_zmbuf_idx) */
983 last = vq->last_zmbuf_idx;
990 static inline bool __attribute__((always_inline))
991 mbuf_is_consumed(struct rte_mbuf *m)
994 if (rte_mbuf_refcnt_read(m) > 1)
1003 rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
1004 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
1006 struct virtio_net *dev;
1007 struct rte_mbuf *rarp_mbuf = NULL;
1008 struct vhost_virtqueue *vq;
1009 uint32_t desc_indexes[MAX_PKT_BURST];
1012 uint16_t free_entries;
1015 dev = get_device(vid);
1019 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->virt_qp_nb))) {
1020 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
1021 dev->vid, __func__, queue_id);
1025 vq = dev->virtqueue[queue_id];
1026 if (unlikely(vq->enabled == 0))
1029 if (unlikely(dev->dequeue_zero_copy)) {
1030 struct zcopy_mbuf *zmbuf, *next;
1033 for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1034 zmbuf != NULL; zmbuf = next) {
1035 next = TAILQ_NEXT(zmbuf, next);
1037 if (mbuf_is_consumed(zmbuf->mbuf)) {
1038 used_idx = vq->last_used_idx++ & (vq->size - 1);
1039 update_used_ring(dev, vq, used_idx,
1043 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1044 rte_pktmbuf_free(zmbuf->mbuf);
1050 update_used_idx(dev, vq, nr_updated);
1054 * Construct a RARP broadcast packet, and inject it to the "pkts"
1055 * array, to looks like that guest actually send such packet.
1057 * Check user_send_rarp() for more information.
1059 if (unlikely(rte_atomic16_cmpset((volatile uint16_t *)
1060 &dev->broadcast_rarp.cnt, 1, 0))) {
1061 rarp_mbuf = rte_pktmbuf_alloc(mbuf_pool);
1062 if (rarp_mbuf == NULL) {
1063 RTE_LOG(ERR, VHOST_DATA,
1064 "Failed to allocate memory for mbuf.\n");
1068 if (make_rarp_packet(rarp_mbuf, &dev->mac)) {
1069 rte_pktmbuf_free(rarp_mbuf);
1076 free_entries = *((volatile uint16_t *)&vq->avail->idx) -
1078 if (free_entries == 0)
1081 LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
1083 /* Prefetch available and used ring */
1084 avail_idx = vq->last_avail_idx & (vq->size - 1);
1085 used_idx = vq->last_used_idx & (vq->size - 1);
1086 rte_prefetch0(&vq->avail->ring[avail_idx]);
1087 rte_prefetch0(&vq->used->ring[used_idx]);
1089 count = RTE_MIN(count, MAX_PKT_BURST);
1090 count = RTE_MIN(count, free_entries);
1091 LOG_DEBUG(VHOST_DATA, "(%d) about to dequeue %u buffers\n",
1094 /* Retrieve all of the head indexes first to avoid caching issues. */
1095 for (i = 0; i < count; i++) {
1096 avail_idx = (vq->last_avail_idx + i) & (vq->size - 1);
1097 used_idx = (vq->last_used_idx + i) & (vq->size - 1);
1098 desc_indexes[i] = vq->avail->ring[avail_idx];
1100 if (likely(dev->dequeue_zero_copy == 0))
1101 update_used_ring(dev, vq, used_idx, desc_indexes[i]);
1104 /* Prefetch descriptor index. */
1105 rte_prefetch0(&vq->desc[desc_indexes[0]]);
1106 for (i = 0; i < count; i++) {
1107 struct vring_desc *desc;
1111 if (likely(i + 1 < count))
1112 rte_prefetch0(&vq->desc[desc_indexes[i + 1]]);
1114 if (vq->desc[desc_indexes[i]].flags & VRING_DESC_F_INDIRECT) {
1115 desc = (struct vring_desc *)(uintptr_t)gpa_to_vva(dev,
1116 vq->desc[desc_indexes[i]].addr);
1117 if (unlikely(!desc))
1120 rte_prefetch0(desc);
1121 sz = vq->desc[desc_indexes[i]].len / sizeof(*desc);
1126 idx = desc_indexes[i];
1129 pkts[i] = rte_pktmbuf_alloc(mbuf_pool);
1130 if (unlikely(pkts[i] == NULL)) {
1131 RTE_LOG(ERR, VHOST_DATA,
1132 "Failed to allocate memory for mbuf.\n");
1136 err = copy_desc_to_mbuf(dev, desc, sz, pkts[i], idx, mbuf_pool);
1137 if (unlikely(err)) {
1138 rte_pktmbuf_free(pkts[i]);
1142 if (unlikely(dev->dequeue_zero_copy)) {
1143 struct zcopy_mbuf *zmbuf;
1145 zmbuf = get_zmbuf(vq);
1147 rte_pktmbuf_free(pkts[i]);
1150 zmbuf->mbuf = pkts[i];
1151 zmbuf->desc_idx = desc_indexes[i];
1154 * Pin lock the mbuf; we will check later to see
1155 * whether the mbuf is freed (when we are the last
1156 * user) or not. If that's the case, we then could
1157 * update the used ring safely.
1159 rte_mbuf_refcnt_update(pkts[i], 1);
1162 TAILQ_INSERT_TAIL(&vq->zmbuf_list, zmbuf, next);
1165 vq->last_avail_idx += i;
1167 if (likely(dev->dequeue_zero_copy == 0)) {
1168 vq->last_used_idx += i;
1169 update_used_idx(dev, vq, i);
1173 if (unlikely(rarp_mbuf != NULL)) {
1175 * Inject it to the head of "pkts" array, so that switch's mac
1176 * learning table will get updated first.
1178 memmove(&pkts[1], pkts, i * sizeof(struct rte_mbuf *));
1179 pkts[0] = rarp_mbuf;