DPDK patches and discussions
 help / color / mirror / Atom feed
From: Yuanhan Liu <yuanhan.liu@linux.intel.com>
To: dev@dpdk.org
Subject: [dpdk-dev] [PATCH v2 2/3] vhost: simplify numa_realloc
Date: Tue, 22 Dec 2015 15:28:23 +0800	[thread overview]
Message-ID: <1450769304-22986-2-git-send-email-yuanhan.liu@linux.intel.com> (raw)
In-Reply-To: <1450769304-22986-1-git-send-email-yuanhan.liu@linux.intel.com>

We could first check if we need realloc vq or not, if so,
reallocate it. We then do similar to vhost dev realloc.

This could get rid of the tons of repeated "if (realloc_dev)"
and "if (realloc_vq)" statements, therefore, makes code
a bit more readable.

Signed-off-by: Yuanhan Liu <yuanhan.liu@linux.intel.com>
---

v2: fix debug message and check return value of all get_mempolicy()
---
 lib/librte_vhost/virtio-net.c | 77 ++++++++++++++++++++-----------------------
 1 file changed, 36 insertions(+), 41 deletions(-)

diff --git a/lib/librte_vhost/virtio-net.c b/lib/librte_vhost/virtio-net.c
index 2f83438..1566c93 100644
--- a/lib/librte_vhost/virtio-net.c
+++ b/lib/librte_vhost/virtio-net.c
@@ -441,64 +441,59 @@ static struct virtio_net*
 numa_realloc(struct virtio_net *dev, int index)
 {
 	int oldnode, newnode;
-	struct virtio_net *old_dev, *new_dev = NULL;
-	struct vhost_virtqueue *old_vq, *new_vq = NULL;
+	struct virtio_net *old_dev;
+	struct vhost_virtqueue *old_vq, *vq;
 	int ret;
-	int realloc_dev = 0, realloc_vq = 0;
 
 	old_dev = dev;
-	old_vq  = dev->virtqueue[index];
+	vq = old_vq = dev->virtqueue[index];
 
-	ret  = get_mempolicy(&newnode, NULL, 0, old_vq->desc,
-			MPOL_F_NODE | MPOL_F_ADDR);
-	ret = ret | get_mempolicy(&oldnode, NULL, 0, old_dev,
+	ret = get_mempolicy(&newnode, NULL, 0, old_vq->desc,
 			MPOL_F_NODE | MPOL_F_ADDR);
+
+	/* check if we need to reallocate vq */
+	ret |= get_mempolicy(&oldnode, NULL, 0, old_vq, MPOL_F_NODE | MPOL_F_ADDR);
 	if (ret) {
 		RTE_LOG(ERR, VHOST_CONFIG,
-			"Unable to get vring desc or dev numa information.\n");
+			"Unable to get vq numa information.\n");
 		return dev;
 	}
-	if (oldnode != newnode)
-		realloc_dev = 1;
+	if (oldnode != newnode) {
+		RTE_LOG(INFO, VHOST_CONFIG,
+			"reallocate vq from %d to %d node\n", oldnode, newnode);
+		vq = rte_malloc_socket(NULL, sizeof(*vq), 0, newnode);
+		if (!vq)
+			return dev;
+
+		memcpy(vq, old_vq, sizeof(*vq));
+		rte_free(old_vq);
+	}
 
-	ret = get_mempolicy(&oldnode, NULL, 0, old_vq,
-			MPOL_F_NODE | MPOL_F_ADDR);
+	/* check if we need to reallocate dev */
+	ret = get_mempolicy(&oldnode, NULL, 0, old_dev, MPOL_F_NODE | MPOL_F_ADDR);
 	if (ret) {
 		RTE_LOG(ERR, VHOST_CONFIG,
-			"Unable to get vq numa information.\n");
-		return dev;
+			"Unable to get dev numa information.\n");
+		goto out;
 	}
-	if (oldnode != newnode)
-		realloc_vq = 1;
-
-	if (realloc_dev == 0 && realloc_vq == 0)
-		return dev;
-
-	if (realloc_dev)
-		new_dev = rte_malloc_socket(NULL,
-			sizeof(struct virtio_net), 0, newnode);
-	if (realloc_vq)
-		new_vq = rte_malloc_socket(NULL,
-			sizeof(struct vhost_virtqueue), 0, newnode);
-	if (!new_dev && !new_vq)
-		return dev;
-
-	if (realloc_vq)
-		memcpy(new_vq, old_vq, sizeof(*new_vq));
-	if (realloc_dev)
-		memcpy(new_dev, old_dev, sizeof(*new_dev));
+	if (oldnode != newnode) {
+		RTE_LOG(INFO, VHOST_CONFIG,
+			"reallocate dev from %d to %d node\n", oldnode, newnode);
+		dev = rte_malloc_socket(NULL, sizeof(*dev), 0, newnode);
+		if (!dev) {
+			dev = old_dev;
+			goto out;
+		}
 
-	(new_dev ? new_dev : old_dev)->virtqueue[index] =
-		new_vq ? new_vq : old_vq;
-	if (realloc_vq)
-		rte_free(old_vq);
-	if (realloc_dev) {
+		memcpy(dev, old_dev, sizeof(*dev));
 		rte_free(old_dev);
-
-		vhost_devices[new_dev->device_fh] = new_dev;
 	}
 
-	return realloc_dev ? new_dev: dev;
+out:
+	dev->virtqueue[index] = vq;
+	vhost_devices[dev->device_fh] = dev;
+
+	return dev;
 }
 #else
 static struct virtio_net*
-- 
1.9.0

  reply	other threads:[~2015-12-22  7:27 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-12-18  7:04 [dpdk-dev] [PATCH 1/3] vhost: get rid of linked list dev Yuanhan Liu
2015-12-18  7:04 ` [dpdk-dev] [PATCH 2/3] vhost: simplify numa_realloc Yuanhan Liu
2015-12-22  6:46   ` Xie, Huawei
2015-12-22  6:52     ` Yuanhan Liu
2015-12-18  7:04 ` [dpdk-dev] [PATCH 3/3] vhost: fix vq realloc at numa_realloc Yuanhan Liu
2015-12-22  6:56   ` Xie, Huawei
2015-12-22  3:33 ` [dpdk-dev] [PATCH 1/3] vhost: get rid of linked list dev Xie, Huawei
2015-12-22  6:21 ` Xie, Huawei
2015-12-22  7:28 ` [dpdk-dev] [PATCH v2 " Yuanhan Liu
2015-12-22  7:28   ` Yuanhan Liu [this message]
2015-12-22 14:40     ` [dpdk-dev] [PATCH v2 2/3] vhost: simplify numa_realloc Xie, Huawei
2015-12-22  7:28   ` [dpdk-dev] [PATCH v2 3/3] vhost: fix vq realloc at numa_realloc Yuanhan Liu
2016-03-07 13:49     ` Loftus, Ciara
2016-03-08 11:54       ` Yuanhan Liu
2016-03-10  4:19 ` [dpdk-dev] [PATCH v3 0/3] vhost: virtio-net.c cleanups and fixes Yuanhan Liu
2016-03-10  4:19   ` [dpdk-dev] [PATCH v3 1/3] vhost: get rid of linked list dev Yuanhan Liu
2016-03-10  4:20   ` [dpdk-dev] [PATCH v3 2/3] vhost: simplify numa_realloc Yuanhan Liu
2016-03-10  4:20   ` [dpdk-dev] [PATCH v3 3/3] vhost: fix vq realloc at numa_realloc Yuanhan Liu
2016-03-11 15:35   ` [dpdk-dev] [PATCH v3 0/3] vhost: virtio-net.c cleanups and fixes Thomas Monjalon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1450769304-22986-2-git-send-email-yuanhan.liu@linux.intel.com \
    --to=yuanhan.liu@linux.intel.com \
    --cc=dev@dpdk.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).