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_memzone.h>
43 #include <rte_branch_prediction.h>
44 #include <rte_mempool.h>
45 #include <rte_malloc.h>
47 #include <rte_ether.h>
48 #include <rte_ethdev.h>
49 #include <rte_prefetch.h>
50 #include <rte_string_fns.h>
51 #include <rte_errno.h>
52 #include <rte_byteorder.h>
53 #include <rte_cpuflags.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 vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx)
76 struct vring_desc *dp, *dp_tail;
77 struct vq_desc_extra *dxp;
78 uint16_t desc_idx_last = desc_idx;
80 dp = &vq->vq_ring.desc[desc_idx];
81 dxp = &vq->vq_descx[desc_idx];
82 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt + dxp->ndescs);
83 if ((dp->flags & VRING_DESC_F_INDIRECT) == 0) {
84 while (dp->flags & VRING_DESC_F_NEXT) {
85 desc_idx_last = dp->next;
86 dp = &vq->vq_ring.desc[dp->next];
92 * We must append the existing free chain, if any, to the end of
93 * newly freed chain. If the virtqueue was completely used, then
94 * head would be VQ_RING_DESC_CHAIN_END (ASSERTed above).
96 if (vq->vq_desc_tail_idx == VQ_RING_DESC_CHAIN_END) {
97 vq->vq_desc_head_idx = desc_idx;
99 dp_tail = &vq->vq_ring.desc[vq->vq_desc_tail_idx];
100 dp_tail->next = desc_idx;
103 vq->vq_desc_tail_idx = desc_idx_last;
104 dp->next = VQ_RING_DESC_CHAIN_END;
108 virtqueue_dequeue_burst_rx(struct virtqueue *vq, struct rte_mbuf **rx_pkts,
109 uint32_t *len, uint16_t num)
111 struct vring_used_elem *uep;
112 struct rte_mbuf *cookie;
113 uint16_t used_idx, desc_idx;
116 /* Caller does the check */
117 for (i = 0; i < num ; i++) {
118 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
119 uep = &vq->vq_ring.used->ring[used_idx];
120 desc_idx = (uint16_t) uep->id;
122 cookie = (struct rte_mbuf *)vq->vq_descx[desc_idx].cookie;
124 if (unlikely(cookie == NULL)) {
125 PMD_DRV_LOG(ERR, "vring descriptor with no mbuf cookie at %u\n",
126 vq->vq_used_cons_idx);
130 rte_prefetch0(cookie);
131 rte_packet_prefetch(rte_pktmbuf_mtod(cookie, void *));
133 vq->vq_used_cons_idx++;
134 vq_ring_free_chain(vq, desc_idx);
135 vq->vq_descx[desc_idx].cookie = NULL;
141 #ifndef DEFAULT_TX_FREE_THRESH
142 #define DEFAULT_TX_FREE_THRESH 32
145 /* Cleanup from completed transmits. */
147 virtio_xmit_cleanup(struct virtqueue *vq, uint16_t num)
149 uint16_t i, used_idx, desc_idx;
150 for (i = 0; i < num; i++) {
151 struct vring_used_elem *uep;
152 struct vq_desc_extra *dxp;
154 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
155 uep = &vq->vq_ring.used->ring[used_idx];
157 desc_idx = (uint16_t) uep->id;
158 dxp = &vq->vq_descx[desc_idx];
159 vq->vq_used_cons_idx++;
160 vq_ring_free_chain(vq, desc_idx);
162 if (dxp->cookie != NULL) {
163 rte_pktmbuf_free(dxp->cookie);
171 virtqueue_enqueue_recv_refill(struct virtqueue *vq, struct rte_mbuf *cookie)
173 struct vq_desc_extra *dxp;
174 struct virtio_hw *hw = vq->hw;
175 struct vring_desc *start_dp;
177 uint16_t head_idx, idx;
179 if (unlikely(vq->vq_free_cnt == 0))
181 if (unlikely(vq->vq_free_cnt < needed))
184 head_idx = vq->vq_desc_head_idx;
185 if (unlikely(head_idx >= vq->vq_nentries))
189 dxp = &vq->vq_descx[idx];
190 dxp->cookie = (void *)cookie;
191 dxp->ndescs = needed;
193 start_dp = vq->vq_ring.desc;
195 VIRTIO_MBUF_ADDR(cookie, vq) +
196 RTE_PKTMBUF_HEADROOM - hw->vtnet_hdr_size;
198 cookie->buf_len - RTE_PKTMBUF_HEADROOM + hw->vtnet_hdr_size;
199 start_dp[idx].flags = VRING_DESC_F_WRITE;
200 idx = start_dp[idx].next;
201 vq->vq_desc_head_idx = idx;
202 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
203 vq->vq_desc_tail_idx = idx;
204 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
205 vq_update_avail_ring(vq, head_idx);
211 virtqueue_enqueue_xmit(struct virtnet_tx *txvq, struct rte_mbuf *cookie,
212 uint16_t needed, int use_indirect, int can_push)
214 struct vq_desc_extra *dxp;
215 struct virtqueue *vq = txvq->vq;
216 struct vring_desc *start_dp;
217 uint16_t seg_num = cookie->nb_segs;
218 uint16_t head_idx, idx;
219 uint16_t head_size = vq->hw->vtnet_hdr_size;
222 head_idx = vq->vq_desc_head_idx;
224 dxp = &vq->vq_descx[idx];
225 dxp->cookie = (void *)cookie;
226 dxp->ndescs = needed;
228 start_dp = vq->vq_ring.desc;
231 /* put on zero'd transmit header (no offloads) */
232 void *hdr = rte_pktmbuf_prepend(cookie, head_size);
234 memset(hdr, 0, head_size);
235 } else if (use_indirect) {
236 /* setup tx ring slot to point to indirect
237 * descriptor list stored in reserved region.
239 * the first slot in indirect ring is already preset
240 * to point to the header in reserved region
242 struct virtio_tx_region *txr = txvq->virtio_net_hdr_mz->addr;
244 offs = idx * sizeof(struct virtio_tx_region)
245 + offsetof(struct virtio_tx_region, tx_indir);
247 start_dp[idx].addr = txvq->virtio_net_hdr_mem + offs;
248 start_dp[idx].len = (seg_num + 1) * sizeof(struct vring_desc);
249 start_dp[idx].flags = VRING_DESC_F_INDIRECT;
251 /* loop below will fill in rest of the indirect elements */
252 start_dp = txr[idx].tx_indir;
255 /* setup first tx ring slot to point to header
256 * stored in reserved region.
258 offs = idx * sizeof(struct virtio_tx_region)
259 + offsetof(struct virtio_tx_region, tx_hdr);
261 start_dp[idx].addr = txvq->virtio_net_hdr_mem + offs;
262 start_dp[idx].len = vq->hw->vtnet_hdr_size;
263 start_dp[idx].flags = VRING_DESC_F_NEXT;
264 idx = start_dp[idx].next;
268 start_dp[idx].addr = VIRTIO_MBUF_DATA_DMA_ADDR(cookie, vq);
269 start_dp[idx].len = cookie->data_len;
270 start_dp[idx].flags = cookie->next ? VRING_DESC_F_NEXT : 0;
271 idx = start_dp[idx].next;
272 } while ((cookie = cookie->next) != NULL);
275 idx = vq->vq_ring.desc[head_idx].next;
277 vq->vq_desc_head_idx = idx;
278 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
279 vq->vq_desc_tail_idx = idx;
280 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
281 vq_update_avail_ring(vq, head_idx);
285 virtio_dev_vring_start(struct virtqueue *vq)
287 int size = vq->vq_nentries;
288 struct vring *vr = &vq->vq_ring;
289 uint8_t *ring_mem = vq->vq_ring_virt_mem;
291 PMD_INIT_FUNC_TRACE();
294 * Reinitialise since virtio port might have been stopped and restarted
296 memset(vq->vq_ring_virt_mem, 0, vq->vq_ring_size);
297 vring_init(vr, size, ring_mem, VIRTIO_PCI_VRING_ALIGN);
298 vq->vq_used_cons_idx = 0;
299 vq->vq_desc_head_idx = 0;
300 vq->vq_avail_idx = 0;
301 vq->vq_desc_tail_idx = (uint16_t)(vq->vq_nentries - 1);
302 vq->vq_free_cnt = vq->vq_nentries;
303 memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries);
305 vring_desc_init(vr->desc, size);
308 * Disable device(host) interrupting guest
310 virtqueue_disable_intr(vq);
314 virtio_dev_cq_start(struct rte_eth_dev *dev)
316 struct virtio_hw *hw = dev->data->dev_private;
318 if (hw->cvq && hw->cvq->vq) {
319 virtio_dev_vring_start(hw->cvq->vq);
320 VIRTQUEUE_DUMP((struct virtqueue *)hw->cvq->vq);
325 virtio_dev_rxtx_start(struct rte_eth_dev *dev)
328 * Start receive and transmit vrings
329 * - Setup vring structure for all queues
330 * - Initialize descriptor for the rx vring
331 * - Allocate blank mbufs for the each rx descriptor
336 struct virtio_hw *hw = dev->data->dev_private;
338 PMD_INIT_FUNC_TRACE();
340 /* Start rx vring. */
341 for (i = 0; i < dev->data->nb_rx_queues; i++) {
342 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
343 struct virtqueue *vq = rxvq->vq;
347 virtio_dev_vring_start(vq);
348 if (rxvq->mpool == NULL) {
349 rte_exit(EXIT_FAILURE,
350 "Cannot allocate mbufs for rx virtqueue");
353 /* Allocate blank mbufs for the each rx descriptor */
357 if (hw->use_simple_rxtx) {
358 for (desc_idx = 0; desc_idx < vq->vq_nentries;
360 vq->vq_ring.avail->ring[desc_idx] = desc_idx;
361 vq->vq_ring.desc[desc_idx].flags =
366 memset(&rxvq->fake_mbuf, 0, sizeof(rxvq->fake_mbuf));
367 for (desc_idx = 0; desc_idx < RTE_PMD_VIRTIO_RX_MAX_BURST;
369 vq->sw_ring[vq->vq_nentries + desc_idx] =
373 while (!virtqueue_full(vq)) {
374 m = rte_mbuf_raw_alloc(rxvq->mpool);
378 /******************************************
379 * Enqueue allocated buffers *
380 *******************************************/
381 if (hw->use_simple_rxtx)
382 error = virtqueue_enqueue_recv_refill_simple(vq, m);
384 error = virtqueue_enqueue_recv_refill(vq, m);
393 vq_update_avail_idx(vq);
395 PMD_INIT_LOG(DEBUG, "Allocated %d bufs", nbufs);
400 /* Start tx vring. */
401 for (i = 0; i < dev->data->nb_tx_queues; i++) {
402 struct virtnet_tx *txvq = dev->data->tx_queues[i];
403 struct virtqueue *vq = txvq->vq;
405 virtio_dev_vring_start(vq);
406 if (hw->use_simple_rxtx) {
407 uint16_t mid_idx = vq->vq_nentries >> 1;
409 for (desc_idx = 0; desc_idx < mid_idx; desc_idx++) {
410 vq->vq_ring.avail->ring[desc_idx] =
412 vq->vq_ring.desc[desc_idx + mid_idx].next =
414 vq->vq_ring.desc[desc_idx + mid_idx].addr =
415 txvq->virtio_net_hdr_mem +
416 offsetof(struct virtio_tx_region, tx_hdr);
417 vq->vq_ring.desc[desc_idx + mid_idx].len =
418 vq->hw->vtnet_hdr_size;
419 vq->vq_ring.desc[desc_idx + mid_idx].flags =
421 vq->vq_ring.desc[desc_idx].flags = 0;
423 for (desc_idx = mid_idx; desc_idx < vq->vq_nentries;
425 vq->vq_ring.avail->ring[desc_idx] = desc_idx;
433 virtio_dev_rx_queue_setup(struct rte_eth_dev *dev,
436 unsigned int socket_id,
437 __rte_unused const struct rte_eth_rxconf *rx_conf,
438 struct rte_mempool *mp)
440 uint16_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
441 struct virtnet_rx *rxvq;
444 PMD_INIT_FUNC_TRACE();
445 ret = virtio_dev_queue_setup(dev, VTNET_RQ, queue_idx, vtpci_queue_idx,
446 nb_desc, socket_id, (void **)&rxvq);
448 PMD_INIT_LOG(ERR, "rvq initialization failed");
452 /* Create mempool for rx mbuf allocation */
455 dev->data->rx_queues[queue_idx] = rxvq;
457 virtio_rxq_vec_setup(rxvq);
463 virtio_dev_rx_queue_release(void *rxq)
465 struct virtnet_rx *rxvq = rxq;
466 struct virtqueue *vq;
467 const struct rte_memzone *mz;
473 * rxvq is freed when vq is freed, and as mz should be freed after the
474 * del_queue, so we reserve the mz pointer first.
479 virtio_dev_queue_release(vq);
480 rte_memzone_free(mz);
484 virtio_update_rxtx_handler(struct rte_eth_dev *dev,
485 const struct rte_eth_txconf *tx_conf)
487 uint8_t use_simple_rxtx = 0;
488 struct virtio_hw *hw = dev->data->dev_private;
490 #if defined RTE_ARCH_X86
491 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE3))
493 #elif defined RTE_ARCH_ARM64 || defined CONFIG_RTE_ARCH_ARM
494 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON))
497 /* Use simple rx/tx func if single segment and no offloads */
498 if (use_simple_rxtx &&
499 (tx_conf->txq_flags & VIRTIO_SIMPLE_FLAGS) == VIRTIO_SIMPLE_FLAGS &&
500 !vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
501 PMD_INIT_LOG(INFO, "Using simple rx/tx path");
502 dev->tx_pkt_burst = virtio_xmit_pkts_simple;
503 dev->rx_pkt_burst = virtio_recv_pkts_vec;
504 hw->use_simple_rxtx = use_simple_rxtx;
509 * struct rte_eth_dev *dev: Used to update dev
510 * uint16_t nb_desc: Defaults to values read from config space
511 * unsigned int socket_id: Used to allocate memzone
512 * const struct rte_eth_txconf *tx_conf: Used to setup tx engine
513 * uint16_t queue_idx: Just used as an index in dev txq list
516 virtio_dev_tx_queue_setup(struct rte_eth_dev *dev,
519 unsigned int socket_id,
520 const struct rte_eth_txconf *tx_conf)
522 uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
523 struct virtnet_tx *txvq;
524 struct virtqueue *vq;
525 uint16_t tx_free_thresh;
528 PMD_INIT_FUNC_TRACE();
530 if ((tx_conf->txq_flags & ETH_TXQ_FLAGS_NOXSUMS)
531 != ETH_TXQ_FLAGS_NOXSUMS) {
532 PMD_INIT_LOG(ERR, "TX checksum offload not supported\n");
536 virtio_update_rxtx_handler(dev, tx_conf);
538 ret = virtio_dev_queue_setup(dev, VTNET_TQ, queue_idx, vtpci_queue_idx,
539 nb_desc, socket_id, (void **)&txvq);
541 PMD_INIT_LOG(ERR, "tvq initialization failed");
546 tx_free_thresh = tx_conf->tx_free_thresh;
547 if (tx_free_thresh == 0)
549 RTE_MIN(vq->vq_nentries / 4, DEFAULT_TX_FREE_THRESH);
551 if (tx_free_thresh >= (vq->vq_nentries - 3)) {
552 RTE_LOG(ERR, PMD, "tx_free_thresh must be less than the "
553 "number of TX entries minus 3 (%u)."
554 " (tx_free_thresh=%u port=%u queue=%u)\n",
556 tx_free_thresh, dev->data->port_id, queue_idx);
560 vq->vq_free_thresh = tx_free_thresh;
562 dev->data->tx_queues[queue_idx] = txvq;
567 virtio_dev_tx_queue_release(void *txq)
569 struct virtnet_tx *txvq = txq;
570 struct virtqueue *vq;
571 const struct rte_memzone *mz;
572 const struct rte_memzone *hdr_mz;
578 * txvq is freed when vq is freed, and as mz should be freed after the
579 * del_queue, so we reserve the mz pointer first.
583 hdr_mz = txvq->virtio_net_hdr_mz;
585 virtio_dev_queue_release(vq);
586 rte_memzone_free(mz);
587 rte_memzone_free(hdr_mz);
591 virtio_discard_rxbuf(struct virtqueue *vq, struct rte_mbuf *m)
595 * Requeue the discarded mbuf. This should always be
596 * successful since it was just dequeued.
598 error = virtqueue_enqueue_recv_refill(vq, m);
599 if (unlikely(error)) {
600 RTE_LOG(ERR, PMD, "cannot requeue discarded mbuf");
606 virtio_update_packet_stats(struct virtnet_stats *stats, struct rte_mbuf *mbuf)
608 uint32_t s = mbuf->pkt_len;
609 struct ether_addr *ea;
612 stats->size_bins[1]++;
613 } else if (s > 64 && s < 1024) {
616 /* count zeros, and offset into correct bin */
617 bin = (sizeof(s) * 8) - __builtin_clz(s) - 5;
618 stats->size_bins[bin]++;
621 stats->size_bins[0]++;
623 stats->size_bins[6]++;
625 stats->size_bins[7]++;
628 ea = rte_pktmbuf_mtod(mbuf, struct ether_addr *);
629 if (is_multicast_ether_addr(ea)) {
630 if (is_broadcast_ether_addr(ea))
637 /* Optionally fill offload information in structure */
639 virtio_rx_offload(struct rte_mbuf *m, struct virtio_net_hdr *hdr)
641 struct rte_net_hdr_lens hdr_lens;
642 uint32_t hdrlen, ptype;
643 int l4_supported = 0;
646 if (hdr->flags == 0 && hdr->gso_type == VIRTIO_NET_HDR_GSO_NONE)
649 m->ol_flags |= PKT_RX_IP_CKSUM_UNKNOWN;
651 ptype = rte_net_get_ptype(m, &hdr_lens, RTE_PTYPE_ALL_MASK);
652 m->packet_type = ptype;
653 if ((ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_TCP ||
654 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP ||
655 (ptype & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_SCTP)
658 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
659 hdrlen = hdr_lens.l2_len + hdr_lens.l3_len + hdr_lens.l4_len;
660 if (hdr->csum_start <= hdrlen && l4_supported) {
661 m->ol_flags |= PKT_RX_L4_CKSUM_NONE;
663 /* Unknown proto or tunnel, do sw cksum. We can assume
664 * the cksum field is in the first segment since the
665 * buffers we provided to the host are large enough.
666 * In case of SCTP, this will be wrong since it's a CRC
667 * but there's nothing we can do.
671 rte_raw_cksum_mbuf(m, hdr->csum_start,
672 rte_pktmbuf_pkt_len(m) - hdr->csum_start,
674 if (likely(csum != 0xffff))
676 off = hdr->csum_offset + hdr->csum_start;
677 if (rte_pktmbuf_data_len(m) >= off + 1)
678 *rte_pktmbuf_mtod_offset(m, uint16_t *,
681 } else if (hdr->flags & VIRTIO_NET_HDR_F_DATA_VALID && l4_supported) {
682 m->ol_flags |= PKT_RX_L4_CKSUM_GOOD;
689 rx_offload_enabled(struct virtio_hw *hw)
691 return vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM);
694 #define VIRTIO_MBUF_BURST_SZ 64
695 #define DESC_PER_CACHELINE (RTE_CACHE_LINE_SIZE / sizeof(struct vring_desc))
697 virtio_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
699 struct virtnet_rx *rxvq = rx_queue;
700 struct virtqueue *vq = rxvq->vq;
701 struct virtio_hw *hw;
702 struct rte_mbuf *rxm, *new_mbuf;
703 uint16_t nb_used, num, nb_rx;
704 uint32_t len[VIRTIO_MBUF_BURST_SZ];
705 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
707 uint32_t i, nb_enqueued;
710 struct virtio_net_hdr *hdr;
712 nb_used = VIRTQUEUE_NUSED(vq);
716 num = (uint16_t)(likely(nb_used <= nb_pkts) ? nb_used : nb_pkts);
717 num = (uint16_t)(likely(num <= VIRTIO_MBUF_BURST_SZ) ? num : VIRTIO_MBUF_BURST_SZ);
718 if (likely(num > DESC_PER_CACHELINE))
719 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
721 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, num);
722 PMD_RX_LOG(DEBUG, "used:%d dequeue:%d", nb_used, num);
727 hdr_size = hw->vtnet_hdr_size;
728 offload = rx_offload_enabled(hw);
730 for (i = 0; i < num ; i++) {
733 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
735 if (unlikely(len[i] < hdr_size + ETHER_HDR_LEN)) {
736 PMD_RX_LOG(ERR, "Packet drop");
738 virtio_discard_rxbuf(vq, rxm);
739 rxvq->stats.errors++;
743 rxm->port = rxvq->port_id;
744 rxm->data_off = RTE_PKTMBUF_HEADROOM;
750 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
751 rxm->data_len = (uint16_t)(len[i] - hdr_size);
753 hdr = (struct virtio_net_hdr *)((char *)rxm->buf_addr +
754 RTE_PKTMBUF_HEADROOM - hdr_size);
759 if (offload && virtio_rx_offload(rxm, hdr) < 0) {
760 virtio_discard_rxbuf(vq, rxm);
761 rxvq->stats.errors++;
765 VIRTIO_DUMP_PACKET(rxm, rxm->data_len);
767 rx_pkts[nb_rx++] = rxm;
769 rxvq->stats.bytes += rx_pkts[nb_rx - 1]->pkt_len;
770 virtio_update_packet_stats(&rxvq->stats, rxm);
773 rxvq->stats.packets += nb_rx;
775 /* Allocate new mbuf for the used descriptor */
777 while (likely(!virtqueue_full(vq))) {
778 new_mbuf = rte_mbuf_raw_alloc(rxvq->mpool);
779 if (unlikely(new_mbuf == NULL)) {
780 struct rte_eth_dev *dev
781 = &rte_eth_devices[rxvq->port_id];
782 dev->data->rx_mbuf_alloc_failed++;
785 error = virtqueue_enqueue_recv_refill(vq, new_mbuf);
786 if (unlikely(error)) {
787 rte_pktmbuf_free(new_mbuf);
793 if (likely(nb_enqueued)) {
794 vq_update_avail_idx(vq);
796 if (unlikely(virtqueue_kick_prepare(vq))) {
797 virtqueue_notify(vq);
798 PMD_RX_LOG(DEBUG, "Notified");
806 virtio_recv_mergeable_pkts(void *rx_queue,
807 struct rte_mbuf **rx_pkts,
810 struct virtnet_rx *rxvq = rx_queue;
811 struct virtqueue *vq = rxvq->vq;
812 struct virtio_hw *hw;
813 struct rte_mbuf *rxm, *new_mbuf;
814 uint16_t nb_used, num, nb_rx;
815 uint32_t len[VIRTIO_MBUF_BURST_SZ];
816 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
817 struct rte_mbuf *prev;
819 uint32_t i, nb_enqueued;
826 nb_used = VIRTQUEUE_NUSED(vq);
830 PMD_RX_LOG(DEBUG, "used:%d", nb_used);
839 hdr_size = hw->vtnet_hdr_size;
840 offload = rx_offload_enabled(hw);
842 while (i < nb_used) {
843 struct virtio_net_hdr_mrg_rxbuf *header;
845 if (nb_rx == nb_pkts)
848 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, 1);
854 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
855 PMD_RX_LOG(DEBUG, "packet len:%d", len[0]);
859 if (unlikely(len[0] < hdr_size + ETHER_HDR_LEN)) {
860 PMD_RX_LOG(ERR, "Packet drop");
862 virtio_discard_rxbuf(vq, rxm);
863 rxvq->stats.errors++;
867 header = (struct virtio_net_hdr_mrg_rxbuf *)((char *)rxm->buf_addr +
868 RTE_PKTMBUF_HEADROOM - hdr_size);
869 seg_num = header->num_buffers;
874 rxm->data_off = RTE_PKTMBUF_HEADROOM;
875 rxm->nb_segs = seg_num;
879 rxm->pkt_len = (uint32_t)(len[0] - hdr_size);
880 rxm->data_len = (uint16_t)(len[0] - hdr_size);
882 rxm->port = rxvq->port_id;
883 rx_pkts[nb_rx] = rxm;
886 if (offload && virtio_rx_offload(rxm, &header->hdr) < 0) {
887 virtio_discard_rxbuf(vq, rxm);
888 rxvq->stats.errors++;
892 seg_res = seg_num - 1;
894 while (seg_res != 0) {
896 * Get extra segments for current uncompleted packet.
899 RTE_MIN(seg_res, RTE_DIM(rcv_pkts));
900 if (likely(VIRTQUEUE_NUSED(vq) >= rcv_cnt)) {
902 virtqueue_dequeue_burst_rx(vq,
903 rcv_pkts, len, rcv_cnt);
908 "No enough segments for packet.");
910 virtio_discard_rxbuf(vq, rxm);
911 rxvq->stats.errors++;
917 while (extra_idx < rcv_cnt) {
918 rxm = rcv_pkts[extra_idx];
920 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
922 rxm->pkt_len = (uint32_t)(len[extra_idx]);
923 rxm->data_len = (uint16_t)(len[extra_idx]);
929 rx_pkts[nb_rx]->pkt_len += rxm->pkt_len;
936 rte_vlan_strip(rx_pkts[nb_rx]);
938 VIRTIO_DUMP_PACKET(rx_pkts[nb_rx],
939 rx_pkts[nb_rx]->data_len);
941 rxvq->stats.bytes += rx_pkts[nb_rx]->pkt_len;
942 virtio_update_packet_stats(&rxvq->stats, rx_pkts[nb_rx]);
946 rxvq->stats.packets += nb_rx;
948 /* Allocate new mbuf for the used descriptor */
950 while (likely(!virtqueue_full(vq))) {
951 new_mbuf = rte_mbuf_raw_alloc(rxvq->mpool);
952 if (unlikely(new_mbuf == NULL)) {
953 struct rte_eth_dev *dev
954 = &rte_eth_devices[rxvq->port_id];
955 dev->data->rx_mbuf_alloc_failed++;
958 error = virtqueue_enqueue_recv_refill(vq, new_mbuf);
959 if (unlikely(error)) {
960 rte_pktmbuf_free(new_mbuf);
966 if (likely(nb_enqueued)) {
967 vq_update_avail_idx(vq);
969 if (unlikely(virtqueue_kick_prepare(vq))) {
970 virtqueue_notify(vq);
971 PMD_RX_LOG(DEBUG, "Notified");
979 virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
981 struct virtnet_tx *txvq = tx_queue;
982 struct virtqueue *vq = txvq->vq;
983 struct virtio_hw *hw = vq->hw;
984 uint16_t hdr_size = hw->vtnet_hdr_size;
985 uint16_t nb_used, nb_tx;
988 if (unlikely(nb_pkts < 1))
991 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
992 nb_used = VIRTQUEUE_NUSED(vq);
995 if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh))
996 virtio_xmit_cleanup(vq, nb_used);
998 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
999 struct rte_mbuf *txm = tx_pkts[nb_tx];
1000 int can_push = 0, use_indirect = 0, slots, need;
1002 /* Do VLAN tag insertion */
1003 if (unlikely(txm->ol_flags & PKT_TX_VLAN_PKT)) {
1004 error = rte_vlan_insert(&txm);
1005 if (unlikely(error)) {
1006 rte_pktmbuf_free(txm);
1011 /* optimize ring usage */
1012 if (vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) &&
1013 rte_mbuf_refcnt_read(txm) == 1 &&
1014 RTE_MBUF_DIRECT(txm) &&
1015 txm->nb_segs == 1 &&
1016 rte_pktmbuf_headroom(txm) >= hdr_size &&
1017 rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
1018 __alignof__(struct virtio_net_hdr_mrg_rxbuf)))
1020 else if (vtpci_with_feature(hw, VIRTIO_RING_F_INDIRECT_DESC) &&
1021 txm->nb_segs < VIRTIO_MAX_TX_INDIRECT)
1024 /* How many main ring entries are needed to this Tx?
1025 * any_layout => number of segments
1027 * default => number of segments + 1
1029 slots = use_indirect ? 1 : (txm->nb_segs + !can_push);
1030 need = slots - vq->vq_free_cnt;
1032 /* Positive value indicates it need free vring descriptors */
1033 if (unlikely(need > 0)) {
1034 nb_used = VIRTQUEUE_NUSED(vq);
1036 need = RTE_MIN(need, (int)nb_used);
1038 virtio_xmit_cleanup(vq, need);
1039 need = slots - vq->vq_free_cnt;
1040 if (unlikely(need > 0)) {
1042 "No free tx descriptors to transmit");
1047 /* Enqueue Packet buffers */
1048 virtqueue_enqueue_xmit(txvq, txm, slots, use_indirect, can_push);
1050 txvq->stats.bytes += txm->pkt_len;
1051 virtio_update_packet_stats(&txvq->stats, txm);
1054 txvq->stats.packets += nb_tx;
1056 if (likely(nb_tx)) {
1057 vq_update_avail_idx(vq);
1059 if (unlikely(virtqueue_kick_prepare(vq))) {
1060 virtqueue_notify(vq);
1061 PMD_TX_LOG(DEBUG, "Notified backend after xmit");