blob: 9ebc86a35e3713b10faf8a24f9f6178606d025d9 [file] [log] [blame]
Paul Vinciguerraa279d9c2019-02-28 09:00:09 -08001import socket
Jakub Grajciar58db6e12020-01-30 13:26:43 +01002from ipaddress import ip_network
Paul Vinciguerraa279d9c2019-02-28 09:00:09 -08003
4from vpp_object import VppObject
Filip Tehlar770e89e2017-01-31 10:39:16 +01005
6
7class VppLispLocatorSet(VppObject):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02008 """Represents LISP locator set in VPP"""
Filip Tehlar770e89e2017-01-31 10:39:16 +01009
10 def __init__(self, test, ls_name):
11 self._test = test
12 self._ls_name = ls_name
13
14 @property
15 def test(self):
16 return self._test
17
18 @property
19 def ls_name(self):
20 return self._ls_name
21
22 def add_vpp_config(self):
Jakub Grajciar58db6e12020-01-30 13:26:43 +010023 self.test.vapi.lisp_add_del_locator_set(locator_set_name=self._ls_name)
Filip Tehlar770e89e2017-01-31 10:39:16 +010024 self._test.registry.register(self, self.test.logger)
25
26 def get_lisp_locator_sets_dump_entry(self):
27 result = self.test.vapi.lisp_locator_set_dump()
28 for ls in result:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020029 if ls.ls_name.strip("\x00") == self._ls_name:
Filip Tehlar770e89e2017-01-31 10:39:16 +010030 return ls
31 return None
32
33 def query_vpp_config(self):
34 return self.get_lisp_locator_sets_dump_entry() is not None
35
36 def remove_vpp_config(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020037 self.test.vapi.lisp_add_del_locator_set(
38 locator_set_name=self._ls_name, is_add=0
39 )
Filip Tehlar770e89e2017-01-31 10:39:16 +010040
41 def object_id(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020042 return "lisp-locator-set-%s" % self._ls_name
Filip Tehlar770e89e2017-01-31 10:39:16 +010043
44
45class VppLispLocator(VppObject):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020046 """Represents LISP locator in VPP"""
Filip Tehlar770e89e2017-01-31 10:39:16 +010047
48 def __init__(self, test, sw_if_index, ls_name, priority=1, weight=1):
49 self._test = test
50 self._sw_if_index = sw_if_index
51 self._ls_name = ls_name
52 self._priority = priority
53 self._weight = weight
54
55 @property
56 def test(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020057 """Test which created this locator"""
Filip Tehlar770e89e2017-01-31 10:39:16 +010058 return self._test
59
60 @property
61 def ls_name(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020062 """Locator set name"""
Filip Tehlar770e89e2017-01-31 10:39:16 +010063 return self._ls_name
64
65 @property
66 def sw_if_index(self):
67 return self._sw_if_index
68
69 @property
70 def priority(self):
Paul Vinciguerraa0d82d62019-01-20 16:36:01 -080071 return self._priority
Filip Tehlar770e89e2017-01-31 10:39:16 +010072
73 @property
74 def weight(self):
75 return self._weight
76
77 def add_vpp_config(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020078 self.test.vapi.lisp_add_del_locator(
79 locator_set_name=self._ls_name,
80 sw_if_index=self._sw_if_index,
81 priority=self._priority,
82 weight=self._weight,
83 )
Filip Tehlar770e89e2017-01-31 10:39:16 +010084 self._test.registry.register(self, self.test.logger)
85
86 def get_lisp_locator_dump_entry(self):
87 locators = self.test.vapi.lisp_locator_dump(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020088 is_index_set=0, ls_name=self._ls_name
89 )
Filip Tehlar770e89e2017-01-31 10:39:16 +010090 for locator in locators:
91 if locator.sw_if_index == self._sw_if_index:
92 return locator
93 return None
94
95 def query_vpp_config(self):
96 locator = self.get_lisp_locator_dump_entry()
97 return locator is not None
98
99 def remove_vpp_config(self):
Ole Troane1ade682019-03-04 23:55:43 +0100100 self.test.vapi.lisp_add_del_locator(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200101 locator_set_name=self._ls_name,
102 sw_if_index=self._sw_if_index,
103 priority=self._priority,
104 weight=self._weight,
105 is_add=0,
106 )
Filip Tehlar770e89e2017-01-31 10:39:16 +0100107 self._test.registry.register(self, self.test.logger)
108
109 def object_id(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200110 return "lisp-locator-%s-%d" % (self._ls_name, self._sw_if_index)
Filip Tehlar770e89e2017-01-31 10:39:16 +0100111
112
Paul Vinciguerrae061dad2020-12-04 14:57:51 -0500113class LispEIDType:
Jakub Grajciar58db6e12020-01-30 13:26:43 +0100114 PREFIX = 0
115 MAC = 1
116 NSH = 2
Filip Tehlar770e89e2017-01-31 10:39:16 +0100117
118
Paul Vinciguerrae061dad2020-12-04 14:57:51 -0500119class LispKeyIdType:
Filip Tehlar770e89e2017-01-31 10:39:16 +0100120 NONE = 0
121 SHA1 = 1
122 SHA256 = 2
123
124
Paul Vinciguerrae061dad2020-12-04 14:57:51 -0500125class LispEID:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200126 """Lisp endpoint identifier"""
127
Filip Tehlar770e89e2017-01-31 10:39:16 +0100128 def __init__(self, eid):
129 self.eid = eid
Jakub Grajciar58db6e12020-01-30 13:26:43 +0100130 self._type = -1
Filip Tehlar770e89e2017-01-31 10:39:16 +0100131
Jakub Grajciar58db6e12020-01-30 13:26:43 +0100132 # find out whether EID is ip prefix, or MAC
133 try:
134 self.prefix = ip_network(self.eid)
135 self._type = LispEIDType.PREFIX
136 return
137 except ValueError:
138 if self.eid.count(":") == 5: # MAC address
139 self.mac = self.eid
140 self._type = LispEIDType.MAC
141 return
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200142 raise Exception("Unsupported EID format {!s}!".format(eid))
Filip Tehlar770e89e2017-01-31 10:39:16 +0100143
Jakub Grajciar58db6e12020-01-30 13:26:43 +0100144 @property
145 def eid_type(self):
146 return self._type
147
148 @property
149 def address(self):
150 if self.eid_type == LispEIDType.PREFIX:
151 return self.prefix
152 elif self.eid_type == LispEIDType.MAC:
153 return self.mac
154 elif self.eid_type == LispEIDType.NSH:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200155 return Exception("Unimplemented")
Filip Tehlar770e89e2017-01-31 10:39:16 +0100156
Paul Vinciguerra22ab6f72019-03-07 17:55:33 -0800157 @property
158 def packed(self):
Jakub Grajciar58db6e12020-01-30 13:26:43 +0100159 if self.eid_type == LispEIDType.PREFIX:
160 return {"type": self._type, "address": {"prefix": self.prefix}}
Filip Tehlar770e89e2017-01-31 10:39:16 +0100161 elif self.eid_type == LispEIDType.MAC:
Jakub Grajciar58db6e12020-01-30 13:26:43 +0100162 return {"type": self._type, "address": {"mac": self.mac}}
163 elif self.eid_type == LispEIDType.NSH:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200164 return Exception("Unimplemented")
Jakub Grajciar58db6e12020-01-30 13:26:43 +0100165
166
Paul Vinciguerrae061dad2020-12-04 14:57:51 -0500167class LispKey:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200168 """Lisp Key"""
169
Jakub Grajciar58db6e12020-01-30 13:26:43 +0100170 def __init__(self, key_type, key):
171 self._key_type = key_type
172 self._key = key
173
174 @property
175 def packed(self):
176 return {"id": self._key_type, "key": self._key}
Filip Tehlar770e89e2017-01-31 10:39:16 +0100177
178
179class VppLispMapping(VppObject):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200180 """Represents common features for remote and local LISP mapping in VPP"""
Filip Tehlar770e89e2017-01-31 10:39:16 +0100181
182 def __init__(self, test, eid, vni=0, priority=1, weight=1):
183 self._eid = LispEID(eid)
184 self._test = test
185 self._priority = priority
186 self._weight = weight
187 self._vni = vni
188
189 @property
190 def test(self):
191 return self._test
192
193 @property
194 def vni(self):
195 return self._vni
196
197 @property
198 def eid(self):
199 return self._eid
200
201 @property
202 def priority(self):
203 return self._priority
204
205 @property
206 def weight(self):
207 return self._weight
208
209 def get_lisp_mapping_dump_entry(self):
210 return self.test.vapi.lisp_eid_table_dump(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200211 eid_set=1, vni=self._vni, eid=self._eid.packed
212 )
Filip Tehlar770e89e2017-01-31 10:39:16 +0100213
214 def query_vpp_config(self):
215 mapping = self.get_lisp_mapping_dump_entry()
216 return mapping
217
Paul Vinciguerra6d9e0452019-01-20 17:11:48 -0800218 def object_id(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200219 return "lisp-mapping-[%s]-%s-%s-%s" % (
220 self.vni,
221 self.eid.address,
222 self.priority,
223 self.weight,
224 )
Paul Vinciguerra6d9e0452019-01-20 17:11:48 -0800225
Filip Tehlar770e89e2017-01-31 10:39:16 +0100226
227class VppLocalMapping(VppLispMapping):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200228 """LISP Local mapping"""
229
230 def __init__(
231 self,
232 test,
233 eid,
234 ls_name,
235 vni=0,
236 priority=1,
237 weight=1,
238 key_id=LispKeyIdType.NONE,
239 key="",
240 ):
Filip Tehlar770e89e2017-01-31 10:39:16 +0100241 super(VppLocalMapping, self).__init__(test, eid, vni, priority, weight)
242 self._ls_name = ls_name
Jakub Grajciar58db6e12020-01-30 13:26:43 +0100243 self._key = LispKey(key_id, key)
Filip Tehlar770e89e2017-01-31 10:39:16 +0100244
245 @property
246 def ls_name(self):
247 return self._ls_name
248
249 @property
250 def key_id(self):
251 return self._key_id
252
253 @property
254 def key(self):
255 return self._key
256
257 def add_vpp_config(self):
Ole Troane1ade682019-03-04 23:55:43 +0100258 self.test.vapi.lisp_add_del_local_eid(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200259 locator_set_name=self._ls_name,
260 eid=self._eid.packed,
261 vni=self._vni,
262 key=self._key.packed,
263 )
Filip Tehlar770e89e2017-01-31 10:39:16 +0100264 self._test.registry.register(self, self.test.logger)
265
266 def remove_vpp_config(self):
Ole Troane1ade682019-03-04 23:55:43 +0100267 self.test.vapi.lisp_add_del_local_eid(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200268 locator_set_name=self._ls_name,
269 eid=self._eid.packed,
270 vni=self._vni,
271 is_add=0,
272 )
Filip Tehlar770e89e2017-01-31 10:39:16 +0100273
274 def object_id(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200275 return "lisp-eid-local-mapping-%s[%d]" % (self._eid.address, self._vni)
Jakub Grajciar58db6e12020-01-30 13:26:43 +0100276
277
Paul Vinciguerrae061dad2020-12-04 14:57:51 -0500278class LispRemoteLocator:
Jakub Grajciar58db6e12020-01-30 13:26:43 +0100279 def __init__(self, addr, priority=1, weight=1):
280 self.addr = addr
281 self.priority = priority
282 self.weight = weight
283
284 @property
285 def packed(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200286 return {
287 "priority": self.priority,
288 "weight": self.weight,
289 "ip_address": self.addr,
290 }
Filip Tehlar770e89e2017-01-31 10:39:16 +0100291
292
293class VppRemoteMapping(VppLispMapping):
Filip Tehlar770e89e2017-01-31 10:39:16 +0100294 def __init__(self, test, eid, rlocs=None, vni=0, priority=1, weight=1):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200295 super(VppRemoteMapping, self).__init__(test, eid, vni, priority, weight)
Filip Tehlar770e89e2017-01-31 10:39:16 +0100296 self._rlocs = rlocs
297
298 @property
299 def rlocs(self):
Jakub Grajciar58db6e12020-01-30 13:26:43 +0100300 rlocs = []
301 for rloc in self._rlocs:
302 rlocs.append(rloc.packed)
303 return rlocs
Filip Tehlar770e89e2017-01-31 10:39:16 +0100304
305 def add_vpp_config(self):
Ole Troane1ade682019-03-04 23:55:43 +0100306 self.test.vapi.lisp_add_del_remote_mapping(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200307 rlocs=self.rlocs,
308 deid=self._eid.packed,
309 vni=self._vni,
310 rloc_num=len(self._rlocs),
311 )
Filip Tehlar770e89e2017-01-31 10:39:16 +0100312 self._test.registry.register(self, self.test.logger)
313
314 def remove_vpp_config(self):
Ole Troane1ade682019-03-04 23:55:43 +0100315 self.test.vapi.lisp_add_del_remote_mapping(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200316 deid=self._eid.packed, vni=self._vni, is_add=0, rloc_num=0
317 )
Filip Tehlar770e89e2017-01-31 10:39:16 +0100318
319 def object_id(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200320 return "lisp-eid-remote-mapping-%s[%d]" % (self._eid.address, self._vni)
Filip Tehlar770e89e2017-01-31 10:39:16 +0100321
322
323class VppLispAdjacency(VppObject):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200324 """Represents LISP adjacency in VPP"""
Filip Tehlar770e89e2017-01-31 10:39:16 +0100325
326 def __init__(self, test, leid, reid, vni=0):
327 self._leid = LispEID(leid)
328 self._reid = LispEID(reid)
329 if self._leid.eid_type != self._reid.eid_type:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200330 raise Exception("remote and local EID are different types!")
Filip Tehlar770e89e2017-01-31 10:39:16 +0100331 self._vni = vni
332 self._test = test
333
334 @property
335 def test(self):
336 return self._test
337
338 @property
339 def leid(self):
340 return self._leid
341
342 @property
343 def reid(self):
344 return self._reid
345
346 @property
347 def vni(self):
348 return self._vni
349
350 def add_vpp_config(self):
Ole Troane1ade682019-03-04 23:55:43 +0100351 self.test.vapi.lisp_add_del_adjacency(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200352 leid=self._leid.packed, reid=self._reid.packed, vni=self._vni
353 )
Filip Tehlar770e89e2017-01-31 10:39:16 +0100354 self._test.registry.register(self, self.test.logger)
355
Paul Vinciguerra4a144b42018-11-23 03:20:21 -0800356 @staticmethod
Jakub Grajciar58db6e12020-01-30 13:26:43 +0100357 def eid_equal(eid, eid_api):
358 if eid.eid_type != eid_api.type:
Filip Tehlar770e89e2017-01-31 10:39:16 +0100359 return False
360
Jakub Grajciar58db6e12020-01-30 13:26:43 +0100361 if eid_api.type == LispEIDType.PREFIX:
362 if eid.address.prefixlen != eid_api.address.prefix.prefixlen:
Filip Tehlar770e89e2017-01-31 10:39:16 +0100363 return False
364
Jakub Grajciar58db6e12020-01-30 13:26:43 +0100365 if eid.address != eid_api.address:
Filip Tehlar770e89e2017-01-31 10:39:16 +0100366 return False
367
368 return True
369
370 def query_vpp_config(self):
371 res = self.test.vapi.lisp_adjacencies_get(vni=self._vni)
372 for adj in res.adjacencies:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200373 if self.eid_equal(self._leid, adj.leid) and self.eid_equal(
374 self._reid, adj.reid
375 ):
Filip Tehlar770e89e2017-01-31 10:39:16 +0100376 return True
377 return False
378
379 def remove_vpp_config(self):
Ole Troane1ade682019-03-04 23:55:43 +0100380 self.test.vapi.lisp_add_del_adjacency(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200381 leid=self._leid.packed, reid=self._reid.packed, vni=self._vni, is_add=0
382 )
Filip Tehlar770e89e2017-01-31 10:39:16 +0100383
384 def object_id(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200385 return "lisp-adjacency-%s-%s[%d]" % (self._leid, self._reid, self._vni)