1 from random import randint
2 from socket import AF_INET, AF_INET6
3 from scapy.all import *
4 from scapy.packet import *
5 from scapy.fields import *
7 from framework import *
8 from vpp_object import *
11 class VppLispLocatorSet(VppObject):
12 """ Represents LISP locator set in VPP """
14 def __init__(self, test, ls_name):
16 self._ls_name = ls_name
26 def add_vpp_config(self):
27 self.test.vapi.lisp_locator_set(ls_name=self._ls_name)
28 self._test.registry.register(self, self.test.logger)
30 def get_lisp_locator_sets_dump_entry(self):
31 result = self.test.vapi.lisp_locator_set_dump()
33 if ls.ls_name.strip('\x00') == self._ls_name:
37 def query_vpp_config(self):
38 return self.get_lisp_locator_sets_dump_entry() is not None
40 def remove_vpp_config(self):
41 self.test.vapi.lisp_locator_set(ls_name=self._ls_name, is_add=0)
44 return 'lisp-locator-set-%s' % self._ls_name
47 class VppLispLocator(VppObject):
48 """ Represents LISP locator in VPP """
50 def __init__(self, test, sw_if_index, ls_name, priority=1, weight=1):
52 self._sw_if_index = sw_if_index
53 self._ls_name = ls_name
54 self._priority = priority
59 """ Test which created this locator """
64 """ Locator set name """
68 def sw_if_index(self):
69 return self._sw_if_index
79 def add_vpp_config(self):
80 self.test.vapi.lisp_locator(ls_name=self._ls_name,
81 sw_if_index=self._sw_if_index,
82 priority=self._priority,
84 self._test.registry.register(self, self.test.logger)
86 def get_lisp_locator_dump_entry(self):
87 locators = self.test.vapi.lisp_locator_dump(
88 is_index_set=0, ls_name=self._ls_name)
89 for locator in locators:
90 if locator.sw_if_index == self._sw_if_index:
94 def query_vpp_config(self):
95 locator = self.get_lisp_locator_dump_entry()
96 return locator is not None
98 def remove_vpp_config(self):
99 self.test.vapi.lisp_locator(
100 ls_name=self._ls_name, sw_if_index=self._sw_if_index,
101 priority=self._priority, weight=self._weight, is_add=0)
102 self._test.registry.register(self, self.test.logger)
105 return 'lisp-locator-%s-%d' % (self._ls_name, self._sw_if_index)
108 class LispEIDType(object):
114 class LispKeyIdType(object):
120 class LispEID(object):
121 """ Lisp endpoint identifier """
122 def __init__(self, eid):
125 # find out whether EID is ip4 prefix, ip6 prefix or MAC
126 if self.eid.find("/") != -1:
127 if self.eid.find(":") == -1:
128 self.eid_type = LispEIDType.IP4
131 self.eid_type = LispEIDType.IP6
132 self.data_length = 16
134 self.eid_address = self.eid.split("/")[0]
135 self.prefix_length = int(self.eid.split("/")[1])
136 elif self.eid.count(":") == 5: # MAC address
137 self.eid_type = LispEIDType.MAC
138 self.eid_address = self.eid
139 self.prefix_length = 0
142 raise Exception('Unsupported EID format {}!'.format(eid))
145 if self.eid_type == LispEIDType.IP4:
146 return socket.inet_pton(socket.AF_INET, self.eid_address)
147 elif self.eid_type == LispEIDType.IP6:
148 return socket.inet_pton(socket.AF_INET6, self.eid_address)
149 elif self.eid_type == LispEIDType.MAC:
150 return Exception('Unimplemented')
151 raise Exception('Unknown EID type {}!'.format(self.eid_type))
154 class VppLispMapping(VppObject):
155 """ Represents common features for remote and local LISP mapping in VPP """
157 def __init__(self, test, eid, vni=0, priority=1, weight=1):
158 self._eid = LispEID(eid)
160 self._priority = priority
161 self._weight = weight
178 return self._priority
184 def get_lisp_mapping_dump_entry(self):
185 return self.test.vapi.lisp_eid_table_dump(
186 eid_set=1, prefix_length=self._eid.prefix_length,
187 vni=self._vni, eid_type=self._eid.eid_type, eid=str(self._eid))
189 def query_vpp_config(self):
190 mapping = self.get_lisp_mapping_dump_entry()
194 class VppLocalMapping(VppLispMapping):
195 """ LISP Local mapping """
196 def __init__(self, test, eid, ls_name, vni=0, priority=1, weight=1,
197 key_id=LispKeyIdType.NONE, key=''):
198 super(VppLocalMapping, self).__init__(test, eid, vni, priority, weight)
199 self._ls_name = ls_name
200 self._key_id = key_id
215 def add_vpp_config(self):
216 self.test.vapi.lisp_local_mapping(
217 ls_name=self._ls_name, eid_type=self._eid.eid_type,
218 eid=str(self._eid), prefix_len=self._eid.prefix_length,
219 vni=self._vni, key_id=self._key_id, key=self._key)
220 self._test.registry.register(self, self.test.logger)
222 def remove_vpp_config(self):
223 self.test.vapi.lisp_local_mapping(
224 ls_name=self._ls_name, eid_type=self._eid.eid_type,
225 eid=str(self._eid), prefix_len=self._eid.prefix_length,
226 vni=self._vni, is_add=0)
229 return 'lisp-eid-local-mapping-%s[%d]' % (self._eid, self._vni)
232 class VppRemoteMapping(VppLispMapping):
234 def __init__(self, test, eid, rlocs=None, vni=0, priority=1, weight=1):
235 super(VppRemoteMapping, self).__init__(test, eid, vni, priority,
243 def add_vpp_config(self):
244 self.test.vapi.lisp_remote_mapping(
245 rlocs=self._rlocs, eid_type=self._eid.eid_type,
246 eid=str(self._eid), eid_prefix_len=self._eid.prefix_length,
247 vni=self._vni, rlocs_num=len(self._rlocs))
248 self._test.registry.register(self, self.test.logger)
250 def remove_vpp_config(self):
251 self.test.vapi.lisp_remote_mapping(
252 eid_type=self._eid.eid_type, eid=str(self._eid),
253 eid_prefix_len=self._eid.prefix_length, vni=self._vni,
254 is_add=0, rlocs_num=0)
257 return 'lisp-eid-remote-mapping-%s[%d]' % (self._eid, self._vni)
260 class VppLispAdjacency(VppObject):
261 """ Represents LISP adjacency in VPP """
263 def __init__(self, test, leid, reid, vni=0):
264 self._leid = LispEID(leid)
265 self._reid = LispEID(reid)
266 if self._leid.eid_type != self._reid.eid_type:
267 raise Exception('remote and local EID are different types!')
287 def add_vpp_config(self):
288 self.test.vapi.lisp_adjacency(
289 leid=str(self._leid),
290 reid=str(self._reid), eid_type=self._leid.eid_type,
291 leid_len=self._leid.prefix_length,
292 reid_len=self._reid.prefix_length, vni=self._vni)
293 self._test.registry.register(self, self.test.logger)
295 def eid_equal(self, eid, eid_type, eid_data, prefix_len):
296 if eid.eid_type != eid_type:
299 if eid_type == LispEIDType.IP4 or eid_type == LispEIDType.IP6:
300 if eid.prefix_length != prefix_len:
303 if str(eid) != eid_data[0:eid.data_length]:
308 def query_vpp_config(self):
309 res = self.test.vapi.lisp_adjacencies_get(vni=self._vni)
310 for adj in res.adjacencies:
311 if self.eid_equal(self._leid, adj.eid_type, adj.leid,
312 adj.leid_prefix_len) and \
313 self.eid_equal(self._reid, adj.eid_type, adj.reid,
314 adj.reid_prefix_len):
318 def remove_vpp_config(self):
319 self.test.vapi.lisp_adjacency(
320 leid=str(self._leid),
321 reid=str(self._reid), eid_type=self._leid.eid_type,
322 leid_len=self._leid.prefix_length,
323 reid_len=self._reid.prefix_length, vni=self._vni, is_add=0)
326 return 'lisp-adjacency-%s-%s[%d]' % (self._leid, self._reid, self._vni)