From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by dpdk.org (Postfix) with ESMTP id D38712965 for ; Thu, 10 Mar 2016 05:17:23 +0100 (CET) Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga103.jf.intel.com with ESMTP; 09 Mar 2016 20:17:23 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.24,314,1455004800"; d="scan'208";a="920663019" Received: from yliu-dev.sh.intel.com ([10.239.66.49]) by fmsmga001.fm.intel.com with ESMTP; 09 Mar 2016 20:17:22 -0800 From: Yuanhan Liu To: dev@dpdk.org Date: Thu, 10 Mar 2016 12:20:00 +0800 Message-Id: <1457583601-27365-3-git-send-email-yuanhan.liu@linux.intel.com> X-Mailer: git-send-email 1.9.0 In-Reply-To: <1457583601-27365-1-git-send-email-yuanhan.liu@linux.intel.com> References: <1450422247-6814-1-git-send-email-yuanhan.liu@linux.intel.com> <1457583601-27365-1-git-send-email-yuanhan.liu@linux.intel.com> Subject: [dpdk-dev] [PATCH v3 2/3] vhost: simplify numa_realloc X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: patches and discussions about DPDK List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 10 Mar 2016 04:17:24 -0000 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 Acked-by: Huawei Xie --- v2: fix debug message and check return value of all get_mempolicy() --- lib/librte_vhost/virtio-net.c | 82 +++++++++++++++++++++---------------------- 1 file changed, 40 insertions(+), 42 deletions(-) diff --git a/lib/librte_vhost/virtio-net.c b/lib/librte_vhost/virtio-net.c index be91e5f..764810e 100644 --- a/lib/librte_vhost/virtio-net.c +++ b/lib/librte_vhost/virtio-net.c @@ -444,64 +444,62 @@ 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, - MPOL_F_NODE | MPOL_F_ADDR); + 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