To make it reusable, here we abstract the initialization of vring
header into a method.

Signed-off-by: Huawei Xie <huawei.xie at intel.com>
Signed-off-by: Jianfeng Tan <jianfeng.tan at intel.com>
Acked-By: Neil Horman <nhorman at tuxdriver.com>
---
 drivers/net/virtio/virtio_ethdev.c | 22 ++++------------------
 drivers/net/virtio/virtqueue.h     | 20 ++++++++++++++++++++
 2 files changed, 24 insertions(+), 18 deletions(-)

diff --git a/drivers/net/virtio/virtio_ethdev.c 
b/drivers/net/virtio/virtio_ethdev.c
index 534f0e6..0c20fb9 100644
--- a/drivers/net/virtio/virtio_ethdev.c
+++ b/drivers/net/virtio/virtio_ethdev.c
@@ -380,8 +380,7 @@ int virtio_dev_queue_setup(struct rte_eth_dev *dev,

        if (queue_type == VTNET_TQ) {
                const struct rte_memzone *hdr_mz;
-               struct virtio_tx_region *txr;
-               unsigned int i;
+               size_t hdr_mz_sz = vq_size * sizeof(struct virtio_tx_region);

                /*
                 * For each xmit packet, allocate a virtio_net_hdr
@@ -390,7 +389,7 @@ int virtio_dev_queue_setup(struct rte_eth_dev *dev,
                snprintf(vq_name, sizeof(vq_name), "port%d_tvq%d_hdrzone",
                         dev->data->port_id, queue_idx);
                hdr_mz = rte_memzone_reserve_aligned(vq_name,
-                                                    vq_size * sizeof(*txr),
+                                                    hdr_mz_sz,
                                                     socket_id, 0,
                                                     RTE_CACHE_LINE_SIZE);
                if (hdr_mz == NULL) {
@@ -404,21 +403,8 @@ int virtio_dev_queue_setup(struct rte_eth_dev *dev,
                vq->virtio_net_hdr_mz = hdr_mz;
                vq->virtio_net_hdr_mem = hdr_mz->phys_addr;

-               txr = hdr_mz->addr;
-               memset(txr, 0, vq_size * sizeof(*txr));
-               for (i = 0; i < vq_size; i++) {
-                       struct vring_desc *start_dp = txr[i].tx_indir;
-
-                       vring_desc_init(start_dp, RTE_DIM(txr[i].tx_indir));
-
-                       /* first indirect descriptor is always the tx header */
-                       start_dp->addr = vq->virtio_net_hdr_mem
-                               + i * sizeof(*txr)
-                               + offsetof(struct virtio_tx_region, tx_hdr);
-
-                       start_dp->len = vq->hw->vtnet_hdr_size;
-                       start_dp->flags = VRING_DESC_F_NEXT;
-               }
+               memset(hdr_mz->addr, 0, hdr_mz_sz);
+               vring_hdr_desc_init(vq);

        } else if (queue_type == VTNET_CQ) {
                /* Allocate a page for control vq command, data and status */
diff --git a/drivers/net/virtio/virtqueue.h b/drivers/net/virtio/virtqueue.h
index 83d89ca..3b19fd1 100644
--- a/drivers/net/virtio/virtqueue.h
+++ b/drivers/net/virtio/virtqueue.h
@@ -264,6 +264,26 @@ vring_desc_init(struct vring_desc *dp, uint16_t n)
        dp[i].next = VQ_RING_DESC_CHAIN_END;
 }

+static inline void
+vring_hdr_desc_init(struct virtqueue *vq)
+{
+       int i;
+       struct virtio_tx_region *txr = vq->virtio_net_hdr_mz->addr;
+
+       for (i = 0; i < vq->vq_nentries; i++) {
+               struct vring_desc *start_dp = txr[i].tx_indir;
+
+               vring_desc_init(start_dp, RTE_DIM(txr[i].tx_indir));
+
+               /* first indirect descriptor is always the tx header */
+               start_dp->addr = vq->virtio_net_hdr_mem + i * sizeof(*txr) +
+                                offsetof(struct virtio_tx_region, tx_hdr);
+
+               start_dp->len = vq->hw->vtnet_hdr_size;
+               start_dp->flags = VRING_DESC_F_NEXT;
+       }
+}
+
 /**
  * Tell the backend not to interrupt us.
  */
-- 
2.1.4

Reply via email to