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 | |
| 11 | class VppIpsecSpd(VppObject): |
| 12 | """ |
| 13 | VPP SPD DB |
| 14 | """ |
| 15 | |
| 16 | def __init__(self, test, id): |
| 17 | self.test = test |
| 18 | self.id = id |
| 19 | |
| 20 | def add_vpp_config(self): |
| 21 | self.test.vapi.ipsec_spd_add_del(self.id) |
| 22 | self.test.registry.register(self, self.test.logger) |
| 23 | |
| 24 | def remove_vpp_config(self): |
| 25 | self.test.vapi.ipsec_spd_add_del(self.id, is_add=0) |
| 26 | |
| 27 | def __str__(self): |
| 28 | return self.object_id() |
| 29 | |
| 30 | def object_id(self): |
| 31 | return "ipsec-spd-%d" % self.id |
| 32 | |
| 33 | def query_vpp_config(self): |
| 34 | spds = self.test.vapi.ipsec_spds_dump() |
| 35 | for spd in spds: |
| 36 | if spd.spd_id == self.id: |
| 37 | return True |
| 38 | return False |
| 39 | |
| 40 | |
| 41 | class VppIpsecSpdItfBinding(VppObject): |
| 42 | """ |
| 43 | VPP SPD DB to interface binding |
| 44 | (i.e. this SPD is used on this interfce) |
| 45 | """ |
| 46 | |
| 47 | def __init__(self, test, spd, itf): |
| 48 | self.test = test |
| 49 | self.spd = spd |
| 50 | self.itf = itf |
| 51 | |
| 52 | def add_vpp_config(self): |
| 53 | self.test.vapi.ipsec_interface_add_del_spd(self.spd.id, |
| 54 | self.itf.sw_if_index) |
| 55 | self.test.registry.register(self, self.test.logger) |
| 56 | |
| 57 | def remove_vpp_config(self): |
| 58 | self.test.vapi.ipsec_interface_add_del_spd(self.spd.id, |
| 59 | self.itf.sw_if_index, |
| 60 | is_add=0) |
| 61 | |
| 62 | def __str__(self): |
| 63 | return self.object_id() |
| 64 | |
| 65 | def object_id(self): |
| 66 | return "bind-%s-to-%s" % (self.spd.id, self.itf) |
| 67 | |
| 68 | def query_vpp_config(self): |
| 69 | bs = self.test.vapi.ipsec_spd_interface_dump() |
| 70 | for b in bs: |
| 71 | if b.sw_if_index == self.itf.sw_if_index: |
| 72 | return True |
| 73 | return False |
| 74 | |
| 75 | |
| 76 | class VppIpsecSpdEntry(VppObject): |
| 77 | """ |
| 78 | VPP SPD DB Entry |
| 79 | """ |
| 80 | |
| 81 | def __init__(self, test, spd, sa_id, |
| 82 | local_start, local_stop, |
| 83 | remote_start, remote_stop, |
| 84 | proto, |
| 85 | priority=100, |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 86 | policy=None, |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 87 | is_outbound=1, |
| 88 | remote_port_start=0, |
| 89 | remote_port_stop=65535, |
| 90 | local_port_start=0, |
| 91 | local_port_stop=65535): |
| 92 | self.test = test |
| 93 | self.spd = spd |
| 94 | self.sa_id = sa_id |
| 95 | self.local_start = ip_address(text_type(local_start)) |
| 96 | self.local_stop = ip_address(text_type(local_stop)) |
| 97 | self.remote_start = ip_address(text_type(remote_start)) |
| 98 | self.remote_stop = ip_address(text_type(remote_stop)) |
| 99 | self.proto = proto |
| 100 | self.is_outbound = is_outbound |
| 101 | self.priority = priority |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 102 | if not policy: |
| 103 | self.policy = (VppEnum.vl_api_ipsec_spd_action_t. |
| 104 | IPSEC_API_SPD_ACTION_BYPASS) |
| 105 | else: |
| 106 | self.policy = policy |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 107 | self.is_ipv6 = (0 if self.local_start.version == 4 else 1) |
| 108 | self.local_port_start = local_port_start |
| 109 | self.local_port_stop = local_port_stop |
| 110 | self.remote_port_start = remote_port_start |
| 111 | self.remote_port_stop = remote_port_stop |
| 112 | |
| 113 | def add_vpp_config(self): |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 114 | rv = self.test.vapi.ipsec_spd_entry_add_del( |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 115 | self.spd.id, |
| 116 | self.sa_id, |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 117 | self.local_start, |
| 118 | self.local_stop, |
| 119 | self.remote_start, |
| 120 | self.remote_stop, |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 121 | protocol=self.proto, |
| 122 | is_ipv6=self.is_ipv6, |
| 123 | is_outbound=self.is_outbound, |
| 124 | priority=self.priority, |
| 125 | policy=self.policy, |
| 126 | local_port_start=self.local_port_start, |
| 127 | local_port_stop=self.local_port_stop, |
| 128 | remote_port_start=self.remote_port_start, |
| 129 | remote_port_stop=self.remote_port_stop) |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 130 | self.stat_index = rv.stat_index |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 131 | self.test.registry.register(self, self.test.logger) |
| 132 | |
| 133 | def remove_vpp_config(self): |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 134 | self.test.vapi.ipsec_spd_entry_add_del( |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 135 | self.spd.id, |
| 136 | self.sa_id, |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 137 | self.local_start, |
| 138 | self.local_stop, |
| 139 | self.remote_start, |
| 140 | self.remote_stop, |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 141 | protocol=self.proto, |
| 142 | is_ipv6=self.is_ipv6, |
| 143 | is_outbound=self.is_outbound, |
| 144 | priority=self.priority, |
| 145 | policy=self.policy, |
| 146 | local_port_start=self.local_port_start, |
| 147 | local_port_stop=self.local_port_stop, |
| 148 | remote_port_start=self.remote_port_start, |
| 149 | remote_port_stop=self.remote_port_stop, |
| 150 | is_add=0) |
| 151 | |
| 152 | def __str__(self): |
| 153 | return self.object_id() |
| 154 | |
| 155 | def object_id(self): |
| 156 | return "spd-entry-%d-%d-%d-%d-%d-%d" % (self.spd.id, |
| 157 | self.priority, |
| 158 | self.policy, |
| 159 | self.is_outbound, |
| 160 | self.is_ipv6, |
| 161 | self.remote_port_start) |
| 162 | |
| 163 | def query_vpp_config(self): |
| 164 | ss = self.test.vapi.ipsec_spd_dump(self.spd.id) |
| 165 | for s in ss: |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 166 | if s.entry.sa_id == self.sa_id and \ |
| 167 | s.entry.is_outbound == self.is_outbound and \ |
| 168 | s.entry.priority == self.priority and \ |
| 169 | s.entry.policy == self.policy and \ |
| 170 | s.entry.remote_address_start == self.remote_start and \ |
| 171 | s.entry.remote_port_start == self.remote_port_start: |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 172 | return True |
| 173 | return False |
| 174 | |
Neale Ranns | a09c1ff | 2019-02-04 01:10:30 -0800 | [diff] [blame] | 175 | def get_stats(self): |
| 176 | c = self.test.statistics.get_counter("/net/ipsec/policy") |
| 177 | return c[0][self.stat_index] |
| 178 | |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 179 | |
| 180 | class VppIpsecSA(VppObject): |
| 181 | """ |
| 182 | VPP SAD Entry |
| 183 | """ |
| 184 | |
| 185 | def __init__(self, test, id, spi, |
| 186 | integ_alg, integ_key, |
| 187 | crypto_alg, crypto_key, |
| 188 | proto, |
| 189 | tun_src=None, tun_dst=None, |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 190 | flags=None): |
| 191 | e = VppEnum.vl_api_ipsec_sad_flags_t |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 192 | self.test = test |
| 193 | self.id = id |
| 194 | self.spi = spi |
| 195 | self.integ_alg = integ_alg |
| 196 | self.integ_key = integ_key |
| 197 | self.crypto_alg = crypto_alg |
| 198 | self.crypto_key = crypto_key |
| 199 | self.proto = proto |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 200 | |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 201 | self.tun_src = tun_src |
| 202 | self.tun_dst = tun_dst |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 203 | if not flags: |
| 204 | self.flags = e.IPSEC_API_SAD_FLAG_NONE |
| 205 | else: |
| 206 | self.flags = flags |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 207 | if (tun_src): |
| 208 | self.tun_src = ip_address(text_type(tun_src)) |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 209 | self.flags = self.flags | e.IPSEC_API_SAD_FLAG_IS_TUNNEL |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 210 | if (self.tun_src.version == 6): |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 211 | self.flags = self.flags | e.IPSEC_API_SAD_FLAG_IS_TUNNEL_V6 |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 212 | if (tun_dst): |
| 213 | self.tun_dst = ip_address(text_type(tun_dst)) |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 214 | |
| 215 | def add_vpp_config(self): |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 216 | r = self.test.vapi.ipsec_sad_entry_add_del( |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 217 | self.id, |
| 218 | self.spi, |
| 219 | self.integ_alg, |
| 220 | self.integ_key, |
| 221 | self.crypto_alg, |
| 222 | self.crypto_key, |
| 223 | self.proto, |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 224 | (self.tun_src if self.tun_src else []), |
| 225 | (self.tun_dst if self.tun_dst else []), |
| 226 | flags=self.flags) |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 227 | self.stat_index = r.stat_index |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 228 | self.test.registry.register(self, self.test.logger) |
| 229 | |
| 230 | def remove_vpp_config(self): |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 231 | self.test.vapi.ipsec_sad_entry_add_del( |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 232 | self.id, |
| 233 | self.spi, |
| 234 | self.integ_alg, |
| 235 | self.integ_key, |
| 236 | self.crypto_alg, |
| 237 | self.crypto_key, |
| 238 | self.proto, |
Neale Ranns | 17dcec0 | 2019-01-09 21:22:20 -0800 | [diff] [blame] | 239 | (self.tun_src if self.tun_src else []), |
| 240 | (self.tun_dst if self.tun_dst else []), |
| 241 | flags=self.flags, |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 242 | is_add=0) |
| 243 | |
| 244 | def __str__(self): |
| 245 | return self.object_id() |
| 246 | |
| 247 | def object_id(self): |
| 248 | return "ipsec-sa-%d" % self.id |
| 249 | |
| 250 | def query_vpp_config(self): |
| 251 | bs = self.test.vapi.ipsec_sa_dump() |
| 252 | for b in bs: |
Neale Ranns | 8d7c502 | 2019-02-06 01:41:05 -0800 | [diff] [blame] | 253 | if b.entry.sad_id == self.id: |
Neale Ranns | 311124e | 2019-01-24 04:52:25 -0800 | [diff] [blame] | 254 | return True |
| 255 | return False |
Neale Ranns | eba31ec | 2019-02-17 18:04:27 +0000 | [diff] [blame] | 256 | |
| 257 | def get_stats(self): |
| 258 | c = self.test.statistics.get_counter("/net/ipsec/sa") |
| 259 | return c[0][self.stat_index] |