From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <prvs=6962a9bc3=yrobot@amazon.com>
Received: from smtp-fw-4101.amazon.com (smtp-fw-4101.amazon.com [72.21.198.25])
 by dpdk.org (Postfix) with ESMTP id 021912A61
 for <dev@dpdk.org>; Fri, 18 Sep 2015 19:53:03 +0200 (CEST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209;
 t=1442598783; x=1474134783;
 h=from:to:cc:subject:date:message-id:mime-version;
 bh=Qa0N5UfFZrG3YrBhDrBQYE4eCUYRj1Iumxcm5tZrVIY=;
 b=pO9mChKx/twylUDyPn3sF8UL1L9K7ToLo6ItK+yeyJGupAByPhB9c3A9
 nYD0nV2Ea9huFI/BaK4qusbaIUFN2fW5BA7YmIQXuuUMLw+iLvABCfrjy
 azy/QrUD2ARZZEkZo0WMpAvkg6l3F1C880T78ByvMQb+DhKJ1vN8vd5Am w=;
X-IronPort-AV: E=Sophos;i="5.17,554,1437436800"; d="scan'208";a="345245918"
Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO
 email-inbound-relay-6001.iad6.amazon.com) ([10.43.8.2])
 by smtp-border-fw-out-4101.iad4.amazon.com with ESMTP/TLS/DHE-RSA-AES256-SHA;
 18 Sep 2015 17:53:02 +0000
Received: from ex10-hub-7001.ant.amazon.com (iad1-ws-svc-lb91-vlan2.amazon.com
 [10.0.103.146])
 by email-inbound-relay-6001.iad6.amazon.com (8.14.7/8.14.7) with ESMTP id
 t8IHqlDk028231
 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=FAIL)
 for <dev@dpdk.org>; Fri, 18 Sep 2015 17:53:02 GMT
Received: from EX13D11UWC003.ant.amazon.com (10.43.162.162) by
 ex10-hub-7001.ant.amazon.com (10.43.103.49) with Microsoft SMTP Server (TLS)
 id 14.3.181.6; Fri, 18 Sep 2015 10:52:41 -0700
Received: from uf8bc12800b5c53f661e4.amazon.com (10.43.160.66) by
 EX13D11UWC003.ant.amazon.com (10.43.162.162) with Microsoft SMTP Server (TLS)
 id 15.0.1076.9; Fri, 18 Sep 2015 17:52:40 +0000
From: Yoni Fogel <yrobot@amazon.com>
To: <dev@dpdk.org>
Date: Fri, 18 Sep 2015 10:52:34 -0700
Message-ID: <1442598754-17322-1-git-send-email-yrobot@amazon.com>
X-Mailer: git-send-email 2.5.2
MIME-Version: 1.0
Content-Type: text/plain
X-Originating-IP: [10.43.160.66]
X-ClientProxiedBy: EX13D08UWB004.ant.amazon.com (10.43.161.232) To
 EX13D11UWC003.ant.amazon.com (10.43.162.162)
Precedence: Bulk
Subject: [dpdk-dev] [PATCH] lib: rte_*_create gives NULL/EEXIST on duped name
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.15
List-Id: patches and discussions about DPDK <dev.dpdk.org>
List-Unsubscribe: <http://dpdk.org/ml/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://dpdk.org/ml/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <http://dpdk.org/ml/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
X-List-Received-Date: Fri, 18 Sep 2015 17:53:04 -0000

Also fixed a bug in many of them where if the rte_malloc of
the TAILQ fails, then we return a pointer to some arbitrary
existing struct.
---
 lib/librte_acl/rte_acl.c          | 53 +++++++++++++++++++++------------------
 lib/librte_hash/rte_cuckoo_hash.c |  6 +++--
 lib/librte_hash/rte_fbk_hash.c    |  5 +++-
 lib/librte_lpm/rte_lpm.c          |  5 +++-
 lib/librte_lpm/rte_lpm6.c         |  5 +++-
 5 files changed, 44 insertions(+), 30 deletions(-)

diff --git a/lib/librte_acl/rte_acl.c b/lib/librte_acl/rte_acl.c
index d60219f..f591556 100644
--- a/lib/librte_acl/rte_acl.c
+++ b/lib/librte_acl/rte_acl.c
@@ -213,37 +213,40 @@ rte_acl_create(const struct rte_acl_param *param)
 			break;
 	}
 
+	ctx = NULL;
+	if (te != NULL) {
+		rte_errno = EEXIST;
+		goto exit;
+	}
+
 	/* if ACL with such name doesn't exist, then create a new one. */
-	if (te == NULL) {
-		ctx = NULL;
-		te = rte_zmalloc("ACL_TAILQ_ENTRY", sizeof(*te), 0);
+	te = rte_zmalloc("ACL_TAILQ_ENTRY", sizeof(*te), 0);
 
-		if (te == NULL) {
-			RTE_LOG(ERR, ACL, "Cannot allocate tailq entry!\n");
-			goto exit;
-		}
+	if (te == NULL) {
+		RTE_LOG(ERR, ACL, "Cannot allocate tailq entry!\n");
+		goto exit;
+	}
 
-		ctx = rte_zmalloc_socket(name, sz, RTE_CACHE_LINE_SIZE, param->socket_id);
+	ctx = rte_zmalloc_socket(name, sz, RTE_CACHE_LINE_SIZE, param->socket_id);
 
-		if (ctx == NULL) {
-			RTE_LOG(ERR, ACL,
-				"allocation of %zu bytes on socket %d for %s failed\n",
-				sz, param->socket_id, name);
-			rte_free(te);
-			goto exit;
-		}
-		/* init new allocated context. */
-		ctx->rules = ctx + 1;
-		ctx->max_rules = param->max_rule_num;
-		ctx->rule_sz = param->rule_size;
-		ctx->socket_id = param->socket_id;
-		ctx->alg = rte_acl_default_classify;
-		snprintf(ctx->name, sizeof(ctx->name), "%s", param->name);
+	if (ctx == NULL) {
+		RTE_LOG(ERR, ACL,
+			"allocation of %zu bytes on socket %d for %s failed\n",
+			sz, param->socket_id, name);
+		rte_free(te);
+		goto exit;
+	}
+	/* init new allocated context. */
+	ctx->rules = ctx + 1;
+	ctx->max_rules = param->max_rule_num;
+	ctx->rule_sz = param->rule_size;
+	ctx->socket_id = param->socket_id;
+	ctx->alg = rte_acl_default_classify;
+	snprintf(ctx->name, sizeof(ctx->name), "%s", param->name);
 
-		te->data = (void *) ctx;
+	te->data = (void *) ctx;
 
-		TAILQ_INSERT_TAIL(acl_list, te, next);
-	}
+	TAILQ_INSERT_TAIL(acl_list, te, next);
 
 exit:
 	rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
diff --git a/lib/librte_hash/rte_cuckoo_hash.c b/lib/librte_hash/rte_cuckoo_hash.c
index 7019763..fe5a79e 100644
--- a/lib/librte_hash/rte_cuckoo_hash.c
+++ b/lib/librte_hash/rte_cuckoo_hash.c
@@ -206,8 +206,10 @@ rte_hash_create(const struct rte_hash_parameters *params)
 
 	/* Guarantee there's no existing */
 	h = rte_hash_find_existing(params->name);
-	if (h != NULL)
-		return h;
+	if (h != NULL) {
+		rte_errno = EEXIST;
+		return NULL;
+	}
 
 	te = rte_zmalloc("HASH_TAILQ_ENTRY", sizeof(*te), 0);
 	if (te == NULL) {
diff --git a/lib/librte_hash/rte_fbk_hash.c b/lib/librte_hash/rte_fbk_hash.c
index 8752a47..55c9f35 100644
--- a/lib/librte_hash/rte_fbk_hash.c
+++ b/lib/librte_hash/rte_fbk_hash.c
@@ -140,8 +140,11 @@ rte_fbk_hash_create(const struct rte_fbk_hash_params *params)
 		if (strncmp(params->name, ht->name, RTE_FBK_HASH_NAMESIZE) == 0)
 			break;
 	}
-	if (te != NULL)
+	ht = NULL;
+	if (te != NULL) {
+		rte_errno = EEXIST;
 		goto exit;
+	}
 
 	te = rte_zmalloc("FBK_HASH_TAILQ_ENTRY", sizeof(*te), 0);
 	if (te == NULL) {
diff --git a/lib/librte_lpm/rte_lpm.c b/lib/librte_lpm/rte_lpm.c
index 163ba3c..ea3cd44 100644
--- a/lib/librte_lpm/rte_lpm.c
+++ b/lib/librte_lpm/rte_lpm.c
@@ -181,8 +181,11 @@ rte_lpm_create(const char *name, int socket_id, int max_rules,
 		if (strncmp(name, lpm->name, RTE_LPM_NAMESIZE) == 0)
 			break;
 	}
-	if (te != NULL)
+	lpm = NULL;
+	if (te != NULL) {
+		rte_errno = EEXIST;
 		goto exit;
+	}
 
 	/* allocate tailq entry */
 	te = rte_zmalloc("LPM_TAILQ_ENTRY", sizeof(*te), 0);
diff --git a/lib/librte_lpm/rte_lpm6.c b/lib/librte_lpm/rte_lpm6.c
index 6c2b293..ff0bd76 100644
--- a/lib/librte_lpm/rte_lpm6.c
+++ b/lib/librte_lpm/rte_lpm6.c
@@ -182,8 +182,11 @@ rte_lpm6_create(const char *name, int socket_id,
 		if (strncmp(name, lpm->name, RTE_LPM6_NAMESIZE) == 0)
 			break;
 	}
-	if (te != NULL)
+	lpm = NULL;
+	if (te != NULL) {
+		rte_errno = EEXIST;
 		goto exit;
+	}
 
 	/* allocate tailq entry */
 	te = rte_zmalloc("LPM6_TAILQ_ENTRY", sizeof(*te), 0);
-- 
2.5.2