4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <linux/virtio_net.h>
39 #include <rte_memcpy.h>
40 #include <rte_ether.h>
42 #include <rte_virtio_net.h>
48 #include "vhost-net.h"
50 #define MAX_PKT_BURST 32
51 #define VHOST_LOG_PAGE 4096
53 static inline void __attribute__((always_inline))
54 vhost_log_page(uint8_t *log_base, uint64_t page)
56 log_base[page / 8] |= 1 << (page % 8);
59 static inline void __attribute__((always_inline))
60 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
64 if (likely(((dev->features & (1ULL << VHOST_F_LOG_ALL)) == 0) ||
65 !dev->log_base || !len))
68 if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
71 /* To make sure guest memory updates are committed before logging */
74 page = addr / VHOST_LOG_PAGE;
75 while (page * VHOST_LOG_PAGE < addr + len) {
76 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
81 static inline void __attribute__((always_inline))
82 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
83 uint64_t offset, uint64_t len)
85 vhost_log_write(dev, vq->log_guest_addr + offset, len);
89 is_valid_virt_queue_idx(uint32_t idx, int is_tx, uint32_t qp_nb)
91 return (is_tx ^ (idx & 1)) == 0 && idx < qp_nb * VIRTIO_QNUM;
95 virtio_enqueue_offload(struct rte_mbuf *m_buf, struct virtio_net_hdr *net_hdr)
97 memset(net_hdr, 0, sizeof(struct virtio_net_hdr));
99 if (m_buf->ol_flags & PKT_TX_L4_MASK) {
100 net_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
101 net_hdr->csum_start = m_buf->l2_len + m_buf->l3_len;
103 switch (m_buf->ol_flags & PKT_TX_L4_MASK) {
104 case PKT_TX_TCP_CKSUM:
105 net_hdr->csum_offset = (offsetof(struct tcp_hdr,
108 case PKT_TX_UDP_CKSUM:
109 net_hdr->csum_offset = (offsetof(struct udp_hdr,
112 case PKT_TX_SCTP_CKSUM:
113 net_hdr->csum_offset = (offsetof(struct sctp_hdr,
119 if (m_buf->ol_flags & PKT_TX_TCP_SEG) {
120 if (m_buf->ol_flags & PKT_TX_IPV4)
121 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
123 net_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
124 net_hdr->gso_size = m_buf->tso_segsz;
125 net_hdr->hdr_len = m_buf->l2_len + m_buf->l3_len
133 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
134 * be received from the physical port or from another virtio device. A packet
135 * count is returned to indicate the number of packets that are succesfully
136 * added to the RX queue. This function works when the mbuf is scattered, but
137 * it doesn't support the mergeable feature.
139 static inline uint32_t __attribute__((always_inline))
140 virtio_dev_rx(struct virtio_net *dev, uint16_t queue_id,
141 struct rte_mbuf **pkts, uint32_t count)
143 struct vhost_virtqueue *vq;
144 struct vring_desc *desc, *hdr_desc;
145 struct rte_mbuf *buff, *first_buff;
146 /* The virtio_hdr is initialised to 0. */
147 struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0, 0, 0, 0, 0, 0}, 0};
148 uint64_t buff_addr = 0;
149 uint64_t buff_hdr_addr = 0;
150 uint32_t head[MAX_PKT_BURST];
151 uint32_t head_idx, packet_success = 0;
152 uint16_t avail_idx, res_cur_idx;
153 uint16_t res_base_idx, res_end_idx;
154 uint16_t free_entries;
157 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") virtio_dev_rx()\n", dev->device_fh);
158 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->virt_qp_nb))) {
159 RTE_LOG(ERR, VHOST_DATA,
160 "%s (%"PRIu64"): virtqueue idx:%d invalid.\n",
161 __func__, dev->device_fh, queue_id);
165 vq = dev->virtqueue[queue_id];
166 if (unlikely(vq->enabled == 0))
169 count = (count > MAX_PKT_BURST) ? MAX_PKT_BURST : count;
172 * As many data cores may want access to available buffers,
173 * they need to be reserved.
176 res_base_idx = vq->last_used_idx_res;
177 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
179 free_entries = (avail_idx - res_base_idx);
180 /*check that we have enough buffers*/
181 if (unlikely(count > free_entries))
182 count = free_entries;
187 res_end_idx = res_base_idx + count;
188 /* vq->last_used_idx_res is atomically updated. */
189 /* TODO: Allow to disable cmpset if no concurrency in application. */
190 success = rte_atomic16_cmpset(&vq->last_used_idx_res,
191 res_base_idx, res_end_idx);
192 } while (unlikely(success == 0));
193 res_cur_idx = res_base_idx;
194 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") Current Index %d| End Index %d\n",
195 dev->device_fh, res_cur_idx, res_end_idx);
197 /* Prefetch available ring to retrieve indexes. */
198 rte_prefetch0(&vq->avail->ring[res_cur_idx & (vq->size - 1)]);
200 /* Retrieve all of the head indexes first to avoid caching issues. */
201 for (head_idx = 0; head_idx < count; head_idx++)
202 head[head_idx] = vq->avail->ring[(res_cur_idx + head_idx) &
205 /*Prefetch descriptor index. */
206 rte_prefetch0(&vq->desc[head[packet_success]]);
208 while (res_cur_idx != res_end_idx) {
209 uint32_t offset = 0, vb_offset = 0;
210 uint32_t pkt_len, len_to_cpy, data_len, total_copied = 0;
211 uint8_t hdr = 0, uncompleted_pkt = 0;
214 /* Get descriptor from available ring */
215 desc = &vq->desc[head[packet_success]];
217 buff = pkts[packet_success];
220 /* Convert from gpa to vva (guest physical addr -> vhost virtual addr) */
221 buff_addr = gpa_to_vva(dev, desc->addr);
222 /* Prefetch buffer address. */
223 rte_prefetch0((void *)(uintptr_t)buff_addr);
225 /* Copy virtio_hdr to packet and increment buffer address */
226 buff_hdr_addr = buff_addr;
230 * If the descriptors are chained the header and data are
231 * placed in separate buffers.
233 if ((desc->flags & VRING_DESC_F_NEXT) &&
234 (desc->len == vq->vhost_hlen)) {
235 desc = &vq->desc[desc->next];
236 /* Buffer address translation. */
237 buff_addr = gpa_to_vva(dev, desc->addr);
239 vb_offset += vq->vhost_hlen;
243 pkt_len = rte_pktmbuf_pkt_len(buff);
244 data_len = rte_pktmbuf_data_len(buff);
245 len_to_cpy = RTE_MIN(data_len,
246 hdr ? desc->len - vq->vhost_hlen : desc->len);
247 while (total_copied < pkt_len) {
248 /* Copy mbuf data to buffer */
249 rte_memcpy((void *)(uintptr_t)(buff_addr + vb_offset),
250 rte_pktmbuf_mtod_offset(buff, const void *, offset),
252 vhost_log_write(dev, desc->addr + vb_offset, len_to_cpy);
253 PRINT_PACKET(dev, (uintptr_t)(buff_addr + vb_offset),
256 offset += len_to_cpy;
257 vb_offset += len_to_cpy;
258 total_copied += len_to_cpy;
260 /* The whole packet completes */
261 if (total_copied == pkt_len)
264 /* The current segment completes */
265 if (offset == data_len) {
268 data_len = rte_pktmbuf_data_len(buff);
271 /* The current vring descriptor done */
272 if (vb_offset == desc->len) {
273 if (desc->flags & VRING_DESC_F_NEXT) {
274 desc = &vq->desc[desc->next];
275 buff_addr = gpa_to_vva(dev, desc->addr);
278 /* Room in vring buffer is not enough */
283 len_to_cpy = RTE_MIN(data_len - offset, desc->len - vb_offset);
286 /* Update used ring with desc information */
287 idx = res_cur_idx & (vq->size - 1);
288 vq->used->ring[idx].id = head[packet_success];
290 /* Drop the packet if it is uncompleted */
291 if (unlikely(uncompleted_pkt == 1))
292 vq->used->ring[idx].len = vq->vhost_hlen;
294 vq->used->ring[idx].len = pkt_len + vq->vhost_hlen;
296 vhost_log_used_vring(dev, vq,
297 offsetof(struct vring_used, ring[idx]),
298 sizeof(vq->used->ring[idx]));
303 if (unlikely(uncompleted_pkt == 1))
306 virtio_enqueue_offload(first_buff, &virtio_hdr.hdr);
308 rte_memcpy((void *)(uintptr_t)buff_hdr_addr,
309 (const void *)&virtio_hdr, vq->vhost_hlen);
310 vhost_log_write(dev, hdr_desc->addr, vq->vhost_hlen);
312 PRINT_PACKET(dev, (uintptr_t)buff_hdr_addr, vq->vhost_hlen, 1);
314 if (res_cur_idx < res_end_idx) {
315 /* Prefetch descriptor index. */
316 rte_prefetch0(&vq->desc[head[packet_success]]);
320 rte_compiler_barrier();
322 /* Wait until it's our turn to add our buffer to the used ring. */
323 while (unlikely(vq->last_used_idx != res_base_idx))
326 *(volatile uint16_t *)&vq->used->idx += count;
327 vq->last_used_idx = res_end_idx;
328 vhost_log_used_vring(dev, vq,
329 offsetof(struct vring_used, idx),
330 sizeof(vq->used->idx));
332 /* flush used->idx update before we read avail->flags. */
335 /* Kick the guest if necessary. */
336 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
337 eventfd_write(vq->callfd, (eventfd_t)1);
341 static inline uint32_t __attribute__((always_inline))
342 copy_from_mbuf_to_vring(struct virtio_net *dev, uint32_t queue_id,
343 uint16_t res_base_idx, uint16_t res_end_idx,
344 struct rte_mbuf *pkt)
346 uint32_t vec_idx = 0;
347 uint32_t entry_success = 0;
348 struct vhost_virtqueue *vq;
349 /* The virtio_hdr is initialised to 0. */
350 struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {
351 {0, 0, 0, 0, 0, 0}, 0};
352 uint16_t cur_idx = res_base_idx;
353 uint64_t vb_addr = 0;
354 uint64_t vb_hdr_addr = 0;
355 uint32_t seg_offset = 0;
356 uint32_t vb_offset = 0;
359 uint32_t cpy_len, entry_len;
365 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") Current Index %d| "
367 dev->device_fh, cur_idx, res_end_idx);
370 * Convert from gpa to vva
371 * (guest physical addr -> vhost virtual addr)
373 vq = dev->virtqueue[queue_id];
375 vb_addr = gpa_to_vva(dev, vq->buf_vec[vec_idx].buf_addr);
376 vb_hdr_addr = vb_addr;
378 /* Prefetch buffer address. */
379 rte_prefetch0((void *)(uintptr_t)vb_addr);
381 virtio_hdr.num_buffers = res_end_idx - res_base_idx;
383 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") RX: Num merge buffers %d\n",
384 dev->device_fh, virtio_hdr.num_buffers);
386 virtio_enqueue_offload(pkt, &virtio_hdr.hdr);
388 rte_memcpy((void *)(uintptr_t)vb_hdr_addr,
389 (const void *)&virtio_hdr, vq->vhost_hlen);
390 vhost_log_write(dev, vq->buf_vec[vec_idx].buf_addr, vq->vhost_hlen);
392 PRINT_PACKET(dev, (uintptr_t)vb_hdr_addr, vq->vhost_hlen, 1);
394 seg_avail = rte_pktmbuf_data_len(pkt);
395 vb_offset = vq->vhost_hlen;
396 vb_avail = vq->buf_vec[vec_idx].buf_len - vq->vhost_hlen;
398 entry_len = vq->vhost_hlen;
401 uint32_t desc_idx = vq->buf_vec[vec_idx].desc_idx;
403 if ((vq->desc[desc_idx].flags & VRING_DESC_F_NEXT) == 0) {
404 idx = cur_idx & (vq->size - 1);
406 /* Update used ring with desc information */
407 vq->used->ring[idx].id = vq->buf_vec[vec_idx].desc_idx;
408 vq->used->ring[idx].len = entry_len;
410 vhost_log_used_vring(dev, vq,
411 offsetof(struct vring_used, ring[idx]),
412 sizeof(vq->used->ring[idx]));
420 vb_addr = gpa_to_vva(dev, vq->buf_vec[vec_idx].buf_addr);
422 /* Prefetch buffer address. */
423 rte_prefetch0((void *)(uintptr_t)vb_addr);
425 vb_avail = vq->buf_vec[vec_idx].buf_len;
428 cpy_len = RTE_MIN(vb_avail, seg_avail);
430 while (cpy_len > 0) {
431 /* Copy mbuf data to vring buffer */
432 rte_memcpy((void *)(uintptr_t)(vb_addr + vb_offset),
433 rte_pktmbuf_mtod_offset(pkt, const void *, seg_offset),
435 vhost_log_write(dev, vq->buf_vec[vec_idx].buf_addr + vb_offset,
439 (uintptr_t)(vb_addr + vb_offset),
442 seg_offset += cpy_len;
443 vb_offset += cpy_len;
444 seg_avail -= cpy_len;
446 entry_len += cpy_len;
448 if (seg_avail != 0) {
450 * The virtio buffer in this vring
451 * entry reach to its end.
452 * But the segment doesn't complete.
454 if ((vq->desc[vq->buf_vec[vec_idx].desc_idx].flags &
455 VRING_DESC_F_NEXT) == 0) {
456 /* Update used ring with desc information */
457 idx = cur_idx & (vq->size - 1);
458 vq->used->ring[idx].id
459 = vq->buf_vec[vec_idx].desc_idx;
460 vq->used->ring[idx].len = entry_len;
461 vhost_log_used_vring(dev, vq,
462 offsetof(struct vring_used, ring[idx]),
463 sizeof(vq->used->ring[idx]));
470 vb_addr = gpa_to_vva(dev,
471 vq->buf_vec[vec_idx].buf_addr);
473 vb_avail = vq->buf_vec[vec_idx].buf_len;
474 cpy_len = RTE_MIN(vb_avail, seg_avail);
477 * This current segment complete, need continue to
478 * check if the whole packet complete or not.
483 * There are more segments.
487 * This current buffer from vring is
488 * used up, need fetch next buffer
492 vq->buf_vec[vec_idx].desc_idx;
494 if ((vq->desc[desc_idx].flags &
495 VRING_DESC_F_NEXT) == 0) {
496 idx = cur_idx & (vq->size - 1);
498 * Update used ring with the
499 * descriptor information
501 vq->used->ring[idx].id
503 vq->used->ring[idx].len
505 vhost_log_used_vring(dev, vq,
506 offsetof(struct vring_used, ring[idx]),
507 sizeof(vq->used->ring[idx]));
513 /* Get next buffer from buf_vec. */
515 vb_addr = gpa_to_vva(dev,
516 vq->buf_vec[vec_idx].buf_addr);
518 vq->buf_vec[vec_idx].buf_len;
523 seg_avail = rte_pktmbuf_data_len(pkt);
524 cpy_len = RTE_MIN(vb_avail, seg_avail);
527 * This whole packet completes.
529 /* Update used ring with desc information */
530 idx = cur_idx & (vq->size - 1);
531 vq->used->ring[idx].id
532 = vq->buf_vec[vec_idx].desc_idx;
533 vq->used->ring[idx].len = entry_len;
534 vhost_log_used_vring(dev, vq,
535 offsetof(struct vring_used, ring[idx]),
536 sizeof(vq->used->ring[idx]));
543 return entry_success;
546 static inline void __attribute__((always_inline))
547 update_secure_len(struct vhost_virtqueue *vq, uint32_t id,
548 uint32_t *secure_len, uint32_t *vec_idx)
550 uint16_t wrapped_idx = id & (vq->size - 1);
551 uint32_t idx = vq->avail->ring[wrapped_idx];
553 uint32_t len = *secure_len;
554 uint32_t vec_id = *vec_idx;
558 len += vq->desc[idx].len;
559 vq->buf_vec[vec_id].buf_addr = vq->desc[idx].addr;
560 vq->buf_vec[vec_id].buf_len = vq->desc[idx].len;
561 vq->buf_vec[vec_id].desc_idx = idx;
564 if (vq->desc[idx].flags & VRING_DESC_F_NEXT) {
565 idx = vq->desc[idx].next;
575 * This function works for mergeable RX.
577 static inline uint32_t __attribute__((always_inline))
578 virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,
579 struct rte_mbuf **pkts, uint32_t count)
581 struct vhost_virtqueue *vq;
582 uint32_t pkt_idx = 0, entry_success = 0;
584 uint16_t res_base_idx, res_cur_idx;
587 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") virtio_dev_merge_rx()\n",
589 if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->virt_qp_nb))) {
590 RTE_LOG(ERR, VHOST_DATA,
591 "%s (%"PRIu64"): virtqueue idx:%d invalid.\n",
592 __func__, dev->device_fh, queue_id);
596 vq = dev->virtqueue[queue_id];
597 if (unlikely(vq->enabled == 0))
600 count = RTE_MIN((uint32_t)MAX_PKT_BURST, count);
605 for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
606 uint32_t pkt_len = pkts[pkt_idx]->pkt_len + vq->vhost_hlen;
610 * As many data cores may want access to available
611 * buffers, they need to be reserved.
613 uint32_t secure_len = 0;
614 uint32_t vec_idx = 0;
616 res_base_idx = vq->last_used_idx_res;
617 res_cur_idx = res_base_idx;
620 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
621 if (unlikely(res_cur_idx == avail_idx))
624 update_secure_len(vq, res_cur_idx,
625 &secure_len, &vec_idx);
627 } while (pkt_len > secure_len);
629 /* vq->last_used_idx_res is atomically updated. */
630 success = rte_atomic16_cmpset(&vq->last_used_idx_res,
633 } while (success == 0);
635 entry_success = copy_from_mbuf_to_vring(dev, queue_id,
636 res_base_idx, res_cur_idx, pkts[pkt_idx]);
638 rte_compiler_barrier();
641 * Wait until it's our turn to add our buffer
644 while (unlikely(vq->last_used_idx != res_base_idx))
647 *(volatile uint16_t *)&vq->used->idx += entry_success;
648 vq->last_used_idx = res_cur_idx;
652 if (likely(pkt_idx)) {
653 /* flush used->idx update before we read avail->flags. */
656 /* Kick the guest if necessary. */
657 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
658 eventfd_write(vq->callfd, (eventfd_t)1);
665 rte_vhost_enqueue_burst(struct virtio_net *dev, uint16_t queue_id,
666 struct rte_mbuf **pkts, uint16_t count)
668 if (unlikely(dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)))
669 return virtio_dev_merge_rx(dev, queue_id, pkts, count);
671 return virtio_dev_rx(dev, queue_id, pkts, count);
675 parse_ethernet(struct rte_mbuf *m, uint16_t *l4_proto, void **l4_hdr)
677 struct ipv4_hdr *ipv4_hdr;
678 struct ipv6_hdr *ipv6_hdr;
680 struct ether_hdr *eth_hdr;
683 eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
685 m->l2_len = sizeof(struct ether_hdr);
686 ethertype = rte_be_to_cpu_16(eth_hdr->ether_type);
688 if (ethertype == ETHER_TYPE_VLAN) {
689 struct vlan_hdr *vlan_hdr = (struct vlan_hdr *)(eth_hdr + 1);
691 m->l2_len += sizeof(struct vlan_hdr);
692 ethertype = rte_be_to_cpu_16(vlan_hdr->eth_proto);
695 l3_hdr = (char *)eth_hdr + m->l2_len;
698 case ETHER_TYPE_IPv4:
699 ipv4_hdr = (struct ipv4_hdr *)l3_hdr;
700 *l4_proto = ipv4_hdr->next_proto_id;
701 m->l3_len = (ipv4_hdr->version_ihl & 0x0f) * 4;
702 *l4_hdr = (char *)l3_hdr + m->l3_len;
703 m->ol_flags |= PKT_TX_IPV4;
705 case ETHER_TYPE_IPv6:
706 ipv6_hdr = (struct ipv6_hdr *)l3_hdr;
707 *l4_proto = ipv6_hdr->proto;
708 m->l3_len = sizeof(struct ipv6_hdr);
709 *l4_hdr = (char *)l3_hdr + m->l3_len;
710 m->ol_flags |= PKT_TX_IPV6;
719 static inline void __attribute__((always_inline))
720 vhost_dequeue_offload(struct virtio_net_hdr *hdr, struct rte_mbuf *m)
722 uint16_t l4_proto = 0;
724 struct tcp_hdr *tcp_hdr = NULL;
726 parse_ethernet(m, &l4_proto, &l4_hdr);
727 if (hdr->flags == VIRTIO_NET_HDR_F_NEEDS_CSUM) {
728 if (hdr->csum_start == (m->l2_len + m->l3_len)) {
729 switch (hdr->csum_offset) {
730 case (offsetof(struct tcp_hdr, cksum)):
731 if (l4_proto == IPPROTO_TCP)
732 m->ol_flags |= PKT_TX_TCP_CKSUM;
734 case (offsetof(struct udp_hdr, dgram_cksum)):
735 if (l4_proto == IPPROTO_UDP)
736 m->ol_flags |= PKT_TX_UDP_CKSUM;
738 case (offsetof(struct sctp_hdr, cksum)):
739 if (l4_proto == IPPROTO_SCTP)
740 m->ol_flags |= PKT_TX_SCTP_CKSUM;
748 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
749 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
750 case VIRTIO_NET_HDR_GSO_TCPV4:
751 case VIRTIO_NET_HDR_GSO_TCPV6:
752 tcp_hdr = (struct tcp_hdr *)l4_hdr;
753 m->ol_flags |= PKT_TX_TCP_SEG;
754 m->tso_segsz = hdr->gso_size;
755 m->l4_len = (tcp_hdr->data_off & 0xf0) >> 2;
758 RTE_LOG(WARNING, VHOST_DATA,
759 "unsupported gso type %u.\n", hdr->gso_type);
765 #define RARP_PKT_SIZE 64
768 make_rarp_packet(struct rte_mbuf *rarp_mbuf, const struct ether_addr *mac)
770 struct ether_hdr *eth_hdr;
771 struct arp_hdr *rarp;
773 if (rarp_mbuf->buf_len < 64) {
774 RTE_LOG(WARNING, VHOST_DATA,
775 "failed to make RARP; mbuf size too small %u (< %d)\n",
776 rarp_mbuf->buf_len, RARP_PKT_SIZE);
780 /* Ethernet header. */
781 eth_hdr = rte_pktmbuf_mtod_offset(rarp_mbuf, struct ether_hdr *, 0);
782 memset(eth_hdr->d_addr.addr_bytes, 0xff, ETHER_ADDR_LEN);
783 ether_addr_copy(mac, ð_hdr->s_addr);
784 eth_hdr->ether_type = htons(ETHER_TYPE_RARP);
787 rarp = (struct arp_hdr *)(eth_hdr + 1);
788 rarp->arp_hrd = htons(ARP_HRD_ETHER);
789 rarp->arp_pro = htons(ETHER_TYPE_IPv4);
790 rarp->arp_hln = ETHER_ADDR_LEN;
792 rarp->arp_op = htons(ARP_OP_REVREQUEST);
794 ether_addr_copy(mac, &rarp->arp_data.arp_sha);
795 ether_addr_copy(mac, &rarp->arp_data.arp_tha);
796 memset(&rarp->arp_data.arp_sip, 0x00, 4);
797 memset(&rarp->arp_data.arp_tip, 0x00, 4);
799 rarp_mbuf->pkt_len = rarp_mbuf->data_len = RARP_PKT_SIZE;
805 rte_vhost_dequeue_burst(struct virtio_net *dev, uint16_t queue_id,
806 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
808 struct rte_mbuf *m, *prev, *rarp_mbuf = NULL;
809 struct vhost_virtqueue *vq;
810 struct vring_desc *desc;
811 uint64_t vb_addr = 0;
812 uint64_t vb_net_hdr_addr = 0;
813 uint32_t head[MAX_PKT_BURST];
816 uint16_t free_entries, entry_success = 0;
818 struct virtio_net_hdr *hdr = NULL;
820 if (unlikely(!is_valid_virt_queue_idx(queue_id, 1, dev->virt_qp_nb))) {
821 RTE_LOG(ERR, VHOST_DATA,
822 "%s (%"PRIu64"): virtqueue idx:%d invalid.\n",
823 __func__, dev->device_fh, queue_id);
827 vq = dev->virtqueue[queue_id];
828 if (unlikely(vq->enabled == 0))
832 * Construct a RARP broadcast packet, and inject it to the "pkts"
833 * array, to looks like that guest actually send such packet.
835 * Check user_send_rarp() for more information.
837 if (unlikely(rte_atomic16_cmpset((volatile uint16_t *)
838 &dev->broadcast_rarp.cnt, 1, 0))) {
839 rarp_mbuf = rte_pktmbuf_alloc(mbuf_pool);
840 if (rarp_mbuf == NULL) {
841 RTE_LOG(ERR, VHOST_DATA,
842 "Failed to allocate memory for mbuf.\n");
846 if (make_rarp_packet(rarp_mbuf, &dev->mac)) {
847 rte_pktmbuf_free(rarp_mbuf);
854 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
856 /* If there are no available buffers then return. */
857 if (vq->last_used_idx == avail_idx)
860 LOG_DEBUG(VHOST_DATA, "%s (%"PRIu64")\n", __func__,
863 /* Prefetch available ring to retrieve head indexes. */
864 rte_prefetch0(&vq->avail->ring[vq->last_used_idx & (vq->size - 1)]);
866 /*get the number of free entries in the ring*/
867 free_entries = (avail_idx - vq->last_used_idx);
869 free_entries = RTE_MIN(free_entries, count);
870 /* Limit to MAX_PKT_BURST. */
871 free_entries = RTE_MIN(free_entries, MAX_PKT_BURST);
873 LOG_DEBUG(VHOST_DATA, "(%"PRIu64") Buffers available %d\n",
874 dev->device_fh, free_entries);
875 /* Retrieve all of the head indexes first to avoid caching issues. */
876 for (i = 0; i < free_entries; i++)
877 head[i] = vq->avail->ring[(vq->last_used_idx + i) & (vq->size - 1)];
879 /* Prefetch descriptor index. */
880 rte_prefetch0(&vq->desc[head[entry_success]]);
881 rte_prefetch0(&vq->used->ring[vq->last_used_idx & (vq->size - 1)]);
883 while (entry_success < free_entries) {
884 uint32_t vb_avail, vb_offset;
885 uint32_t seg_avail, seg_offset;
887 uint32_t seg_num = 0;
888 struct rte_mbuf *cur;
889 uint8_t alloc_err = 0;
891 desc = &vq->desc[head[entry_success]];
893 vb_net_hdr_addr = gpa_to_vva(dev, desc->addr);
894 hdr = (struct virtio_net_hdr *)((uintptr_t)vb_net_hdr_addr);
896 /* Discard first buffer as it is the virtio header */
897 if (desc->flags & VRING_DESC_F_NEXT) {
898 desc = &vq->desc[desc->next];
900 vb_avail = desc->len;
902 vb_offset = vq->vhost_hlen;
903 vb_avail = desc->len - vb_offset;
906 /* Buffer address translation. */
907 vb_addr = gpa_to_vva(dev, desc->addr);
908 /* Prefetch buffer address. */
909 rte_prefetch0((void *)(uintptr_t)vb_addr);
911 used_idx = vq->last_used_idx & (vq->size - 1);
913 if (entry_success < (free_entries - 1)) {
914 /* Prefetch descriptor index. */
915 rte_prefetch0(&vq->desc[head[entry_success+1]]);
916 rte_prefetch0(&vq->used->ring[(used_idx + 1) & (vq->size - 1)]);
919 /* Update used index buffer information. */
920 vq->used->ring[used_idx].id = head[entry_success];
921 vq->used->ring[used_idx].len = 0;
922 vhost_log_used_vring(dev, vq,
923 offsetof(struct vring_used, ring[used_idx]),
924 sizeof(vq->used->ring[used_idx]));
926 /* Allocate an mbuf and populate the structure. */
927 m = rte_pktmbuf_alloc(mbuf_pool);
928 if (unlikely(m == NULL)) {
929 RTE_LOG(ERR, VHOST_DATA,
930 "Failed to allocate memory for mbuf.\n");
934 seg_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
935 cpy_len = RTE_MIN(vb_avail, seg_avail);
937 PRINT_PACKET(dev, (uintptr_t)vb_addr, desc->len, 0);
942 while (cpy_len != 0) {
943 rte_memcpy(rte_pktmbuf_mtod_offset(cur, void *, seg_offset),
944 (void *)((uintptr_t)(vb_addr + vb_offset)),
947 seg_offset += cpy_len;
948 vb_offset += cpy_len;
950 seg_avail -= cpy_len;
954 * The segment reachs to its end,
955 * while the virtio buffer in TX vring has
956 * more data to be copied.
958 cur->data_len = seg_offset;
959 m->pkt_len += seg_offset;
960 /* Allocate mbuf and populate the structure. */
961 cur = rte_pktmbuf_alloc(mbuf_pool);
962 if (unlikely(cur == NULL)) {
963 RTE_LOG(ERR, VHOST_DATA, "Failed to "
964 "allocate memory for mbuf.\n");
974 seg_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
976 if (desc->flags & VRING_DESC_F_NEXT) {
978 * There are more virtio buffers in
979 * same vring entry need to be copied.
981 if (seg_avail == 0) {
983 * The current segment hasn't
984 * room to accomodate more
987 cur->data_len = seg_offset;
988 m->pkt_len += seg_offset;
990 * Allocate an mbuf and
991 * populate the structure.
993 cur = rte_pktmbuf_alloc(mbuf_pool);
994 if (unlikely(cur == NULL)) {
1000 rte_pktmbuf_free(m);
1008 seg_avail = cur->buf_len - RTE_PKTMBUF_HEADROOM;
1011 desc = &vq->desc[desc->next];
1013 /* Buffer address translation. */
1014 vb_addr = gpa_to_vva(dev, desc->addr);
1015 /* Prefetch buffer address. */
1016 rte_prefetch0((void *)(uintptr_t)vb_addr);
1018 vb_avail = desc->len;
1020 PRINT_PACKET(dev, (uintptr_t)vb_addr,
1023 /* The whole packet completes. */
1024 cur->data_len = seg_offset;
1025 m->pkt_len += seg_offset;
1030 cpy_len = RTE_MIN(vb_avail, seg_avail);
1033 if (unlikely(alloc_err == 1))
1036 m->nb_segs = seg_num;
1037 if ((hdr->flags != 0) || (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE))
1038 vhost_dequeue_offload(hdr, m);
1040 pkts[entry_success] = m;
1041 vq->last_used_idx++;
1045 rte_compiler_barrier();
1046 vq->used->idx += entry_success;
1047 vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
1048 sizeof(vq->used->idx));
1050 /* Kick guest if required. */
1051 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT))
1052 eventfd_write(vq->callfd, (eventfd_t)1);
1055 if (unlikely(rarp_mbuf != NULL)) {
1057 * Inject it to the head of "pkts" array, so that switch's mac
1058 * learning table will get updated first.
1060 memmove(&pkts[1], pkts, entry_success * sizeof(m));
1061 pkts[0] = rarp_mbuf;
1065 return entry_success;