app/testpmd: fix memory leaks after xstats errors
[dpdk.git] / examples / l2fwd-crypto / main.c
index 5fb33df..8dc616d 100644 (file)
@@ -71,6 +71,7 @@
 #include <rte_prefetch.h>
 #include <rte_random.h>
 #include <rte_ring.h>
+#include <rte_hexdump.h>
 
 enum cdev_type {
        CDEV_TYPE_ANY,
@@ -340,20 +341,25 @@ fill_supported_algorithm_tables(void)
        strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_AES_GCM], "AES_GCM");
        strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_MD5_HMAC], "MD5_HMAC");
        strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_NULL], "NULL");
+       strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_AES_XCBC_MAC],
+               "AES_XCBC_MAC");
        strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_SHA1_HMAC], "SHA1_HMAC");
        strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_SHA224_HMAC], "SHA224_HMAC");
        strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_SHA256_HMAC], "SHA256_HMAC");
        strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_SHA384_HMAC], "SHA384_HMAC");
        strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_SHA512_HMAC], "SHA512_HMAC");
        strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_SNOW3G_UIA2], "SNOW3G_UIA2");
+       strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_KASUMI_F9], "KASUMI_F9");
 
        for (i = 0; i < RTE_CRYPTO_CIPHER_LIST_END; i++)
                strcpy(supported_cipher_algo[i], "NOT_SUPPORTED");
 
        strcpy(supported_cipher_algo[RTE_CRYPTO_CIPHER_AES_CBC], "AES_CBC");
+       strcpy(supported_cipher_algo[RTE_CRYPTO_CIPHER_AES_CTR], "AES_CTR");
        strcpy(supported_cipher_algo[RTE_CRYPTO_CIPHER_AES_GCM], "AES_GCM");
        strcpy(supported_cipher_algo[RTE_CRYPTO_CIPHER_NULL], "NULL");
        strcpy(supported_cipher_algo[RTE_CRYPTO_CIPHER_SNOW3G_UEA2], "SNOW3G_UEA2");
+       strcpy(supported_cipher_algo[RTE_CRYPTO_CIPHER_KASUMI_F8], "KASUMI_F8");
 }
 
 
@@ -462,8 +468,9 @@ l2fwd_simple_crypto_enqueue(struct rte_mbuf *m,
                                rte_pktmbuf_pkt_len(m) - cparams->digest_length);
                op->sym->auth.digest.length = cparams->digest_length;
 
-               /* For SNOW3G algorithms, offset/length must be in bits */
-               if (cparams->auth_algo == RTE_CRYPTO_AUTH_SNOW3G_UIA2) {
+               /* For SNOW3G/KASUMI algorithms, offset/length must be in bits */
+               if (cparams->auth_algo == RTE_CRYPTO_AUTH_SNOW3G_UIA2 ||
+                               cparams->auth_algo == RTE_CRYPTO_AUTH_KASUMI_F9) {
                        op->sym->auth.data.offset = ipdata_offset << 3;
                        op->sym->auth.data.length = data_len << 3;
                } else {
@@ -484,7 +491,8 @@ l2fwd_simple_crypto_enqueue(struct rte_mbuf *m,
                op->sym->cipher.iv.length = cparams->iv.length;
 
                /* For SNOW3G algorithms, offset/length must be in bits */
-               if (cparams->cipher_algo == RTE_CRYPTO_CIPHER_SNOW3G_UEA2) {
+               if (cparams->cipher_algo == RTE_CRYPTO_CIPHER_SNOW3G_UEA2 ||
+                               cparams->cipher_algo == RTE_CRYPTO_CIPHER_KASUMI_F8) {
                        op->sym->cipher.data.offset = ipdata_offset << 3;
                        if (cparams->do_hash && cparams->hash_verify)
                                /* Do not cipher the hash tag */
@@ -634,8 +642,6 @@ l2fwd_main_loop(struct l2fwd_crypto_options *options)
 
        RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
 
-       l2fwd_crypto_options_print(options);
-
        for (i = 0; i < qconf->nb_rx_ports; i++) {
 
                portid = qconf->rx_port_list[i];
@@ -708,6 +714,14 @@ l2fwd_main_loop(struct l2fwd_crypto_options *options)
                                port_cparams[i].dev_id);
        }
 
+       l2fwd_crypto_options_print(options);
+
+       /*
+        * Initialize previous tsc timestamp before the loop,
+        * to avoid showing the port statistics immediately,
+        * so user can see the crypto information.
+        */
+       prev_tsc = rte_rdtsc();
        while (1) {
 
                cur_tsc = rte_rdtsc();
@@ -895,7 +909,7 @@ parse_cipher_algo(enum rte_crypto_cipher_algorithm *algo, char *optarg)
 
        for (i = 0; i < RTE_CRYPTO_CIPHER_LIST_END; i++) {
                if (!strcmp(supported_cipher_algo[i], optarg)) {
-                       *algo = i;
+                       *algo = (enum rte_crypto_cipher_algorithm)i;
                        return 0;
                }
        }
@@ -971,7 +985,7 @@ parse_auth_algo(enum rte_crypto_auth_algorithm *algo, char *optarg)
 
        for (i = 0; i < RTE_CRYPTO_AUTH_LIST_END; i++) {
                if (!strcmp(supported_auth_algo[i], optarg)) {
-                       *algo = i;
+                       *algo = (enum rte_crypto_auth_algorithm)i;
                        return 0;
                }
        }
@@ -1005,7 +1019,8 @@ l2fwd_crypto_parse_args_long_options(struct l2fwd_crypto_options *options,
        if (strcmp(lgopts[option_index].name, "cdev_type") == 0) {
                retval = parse_cryptodev_type(&options->type, optarg);
                if (retval == 0)
-                       strcpy(options->string_type, optarg);
+                       snprintf(options->string_type, MAX_STR_LEN,
+                               "%s", optarg);
                return retval;
        }
 
@@ -1212,9 +1227,46 @@ l2fwd_crypto_default_options(struct l2fwd_crypto_options *options)
        options->type = CDEV_TYPE_ANY;
 }
 
+static void
+display_cipher_info(struct l2fwd_crypto_options *options)
+{
+       printf("\n---- Cipher information ---\n");
+       printf("Algorithm: %s\n",
+               supported_cipher_algo[options->cipher_xform.cipher.algo]);
+       rte_hexdump(stdout, "Cipher key:",
+                       options->cipher_xform.cipher.key.data,
+                       options->cipher_xform.cipher.key.length);
+       rte_hexdump(stdout, "IV:", options->iv.data, options->iv.length);
+}
+
+static void
+display_auth_info(struct l2fwd_crypto_options *options)
+{
+       printf("\n---- Authentication information ---\n");
+       printf("Algorithm: %s\n",
+               supported_auth_algo[options->auth_xform.auth.algo]);
+       rte_hexdump(stdout, "Auth key:",
+                       options->auth_xform.auth.key.data,
+                       options->auth_xform.auth.key.length);
+       rte_hexdump(stdout, "AAD:", options->aad.data, options->aad.length);
+}
+
 static void
 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options)
 {
+       char string_cipher_op[MAX_STR_LEN];
+       char string_auth_op[MAX_STR_LEN];
+
+       if (options->cipher_xform.cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
+               strcpy(string_cipher_op, "Encrypt");
+       else
+               strcpy(string_cipher_op, "Decrypt");
+
+       if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_GENERATE)
+               strcpy(string_auth_op, "Auth generate");
+       else
+               strcpy(string_auth_op, "Auth verify");
+
        printf("Options:-\nn");
        printf("portmask: %x\n", options->portmask);
        printf("ports per lcore: %u\n", options->nb_ports_per_lcore);
@@ -1226,6 +1278,42 @@ l2fwd_crypto_options_print(struct l2fwd_crypto_options *options)
 
        printf("sessionless crypto: %s\n",
                        options->sessionless ? "enabled" : "disabled");
+
+       if (options->ckey_param && (options->ckey_random_size != -1))
+               printf("Cipher key already parsed, ignoring size of random key\n");
+
+       if (options->akey_param && (options->akey_random_size != -1))
+               printf("Auth key already parsed, ignoring size of random key\n");
+
+       if (options->iv_param && (options->iv_random_size != -1))
+               printf("IV already parsed, ignoring size of random IV\n");
+
+       if (options->aad_param && (options->aad_random_size != -1))
+               printf("AAD already parsed, ignoring size of random AAD\n");
+
+       printf("\nCrypto chain: ");
+       switch (options->xform_chain) {
+       case L2FWD_CRYPTO_CIPHER_HASH:
+               printf("Input --> %s --> %s --> Output\n",
+                       string_cipher_op, string_auth_op);
+               display_cipher_info(options);
+               display_auth_info(options);
+               break;
+       case L2FWD_CRYPTO_HASH_CIPHER:
+               printf("Input --> %s --> %s --> Output\n",
+                       string_auth_op, string_cipher_op);
+               display_cipher_info(options);
+               display_auth_info(options);
+               break;
+       case L2FWD_CRYPTO_HASH_ONLY:
+               printf("Input --> %s --> Output\n", string_auth_op);
+               display_auth_info(options);
+               break;
+       case L2FWD_CRYPTO_CIPHER_ONLY:
+               printf("Input --> %s --> Output\n", string_cipher_op);
+               display_cipher_info(options);
+               break;
+       }
 }
 
 /* Parse the argument given in the command line of the application */
@@ -1360,7 +1448,7 @@ check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
                                continue;
                        }
                        /* clear all_ports_up flag if any link down */
-                       if (link.link_status == 0) {
+                       if (link.link_status == ETH_LINK_DOWN) {
                                all_ports_up = 0;
                                break;
                        }
@@ -1405,6 +1493,15 @@ check_supported_size(uint16_t length, uint16_t min, uint16_t max,
 {
        uint16_t supp_size;
 
+       /* Single value */
+       if (increment == 0) {
+               if (length == min)
+                       return 0;
+               else
+                       return -1;
+       }
+
+       /* Range of values */
        for (supp_size = min; supp_size <= max; supp_size += increment) {
                if (length == supp_size)
                        return 0;
@@ -1704,9 +1801,6 @@ initialize_ports(struct l2fwd_crypto_options *options)
                return -1;
        }
 
-       if (nb_ports > RTE_MAX_ETHPORTS)
-               nb_ports = RTE_MAX_ETHPORTS;
-
        /* Reset l2fwd_dst_ports */
        for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
                l2fwd_dst_ports[portid] = 0;