cryptodev: pass IV as offset
[dpdk.git] / examples / l2fwd-crypto / main.c
index 6a3965b..c810b48 100644 (file)
@@ -1,7 +1,7 @@
 /*-
  *   BSD LICENSE
  *
- *   Copyright(c) 2015-2016 Intel Corporation. All rights reserved.
+ *   Copyright(c) 2015-2017 Intel Corporation. All rights reserved.
  *   All rights reserved.
  *
  *   Redistribution and use in source and binary forms, with or without
@@ -89,6 +89,10 @@ enum cdev_type {
 #define MAX_PKT_BURST 32
 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
 
+#define MAXIMUM_IV_LENGTH      16
+#define IV_OFFSET              (sizeof(struct rte_crypto_op) + \
+                               sizeof(struct rte_crypto_sym_op))
+
 /*
  * Configurable number of RX/TX ring descriptors
  */
@@ -167,6 +171,8 @@ struct l2fwd_crypto_options {
 
        uint16_t block_size;
        char string_type[MAX_STR_LEN];
+
+       uint64_t cryptodev_mask;
 };
 
 /** l2fwd crypto lcore params */
@@ -212,7 +218,7 @@ static const struct rte_eth_conf port_conf = {
                .hw_ip_checksum = 0, /**< IP checksum offload disabled */
                .hw_vlan_filter = 0, /**< VLAN filtering disabled */
                .jumbo_frame    = 0, /**< Jumbo Frame Support disabled */
-               .hw_strip_crc   = 0, /**< CRC stripped by hardware */
+               .hw_strip_crc   = 1, /**< CRC stripped by hardware */
        },
        .txmode = {
                .mq_mode = ETH_MQ_TX_NONE,
@@ -470,12 +476,20 @@ l2fwd_simple_crypto_enqueue(struct rte_mbuf *m,
                        op->sym->auth.aad.data = cparams->aad.data;
                        op->sym->auth.aad.phys_addr = cparams->aad.phys_addr;
                        op->sym->auth.aad.length = cparams->aad.length;
+               } else {
+                       op->sym->auth.aad.data = NULL;
+                       op->sym->auth.aad.phys_addr = 0;
+                       op->sym->auth.aad.length = 0;
                }
        }
 
        if (cparams->do_cipher) {
-               op->sym->cipher.iv.data = cparams->iv.data;
-               op->sym->cipher.iv.phys_addr = cparams->iv.phys_addr;
+               uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
+                                                       IV_OFFSET);
+               /* Copy IV at the end of the crypto operation */
+               rte_memcpy(iv_ptr, cparams->iv.data, cparams->iv.length);
+
+               op->sym->cipher.iv.offset = IV_OFFSET;
                op->sym->cipher.iv.length = cparams->iv.length;
 
                /* For wireless algorithms, offset/length must be in bits */
@@ -670,7 +684,8 @@ l2fwd_main_loop(struct l2fwd_crypto_options *options)
                                        generate_random_key(port_cparams[i].aad.data,
                                                port_cparams[i].aad.length);
 
-                       }
+                       } else
+                               port_cparams[i].aad.length = 0;
 
                        if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_VERIFY)
                                port_cparams[i].hash_verify = 1;
@@ -683,7 +698,6 @@ l2fwd_main_loop(struct l2fwd_crypto_options *options)
                if (port_cparams[i].do_cipher) {
                        port_cparams[i].iv.data = options->iv.data;
                        port_cparams[i].iv.length = options->iv.length;
-                       port_cparams[i].iv.phys_addr = options->iv.phys_addr;
                        if (!options->iv_param)
                                generate_random_key(port_cparams[i].iv.data,
                                                port_cparams[i].iv.length);
@@ -781,7 +795,7 @@ l2fwd_main_loop(struct l2fwd_crypto_options *options)
                                                ops_burst, nb_rx) !=
                                                                nb_rx) {
                                        for (j = 0; j < nb_rx; j++)
-                                               rte_pktmbuf_free(pkts_burst[i]);
+                                               rte_pktmbuf_free(pkts_burst[j]);
 
                                        nb_rx = 0;
                                }
@@ -835,7 +849,8 @@ l2fwd_crypto_usage(const char *prgname)
                " (0 to disable, 10 default, 86400 maximum)\n"
 
                "  --cdev_type HW / SW / ANY\n"
-               "  --chain HASH_CIPHER / CIPHER_HASH\n"
+               "  --chain HASH_CIPHER / CIPHER_HASH / CIPHER_ONLY /"
+               " HASH_ONLY\n"
 
                "  --cipher_algo ALGO\n"
                "  --cipher_op ENCRYPT / DECRYPT\n"
@@ -852,7 +867,8 @@ l2fwd_crypto_usage(const char *prgname)
                "  --aad_random_size SIZE: size of AAD when generated randomly\n"
                "  --digest_size SIZE: size of digest to be generated/verified\n"
 
-               "  --sessionless\n",
+               "  --sessionless\n"
+               "  --cryptodev_mask MASK: hexadecimal bitmask of crypto devices to configure\n",
               prgname);
 }
 
@@ -996,6 +1012,27 @@ parse_auth_op(enum rte_crypto_auth_operation *op, char *optarg)
        return -1;
 }
 
+static int
+parse_cryptodev_mask(struct l2fwd_crypto_options *options,
+               const char *q_arg)
+{
+       char *end = NULL;
+       uint64_t pm;
+
+       /* parse hexadecimal string */
+       pm = strtoul(q_arg, &end, 16);
+       if ((pm == '\0') || (end == NULL) || (*end != '\0'))
+               pm = 0;
+
+       options->cryptodev_mask = pm;
+       if (options->cryptodev_mask == 0) {
+               printf("invalid cryptodev_mask specified\n");
+               return -1;
+       }
+
+       return 0;
+}
+
 /** Parse long options */
 static int
 l2fwd_crypto_parse_args_long_options(struct l2fwd_crypto_options *options,
@@ -1096,6 +1133,9 @@ l2fwd_crypto_parse_args_long_options(struct l2fwd_crypto_options *options,
                return 0;
        }
 
+       else if (strcmp(lgopts[option_index].name, "cryptodev_mask") == 0)
+               return parse_cryptodev_mask(options, optarg);
+
        return -1;
 }
 
@@ -1210,6 +1250,7 @@ l2fwd_crypto_default_options(struct l2fwd_crypto_options *options)
        options->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
 
        options->type = CDEV_TYPE_ANY;
+       options->cryptodev_mask = UINT64_MAX;
 }
 
 static void
@@ -1229,7 +1270,7 @@ display_auth_info(struct l2fwd_crypto_options *options)
 {
        printf("\n---- Authentication information ---\n");
        printf("Algorithm: %s\n",
-               rte_crypto_auth_algorithm_strings[options->auth_xform.cipher.algo]);
+               rte_crypto_auth_algorithm_strings[options->auth_xform.auth.algo]);
        rte_hexdump(stdout, "Auth key:",
                        options->auth_xform.auth.key.data,
                        options->auth_xform.auth.key.length);
@@ -1332,13 +1373,14 @@ l2fwd_crypto_parse_args(struct l2fwd_crypto_options *options,
                        { "digest_size", required_argument, 0, 0 },
 
                        { "sessionless", no_argument, 0, 0 },
+                       { "cryptodev_mask", required_argument, 0, 0},
 
                        { NULL, 0, 0, 0 }
        };
 
        l2fwd_crypto_default_options(options);
 
-       while ((opt = getopt_long(argc, argvopt, "p:q:st:", lgopts,
+       while ((opt = getopt_long(argc, argvopt, "p:q:sT:", lgopts,
                        &option_index)) != EOF) {
                switch (opt) {
                /* long options */
@@ -1472,6 +1514,17 @@ check_type(struct l2fwd_crypto_options *options, struct rte_cryptodev_info *dev_
        return -1;
 }
 
+/* Check if the device is enabled by cryptodev_mask */
+static int
+check_cryptodev_mask(struct l2fwd_crypto_options *options,
+               uint8_t cdev_id)
+{
+       if (options->cryptodev_mask & (1 << cdev_id))
+               return 0;
+
+       return -1;
+}
+
 static inline int
 check_supported_size(uint16_t length, uint16_t min, uint16_t max,
                uint16_t increment)
@@ -1526,6 +1579,9 @@ initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports,
                        }
                };
 
+               if (check_cryptodev_mask(options, (uint8_t)cdev_id))
+                       continue;
+
                rte_cryptodev_info_get(cdev_id, &dev_info);
 
                /* Set cipher parameters */
@@ -1657,7 +1713,6 @@ initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports,
                                continue;
                        }
 
-                       options->block_size = cap->sym.auth.block_size;
                        /*
                         * Check if length of provided AAD is supported
                         * by the algorithm chosen.
@@ -1902,7 +1957,6 @@ reserve_key_memory(struct l2fwd_crypto_options *options)
        options->iv.data = rte_malloc("iv", MAX_KEY_SIZE, 0);
        if (options->iv.data == NULL)
                rte_exit(EXIT_FAILURE, "Failed to allocate memory for IV");
-       options->iv.phys_addr = rte_malloc_virt2phy(options->iv.data);
 
        options->aad.data = rte_malloc("aad", MAX_KEY_SIZE, 0);
        if (options->aad.data == NULL)
@@ -1945,7 +1999,7 @@ main(int argc, char **argv)
 
        /* create crypto op pool */
        l2fwd_crypto_op_pool = rte_crypto_op_pool_create("crypto_op_pool",
-                       RTE_CRYPTO_OP_TYPE_SYMMETRIC, NB_MBUF, 128, 0,
+                       RTE_CRYPTO_OP_TYPE_SYMMETRIC, NB_MBUF, 128, MAXIMUM_IV_LENGTH,
                        rte_socket_id());
        if (l2fwd_crypto_op_pool == NULL)
                rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n");