examples/l3fwd: support config file for EM
[dpdk.git] / examples / fips_validation / main.c
index 07532c9..e06ae37 100644 (file)
@@ -7,7 +7,7 @@
 #include <dirent.h>
 
 #include <rte_cryptodev.h>
-#include <rte_cryptodev_pmd.h>
+#include <rte_malloc.h>
 #include <rte_mempool.h>
 #include <rte_mbuf.h>
 #include <rte_string_fns.h>
 #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;
@@ -64,10 +73,7 @@ cryptodev_fips_validate_app_int(void)
        if (env.self_test) {
                ret = fips_dev_self_test(env.dev_id, env.broken_test_config);
                if (ret < 0) {
-                       struct rte_cryptodev *cryptodev =
-                                       rte_cryptodev_pmd_get_dev(env.dev_id);
-
-                       rte_cryptodev_pmd_destroy(cryptodev);
+                       rte_cryptodev_close(env.dev_id);
 
                        return ret;
                }
@@ -128,17 +134,18 @@ cryptodev_fips_validate_app_int(void)
        if (ret < 0)
                goto error_exit;
 
+       ret = rte_cryptodev_start(env.dev_id);
+       if (ret < 0)
+               goto error_exit;
+
        return 0;
 
 error_exit:
 
        rte_mempool_free(env.mpool);
-       if (env.sess_mpool)
-               rte_mempool_free(env.sess_mpool);
-       if (env.sess_priv_mpool)
-               rte_mempool_free(env.sess_priv_mpool);
-       if (env.op_pool)
-               rte_mempool_free(env.op_pool);
+       rte_mempool_free(env.sess_mpool);
+       rte_mempool_free(env.sess_priv_mpool);
+       rte_mempool_free(env.op_pool);
 
        return ret;
 }
@@ -187,7 +194,7 @@ parse_cryptodev_id_arg(char *arg)
        }
 
 
-       if (!rte_cryptodev_pmd_is_valid_dev(cryptodev_id)) {
+       if (!rte_cryptodev_is_valid_dev(cryptodev_id)) {
                RTE_LOG(ERR, USER1, "Error %i: invalid cryptodev id %s\n",
                                cryptodev_id, arg);
                return -1;
@@ -212,10 +219,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 +233,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 +268,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;
                }
        }
 
@@ -483,6 +499,9 @@ exit:
        fips_test_clear();
        cryptodev_fips_validate_app_uninit();
 
+       /* clean up the EAL */
+       rte_eal_cleanup();
+
        return ret;
 
 }
@@ -501,8 +520,7 @@ prepare_data_mbufs(struct fips_val *val)
        uint16_t nb_seg;
        int ret = 0;
 
-       if (env.mbuf)
-               rte_pktmbuf_free(env.mbuf);
+       rte_pktmbuf_free(env.mbuf);
 
        if (total_len > RTE_MBUF_MAX_NB_SEGS) {
                RTE_LOG(ERR, USER1, "Data len %u too big\n", total_len);
@@ -576,8 +594,7 @@ prepare_data_mbufs(struct fips_val *val)
        return 0;
 
 error_exit:
-       if (head)
-               rte_pktmbuf_free(head);
+       rte_pktmbuf_free(head);
        return ret;
 }
 
@@ -634,8 +651,7 @@ prepare_auth_op(void)
        if (ret < 0)
                return ret;
 
-       if (env.digest)
-               rte_free(env.digest);
+       rte_free(env.digest);
 
        env.digest = rte_zmalloc(NULL, vec.cipher_auth.digest.len,
                        RTE_CACHE_LINE_SIZE);
@@ -683,8 +699,7 @@ prepare_aead_op(void)
                if (ret < 0)
                        return ret;
 
-               if (env.digest)
-                       rte_free(env.digest);
+               rte_free(env.digest);
                env.digest = rte_zmalloc(NULL, vec.aead.digest.len,
                                RTE_CACHE_LINE_SIZE);
                if (!env.digest) {
@@ -927,10 +942,14 @@ prepare_gmac_xform(struct rte_crypto_sym_xform *xform)
 
        if (rte_cryptodev_sym_capability_check_auth(cap,
                        auth_xform->key.length,
-                       auth_xform->digest_length, 0) != 0) {
-               RTE_LOG(ERR, USER1, "PMD %s key length %u IV length %u\n",
+                       auth_xform->digest_length,
+                       auth_xform->iv.length) != 0) {
+
+               RTE_LOG(ERR, USER1,
+                       "PMD %s key length %u Digest length %u IV length %u\n",
                                info.device_name, auth_xform->key.length,
-                               auth_xform->digest_length);
+                               auth_xform->digest_length,
+                               auth_xform->iv.length);
                return -EPERM;
        }
 
@@ -1128,6 +1147,7 @@ get_writeback_data(struct fips_val *val)
 
        if (data_len) {
                RTE_LOG(ERR, USER1, "Error -1: write back data\n");
+               free(wb_data);
                return -1;
        }
 
@@ -1403,8 +1423,7 @@ fips_mct_tdes_test(void)
                }
        }
 
-       if (val.val)
-               free(val.val);
+       free(val.val);
 
        return 0;
 }
@@ -1483,8 +1502,7 @@ fips_mct_aes_ecb_test(void)
                }
        }
 
-       if (val.val)
-               free(val.val);
+       free(val.val);
 
        return 0;
 }
@@ -1520,7 +1538,9 @@ fips_mct_aes_test(void)
                                return ret;
                        }
 
-                       get_writeback_data(&val);
+                       ret = get_writeback_data(&val);
+                       if (ret < 0)
+                               return ret;
 
                        if (info.op == FIPS_TEST_DEC_AUTH_VERIF)
                                memcpy(prev_in, vec.ct.val, AES_BLOCK_SIZE);
@@ -1590,8 +1610,7 @@ fips_mct_aes_test(void)
                        memcpy(vec.iv.val, val.val, AES_BLOCK_SIZE);
        }
 
-       if (val.val)
-               free(val.val);
+       free(val.val);
 
        return 0;
 }
@@ -1607,7 +1626,6 @@ fips_mct_sha_test(void)
        int ret;
        uint32_t i, j;
 
-       val.val = rte_malloc(NULL, (MAX_DIGEST_SIZE*SHA_MD_BLOCK), 0);
        for (i = 0; i < SHA_MD_BLOCK; i++)
                md[i].val = rte_malloc(NULL, (MAX_DIGEST_SIZE*2), 0);
 
@@ -1649,7 +1667,9 @@ fips_mct_sha_test(void)
                                return ret;
                        }
 
-                       get_writeback_data(&val);
+                       ret = get_writeback_data(&val);
+                       if (ret < 0)
+                               return ret;
 
                        memcpy(md[0].val, md[1].val, md[1].len);
                        md[0].len = md[1].len;
@@ -1676,8 +1696,7 @@ fips_mct_sha_test(void)
 
        rte_free(vec.pt.val);
 
-       if (val.val)
-               free(val.val);
+       free(val.val);
 
        return 0;
 }
@@ -1817,10 +1836,11 @@ error_one_case:
 
        fips_test_clear();
 
-       if (env.digest)
+       if (env.digest) {
                rte_free(env.digest);
-       if (env.mbuf)
-               rte_pktmbuf_free(env.mbuf);
+               env.digest = NULL;
+       }
+       rte_pktmbuf_free(env.mbuf);
 
        return ret;
 }