From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id B1BACA0A0E; Thu, 4 Feb 2021 08:34:34 +0100 (CET) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 33BDE240573; Thu, 4 Feb 2021 08:34:34 +0100 (CET) Received: from mail-wr1-f53.google.com (mail-wr1-f53.google.com [209.85.221.53]) by mails.dpdk.org (Postfix) with ESMTP id C2D88240570 for ; Thu, 4 Feb 2021 08:34:32 +0100 (CET) Received: by mail-wr1-f53.google.com with SMTP id l12so2308573wry.2 for ; Wed, 03 Feb 2021 23:34:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=emumba-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id; bh=0CkJHgMooaCSAlWV4f6dM8g+t8Ln0RTTPcI4Z4TJ7Xs=; b=t9DaquAR9yw1zr68QUMywMsgD4LlEToyBQg9auyk3m6N58EPFwmEn+RUvvtJSl2NUC M4D0YW1iZ2bdn953TlDjv4fjL8O1gC5cpJmLrhEWNDb8faFmZvHkFYnivLxB/yukQezr EdiJ4Ys7yNXcZ8ysfEbHcs1dvbGdMZkVAobxt935uXl0blBVD5k6/Yp6vGBEMs1N1kOG SMID//KQ113SM5ct1Slxkj9Z/dCfKgDDqbWSm2BsMJO4lWQhDf/JNwE0roRh5lm5mjUR y9kjKneMT8ECz+Klwz68CEiXjRw7HPzzBS+aKUXOSRL4y/L62mqyZWQZI+RryjH/jvdC E5ZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=0CkJHgMooaCSAlWV4f6dM8g+t8Ln0RTTPcI4Z4TJ7Xs=; b=FTJWAGiEzS0hQdBwdlGhuWjtFKUKT6KN1TQAn4TNrpDYy1LovNLdwowgtxWqdS4XWx BMaTA3mpZxGEQj109mnIGNQG84OayA4qAl00Ak+zh1WyH/gZAp5k4gI5RhEzimtQtUqU ortKE+/oUX9p762C46vg0HpDdYlZlg0PbQO8TlTFY8nn3j0Ds34oaeJ48CRNJK8ml6Y2 xl+oSLwTv8ZejQocvuvs5StyKLt542PDxBlTKt0/yXZLycyzvy46UvbJX3YflmbcPxRQ Q7ms47BtDvk80g/JfO7V0cM+8037tCiELPfWrmcwzb9FmQqCgENOsb6sM54VHoDkH6bT poxQ== X-Gm-Message-State: AOAM532SY1czrsFH//g0OY5hBhNXsVN6E5yQzL20IxEqMJZtkzDz4EEr I6RH9z3XA4FnUaIycOKtjDi3rg== X-Google-Smtp-Source: ABdhPJwAP8mTPkSaojSf5gtHbdgOBuvdnQd0p1iGCqxWKNXEZkx99Nq+UgiF/Hk4JIGegaK0hsnaHw== X-Received: by 2002:a05:6000:192:: with SMTP id p18mr7562669wrx.69.1612424072452; Wed, 03 Feb 2021 23:34:32 -0800 (PST) Received: from localhost.localdomain ([39.33.143.135]) by smtp.gmail.com with ESMTPSA id j11sm7082604wrt.26.2021.02.03.23.34.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Feb 2021 23:34:31 -0800 (PST) From: Ibtisam Tariq To: maxime.coquelin@redhat.com, chenbo.xia@intel.com, cristian.dumitrescu@intel.com, jasvinder.singh@intel.com, john.mcnamara@intel.com, reshma.pattan@intel.com, konstantin.ananyev@intel.com, marko.kovacevic@intel.com Cc: dev@dpdk.org, Ibtisam Tariq Date: Thu, 4 Feb 2021 07:34:14 +0000 Message-Id: <20210204073420.2421-1-ibtisam.tariq@emumba.com> X-Mailer: git-send-email 2.17.1 Subject: [dpdk-dev] [PATCH v4 1/7] examples/fips_validation: enhance getopt_long usage X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Instead of using getopt_long return value, strcmp was used to compare the input parameters with the struct option array. This patch get rid of all those strcmp by directly binding each longopt with an int enum. This is to improve readability and consistency in all examples. Bugzilla ID: 238 Cc: marko.kovacevic@intel.com Reported-by: David Marchand Signed-off-by: Ibtisam Tariq --- v4: * Set indentation of preprocessor directives. * Remove condition on question mark. v3: * None. v2: * Added parser_read_uint16 function to remove extra variable. * Remove extra indentations. * Remove extra block brackets in switch statement. * Replace the pre-processor with actual value, if it used once. * Change enum names to start with OPT_ and remove KEYWORD from enum names. v1: * enhance getopt_long usage. --- examples/fips_validation/fips_validation.c | 16 ++ examples/fips_validation/fips_validation.h | 3 + examples/fips_validation/main.c | 246 +++++++++++---------- 3 files changed, 151 insertions(+), 114 deletions(-) diff --git a/examples/fips_validation/fips_validation.c b/examples/fips_validation/fips_validation.c index 4c3ed80c8..52a7bf952 100644 --- a/examples/fips_validation/fips_validation.c +++ b/examples/fips_validation/fips_validation.c @@ -650,6 +650,22 @@ parser_read_uint32(uint32_t *value, char *p) return 0; } +int +parser_read_uint16(uint16_t *value, const char *p) +{ + uint64_t val = 0; + int ret = parser_read_uint64(&val, p); + + if (ret < 0) + return ret; + + if (val > UINT16_MAX) + return -ERANGE; + + *value = val; + return 0; +} + void parse_write_hex_str(struct fips_val *src) { diff --git a/examples/fips_validation/fips_validation.h b/examples/fips_validation/fips_validation.h index 8396932f9..6cda4e0bd 100644 --- a/examples/fips_validation/fips_validation.h +++ b/examples/fips_validation/fips_validation.h @@ -257,6 +257,9 @@ parser_read_uint32(uint32_t *value, char *p); int parser_read_uint32_val(const char *key, char *src, struct fips_val *val); +int +parser_read_uint16(uint16_t *value, const char *p); + int writeback_hex_str(const char *key, char *dst, struct fips_val *val); diff --git a/examples/fips_validation/main.c b/examples/fips_validation/main.c index cad6bcb18..b73691dd2 100644 --- a/examples/fips_validation/main.c +++ b/examples/fips_validation/main.c @@ -15,17 +15,26 @@ #include "fips_validation.h" #include "fips_dev_self_test.h" -#define REQ_FILE_PATH_KEYWORD "req-file" -#define RSP_FILE_PATH_KEYWORD "rsp-file" -#define MBUF_DATAROOM_KEYWORD "mbuf-dataroom" -#define FOLDER_KEYWORD "path-is-folder" -#define CRYPTODEV_KEYWORD "cryptodev" -#define CRYPTODEV_ID_KEYWORD "cryptodev-id" -#define CRYPTODEV_ST_KEYWORD "self-test" -#define CRYPTODEV_BK_ID_KEYWORD "broken-test-id" -#define CRYPTODEV_BK_DIR_KEY "broken-test-dir" -#define CRYPTODEV_ENC_KEYWORD "enc" -#define CRYPTODEV_DEC_KEYWORD "dec" +enum { +#define OPT_REQ_FILE_PATH "req-file" + OPT_REQ_FILE_PATH_NUM = 256, +#define OPT_RSP_FILE_PATH "rsp-file" + OPT_RSP_FILE_PATH_NUM, +#define OPT_MBUF_DATAROOM "mbuf-dataroom" + OPT_MBUF_DATAROOM_NUM, +#define OPT_FOLDER "path-is-folder" + OPT_FOLDER_NUM, +#define OPT_CRYPTODEV "cryptodev" + OPT_CRYPTODEV_NUM, +#define OPT_CRYPTODEV_ID "cryptodev-id" + OPT_CRYPTODEV_ID_NUM, +#define OPT_CRYPTODEV_ST "self-test" + OPT_CRYPTODEV_ST_NUM, +#define OPT_CRYPTODEV_BK_ID "broken-test-id" + OPT_CRYPTODEV_BK_ID_NUM, +#define OPT_CRYPTODEV_BK_DIR_KEY "broken-test-dir" + OPT_CRYPTODEV_BK_DIR_KEY_NUM, +}; struct fips_test_vector vec; struct fips_test_interim_info info; @@ -212,10 +221,10 @@ cryptodev_fips_validate_usage(const char *prgname) " --%s: self test indicator\n" " --%s: self broken test ID\n" " --%s: self broken test direction\n", - prgname, REQ_FILE_PATH_KEYWORD, RSP_FILE_PATH_KEYWORD, - FOLDER_KEYWORD, MBUF_DATAROOM_KEYWORD, def_mbuf_seg_size, - CRYPTODEV_KEYWORD, CRYPTODEV_ID_KEYWORD, CRYPTODEV_ST_KEYWORD, - CRYPTODEV_BK_ID_KEYWORD, CRYPTODEV_BK_DIR_KEY); + prgname, OPT_REQ_FILE_PATH, OPT_RSP_FILE_PATH, + OPT_FOLDER, OPT_MBUF_DATAROOM, def_mbuf_seg_size, + OPT_CRYPTODEV, OPT_CRYPTODEV_ID, OPT_CRYPTODEV_ST, + OPT_CRYPTODEV_BK_ID, OPT_CRYPTODEV_BK_DIR_KEY); } static int @@ -226,16 +235,25 @@ cryptodev_fips_validate_parse_args(int argc, char **argv) char **argvopt; int option_index; struct option lgopts[] = { - {REQ_FILE_PATH_KEYWORD, required_argument, 0, 0}, - {RSP_FILE_PATH_KEYWORD, required_argument, 0, 0}, - {FOLDER_KEYWORD, no_argument, 0, 0}, - {MBUF_DATAROOM_KEYWORD, required_argument, 0, 0}, - {CRYPTODEV_KEYWORD, required_argument, 0, 0}, - {CRYPTODEV_ID_KEYWORD, required_argument, 0, 0}, - {CRYPTODEV_ST_KEYWORD, no_argument, 0, 0}, - {CRYPTODEV_BK_ID_KEYWORD, required_argument, 0, 0}, - {CRYPTODEV_BK_DIR_KEY, required_argument, 0, 0}, - {NULL, 0, 0, 0} + {OPT_REQ_FILE_PATH, required_argument, + NULL, OPT_REQ_FILE_PATH_NUM}, + {OPT_RSP_FILE_PATH, required_argument, + NULL, OPT_RSP_FILE_PATH_NUM}, + {OPT_FOLDER, no_argument, + NULL, OPT_FOLDER_NUM}, + {OPT_MBUF_DATAROOM, required_argument, + NULL, OPT_MBUF_DATAROOM_NUM}, + {OPT_CRYPTODEV, required_argument, + NULL, OPT_CRYPTODEV_NUM}, + {OPT_CRYPTODEV_ID, required_argument, + NULL, OPT_CRYPTODEV_ID_NUM}, + {OPT_CRYPTODEV_ST, no_argument, + NULL, OPT_CRYPTODEV_ST_NUM}, + {OPT_CRYPTODEV_BK_ID, required_argument, + NULL, OPT_CRYPTODEV_BK_ID_NUM}, + {OPT_CRYPTODEV_BK_DIR_KEY, required_argument, + NULL, OPT_CRYPTODEV_BK_DIR_KEY_NUM}, + {NULL, 0, 0, 0} }; argvopt = argv; @@ -252,104 +270,104 @@ cryptodev_fips_validate_parse_args(int argc, char **argv) lgopts, &option_index)) != EOF) { switch (opt) { - case 0: - if (strcmp(lgopts[option_index].name, - REQ_FILE_PATH_KEYWORD) == 0) - env.req_path = optarg; - else if (strcmp(lgopts[option_index].name, - RSP_FILE_PATH_KEYWORD) == 0) - env.rsp_path = optarg; - else if (strcmp(lgopts[option_index].name, - FOLDER_KEYWORD) == 0) - env.is_path_folder = 1; - else if (strcmp(lgopts[option_index].name, - CRYPTODEV_KEYWORD) == 0) { - ret = parse_cryptodev_arg(optarg); - if (ret < 0) { - cryptodev_fips_validate_usage(prgname); - return -EINVAL; - } - } else if (strcmp(lgopts[option_index].name, - CRYPTODEV_ID_KEYWORD) == 0) { - ret = parse_cryptodev_id_arg(optarg); - if (ret < 0) { - cryptodev_fips_validate_usage(prgname); - return -EINVAL; - } - } else if (strcmp(lgopts[option_index].name, - CRYPTODEV_ST_KEYWORD) == 0) { - env.self_test = 1; - } else if (strcmp(lgopts[option_index].name, - CRYPTODEV_BK_ID_KEYWORD) == 0) { - if (!env.broken_test_config) { - env.broken_test_config = rte_malloc( - NULL, - sizeof(*env.broken_test_config), - 0); - if (!env.broken_test_config) - return -ENOMEM; - - env.broken_test_config->expect_fail_dir = - self_test_dir_enc_auth_gen; - } + case OPT_REQ_FILE_PATH_NUM: + env.req_path = optarg; + break; - if (parser_read_uint32( - &env.broken_test_config->expect_fail_test_idx, - optarg) < 0) { - rte_free(env.broken_test_config); - cryptodev_fips_validate_usage(prgname); - return -EINVAL; - } - } else if (strcmp(lgopts[option_index].name, - CRYPTODEV_BK_DIR_KEY) == 0) { - if (!env.broken_test_config) { - env.broken_test_config = rte_malloc( - NULL, - sizeof(*env.broken_test_config), - 0); - if (!env.broken_test_config) - return -ENOMEM; - - env.broken_test_config-> - expect_fail_test_idx = 0; - } + case OPT_RSP_FILE_PATH_NUM: + env.rsp_path = optarg; + break; - if (strcmp(optarg, CRYPTODEV_ENC_KEYWORD) == 0) - env.broken_test_config->expect_fail_dir = - self_test_dir_enc_auth_gen; - else if (strcmp(optarg, CRYPTODEV_DEC_KEYWORD) - == 0) - env.broken_test_config->expect_fail_dir = - self_test_dir_dec_auth_verify; - else { - rte_free(env.broken_test_config); - cryptodev_fips_validate_usage(prgname); - return -EINVAL; - } - } else if (strcmp(lgopts[option_index].name, - MBUF_DATAROOM_KEYWORD) == 0) { - uint32_t data_room_size; - - if (parser_read_uint32(&data_room_size, - optarg) < 0) { - cryptodev_fips_validate_usage(prgname); - return -EINVAL; - } + case OPT_FOLDER_NUM: + env.is_path_folder = 1; + break; - if (data_room_size == 0 || - data_room_size > UINT16_MAX) { - cryptodev_fips_validate_usage(prgname); - return -EINVAL; - } + case OPT_CRYPTODEV_NUM: + ret = parse_cryptodev_arg(optarg); + if (ret < 0) { + cryptodev_fips_validate_usage(prgname); + return -EINVAL; + } + break; - env.mbuf_data_room = data_room_size; - } else { + case OPT_CRYPTODEV_ID_NUM: + ret = parse_cryptodev_id_arg(optarg); + if (ret < 0) { cryptodev_fips_validate_usage(prgname); return -EINVAL; } break; + + case OPT_CRYPTODEV_ST_NUM: + env.self_test = 1; + break; + + case OPT_CRYPTODEV_BK_ID_NUM: + if (!env.broken_test_config) { + env.broken_test_config = rte_malloc( + NULL, + sizeof(*env.broken_test_config), + 0); + if (!env.broken_test_config) + return -ENOMEM; + + env.broken_test_config->expect_fail_dir = + self_test_dir_enc_auth_gen; + } + + if (parser_read_uint32( + &env.broken_test_config->expect_fail_test_idx, + optarg) < 0) { + rte_free(env.broken_test_config); + cryptodev_fips_validate_usage(prgname); + return -EINVAL; + } + break; + + case OPT_CRYPTODEV_BK_DIR_KEY_NUM: + if (!env.broken_test_config) { + env.broken_test_config = rte_malloc( + NULL, + sizeof(*env.broken_test_config), + 0); + if (!env.broken_test_config) + return -ENOMEM; + + env.broken_test_config->expect_fail_test_idx = + 0; + } + + if (strcmp(optarg, "enc") == 0) + env.broken_test_config->expect_fail_dir = + self_test_dir_enc_auth_gen; + else if (strcmp(optarg, "dec") + == 0) + env.broken_test_config->expect_fail_dir = + self_test_dir_dec_auth_verify; + else { + rte_free(env.broken_test_config); + cryptodev_fips_validate_usage(prgname); + return -EINVAL; + } + break; + + + case OPT_MBUF_DATAROOM_NUM: + if (parser_read_uint16(&env.mbuf_data_room, + optarg) < 0) { + cryptodev_fips_validate_usage(prgname); + return -EINVAL; + } + + if (env.mbuf_data_room == 0) { + cryptodev_fips_validate_usage(prgname); + return -EINVAL; + } + break; + default: - return -1; + cryptodev_fips_validate_usage(prgname); + return -EINVAL; } } -- 2.17.1