Paul Vinciguerra | a279d9c | 2019-02-28 09:00:09 -0800 | [diff] [blame] | 1 | from vpp_object import VppObject |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 2 | from ipaddress import ip_address |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 3 | from vpp_papi import VppEnum |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 4 | from vpp_interface import VppInterface |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 5 | |
| 6 | try: |
| 7 | text_type = unicode |
| 8 | except NameError: |
| 9 | text_type = str |
| 10 | |
| 11 | |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 12 | def mk_counter(): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 13 | return {"packets": 0, "bytes": 0} |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 14 | |
| 15 | |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 16 | class VppIpsecSpd(VppObject): |
| 17 | """ |
| 18 | VPP SPD DB |
| 19 | """ |
| 20 | |
| 21 | def __init__(self, test, id): |
| 22 | self.test = test |
| 23 | self.id = id |
| 24 | |
| 25 | def add_vpp_config(self): |
| 26 | self.test.vapi.ipsec_spd_add_del(self.id) |
| 27 | self.test.registry.register(self, self.test.logger) |
| 28 | |
| 29 | def remove_vpp_config(self): |
| 30 | self.test.vapi.ipsec_spd_add_del(self.id, is_add=0) |
| 31 | |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 32 | def object_id(self): |
| 33 | return "ipsec-spd-%d" % self.id |
| 34 | |
| 35 | def query_vpp_config(self): |
| 36 | spds = self.test.vapi.ipsec_spds_dump() |
| 37 | for spd in spds: |
| 38 | if spd.spd_id == self.id: |
| 39 | return True |
| 40 | return False |
| 41 | |
| 42 | |
| 43 | class VppIpsecSpdItfBinding(VppObject): |
| 44 | """ |
| 45 | VPP SPD DB to interface binding |
Paul Vinciguerra | 8feeaff | 2019-03-27 11:25:48 -0700 | [diff] [blame] | 46 | (i.e. this SPD is used on this interface) |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 47 | """ |
| 48 | |
| 49 | def __init__(self, test, spd, itf): |
| 50 | self.test = test |
| 51 | self.spd = spd |
| 52 | self.itf = itf |
| 53 | |
| 54 | def add_vpp_config(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 55 | self.test.vapi.ipsec_interface_add_del_spd(self.spd.id, self.itf.sw_if_index) |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 56 | self.test.registry.register(self, self.test.logger) |
| 57 | |
| 58 | def remove_vpp_config(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 59 | self.test.vapi.ipsec_interface_add_del_spd( |
| 60 | self.spd.id, self.itf.sw_if_index, is_add=0 |
| 61 | ) |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 62 | |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 63 | def object_id(self): |
| 64 | return "bind-%s-to-%s" % (self.spd.id, self.itf) |
| 65 | |
| 66 | def query_vpp_config(self): |
| 67 | bs = self.test.vapi.ipsec_spd_interface_dump() |
| 68 | for b in bs: |
| 69 | if b.sw_if_index == self.itf.sw_if_index: |
| 70 | return True |
| 71 | return False |
| 72 | |
| 73 | |
| 74 | class VppIpsecSpdEntry(VppObject): |
| 75 | """ |
| 76 | VPP SPD DB Entry |
| 77 | """ |
| 78 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 79 | def __init__( |
| 80 | self, |
| 81 | test, |
| 82 | spd, |
| 83 | sa_id, |
| 84 | local_start, |
| 85 | local_stop, |
| 86 | remote_start, |
| 87 | remote_stop, |
Piotr Bronowski | 815c6a4 | 2022-06-09 09:09:28 +0000 | [diff] [blame] | 88 | proto=socket.IPPROTO_RAW, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 89 | priority=100, |
| 90 | policy=None, |
| 91 | is_outbound=1, |
| 92 | remote_port_start=0, |
| 93 | remote_port_stop=65535, |
| 94 | local_port_start=0, |
| 95 | local_port_stop=65535, |
| 96 | ): |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 97 | self.test = test |
| 98 | self.spd = spd |
| 99 | self.sa_id = sa_id |
| 100 | self.local_start = ip_address(text_type(local_start)) |
| 101 | self.local_stop = ip_address(text_type(local_stop)) |
| 102 | self.remote_start = ip_address(text_type(remote_start)) |
| 103 | self.remote_stop = ip_address(text_type(remote_stop)) |
| 104 | self.proto = proto |
| 105 | self.is_outbound = is_outbound |
| 106 | self.priority = priority |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 107 | if not policy: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 108 | self.policy = VppEnum.vl_api_ipsec_spd_action_t.IPSEC_API_SPD_ACTION_BYPASS |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 109 | else: |
| 110 | self.policy = policy |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 111 | self.is_ipv6 = 0 if self.local_start.version == 4 else 1 |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 112 | self.local_port_start = local_port_start |
| 113 | self.local_port_stop = local_port_stop |
| 114 | self.remote_port_start = remote_port_start |
| 115 | self.remote_port_stop = remote_port_stop |
| 116 | |
| 117 | def add_vpp_config(self): |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 118 | rv = self.test.vapi.ipsec_spd_entry_add_del( |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 119 | self.spd.id, |
| 120 | self.sa_id, |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 121 | self.local_start, |
| 122 | self.local_stop, |
| 123 | self.remote_start, |
| 124 | self.remote_stop, |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 125 | protocol=self.proto, |
| 126 | is_ipv6=self.is_ipv6, |
| 127 | is_outbound=self.is_outbound, |
| 128 | priority=self.priority, |
| 129 | policy=self.policy, |
| 130 | local_port_start=self.local_port_start, |
| 131 | local_port_stop=self.local_port_stop, |
| 132 | remote_port_start=self.remote_port_start, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 133 | remote_port_stop=self.remote_port_stop, |
| 134 | ) |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 135 | self.stat_index = rv.stat_index |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 136 | self.test.registry.register(self, self.test.logger) |
Neale Ranns | fc81134 | 2021-02-26 10:35:33 +0000 | [diff] [blame] | 137 | return self |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 138 | |
| 139 | def remove_vpp_config(self): |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 140 | self.test.vapi.ipsec_spd_entry_add_del( |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 141 | self.spd.id, |
| 142 | self.sa_id, |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 143 | self.local_start, |
| 144 | self.local_stop, |
| 145 | self.remote_start, |
| 146 | self.remote_stop, |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 147 | protocol=self.proto, |
| 148 | is_ipv6=self.is_ipv6, |
| 149 | is_outbound=self.is_outbound, |
| 150 | priority=self.priority, |
| 151 | policy=self.policy, |
| 152 | local_port_start=self.local_port_start, |
| 153 | local_port_stop=self.local_port_stop, |
| 154 | remote_port_start=self.remote_port_start, |
| 155 | remote_port_stop=self.remote_port_stop, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 156 | is_add=0, |
| 157 | ) |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 158 | |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 159 | def object_id(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 160 | return "spd-entry-%d-%d-%d-%d-%d-%d" % ( |
| 161 | self.spd.id, |
| 162 | self.priority, |
| 163 | self.policy, |
| 164 | self.is_outbound, |
| 165 | self.is_ipv6, |
| 166 | self.remote_port_start, |
| 167 | ) |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 168 | |
| 169 | def query_vpp_config(self): |
| 170 | ss = self.test.vapi.ipsec_spd_dump(self.spd.id) |
| 171 | for s in ss: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 172 | if ( |
| 173 | s.entry.sa_id == self.sa_id |
| 174 | and s.entry.is_outbound == self.is_outbound |
| 175 | and s.entry.priority == self.priority |
| 176 | and s.entry.policy == self.policy |
| 177 | and s.entry.remote_address_start == self.remote_start |
| 178 | and s.entry.remote_port_start == self.remote_port_start |
| 179 | ): |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 180 | return True |
| 181 | return False |
| 182 | |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 183 | def get_stats(self, worker=None): |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 184 | c = self.test.statistics.get_counter("/net/ipsec/policy") |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 185 | if worker is None: |
| 186 | total = mk_counter() |
| 187 | for t in c: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 188 | total["packets"] += t[self.stat_index]["packets"] |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 189 | return total |
| 190 | else: |
| 191 | # +1 to skip main thread |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 192 | return c[worker + 1][self.stat_index] |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 193 | |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 194 | |
| 195 | class VppIpsecSA(VppObject): |
| 196 | """ |
| 197 | VPP SAD Entry |
| 198 | """ |
| 199 | |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 200 | DEFAULT_UDP_PORT = 4500 |
| 201 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 202 | def __init__( |
| 203 | self, |
| 204 | test, |
| 205 | id, |
| 206 | spi, |
| 207 | integ_alg, |
| 208 | integ_key, |
| 209 | crypto_alg, |
| 210 | crypto_key, |
| 211 | proto, |
| 212 | tun_src=None, |
| 213 | tun_dst=None, |
| 214 | flags=None, |
| 215 | salt=0, |
| 216 | tun_flags=None, |
| 217 | dscp=None, |
| 218 | udp_src=None, |
| 219 | udp_dst=None, |
| 220 | hop_limit=None, |
| 221 | ): |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 222 | e = VppEnum.vl_api_ipsec_sad_flags_t |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 223 | self.test = test |
| 224 | self.id = id |
| 225 | self.spi = spi |
| 226 | self.integ_alg = integ_alg |
| 227 | self.integ_key = integ_key |
| 228 | self.crypto_alg = crypto_alg |
| 229 | self.crypto_key = crypto_key |
| 230 | self.proto = proto |
Neale Ranns | 80f6fd5 | 2019-04-16 02:41:34 +0000 | [diff] [blame] | 231 | self.salt = salt |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 232 | |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 233 | self.table_id = 0 |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 234 | self.tun_src = tun_src |
| 235 | self.tun_dst = tun_dst |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 236 | if not flags: |
| 237 | self.flags = e.IPSEC_API_SAD_FLAG_NONE |
| 238 | else: |
| 239 | self.flags = flags |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 240 | if tun_src: |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 241 | self.tun_src = ip_address(text_type(tun_src)) |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 242 | self.flags = self.flags | e.IPSEC_API_SAD_FLAG_IS_TUNNEL |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 243 | if tun_dst: |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 244 | self.tun_dst = ip_address(text_type(tun_dst)) |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 245 | self.udp_src = udp_src |
| 246 | self.udp_dst = udp_dst |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 247 | self.tun_flags = ( |
| 248 | VppEnum.vl_api_tunnel_encap_decap_flags_t.TUNNEL_API_ENCAP_DECAP_FLAG_NONE |
| 249 | ) |
Neale Ranns | 041add7 | 2020-01-02 04:06:10 +0000 | [diff] [blame] | 250 | if tun_flags: |
| 251 | self.tun_flags = tun_flags |
| 252 | self.dscp = VppEnum.vl_api_ip_dscp_t.IP_API_DSCP_CS0 |
| 253 | if dscp: |
| 254 | self.dscp = dscp |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 255 | self.hop_limit = 255 |
| 256 | if hop_limit: |
| 257 | self.hop_limit = hop_limit |
| 258 | |
| 259 | def tunnel_encode(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 260 | return { |
| 261 | "src": (self.tun_src if self.tun_src else []), |
| 262 | "dst": (self.tun_dst if self.tun_dst else []), |
| 263 | "encap_decap_flags": self.tun_flags, |
| 264 | "dscp": self.dscp, |
| 265 | "hop_limit": self.hop_limit, |
| 266 | "table_id": self.table_id, |
| 267 | } |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 268 | |
| 269 | def add_vpp_config(self): |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 270 | entry = { |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 271 | "sad_id": self.id, |
| 272 | "spi": self.spi, |
| 273 | "integrity_algorithm": self.integ_alg, |
| 274 | "integrity_key": { |
| 275 | "length": len(self.integ_key), |
| 276 | "data": self.integ_key, |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 277 | }, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 278 | "crypto_algorithm": self.crypto_alg, |
| 279 | "crypto_key": { |
| 280 | "data": self.crypto_key, |
| 281 | "length": len(self.crypto_key), |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 282 | }, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 283 | "protocol": self.proto, |
| 284 | "tunnel": self.tunnel_encode(), |
| 285 | "flags": self.flags, |
| 286 | "salt": self.salt, |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 287 | } |
| 288 | # don't explicitly send the defaults, let papi fill them in |
| 289 | if self.udp_src: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 290 | entry["udp_src_port"] = self.udp_src |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 291 | if self.udp_dst: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 292 | entry["udp_dst_port"] = self.udp_dst |
Neale Ranns | ff2e413 | 2021-06-24 14:57:56 +0000 | [diff] [blame] | 293 | r = self.test.vapi.ipsec_sad_entry_add(entry=entry) |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 294 | self.stat_index = r.stat_index |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 295 | self.test.registry.register(self, self.test.logger) |
Neale Ranns | a9e2774 | 2020-12-23 16:22:28 +0000 | [diff] [blame] | 296 | return self |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 297 | |
| 298 | def remove_vpp_config(self): |
Neale Ranns | ff2e413 | 2021-06-24 14:57:56 +0000 | [diff] [blame] | 299 | self.test.vapi.ipsec_sad_entry_del(id=self.id) |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 300 | |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 301 | def object_id(self): |
| 302 | return "ipsec-sa-%d" % self.id |
| 303 | |
| 304 | def query_vpp_config(self): |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 305 | e = VppEnum.vl_api_ipsec_sad_flags_t |
| 306 | |
Neale Ranns | 9ec846c | 2021-02-09 14:04:02 +0000 | [diff] [blame] | 307 | bs = self.test.vapi.ipsec_sa_v3_dump() |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 308 | for b in bs: |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 309 | if b.entry.sad_id == self.id: |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 310 | # if udp encap is configured then the ports should match |
| 311 | # those configured or the default |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 312 | if self.flags & e.IPSEC_API_SAD_FLAG_UDP_ENCAP: |
Neale Ranns | abc5660 | 2020-04-01 09:45:23 +0000 | [diff] [blame] | 313 | if not b.entry.flags & e.IPSEC_API_SAD_FLAG_UDP_ENCAP: |
| 314 | return False |
| 315 | if self.udp_src: |
| 316 | if self.udp_src != b.entry.udp_src_port: |
| 317 | return False |
| 318 | else: |
| 319 | if self.DEFAULT_UDP_PORT != b.entry.udp_src_port: |
| 320 | return False |
| 321 | if self.udp_dst: |
| 322 | if self.udp_dst != b.entry.udp_dst_port: |
| 323 | return False |
| 324 | else: |
| 325 | if self.DEFAULT_UDP_PORT != b.entry.udp_dst_port: |
| 326 | return False |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 327 | return True |
| 328 | return False |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 329 | |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 330 | def get_stats(self, worker=None): |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 331 | c = self.test.statistics.get_counter("/net/ipsec/sa") |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 332 | if worker is None: |
| 333 | total = mk_counter() |
| 334 | for t in c: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 335 | total["packets"] += t[self.stat_index]["packets"] |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame] | 336 | return total |
| 337 | else: |
| 338 | # +1 to skip main thread |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 339 | return c[worker + 1][self.stat_index] |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 340 | |
Neale Ranns | e11203e | 2021-09-21 12:34:19 +0000 | [diff] [blame] | 341 | def get_lost(self, worker=None): |
| 342 | c = self.test.statistics.get_counter("/net/ipsec/sa/lost") |
| 343 | if worker is None: |
| 344 | total = 0 |
| 345 | for t in c: |
| 346 | total += t[self.stat_index] |
| 347 | return total |
| 348 | else: |
| 349 | # +1 to skip main thread |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 350 | return c[worker + 1][self.stat_index] |
Neale Ranns | e11203e | 2021-09-21 12:34:19 +0000 | [diff] [blame] | 351 | |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 352 | |
| 353 | class VppIpsecTunProtect(VppObject): |
| 354 | """ |
| 355 | VPP IPSEC tunnel protection |
| 356 | """ |
| 357 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 358 | def __init__(self, test, itf, sa_out, sas_in, nh=None): |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 359 | self.test = test |
| 360 | self.itf = itf |
| 361 | self.sas_in = [] |
| 362 | for sa in sas_in: |
| 363 | self.sas_in.append(sa.id) |
| 364 | self.sa_out = sa_out.id |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 365 | self.nh = nh |
| 366 | if not self.nh: |
| 367 | self.nh = "0.0.0.0" |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 368 | |
| 369 | def update_vpp_config(self, sa_out, sas_in): |
| 370 | self.sas_in = [] |
| 371 | for sa in sas_in: |
| 372 | self.sas_in.append(sa.id) |
| 373 | self.sa_out = sa_out.id |
| 374 | self.test.vapi.ipsec_tunnel_protect_update( |
| 375 | tunnel={ |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 376 | "sw_if_index": self.itf._sw_if_index, |
| 377 | "n_sa_in": len(self.sas_in), |
| 378 | "sa_out": self.sa_out, |
| 379 | "sa_in": self.sas_in, |
| 380 | "nh": self.nh, |
| 381 | } |
| 382 | ) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 383 | |
| 384 | def object_id(self): |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 385 | return "ipsec-tun-protect-%s-%s" % (self.itf, self.nh) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 386 | |
| 387 | def add_vpp_config(self): |
| 388 | self.test.vapi.ipsec_tunnel_protect_update( |
| 389 | tunnel={ |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 390 | "sw_if_index": self.itf._sw_if_index, |
| 391 | "n_sa_in": len(self.sas_in), |
| 392 | "sa_out": self.sa_out, |
| 393 | "sa_in": self.sas_in, |
| 394 | "nh": self.nh, |
| 395 | } |
| 396 | ) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 397 | self.test.registry.register(self, self.test.logger) |
| 398 | |
| 399 | def remove_vpp_config(self): |
| 400 | self.test.vapi.ipsec_tunnel_protect_del( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 401 | sw_if_index=self.itf.sw_if_index, nh=self.nh |
| 402 | ) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 403 | |
| 404 | def query_vpp_config(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 405 | bs = self.test.vapi.ipsec_tunnel_protect_dump(sw_if_index=self.itf.sw_if_index) |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 406 | for b in bs: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 407 | if b.tun.sw_if_index == self.itf.sw_if_index and self.nh == str(b.tun.nh): |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 408 | return True |
| 409 | return False |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 410 | |
| 411 | |
| 412 | class VppIpsecInterface(VppInterface): |
| 413 | """ |
| 414 | VPP IPSec interface |
| 415 | """ |
| 416 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 417 | def __init__(self, test, mode=None, instance=0xFFFFFFFF): |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 418 | super(VppIpsecInterface, self).__init__(test) |
| 419 | |
Neale Ranns | 6ba4e41 | 2020-10-19 09:59:41 +0000 | [diff] [blame] | 420 | self.mode = mode |
| 421 | if not self.mode: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 422 | self.mode = VppEnum.vl_api_tunnel_mode_t.TUNNEL_API_MODE_P2P |
Eric Kinzie | 609d579 | 2020-10-13 20:02:11 -0400 | [diff] [blame] | 423 | self.instance = instance |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 424 | |
| 425 | def add_vpp_config(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 426 | r = self.test.vapi.ipsec_itf_create( |
| 427 | itf={ |
| 428 | "user_instance": self.instance, |
| 429 | "mode": self.mode, |
| 430 | } |
| 431 | ) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 432 | self.set_sw_if_index(r.sw_if_index) |
| 433 | self.test.registry.register(self, self.test.logger) |
Neale Ranns | 89d939e | 2021-06-07 09:34:07 +0000 | [diff] [blame] | 434 | ts = self.test.vapi.ipsec_itf_dump(sw_if_index=self._sw_if_index) |
| 435 | self.instance = ts[0].itf.user_instance |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 436 | return self |
| 437 | |
| 438 | def remove_vpp_config(self): |
| 439 | self.test.vapi.ipsec_itf_delete(sw_if_index=self._sw_if_index) |
| 440 | |
| 441 | def query_vpp_config(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 442 | ts = self.test.vapi.ipsec_itf_dump(sw_if_index=0xFFFFFFFF) |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 443 | for t in ts: |
Neale Ranns | 89d939e | 2021-06-07 09:34:07 +0000 | [diff] [blame] | 444 | if t.itf.sw_if_index == self._sw_if_index: |
Neale Ranns | dd4ccf2 | 2020-06-30 07:47:14 +0000 | [diff] [blame] | 445 | return True |
| 446 | return False |
| 447 | |
| 448 | def __str__(self): |
| 449 | return self.object_id() |
| 450 | |
| 451 | def object_id(self): |
Neale Ranns | 89d939e | 2021-06-07 09:34:07 +0000 | [diff] [blame] | 452 | return "ipsec%d" % self.instance |