1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(C) 2021 Marvell.
5 #ifndef RTE_EXEC_ENV_WINDOWS
7 #include <rte_common.h>
8 #include <rte_cryptodev.h>
11 #include <rte_security.h>
16 #include "test_cryptodev_security_ipsec.h"
20 struct crypto_param_comb alg_list[RTE_DIM(aead_list) +
21 (RTE_DIM(cipher_list) *
24 struct crypto_param_comb ah_alg_list[2 * (RTE_DIM(auth_list) - 1)];
27 is_valid_ipv4_pkt(const struct rte_ipv4_hdr *pkt)
29 /* The IP version number must be 4 */
30 if (((pkt->version_ihl) >> 4) != 4)
33 * The IP header length field must be large enough to hold the
34 * minimum length legal IP datagram (20 bytes = 5 words).
36 if ((pkt->version_ihl & 0xf) < 5)
40 * The IP total length field must be large enough to hold the IP
41 * datagram header, whose length is specified in the IP header length
44 if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct rte_ipv4_hdr))
51 is_valid_ipv6_pkt(const struct rte_ipv6_hdr *pkt)
53 /* The IP version number must be 6 */
54 if ((rte_be_to_cpu_32((pkt->vtc_flow)) >> 28) != 6)
61 test_ipsec_alg_list_populate(void)
63 unsigned long i, j, index = 0;
65 for (i = 0; i < RTE_DIM(aead_list); i++) {
66 alg_list[index].param1 = &aead_list[i];
67 alg_list[index].param2 = NULL;
71 for (i = 0; i < RTE_DIM(cipher_list); i++) {
72 for (j = 0; j < RTE_DIM(auth_list); j++) {
73 alg_list[index].param1 = &cipher_list[i];
74 alg_list[index].param2 = &auth_list[j];
81 test_ipsec_ah_alg_list_populate(void)
83 unsigned long i, index = 0;
85 for (i = 1; i < RTE_DIM(auth_list); i++) {
86 ah_alg_list[index].param1 = &auth_list[i];
87 ah_alg_list[index].param2 = NULL;
91 for (i = 1; i < RTE_DIM(auth_list); i++) {
93 ah_alg_list[index].param1 = &cipher_list[0];
95 ah_alg_list[index].param2 = &auth_list[i];
101 test_ipsec_sec_caps_verify(struct rte_security_ipsec_xform *ipsec_xform,
102 const struct rte_security_capability *sec_cap,
105 /* Verify security capabilities */
107 if (ipsec_xform->options.esn == 1 && sec_cap->ipsec.options.esn == 0) {
109 RTE_LOG(INFO, USER1, "ESN is not supported\n");
113 if (ipsec_xform->options.udp_encap == 1 &&
114 sec_cap->ipsec.options.udp_encap == 0) {
116 RTE_LOG(INFO, USER1, "UDP encapsulation is not supported\n");
120 if (ipsec_xform->options.udp_ports_verify == 1 &&
121 sec_cap->ipsec.options.udp_ports_verify == 0) {
123 RTE_LOG(INFO, USER1, "UDP encapsulation ports "
124 "verification is not supported\n");
128 if (ipsec_xform->options.copy_dscp == 1 &&
129 sec_cap->ipsec.options.copy_dscp == 0) {
131 RTE_LOG(INFO, USER1, "Copy DSCP is not supported\n");
135 if (ipsec_xform->options.copy_flabel == 1 &&
136 sec_cap->ipsec.options.copy_flabel == 0) {
138 RTE_LOG(INFO, USER1, "Copy Flow Label is not supported\n");
142 if (ipsec_xform->options.copy_df == 1 &&
143 sec_cap->ipsec.options.copy_df == 0) {
145 RTE_LOG(INFO, USER1, "Copy DP bit is not supported\n");
149 if (ipsec_xform->options.dec_ttl == 1 &&
150 sec_cap->ipsec.options.dec_ttl == 0) {
152 RTE_LOG(INFO, USER1, "Decrement TTL is not supported\n");
156 if (ipsec_xform->options.ecn == 1 && sec_cap->ipsec.options.ecn == 0) {
158 RTE_LOG(INFO, USER1, "ECN is not supported\n");
162 if (ipsec_xform->options.stats == 1 &&
163 sec_cap->ipsec.options.stats == 0) {
165 RTE_LOG(INFO, USER1, "Stats is not supported\n");
169 if ((ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) &&
170 (ipsec_xform->options.iv_gen_disable == 1) &&
171 (sec_cap->ipsec.options.iv_gen_disable != 1)) {
174 "Application provided IV is not supported\n");
178 if ((ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS) &&
179 (ipsec_xform->options.tunnel_hdr_verify >
180 sec_cap->ipsec.options.tunnel_hdr_verify)) {
183 "Tunnel header verify is not supported\n");
187 if (ipsec_xform->options.ip_csum_enable == 1 &&
188 sec_cap->ipsec.options.ip_csum_enable == 0) {
191 "Inner IP checksum is not supported\n");
195 if (ipsec_xform->options.l4_csum_enable == 1 &&
196 sec_cap->ipsec.options.l4_csum_enable == 0) {
199 "Inner L4 checksum is not supported\n");
203 if (ipsec_xform->replay_win_sz > sec_cap->ipsec.replay_win_sz_max) {
206 "Replay window size is not supported\n");
214 test_ipsec_crypto_caps_aead_verify(
215 const struct rte_security_capability *sec_cap,
216 struct rte_crypto_sym_xform *aead)
218 const struct rte_cryptodev_symmetric_capability *sym_cap;
219 const struct rte_cryptodev_capabilities *crypto_cap;
222 while ((crypto_cap = &sec_cap->crypto_capabilities[j++])->op !=
223 RTE_CRYPTO_OP_TYPE_UNDEFINED) {
224 if (crypto_cap->op == RTE_CRYPTO_OP_TYPE_SYMMETRIC &&
225 crypto_cap->sym.xform_type == aead->type &&
226 crypto_cap->sym.aead.algo == aead->aead.algo) {
227 sym_cap = &crypto_cap->sym;
228 if (rte_cryptodev_sym_capability_check_aead(sym_cap,
229 aead->aead.key.length,
230 aead->aead.digest_length,
231 aead->aead.aad_length,
232 aead->aead.iv.length) == 0)
241 test_ipsec_crypto_caps_cipher_verify(
242 const struct rte_security_capability *sec_cap,
243 struct rte_crypto_sym_xform *cipher)
245 const struct rte_cryptodev_symmetric_capability *sym_cap;
246 const struct rte_cryptodev_capabilities *cap;
249 while ((cap = &sec_cap->crypto_capabilities[j++])->op !=
250 RTE_CRYPTO_OP_TYPE_UNDEFINED) {
251 if (cap->op == RTE_CRYPTO_OP_TYPE_SYMMETRIC &&
252 cap->sym.xform_type == cipher->type &&
253 cap->sym.cipher.algo == cipher->cipher.algo) {
255 if (rte_cryptodev_sym_capability_check_cipher(sym_cap,
256 cipher->cipher.key.length,
257 cipher->cipher.iv.length) == 0)
266 test_ipsec_crypto_caps_auth_verify(
267 const struct rte_security_capability *sec_cap,
268 struct rte_crypto_sym_xform *auth)
270 const struct rte_cryptodev_symmetric_capability *sym_cap;
271 const struct rte_cryptodev_capabilities *cap;
274 while ((cap = &sec_cap->crypto_capabilities[j++])->op !=
275 RTE_CRYPTO_OP_TYPE_UNDEFINED) {
276 if (cap->op == RTE_CRYPTO_OP_TYPE_SYMMETRIC &&
277 cap->sym.xform_type == auth->type &&
278 cap->sym.auth.algo == auth->auth.algo) {
280 if (rte_cryptodev_sym_capability_check_auth(sym_cap,
281 auth->auth.key.length,
282 auth->auth.digest_length,
283 auth->auth.iv.length) == 0)
292 test_ipsec_td_in_from_out(const struct ipsec_test_data *td_out,
293 struct ipsec_test_data *td_in)
295 memcpy(td_in, td_out, sizeof(*td_in));
297 /* Populate output text of td_in with input text of td_out */
298 memcpy(td_in->output_text.data, td_out->input_text.data,
299 td_out->input_text.len);
300 td_in->output_text.len = td_out->input_text.len;
302 /* Populate input text of td_in with output text of td_out */
303 memcpy(td_in->input_text.data, td_out->output_text.data,
304 td_out->output_text.len);
305 td_in->input_text.len = td_out->output_text.len;
307 td_in->ipsec_xform.direction = RTE_SECURITY_IPSEC_SA_DIR_INGRESS;
310 td_in->xform.aead.aead.op = RTE_CRYPTO_AEAD_OP_DECRYPT;
312 td_in->xform.chain.auth.auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
313 td_in->xform.chain.cipher.cipher.op =
314 RTE_CRYPTO_CIPHER_OP_DECRYPT;
321 struct rte_ipv4_hdr *ipv4 = ip;
324 ip_ver = (ipv4->version_ihl & 0xf0) >> RTE_IPV4_IHL_MULTIPLIER;
325 if (ip_ver == IPVERSION)
332 test_ipsec_csum_init(void *ip, bool l3, bool l4)
334 struct rte_ipv4_hdr *ipv4;
335 struct rte_tcp_hdr *tcp;
336 struct rte_udp_hdr *udp;
342 size = sizeof(struct rte_ipv4_hdr);
343 next_proto = ipv4->next_proto_id;
346 ipv4->hdr_checksum = 0;
348 size = sizeof(struct rte_ipv6_hdr);
349 next_proto = ((struct rte_ipv6_hdr *)ip)->proto;
353 switch (next_proto) {
355 tcp = (struct rte_tcp_hdr *)RTE_PTR_ADD(ip, size);
359 udp = (struct rte_udp_hdr *)RTE_PTR_ADD(ip, size);
360 udp->dgram_cksum = 0;
369 test_ipsec_td_prepare(const struct crypto_param *param1,
370 const struct crypto_param *param2,
371 const struct ipsec_test_flags *flags,
372 struct ipsec_test_data *td_array,
376 struct ipsec_test_data *td;
379 memset(td_array, 0, nb_td * sizeof(*td));
381 for (i = 0; i < nb_td; i++) {
384 /* Prepare fields based on param */
386 if (param1->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
387 /* Copy template for packet & key fields */
389 memcpy(td, &pkt_aes_256_gcm_v6, sizeof(*td));
391 memcpy(td, &pkt_aes_256_gcm, sizeof(*td));
394 td->xform.aead.aead.algo = param1->alg.aead;
395 td->xform.aead.aead.key.length = param1->key_length;
397 /* Copy template for packet & key fields */
399 memcpy(td, &pkt_aes_128_cbc_hmac_sha256_v6,
402 memcpy(td, &pkt_aes_128_cbc_hmac_sha256,
407 if (param1->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
408 td->xform.chain.auth.auth.algo =
410 td->xform.chain.auth.auth.key.length =
412 td->xform.chain.auth.auth.digest_length =
413 param1->digest_length;
414 td->auth_only = true;
416 if (td->xform.chain.auth.auth.algo == RTE_CRYPTO_AUTH_AES_GMAC) {
417 td->xform.chain.auth.auth.iv.length =
422 td->xform.chain.cipher.cipher.algo =
424 td->xform.chain.cipher.cipher.key.length =
426 td->xform.chain.cipher.cipher.iv.length =
428 td->xform.chain.auth.auth.algo =
430 td->xform.chain.auth.auth.key.length =
432 td->xform.chain.auth.auth.digest_length =
433 param2->digest_length;
435 if (td->xform.chain.auth.auth.algo == RTE_CRYPTO_AUTH_AES_GMAC) {
436 td->xform.chain.auth.auth.iv.length =
444 td->ipsec_xform.proto =
445 RTE_SECURITY_IPSEC_SA_PROTO_AH;
449 td->ipsec_xform.options.iv_gen_disable = 0;
451 if (flags->sa_expiry_pkts_soft)
452 td->ipsec_xform.life.packets_soft_limit =
453 IPSEC_TEST_PACKETS_MAX - 1;
455 if (flags->ip_csum) {
456 td->ipsec_xform.options.ip_csum_enable = 1;
457 test_ipsec_csum_init(&td->input_text.data, true, false);
460 if (flags->l4_csum) {
461 td->ipsec_xform.options.l4_csum_enable = 1;
462 test_ipsec_csum_init(&td->input_text.data, false, true);
465 if (flags->transport) {
466 td->ipsec_xform.mode =
467 RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT;
469 td->ipsec_xform.mode =
470 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL;
472 if (flags->tunnel_ipv6)
473 td->ipsec_xform.tunnel.type =
474 RTE_SECURITY_IPSEC_TUNNEL_IPV6;
476 td->ipsec_xform.tunnel.type =
477 RTE_SECURITY_IPSEC_TUNNEL_IPV4;
480 if (flags->stats_success)
481 td->ipsec_xform.options.stats = 1;
483 if (flags->fragment) {
484 struct rte_ipv4_hdr *ip;
485 ip = (struct rte_ipv4_hdr *)&td->input_text.data;
486 ip->fragment_offset = 4;
487 ip->hdr_checksum = rte_ipv4_cksum(ip);
490 if (flags->df == TEST_IPSEC_COPY_DF_INNER_0 ||
491 flags->df == TEST_IPSEC_COPY_DF_INNER_1)
492 td->ipsec_xform.options.copy_df = 1;
494 if (flags->dscp == TEST_IPSEC_COPY_DSCP_INNER_0 ||
495 flags->dscp == TEST_IPSEC_COPY_DSCP_INNER_1)
496 td->ipsec_xform.options.copy_dscp = 1;
498 if (flags->dec_ttl_or_hop_limit)
499 td->ipsec_xform.options.dec_ttl = 1;
504 test_ipsec_td_update(struct ipsec_test_data td_inb[],
505 const struct ipsec_test_data td_outb[],
507 const struct ipsec_test_flags *flags)
511 for (i = 0; i < nb_td; i++) {
512 memcpy(td_inb[i].output_text.data, td_outb[i].input_text.data,
513 td_outb[i].input_text.len);
514 td_inb[i].output_text.len = td_outb->input_text.len;
516 if (flags->icv_corrupt) {
517 int icv_pos = td_inb[i].input_text.len - 4;
518 td_inb[i].input_text.data[icv_pos] += 1;
521 if (flags->sa_expiry_pkts_hard)
522 td_inb[i].ipsec_xform.life.packets_hard_limit =
523 IPSEC_TEST_PACKETS_MAX - 1;
525 if (flags->udp_encap)
526 td_inb[i].ipsec_xform.options.udp_encap = 1;
528 if (flags->udp_ports_verify)
529 td_inb[i].ipsec_xform.options.udp_ports_verify = 1;
531 td_inb[i].ipsec_xform.options.tunnel_hdr_verify =
532 flags->tunnel_hdr_verify;
535 td_inb[i].ipsec_xform.options.ip_csum_enable = 1;
538 td_inb[i].ipsec_xform.options.l4_csum_enable = 1;
540 /* Clear outbound specific flags */
541 td_inb[i].ipsec_xform.options.iv_gen_disable = 0;
546 test_ipsec_display_alg(const struct crypto_param *param1,
547 const struct crypto_param *param2)
549 if (param1->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
551 rte_crypto_aead_algorithm_strings[param1->alg.aead],
552 param1->key_length * 8);
553 } else if (param1->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
555 rte_crypto_auth_algorithm_strings[param1->alg.auth]);
556 if (param1->alg.auth != RTE_CRYPTO_AUTH_NULL)
557 printf(" [%dB ICV]", param1->digest_length);
560 rte_crypto_cipher_algorithm_strings[param1->alg.cipher]);
561 if (param1->alg.cipher != RTE_CRYPTO_CIPHER_NULL)
562 printf(" [%d]", param1->key_length * 8);
564 rte_crypto_auth_algorithm_strings[param2->alg.auth]);
565 if (param2->alg.auth != RTE_CRYPTO_AUTH_NULL)
566 printf(" [%dB ICV]", param2->digest_length);
572 test_ipsec_tunnel_hdr_len_get(const struct ipsec_test_data *td)
576 if (td->ipsec_xform.direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
577 if (td->ipsec_xform.mode == RTE_SECURITY_IPSEC_SA_MODE_TUNNEL) {
578 if (td->ipsec_xform.tunnel.type ==
579 RTE_SECURITY_IPSEC_TUNNEL_IPV4)
580 len += sizeof(struct rte_ipv4_hdr);
582 len += sizeof(struct rte_ipv6_hdr);
590 test_ipsec_iv_verify_push(struct rte_mbuf *m, const struct ipsec_test_data *td)
592 static uint8_t iv_queue[IV_LEN_MAX * IPSEC_TEST_PACKETS_MAX];
593 uint8_t *iv_tmp, *output_text = rte_pktmbuf_mtod(m, uint8_t *);
594 int i, iv_pos, iv_len;
598 iv_len = td->xform.aead.aead.iv.length - td->salt.len;
600 iv_len = td->xform.chain.cipher.cipher.iv.length;
602 iv_pos = test_ipsec_tunnel_hdr_len_get(td) + sizeof(struct rte_esp_hdr);
603 output_text += iv_pos;
605 TEST_ASSERT(iv_len <= IV_LEN_MAX, "IV length greater than supported");
607 /* Compare against previous values */
608 for (i = 0; i < index; i++) {
609 iv_tmp = &iv_queue[i * IV_LEN_MAX];
611 if (memcmp(output_text, iv_tmp, iv_len) == 0) {
612 printf("IV repeated");
617 /* Save IV for future comparisons */
619 iv_tmp = &iv_queue[index * IV_LEN_MAX];
620 memcpy(iv_tmp, output_text, iv_len);
623 if (index == IPSEC_TEST_PACKETS_MAX)
630 test_ipsec_l3_csum_verify(struct rte_mbuf *m)
632 uint16_t actual_cksum, expected_cksum;
633 struct rte_ipv4_hdr *ip;
635 ip = rte_pktmbuf_mtod(m, struct rte_ipv4_hdr *);
637 if (!is_ipv4((void *)ip))
640 actual_cksum = ip->hdr_checksum;
642 ip->hdr_checksum = 0;
644 expected_cksum = rte_ipv4_cksum(ip);
646 if (actual_cksum != expected_cksum)
653 test_ipsec_l4_csum_verify(struct rte_mbuf *m)
655 uint16_t actual_cksum = 0, expected_cksum = 0;
656 struct rte_ipv4_hdr *ipv4;
657 struct rte_ipv6_hdr *ipv6;
658 struct rte_tcp_hdr *tcp;
659 struct rte_udp_hdr *udp;
662 ip = rte_pktmbuf_mtod(m, void *);
666 l4 = RTE_PTR_ADD(ipv4, sizeof(struct rte_ipv4_hdr));
668 switch (ipv4->next_proto_id) {
670 tcp = (struct rte_tcp_hdr *)l4;
671 actual_cksum = tcp->cksum;
673 expected_cksum = rte_ipv4_udptcp_cksum(ipv4, l4);
676 udp = (struct rte_udp_hdr *)l4;
677 actual_cksum = udp->dgram_cksum;
678 udp->dgram_cksum = 0;
679 expected_cksum = rte_ipv4_udptcp_cksum(ipv4, l4);
686 l4 = RTE_PTR_ADD(ipv6, sizeof(struct rte_ipv6_hdr));
688 switch (ipv6->proto) {
690 tcp = (struct rte_tcp_hdr *)l4;
691 actual_cksum = tcp->cksum;
693 expected_cksum = rte_ipv6_udptcp_cksum(ipv6, l4);
696 udp = (struct rte_udp_hdr *)l4;
697 actual_cksum = udp->dgram_cksum;
698 udp->dgram_cksum = 0;
699 expected_cksum = rte_ipv6_udptcp_cksum(ipv6, l4);
706 if (actual_cksum != expected_cksum)
713 test_ipsec_ttl_or_hop_decrement_verify(void *received, void *expected)
715 struct rte_ipv4_hdr *iph4_ex, *iph4_re;
716 struct rte_ipv6_hdr *iph6_ex, *iph6_re;
718 if (is_ipv4(received) && is_ipv4(expected)) {
721 iph4_ex->time_to_live -= 1;
722 if (iph4_re->time_to_live != iph4_ex->time_to_live)
724 } else if (!is_ipv4(received) && !is_ipv4(expected)) {
727 iph6_ex->hop_limits -= 1;
728 if (iph6_re->hop_limits != iph6_ex->hop_limits)
731 printf("IP header version miss match\n");
739 test_ipsec_td_verify(struct rte_mbuf *m, const struct ipsec_test_data *td,
740 bool silent, const struct ipsec_test_flags *flags)
742 uint8_t *output_text = rte_pktmbuf_mtod(m, uint8_t *);
743 uint32_t skip, len = rte_pktmbuf_pkt_len(m);
744 uint8_t td_output_text[4096];
747 /* For tests with status as error for test success, skip verification */
748 if (td->ipsec_xform.direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS &&
749 (flags->icv_corrupt ||
750 flags->sa_expiry_pkts_hard ||
751 flags->tunnel_hdr_verify ||
755 if (td->ipsec_xform.direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS &&
757 const struct rte_ipv4_hdr *iph4;
758 const struct rte_ipv6_hdr *iph6;
760 if (td->ipsec_xform.tunnel.type ==
761 RTE_SECURITY_IPSEC_TUNNEL_IPV4) {
762 iph4 = (const struct rte_ipv4_hdr *)output_text;
763 if (iph4->next_proto_id != IPPROTO_UDP) {
764 printf("UDP header is not found\n");
768 iph6 = (const struct rte_ipv6_hdr *)output_text;
769 if (iph6->proto != IPPROTO_UDP) {
770 printf("UDP header is not found\n");
775 len -= sizeof(struct rte_udp_hdr);
776 output_text += sizeof(struct rte_udp_hdr);
779 if (len != td->output_text.len) {
780 printf("Output length (%d) not matching with expected (%d)\n",
781 len, td->output_text.len);
785 if ((td->ipsec_xform.direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) &&
787 const struct rte_ipv4_hdr *iph4;
788 iph4 = (const struct rte_ipv4_hdr *)output_text;
789 if (iph4->fragment_offset) {
790 printf("Output packet is fragmented");
795 skip = test_ipsec_tunnel_hdr_len_get(td);
800 if ((td->ipsec_xform.direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS) &&
802 if (m->ol_flags & RTE_MBUF_F_RX_IP_CKSUM_GOOD)
803 ret = test_ipsec_l3_csum_verify(m);
807 if (ret == TEST_FAILED)
808 printf("Inner IP checksum test failed\n");
813 if ((td->ipsec_xform.direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS) &&
815 if (m->ol_flags & RTE_MBUF_F_RX_L4_CKSUM_GOOD)
816 ret = test_ipsec_l4_csum_verify(m);
820 if (ret == TEST_FAILED)
821 printf("Inner L4 checksum test failed\n");
826 memcpy(td_output_text, td->output_text.data + skip, len);
828 if ((td->ipsec_xform.direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS) &&
829 flags->dec_ttl_or_hop_limit) {
830 if (test_ipsec_ttl_or_hop_decrement_verify(output_text, td_output_text)) {
831 printf("Inner TTL/hop limit decrement test failed\n");
836 if (test_ipsec_pkt_update(td_output_text, flags)) {
837 printf("Could not update expected vector");
841 if (memcmp(output_text, td_output_text, len)) {
845 printf("TestCase %s line %d: %s\n", __func__, __LINE__,
846 "output text not as expected\n");
848 rte_hexdump(stdout, "expected", td_output_text, len);
849 rte_hexdump(stdout, "actual", output_text, len);
857 test_ipsec_res_d_prepare(struct rte_mbuf *m, const struct ipsec_test_data *td,
858 struct ipsec_test_data *res_d)
860 uint8_t *output_text = rte_pktmbuf_mtod(m, uint8_t *);
861 uint32_t len = rte_pktmbuf_pkt_len(m);
863 memcpy(res_d, td, sizeof(*res_d));
864 memcpy(res_d->input_text.data, output_text, len);
865 res_d->input_text.len = len;
867 res_d->ipsec_xform.direction = RTE_SECURITY_IPSEC_SA_DIR_INGRESS;
869 res_d->xform.aead.aead.op = RTE_CRYPTO_AEAD_OP_DECRYPT;
871 res_d->xform.chain.cipher.cipher.op =
872 RTE_CRYPTO_CIPHER_OP_DECRYPT;
873 res_d->xform.chain.auth.auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
880 test_ipsec_iph4_hdr_validate(const struct rte_ipv4_hdr *iph4,
881 const struct ipsec_test_flags *flags)
886 if (!is_valid_ipv4_pkt(iph4)) {
887 printf("Tunnel outer header is not IPv4\n");
891 if (flags->ah && iph4->next_proto_id != IPPROTO_AH) {
892 printf("Tunnel outer header proto is not AH\n");
896 f_off = rte_be_to_cpu_16(iph4->fragment_offset);
897 if (flags->df == TEST_IPSEC_COPY_DF_INNER_1 ||
898 flags->df == TEST_IPSEC_SET_DF_1_INNER_0) {
899 if (!(f_off & RTE_IPV4_HDR_DF_FLAG)) {
900 printf("DF bit is not set\n");
904 if (f_off & RTE_IPV4_HDR_DF_FLAG) {
905 printf("DF bit is set\n");
910 tos = iph4->type_of_service;
911 dscp = (tos & RTE_IPV4_HDR_DSCP_MASK) >> 2;
913 if (flags->dscp == TEST_IPSEC_COPY_DSCP_INNER_1 ||
914 flags->dscp == TEST_IPSEC_SET_DSCP_1_INNER_0) {
915 if (dscp != TEST_IPSEC_DSCP_VAL) {
916 printf("DSCP value is not matching [exp: %x, actual: %x]\n",
917 TEST_IPSEC_DSCP_VAL, dscp);
922 printf("DSCP value is set [exp: 0, actual: %x]\n",
932 test_ipsec_iph6_hdr_validate(const struct rte_ipv6_hdr *iph6,
933 const struct ipsec_test_flags *flags)
938 if (!is_valid_ipv6_pkt(iph6)) {
939 printf("Tunnel outer header is not IPv6\n");
943 vtc_flow = rte_be_to_cpu_32(iph6->vtc_flow);
944 dscp = (vtc_flow & RTE_IPV6_HDR_DSCP_MASK) >>
945 (RTE_IPV6_HDR_TC_SHIFT + 2);
947 if (flags->dscp == TEST_IPSEC_COPY_DSCP_INNER_1 ||
948 flags->dscp == TEST_IPSEC_SET_DSCP_1_INNER_0) {
949 if (dscp != TEST_IPSEC_DSCP_VAL) {
950 printf("DSCP value is not matching [exp: %x, actual: %x]\n",
951 TEST_IPSEC_DSCP_VAL, dscp);
956 printf("DSCP value is set [exp: 0, actual: %x]\n",
966 test_ipsec_post_process(struct rte_mbuf *m, const struct ipsec_test_data *td,
967 struct ipsec_test_data *res_d, bool silent,
968 const struct ipsec_test_flags *flags)
970 uint8_t *output_text = rte_pktmbuf_mtod(m, uint8_t *);
973 if (td->ipsec_xform.direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
974 const struct rte_ipv4_hdr *iph4;
975 const struct rte_ipv6_hdr *iph6;
978 ret = test_ipsec_iv_verify_push(m, td);
979 if (ret != TEST_SUCCESS)
983 iph4 = (const struct rte_ipv4_hdr *)output_text;
985 if (td->ipsec_xform.mode ==
986 RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT) {
988 iph6 = (const struct rte_ipv6_hdr *)output_text;
989 if (is_valid_ipv6_pkt(iph6) == false) {
990 printf("Transport packet is not IPv6\n");
994 if (is_valid_ipv4_pkt(iph4) == false) {
995 printf("Transport packet is not IPv4\n");
999 if (flags->ah && iph4->next_proto_id != IPPROTO_AH) {
1000 printf("Transport IPv4 header proto is not AH\n");
1005 if (td->ipsec_xform.tunnel.type ==
1006 RTE_SECURITY_IPSEC_TUNNEL_IPV4) {
1007 if (test_ipsec_iph4_hdr_validate(iph4, flags))
1010 iph6 = (const struct rte_ipv6_hdr *)output_text;
1011 if (test_ipsec_iph6_hdr_validate(iph6, flags))
1018 * In case of known vector tests & all inbound tests, res_d provided
1019 * would be NULL and output data need to be validated against expected.
1020 * For inbound, output_text would be plain packet and for outbound
1021 * output_text would IPsec packet. Validate by comparing against
1024 * In case of combined mode tests, the output_text from outbound
1025 * operation (ie, IPsec packet) would need to be inbound processed to
1026 * obtain the plain text. Copy output_text to result data, 'res_d', so
1027 * that inbound processing can be done.
1031 return test_ipsec_td_verify(m, td, silent, flags);
1033 return test_ipsec_res_d_prepare(m, td, res_d);
1037 test_ipsec_status_check(const struct ipsec_test_data *td,
1038 struct rte_crypto_op *op,
1039 const struct ipsec_test_flags *flags,
1040 enum rte_security_ipsec_sa_direction dir,
1043 int ret = TEST_SUCCESS;
1045 if ((dir == RTE_SECURITY_IPSEC_SA_DIR_INGRESS) &&
1047 if (op->status != RTE_CRYPTO_OP_STATUS_ERROR) {
1048 printf("Anti replay test case failed\n");
1051 return TEST_SUCCESS;
1055 if (dir == RTE_SECURITY_IPSEC_SA_DIR_INGRESS &&
1056 flags->sa_expiry_pkts_hard &&
1057 pkt_num == IPSEC_TEST_PACKETS_MAX) {
1058 if (op->status != RTE_CRYPTO_OP_STATUS_ERROR) {
1059 printf("SA hard expiry (pkts) test failed\n");
1062 return TEST_SUCCESS;
1066 if ((dir == RTE_SECURITY_IPSEC_SA_DIR_INGRESS) &&
1067 flags->tunnel_hdr_verify) {
1068 if (op->status != RTE_CRYPTO_OP_STATUS_ERROR) {
1069 printf("Tunnel header verify test case failed\n");
1072 return TEST_SUCCESS;
1076 if (dir == RTE_SECURITY_IPSEC_SA_DIR_INGRESS && flags->icv_corrupt) {
1077 if (op->status != RTE_CRYPTO_OP_STATUS_ERROR) {
1078 printf("ICV corruption test case failed\n");
1082 if (op->status != RTE_CRYPTO_OP_STATUS_SUCCESS) {
1083 printf("Security op processing failed [pkt_num: %d]\n",
1089 if (flags->sa_expiry_pkts_soft && pkt_num == IPSEC_TEST_PACKETS_MAX) {
1090 if (!(op->aux_flags &
1091 RTE_CRYPTO_OP_AUX_FLAGS_IPSEC_SOFT_EXPIRY)) {
1092 printf("SA soft expiry (pkts) test failed\n");
1101 test_ipsec_stats_verify(struct rte_security_ctx *ctx,
1102 struct rte_security_session *sess,
1103 const struct ipsec_test_flags *flags,
1104 enum rte_security_ipsec_sa_direction dir)
1106 struct rte_security_stats stats = {0};
1107 int ret = TEST_SUCCESS;
1109 if (flags->stats_success) {
1110 if (rte_security_session_stats_get(ctx, sess, &stats) < 0)
1113 if (dir == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
1114 if (stats.ipsec.opackets != 1 ||
1115 stats.ipsec.oerrors != 0)
1118 if (stats.ipsec.ipackets != 1 ||
1119 stats.ipsec.ierrors != 0)
1128 test_ipsec_pkt_update(uint8_t *pkt, const struct ipsec_test_flags *flags)
1130 struct rte_ipv4_hdr *iph4;
1131 struct rte_ipv6_hdr *iph6;
1132 bool cksum_dirty = false;
1134 iph4 = (struct rte_ipv4_hdr *)pkt;
1136 if (flags->df == TEST_IPSEC_COPY_DF_INNER_1 ||
1137 flags->df == TEST_IPSEC_SET_DF_0_INNER_1 ||
1138 flags->df == TEST_IPSEC_COPY_DF_INNER_0 ||
1139 flags->df == TEST_IPSEC_SET_DF_1_INNER_0) {
1142 if (!is_ipv4(iph4)) {
1143 printf("Invalid packet type\n");
1147 frag_off = rte_be_to_cpu_16(iph4->fragment_offset);
1149 if (flags->df == TEST_IPSEC_COPY_DF_INNER_1 ||
1150 flags->df == TEST_IPSEC_SET_DF_0_INNER_1)
1151 frag_off |= RTE_IPV4_HDR_DF_FLAG;
1153 frag_off &= ~RTE_IPV4_HDR_DF_FLAG;
1155 iph4->fragment_offset = rte_cpu_to_be_16(frag_off);
1159 if (flags->dscp == TEST_IPSEC_COPY_DSCP_INNER_1 ||
1160 flags->dscp == TEST_IPSEC_SET_DSCP_0_INNER_1 ||
1161 flags->dscp == TEST_IPSEC_COPY_DSCP_INNER_0 ||
1162 flags->dscp == TEST_IPSEC_SET_DSCP_1_INNER_0) {
1164 if (is_ipv4(iph4)) {
1167 tos = iph4->type_of_service;
1168 if (flags->dscp == TEST_IPSEC_COPY_DSCP_INNER_1 ||
1169 flags->dscp == TEST_IPSEC_SET_DSCP_0_INNER_1)
1170 tos |= (RTE_IPV4_HDR_DSCP_MASK &
1171 (TEST_IPSEC_DSCP_VAL << 2));
1173 tos &= ~RTE_IPV4_HDR_DSCP_MASK;
1175 iph4->type_of_service = tos;
1180 iph6 = (struct rte_ipv6_hdr *)pkt;
1182 vtc_flow = rte_be_to_cpu_32(iph6->vtc_flow);
1183 if (flags->dscp == TEST_IPSEC_COPY_DSCP_INNER_1 ||
1184 flags->dscp == TEST_IPSEC_SET_DSCP_0_INNER_1)
1185 vtc_flow |= (RTE_IPV6_HDR_DSCP_MASK &
1186 (TEST_IPSEC_DSCP_VAL << (RTE_IPV6_HDR_TC_SHIFT + 2)));
1188 vtc_flow &= ~RTE_IPV6_HDR_DSCP_MASK;
1190 iph6->vtc_flow = rte_cpu_to_be_32(vtc_flow);
1194 if (cksum_dirty && is_ipv4(iph4)) {
1195 iph4->hdr_checksum = 0;
1196 iph4->hdr_checksum = rte_ipv4_cksum(iph4);
1202 #endif /* !RTE_EXEC_ENV_WINDOWS */