virtio: fill Rx avail ring with blank mbufs
[dpdk.git] / drivers / net / virtio / virtio_rxtx.c
index 3ff275c..4fd7546 100644 (file)
@@ -54,6 +54,7 @@
 #include "virtio_logs.h"
 #include "virtio_ethdev.h"
 #include "virtqueue.h"
+#include "virtio_rxtx.h"
 
 #ifdef RTE_LIBRTE_VIRTIO_DEBUG_DUMP
 #define VIRTIO_DUMP_PACKET(m, len) rte_pktmbuf_dump(stdout, m, len)
@@ -61,6 +62,8 @@
 #define  VIRTIO_DUMP_PACKET(m, len) do { } while (0)
 #endif
 
+static int use_simple_rxtx;
+
 static void
 vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx)
 {
@@ -206,7 +209,7 @@ virtqueue_enqueue_xmit(struct virtqueue *txvq, struct rte_mbuf *cookie)
        uint16_t seg_num = cookie->nb_segs;
        uint16_t needed = 1 + seg_num;
        uint16_t head_idx, idx;
-       uint16_t head_size = txvq->hw->vtnet_hdr_size;
+       size_t head_size = txvq->hw->vtnet_hdr_size;
 
        if (unlikely(txvq->vq_free_cnt == 0))
                return -ENOSPC;
@@ -224,7 +227,7 @@ virtqueue_enqueue_xmit(struct virtqueue *txvq, struct rte_mbuf *cookie)
        start_dp = txvq->vq_ring.desc;
        start_dp[idx].addr =
                txvq->virtio_net_hdr_mem + idx * head_size;
-       start_dp[idx].len = (uint32_t)head_size;
+       start_dp[idx].len = head_size;
        start_dp[idx].flags = VRING_DESC_F_NEXT;
 
        for (; ((seg_num > 0) && (cookie != NULL)); seg_num--) {
@@ -298,6 +301,17 @@ virtio_dev_vring_start(struct virtqueue *vq, int queue_type)
                /* Allocate blank mbufs for the each rx descriptor */
                nbufs = 0;
                error = ENOSPC;
+
+               if (use_simple_rxtx)
+                       for (i = 0; i < vq->vq_nentries; i++) {
+                               vq->vq_ring.avail->ring[i] = i;
+                               vq->vq_ring.desc[i].flags = VRING_DESC_F_WRITE;
+                       }
+
+               memset(&vq->fake_mbuf, 0, sizeof(vq->fake_mbuf));
+               for (i = 0; i < RTE_PMD_VIRTIO_RX_MAX_BURST; i++)
+                       vq->sw_ring[vq->vq_nentries + i] = &vq->fake_mbuf;
+
                while (!virtqueue_full(vq)) {
                        m = rte_rxmbuf_alloc(vq->mpool);
                        if (m == NULL)
@@ -306,8 +320,10 @@ virtio_dev_vring_start(struct virtqueue *vq, int queue_type)
                        /******************************************
                        *         Enqueue allocated buffers        *
                        *******************************************/
-                       error = virtqueue_enqueue_recv_refill(vq, m);
-
+                       if (use_simple_rxtx)
+                               error = virtqueue_enqueue_recv_refill_simple(vq, m);
+                       else
+                               error = virtqueue_enqueue_recv_refill(vq, m);
                        if (error) {
                                rte_pktmbuf_free(m);
                                break;
@@ -324,6 +340,24 @@ virtio_dev_vring_start(struct virtqueue *vq, int queue_type)
                VIRTIO_WRITE_REG_4(vq->hw, VIRTIO_PCI_QUEUE_PFN,
                        vq->mz->phys_addr >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
        } else if (queue_type == VTNET_TQ) {
+               if (use_simple_rxtx) {
+                       int mid_idx  = vq->vq_nentries >> 1;
+                       for (i = 0; i < mid_idx; i++) {
+                               vq->vq_ring.avail->ring[i] = i + mid_idx;
+                               vq->vq_ring.desc[i + mid_idx].next = i;
+                               vq->vq_ring.desc[i + mid_idx].addr =
+                                       vq->virtio_net_hdr_mem +
+                                               mid_idx * vq->hw->vtnet_hdr_size;
+                               vq->vq_ring.desc[i + mid_idx].len =
+                                       vq->hw->vtnet_hdr_size;
+                               vq->vq_ring.desc[i + mid_idx].flags =
+                                       VRING_DESC_F_NEXT;
+                               vq->vq_ring.desc[i].flags = 0;
+                       }
+                       for (i = mid_idx; i < vq->vq_nentries; i++)
+                               vq->vq_ring.avail->ring[i] = i;
+               }
+
                VIRTIO_WRITE_REG_2(vq->hw, VIRTIO_PCI_QUEUE_SEL,
                        vq->vq_queue_index);
                VIRTIO_WRITE_REG_4(vq->hw, VIRTIO_PCI_QUEUE_PFN,
@@ -390,7 +424,7 @@ virtio_dev_rx_queue_setup(struct rte_eth_dev *dev,
        ret = virtio_dev_queue_setup(dev, VTNET_RQ, queue_idx, vtpci_queue_idx,
                        nb_desc, socket_id, &vq);
        if (ret < 0) {
-               PMD_INIT_LOG(ERR, "tvq initialization failed");
+               PMD_INIT_LOG(ERR, "rvq initialization failed");
                return ret;
        }
 
@@ -401,6 +435,12 @@ virtio_dev_rx_queue_setup(struct rte_eth_dev *dev,
        return 0;
 }
 
+void
+virtio_dev_rx_queue_release(void *rxq)
+{
+       virtio_dev_queue_release(rxq);
+}
+
 /*
  * struct rte_eth_dev *dev: Used to update dev
  * uint16_t nb_desc: Defaults to values read from config space
@@ -455,6 +495,12 @@ virtio_dev_tx_queue_setup(struct rte_eth_dev *dev,
        return 0;
 }
 
+void
+virtio_dev_tx_queue_release(void *txq)
+{
+       virtio_dev_queue_release(txq);
+}
+
 static void
 virtio_discard_rxbuf(struct virtqueue *vq, struct rte_mbuf *m)
 {
@@ -744,7 +790,7 @@ virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
        nb_used = VIRTQUEUE_NUSED(txvq);
 
        virtio_rmb();
-       if (likely(nb_used > txvq->vq_free_thresh))
+       if (likely(nb_used > txvq->vq_nentries - txvq->vq_free_thresh))
                virtio_xmit_cleanup(txvq, nb_used);
 
        nb_tx = 0;