DPDK patches and discussions
 help / color / mirror / Atom feed
From: Konstantin Ananyev <konstantin.ananyev@intel.com>
To: dev@dpdk.org
Subject: [dpdk-dev] [PATCH 3/8] ACL: add function to check rte_acl_build() input parameters
Date: Thu,  4 Jun 2015 00:10:19 +0100	[thread overview]
Message-ID: <1433373024-5558-4-git-send-email-konstantin.ananyev@intel.com> (raw)
In-Reply-To: <1433373024-5558-1-git-send-email-konstantin.ananyev@intel.com>

Move check for build parameters into a separate function.
Simplify acl_calc_wildness() function.

Signed-off-by: Konstantin Ananyev <konstantin.ananyev@intel.com>
---
 lib/librte_acl/acl_bld.c | 107 ++++++++++++++++++++++++-----------------------
 1 file changed, 54 insertions(+), 53 deletions(-)

diff --git a/lib/librte_acl/acl_bld.c b/lib/librte_acl/acl_bld.c
index 19a4178..8315d84 100644
--- a/lib/librte_acl/acl_bld.c
+++ b/lib/librte_acl/acl_bld.c
@@ -1350,7 +1350,7 @@ build_trie(struct acl_build_context *context, struct rte_acl_build_rule *head,
 	return trie;
 }
 
-static int
+static void
 acl_calc_wildness(struct rte_acl_build_rule *head,
 	const struct rte_acl_config *config)
 {
@@ -1362,10 +1362,10 @@ acl_calc_wildness(struct rte_acl_build_rule *head,
 		for (n = 0; n < config->num_fields; n++) {
 
 			double wild = 0;
-			uint64_t msk_val =
-				RTE_LEN2MASK(CHAR_BIT * config->defs[n].size,
+			uint32_t bit_len = CHAR_BIT * config->defs[n].size;
+			uint64_t msk_val = RTE_LEN2MASK(bit_len,
 				typeof(msk_val));
-			double size = CHAR_BIT * config->defs[n].size;
+			double size = bit_len;
 			int field_index = config->defs[n].field_index;
 			const struct rte_acl_field *fld = rule->f->field +
 				field_index;
@@ -1382,54 +1382,15 @@ acl_calc_wildness(struct rte_acl_build_rule *head,
 				break;
 
 			case RTE_ACL_FIELD_TYPE_RANGE:
-				switch (rule->config->defs[n].size) {
-				case sizeof(uint8_t):
-					wild = ((double)fld->mask_range.u8 -
-						fld->value.u8) / UINT8_MAX;
-					break;
-				case sizeof(uint16_t):
-					wild = ((double)fld->mask_range.u16 -
-						fld->value.u16) / UINT16_MAX;
-					break;
-				case sizeof(uint32_t):
-					wild = ((double)fld->mask_range.u32 -
-						fld->value.u32) / UINT32_MAX;
-					break;
-				case sizeof(uint64_t):
-					wild = ((double)fld->mask_range.u64 -
-						fld->value.u64) / UINT64_MAX;
-					break;
-				default:
-					RTE_LOG(ERR, ACL,
-						"%s(rule: %u) invalid %u-th "
-						"field, type: %hhu, "
-						"unknown size: %hhu\n",
-						__func__,
-						rule->f->data.userdata,
-						n,
-						rule->config->defs[n].type,
-						rule->config->defs[n].size);
-					return -EINVAL;
-				}
+				wild = (fld->mask_range.u64 & msk_val) -
+					(fld->value.u64 & msk_val);
+				wild = wild / msk_val;
 				break;
-
-			default:
-				RTE_LOG(ERR, ACL,
-					"%s(rule: %u) invalid %u-th "
-					"field, unknown type: %hhu\n",
-					__func__,
-					rule->f->data.userdata,
-					n,
-					rule->config->defs[n].type);
-					return -EINVAL;
-
 			}
 
 			rule->wildness[field_index] = (uint32_t)(wild * 100);
 		}
 	}
-
-	return 0;
 }
 
 static void
@@ -1602,7 +1563,6 @@ static int
 acl_build_tries(struct acl_build_context *context,
 	struct rte_acl_build_rule *head)
 {
-	int32_t rc;
 	uint32_t n, num_tries;
 	struct rte_acl_config *config;
 	struct rte_acl_build_rule *last;
@@ -1621,9 +1581,7 @@ acl_build_tries(struct acl_build_context *context,
 	context->tries[0].type = RTE_ACL_FULL_TRIE;
 
 	/* calc wildness of each field of each rule */
-	rc = acl_calc_wildness(head, config);
-	if (rc != 0)
-		return rc;
+	acl_calc_wildness(head, config);
 
 	for (n = 0;; n = num_tries) {
 
@@ -1801,6 +1759,49 @@ acl_bld(struct acl_build_context *bcx, struct rte_acl_ctx *ctx,
 	return rc;
 }
 
+/*
+ * Check that parameters for acl_build() are valid.
+ */
+static int
+acl_check_bld_param(struct rte_acl_ctx *ctx, const struct rte_acl_config *cfg)
+{
+	static const size_t field_sizes[] = {
+		sizeof(uint8_t), sizeof(uint16_t),
+		sizeof(uint32_t), sizeof(uint64_t),
+	};
+
+	uint32_t i, j;
+
+	if (ctx == NULL || cfg == NULL || cfg->num_categories == 0 ||
+			cfg->num_categories > RTE_ACL_MAX_CATEGORIES ||
+			cfg->num_fields == 0 ||
+			cfg->num_fields > RTE_ACL_MAX_FIELDS)
+		return -EINVAL;
+
+	for (i = 0; i != cfg->num_fields; i++) {
+		if (cfg->defs[i].type > RTE_ACL_FIELD_TYPE_BITMASK) {
+			RTE_LOG(ERR, ACL,
+			"ACL context: %s, invalid type: %hhu for %u-th field\n",
+			ctx->name, cfg->defs[i].type, i);
+			return -EINVAL;
+		}
+		for (j = 0;
+				j != RTE_DIM(field_sizes) &&
+				cfg->defs[i].size != field_sizes[j];
+				j++)
+			;
+
+		if (j == RTE_DIM(field_sizes)) {
+			RTE_LOG(ERR, ACL,
+			"ACL context: %s, invalid size: %hhu for %u-th field\n",
+			ctx->name, cfg->defs[i].size, i);
+			return -EINVAL;
+		}
+	}
+
+	return 0;
+}
+
 int
 rte_acl_build(struct rte_acl_ctx *ctx, const struct rte_acl_config *cfg)
 {
@@ -1809,9 +1810,9 @@ rte_acl_build(struct rte_acl_ctx *ctx, const struct rte_acl_config *cfg)
 	size_t max_size;
 	struct acl_build_context bcx;
 
-	if (ctx == NULL || cfg == NULL || cfg->num_categories == 0 ||
-			cfg->num_categories > RTE_ACL_MAX_CATEGORIES)
-		return -EINVAL;
+	rc = acl_check_bld_param(ctx, cfg);
+	if (rc != 0)
+		return rc;
 
 	acl_build_reset(ctx);
 
-- 
1.8.5.3

  parent reply	other threads:[~2015-06-03 23:10 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-03 23:10 [dpdk-dev] [PATCH 0/8] ACL: various fixes and cleanups Konstantin Ananyev
2015-06-03 23:10 ` [dpdk-dev] [PATCH 1/8] ACL: fix invalid rule wildness calculation for RTE_ACL_FIELD_TYPE_BITMASK Konstantin Ananyev
2015-06-08 10:41   ` [dpdk-dev] [PATCHv2 0/8] acl: various fixes and cleanups Konstantin Ananyev
2015-06-08 10:41     ` [dpdk-dev] [PATCHv2 1/8] acl: fix invalid rule wildness calculation for bitmask field type Konstantin Ananyev
2015-06-08 10:41     ` [dpdk-dev] [PATCHv2 2/8] acl: code cleanup - use global EAL macro, instead of creating a local copy Konstantin Ananyev
2015-06-08 10:41     ` [dpdk-dev] [PATCHv2 3/8] acl: add function to check build input parameters Konstantin Ananyev
2015-06-08 10:41     ` [dpdk-dev] [PATCHv2 4/8] acl: fix avoid unneeded trie splitting for subset of rules Konstantin Ananyev
2015-06-08 10:41     ` [dpdk-dev] [PATCHv2 5/8] acl: code dedup - introduce a new macro Konstantin Ananyev
2015-06-08 10:41     ` [dpdk-dev] [PATCHv2 6/8] acl: cleanup remove unused code from acl_bld.c Konstantin Ananyev
2015-06-08 10:41     ` [dpdk-dev] [PATCHv2 7/8] acl: fix ambiguity between ACL rules in UT Konstantin Ananyev
2015-06-08 10:41     ` [dpdk-dev] [PATCHv2 8/8] acl: add new test-cases into UT Konstantin Ananyev
2015-06-18 16:14     ` [dpdk-dev] [PATCHv2 0/8] acl: various fixes and cleanups Thomas Monjalon
2015-06-03 23:10 ` [dpdk-dev] [PATCH 2/8] ACL: code cleanup - use global RTE_LEN2MASK macro Konstantin Ananyev
2015-06-03 23:10 ` Konstantin Ananyev [this message]
2015-06-03 23:10 ` [dpdk-dev] [PATCH 4/8] ACL: fix rebuilding a trie for subset of rules Konstantin Ananyev
2015-06-03 23:10 ` [dpdk-dev] [PATCH 5/8] ACL: introduce RTE_ACL_MASKLEN_TO_BITMASK macro Konstantin Ananyev
2015-06-03 23:10 ` [dpdk-dev] [PATCH 6/8] ACL: cleanup remove unused code from acl_bld.c Konstantin Ananyev
2015-06-03 23:10 ` [dpdk-dev] [PATCH 7/8] ACL: fix remove ambiguity between rules at UT Konstantin Ananyev
2015-06-03 23:10 ` [dpdk-dev] [PATCH 8/8] ACL: add new test-cases into UT Konstantin Ananyev
2015-06-04  9:27 ` [dpdk-dev] [PATCH 0/8] ACL: various fixes and cleanups Thomas Monjalon
2015-06-04 10:20   ` Ananyev, Konstantin

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=1433373024-5558-4-git-send-email-konstantin.ananyev@intel.com \
    --to=konstantin.ananyev@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).