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): |
| 67 | return self.priority |
| 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 | |
| 187 | |
| 188 | class VppLocalMapping(VppLispMapping): |
| 189 | """ LISP Local mapping """ |
| 190 | def __init__(self, test, eid, ls_name, vni=0, priority=1, weight=1, |
| 191 | key_id=LispKeyIdType.NONE, key=''): |
| 192 | super(VppLocalMapping, self).__init__(test, eid, vni, priority, weight) |
| 193 | self._ls_name = ls_name |
| 194 | self._key_id = key_id |
| 195 | self._key = key |
| 196 | |
| 197 | @property |
| 198 | def ls_name(self): |
| 199 | return self._ls_name |
| 200 | |
| 201 | @property |
| 202 | def key_id(self): |
| 203 | return self._key_id |
| 204 | |
| 205 | @property |
| 206 | def key(self): |
| 207 | return self._key |
| 208 | |
| 209 | def add_vpp_config(self): |
| 210 | self.test.vapi.lisp_local_mapping( |
| 211 | ls_name=self._ls_name, eid_type=self._eid.eid_type, |
| 212 | eid=str(self._eid), prefix_len=self._eid.prefix_length, |
| 213 | vni=self._vni, key_id=self._key_id, key=self._key) |
| 214 | self._test.registry.register(self, self.test.logger) |
| 215 | |
| 216 | def remove_vpp_config(self): |
| 217 | self.test.vapi.lisp_local_mapping( |
| 218 | ls_name=self._ls_name, eid_type=self._eid.eid_type, |
| 219 | eid=str(self._eid), prefix_len=self._eid.prefix_length, |
| 220 | vni=self._vni, is_add=0) |
| 221 | |
| 222 | def object_id(self): |
| 223 | return 'lisp-eid-local-mapping-%s[%d]' % (self._eid, self._vni) |
| 224 | |
| 225 | |
| 226 | class VppRemoteMapping(VppLispMapping): |
| 227 | |
| 228 | def __init__(self, test, eid, rlocs=None, vni=0, priority=1, weight=1): |
| 229 | super(VppRemoteMapping, self).__init__(test, eid, vni, priority, |
| 230 | weight) |
| 231 | self._rlocs = rlocs |
| 232 | |
| 233 | @property |
| 234 | def rlocs(self): |
| 235 | return self._rlocs |
| 236 | |
| 237 | def add_vpp_config(self): |
| 238 | self.test.vapi.lisp_remote_mapping( |
| 239 | rlocs=self._rlocs, eid_type=self._eid.eid_type, |
| 240 | eid=str(self._eid), eid_prefix_len=self._eid.prefix_length, |
| 241 | vni=self._vni, rlocs_num=len(self._rlocs)) |
| 242 | self._test.registry.register(self, self.test.logger) |
| 243 | |
| 244 | def remove_vpp_config(self): |
| 245 | self.test.vapi.lisp_remote_mapping( |
| 246 | eid_type=self._eid.eid_type, eid=str(self._eid), |
| 247 | eid_prefix_len=self._eid.prefix_length, vni=self._vni, |
| 248 | is_add=0, rlocs_num=0) |
| 249 | |
| 250 | def object_id(self): |
| 251 | return 'lisp-eid-remote-mapping-%s[%d]' % (self._eid, self._vni) |
| 252 | |
| 253 | |
| 254 | class VppLispAdjacency(VppObject): |
| 255 | """ Represents LISP adjacency in VPP """ |
| 256 | |
| 257 | def __init__(self, test, leid, reid, vni=0): |
| 258 | self._leid = LispEID(leid) |
| 259 | self._reid = LispEID(reid) |
| 260 | if self._leid.eid_type != self._reid.eid_type: |
| 261 | raise Exception('remote and local EID are different types!') |
| 262 | self._vni = vni |
| 263 | self._test = test |
| 264 | |
| 265 | @property |
| 266 | def test(self): |
| 267 | return self._test |
| 268 | |
| 269 | @property |
| 270 | def leid(self): |
| 271 | return self._leid |
| 272 | |
| 273 | @property |
| 274 | def reid(self): |
| 275 | return self._reid |
| 276 | |
| 277 | @property |
| 278 | def vni(self): |
| 279 | return self._vni |
| 280 | |
| 281 | def add_vpp_config(self): |
| 282 | self.test.vapi.lisp_adjacency( |
| 283 | leid=str(self._leid), |
| 284 | reid=str(self._reid), eid_type=self._leid.eid_type, |
| 285 | leid_len=self._leid.prefix_length, |
| 286 | reid_len=self._reid.prefix_length, vni=self._vni) |
| 287 | self._test.registry.register(self, self.test.logger) |
| 288 | |
Paul Vinciguerra | 4a144b4 | 2018-11-23 03:20:21 -0800 | [diff] [blame^] | 289 | @staticmethod |
| 290 | def eid_equal(eid, eid_type, eid_data, prefix_len): |
Filip Tehlar | 770e89e | 2017-01-31 10:39:16 +0100 | [diff] [blame] | 291 | if eid.eid_type != eid_type: |
| 292 | return False |
| 293 | |
| 294 | if eid_type == LispEIDType.IP4 or eid_type == LispEIDType.IP6: |
| 295 | if eid.prefix_length != prefix_len: |
| 296 | return False |
| 297 | |
| 298 | if str(eid) != eid_data[0:eid.data_length]: |
| 299 | return False |
| 300 | |
| 301 | return True |
| 302 | |
| 303 | def query_vpp_config(self): |
| 304 | res = self.test.vapi.lisp_adjacencies_get(vni=self._vni) |
| 305 | for adj in res.adjacencies: |
| 306 | if self.eid_equal(self._leid, adj.eid_type, adj.leid, |
| 307 | adj.leid_prefix_len) and \ |
| 308 | self.eid_equal(self._reid, adj.eid_type, adj.reid, |
| 309 | adj.reid_prefix_len): |
| 310 | return True |
| 311 | return False |
| 312 | |
| 313 | def remove_vpp_config(self): |
| 314 | self.test.vapi.lisp_adjacency( |
| 315 | leid=str(self._leid), |
| 316 | reid=str(self._reid), eid_type=self._leid.eid_type, |
| 317 | leid_len=self._leid.prefix_length, |
| 318 | reid_len=self._reid.prefix_length, vni=self._vni, is_add=0) |
| 319 | |
| 320 | def object_id(self): |
| 321 | return 'lisp-adjacency-%s-%s[%d]' % (self._leid, self._reid, self._vni) |