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>
54 #include "virtio_logs.h"
55 #include "virtio_ethdev.h"
56 #include "virtio_pci.h"
57 #include "virtqueue.h"
58 #include "virtio_rxtx.h"
60 #ifdef RTE_LIBRTE_VIRTIO_DEBUG_DUMP
61 #define VIRTIO_DUMP_PACKET(m, len) rte_pktmbuf_dump(stdout, m, len)
63 #define VIRTIO_DUMP_PACKET(m, len) do { } while (0)
67 #define VIRTIO_SIMPLE_FLAGS ((uint32_t)ETH_TXQ_FLAGS_NOMULTSEGS | \
68 ETH_TXQ_FLAGS_NOOFFLOADS)
70 #ifdef RTE_MACHINE_CPUFLAG_SSSE3
71 static int use_simple_rxtx;
75 vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx)
77 struct vring_desc *dp, *dp_tail;
78 struct vq_desc_extra *dxp;
79 uint16_t desc_idx_last = desc_idx;
81 dp = &vq->vq_ring.desc[desc_idx];
82 dxp = &vq->vq_descx[desc_idx];
83 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt + dxp->ndescs);
84 if ((dp->flags & VRING_DESC_F_INDIRECT) == 0) {
85 while (dp->flags & VRING_DESC_F_NEXT) {
86 desc_idx_last = dp->next;
87 dp = &vq->vq_ring.desc[dp->next];
93 * We must append the existing free chain, if any, to the end of
94 * newly freed chain. If the virtqueue was completely used, then
95 * head would be VQ_RING_DESC_CHAIN_END (ASSERTed above).
97 if (vq->vq_desc_tail_idx == VQ_RING_DESC_CHAIN_END) {
98 vq->vq_desc_head_idx = desc_idx;
100 dp_tail = &vq->vq_ring.desc[vq->vq_desc_tail_idx];
101 dp_tail->next = desc_idx;
104 vq->vq_desc_tail_idx = desc_idx_last;
105 dp->next = VQ_RING_DESC_CHAIN_END;
109 virtqueue_dequeue_burst_rx(struct virtqueue *vq, struct rte_mbuf **rx_pkts,
110 uint32_t *len, uint16_t num)
112 struct vring_used_elem *uep;
113 struct rte_mbuf *cookie;
114 uint16_t used_idx, desc_idx;
117 /* Caller does the check */
118 for (i = 0; i < num ; i++) {
119 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
120 uep = &vq->vq_ring.used->ring[used_idx];
121 desc_idx = (uint16_t) uep->id;
123 cookie = (struct rte_mbuf *)vq->vq_descx[desc_idx].cookie;
125 if (unlikely(cookie == NULL)) {
126 PMD_DRV_LOG(ERR, "vring descriptor with no mbuf cookie at %u\n",
127 vq->vq_used_cons_idx);
131 rte_prefetch0(cookie);
132 rte_packet_prefetch(rte_pktmbuf_mtod(cookie, void *));
134 vq->vq_used_cons_idx++;
135 vq_ring_free_chain(vq, desc_idx);
136 vq->vq_descx[desc_idx].cookie = NULL;
142 #ifndef DEFAULT_TX_FREE_THRESH
143 #define DEFAULT_TX_FREE_THRESH 32
146 /* Cleanup from completed transmits. */
148 virtio_xmit_cleanup(struct virtqueue *vq, uint16_t num)
150 uint16_t i, used_idx, desc_idx;
151 for (i = 0; i < num; i++) {
152 struct vring_used_elem *uep;
153 struct vq_desc_extra *dxp;
155 used_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
156 uep = &vq->vq_ring.used->ring[used_idx];
158 desc_idx = (uint16_t) uep->id;
159 dxp = &vq->vq_descx[desc_idx];
160 vq->vq_used_cons_idx++;
161 vq_ring_free_chain(vq, desc_idx);
163 if (dxp->cookie != NULL) {
164 rte_pktmbuf_free(dxp->cookie);
172 virtqueue_enqueue_recv_refill(struct virtqueue *vq, struct rte_mbuf *cookie)
174 struct vq_desc_extra *dxp;
175 struct virtio_hw *hw = vq->hw;
176 struct vring_desc *start_dp;
178 uint16_t head_idx, idx;
180 if (unlikely(vq->vq_free_cnt == 0))
182 if (unlikely(vq->vq_free_cnt < needed))
185 head_idx = vq->vq_desc_head_idx;
186 if (unlikely(head_idx >= vq->vq_nentries))
190 dxp = &vq->vq_descx[idx];
191 dxp->cookie = (void *)cookie;
192 dxp->ndescs = needed;
194 start_dp = vq->vq_ring.desc;
196 (uint64_t)(cookie->buf_physaddr + RTE_PKTMBUF_HEADROOM
197 - hw->vtnet_hdr_size);
199 cookie->buf_len - RTE_PKTMBUF_HEADROOM + hw->vtnet_hdr_size;
200 start_dp[idx].flags = VRING_DESC_F_WRITE;
201 idx = start_dp[idx].next;
202 vq->vq_desc_head_idx = idx;
203 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
204 vq->vq_desc_tail_idx = idx;
205 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
206 vq_update_avail_ring(vq, head_idx);
212 virtqueue_enqueue_xmit(struct virtnet_tx *txvq, struct rte_mbuf *cookie,
213 uint16_t needed, int use_indirect, int can_push)
215 struct vq_desc_extra *dxp;
216 struct virtqueue *vq = txvq->vq;
217 struct vring_desc *start_dp;
218 uint16_t seg_num = cookie->nb_segs;
219 uint16_t head_idx, idx;
220 uint16_t head_size = vq->hw->vtnet_hdr_size;
223 head_idx = vq->vq_desc_head_idx;
225 dxp = &vq->vq_descx[idx];
226 dxp->cookie = (void *)cookie;
227 dxp->ndescs = needed;
229 start_dp = vq->vq_ring.desc;
232 /* put on zero'd transmit header (no offloads) */
233 void *hdr = rte_pktmbuf_prepend(cookie, head_size);
235 memset(hdr, 0, head_size);
236 } else if (use_indirect) {
237 /* setup tx ring slot to point to indirect
238 * descriptor list stored in reserved region.
240 * the first slot in indirect ring is already preset
241 * to point to the header in reserved region
243 struct virtio_tx_region *txr = txvq->virtio_net_hdr_mz->addr;
245 offs = idx * sizeof(struct virtio_tx_region)
246 + offsetof(struct virtio_tx_region, tx_indir);
248 start_dp[idx].addr = txvq->virtio_net_hdr_mem + offs;
249 start_dp[idx].len = (seg_num + 1) * sizeof(struct vring_desc);
250 start_dp[idx].flags = VRING_DESC_F_INDIRECT;
252 /* loop below will fill in rest of the indirect elements */
253 start_dp = txr[idx].tx_indir;
256 /* setup first tx ring slot to point to header
257 * stored in reserved region.
259 offs = idx * sizeof(struct virtio_tx_region)
260 + offsetof(struct virtio_tx_region, tx_hdr);
262 start_dp[idx].addr = txvq->virtio_net_hdr_mem + offs;
263 start_dp[idx].len = vq->hw->vtnet_hdr_size;
264 start_dp[idx].flags = VRING_DESC_F_NEXT;
265 idx = start_dp[idx].next;
269 start_dp[idx].addr = rte_mbuf_data_dma_addr(cookie);
270 start_dp[idx].len = cookie->data_len;
271 start_dp[idx].flags = cookie->next ? VRING_DESC_F_NEXT : 0;
272 idx = start_dp[idx].next;
273 } while ((cookie = cookie->next) != NULL);
276 idx = vq->vq_ring.desc[head_idx].next;
278 vq->vq_desc_head_idx = idx;
279 if (vq->vq_desc_head_idx == VQ_RING_DESC_CHAIN_END)
280 vq->vq_desc_tail_idx = idx;
281 vq->vq_free_cnt = (uint16_t)(vq->vq_free_cnt - needed);
282 vq_update_avail_ring(vq, head_idx);
286 virtio_dev_vring_start(struct virtqueue *vq)
288 int size = vq->vq_nentries;
289 struct vring *vr = &vq->vq_ring;
290 uint8_t *ring_mem = vq->vq_ring_virt_mem;
292 PMD_INIT_FUNC_TRACE();
295 * Reinitialise since virtio port might have been stopped and restarted
297 memset(vq->vq_ring_virt_mem, 0, vq->vq_ring_size);
298 vring_init(vr, size, ring_mem, VIRTIO_PCI_VRING_ALIGN);
299 vq->vq_used_cons_idx = 0;
300 vq->vq_desc_head_idx = 0;
301 vq->vq_avail_idx = 0;
302 vq->vq_desc_tail_idx = (uint16_t)(vq->vq_nentries - 1);
303 vq->vq_free_cnt = vq->vq_nentries;
304 memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries);
306 vring_desc_init(vr->desc, size);
309 * Disable device(host) interrupting guest
311 virtqueue_disable_intr(vq);
315 virtio_dev_cq_start(struct rte_eth_dev *dev)
317 struct virtio_hw *hw = dev->data->dev_private;
319 if (hw->cvq && hw->cvq->vq) {
320 virtio_dev_vring_start(hw->cvq->vq);
321 VIRTQUEUE_DUMP((struct virtqueue *)hw->cvq->vq);
326 virtio_dev_rxtx_start(struct rte_eth_dev *dev)
329 * Start receive and transmit vrings
330 * - Setup vring structure for all queues
331 * - Initialize descriptor for the rx vring
332 * - Allocate blank mbufs for the each rx descriptor
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 #ifdef RTE_MACHINE_CPUFLAG_SSSE3
358 if (use_simple_rxtx) {
359 for (desc_idx = 0; desc_idx < vq->vq_nentries;
361 vq->vq_ring.avail->ring[desc_idx] = desc_idx;
362 vq->vq_ring.desc[desc_idx].flags =
367 memset(&rxvq->fake_mbuf, 0, sizeof(rxvq->fake_mbuf));
368 for (desc_idx = 0; desc_idx < RTE_PMD_VIRTIO_RX_MAX_BURST;
370 vq->sw_ring[vq->vq_nentries + desc_idx] =
374 while (!virtqueue_full(vq)) {
375 m = rte_mbuf_raw_alloc(rxvq->mpool);
379 /******************************************
380 * Enqueue allocated buffers *
381 *******************************************/
382 #ifdef RTE_MACHINE_CPUFLAG_SSSE3
384 error = virtqueue_enqueue_recv_refill_simple(vq, m);
387 error = virtqueue_enqueue_recv_refill(vq, m);
395 vq_update_avail_idx(vq);
397 PMD_INIT_LOG(DEBUG, "Allocated %d bufs", nbufs);
402 /* Start tx vring. */
403 for (i = 0; i < dev->data->nb_tx_queues; i++) {
404 struct virtnet_tx *txvq = dev->data->tx_queues[i];
405 struct virtqueue *vq = txvq->vq;
407 virtio_dev_vring_start(vq);
408 #ifdef RTE_MACHINE_CPUFLAG_SSSE3
409 if (use_simple_rxtx) {
410 uint16_t mid_idx = vq->vq_nentries >> 1;
412 for (desc_idx = 0; desc_idx < mid_idx; desc_idx++) {
413 vq->vq_ring.avail->ring[desc_idx] =
415 vq->vq_ring.desc[desc_idx + mid_idx].next =
417 vq->vq_ring.desc[desc_idx + mid_idx].addr =
418 txvq->virtio_net_hdr_mem +
419 offsetof(struct virtio_tx_region, tx_hdr);
420 vq->vq_ring.desc[desc_idx + mid_idx].len =
421 vq->hw->vtnet_hdr_size;
422 vq->vq_ring.desc[desc_idx + mid_idx].flags =
424 vq->vq_ring.desc[desc_idx].flags = 0;
426 for (desc_idx = mid_idx; desc_idx < vq->vq_nentries;
428 vq->vq_ring.avail->ring[desc_idx] = desc_idx;
436 virtio_dev_rx_queue_setup(struct rte_eth_dev *dev,
439 unsigned int socket_id,
440 __rte_unused const struct rte_eth_rxconf *rx_conf,
441 struct rte_mempool *mp)
443 uint16_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_RQ_QUEUE_IDX;
444 struct virtnet_rx *rxvq;
447 PMD_INIT_FUNC_TRACE();
448 ret = virtio_dev_queue_setup(dev, VTNET_RQ, queue_idx, vtpci_queue_idx,
449 nb_desc, socket_id, (void **)&rxvq);
451 PMD_INIT_LOG(ERR, "rvq initialization failed");
455 /* Create mempool for rx mbuf allocation */
458 dev->data->rx_queues[queue_idx] = rxvq;
460 #ifdef RTE_MACHINE_CPUFLAG_SSSE3
461 virtio_rxq_vec_setup(rxvq);
468 virtio_dev_rx_queue_release(void *rxq)
470 struct virtnet_rx *rxvq = rxq;
471 struct virtqueue *vq = rxvq->vq;
472 /* rxvq is freed when vq is freed, and as mz should be freed after the
473 * del_queue, so we reserve the mz pointer first.
475 const struct rte_memzone *mz = rxvq->mz;
477 /* no need to free rxq as vq and rxq are allocated together */
478 virtio_dev_queue_release(vq);
479 rte_memzone_free(mz);
483 * struct rte_eth_dev *dev: Used to update dev
484 * uint16_t nb_desc: Defaults to values read from config space
485 * unsigned int socket_id: Used to allocate memzone
486 * const struct rte_eth_txconf *tx_conf: Used to setup tx engine
487 * uint16_t queue_idx: Just used as an index in dev txq list
490 virtio_dev_tx_queue_setup(struct rte_eth_dev *dev,
493 unsigned int socket_id,
494 const struct rte_eth_txconf *tx_conf)
496 uint8_t vtpci_queue_idx = 2 * queue_idx + VTNET_SQ_TQ_QUEUE_IDX;
498 #ifdef RTE_MACHINE_CPUFLAG_SSSE3
499 struct virtio_hw *hw = dev->data->dev_private;
501 struct virtnet_tx *txvq;
502 struct virtqueue *vq;
503 uint16_t tx_free_thresh;
506 PMD_INIT_FUNC_TRACE();
508 if ((tx_conf->txq_flags & ETH_TXQ_FLAGS_NOXSUMS)
509 != ETH_TXQ_FLAGS_NOXSUMS) {
510 PMD_INIT_LOG(ERR, "TX checksum offload not supported\n");
514 #ifdef RTE_MACHINE_CPUFLAG_SSSE3
515 /* Use simple rx/tx func if single segment and no offloads */
516 if ((tx_conf->txq_flags & VIRTIO_SIMPLE_FLAGS) == VIRTIO_SIMPLE_FLAGS &&
517 !vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
518 PMD_INIT_LOG(INFO, "Using simple rx/tx path");
519 dev->tx_pkt_burst = virtio_xmit_pkts_simple;
520 dev->rx_pkt_burst = virtio_recv_pkts_vec;
525 ret = virtio_dev_queue_setup(dev, VTNET_TQ, queue_idx, vtpci_queue_idx,
526 nb_desc, socket_id, (void **)&txvq);
528 PMD_INIT_LOG(ERR, "tvq initialization failed");
533 tx_free_thresh = tx_conf->tx_free_thresh;
534 if (tx_free_thresh == 0)
536 RTE_MIN(vq->vq_nentries / 4, DEFAULT_TX_FREE_THRESH);
538 if (tx_free_thresh >= (vq->vq_nentries - 3)) {
539 RTE_LOG(ERR, PMD, "tx_free_thresh must be less than the "
540 "number of TX entries minus 3 (%u)."
541 " (tx_free_thresh=%u port=%u queue=%u)\n",
543 tx_free_thresh, dev->data->port_id, queue_idx);
547 vq->vq_free_thresh = tx_free_thresh;
549 dev->data->tx_queues[queue_idx] = txvq;
554 virtio_dev_tx_queue_release(void *txq)
556 struct virtnet_tx *txvq = txq;
557 struct virtqueue *vq = txvq->vq;
558 /* txvq is freed when vq is freed, and as mz should be freed after the
559 * del_queue, so we reserve the mz pointer first.
561 const struct rte_memzone *hdr_mz = txvq->virtio_net_hdr_mz;
562 const struct rte_memzone *mz = txvq->mz;
564 virtio_dev_queue_release(vq);
565 rte_memzone_free(mz);
566 rte_memzone_free(hdr_mz);
570 virtio_discard_rxbuf(struct virtqueue *vq, struct rte_mbuf *m)
574 * Requeue the discarded mbuf. This should always be
575 * successful since it was just dequeued.
577 error = virtqueue_enqueue_recv_refill(vq, m);
578 if (unlikely(error)) {
579 RTE_LOG(ERR, PMD, "cannot requeue discarded mbuf");
585 virtio_update_packet_stats(struct virtnet_stats *stats, struct rte_mbuf *mbuf)
587 uint32_t s = mbuf->pkt_len;
588 struct ether_addr *ea;
591 stats->size_bins[1]++;
592 } else if (s > 64 && s < 1024) {
595 /* count zeros, and offset into correct bin */
596 bin = (sizeof(s) * 8) - __builtin_clz(s) - 5;
597 stats->size_bins[bin]++;
600 stats->size_bins[0]++;
602 stats->size_bins[6]++;
604 stats->size_bins[7]++;
607 ea = rte_pktmbuf_mtod(mbuf, struct ether_addr *);
608 if (is_multicast_ether_addr(ea)) {
609 if (is_broadcast_ether_addr(ea))
616 #define VIRTIO_MBUF_BURST_SZ 64
617 #define DESC_PER_CACHELINE (RTE_CACHE_LINE_SIZE / sizeof(struct vring_desc))
619 virtio_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
621 struct virtnet_rx *rxvq = rx_queue;
622 struct virtqueue *vq = rxvq->vq;
623 struct virtio_hw *hw;
624 struct rte_mbuf *rxm, *new_mbuf;
625 uint16_t nb_used, num, nb_rx;
626 uint32_t len[VIRTIO_MBUF_BURST_SZ];
627 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
629 uint32_t i, nb_enqueued;
632 nb_used = VIRTQUEUE_NUSED(vq);
636 num = (uint16_t)(likely(nb_used <= nb_pkts) ? nb_used : nb_pkts);
637 num = (uint16_t)(likely(num <= VIRTIO_MBUF_BURST_SZ) ? num : VIRTIO_MBUF_BURST_SZ);
638 if (likely(num > DESC_PER_CACHELINE))
639 num = num - ((vq->vq_used_cons_idx + num) % DESC_PER_CACHELINE);
641 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, num);
642 PMD_RX_LOG(DEBUG, "used:%d dequeue:%d", nb_used, num);
647 hdr_size = hw->vtnet_hdr_size;
649 for (i = 0; i < num ; i++) {
652 PMD_RX_LOG(DEBUG, "packet len:%d", len[i]);
654 if (unlikely(len[i] < hdr_size + ETHER_HDR_LEN)) {
655 PMD_RX_LOG(ERR, "Packet drop");
657 virtio_discard_rxbuf(vq, rxm);
658 rxvq->stats.errors++;
662 rxm->port = rxvq->port_id;
663 rxm->data_off = RTE_PKTMBUF_HEADROOM;
669 rxm->pkt_len = (uint32_t)(len[i] - hdr_size);
670 rxm->data_len = (uint16_t)(len[i] - hdr_size);
675 VIRTIO_DUMP_PACKET(rxm, rxm->data_len);
677 rx_pkts[nb_rx++] = rxm;
679 rxvq->stats.bytes += rx_pkts[nb_rx - 1]->pkt_len;
680 virtio_update_packet_stats(&rxvq->stats, rxm);
683 rxvq->stats.packets += nb_rx;
685 /* Allocate new mbuf for the used descriptor */
687 while (likely(!virtqueue_full(vq))) {
688 new_mbuf = rte_mbuf_raw_alloc(rxvq->mpool);
689 if (unlikely(new_mbuf == NULL)) {
690 struct rte_eth_dev *dev
691 = &rte_eth_devices[rxvq->port_id];
692 dev->data->rx_mbuf_alloc_failed++;
695 error = virtqueue_enqueue_recv_refill(vq, new_mbuf);
696 if (unlikely(error)) {
697 rte_pktmbuf_free(new_mbuf);
703 if (likely(nb_enqueued)) {
704 vq_update_avail_idx(vq);
706 if (unlikely(virtqueue_kick_prepare(vq))) {
707 virtqueue_notify(vq);
708 PMD_RX_LOG(DEBUG, "Notified");
716 virtio_recv_mergeable_pkts(void *rx_queue,
717 struct rte_mbuf **rx_pkts,
720 struct virtnet_rx *rxvq = rx_queue;
721 struct virtqueue *vq = rxvq->vq;
722 struct virtio_hw *hw;
723 struct rte_mbuf *rxm, *new_mbuf;
724 uint16_t nb_used, num, nb_rx;
725 uint32_t len[VIRTIO_MBUF_BURST_SZ];
726 struct rte_mbuf *rcv_pkts[VIRTIO_MBUF_BURST_SZ];
727 struct rte_mbuf *prev;
729 uint32_t i, nb_enqueued;
735 nb_used = VIRTQUEUE_NUSED(vq);
739 PMD_RX_LOG(DEBUG, "used:%d", nb_used);
748 hdr_size = hw->vtnet_hdr_size;
750 while (i < nb_used) {
751 struct virtio_net_hdr_mrg_rxbuf *header;
753 if (nb_rx == nb_pkts)
756 num = virtqueue_dequeue_burst_rx(vq, rcv_pkts, len, 1);
762 PMD_RX_LOG(DEBUG, "dequeue:%d", num);
763 PMD_RX_LOG(DEBUG, "packet len:%d", len[0]);
767 if (unlikely(len[0] < hdr_size + ETHER_HDR_LEN)) {
768 PMD_RX_LOG(ERR, "Packet drop");
770 virtio_discard_rxbuf(vq, rxm);
771 rxvq->stats.errors++;
775 header = (struct virtio_net_hdr_mrg_rxbuf *)((char *)rxm->buf_addr +
776 RTE_PKTMBUF_HEADROOM - hdr_size);
777 seg_num = header->num_buffers;
782 rxm->data_off = RTE_PKTMBUF_HEADROOM;
783 rxm->nb_segs = seg_num;
787 rxm->pkt_len = (uint32_t)(len[0] - hdr_size);
788 rxm->data_len = (uint16_t)(len[0] - hdr_size);
790 rxm->port = rxvq->port_id;
791 rx_pkts[nb_rx] = rxm;
794 seg_res = seg_num - 1;
796 while (seg_res != 0) {
798 * Get extra segments for current uncompleted packet.
801 RTE_MIN(seg_res, RTE_DIM(rcv_pkts));
802 if (likely(VIRTQUEUE_NUSED(vq) >= rcv_cnt)) {
804 virtqueue_dequeue_burst_rx(vq,
805 rcv_pkts, len, rcv_cnt);
810 "No enough segments for packet.");
812 virtio_discard_rxbuf(vq, rxm);
813 rxvq->stats.errors++;
819 while (extra_idx < rcv_cnt) {
820 rxm = rcv_pkts[extra_idx];
822 rxm->data_off = RTE_PKTMBUF_HEADROOM - hdr_size;
824 rxm->pkt_len = (uint32_t)(len[extra_idx]);
825 rxm->data_len = (uint16_t)(len[extra_idx]);
831 rx_pkts[nb_rx]->pkt_len += rxm->pkt_len;
838 rte_vlan_strip(rx_pkts[nb_rx]);
840 VIRTIO_DUMP_PACKET(rx_pkts[nb_rx],
841 rx_pkts[nb_rx]->data_len);
843 rxvq->stats.bytes += rx_pkts[nb_rx]->pkt_len;
844 virtio_update_packet_stats(&rxvq->stats, rx_pkts[nb_rx]);
848 rxvq->stats.packets += nb_rx;
850 /* Allocate new mbuf for the used descriptor */
852 while (likely(!virtqueue_full(vq))) {
853 new_mbuf = rte_mbuf_raw_alloc(rxvq->mpool);
854 if (unlikely(new_mbuf == NULL)) {
855 struct rte_eth_dev *dev
856 = &rte_eth_devices[rxvq->port_id];
857 dev->data->rx_mbuf_alloc_failed++;
860 error = virtqueue_enqueue_recv_refill(vq, new_mbuf);
861 if (unlikely(error)) {
862 rte_pktmbuf_free(new_mbuf);
868 if (likely(nb_enqueued)) {
869 vq_update_avail_idx(vq);
871 if (unlikely(virtqueue_kick_prepare(vq))) {
872 virtqueue_notify(vq);
873 PMD_RX_LOG(DEBUG, "Notified");
881 virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
883 struct virtnet_tx *txvq = tx_queue;
884 struct virtqueue *vq = txvq->vq;
885 struct virtio_hw *hw = vq->hw;
886 uint16_t hdr_size = hw->vtnet_hdr_size;
887 uint16_t nb_used, nb_tx;
890 if (unlikely(nb_pkts < 1))
893 PMD_TX_LOG(DEBUG, "%d packets to xmit", nb_pkts);
894 nb_used = VIRTQUEUE_NUSED(vq);
897 if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh))
898 virtio_xmit_cleanup(vq, nb_used);
900 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
901 struct rte_mbuf *txm = tx_pkts[nb_tx];
902 int can_push = 0, use_indirect = 0, slots, need;
904 /* Do VLAN tag insertion */
905 if (unlikely(txm->ol_flags & PKT_TX_VLAN_PKT)) {
906 error = rte_vlan_insert(&txm);
907 if (unlikely(error)) {
908 rte_pktmbuf_free(txm);
913 /* optimize ring usage */
914 if (vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) &&
915 rte_mbuf_refcnt_read(txm) == 1 &&
916 RTE_MBUF_DIRECT(txm) &&
918 rte_pktmbuf_headroom(txm) >= hdr_size &&
919 rte_is_aligned(rte_pktmbuf_mtod(txm, char *),
920 __alignof__(struct virtio_net_hdr_mrg_rxbuf)))
922 else if (vtpci_with_feature(hw, VIRTIO_RING_F_INDIRECT_DESC) &&
923 txm->nb_segs < VIRTIO_MAX_TX_INDIRECT)
926 /* How many main ring entries are needed to this Tx?
927 * any_layout => number of segments
929 * default => number of segments + 1
931 slots = use_indirect ? 1 : (txm->nb_segs + !can_push);
932 need = slots - vq->vq_free_cnt;
934 /* Positive value indicates it need free vring descriptors */
935 if (unlikely(need > 0)) {
936 nb_used = VIRTQUEUE_NUSED(vq);
938 need = RTE_MIN(need, (int)nb_used);
940 virtio_xmit_cleanup(vq, need);
941 need = slots - vq->vq_free_cnt;
942 if (unlikely(need > 0)) {
944 "No free tx descriptors to transmit");
949 /* Enqueue Packet buffers */
950 virtqueue_enqueue_xmit(txvq, txm, slots, use_indirect, can_push);
952 txvq->stats.bytes += txm->pkt_len;
953 virtio_update_packet_stats(&txvq->stats, txm);
956 txvq->stats.packets += nb_tx;
959 vq_update_avail_idx(vq);
961 if (unlikely(virtqueue_kick_prepare(vq))) {
962 virtqueue_notify(vq);
963 PMD_TX_LOG(DEBUG, "Notified backend after xmit");