blob: 7208d2887b522b210cfca16b0e4341863cc893c2 [file] [log] [blame]
Klement Sekerab4d30532018-11-08 13:00:02 +01001import unittest
2
Dave Wallace8800f732023-08-31 00:47:44 -04003from framework import VppTestCase
4from asfframework import VppTestRunner
5from template_ipsec import IPsecIPv4Params
Neale Ranns17dcec02019-01-09 21:22:20 -08006from vpp_papi import VppEnum
Klement Sekerab4d30532018-11-08 13:00:02 +01007
Maxime Peim1271e3a2023-03-20 14:13:56 +00008from vpp_ipsec import VppIpsecSA
9
Klement Sekerab4d30532018-11-08 13:00:02 +010010
11class IpsecApiTestCase(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020012 """IPSec API tests"""
Klement Sekerab4d30532018-11-08 13:00:02 +010013
Maxime Peim1271e3a2023-03-20 14:13:56 +000014 vpp_worker_count = 2
15
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -070016 @classmethod
17 def setUpClass(cls):
18 super(IpsecApiTestCase, cls).setUpClass()
19
20 @classmethod
21 def tearDownClass(cls):
22 super(IpsecApiTestCase, cls).tearDownClass()
23
Neale Ranns8e4a89b2019-01-23 08:16:17 -080024 def setUp(self):
25 super(IpsecApiTestCase, self).setUp()
26 self.create_pg_interfaces([0])
27 self.pg0.config_ip4()
28 self.pg0.admin_up()
29
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020030 self.vpp_esp_protocol = VppEnum.vl_api_ipsec_proto_t.IPSEC_API_PROTO_ESP
31 self.vpp_ah_protocol = VppEnum.vl_api_ipsec_proto_t.IPSEC_API_PROTO_AH
Neale Ranns8e4a89b2019-01-23 08:16:17 -080032 self.ipv4_params = IPsecIPv4Params()
33
34 def tearDown(self):
35 self.pg0.unconfig_ip4()
36 self.pg0.admin_down()
37 super(IpsecApiTestCase, self).tearDown()
Klement Sekerab4d30532018-11-08 13:00:02 +010038
39 def test_backend_dump(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020040 """backend dump"""
Klement Sekerab4d30532018-11-08 13:00:02 +010041 d = self.vapi.ipsec_backend_dump()
42 self.assert_equal(len(d), 2, "number of ipsec backends in dump")
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020043 self.assert_equal(
44 d[0].protocol, self.vpp_ah_protocol, "ipsec protocol in dump entry"
45 )
Klement Sekerab4d30532018-11-08 13:00:02 +010046 self.assert_equal(d[0].index, 0, "index in dump entry")
47 self.assert_equal(d[0].active, 1, "active flag in dump entry")
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020048 self.assert_equal(
49 d[1].protocol, self.vpp_esp_protocol, "ipsec protocol in dump entry"
50 )
Klement Sekerab4d30532018-11-08 13:00:02 +010051 self.assert_equal(d[1].index, 0, "index in dump entry")
52 self.assert_equal(d[1].active, 1, "active flag in dump entry")
53
54 def test_select_valid_backend(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020055 """select valid backend"""
Neale Ranns8e4a89b2019-01-23 08:16:17 -080056 self.vapi.ipsec_select_backend(self.vpp_ah_protocol, 0)
57 self.vapi.ipsec_select_backend(self.vpp_esp_protocol, 0)
Klement Sekerab4d30532018-11-08 13:00:02 +010058
59 def test_select_invalid_backend(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020060 """select invalid backend"""
Klement Sekerab4d30532018-11-08 13:00:02 +010061 with self.vapi.assert_negative_api_retval():
Neale Ranns8e4a89b2019-01-23 08:16:17 -080062 self.vapi.ipsec_select_backend(self.vpp_ah_protocol, 200)
Klement Sekerab4d30532018-11-08 13:00:02 +010063 with self.vapi.assert_negative_api_retval():
Neale Ranns8e4a89b2019-01-23 08:16:17 -080064 self.vapi.ipsec_select_backend(self.vpp_esp_protocol, 200)
Klement Sekerab4d30532018-11-08 13:00:02 +010065
66 def test_select_backend_in_use(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020067 """attempt to change backend while sad configured"""
Neale Ranns8e4a89b2019-01-23 08:16:17 -080068 params = self.ipv4_params
Klement Sekerab4d30532018-11-08 13:00:02 +010069 addr_type = params.addr_type
70 is_ipv6 = params.is_ipv6
71 scapy_tun_sa_id = params.scapy_tun_sa_id
72 scapy_tun_spi = params.scapy_tun_spi
73 auth_algo_vpp_id = params.auth_algo_vpp_id
74 auth_key = params.auth_key
75 crypt_algo_vpp_id = params.crypt_algo_vpp_id
76 crypt_key = params.crypt_key
77
Neale Rannsabc56602020-04-01 09:45:23 +000078 self.vapi.ipsec_sad_entry_add_del(
79 is_add=1,
80 entry={
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020081 "sad_id": scapy_tun_sa_id,
82 "spi": scapy_tun_spi,
83 "integrity_algorithm": auth_algo_vpp_id,
84 "integrity_key": {
85 "data": auth_key,
86 "length": len(auth_key),
Neale Rannsabc56602020-04-01 09:45:23 +000087 },
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020088 "crypto_algorithm": crypt_algo_vpp_id,
89 "crypto_key": {
90 "data": crypt_key,
91 "length": len(crypt_key),
Neale Rannsabc56602020-04-01 09:45:23 +000092 },
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020093 "protocol": self.vpp_ah_protocol,
94 "tunnel_src": self.pg0.local_addr[addr_type],
95 "tunnel_dst": self.pg0.remote_addr[addr_type],
96 },
97 )
Klement Sekerab4d30532018-11-08 13:00:02 +010098 with self.vapi.assert_negative_api_retval():
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020099 self.vapi.ipsec_select_backend(protocol=self.vpp_ah_protocol, index=0)
Klement Sekerab4d30532018-11-08 13:00:02 +0100100
Neale Rannsabc56602020-04-01 09:45:23 +0000101 self.vapi.ipsec_sad_entry_add_del(
102 is_add=0,
103 entry={
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200104 "sad_id": scapy_tun_sa_id,
105 "spi": scapy_tun_spi,
106 "integrity_algorithm": auth_algo_vpp_id,
107 "integrity_key": {
108 "data": auth_key,
109 "length": len(auth_key),
Neale Rannsabc56602020-04-01 09:45:23 +0000110 },
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200111 "crypto_algorithm": crypt_algo_vpp_id,
112 "crypto_key": {
113 "data": crypt_key,
114 "length": len(crypt_key),
Neale Rannsabc56602020-04-01 09:45:23 +0000115 },
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200116 "protocol": self.vpp_ah_protocol,
117 "tunnel_src": self.pg0.local_addr[addr_type],
118 "tunnel_dst": self.pg0.remote_addr[addr_type],
119 },
120 )
121 self.vapi.ipsec_select_backend(protocol=self.vpp_ah_protocol, index=0)
Klement Sekerab4d30532018-11-08 13:00:02 +0100122
Maxime Peim1271e3a2023-03-20 14:13:56 +0000123 def __check_sa_binding(self, sa_id, thread_index):
124 found_sa = False
Maxime Peim0e2f1882022-12-22 11:26:57 +0000125 sa_dumps = self.vapi.ipsec_sa_v5_dump()
Maxime Peim1271e3a2023-03-20 14:13:56 +0000126 for dump in sa_dumps:
127 if dump.entry.sad_id == sa_id:
128 self.assertEqual(dump.thread_index, thread_index)
129 found_sa = True
130 break
131
132 if not found_sa:
133 self.fail("SA not found in VPP")
134
135 def test_sa_worker_bind(self):
136 """Bind an SA to a worker"""
137 sa = VppIpsecSA(
138 self,
139 self.ipv4_params.scapy_tun_sa_id,
140 self.ipv4_params.scapy_tun_spi,
141 self.ipv4_params.auth_algo_vpp_id,
142 self.ipv4_params.auth_key,
143 self.ipv4_params.crypt_algo_vpp_id,
144 self.ipv4_params.crypt_key,
145 VppEnum.vl_api_ipsec_proto_t.IPSEC_API_PROTO_ESP,
146 )
147 sa.add_vpp_config()
148
149 self.__check_sa_binding(sa.id, 0xFFFF)
150
151 self.vapi.ipsec_sad_bind(sa_id=sa.id, worker=1)
152
153 self.__check_sa_binding(sa.id, 2)
154
155 sa.remove_vpp_config()
156
Klement Sekerab4d30532018-11-08 13:00:02 +0100157
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200158if __name__ == "__main__":
Klement Sekerab4d30532018-11-08 13:00:02 +0100159 unittest.main(testRunner=VppTestRunner)