6 from scapy.fields import BitField, ByteField, FlagsField, IntField
7 from scapy.packet import bind_layers, Packet, Raw
8 from scapy.layers.inet import IP, UDP, Ether
9 from scapy.layers.inet6 import IPv6
11 from framework import VppTestCase
12 from asfframework import VppTestRunner
23 # From py_lispnetworking.lisp.py: # GNU General Public License v2.0
26 class LISP_GPE_Header(Packet):
27 name = "LISP GPE Header"
29 FlagsField("gpe_flags", None, 6, ["N", "L", "E", "V", "I", "P"]),
30 BitField("reserved", 0, 18),
31 ByteField("next_proto", 0),
36 bind_layers(UDP, LISP_GPE_Header, dport=4341)
37 bind_layers(UDP, LISP_GPE_Header, sport=4341)
38 bind_layers(LISP_GPE_Header, IP, next_proto=1)
39 bind_layers(LISP_GPE_Header, IPv6, next_proto=2)
40 bind_layers(LISP_GPE_Header, Ether, next_proto=3)
43 class ForeignAddressFactory(object):
46 net_template = "10.10.10.{}"
47 net = net_template.format(0) + "/" + str(prefix_len)
51 raise Exception("Network host address exhaustion")
53 return self.net_template.format(self.count)
56 class Driver(metaclass=abc.ABCMeta):
65 """ Basic class for data driven testing """
67 def __init__(self, test, test_cases):
68 self._test_cases = test_cases
73 return self._test_cases
79 def create_packet(self, src_if, dst_if, deid, payload=""):
87 Ether(dst=src_if.local_mac, src=src_if.remote_mac)
88 / IP(src=src_if.remote_ip4, dst=deid)
95 """testing procedure"""
99 class SimpleDriver(Driver):
100 """Implements simple test procedure"""
102 def __init__(self, test, test_cases):
103 super(SimpleDriver, self).__init__(test, test_cases)
105 def verify_capture(self, src_loc, dst_loc, capture):
107 Verify captured packet
109 :param src_loc: source locator address
110 :param dst_loc: destination locator address
111 :param capture: list of captured packets
113 self.test.assertEqual(
116 "Unexpected number of "
117 "packets! Expected 1 but {} received".format(len(capture)),
122 # assert the values match
123 self.test.assertEqual(ip_hdr.src, src_loc, "IP source address")
124 self.test.assertEqual(ip_hdr.dst, dst_loc, "IP destination address")
125 gpe_hdr = packet[LISP_GPE_Header]
126 self.test.assertEqual(gpe_hdr.next_proto, 1, "next_proto is not ipv4!")
128 self.test.assertEqual(
129 ih.src, self.test.pg0.remote_ip4, "unexpected source EID!"
131 self.test.assertEqual(ih.dst, self.test.deid_ip4, "unexpected dest EID!")
133 self.test.logger.error(ppp("Unexpected or invalid packet:", packet))
136 def configure_tc(self, tc):
137 for config_item in self.config_order:
138 for vpp_object in tc[config_item]:
139 vpp_object.add_vpp_config()
142 """Send traffic for each test case and verify that it
144 for tc in enumerate(self.test_cases):
145 self.test.logger.info("Running {}".format(tc[1]["name"]))
146 self.configure_tc(tc[1])
148 packet = self.create_packet(self.test.pg0, self.test.pg1, dest, "data")
149 self.test.pg0.add_stream(packet)
150 self.test.pg0.enable_capture()
151 self.test.pg1.enable_capture()
153 capture = self.test.pg1.get_capture(1)
155 self.test.pg1.local_ip4, self.test.pg1.remote_ip4, capture
157 self.test.pg0.assert_nothing_captured()
160 class TestLisp(VppTestCase):
161 """Basic LISP test"""
165 super(TestLisp, cls).setUpClass()
166 cls.faf = ForeignAddressFactory()
167 cls.create_pg_interfaces(range(2)) # create pg0 and pg1
168 for i in cls.pg_interfaces:
169 i.admin_up() # put the interface upsrc_if
170 i.config_ip4() # configure IPv4 address on the interface
171 i.resolve_arp() # resolve ARP, so that we know VPP MAC
174 def tearDownClass(cls):
175 super(TestLisp, cls).tearDownClass()
178 super(TestLisp, self).setUp()
179 self.vapi.lisp_enable_disable(is_enable=1)
181 def test_lisp_basic_encap(self):
182 """Test case for basic encapsulation"""
184 self.deid_ip4_net = self.faf.net
185 self.deid_ip4 = self.faf.get_ip4()
186 self.seid_ip4 = "{!s}/{!s}".format(self.pg0.local_ip4, 32)
187 self.rloc_ip4 = self.pg1.remote_ip4
191 "name": "basic ip4 over ip4",
192 "locator-sets": [VppLispLocatorSet(self, "ls-4o4")],
193 "locators": [VppLispLocator(self, self.pg1.sw_if_index, "ls-4o4")],
194 "local-mappings": [VppLocalMapping(self, self.seid_ip4, "ls-4o4")],
197 self, self.deid_ip4_net, [LispRemoteLocator(self.rloc_ip4)]
201 VppLispAdjacency(self, self.seid_ip4, self.deid_ip4_net)
205 self.test_driver = SimpleDriver(self, test_cases)
206 self.test_driver.run(self.deid_ip4)
209 class TestLispUT(VppTestCase):
214 super(TestLispUT, cls).setUpClass()
217 def tearDownClass(cls):
218 super(TestLispUT, cls).tearDownClass()
221 """LISP Unit Tests"""
222 error = self.vapi.cli("test lisp cp")
225 self.logger.critical(error)
226 self.assertNotIn("Failed", error)
229 if __name__ == "__main__":
230 unittest.main(testRunner=VppTestRunner)