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.
40 #include <rte_cycles.h>
41 #include <rte_memory.h>
42 #include <rte_branch_prediction.h>
43 #include <rte_mempool.h>
44 #include <rte_malloc.h>
46 #include <rte_ether.h>
47 #include <rte_ethdev.h>
48 #include <rte_prefetch.h>
49 #include <rte_string_fns.h>
50 #include <rte_errno.h>
51 #include <rte_byteorder.h>
57 #include "virtio_logs.h"
58 #include "virtio_ethdev.h"
59 #include "virtio_pci.h"
60 #include "virtqueue.h"
61 #include "virtio_rxtx.h"
63 #ifdef RTE_LIBRTE_VIRTIO_DEBUG_DUMP
64 #define VIRTIO_DUMP_PACKET(m, len) rte_pktmbuf_dump(stdout, m, len)
66 #define VIRTIO_DUMP_PACKET(m, len) do { } while (0)
70 #define VIRTIO_SIMPLE_FLAGS ((uint32_t)ETH_TXQ_FLAGS_NOMULTSEGS | \
71 ETH_TXQ_FLAGS_NOOFFLOADS)
74 virtio_dev_rx_queue_done(void *rxq, uint16_t offset)
76 struct virtnet_rx *rxvq = rxq;
77 struct virtqueue *vq = rxvq->vq;
79 return VIRTQUEUE_NUSED(vq) >= offset;
83 vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx)
85 struct vring_desc *dp, *dp_tail;
86 struct vq_desc_extra *dxp;
87 uint16_t desc_idx_last = desc_idx;
89 dp = &vq->vq_ring.desc[desc_idx];
90 dxp = &vq->vq_descx[desc_idx];
91 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt + dxp->ndescs);
92 if ((dp->flags & VRING_DESC_F_INDIRECT) == 0) {
93 while (dp->flags & VRING_DESC_F_NEXT) {
94 desc_idx_last = dp->next;
95 dp = &vq->vq_ring.desc[dp->next];
101 * We must append the existing free chain, if any, to the end of
102 * newly freed chain. If the virtqueue was completely used, then
103 * head would be VQ_RING_DESC_CHAIN_END (ASSERTed above).
105 if (vq->vq_desc_tail_idx == VQ_RING_DESC_CHAIN_END) {
106 vq->vq_desc_head_idx = desc_idx;
108 dp_tail = &vq->vq_ring.desc[vq->vq_desc_tail_idx];
109 dp_tail->next = desc_idx;
112 vq->vq_desc_tail_idx = desc_idx_last;
113 dp->next = VQ_RING_DESC_CHAIN_END;
117 virtqueue_dequeue_burst_rx(struct virtqueue *vq, struct rte_mbuf **rx_pkts,
118 uint32_t *len, uint16_t num)
120 struct vring_used_elem *uep;
121 struct rte_mbuf *cookie;
122 uint16_t used_idx, desc_idx;
125 /* Caller does the check */
126 for (i = 0; i < num ; i++) {
127 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
128 uep = &vq->vq_ring.used->ring[used_idx];
129 desc_idx = (uint16_t) uep->id;
131 cookie = (struct rte_mbuf *)vq->vq_descx[desc_idx].cookie;
133 if (unlikely(cookie == NULL)) {
134 PMD_DRV_LOG(ERR, "vring descriptor with no mbuf cookie at %u",
135 vq->vq_used_cons_idx);
139 rte_prefetch0(cookie);
140 rte_packet_prefetch(rte_pktmbuf_mtod(cookie, void *));
142 vq->vq_used_cons_idx++;
143 vq_ring_free_chain(vq, desc_idx);
144 vq->vq_descx[desc_idx].cookie = NULL;
150 #ifndef DEFAULT_TX_FREE_THRESH
151 #define DEFAULT_TX_FREE_THRESH 32
154 /* Cleanup from completed transmits. */
156 virtio_xmit_cleanup(struct virtqueue *vq, uint16_t num)
158 uint16_t i, used_idx, desc_idx;
159 for (i = 0; i < num; i++) {
160 struct vring_used_elem *uep;
161 struct vq_desc_extra *dxp;
163 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
164 uep = &vq->vq_ring.used->ring[used_idx];
166 desc_idx = (uint16_t) uep->id;
167 dxp = &vq->vq_descx[desc_idx];
168 vq->vq_used_cons_idx++;
169 vq_ring_free_chain(vq, desc_idx);
171 if (dxp->cookie != NULL) {
172 rte_pktmbuf_free(dxp->cookie);
180 virtqueue_enqueue_recv_refill(struct virtqueue *vq, struct rte_mbuf *cookie)
182 struct vq_desc_extra *dxp;
183 struct virtio_hw *hw = vq->hw;
184 struct vring_desc *start_dp;
186 uint16_t head_idx, idx;
188 if (unlikely(vq->vq_free_cnt == 0))
190 if (unlikely(vq->vq_free_cnt < needed))
193 head_idx = vq->vq_desc_head_idx;
194 if (unlikely(head_idx >= vq->vq_nentries))
198 dxp = &vq->vq_descx[idx];
199 dxp->cookie = (void *)cookie;
200 dxp->ndescs = needed;
202 start_dp = vq->vq_ring.desc;
204 VIRTIO_MBUF_ADDR(cookie, vq) +
205 RTE_PKTMBUF_HEADROOM - hw->vtnet_hdr_size;
207 cookie->buf_len - RTE_PKTMBUF_HEADROOM + hw->vtnet_hdr_size;
208 start_dp[idx].flags = VRING_DESC_F_WRITE;
209 idx = start_dp[idx].next;
210 vq->vq_desc_head_idx = idx;
211 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
212 vq->vq_desc_tail_idx = idx;
213 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
214 vq_update_avail_ring(vq, head_idx);
219 /* When doing TSO, the IP length is not included in the pseudo header
220 * checksum of the packet given to the PMD, but for virtio it is
224 virtio_tso_fix_cksum(struct rte_mbuf *m)
226 /* common case: header is not fragmented */
227 if (likely(rte_pktmbuf_data_len(m) >= m->l2_len + m->l3_len +
229 struct ipv4_hdr *iph;
230 struct ipv6_hdr *ip6h;
232 uint16_t prev_cksum, new_cksum, ip_len, ip_paylen;
235 iph = rte_pktmbuf_mtod_offset(m, struct ipv4_hdr *, m->l2_len);
236 th = RTE_PTR_ADD(iph, m->l3_len);
237 if ((iph->version_ihl >> 4) == 4) {
238 iph->hdr_checksum = 0;
239 iph->hdr_checksum = rte_ipv4_cksum(iph);
240 ip_len = iph->total_length;
241 ip_paylen = rte_cpu_to_be_16(rte_be_to_cpu_16(ip_len) -
244 ip6h = (struct ipv6_hdr *)iph;
245 ip_paylen = ip6h->payload_len;
248 /* calculate the new phdr checksum not including ip_paylen */
249 prev_cksum = th->cksum;
252 tmp = (tmp & 0xffff) + (tmp >> 16);
255 /* replace it in the packet */
256 th->cksum = new_cksum;
261 tx_offload_enabled(struct virtio_hw *hw)
263 return vtpci_with_feature(hw, VIRTIO_NET_F_CSUM) ||
264 vtpci_with_feature(hw, VIRTIO_NET_F_HOST_TSO4) ||
265 vtpci_with_feature(hw, VIRTIO_NET_F_HOST_TSO6);
268 /* avoid write operation when necessary, to lessen cache issues */
269 #define ASSIGN_UNLESS_EQUAL(var, val) do { \
270 if ((var) != (val)) \
275 virtqueue_enqueue_xmit(struct virtnet_tx *txvq, struct rte_mbuf *cookie,
276 uint16_t needed, int use_indirect, int can_push)
278 struct virtio_tx_region *txr = txvq->virtio_net_hdr_mz->addr;
279 struct vq_desc_extra *dxp;
280 struct virtqueue *vq = txvq->vq;
281 struct vring_desc *start_dp;
282 uint16_t seg_num = cookie->nb_segs;
283 uint16_t head_idx, idx;
284 uint16_t head_size = vq->hw->vtnet_hdr_size;
285 struct virtio_net_hdr *hdr;
288 offload = tx_offload_enabled(vq->hw);
289 head_idx = vq->vq_desc_head_idx;
291 dxp = &vq->vq_descx[idx];
292 dxp->cookie = (void *)cookie;
293 dxp->ndescs = needed;
295 start_dp = vq->vq_ring.desc;
298 /* prepend cannot fail, checked by caller */
299 hdr = (struct virtio_net_hdr *)
300 rte_pktmbuf_prepend(cookie, head_size);
301 /* rte_pktmbuf_prepend() counts the hdr size to the pkt length,
302 * which is wrong. Below subtract restores correct pkt size.
304 cookie->pkt_len -= head_size;
305 /* if offload disabled, it is not zeroed below, do it now */
307 ASSIGN_UNLESS_EQUAL(hdr->csum_start, 0);
308 ASSIGN_UNLESS_EQUAL(hdr->csum_offset, 0);
309 ASSIGN_UNLESS_EQUAL(hdr->flags, 0);
310 ASSIGN_UNLESS_EQUAL(hdr->gso_type, 0);
311 ASSIGN_UNLESS_EQUAL(hdr->gso_size, 0);
312 ASSIGN_UNLESS_EQUAL(hdr->hdr_len, 0);
314 } else if (use_indirect) {
315 /* setup tx ring slot to point to indirect
316 * descriptor list stored in reserved region.
318 * the first slot in indirect ring is already preset
319 * to point to the header in reserved region
321 start_dp[idx].addr = txvq->virtio_net_hdr_mem +
322 RTE_PTR_DIFF(&txr[idx].tx_indir, txr);
323 start_dp[idx].len = (seg_num + 1) * sizeof(struct vring_desc);
324 start_dp[idx].flags = VRING_DESC_F_INDIRECT;
325 hdr = (struct virtio_net_hdr *)&txr[idx].tx_hdr;
327 /* loop below will fill in rest of the indirect elements */
328 start_dp = txr[idx].tx_indir;
331 /* setup first tx ring slot to point to header
332 * stored in reserved region.
334 start_dp[idx].addr = txvq->virtio_net_hdr_mem +
335 RTE_PTR_DIFF(&txr[idx].tx_hdr, txr);
336 start_dp[idx].len = vq->hw->vtnet_hdr_size;
337 start_dp[idx].flags = VRING_DESC_F_NEXT;
338 hdr = (struct virtio_net_hdr *)&txr[idx].tx_hdr;
340 idx = start_dp[idx].next;
343 /* Checksum Offload / TSO */
345 if (cookie->ol_flags & PKT_TX_TCP_SEG)
346 cookie->ol_flags |= PKT_TX_TCP_CKSUM;
348 switch (cookie->ol_flags & PKT_TX_L4_MASK) {
349 case PKT_TX_UDP_CKSUM:
350 hdr->csum_start = cookie->l2_len + cookie->l3_len;
351 hdr->csum_offset = offsetof(struct udp_hdr,
353 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
356 case PKT_TX_TCP_CKSUM:
357 hdr->csum_start = cookie->l2_len + cookie->l3_len;
358 hdr->csum_offset = offsetof(struct tcp_hdr, cksum);
359 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
363 ASSIGN_UNLESS_EQUAL(hdr->csum_start, 0);
364 ASSIGN_UNLESS_EQUAL(hdr->csum_offset, 0);
365 ASSIGN_UNLESS_EQUAL(hdr->flags, 0);
369 /* TCP Segmentation Offload */
370 if (cookie->ol_flags & PKT_TX_TCP_SEG) {
371 virtio_tso_fix_cksum(cookie);
372 hdr->gso_type = (cookie->ol_flags & PKT_TX_IPV6) ?
373 VIRTIO_NET_HDR_GSO_TCPV6 :
374 VIRTIO_NET_HDR_GSO_TCPV4;
375 hdr->gso_size = cookie->tso_segsz;
381 ASSIGN_UNLESS_EQUAL(hdr->gso_type, 0);
382 ASSIGN_UNLESS_EQUAL(hdr->gso_size, 0);
383 ASSIGN_UNLESS_EQUAL(hdr->hdr_len, 0);
388 start_dp[idx].addr = VIRTIO_MBUF_DATA_DMA_ADDR(cookie, vq);
389 start_dp[idx].len = cookie->data_len;
390 start_dp[idx].flags = cookie->next ? VRING_DESC_F_NEXT : 0;
391 idx = start_dp[idx].next;
392 } while ((cookie = cookie->next) != NULL);
395 idx = vq->vq_ring.desc[head_idx].next;
397 vq->vq_desc_head_idx = idx;
398 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
399 vq->vq_desc_tail_idx = idx;
400 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
401 vq_update_avail_ring(vq, head_idx);
405 virtio_dev_cq_start(struct rte_eth_dev *dev)
407 struct virtio_hw *hw = dev->data->dev_private;
409 if (hw->cvq && hw->cvq->vq) {
410 VIRTQUEUE_DUMP((struct virtqueue *)hw->cvq->vq);
415 virtio_dev_rx_queue_setup(struct rte_eth_dev *dev,
418 unsigned int socket_id __rte_unused,
419 __rte_unused const struct rte_eth_rxconf *rx_conf,
420 struct rte_mempool *mp)
422 uint16_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
423 struct virtio_hw *hw = dev->data->dev_private;
424 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
425 struct virtnet_rx *rxvq;
427 PMD_INIT_FUNC_TRACE();
429 if (nb_desc == 0 || nb_desc > vq->vq_nentries)
430 nb_desc = vq->vq_nentries;
431 vq->vq_free_cnt = RTE_MIN(vq->vq_free_cnt, nb_desc);
434 rxvq->queue_id = queue_idx;
436 if (rxvq->mpool == NULL) {
437 rte_exit(EXIT_FAILURE,
438 "Cannot allocate mbufs for rx virtqueue");
440 dev->data->rx_queues[queue_idx] = rxvq;
446 virtio_dev_rx_queue_setup_finish(struct rte_eth_dev *dev, uint16_t queue_idx)
448 uint16_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
449 struct virtio_hw *hw = dev->data->dev_private;
450 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
451 struct virtnet_rx *rxvq = &vq->rxq;
456 PMD_INIT_FUNC_TRACE();
458 /* Allocate blank mbufs for the each rx descriptor */
461 if (hw->use_simple_rx) {
462 for (desc_idx = 0; desc_idx < vq->vq_nentries;
464 vq->vq_ring.avail->ring[desc_idx] = desc_idx;
465 vq->vq_ring.desc[desc_idx].flags =
470 memset(&rxvq->fake_mbuf, 0, sizeof(rxvq->fake_mbuf));
471 for (desc_idx = 0; desc_idx < RTE_PMD_VIRTIO_RX_MAX_BURST;
473 vq->sw_ring[vq->vq_nentries + desc_idx] =
477 while (!virtqueue_full(vq)) {
478 m = rte_mbuf_raw_alloc(rxvq->mpool);
482 /* Enqueue allocated buffers */
483 if (hw->use_simple_rx)
484 error = virtqueue_enqueue_recv_refill_simple(vq, m);
486 error = virtqueue_enqueue_recv_refill(vq, m);
495 vq_update_avail_idx(vq);
497 PMD_INIT_LOG(DEBUG, "Allocated %d bufs", nbufs);
499 virtio_rxq_vec_setup(rxvq);
507 * struct rte_eth_dev *dev: Used to update dev
508 * uint16_t nb_desc: Defaults to values read from config space
509 * unsigned int socket_id: Used to allocate memzone
510 * const struct rte_eth_txconf *tx_conf: Used to setup tx engine
511 * uint16_t queue_idx: Just used as an index in dev txq list
514 virtio_dev_tx_queue_setup(struct rte_eth_dev *dev,
517 unsigned int socket_id __rte_unused,
518 const struct rte_eth_txconf *tx_conf)
520 uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
521 struct virtio_hw *hw = dev->data->dev_private;
522 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
523 struct virtnet_tx *txvq;
524 uint16_t tx_free_thresh;
526 PMD_INIT_FUNC_TRACE();
528 /* cannot use simple rxtx funcs with multisegs or offloads */
529 if ((tx_conf->txq_flags & VIRTIO_SIMPLE_FLAGS) != VIRTIO_SIMPLE_FLAGS)
530 hw->use_simple_tx = 0;
532 if (nb_desc == 0 || nb_desc > vq->vq_nentries)
533 nb_desc = vq->vq_nentries;
534 vq->vq_free_cnt = RTE_MIN(vq->vq_free_cnt, nb_desc);
537 txvq->queue_id = queue_idx;
539 tx_free_thresh = tx_conf->tx_free_thresh;
540 if (tx_free_thresh == 0)
542 RTE_MIN(vq->vq_nentries / 4, DEFAULT_TX_FREE_THRESH);
544 if (tx_free_thresh >= (vq->vq_nentries - 3)) {
545 RTE_LOG(ERR, PMD, "tx_free_thresh must be less than the "
546 "number of TX entries minus 3 (%u)."
547 " (tx_free_thresh=%u port=%u queue=%u)\n",
549 tx_free_thresh, dev->data->port_id, queue_idx);
553 vq->vq_free_thresh = tx_free_thresh;
555 dev->data->tx_queues[queue_idx] = txvq;
560 virtio_dev_tx_queue_setup_finish(struct rte_eth_dev *dev,
563 uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
564 struct virtio_hw *hw = dev->data->dev_private;
565 struct virtqueue *vq = hw->vqs[vtpci_queue_idx];
566 uint16_t mid_idx = vq->vq_nentries >> 1;
567 struct virtnet_tx *txvq = &vq->txq;
570 PMD_INIT_FUNC_TRACE();
572 if (hw->use_simple_tx) {
573 for (desc_idx = 0; desc_idx < mid_idx; desc_idx++) {
574 vq->vq_ring.avail->ring[desc_idx] =
576 vq->vq_ring.desc[desc_idx + mid_idx].next =
578 vq->vq_ring.desc[desc_idx + mid_idx].addr =
579 txvq->virtio_net_hdr_mem +
580 offsetof(struct virtio_tx_region, tx_hdr);
581 vq->vq_ring.desc[desc_idx + mid_idx].len =
582 vq->hw->vtnet_hdr_size;
583 vq->vq_ring.desc[desc_idx + mid_idx].flags =
585 vq->vq_ring.desc[desc_idx].flags = 0;
587 for (desc_idx = mid_idx; desc_idx < vq->vq_nentries;
589 vq->vq_ring.avail->ring[desc_idx] = desc_idx;
598 virtio_discard_rxbuf(struct virtqueue *vq, struct rte_mbuf *m)
602 * Requeue the discarded mbuf. This should always be
603 * successful since it was just dequeued.
605 error = virtqueue_enqueue_recv_refill(vq, m);
606 if (unlikely(error)) {
607 RTE_LOG(ERR, PMD, "cannot requeue discarded mbuf");
613 virtio_update_packet_stats(struct virtnet_stats *stats, struct rte_mbuf *mbuf)
615 uint32_t s = mbuf->pkt_len;
616 struct ether_addr *ea;
619 stats->size_bins[1]++;
620 } else if (s > 64 && s < 1024) {
623 /* count zeros, and offset into correct bin */
624 bin = (sizeof(s) * 8) - __builtin_clz(s) - 5;
625 stats->size_bins[bin]++;
628 stats->size_bins[0]++;
630 stats->size_bins[6]++;
632 stats->size_bins[7]++;
635 ea = rte_pktmbuf_mtod(mbuf, struct ether_addr *);
636 if (is_multicast_ether_addr(ea)) {
637 if (is_broadcast_ether_addr(ea))
644 /* Optionally fill offload information in structure */
646 virtio_rx_offload(struct rte_mbuf *m, struct virtio_net_hdr *hdr)
648 struct rte_net_hdr_lens hdr_lens;
649 uint32_t hdrlen, ptype;
650 int l4_supported = 0;
653 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
656 m->ol_flags |= PKT_RX_IP_CKSUM_UNKNOWN;
658 ptype = rte_net_get_ptype(m, &hdr_lens, RTE_PTYPE_ALL_MASK);
659 m->packet_type = ptype;
660 if ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP ||
661 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP ||
662 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_SCTP)
665 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
666 hdrlen = hdr_lens.l2_len + hdr_lens.l3_len + hdr_lens.l4_len;
667 if (hdr->csum_start <= hdrlen && l4_supported) {
668 m->ol_flags |= PKT_RX_L4_CKSUM_NONE;
670 /* Unknown proto or tunnel, do sw cksum. We can assume
671 * the cksum field is in the first segment since the
672 * buffers we provided to the host are large enough.
673 * In case of SCTP, this will be wrong since it's a CRC
674 * but there's nothing we can do.
676 uint16_t csum = 0, off;
678 rte_raw_cksum_mbuf(m, hdr->csum_start,
679 rte_pktmbuf_pkt_len(m) - hdr->csum_start,
681 if (likely(csum != 0xffff))
683 off = hdr->csum_offset + hdr->csum_start;
684 if (rte_pktmbuf_data_len(m) >= off + 1)
685 *rte_pktmbuf_mtod_offset(m, uint16_t *,
688 } else if (hdr->flags & VIRTIO_NET_HDR_F_DATA_VALID && l4_supported) {
689 m->ol_flags |= PKT_RX_L4_CKSUM_GOOD;
692 /* GSO request, save required information in mbuf */
693 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
694 /* Check unsupported modes */
695 if ((hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN) ||
696 (hdr->gso_size == 0)) {
700 /* Update mss lengthes in mbuf */
701 m->tso_segsz = hdr->gso_size;
702 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
703 case VIRTIO_NET_HDR_GSO_TCPV4:
704 case VIRTIO_NET_HDR_GSO_TCPV6:
705 m->ol_flags |= PKT_RX_LRO | \
706 PKT_RX_L4_CKSUM_NONE;
717 rx_offload_enabled(struct virtio_hw *hw)
719 return vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM) ||
720 vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
721 vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6);
724 #define VIRTIO_MBUF_BURST_SZ 64
725 #define DESC_PER_CACHELINE (RTE_CACHE_LINE_SIZE / sizeof(struct vring_desc))
727 virtio_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
729 struct virtnet_rx *rxvq = rx_queue;
730 struct virtqueue *vq = rxvq->vq;
731 struct virtio_hw *hw = vq->hw;
732 struct rte_mbuf *rxm, *new_mbuf;
733 uint16_t nb_used, num, nb_rx;
734 uint32_t len[VIRTIO_MBUF_BURST_SZ];
735 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
737 uint32_t i, nb_enqueued;
740 struct virtio_net_hdr *hdr;
743 if (unlikely(hw->started == 0))
746 nb_used = VIRTQUEUE_NUSED(vq);
750 num = likely(nb_used <= nb_pkts) ? nb_used : nb_pkts;
751 if (unlikely(num > VIRTIO_MBUF_BURST_SZ))
752 num = VIRTIO_MBUF_BURST_SZ;
753 if (likely(num > DESC_PER_CACHELINE))
754 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
756 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, num);
757 PMD_RX_LOG(DEBUG, "used:%d dequeue:%d", nb_used, num);
760 hdr_size = hw->vtnet_hdr_size;
761 offload = rx_offload_enabled(hw);
763 for (i = 0; i < num ; i++) {
766 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
768 if (unlikely(len[i] < hdr_size + ETHER_HDR_LEN)) {
769 PMD_RX_LOG(ERR, "Packet drop");
771 virtio_discard_rxbuf(vq, rxm);
772 rxvq->stats.errors++;
776 rxm->port = rxvq->port_id;
777 rxm->data_off = RTE_PKTMBUF_HEADROOM;
781 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
782 rxm->data_len = (uint16_t)(len[i] - hdr_size);
784 hdr = (struct virtio_net_hdr *)((char *)rxm->buf_addr +
785 RTE_PKTMBUF_HEADROOM - hdr_size);
790 if (offload && virtio_rx_offload(rxm, hdr) < 0) {
791 virtio_discard_rxbuf(vq, rxm);
792 rxvq->stats.errors++;
796 VIRTIO_DUMP_PACKET(rxm, rxm->data_len);
798 rx_pkts[nb_rx++] = rxm;
800 rxvq->stats.bytes += rxm->pkt_len;
801 virtio_update_packet_stats(&rxvq->stats, rxm);
804 rxvq->stats.packets += nb_rx;
806 /* Allocate new mbuf for the used descriptor */
808 while (likely(!virtqueue_full(vq))) {
809 new_mbuf = rte_mbuf_raw_alloc(rxvq->mpool);
810 if (unlikely(new_mbuf == NULL)) {
811 struct rte_eth_dev *dev
812 = &rte_eth_devices[rxvq->port_id];
813 dev->data->rx_mbuf_alloc_failed++;
816 error = virtqueue_enqueue_recv_refill(vq, new_mbuf);
817 if (unlikely(error)) {
818 rte_pktmbuf_free(new_mbuf);
824 if (likely(nb_enqueued)) {
825 vq_update_avail_idx(vq);
827 if (unlikely(virtqueue_kick_prepare(vq))) {
828 virtqueue_notify(vq);
829 PMD_RX_LOG(DEBUG, "Notified");
837 virtio_recv_mergeable_pkts(void *rx_queue,
838 struct rte_mbuf **rx_pkts,
841 struct virtnet_rx *rxvq = rx_queue;
842 struct virtqueue *vq = rxvq->vq;
843 struct virtio_hw *hw = vq->hw;
844 struct rte_mbuf *rxm, *new_mbuf;
845 uint16_t nb_used, num, nb_rx;
846 uint32_t len[VIRTIO_MBUF_BURST_SZ];
847 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
848 struct rte_mbuf *prev;
850 uint32_t i, nb_enqueued;
858 if (unlikely(hw->started == 0))
861 nb_used = VIRTQUEUE_NUSED(vq);
865 PMD_RX_LOG(DEBUG, "used:%d", nb_used);
872 hdr_size = hw->vtnet_hdr_size;
873 offload = rx_offload_enabled(hw);
875 while (i < nb_used) {
876 struct virtio_net_hdr_mrg_rxbuf *header;
878 if (nb_rx == nb_pkts)
881 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, 1);
887 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
888 PMD_RX_LOG(DEBUG, "packet len:%d", len[0]);
892 if (unlikely(len[0] < hdr_size + ETHER_HDR_LEN)) {
893 PMD_RX_LOG(ERR, "Packet drop");
895 virtio_discard_rxbuf(vq, rxm);
896 rxvq->stats.errors++;
900 header = (struct virtio_net_hdr_mrg_rxbuf *)((char *)rxm->buf_addr +
901 RTE_PKTMBUF_HEADROOM - hdr_size);
902 seg_num = header->num_buffers;
907 rxm->data_off = RTE_PKTMBUF_HEADROOM;
908 rxm->nb_segs = seg_num;
911 rxm->pkt_len = (uint32_t)(len[0] - hdr_size);
912 rxm->data_len = (uint16_t)(len[0] - hdr_size);
914 rxm->port = rxvq->port_id;
915 rx_pkts[nb_rx] = rxm;
918 if (offload && virtio_rx_offload(rxm, &header->hdr) < 0) {
919 virtio_discard_rxbuf(vq, rxm);
920 rxvq->stats.errors++;
924 seg_res = seg_num - 1;
926 while (seg_res != 0) {
928 * Get extra segments for current uncompleted packet.
931 RTE_MIN(seg_res, RTE_DIM(rcv_pkts));
932 if (likely(VIRTQUEUE_NUSED(vq) >= rcv_cnt)) {
934 virtqueue_dequeue_burst_rx(vq,
935 rcv_pkts, len, rcv_cnt);
940 "No enough segments for packet.");
942 virtio_discard_rxbuf(vq, rxm);
943 rxvq->stats.errors++;
949 while (extra_idx < rcv_cnt) {
950 rxm = rcv_pkts[extra_idx];
952 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
953 rxm->pkt_len = (uint32_t)(len[extra_idx]);
954 rxm->data_len = (uint16_t)(len[extra_idx]);
960 rx_pkts[nb_rx]->pkt_len += rxm->pkt_len;
967 rte_vlan_strip(rx_pkts[nb_rx]);
969 VIRTIO_DUMP_PACKET(rx_pkts[nb_rx],
970 rx_pkts[nb_rx]->data_len);
972 rxvq->stats.bytes += rx_pkts[nb_rx]->pkt_len;
973 virtio_update_packet_stats(&rxvq->stats, rx_pkts[nb_rx]);
977 rxvq->stats.packets += nb_rx;
979 /* Allocate new mbuf for the used descriptor */
981 while (likely(!virtqueue_full(vq))) {
982 new_mbuf = rte_mbuf_raw_alloc(rxvq->mpool);
983 if (unlikely(new_mbuf == NULL)) {
984 struct rte_eth_dev *dev
985 = &rte_eth_devices[rxvq->port_id];
986 dev->data->rx_mbuf_alloc_failed++;
989 error = virtqueue_enqueue_recv_refill(vq, new_mbuf);
990 if (unlikely(error)) {
991 rte_pktmbuf_free(new_mbuf);
997 if (likely(nb_enqueued)) {
998 vq_update_avail_idx(vq);
1000 if (unlikely(virtqueue_kick_prepare(vq))) {
1001 virtqueue_notify(vq);
1002 PMD_RX_LOG(DEBUG, "Notified");
1010 virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
1012 struct virtnet_tx *txvq = tx_queue;
1013 struct virtqueue *vq = txvq->vq;
1014 struct virtio_hw *hw = vq->hw;
1015 uint16_t hdr_size = hw->vtnet_hdr_size;
1016 uint16_t nb_used, nb_tx = 0;
1019 if (unlikely(hw->started == 0))
1022 if (unlikely(nb_pkts < 1))
1025 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
1026 nb_used = VIRTQUEUE_NUSED(vq);
1029 if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh))
1030 virtio_xmit_cleanup(vq, nb_used);
1032 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
1033 struct rte_mbuf *txm = tx_pkts[nb_tx];
1034 int can_push = 0, use_indirect = 0, slots, need;
1036 /* Do VLAN tag insertion */
1037 if (unlikely(txm->ol_flags & PKT_TX_VLAN_PKT)) {
1038 error = rte_vlan_insert(&txm);
1039 if (unlikely(error)) {
1040 rte_pktmbuf_free(txm);
1045 /* optimize ring usage */
1046 if ((vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) ||
1047 vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) &&
1048 rte_mbuf_refcnt_read(txm) == 1 &&
1049 RTE_MBUF_DIRECT(txm) &&
1050 txm->nb_segs == 1 &&
1051 rte_pktmbuf_headroom(txm) >= hdr_size &&
1052 rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
1053 __alignof__(struct virtio_net_hdr_mrg_rxbuf)))
1055 else if (vtpci_with_feature(hw, VIRTIO_RING_F_INDIRECT_DESC) &&
1056 txm->nb_segs < VIRTIO_MAX_TX_INDIRECT)
1059 /* How many main ring entries are needed to this Tx?
1060 * any_layout => number of segments
1062 * default => number of segments + 1
1064 slots = use_indirect ? 1 : (txm->nb_segs + !can_push);
1065 need = slots - vq->vq_free_cnt;
1067 /* Positive value indicates it need free vring descriptors */
1068 if (unlikely(need > 0)) {
1069 nb_used = VIRTQUEUE_NUSED(vq);
1071 need = RTE_MIN(need, (int)nb_used);
1073 virtio_xmit_cleanup(vq, need);
1074 need = slots - vq->vq_free_cnt;
1075 if (unlikely(need > 0)) {
1077 "No free tx descriptors to transmit");
1082 /* Enqueue Packet buffers */
1083 virtqueue_enqueue_xmit(txvq, txm, slots, use_indirect, can_push);
1085 txvq->stats.bytes += txm->pkt_len;
1086 virtio_update_packet_stats(&txvq->stats, txm);
1089 txvq->stats.packets += nb_tx;
1091 if (likely(nb_tx)) {
1092 vq_update_avail_idx(vq);
1094 if (unlikely(virtqueue_kick_prepare(vq))) {
1095 virtqueue_notify(vq);
1096 PMD_TX_LOG(DEBUG, "Notified backend after xmit");