cryptodev: add feature flags to disable
[dpdk.git] / examples / l2fwd-crypto / main.c
index a98190a..b2f2011 100644 (file)
@@ -19,6 +19,7 @@
 #include <fcntl.h>
 #include <unistd.h>
 
+#include <rte_string_fns.h>
 #include <rte_atomic.h>
 #include <rte_branch_prediction.h>
 #include <rte_common.h>
@@ -78,7 +79,7 @@ static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
 
 /* ethernet addresses of ports */
-static struct ether_addr l2fwd_ports_eth_addr[RTE_MAX_ETHPORTS];
+static struct rte_ether_addr l2fwd_ports_eth_addr[RTE_MAX_ETHPORTS];
 
 /* mask of enabled ports */
 static uint64_t l2fwd_enabled_port_mask;
@@ -211,9 +212,8 @@ struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
 static struct rte_eth_conf port_conf = {
        .rxmode = {
                .mq_mode = ETH_MQ_RX_NONE,
-               .max_rx_pkt_len = ETHER_MAX_LEN,
+               .max_rx_pkt_len = RTE_ETHER_MAX_LEN,
                .split_hdr_size = 0,
-               .offloads = DEV_RX_OFFLOAD_CRC_STRIP,
        },
        .txmode = {
                .mq_mode = ETH_MQ_TX_NONE,
@@ -222,7 +222,10 @@ static struct rte_eth_conf port_conf = {
 
 struct rte_mempool *l2fwd_pktmbuf_pool;
 struct rte_mempool *l2fwd_crypto_op_pool;
-struct rte_mempool *session_pool_socket[RTE_MAX_NUMA_NODES] = { 0 };
+static struct {
+       struct rte_mempool *sess_mp;
+       struct rte_mempool *priv_mp;
+} session_pool_socket[RTE_MAX_NUMA_NODES];
 
 /* Per-port statistics struct */
 struct l2fwd_port_statistics {
@@ -384,25 +387,25 @@ l2fwd_simple_crypto_enqueue(struct rte_mbuf *m,
                struct rte_crypto_op *op,
                struct l2fwd_crypto_params *cparams)
 {
-       struct ether_hdr *eth_hdr;
-       struct ipv4_hdr *ip_hdr;
+       struct rte_ether_hdr *eth_hdr;
+       struct rte_ipv4_hdr *ip_hdr;
 
        uint32_t ipdata_offset, data_len;
        uint32_t pad_len = 0;
        char *padding;
 
-       eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
+       eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
 
-       if (eth_hdr->ether_type != rte_cpu_to_be_16(ETHER_TYPE_IPv4))
+       if (eth_hdr->ether_type != rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4))
                return -1;
 
-       ipdata_offset = sizeof(struct ether_hdr);
+       ipdata_offset = sizeof(struct rte_ether_hdr);
 
-       ip_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m, char *) +
+       ip_hdr = (struct rte_ipv4_hdr *)(rte_pktmbuf_mtod(m, char *) +
                        ipdata_offset);
 
-       ipdata_offset += (ip_hdr->version_ihl & IPV4_HDR_IHL_MASK)
-                       * IPV4_IHL_MULTIPLIER;
+       ipdata_offset += (ip_hdr->version_ihl & RTE_IPV4_HDR_IHL_MASK)
+                       * RTE_IPV4_IHL_MULTIPLIER;
 
 
        /* Zero pad data to be crypto'd so it is block aligned */
@@ -590,17 +593,17 @@ l2fwd_send_packet(struct rte_mbuf *m, uint16_t port)
 static void
 l2fwd_mac_updating(struct rte_mbuf *m, uint16_t dest_portid)
 {
-       struct ether_hdr *eth;
+       struct rte_ether_hdr *eth;
        void *tmp;
 
-       eth = rte_pktmbuf_mtod(m, struct ether_hdr *);
+       eth = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
 
        /* 02:00:00:00:00:xx */
        tmp = &eth->d_addr.addr_bytes[0];
        *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dest_portid << 40);
 
        /* src addr */
-       ether_addr_copy(&l2fwd_ports_eth_addr[dest_portid], &eth->s_addr);
+       rte_ether_addr_copy(&l2fwd_ports_eth_addr[dest_portid], &eth->s_addr);
 }
 
 static void
@@ -646,7 +649,6 @@ initialize_crypto_session(struct l2fwd_crypto_options *options, uint8_t cdev_id)
                return NULL;
 
        uint8_t socket_id = (uint8_t) retval;
-       struct rte_mempool *sess_mp = session_pool_socket[socket_id];
 
        if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
                first_xform = &options->aead_xform;
@@ -662,13 +664,14 @@ initialize_crypto_session(struct l2fwd_crypto_options *options, uint8_t cdev_id)
                first_xform = &options->auth_xform;
        }
 
-       session = rte_cryptodev_sym_session_create(sess_mp);
-
+       session = rte_cryptodev_sym_session_create(
+                       session_pool_socket[socket_id].sess_mp);
        if (session == NULL)
                return NULL;
 
        if (rte_cryptodev_sym_session_init(cdev_id, session,
-                               first_xform, sess_mp) < 0)
+                               first_xform,
+                               session_pool_socket[socket_id].priv_mp) < 0)
                return NULL;
 
        return session;
@@ -1197,8 +1200,7 @@ 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)
-                       snprintf(options->string_type, MAX_STR_LEN,
-                               "%s", optarg);
+                       strlcpy(options->string_type, optarg, MAX_STR_LEN);
                return retval;
        }
 
@@ -2254,6 +2256,7 @@ initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports,
                struct rte_cryptodev_config conf = {
                        .nb_queue_pairs = 1,
                        .socket_id = socket_id,
+                       .ff_disable = RTE_CRYPTODEV_FF_SECURITY,
                };
 
                rte_cryptodev_info_get(cdev_id, &dev_info);
@@ -2268,58 +2271,63 @@ initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports,
                                rte_cryptodev_scheduler_slaves_get(cdev_id,
                                                                NULL);
 
-                       sessions_needed = 2 * enabled_cdev_count * nb_slaves;
+                       sessions_needed = enabled_cdev_count * nb_slaves;
 #endif
                } else
-                       sessions_needed = 2 * enabled_cdev_count;
+                       sessions_needed = enabled_cdev_count;
 
-               if (session_pool_socket[socket_id] == NULL) {
+               if (session_pool_socket[socket_id].priv_mp == NULL) {
                        char mp_name[RTE_MEMPOOL_NAMESIZE];
-                       struct rte_mempool *sess_mp;
 
                        snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
-                               "sess_mp_%u", socket_id);
+                               "priv_sess_mp_%u", socket_id);
 
-                       /*
-                        * Create enough objects for session headers and
-                        * device private data
-                        */
-                       sess_mp = rte_mempool_create(mp_name,
+                       session_pool_socket[socket_id].priv_mp =
+                                       rte_mempool_create(mp_name,
                                                sessions_needed,
                                                max_sess_sz,
-                                               SESSION_POOL_CACHE_SIZE,
-                                               0, NULL, NULL, NULL,
+                                               0, 0, NULL, NULL, NULL,
                                                NULL, socket_id,
                                                0);
 
-                       if (sess_mp == NULL) {
-                               printf("Cannot create session pool on socket %d\n",
+                       if (session_pool_socket[socket_id].priv_mp == NULL) {
+                               printf("Cannot create pool on socket %d\n",
+                                       socket_id);
+                               return -ENOMEM;
+                       }
+
+                       printf("Allocated pool \"%s\" on socket %d\n",
+                               mp_name, socket_id);
+               }
+
+               if (session_pool_socket[socket_id].sess_mp == NULL) {
+                       char mp_name[RTE_MEMPOOL_NAMESIZE];
+                       snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
+                               "sess_mp_%u", socket_id);
+
+                       session_pool_socket[socket_id].sess_mp =
+                                       rte_cryptodev_sym_session_pool_create(
+                                                       mp_name,
+                                                       sessions_needed,
+                                                       0, 0, 0, socket_id);
+
+                       if (session_pool_socket[socket_id].sess_mp == NULL) {
+                               printf("Cannot create pool on socket %d\n",
                                        socket_id);
                                return -ENOMEM;
                        }
 
-                       printf("Allocated session pool on socket %d\n", socket_id);
-                       session_pool_socket[socket_id] = sess_mp;
+                       printf("Allocated pool \"%s\" on socket %d\n",
+                               mp_name, socket_id);
                }
 
                /* Set AEAD parameters */
                if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
-                       /* Check if device supports AEAD algo */
                        cap = check_device_support_aead_algo(options, &dev_info,
                                                        cdev_id);
-                       if (cap == NULL)
-                               continue;
 
                        options->block_size = cap->sym.aead.block_size;
 
-                       if (check_iv_param(&cap->sym.aead.iv_size,
-                                       options->aead_iv_param,
-                                       options->aead_iv_random_size,
-                                       options->aead_iv.length) < 0) {
-                               printf("Unsupported IV length\n");
-                               continue;
-                       }
-
                        /* Set IV if not provided from command line */
                        if (options->aead_iv_param == 0) {
                                if (options->aead_iv_random_size != -1)
@@ -2331,93 +2339,41 @@ initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports,
                                                cap->sym.aead.iv_size.min;
                        }
 
-                       /*
-                        * Check if length of provided AEAD key is supported
-                        * by the algorithm chosen.
-                        */
-                       if (options->aead_key_param) {
-                               if (check_supported_size(
-                                               options->aead_xform.aead.key.length,
-                                               cap->sym.aead.key_size.min,
-                                               cap->sym.aead.key_size.max,
-                                               cap->sym.aead.key_size.increment)
-                                                       != 0) {
-                                       printf("Unsupported aead key length\n");
-                                       continue;
-                               }
-                       /*
-                        * Check if length of the aead key to be randomly generated
-                        * is supported by the algorithm chosen.
-                        */
-                       } else if (options->aead_key_random_size != -1) {
-                               if (check_supported_size(options->aead_key_random_size,
-                                               cap->sym.aead.key_size.min,
-                                               cap->sym.aead.key_size.max,
-                                               cap->sym.aead.key_size.increment)
-                                                       != 0) {
-                                       printf("Unsupported aead key length\n");
-                                       continue;
-                               }
-                               options->aead_xform.aead.key.length =
-                                                       options->aead_key_random_size;
-                       /* No size provided, use minimum size. */
-                       } else
-                               options->aead_xform.aead.key.length =
+                       /* Set key if not provided from command line */
+                       if (options->aead_key_param == 0) {
+                               if (options->aead_key_random_size != -1)
+                                       options->aead_xform.aead.key.length =
+                                               options->aead_key_random_size;
+                               /* No size provided, use minimum size. */
+                               else
+                                       options->aead_xform.aead.key.length =
                                                cap->sym.aead.key_size.min;
 
-                       if (!options->aead_key_param)
                                generate_random_key(
                                        options->aead_xform.aead.key.data,
                                        options->aead_xform.aead.key.length);
+                       }
 
-                       /*
-                        * Check if length of provided AAD is supported
-                        * by the algorithm chosen.
-                        */
-                       if (options->aad_param) {
-                               if (check_supported_size(options->aad.length,
-                                               cap->sym.aead.aad_size.min,
-                                               cap->sym.aead.aad_size.max,
-                                               cap->sym.aead.aad_size.increment)
-                                                       != 0) {
-                                       printf("Unsupported AAD length\n");
-                                       continue;
-                               }
-                       /*
-                        * Check if length of AAD to be randomly generated
-                        * is supported by the algorithm chosen.
-                        */
-                       } else if (options->aad_random_size != -1) {
-                               if (check_supported_size(options->aad_random_size,
-                                               cap->sym.aead.aad_size.min,
-                                               cap->sym.aead.aad_size.max,
-                                               cap->sym.aead.aad_size.increment)
-                                                       != 0) {
-                                       printf("Unsupported AAD length\n");
-                                       continue;
-                               }
-                               options->aad.length = options->aad_random_size;
-                       /* No size provided, use minimum size. */
-                       } else
-                               options->aad.length = cap->sym.auth.aad_size.min;
+                       /* Set AAD if not provided from command line */
+                       if (options->aad_param == 0) {
+                               if (options->aad_random_size != -1)
+                                       options->aad.length =
+                                               options->aad_random_size;
+                               /* No size provided, use minimum size. */
+                               else
+                                       options->aad.length =
+                                               cap->sym.auth.aad_size.min;
+                       }
 
                        options->aead_xform.aead.aad_length =
                                                options->aad.length;
 
-                       /* Check if digest size is supported by the algorithm. */
-                       if (options->digest_size != -1) {
-                               if (check_supported_size(options->digest_size,
-                                               cap->sym.aead.digest_size.min,
-                                               cap->sym.aead.digest_size.max,
-                                               cap->sym.aead.digest_size.increment)
-                                                       != 0) {
-                                       printf("Unsupported digest length\n");
-                                       continue;
-                               }
+                       /* Set digest size if not provided from command line */
+                       if (options->digest_size != -1)
                                options->aead_xform.aead.digest_length =
                                                        options->digest_size;
-                       /* No size provided, use minimum size. */
-                       else
+                               /* No size provided, use minimum size. */
+                       else
                                options->aead_xform.aead.digest_length =
                                                cap->sym.aead.digest_size.min;
                }
@@ -2426,22 +2382,10 @@ initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports,
                if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
                                options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
                                options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
-                       /* Check if device supports cipher algo */
                        cap = check_device_support_cipher_algo(options, &dev_info,
                                                        cdev_id);
-                       if (cap == NULL)
-                               continue;
-
                        options->block_size = cap->sym.cipher.block_size;
 
-                       if (check_iv_param(&cap->sym.cipher.iv_size,
-                                       options->cipher_iv_param,
-                                       options->cipher_iv_random_size,
-                                       options->cipher_iv.length) < 0) {
-                               printf("Unsupported IV length\n");
-                               continue;
-                       }
-
                        /* Set IV if not provided from command line */
                        if (options->cipher_iv_param == 0) {
                                if (options->cipher_iv_random_size != -1)
@@ -2453,64 +2397,28 @@ initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports,
                                                cap->sym.cipher.iv_size.min;
                        }
 
-                       /*
-                        * Check if length of provided cipher key is supported
-                        * by the algorithm chosen.
-                        */
-                       if (options->ckey_param) {
-                               if (check_supported_size(
-                                               options->cipher_xform.cipher.key.length,
-                                               cap->sym.cipher.key_size.min,
-                                               cap->sym.cipher.key_size.max,
-                                               cap->sym.cipher.key_size.increment)
-                                                       != 0) {
-                                       printf("Unsupported cipher key length\n");
-                                       continue;
-                               }
-                       /*
-                        * Check if length of the cipher key to be randomly generated
-                        * is supported by the algorithm chosen.
-                        */
-                       } else if (options->ckey_random_size != -1) {
-                               if (check_supported_size(options->ckey_random_size,
-                                               cap->sym.cipher.key_size.min,
-                                               cap->sym.cipher.key_size.max,
-                                               cap->sym.cipher.key_size.increment)
-                                                       != 0) {
-                                       printf("Unsupported cipher key length\n");
-                                       continue;
-                               }
-                               options->cipher_xform.cipher.key.length =
-                                                       options->ckey_random_size;
-                       /* No size provided, use minimum size. */
-                       } else
-                               options->cipher_xform.cipher.key.length =
+                       /* Set key if not provided from command line */
+                       if (options->ckey_param == 0) {
+                               if (options->ckey_random_size != -1)
+                                       options->cipher_xform.cipher.key.length =
+                                               options->ckey_random_size;
+                               /* No size provided, use minimum size. */
+                               else
+                                       options->cipher_xform.cipher.key.length =
                                                cap->sym.cipher.key_size.min;
 
-                       if (!options->ckey_param)
                                generate_random_key(
                                        options->cipher_xform.cipher.key.data,
                                        options->cipher_xform.cipher.key.length);
-
+                       }
                }
 
                /* Set auth parameters */
                if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
                                options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
                                options->xform_chain == L2FWD_CRYPTO_HASH_ONLY) {
-                       /* Check if device supports auth algo */
                        cap = check_device_support_auth_algo(options, &dev_info,
                                                        cdev_id);
-                       if (cap == NULL)
-                               continue;
-
-                       if (check_iv_param(&cap->sym.auth.iv_size,
-                                       options->auth_iv_param,
-                                       options->auth_iv_random_size,
-                                       options->auth_iv.length) < 0) {
-                               printf("Unsupported IV length\n");
-                               continue;
-                       }
 
                        /* Set IV if not provided from command line */
                        if (options->auth_iv_param == 0) {
@@ -2523,59 +2431,27 @@ initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports,
                                                cap->sym.auth.iv_size.min;
                        }
 
-                       /*
-                        * Check if length of provided auth key is supported
-                        * by the algorithm chosen.
-                        */
-                       if (options->akey_param) {
-                               if (check_supported_size(
-                                               options->auth_xform.auth.key.length,
-                                               cap->sym.auth.key_size.min,
-                                               cap->sym.auth.key_size.max,
-                                               cap->sym.auth.key_size.increment)
-                                                       != 0) {
-                                       printf("Unsupported auth key length\n");
-                                       continue;
-                               }
-                       /*
-                        * Check if length of the auth key to be randomly generated
-                        * is supported by the algorithm chosen.
-                        */
-                       } else if (options->akey_random_size != -1) {
-                               if (check_supported_size(options->akey_random_size,
-                                               cap->sym.auth.key_size.min,
-                                               cap->sym.auth.key_size.max,
-                                               cap->sym.auth.key_size.increment)
-                                                       != 0) {
-                                       printf("Unsupported auth key length\n");
-                                       continue;
-                               }
-                               options->auth_xform.auth.key.length =
-                                                       options->akey_random_size;
-                       /* No size provided, use minimum size. */
-                       } else
-                               options->auth_xform.auth.key.length =
+                       /* Set key if not provided from command line */
+                       if (options->akey_param == 0) {
+                               if (options->akey_random_size != -1)
+                                       options->auth_xform.auth.key.length =
+                                               options->akey_random_size;
+                               /* No size provided, use minimum size. */
+                               else
+                                       options->auth_xform.auth.key.length =
                                                cap->sym.auth.key_size.min;
 
-                       if (!options->akey_param)
                                generate_random_key(
                                        options->auth_xform.auth.key.data,
                                        options->auth_xform.auth.key.length);
+                       }
 
-                       /* Check if digest size is supported by the algorithm. */
-                       if (options->digest_size != -1) {
-                               if (check_supported_size(options->digest_size,
-                                               cap->sym.auth.digest_size.min,
-                                               cap->sym.auth.digest_size.max,
-                                               cap->sym.auth.digest_size.increment)
-                                                       != 0) {
-                                       printf("Unsupported digest length\n");
-                                       continue;
-                               }
+                       /* Set digest size if not provided from command line */
+                       if (options->digest_size != -1)
                                options->auth_xform.auth.digest_length =
                                                        options->digest_size;
-                       /* No size provided, use minimum size. */
-                       else
+                               /* No size provided, use minimum size. */
+                       else
                                options->auth_xform.auth.digest_length =
                                                cap->sym.auth.digest_size.min;
                }
@@ -2587,9 +2463,12 @@ initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports,
                }
 
                qp_conf.nb_descriptors = 2048;
+               qp_conf.mp_session = session_pool_socket[socket_id].sess_mp;
+               qp_conf.mp_session_private =
+                               session_pool_socket[socket_id].priv_mp;
 
                retval = rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf,
-                               socket_id, session_pool_socket[socket_id]);
+                               socket_id);
                if (retval < 0) {
                        printf("Failed to setup queue pair %u on cryptodev %u",
                                        0, cdev_id);