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 | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 4 | |
| 5 | try: |
| 6 | text_type = unicode |
| 7 | except NameError: |
| 8 | text_type = str |
| 9 | |
| 10 | |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame^] | 11 | def mk_counter(): |
| 12 | return {'packets': 0, 'bytes': 0} |
| 13 | |
| 14 | |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 15 | class VppIpsecSpd(VppObject): |
| 16 | """ |
| 17 | VPP SPD DB |
| 18 | """ |
| 19 | |
| 20 | def __init__(self, test, id): |
| 21 | self.test = test |
| 22 | self.id = id |
| 23 | |
| 24 | def add_vpp_config(self): |
| 25 | self.test.vapi.ipsec_spd_add_del(self.id) |
| 26 | self.test.registry.register(self, self.test.logger) |
| 27 | |
| 28 | def remove_vpp_config(self): |
| 29 | self.test.vapi.ipsec_spd_add_del(self.id, is_add=0) |
| 30 | |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 31 | def object_id(self): |
| 32 | return "ipsec-spd-%d" % self.id |
| 33 | |
| 34 | def query_vpp_config(self): |
| 35 | spds = self.test.vapi.ipsec_spds_dump() |
| 36 | for spd in spds: |
| 37 | if spd.spd_id == self.id: |
| 38 | return True |
| 39 | return False |
| 40 | |
| 41 | |
| 42 | class VppIpsecSpdItfBinding(VppObject): |
| 43 | """ |
| 44 | VPP SPD DB to interface binding |
Paul Vinciguerra | 8feeaff | 2019-03-27 11:25:48 -0700 | [diff] [blame] | 45 | (i.e. this SPD is used on this interface) |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 46 | """ |
| 47 | |
| 48 | def __init__(self, test, spd, itf): |
| 49 | self.test = test |
| 50 | self.spd = spd |
| 51 | self.itf = itf |
| 52 | |
| 53 | def add_vpp_config(self): |
| 54 | self.test.vapi.ipsec_interface_add_del_spd(self.spd.id, |
| 55 | self.itf.sw_if_index) |
| 56 | self.test.registry.register(self, self.test.logger) |
| 57 | |
| 58 | def remove_vpp_config(self): |
| 59 | self.test.vapi.ipsec_interface_add_del_spd(self.spd.id, |
| 60 | self.itf.sw_if_index, |
| 61 | is_add=0) |
| 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 | |
| 79 | def __init__(self, test, spd, sa_id, |
| 80 | local_start, local_stop, |
| 81 | remote_start, remote_stop, |
| 82 | proto, |
| 83 | priority=100, |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 84 | policy=None, |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 85 | is_outbound=1, |
| 86 | remote_port_start=0, |
| 87 | remote_port_stop=65535, |
| 88 | local_port_start=0, |
| 89 | local_port_stop=65535): |
| 90 | self.test = test |
| 91 | self.spd = spd |
| 92 | self.sa_id = sa_id |
| 93 | self.local_start = ip_address(text_type(local_start)) |
| 94 | self.local_stop = ip_address(text_type(local_stop)) |
| 95 | self.remote_start = ip_address(text_type(remote_start)) |
| 96 | self.remote_stop = ip_address(text_type(remote_stop)) |
| 97 | self.proto = proto |
| 98 | self.is_outbound = is_outbound |
| 99 | self.priority = priority |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 100 | if not policy: |
| 101 | self.policy = (VppEnum.vl_api_ipsec_spd_action_t. |
| 102 | IPSEC_API_SPD_ACTION_BYPASS) |
| 103 | else: |
| 104 | self.policy = policy |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 105 | self.is_ipv6 = (0 if self.local_start.version == 4 else 1) |
| 106 | self.local_port_start = local_port_start |
| 107 | self.local_port_stop = local_port_stop |
| 108 | self.remote_port_start = remote_port_start |
| 109 | self.remote_port_stop = remote_port_stop |
| 110 | |
| 111 | def add_vpp_config(self): |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 112 | rv = self.test.vapi.ipsec_spd_entry_add_del( |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 113 | self.spd.id, |
| 114 | self.sa_id, |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 115 | self.local_start, |
| 116 | self.local_stop, |
| 117 | self.remote_start, |
| 118 | self.remote_stop, |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 119 | protocol=self.proto, |
| 120 | is_ipv6=self.is_ipv6, |
| 121 | is_outbound=self.is_outbound, |
| 122 | priority=self.priority, |
| 123 | policy=self.policy, |
| 124 | local_port_start=self.local_port_start, |
| 125 | local_port_stop=self.local_port_stop, |
| 126 | remote_port_start=self.remote_port_start, |
| 127 | remote_port_stop=self.remote_port_stop) |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 128 | self.stat_index = rv.stat_index |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 129 | self.test.registry.register(self, self.test.logger) |
| 130 | |
| 131 | def remove_vpp_config(self): |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 132 | self.test.vapi.ipsec_spd_entry_add_del( |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 133 | self.spd.id, |
| 134 | self.sa_id, |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 135 | self.local_start, |
| 136 | self.local_stop, |
| 137 | self.remote_start, |
| 138 | self.remote_stop, |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 139 | protocol=self.proto, |
| 140 | is_ipv6=self.is_ipv6, |
| 141 | is_outbound=self.is_outbound, |
| 142 | priority=self.priority, |
| 143 | policy=self.policy, |
| 144 | local_port_start=self.local_port_start, |
| 145 | local_port_stop=self.local_port_stop, |
| 146 | remote_port_start=self.remote_port_start, |
| 147 | remote_port_stop=self.remote_port_stop, |
| 148 | is_add=0) |
| 149 | |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 150 | def object_id(self): |
| 151 | return "spd-entry-%d-%d-%d-%d-%d-%d" % (self.spd.id, |
| 152 | self.priority, |
| 153 | self.policy, |
| 154 | self.is_outbound, |
| 155 | self.is_ipv6, |
| 156 | self.remote_port_start) |
| 157 | |
| 158 | def query_vpp_config(self): |
| 159 | ss = self.test.vapi.ipsec_spd_dump(self.spd.id) |
| 160 | for s in ss: |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 161 | if s.entry.sa_id == self.sa_id and \ |
| 162 | s.entry.is_outbound == self.is_outbound and \ |
| 163 | s.entry.priority == self.priority and \ |
| 164 | s.entry.policy == self.policy and \ |
| 165 | s.entry.remote_address_start == self.remote_start and \ |
| 166 | s.entry.remote_port_start == self.remote_port_start: |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 167 | return True |
| 168 | return False |
| 169 | |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame^] | 170 | def get_stats(self, worker=None): |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 171 | c = self.test.statistics.get_counter("/net/ipsec/policy") |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame^] | 172 | if worker is None: |
| 173 | total = mk_counter() |
| 174 | for t in c: |
| 175 | total['packets'] += t[self.stat_index]['packets'] |
| 176 | return total |
| 177 | else: |
| 178 | # +1 to skip main thread |
| 179 | return c[worker+1][self.stat_index] |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 180 | |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 181 | |
| 182 | class VppIpsecSA(VppObject): |
| 183 | """ |
| 184 | VPP SAD Entry |
| 185 | """ |
| 186 | |
| 187 | def __init__(self, test, id, spi, |
| 188 | integ_alg, integ_key, |
| 189 | crypto_alg, crypto_key, |
| 190 | proto, |
| 191 | tun_src=None, tun_dst=None, |
Neale Ranns | 80f6fd5 | 2019-04-16 02:41:34 +0000 | [diff] [blame] | 192 | flags=None, salt=0): |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 193 | e = VppEnum.vl_api_ipsec_sad_flags_t |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 194 | self.test = test |
| 195 | self.id = id |
| 196 | self.spi = spi |
| 197 | self.integ_alg = integ_alg |
| 198 | self.integ_key = integ_key |
| 199 | self.crypto_alg = crypto_alg |
| 200 | self.crypto_key = crypto_key |
| 201 | self.proto = proto |
Neale Ranns | 80f6fd5 | 2019-04-16 02:41:34 +0000 | [diff] [blame] | 202 | self.salt = salt |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 203 | |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 204 | self.tun_src = tun_src |
| 205 | self.tun_dst = tun_dst |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 206 | if not flags: |
| 207 | self.flags = e.IPSEC_API_SAD_FLAG_NONE |
| 208 | else: |
| 209 | self.flags = flags |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 210 | if (tun_src): |
| 211 | self.tun_src = ip_address(text_type(tun_src)) |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 212 | self.flags = self.flags | e.IPSEC_API_SAD_FLAG_IS_TUNNEL |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 213 | if (self.tun_src.version == 6): |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 214 | self.flags = self.flags | e.IPSEC_API_SAD_FLAG_IS_TUNNEL_V6 |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 215 | if (tun_dst): |
| 216 | self.tun_dst = ip_address(text_type(tun_dst)) |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 217 | |
| 218 | def add_vpp_config(self): |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 219 | r = self.test.vapi.ipsec_sad_entry_add_del( |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 220 | self.id, |
| 221 | self.spi, |
| 222 | self.integ_alg, |
| 223 | self.integ_key, |
| 224 | self.crypto_alg, |
| 225 | self.crypto_key, |
| 226 | self.proto, |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 227 | (self.tun_src if self.tun_src else []), |
| 228 | (self.tun_dst if self.tun_dst else []), |
Neale Ranns | 80f6fd5 | 2019-04-16 02:41:34 +0000 | [diff] [blame] | 229 | flags=self.flags, |
| 230 | salt=self.salt) |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 231 | self.stat_index = r.stat_index |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 232 | self.test.registry.register(self, self.test.logger) |
| 233 | |
| 234 | def remove_vpp_config(self): |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 235 | self.test.vapi.ipsec_sad_entry_add_del( |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 236 | self.id, |
| 237 | self.spi, |
| 238 | self.integ_alg, |
| 239 | self.integ_key, |
| 240 | self.crypto_alg, |
| 241 | self.crypto_key, |
| 242 | self.proto, |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 243 | (self.tun_src if self.tun_src else []), |
| 244 | (self.tun_dst if self.tun_dst else []), |
| 245 | flags=self.flags, |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 246 | is_add=0) |
| 247 | |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 248 | def object_id(self): |
| 249 | return "ipsec-sa-%d" % self.id |
| 250 | |
| 251 | def query_vpp_config(self): |
| 252 | bs = self.test.vapi.ipsec_sa_dump() |
| 253 | for b in bs: |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 254 | if b.entry.sad_id == self.id: |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 255 | return True |
| 256 | return False |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 257 | |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame^] | 258 | def get_stats(self, worker=None): |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 259 | c = self.test.statistics.get_counter("/net/ipsec/sa") |
Neale Ranns | 4a56f4e | 2019-12-23 04:10:25 +0000 | [diff] [blame^] | 260 | if worker is None: |
| 261 | total = mk_counter() |
| 262 | for t in c: |
| 263 | total['packets'] += t[self.stat_index]['packets'] |
| 264 | return total |
| 265 | else: |
| 266 | # +1 to skip main thread |
| 267 | return c[worker+1][self.stat_index] |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 268 | |
| 269 | |
| 270 | class VppIpsecTunProtect(VppObject): |
| 271 | """ |
| 272 | VPP IPSEC tunnel protection |
| 273 | """ |
| 274 | |
| 275 | def __init__(self, test, itf, sa_out, sas_in): |
| 276 | self.test = test |
| 277 | self.itf = itf |
| 278 | self.sas_in = [] |
| 279 | for sa in sas_in: |
| 280 | self.sas_in.append(sa.id) |
| 281 | self.sa_out = sa_out.id |
| 282 | |
| 283 | def update_vpp_config(self, sa_out, sas_in): |
| 284 | self.sas_in = [] |
| 285 | for sa in sas_in: |
| 286 | self.sas_in.append(sa.id) |
| 287 | self.sa_out = sa_out.id |
| 288 | self.test.vapi.ipsec_tunnel_protect_update( |
| 289 | tunnel={ |
| 290 | 'sw_if_index': self.itf._sw_if_index, |
| 291 | 'n_sa_in': len(self.sas_in), |
| 292 | 'sa_out': self.sa_out, |
| 293 | 'sa_in': self.sas_in}) |
| 294 | |
| 295 | def object_id(self): |
| 296 | return "ipsec-tun-protect-%s" % self.itf |
| 297 | |
| 298 | def add_vpp_config(self): |
| 299 | self.test.vapi.ipsec_tunnel_protect_update( |
| 300 | tunnel={ |
| 301 | 'sw_if_index': self.itf._sw_if_index, |
| 302 | 'n_sa_in': len(self.sas_in), |
| 303 | 'sa_out': self.sa_out, |
| 304 | 'sa_in': self.sas_in}) |
| 305 | self.test.registry.register(self, self.test.logger) |
| 306 | |
| 307 | def remove_vpp_config(self): |
| 308 | self.test.vapi.ipsec_tunnel_protect_del( |
| 309 | sw_if_index=self.itf.sw_if_index) |
| 310 | |
| 311 | def query_vpp_config(self): |
| 312 | bs = self.test.vapi.ipsec_tunnel_protect_dump( |
| 313 | sw_if_index=self.itf.sw_if_index) |
| 314 | for b in bs: |
| 315 | if b.tun.sw_if_index == self.itf.sw_if_index: |
| 316 | return True |
| 317 | return False |