eal/x86: remove atomic header include loop
[dpdk.git] / app / test / test_cryptodev.c
index ec4a61b..a0c8926 100644 (file)
@@ -3,6 +3,8 @@
  * Copyright 2020 NXP
  */
 
+#ifndef RTE_EXEC_ENV_WINDOWS
+
 #include <time.h>
 
 #include <rte_common.h>
@@ -209,6 +211,7 @@ process_sym_raw_dp_op(uint8_t dev_id, uint16_t qp_id,
        int enqueue_status, dequeue_status;
        struct crypto_unittest_params *ut_params = &unittest_params;
        int is_sgl = sop->m_src->nb_segs > 1;
+       int is_oop = 0;
 
        ctx_service_size = rte_cryptodev_get_raw_dp_ctx_size(dev_id);
        if (ctx_service_size < 0) {
@@ -247,6 +250,9 @@ process_sym_raw_dp_op(uint8_t dev_id, uint16_t qp_id,
 
        ofs.raw = 0;
 
+       if ((sop->m_dst != NULL) && (sop->m_dst != sop->m_src))
+               is_oop = 1;
+
        if (is_cipher && is_auth) {
                cipher_offset = sop->cipher.data.offset;
                cipher_len = sop->cipher.data.length;
@@ -277,6 +283,8 @@ process_sym_raw_dp_op(uint8_t dev_id, uint16_t qp_id,
                if (is_sgl) {
                        uint32_t remaining_off = auth_offset + auth_len;
                        struct rte_mbuf *sgl_buf = sop->m_src;
+                       if (is_oop)
+                               sgl_buf = sop->m_dst;
 
                        while (remaining_off >= rte_pktmbuf_data_len(sgl_buf)
                                        && sgl_buf->next != NULL) {
@@ -293,7 +301,8 @@ process_sym_raw_dp_op(uint8_t dev_id, uint16_t qp_id,
                /* Then check if digest-encrypted conditions are met */
                if ((auth_offset + auth_len < cipher_offset + cipher_len) &&
                                (digest.iova == auth_end_iova) && is_sgl)
-                       max_len = RTE_MAX(max_len, auth_offset + auth_len +
+                       max_len = RTE_MAX(max_len,
+                               auth_offset + auth_len +
                                ut_params->auth_xform.auth.digest_length);
 
        } else if (is_cipher) {
@@ -356,7 +365,7 @@ process_sym_raw_dp_op(uint8_t dev_id, uint16_t qp_id,
 
        sgl.num = n;
        /* Out of place */
-       if (sop->m_dst != NULL) {
+       if (is_oop) {
                dest_sgl.vec = dest_data_vec;
                vec.dest_sgl = &dest_sgl;
                n = rte_crypto_mbuf_to_vec(sop->m_dst, 0, max_len,
@@ -9176,7 +9185,21 @@ test_ipsec_proto_process(const struct ipsec_test_data td[],
                        if (flags->df == TEST_IPSEC_SET_DF_1_INNER_0)
                                ipsec_xform.tunnel.ipv4.df = 1;
 
+                       if (flags->dscp == TEST_IPSEC_SET_DSCP_0_INNER_1)
+                               ipsec_xform.tunnel.ipv4.dscp = 0;
+
+                       if (flags->dscp == TEST_IPSEC_SET_DSCP_1_INNER_0)
+                               ipsec_xform.tunnel.ipv4.dscp =
+                                               TEST_IPSEC_DSCP_VAL;
+
                } else {
+                       if (flags->dscp == TEST_IPSEC_SET_DSCP_0_INNER_1)
+                               ipsec_xform.tunnel.ipv6.dscp = 0;
+
+                       if (flags->dscp == TEST_IPSEC_SET_DSCP_1_INNER_0)
+                               ipsec_xform.tunnel.ipv6.dscp =
+                                               TEST_IPSEC_DSCP_VAL;
+
                        memcpy(&ipsec_xform.tunnel.ipv6.src_addr, &v6_src,
                               sizeof(v6_src));
                        memcpy(&ipsec_xform.tunnel.ipv6.dst_addr, &v6_dst,
@@ -9278,6 +9301,18 @@ test_ipsec_proto_process(const struct ipsec_test_data td[],
                return TEST_SKIPPED;
 
        for (i = 0; i < nb_td; i++) {
+               if (flags->antireplay &&
+                   (dir == RTE_SECURITY_IPSEC_SA_DIR_EGRESS)) {
+                       sess_conf.ipsec.esn.value = td[i].ipsec_xform.esn.value;
+                       ret = rte_security_session_update(ctx,
+                               ut_params->sec_session, &sess_conf);
+                       if (ret) {
+                               printf("Could not update sequence number in "
+                                      "session\n");
+                               return TEST_SKIPPED;
+                       }
+               }
+
                /* Setup source mbuf payload */
                ut_params->ibuf = rte_pktmbuf_alloc(ts_params->mbuf_pool);
                memset(rte_pktmbuf_mtod(ut_params->ibuf, uint8_t *), 0,
@@ -9330,7 +9365,8 @@ test_ipsec_proto_process(const struct ipsec_test_data td[],
                /* Process crypto operation */
                process_crypto_request(dev_id, ut_params->op);
 
-               ret = test_ipsec_status_check(ut_params->op, flags, dir, i + 1);
+               ret = test_ipsec_status_check(&td[i], ut_params->op, flags, dir,
+                                             i + 1);
                if (ret != TEST_SUCCESS)
                        goto crypto_op_free;
 
@@ -9688,6 +9724,17 @@ test_ipsec_proto_transport_v4(const void *data __rte_unused)
        return test_ipsec_proto_all(&flags);
 }
 
+static int
+test_ipsec_proto_transport_l4_csum(const void *data __rte_unused)
+{
+       struct ipsec_test_flags flags = {
+               .l4_csum = true,
+               .transport = true,
+       };
+
+       return test_ipsec_proto_all(&flags);
+}
+
 static int
 test_ipsec_proto_stats(const void *data __rte_unused)
 {
@@ -9761,6 +9808,270 @@ test_ipsec_proto_set_df_1_inner_0(const void *data __rte_unused)
        return test_ipsec_proto_all(&flags);
 }
 
+static int
+test_ipsec_proto_ipv4_copy_dscp_inner_0(const void *data __rte_unused)
+{
+       struct ipsec_test_flags flags;
+
+       memset(&flags, 0, sizeof(flags));
+
+       flags.dscp = TEST_IPSEC_COPY_DSCP_INNER_0;
+
+       return test_ipsec_proto_all(&flags);
+}
+
+static int
+test_ipsec_proto_ipv4_copy_dscp_inner_1(const void *data __rte_unused)
+{
+       struct ipsec_test_flags flags;
+
+       memset(&flags, 0, sizeof(flags));
+
+       flags.dscp = TEST_IPSEC_COPY_DSCP_INNER_1;
+
+       return test_ipsec_proto_all(&flags);
+}
+
+static int
+test_ipsec_proto_ipv4_set_dscp_0_inner_1(const void *data __rte_unused)
+{
+       struct ipsec_test_flags flags;
+
+       if (gbl_driver_id == rte_cryptodev_driver_id_get(
+                       RTE_STR(CRYPTODEV_NAME_CN9K_PMD)))
+               return TEST_SKIPPED;
+
+       memset(&flags, 0, sizeof(flags));
+
+       flags.dscp = TEST_IPSEC_SET_DSCP_0_INNER_1;
+
+       return test_ipsec_proto_all(&flags);
+}
+
+static int
+test_ipsec_proto_ipv4_set_dscp_1_inner_0(const void *data __rte_unused)
+{
+       struct ipsec_test_flags flags;
+
+       if (gbl_driver_id == rte_cryptodev_driver_id_get(
+                       RTE_STR(CRYPTODEV_NAME_CN9K_PMD)))
+               return TEST_SKIPPED;
+
+       memset(&flags, 0, sizeof(flags));
+
+       flags.dscp = TEST_IPSEC_SET_DSCP_1_INNER_0;
+
+       return test_ipsec_proto_all(&flags);
+}
+
+static int
+test_ipsec_proto_ipv6_copy_dscp_inner_0(const void *data __rte_unused)
+{
+       struct ipsec_test_flags flags;
+
+       memset(&flags, 0, sizeof(flags));
+
+       flags.ipv6 = true;
+       flags.tunnel_ipv6 = true;
+       flags.dscp = TEST_IPSEC_COPY_DSCP_INNER_0;
+
+       return test_ipsec_proto_all(&flags);
+}
+
+static int
+test_ipsec_proto_ipv6_copy_dscp_inner_1(const void *data __rte_unused)
+{
+       struct ipsec_test_flags flags;
+
+       memset(&flags, 0, sizeof(flags));
+
+       flags.ipv6 = true;
+       flags.tunnel_ipv6 = true;
+       flags.dscp = TEST_IPSEC_COPY_DSCP_INNER_1;
+
+       return test_ipsec_proto_all(&flags);
+}
+
+static int
+test_ipsec_proto_ipv6_set_dscp_0_inner_1(const void *data __rte_unused)
+{
+       struct ipsec_test_flags flags;
+
+       if (gbl_driver_id == rte_cryptodev_driver_id_get(
+                       RTE_STR(CRYPTODEV_NAME_CN9K_PMD)))
+               return TEST_SKIPPED;
+
+       memset(&flags, 0, sizeof(flags));
+
+       flags.ipv6 = true;
+       flags.tunnel_ipv6 = true;
+       flags.dscp = TEST_IPSEC_SET_DSCP_0_INNER_1;
+
+       return test_ipsec_proto_all(&flags);
+}
+
+static int
+test_ipsec_proto_ipv6_set_dscp_1_inner_0(const void *data __rte_unused)
+{
+       struct ipsec_test_flags flags;
+
+       if (gbl_driver_id == rte_cryptodev_driver_id_get(
+                       RTE_STR(CRYPTODEV_NAME_CN9K_PMD)))
+               return TEST_SKIPPED;
+
+       memset(&flags, 0, sizeof(flags));
+
+       flags.ipv6 = true;
+       flags.tunnel_ipv6 = true;
+       flags.dscp = TEST_IPSEC_SET_DSCP_1_INNER_0;
+
+       return test_ipsec_proto_all(&flags);
+}
+
+static int
+test_ipsec_pkt_replay(const void *test_data, const uint64_t esn[],
+                     bool replayed_pkt[], uint32_t nb_pkts, bool esn_en,
+                     uint64_t winsz)
+{
+       struct ipsec_test_data td_outb[IPSEC_TEST_PACKETS_MAX];
+       struct ipsec_test_data td_inb[IPSEC_TEST_PACKETS_MAX];
+       struct ipsec_test_flags flags;
+       uint32_t i = 0, ret = 0;
+
+       memset(&flags, 0, sizeof(flags));
+       flags.antireplay = true;
+
+       for (i = 0; i < nb_pkts; i++) {
+               memcpy(&td_outb[i], test_data, sizeof(td_outb[i]));
+               td_outb[i].ipsec_xform.options.iv_gen_disable = 1;
+               td_outb[i].ipsec_xform.replay_win_sz = winsz;
+               td_outb[i].ipsec_xform.options.esn = esn_en;
+       }
+
+       for (i = 0; i < nb_pkts; i++)
+               td_outb[i].ipsec_xform.esn.value = esn[i];
+
+       ret = test_ipsec_proto_process(td_outb, td_inb, nb_pkts, true,
+                                      &flags);
+       if (ret != TEST_SUCCESS)
+               return ret;
+
+       test_ipsec_td_update(td_inb, td_outb, nb_pkts, &flags);
+
+       for (i = 0; i < nb_pkts; i++) {
+               td_inb[i].ipsec_xform.options.esn = esn_en;
+               /* Set antireplay flag for packets to be dropped */
+               td_inb[i].ar_packet = replayed_pkt[i];
+       }
+
+       ret = test_ipsec_proto_process(td_inb, NULL, nb_pkts, true,
+                                      &flags);
+
+       return ret;
+}
+
+static int
+test_ipsec_proto_pkt_antireplay(const void *test_data, uint64_t winsz)
+{
+
+       uint32_t nb_pkts = 5;
+       bool replayed_pkt[5];
+       uint64_t esn[5];
+
+       /* 1. Advance the TOP of the window to WS * 2 */
+       esn[0] = winsz * 2;
+       /* 2. Test sequence number within the new window(WS + 1) */
+       esn[1] = winsz + 1;
+       /* 3. Test sequence number less than the window BOTTOM */
+       esn[2] = winsz;
+       /* 4. Test sequence number in the middle of the window */
+       esn[3] = winsz + (winsz / 2);
+       /* 5. Test replay of the packet in the middle of the window */
+       esn[4] = winsz + (winsz / 2);
+
+       replayed_pkt[0] = false;
+       replayed_pkt[1] = false;
+       replayed_pkt[2] = true;
+       replayed_pkt[3] = false;
+       replayed_pkt[4] = true;
+
+       return test_ipsec_pkt_replay(test_data, esn, replayed_pkt, nb_pkts,
+                                    false, winsz);
+}
+
+static int
+test_ipsec_proto_pkt_antireplay1024(const void *test_data)
+{
+       return test_ipsec_proto_pkt_antireplay(test_data, 1024);
+}
+
+static int
+test_ipsec_proto_pkt_antireplay2048(const void *test_data)
+{
+       return test_ipsec_proto_pkt_antireplay(test_data, 2048);
+}
+
+static int
+test_ipsec_proto_pkt_antireplay4096(const void *test_data)
+{
+       return test_ipsec_proto_pkt_antireplay(test_data, 4096);
+}
+
+static int
+test_ipsec_proto_pkt_esn_antireplay(const void *test_data, uint64_t winsz)
+{
+
+       uint32_t nb_pkts = 7;
+       bool replayed_pkt[7];
+       uint64_t esn[7];
+
+       /* Set the initial sequence number */
+       esn[0] = (uint64_t)(0xFFFFFFFF - winsz);
+       /* 1. Advance the TOP of the window to (1<<32 + WS/2) */
+       esn[1] = (uint64_t)((1ULL << 32) + (winsz / 2));
+       /* 2. Test sequence number within new window (1<<32 + WS/2 + 1) */
+       esn[2] = (uint64_t)((1ULL << 32) - (winsz / 2) + 1);
+       /* 3. Test with sequence number within window (1<<32 - 1) */
+       esn[3] = (uint64_t)((1ULL << 32) - 1);
+       /* 4. Test with sequence number within window (1<<32 - 1) */
+       esn[4] = (uint64_t)(1ULL << 32);
+       /* 5. Test with duplicate sequence number within
+        * new window (1<<32 - 1)
+        */
+       esn[5] = (uint64_t)((1ULL << 32) - 1);
+       /* 6. Test with duplicate sequence number within new window (1<<32) */
+       esn[6] = (uint64_t)(1ULL << 32);
+
+       replayed_pkt[0] = false;
+       replayed_pkt[1] = false;
+       replayed_pkt[2] = false;
+       replayed_pkt[3] = false;
+       replayed_pkt[4] = false;
+       replayed_pkt[5] = true;
+       replayed_pkt[6] = true;
+
+       return test_ipsec_pkt_replay(test_data, esn, replayed_pkt, nb_pkts,
+                                    true, winsz);
+}
+
+static int
+test_ipsec_proto_pkt_esn_antireplay1024(const void *test_data)
+{
+       return test_ipsec_proto_pkt_esn_antireplay(test_data, 1024);
+}
+
+static int
+test_ipsec_proto_pkt_esn_antireplay2048(const void *test_data)
+{
+       return test_ipsec_proto_pkt_esn_antireplay(test_data, 2048);
+}
+
+static int
+test_ipsec_proto_pkt_esn_antireplay4096(const void *test_data)
+{
+       return test_ipsec_proto_pkt_esn_antireplay(test_data, 4096);
+}
+
 static int
 test_PDCP_PROTO_all(void)
 {
@@ -9802,6 +10113,27 @@ test_PDCP_PROTO_all(void)
                return TEST_SUCCESS;
 }
 
+static int
+test_ipsec_proto_ipv4_ttl_decrement(const void *data __rte_unused)
+{
+       struct ipsec_test_flags flags = {
+               .dec_ttl_or_hop_limit = true
+       };
+
+       return test_ipsec_proto_all(&flags);
+}
+
+static int
+test_ipsec_proto_ipv6_hop_limit_decrement(const void *data __rte_unused)
+{
+       struct ipsec_test_flags flags = {
+               .ipv6 = true,
+               .dec_ttl_or_hop_limit = true
+       };
+
+       return test_ipsec_proto_all(&flags);
+}
+
 static int
 test_docsis_proto_uplink(const void *data)
 {
@@ -14775,6 +15107,10 @@ static struct unit_test_suite ipsec_proto_testsuite  = {
                        "Transport IPv4",
                        ut_setup_security, ut_teardown,
                        test_ipsec_proto_transport_v4),
+               TEST_CASE_NAMED_ST(
+                       "Transport l4 checksum",
+                       ut_setup_security, ut_teardown,
+                       test_ipsec_proto_transport_l4_csum),
                TEST_CASE_NAMED_ST(
                        "Statistics: success",
                        ut_setup_security, ut_teardown,
@@ -14799,6 +15135,73 @@ static struct unit_test_suite ipsec_proto_testsuite  = {
                        "Tunnel header set DF 1 (inner 0)",
                        ut_setup_security, ut_teardown,
                        test_ipsec_proto_set_df_1_inner_0),
+               TEST_CASE_NAMED_ST(
+                       "Tunnel header IPv4 copy DSCP (inner 0)",
+                       ut_setup_security, ut_teardown,
+                       test_ipsec_proto_ipv4_copy_dscp_inner_0),
+               TEST_CASE_NAMED_ST(
+                       "Tunnel header IPv4 copy DSCP (inner 1)",
+                       ut_setup_security, ut_teardown,
+                       test_ipsec_proto_ipv4_copy_dscp_inner_1),
+               TEST_CASE_NAMED_ST(
+                       "Tunnel header IPv4 set DSCP 0 (inner 1)",
+                       ut_setup_security, ut_teardown,
+                       test_ipsec_proto_ipv4_set_dscp_0_inner_1),
+               TEST_CASE_NAMED_ST(
+                       "Tunnel header IPv4 set DSCP 1 (inner 0)",
+                       ut_setup_security, ut_teardown,
+                       test_ipsec_proto_ipv4_set_dscp_1_inner_0),
+               TEST_CASE_NAMED_ST(
+                       "Tunnel header IPv6 copy DSCP (inner 0)",
+                       ut_setup_security, ut_teardown,
+                       test_ipsec_proto_ipv6_copy_dscp_inner_0),
+               TEST_CASE_NAMED_ST(
+                       "Tunnel header IPv6 copy DSCP (inner 1)",
+                       ut_setup_security, ut_teardown,
+                       test_ipsec_proto_ipv6_copy_dscp_inner_1),
+               TEST_CASE_NAMED_ST(
+                       "Tunnel header IPv6 set DSCP 0 (inner 1)",
+                       ut_setup_security, ut_teardown,
+                       test_ipsec_proto_ipv6_set_dscp_0_inner_1),
+               TEST_CASE_NAMED_ST(
+                       "Tunnel header IPv6 set DSCP 1 (inner 0)",
+                       ut_setup_security, ut_teardown,
+                       test_ipsec_proto_ipv6_set_dscp_1_inner_0),
+               TEST_CASE_NAMED_WITH_DATA(
+                       "Antireplay with window size 1024",
+                       ut_setup_security, ut_teardown,
+                       test_ipsec_proto_pkt_antireplay1024, &pkt_aes_128_gcm),
+               TEST_CASE_NAMED_WITH_DATA(
+                       "Antireplay with window size 2048",
+                       ut_setup_security, ut_teardown,
+                       test_ipsec_proto_pkt_antireplay2048, &pkt_aes_128_gcm),
+               TEST_CASE_NAMED_WITH_DATA(
+                       "Antireplay with window size 4096",
+                       ut_setup_security, ut_teardown,
+                       test_ipsec_proto_pkt_antireplay4096, &pkt_aes_128_gcm),
+               TEST_CASE_NAMED_WITH_DATA(
+                       "ESN and Antireplay with window size 1024",
+                       ut_setup_security, ut_teardown,
+                       test_ipsec_proto_pkt_esn_antireplay1024,
+                       &pkt_aes_128_gcm),
+               TEST_CASE_NAMED_WITH_DATA(
+                       "ESN and Antireplay with window size 2048",
+                       ut_setup_security, ut_teardown,
+                       test_ipsec_proto_pkt_esn_antireplay2048,
+                       &pkt_aes_128_gcm),
+               TEST_CASE_NAMED_WITH_DATA(
+                       "ESN and Antireplay with window size 4096",
+                       ut_setup_security, ut_teardown,
+                       test_ipsec_proto_pkt_esn_antireplay4096,
+                       &pkt_aes_128_gcm),
+               TEST_CASE_NAMED_ST(
+                       "Tunnel header IPv4 decrement inner TTL",
+                       ut_setup_security, ut_teardown,
+                       test_ipsec_proto_ipv4_ttl_decrement),
+               TEST_CASE_NAMED_ST(
+                       "Tunnel header IPv6 decrement inner hop limit",
+                       ut_setup_security, ut_teardown,
+                       test_ipsec_proto_ipv6_hop_limit_decrement),
                TEST_CASES_END() /**< NULL terminate unit test array */
        }
 };
@@ -16093,3 +16496,5 @@ REGISTER_TEST_COMMAND(cryptodev_nitrox_autotest, test_cryptodev_nitrox);
 REGISTER_TEST_COMMAND(cryptodev_bcmfs_autotest, test_cryptodev_bcmfs);
 REGISTER_TEST_COMMAND(cryptodev_cn9k_autotest, test_cryptodev_cn9k);
 REGISTER_TEST_COMMAND(cryptodev_cn10k_autotest, test_cryptodev_cn10k);
+
+#endif /* !RTE_EXEC_ENV_WINDOWS */