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>
55 #include "virtio_logs.h"
56 #include "virtio_ethdev.h"
57 #include "virtio_pci.h"
58 #include "virtqueue.h"
59 #include "virtio_rxtx.h"
61 #ifdef RTE_LIBRTE_VIRTIO_DEBUG_DUMP
62 #define VIRTIO_DUMP_PACKET(m, len) rte_pktmbuf_dump(stdout, m, len)
64 #define VIRTIO_DUMP_PACKET(m, len) do { } while (0)
68 #define VIRTIO_SIMPLE_FLAGS ((uint32_t)ETH_TXQ_FLAGS_NOMULTSEGS | \
69 ETH_TXQ_FLAGS_NOOFFLOADS)
72 vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx)
74 struct vring_desc *dp, *dp_tail;
75 struct vq_desc_extra *dxp;
76 uint16_t desc_idx_last = desc_idx;
78 dp = &vq->vq_ring.desc[desc_idx];
79 dxp = &vq->vq_descx[desc_idx];
80 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt + dxp->ndescs);
81 if ((dp->flags & VRING_DESC_F_INDIRECT) == 0) {
82 while (dp->flags & VRING_DESC_F_NEXT) {
83 desc_idx_last = dp->next;
84 dp = &vq->vq_ring.desc[dp->next];
90 * We must append the existing free chain, if any, to the end of
91 * newly freed chain. If the virtqueue was completely used, then
92 * head would be VQ_RING_DESC_CHAIN_END (ASSERTed above).
94 if (vq->vq_desc_tail_idx == VQ_RING_DESC_CHAIN_END) {
95 vq->vq_desc_head_idx = desc_idx;
97 dp_tail = &vq->vq_ring.desc[vq->vq_desc_tail_idx];
98 dp_tail->next = desc_idx;
101 vq->vq_desc_tail_idx = desc_idx_last;
102 dp->next = VQ_RING_DESC_CHAIN_END;
106 virtqueue_dequeue_burst_rx(struct virtqueue *vq, struct rte_mbuf **rx_pkts,
107 uint32_t *len, uint16_t num)
109 struct vring_used_elem *uep;
110 struct rte_mbuf *cookie;
111 uint16_t used_idx, desc_idx;
114 /* Caller does the check */
115 for (i = 0; i < num ; i++) {
116 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
117 uep = &vq->vq_ring.used->ring[used_idx];
118 desc_idx = (uint16_t) uep->id;
120 cookie = (struct rte_mbuf *)vq->vq_descx[desc_idx].cookie;
122 if (unlikely(cookie == NULL)) {
123 PMD_DRV_LOG(ERR, "vring descriptor with no mbuf cookie at %u\n",
124 vq->vq_used_cons_idx);
128 rte_prefetch0(cookie);
129 rte_packet_prefetch(rte_pktmbuf_mtod(cookie, void *));
131 vq->vq_used_cons_idx++;
132 vq_ring_free_chain(vq, desc_idx);
133 vq->vq_descx[desc_idx].cookie = NULL;
139 #ifndef DEFAULT_TX_FREE_THRESH
140 #define DEFAULT_TX_FREE_THRESH 32
143 /* Cleanup from completed transmits. */
145 virtio_xmit_cleanup(struct virtqueue *vq, uint16_t num)
147 uint16_t i, used_idx, desc_idx;
148 for (i = 0; i < num; i++) {
149 struct vring_used_elem *uep;
150 struct vq_desc_extra *dxp;
152 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
153 uep = &vq->vq_ring.used->ring[used_idx];
155 desc_idx = (uint16_t) uep->id;
156 dxp = &vq->vq_descx[desc_idx];
157 vq->vq_used_cons_idx++;
158 vq_ring_free_chain(vq, desc_idx);
160 if (dxp->cookie != NULL) {
161 rte_pktmbuf_free(dxp->cookie);
169 virtqueue_enqueue_recv_refill(struct virtqueue *vq, struct rte_mbuf *cookie)
171 struct vq_desc_extra *dxp;
172 struct virtio_hw *hw = vq->hw;
173 struct vring_desc *start_dp;
175 uint16_t head_idx, idx;
177 if (unlikely(vq->vq_free_cnt == 0))
179 if (unlikely(vq->vq_free_cnt < needed))
182 head_idx = vq->vq_desc_head_idx;
183 if (unlikely(head_idx >= vq->vq_nentries))
187 dxp = &vq->vq_descx[idx];
188 dxp->cookie = (void *)cookie;
189 dxp->ndescs = needed;
191 start_dp = vq->vq_ring.desc;
193 VIRTIO_MBUF_ADDR(cookie, vq) +
194 RTE_PKTMBUF_HEADROOM - hw->vtnet_hdr_size;
196 cookie->buf_len - RTE_PKTMBUF_HEADROOM + hw->vtnet_hdr_size;
197 start_dp[idx].flags = VRING_DESC_F_WRITE;
198 idx = start_dp[idx].next;
199 vq->vq_desc_head_idx = idx;
200 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
201 vq->vq_desc_tail_idx = idx;
202 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
203 vq_update_avail_ring(vq, head_idx);
209 virtqueue_enqueue_xmit(struct virtnet_tx *txvq, struct rte_mbuf *cookie,
210 uint16_t needed, int use_indirect, int can_push)
212 struct vq_desc_extra *dxp;
213 struct virtqueue *vq = txvq->vq;
214 struct vring_desc *start_dp;
215 uint16_t seg_num = cookie->nb_segs;
216 uint16_t head_idx, idx;
217 uint16_t head_size = vq->hw->vtnet_hdr_size;
220 head_idx = vq->vq_desc_head_idx;
222 dxp = &vq->vq_descx[idx];
223 dxp->cookie = (void *)cookie;
224 dxp->ndescs = needed;
226 start_dp = vq->vq_ring.desc;
229 /* put on zero'd transmit header (no offloads) */
230 void *hdr = rte_pktmbuf_prepend(cookie, head_size);
232 memset(hdr, 0, head_size);
233 } else if (use_indirect) {
234 /* setup tx ring slot to point to indirect
235 * descriptor list stored in reserved region.
237 * the first slot in indirect ring is already preset
238 * to point to the header in reserved region
240 struct virtio_tx_region *txr = txvq->virtio_net_hdr_mz->addr;
242 offs = idx * sizeof(struct virtio_tx_region)
243 + offsetof(struct virtio_tx_region, tx_indir);
245 start_dp[idx].addr = txvq->virtio_net_hdr_mem + offs;
246 start_dp[idx].len = (seg_num + 1) * sizeof(struct vring_desc);
247 start_dp[idx].flags = VRING_DESC_F_INDIRECT;
249 /* loop below will fill in rest of the indirect elements */
250 start_dp = txr[idx].tx_indir;
253 /* setup first tx ring slot to point to header
254 * stored in reserved region.
256 offs = idx * sizeof(struct virtio_tx_region)
257 + offsetof(struct virtio_tx_region, tx_hdr);
259 start_dp[idx].addr = txvq->virtio_net_hdr_mem + offs;
260 start_dp[idx].len = vq->hw->vtnet_hdr_size;
261 start_dp[idx].flags = VRING_DESC_F_NEXT;
262 idx = start_dp[idx].next;
266 start_dp[idx].addr = VIRTIO_MBUF_DATA_DMA_ADDR(cookie, vq);
267 start_dp[idx].len = cookie->data_len;
268 start_dp[idx].flags = cookie->next ? VRING_DESC_F_NEXT : 0;
269 idx = start_dp[idx].next;
270 } while ((cookie = cookie->next) != NULL);
273 idx = vq->vq_ring.desc[head_idx].next;
275 vq->vq_desc_head_idx = idx;
276 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
277 vq->vq_desc_tail_idx = idx;
278 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
279 vq_update_avail_ring(vq, head_idx);
283 virtio_dev_vring_start(struct virtqueue *vq)
285 int size = vq->vq_nentries;
286 struct vring *vr = &vq->vq_ring;
287 uint8_t *ring_mem = vq->vq_ring_virt_mem;
289 PMD_INIT_FUNC_TRACE();
292 * Reinitialise since virtio port might have been stopped and restarted
294 memset(vq->vq_ring_virt_mem, 0, vq->vq_ring_size);
295 vring_init(vr, size, ring_mem, VIRTIO_PCI_VRING_ALIGN);
296 vq->vq_used_cons_idx = 0;
297 vq->vq_desc_head_idx = 0;
298 vq->vq_avail_idx = 0;
299 vq->vq_desc_tail_idx = (uint16_t)(vq->vq_nentries - 1);
300 vq->vq_free_cnt = vq->vq_nentries;
301 memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries);
303 vring_desc_init(vr->desc, size);
306 * Disable device(host) interrupting guest
308 virtqueue_disable_intr(vq);
312 virtio_dev_cq_start(struct rte_eth_dev *dev)
314 struct virtio_hw *hw = dev->data->dev_private;
316 if (hw->cvq && hw->cvq->vq) {
317 virtio_dev_vring_start(hw->cvq->vq);
318 VIRTQUEUE_DUMP((struct virtqueue *)hw->cvq->vq);
323 virtio_dev_rxtx_start(struct rte_eth_dev *dev)
326 * Start receive and transmit vrings
327 * - Setup vring structure for all queues
328 * - Initialize descriptor for the rx vring
329 * - Allocate blank mbufs for the each rx descriptor
334 struct virtio_hw *hw = dev->data->dev_private;
336 PMD_INIT_FUNC_TRACE();
338 /* Start rx vring. */
339 for (i = 0; i < dev->data->nb_rx_queues; i++) {
340 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
341 struct virtqueue *vq = rxvq->vq;
345 virtio_dev_vring_start(vq);
346 if (rxvq->mpool == NULL) {
347 rte_exit(EXIT_FAILURE,
348 "Cannot allocate mbufs for rx virtqueue");
351 /* Allocate blank mbufs for the each rx descriptor */
355 if (hw->use_simple_rxtx) {
356 for (desc_idx = 0; desc_idx < vq->vq_nentries;
358 vq->vq_ring.avail->ring[desc_idx] = desc_idx;
359 vq->vq_ring.desc[desc_idx].flags =
364 memset(&rxvq->fake_mbuf, 0, sizeof(rxvq->fake_mbuf));
365 for (desc_idx = 0; desc_idx < RTE_PMD_VIRTIO_RX_MAX_BURST;
367 vq->sw_ring[vq->vq_nentries + desc_idx] =
371 while (!virtqueue_full(vq)) {
372 m = rte_mbuf_raw_alloc(rxvq->mpool);
376 /******************************************
377 * Enqueue allocated buffers *
378 *******************************************/
379 if (hw->use_simple_rxtx)
380 error = virtqueue_enqueue_recv_refill_simple(vq, m);
382 error = virtqueue_enqueue_recv_refill(vq, m);
391 vq_update_avail_idx(vq);
393 PMD_INIT_LOG(DEBUG, "Allocated %d bufs", nbufs);
398 /* Start tx vring. */
399 for (i = 0; i < dev->data->nb_tx_queues; i++) {
400 struct virtnet_tx *txvq = dev->data->tx_queues[i];
401 struct virtqueue *vq = txvq->vq;
403 virtio_dev_vring_start(vq);
404 if (hw->use_simple_rxtx) {
405 uint16_t mid_idx = vq->vq_nentries >> 1;
407 for (desc_idx = 0; desc_idx < mid_idx; desc_idx++) {
408 vq->vq_ring.avail->ring[desc_idx] =
410 vq->vq_ring.desc[desc_idx + mid_idx].next =
412 vq->vq_ring.desc[desc_idx + mid_idx].addr =
413 txvq->virtio_net_hdr_mem +
414 offsetof(struct virtio_tx_region, tx_hdr);
415 vq->vq_ring.desc[desc_idx + mid_idx].len =
416 vq->hw->vtnet_hdr_size;
417 vq->vq_ring.desc[desc_idx + mid_idx].flags =
419 vq->vq_ring.desc[desc_idx].flags = 0;
421 for (desc_idx = mid_idx; desc_idx < vq->vq_nentries;
423 vq->vq_ring.avail->ring[desc_idx] = desc_idx;
431 virtio_dev_rx_queue_setup(struct rte_eth_dev *dev,
434 unsigned int socket_id,
435 __rte_unused const struct rte_eth_rxconf *rx_conf,
436 struct rte_mempool *mp)
438 uint16_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
439 struct virtnet_rx *rxvq;
442 PMD_INIT_FUNC_TRACE();
443 ret = virtio_dev_queue_setup(dev, VTNET_RQ, queue_idx, vtpci_queue_idx,
444 nb_desc, socket_id, (void **)&rxvq);
446 PMD_INIT_LOG(ERR, "rvq initialization failed");
450 /* Create mempool for rx mbuf allocation */
453 dev->data->rx_queues[queue_idx] = rxvq;
455 virtio_rxq_vec_setup(rxvq);
461 virtio_dev_rx_queue_release(void *rxq)
463 struct virtnet_rx *rxvq = rxq;
464 struct virtqueue *vq;
465 const struct rte_memzone *mz;
471 * rxvq is freed when vq is freed, and as mz should be freed after the
472 * del_queue, so we reserve the mz pointer first.
477 virtio_dev_queue_release(vq);
478 rte_memzone_free(mz);
482 virtio_update_rxtx_handler(struct rte_eth_dev *dev,
483 const struct rte_eth_txconf *tx_conf)
485 uint8_t use_simple_rxtx = 0;
486 struct virtio_hw *hw = dev->data->dev_private;
488 #if defined RTE_ARCH_X86
489 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE3))
491 #elif defined RTE_ARCH_ARM64 || defined CONFIG_RTE_ARCH_ARM
492 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON))
495 /* Use simple rx/tx func if single segment and no offloads */
496 if (use_simple_rxtx &&
497 (tx_conf->txq_flags & VIRTIO_SIMPLE_FLAGS) == VIRTIO_SIMPLE_FLAGS &&
498 !vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
499 PMD_INIT_LOG(INFO, "Using simple rx/tx path");
500 dev->tx_pkt_burst = virtio_xmit_pkts_simple;
501 dev->rx_pkt_burst = virtio_recv_pkts_vec;
502 hw->use_simple_rxtx = use_simple_rxtx;
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,
518 const struct rte_eth_txconf *tx_conf)
520 uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
521 struct virtnet_tx *txvq;
522 struct virtqueue *vq;
523 uint16_t tx_free_thresh;
526 PMD_INIT_FUNC_TRACE();
528 if ((tx_conf->txq_flags & ETH_TXQ_FLAGS_NOXSUMS)
529 != ETH_TXQ_FLAGS_NOXSUMS) {
530 PMD_INIT_LOG(ERR, "TX checksum offload not supported\n");
534 virtio_update_rxtx_handler(dev, tx_conf);
536 ret = virtio_dev_queue_setup(dev, VTNET_TQ, queue_idx, vtpci_queue_idx,
537 nb_desc, socket_id, (void **)&txvq);
539 PMD_INIT_LOG(ERR, "tvq initialization failed");
544 tx_free_thresh = tx_conf->tx_free_thresh;
545 if (tx_free_thresh == 0)
547 RTE_MIN(vq->vq_nentries / 4, DEFAULT_TX_FREE_THRESH);
549 if (tx_free_thresh >= (vq->vq_nentries - 3)) {
550 RTE_LOG(ERR, PMD, "tx_free_thresh must be less than the "
551 "number of TX entries minus 3 (%u)."
552 " (tx_free_thresh=%u port=%u queue=%u)\n",
554 tx_free_thresh, dev->data->port_id, queue_idx);
558 vq->vq_free_thresh = tx_free_thresh;
560 dev->data->tx_queues[queue_idx] = txvq;
565 virtio_dev_tx_queue_release(void *txq)
567 struct virtnet_tx *txvq = txq;
568 struct virtqueue *vq;
569 const struct rte_memzone *mz;
570 const struct rte_memzone *hdr_mz;
576 * txvq is freed when vq is freed, and as mz should be freed after the
577 * del_queue, so we reserve the mz pointer first.
581 hdr_mz = txvq->virtio_net_hdr_mz;
583 virtio_dev_queue_release(vq);
584 rte_memzone_free(mz);
585 rte_memzone_free(hdr_mz);
589 virtio_discard_rxbuf(struct virtqueue *vq, struct rte_mbuf *m)
593 * Requeue the discarded mbuf. This should always be
594 * successful since it was just dequeued.
596 error = virtqueue_enqueue_recv_refill(vq, m);
597 if (unlikely(error)) {
598 RTE_LOG(ERR, PMD, "cannot requeue discarded mbuf");
604 virtio_update_packet_stats(struct virtnet_stats *stats, struct rte_mbuf *mbuf)
606 uint32_t s = mbuf->pkt_len;
607 struct ether_addr *ea;
610 stats->size_bins[1]++;
611 } else if (s > 64 && s < 1024) {
614 /* count zeros, and offset into correct bin */
615 bin = (sizeof(s) * 8) - __builtin_clz(s) - 5;
616 stats->size_bins[bin]++;
619 stats->size_bins[0]++;
621 stats->size_bins[6]++;
623 stats->size_bins[7]++;
626 ea = rte_pktmbuf_mtod(mbuf, struct ether_addr *);
627 if (is_multicast_ether_addr(ea)) {
628 if (is_broadcast_ether_addr(ea))
635 #define VIRTIO_MBUF_BURST_SZ 64
636 #define DESC_PER_CACHELINE (RTE_CACHE_LINE_SIZE / sizeof(struct vring_desc))
638 virtio_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
640 struct virtnet_rx *rxvq = rx_queue;
641 struct virtqueue *vq = rxvq->vq;
642 struct virtio_hw *hw;
643 struct rte_mbuf *rxm, *new_mbuf;
644 uint16_t nb_used, num, nb_rx;
645 uint32_t len[VIRTIO_MBUF_BURST_SZ];
646 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
648 uint32_t i, nb_enqueued;
651 nb_used = VIRTQUEUE_NUSED(vq);
655 num = (uint16_t)(likely(nb_used <= nb_pkts) ? nb_used : nb_pkts);
656 num = (uint16_t)(likely(num <= VIRTIO_MBUF_BURST_SZ) ? num : VIRTIO_MBUF_BURST_SZ);
657 if (likely(num > DESC_PER_CACHELINE))
658 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
660 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, num);
661 PMD_RX_LOG(DEBUG, "used:%d dequeue:%d", nb_used, num);
666 hdr_size = hw->vtnet_hdr_size;
668 for (i = 0; i < num ; i++) {
671 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
673 if (unlikely(len[i] < hdr_size + ETHER_HDR_LEN)) {
674 PMD_RX_LOG(ERR, "Packet drop");
676 virtio_discard_rxbuf(vq, rxm);
677 rxvq->stats.errors++;
681 rxm->port = rxvq->port_id;
682 rxm->data_off = RTE_PKTMBUF_HEADROOM;
688 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
689 rxm->data_len = (uint16_t)(len[i] - hdr_size);
694 VIRTIO_DUMP_PACKET(rxm, rxm->data_len);
696 rx_pkts[nb_rx++] = rxm;
698 rxvq->stats.bytes += rx_pkts[nb_rx - 1]->pkt_len;
699 virtio_update_packet_stats(&rxvq->stats, rxm);
702 rxvq->stats.packets += nb_rx;
704 /* Allocate new mbuf for the used descriptor */
706 while (likely(!virtqueue_full(vq))) {
707 new_mbuf = rte_mbuf_raw_alloc(rxvq->mpool);
708 if (unlikely(new_mbuf == NULL)) {
709 struct rte_eth_dev *dev
710 = &rte_eth_devices[rxvq->port_id];
711 dev->data->rx_mbuf_alloc_failed++;
714 error = virtqueue_enqueue_recv_refill(vq, new_mbuf);
715 if (unlikely(error)) {
716 rte_pktmbuf_free(new_mbuf);
722 if (likely(nb_enqueued)) {
723 vq_update_avail_idx(vq);
725 if (unlikely(virtqueue_kick_prepare(vq))) {
726 virtqueue_notify(vq);
727 PMD_RX_LOG(DEBUG, "Notified");
735 virtio_recv_mergeable_pkts(void *rx_queue,
736 struct rte_mbuf **rx_pkts,
739 struct virtnet_rx *rxvq = rx_queue;
740 struct virtqueue *vq = rxvq->vq;
741 struct virtio_hw *hw;
742 struct rte_mbuf *rxm, *new_mbuf;
743 uint16_t nb_used, num, nb_rx;
744 uint32_t len[VIRTIO_MBUF_BURST_SZ];
745 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
746 struct rte_mbuf *prev;
748 uint32_t i, nb_enqueued;
754 nb_used = VIRTQUEUE_NUSED(vq);
758 PMD_RX_LOG(DEBUG, "used:%d", nb_used);
767 hdr_size = hw->vtnet_hdr_size;
769 while (i < nb_used) {
770 struct virtio_net_hdr_mrg_rxbuf *header;
772 if (nb_rx == nb_pkts)
775 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, 1);
781 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
782 PMD_RX_LOG(DEBUG, "packet len:%d", len[0]);
786 if (unlikely(len[0] < hdr_size + ETHER_HDR_LEN)) {
787 PMD_RX_LOG(ERR, "Packet drop");
789 virtio_discard_rxbuf(vq, rxm);
790 rxvq->stats.errors++;
794 header = (struct virtio_net_hdr_mrg_rxbuf *)((char *)rxm->buf_addr +
795 RTE_PKTMBUF_HEADROOM - hdr_size);
796 seg_num = header->num_buffers;
801 rxm->data_off = RTE_PKTMBUF_HEADROOM;
802 rxm->nb_segs = seg_num;
806 rxm->pkt_len = (uint32_t)(len[0] - hdr_size);
807 rxm->data_len = (uint16_t)(len[0] - hdr_size);
809 rxm->port = rxvq->port_id;
810 rx_pkts[nb_rx] = rxm;
813 seg_res = seg_num - 1;
815 while (seg_res != 0) {
817 * Get extra segments for current uncompleted packet.
820 RTE_MIN(seg_res, RTE_DIM(rcv_pkts));
821 if (likely(VIRTQUEUE_NUSED(vq) >= rcv_cnt)) {
823 virtqueue_dequeue_burst_rx(vq,
824 rcv_pkts, len, rcv_cnt);
829 "No enough segments for packet.");
831 virtio_discard_rxbuf(vq, rxm);
832 rxvq->stats.errors++;
838 while (extra_idx < rcv_cnt) {
839 rxm = rcv_pkts[extra_idx];
841 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
843 rxm->pkt_len = (uint32_t)(len[extra_idx]);
844 rxm->data_len = (uint16_t)(len[extra_idx]);
850 rx_pkts[nb_rx]->pkt_len += rxm->pkt_len;
857 rte_vlan_strip(rx_pkts[nb_rx]);
859 VIRTIO_DUMP_PACKET(rx_pkts[nb_rx],
860 rx_pkts[nb_rx]->data_len);
862 rxvq->stats.bytes += rx_pkts[nb_rx]->pkt_len;
863 virtio_update_packet_stats(&rxvq->stats, rx_pkts[nb_rx]);
867 rxvq->stats.packets += nb_rx;
869 /* Allocate new mbuf for the used descriptor */
871 while (likely(!virtqueue_full(vq))) {
872 new_mbuf = rte_mbuf_raw_alloc(rxvq->mpool);
873 if (unlikely(new_mbuf == NULL)) {
874 struct rte_eth_dev *dev
875 = &rte_eth_devices[rxvq->port_id];
876 dev->data->rx_mbuf_alloc_failed++;
879 error = virtqueue_enqueue_recv_refill(vq, new_mbuf);
880 if (unlikely(error)) {
881 rte_pktmbuf_free(new_mbuf);
887 if (likely(nb_enqueued)) {
888 vq_update_avail_idx(vq);
890 if (unlikely(virtqueue_kick_prepare(vq))) {
891 virtqueue_notify(vq);
892 PMD_RX_LOG(DEBUG, "Notified");
900 virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
902 struct virtnet_tx *txvq = tx_queue;
903 struct virtqueue *vq = txvq->vq;
904 struct virtio_hw *hw = vq->hw;
905 uint16_t hdr_size = hw->vtnet_hdr_size;
906 uint16_t nb_used, nb_tx;
909 if (unlikely(nb_pkts < 1))
912 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
913 nb_used = VIRTQUEUE_NUSED(vq);
916 if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh))
917 virtio_xmit_cleanup(vq, nb_used);
919 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
920 struct rte_mbuf *txm = tx_pkts[nb_tx];
921 int can_push = 0, use_indirect = 0, slots, need;
923 /* Do VLAN tag insertion */
924 if (unlikely(txm->ol_flags & PKT_TX_VLAN_PKT)) {
925 error = rte_vlan_insert(&txm);
926 if (unlikely(error)) {
927 rte_pktmbuf_free(txm);
932 /* optimize ring usage */
933 if (vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) &&
934 rte_mbuf_refcnt_read(txm) == 1 &&
935 RTE_MBUF_DIRECT(txm) &&
937 rte_pktmbuf_headroom(txm) >= hdr_size &&
938 rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
939 __alignof__(struct virtio_net_hdr_mrg_rxbuf)))
941 else if (vtpci_with_feature(hw, VIRTIO_RING_F_INDIRECT_DESC) &&
942 txm->nb_segs < VIRTIO_MAX_TX_INDIRECT)
945 /* How many main ring entries are needed to this Tx?
946 * any_layout => number of segments
948 * default => number of segments + 1
950 slots = use_indirect ? 1 : (txm->nb_segs + !can_push);
951 need = slots - vq->vq_free_cnt;
953 /* Positive value indicates it need free vring descriptors */
954 if (unlikely(need > 0)) {
955 nb_used = VIRTQUEUE_NUSED(vq);
957 need = RTE_MIN(need, (int)nb_used);
959 virtio_xmit_cleanup(vq, need);
960 need = slots - vq->vq_free_cnt;
961 if (unlikely(need > 0)) {
963 "No free tx descriptors to transmit");
968 /* Enqueue Packet buffers */
969 virtqueue_enqueue_xmit(txvq, txm, slots, use_indirect, can_push);
971 txvq->stats.bytes += txm->pkt_len;
972 virtio_update_packet_stats(&txvq->stats, txm);
975 txvq->stats.packets += nb_tx;
978 vq_update_avail_idx(vq);
980 if (unlikely(virtqueue_kick_prepare(vq))) {
981 virtqueue_notify(vq);
982 PMD_TX_LOG(DEBUG, "Notified backend after xmit");