Filip Tehlar | 770e89e | 2017-01-31 10:39:16 +0100 | [diff] [blame] | 1 | from scapy.fields import * |
Filip Tehlar | 770e89e | 2017-01-31 10:39:16 +0100 | [diff] [blame] | 2 | from vpp_object import * |
| 3 | |
| 4 | |
| 5 | class VppLispLocatorSet(VppObject): |
| 6 | """ Represents LISP locator set in VPP """ |
| 7 | |
| 8 | def __init__(self, test, ls_name): |
| 9 | self._test = test |
| 10 | self._ls_name = ls_name |
| 11 | |
| 12 | @property |
| 13 | def test(self): |
| 14 | return self._test |
| 15 | |
| 16 | @property |
| 17 | def ls_name(self): |
| 18 | return self._ls_name |
| 19 | |
| 20 | def add_vpp_config(self): |
| 21 | self.test.vapi.lisp_locator_set(ls_name=self._ls_name) |
| 22 | self._test.registry.register(self, self.test.logger) |
| 23 | |
| 24 | def get_lisp_locator_sets_dump_entry(self): |
| 25 | result = self.test.vapi.lisp_locator_set_dump() |
| 26 | for ls in result: |
| 27 | if ls.ls_name.strip('\x00') == self._ls_name: |
| 28 | return ls |
| 29 | return None |
| 30 | |
| 31 | def query_vpp_config(self): |
| 32 | return self.get_lisp_locator_sets_dump_entry() is not None |
| 33 | |
| 34 | def remove_vpp_config(self): |
| 35 | self.test.vapi.lisp_locator_set(ls_name=self._ls_name, is_add=0) |
| 36 | |
| 37 | def object_id(self): |
| 38 | return 'lisp-locator-set-%s' % self._ls_name |
| 39 | |
| 40 | |
| 41 | class VppLispLocator(VppObject): |
| 42 | """ Represents LISP locator in VPP """ |
| 43 | |
| 44 | def __init__(self, test, sw_if_index, ls_name, priority=1, weight=1): |
| 45 | self._test = test |
| 46 | self._sw_if_index = sw_if_index |
| 47 | self._ls_name = ls_name |
| 48 | self._priority = priority |
| 49 | self._weight = weight |
| 50 | |
| 51 | @property |
| 52 | def test(self): |
| 53 | """ Test which created this locator """ |
| 54 | return self._test |
| 55 | |
| 56 | @property |
| 57 | def ls_name(self): |
| 58 | """ Locator set name """ |
| 59 | return self._ls_name |
| 60 | |
| 61 | @property |
| 62 | def sw_if_index(self): |
| 63 | return self._sw_if_index |
| 64 | |
| 65 | @property |
| 66 | def priority(self): |
Paul Vinciguerra | a0d82d6 | 2019-01-20 16:36:01 -0800 | [diff] [blame] | 67 | return self._priority |
Filip Tehlar | 770e89e | 2017-01-31 10:39:16 +0100 | [diff] [blame] | 68 | |
| 69 | @property |
| 70 | def weight(self): |
| 71 | return self._weight |
| 72 | |
| 73 | def add_vpp_config(self): |
| 74 | self.test.vapi.lisp_locator(ls_name=self._ls_name, |
| 75 | sw_if_index=self._sw_if_index, |
| 76 | priority=self._priority, |
| 77 | weight=self._weight) |
| 78 | self._test.registry.register(self, self.test.logger) |
| 79 | |
| 80 | def get_lisp_locator_dump_entry(self): |
| 81 | locators = self.test.vapi.lisp_locator_dump( |
| 82 | is_index_set=0, ls_name=self._ls_name) |
| 83 | for locator in locators: |
| 84 | if locator.sw_if_index == self._sw_if_index: |
| 85 | return locator |
| 86 | return None |
| 87 | |
| 88 | def query_vpp_config(self): |
| 89 | locator = self.get_lisp_locator_dump_entry() |
| 90 | return locator is not None |
| 91 | |
| 92 | def remove_vpp_config(self): |
| 93 | self.test.vapi.lisp_locator( |
| 94 | ls_name=self._ls_name, sw_if_index=self._sw_if_index, |
| 95 | priority=self._priority, weight=self._weight, is_add=0) |
| 96 | self._test.registry.register(self, self.test.logger) |
| 97 | |
| 98 | def object_id(self): |
| 99 | return 'lisp-locator-%s-%d' % (self._ls_name, self._sw_if_index) |
| 100 | |
| 101 | |
| 102 | class LispEIDType(object): |
| 103 | IP4 = 0 |
| 104 | IP6 = 1 |
| 105 | MAC = 2 |
| 106 | |
| 107 | |
| 108 | class LispKeyIdType(object): |
| 109 | NONE = 0 |
| 110 | SHA1 = 1 |
| 111 | SHA256 = 2 |
| 112 | |
| 113 | |
| 114 | class LispEID(object): |
| 115 | """ Lisp endpoint identifier """ |
| 116 | def __init__(self, eid): |
| 117 | self.eid = eid |
| 118 | |
| 119 | # find out whether EID is ip4 prefix, ip6 prefix or MAC |
| 120 | if self.eid.find("/") != -1: |
| 121 | if self.eid.find(":") == -1: |
| 122 | self.eid_type = LispEIDType.IP4 |
| 123 | self.data_length = 4 |
| 124 | else: |
| 125 | self.eid_type = LispEIDType.IP6 |
| 126 | self.data_length = 16 |
| 127 | |
| 128 | self.eid_address = self.eid.split("/")[0] |
| 129 | self.prefix_length = int(self.eid.split("/")[1]) |
| 130 | elif self.eid.count(":") == 5: # MAC address |
| 131 | self.eid_type = LispEIDType.MAC |
| 132 | self.eid_address = self.eid |
| 133 | self.prefix_length = 0 |
| 134 | self.data_length = 6 |
| 135 | else: |
| 136 | raise Exception('Unsupported EID format {}!'.format(eid)) |
| 137 | |
| 138 | def __str__(self): |
| 139 | if self.eid_type == LispEIDType.IP4: |
| 140 | return socket.inet_pton(socket.AF_INET, self.eid_address) |
| 141 | elif self.eid_type == LispEIDType.IP6: |
| 142 | return socket.inet_pton(socket.AF_INET6, self.eid_address) |
| 143 | elif self.eid_type == LispEIDType.MAC: |
| 144 | return Exception('Unimplemented') |
| 145 | raise Exception('Unknown EID type {}!'.format(self.eid_type)) |
| 146 | |
| 147 | |
| 148 | class VppLispMapping(VppObject): |
| 149 | """ Represents common features for remote and local LISP mapping in VPP """ |
| 150 | |
| 151 | def __init__(self, test, eid, vni=0, priority=1, weight=1): |
| 152 | self._eid = LispEID(eid) |
| 153 | self._test = test |
| 154 | self._priority = priority |
| 155 | self._weight = weight |
| 156 | self._vni = vni |
| 157 | |
| 158 | @property |
| 159 | def test(self): |
| 160 | return self._test |
| 161 | |
| 162 | @property |
| 163 | def vni(self): |
| 164 | return self._vni |
| 165 | |
| 166 | @property |
| 167 | def eid(self): |
| 168 | return self._eid |
| 169 | |
| 170 | @property |
| 171 | def priority(self): |
| 172 | return self._priority |
| 173 | |
| 174 | @property |
| 175 | def weight(self): |
| 176 | return self._weight |
| 177 | |
| 178 | def get_lisp_mapping_dump_entry(self): |
| 179 | return self.test.vapi.lisp_eid_table_dump( |
| 180 | eid_set=1, prefix_length=self._eid.prefix_length, |
| 181 | vni=self._vni, eid_type=self._eid.eid_type, eid=str(self._eid)) |
| 182 | |
| 183 | def query_vpp_config(self): |
| 184 | mapping = self.get_lisp_mapping_dump_entry() |
| 185 | return mapping |
| 186 | |
Paul Vinciguerra | 6d9e045 | 2019-01-20 17:11:48 -0800 | [diff] [blame] | 187 | def object_id(self): |
| 188 | return 'lisp-mapping-[%s]-%s-%s-%s' % ( |
| 189 | self.vni, self.eid, self.priority, self.weight) |
| 190 | |
Filip Tehlar | 770e89e | 2017-01-31 10:39:16 +0100 | [diff] [blame] | 191 | |
| 192 | class VppLocalMapping(VppLispMapping): |
| 193 | """ LISP Local mapping """ |
| 194 | def __init__(self, test, eid, ls_name, vni=0, priority=1, weight=1, |
| 195 | key_id=LispKeyIdType.NONE, key=''): |
| 196 | super(VppLocalMapping, self).__init__(test, eid, vni, priority, weight) |
| 197 | self._ls_name = ls_name |
| 198 | self._key_id = key_id |
| 199 | self._key = key |
| 200 | |
| 201 | @property |
| 202 | def ls_name(self): |
| 203 | return self._ls_name |
| 204 | |
| 205 | @property |
| 206 | def key_id(self): |
| 207 | return self._key_id |
| 208 | |
| 209 | @property |
| 210 | def key(self): |
| 211 | return self._key |
| 212 | |
| 213 | def add_vpp_config(self): |
| 214 | self.test.vapi.lisp_local_mapping( |
| 215 | ls_name=self._ls_name, eid_type=self._eid.eid_type, |
| 216 | eid=str(self._eid), prefix_len=self._eid.prefix_length, |
| 217 | vni=self._vni, key_id=self._key_id, key=self._key) |
| 218 | self._test.registry.register(self, self.test.logger) |
| 219 | |
| 220 | def remove_vpp_config(self): |
| 221 | self.test.vapi.lisp_local_mapping( |
| 222 | ls_name=self._ls_name, eid_type=self._eid.eid_type, |
| 223 | eid=str(self._eid), prefix_len=self._eid.prefix_length, |
| 224 | vni=self._vni, is_add=0) |
| 225 | |
| 226 | def object_id(self): |
| 227 | return 'lisp-eid-local-mapping-%s[%d]' % (self._eid, self._vni) |
| 228 | |
| 229 | |
| 230 | class VppRemoteMapping(VppLispMapping): |
| 231 | |
| 232 | def __init__(self, test, eid, rlocs=None, vni=0, priority=1, weight=1): |
| 233 | super(VppRemoteMapping, self).__init__(test, eid, vni, priority, |
| 234 | weight) |
| 235 | self._rlocs = rlocs |
| 236 | |
| 237 | @property |
| 238 | def rlocs(self): |
| 239 | return self._rlocs |
| 240 | |
| 241 | def add_vpp_config(self): |
| 242 | self.test.vapi.lisp_remote_mapping( |
| 243 | rlocs=self._rlocs, eid_type=self._eid.eid_type, |
| 244 | eid=str(self._eid), eid_prefix_len=self._eid.prefix_length, |
| 245 | vni=self._vni, rlocs_num=len(self._rlocs)) |
| 246 | self._test.registry.register(self, self.test.logger) |
| 247 | |
| 248 | def remove_vpp_config(self): |
| 249 | self.test.vapi.lisp_remote_mapping( |
| 250 | eid_type=self._eid.eid_type, eid=str(self._eid), |
| 251 | eid_prefix_len=self._eid.prefix_length, vni=self._vni, |
| 252 | is_add=0, rlocs_num=0) |
| 253 | |
| 254 | def object_id(self): |
| 255 | return 'lisp-eid-remote-mapping-%s[%d]' % (self._eid, self._vni) |
| 256 | |
| 257 | |
| 258 | class VppLispAdjacency(VppObject): |
| 259 | """ Represents LISP adjacency in VPP """ |
| 260 | |
| 261 | def __init__(self, test, leid, reid, vni=0): |
| 262 | self._leid = LispEID(leid) |
| 263 | self._reid = LispEID(reid) |
| 264 | if self._leid.eid_type != self._reid.eid_type: |
| 265 | raise Exception('remote and local EID are different types!') |
| 266 | self._vni = vni |
| 267 | self._test = test |
| 268 | |
| 269 | @property |
| 270 | def test(self): |
| 271 | return self._test |
| 272 | |
| 273 | @property |
| 274 | def leid(self): |
| 275 | return self._leid |
| 276 | |
| 277 | @property |
| 278 | def reid(self): |
| 279 | return self._reid |
| 280 | |
| 281 | @property |
| 282 | def vni(self): |
| 283 | return self._vni |
| 284 | |
| 285 | def add_vpp_config(self): |
| 286 | self.test.vapi.lisp_adjacency( |
| 287 | leid=str(self._leid), |
| 288 | reid=str(self._reid), eid_type=self._leid.eid_type, |
| 289 | leid_len=self._leid.prefix_length, |
| 290 | reid_len=self._reid.prefix_length, vni=self._vni) |
| 291 | self._test.registry.register(self, self.test.logger) |
| 292 | |
Paul Vinciguerra | 4a144b4 | 2018-11-23 03:20:21 -0800 | [diff] [blame] | 293 | @staticmethod |
| 294 | def eid_equal(eid, eid_type, eid_data, prefix_len): |
Filip Tehlar | 770e89e | 2017-01-31 10:39:16 +0100 | [diff] [blame] | 295 | if eid.eid_type != eid_type: |
| 296 | return False |
| 297 | |
| 298 | if eid_type == LispEIDType.IP4 or eid_type == LispEIDType.IP6: |
| 299 | if eid.prefix_length != prefix_len: |
| 300 | return False |
| 301 | |
| 302 | if str(eid) != eid_data[0:eid.data_length]: |
| 303 | return False |
| 304 | |
| 305 | return True |
| 306 | |
| 307 | def query_vpp_config(self): |
| 308 | res = self.test.vapi.lisp_adjacencies_get(vni=self._vni) |
| 309 | for adj in res.adjacencies: |
| 310 | if self.eid_equal(self._leid, adj.eid_type, adj.leid, |
| 311 | adj.leid_prefix_len) and \ |
| 312 | self.eid_equal(self._reid, adj.eid_type, adj.reid, |
| 313 | adj.reid_prefix_len): |
| 314 | return True |
| 315 | return False |
| 316 | |
| 317 | def remove_vpp_config(self): |
| 318 | self.test.vapi.lisp_adjacency( |
| 319 | leid=str(self._leid), |
| 320 | reid=str(self._reid), eid_type=self._leid.eid_type, |
| 321 | leid_len=self._leid.prefix_length, |
| 322 | reid_len=self._reid.prefix_length, vni=self._vni, is_add=0) |
| 323 | |
| 324 | def object_id(self): |
| 325 | return 'lisp-adjacency-%s-%s[%d]' % (self._leid, self._reid, self._vni) |