4 from scapy.packet import Raw
5 from scapy.layers.inet import IP, UDP, Ether
6 from py_lispnetworking.lisp import LISP_GPE_Header
8 from util import ppp, ForeignAddressFactory
9 from framework import VppTestCase, VppTestRunner
15 config_order = ['locator-sets',
21 """ Basic class for data driven testing """
22 def __init__(self, test, test_cases):
23 self._test_cases = test_cases
28 return self._test_cases
34 def create_packet(self, src_if, dst_if, deid, payload=''):
41 packet = (Ether(dst=src_if.local_mac, src=src_if.remote_mac) /
42 IP(src=src_if.remote_ip4, dst=deid) /
48 """ testing procedure """
52 class SimpleDriver(Driver):
53 """ Implements simple test procedure """
54 def __init__(self, test, test_cases):
55 super(SimpleDriver, self).__init__(test, test_cases)
57 def verify_capture(self, src_loc, dst_loc, capture):
59 Verify captured packet
61 :param src_loc: source locator address
62 :param dst_loc: destination locator address
63 :param capture: list of captured packets
65 self.test.assertEqual(len(capture), 1, "Unexpected number of "
66 "packets! Expected 1 but {} received"
67 .format(len(capture)))
71 # assert the values match
72 self.test.assertEqual(ip_hdr.src, src_loc, "IP source address")
73 self.test.assertEqual(ip_hdr.dst, dst_loc,
74 "IP destination address")
75 gpe_hdr = packet[LISP_GPE_Header]
76 self.test.assertEqual(gpe_hdr.next_proto, 1,
77 "next_proto is not ipv4!")
79 self.test.assertEqual(ih.src, self.test.pg0.remote_ip4,
80 "unexpected source EID!")
81 self.test.assertEqual(ih.dst, self.test.deid_ip4,
82 "unexpected dest EID!")
84 self.test.logger.error(ppp("Unexpected or invalid packet:",
88 def configure_tc(self, tc):
89 for config_item in self.config_order:
90 for vpp_object in tc[config_item]:
91 vpp_object.add_vpp_config()
94 """ Send traffic for each test case and verify that it
96 for tc in enumerate(self.test_cases):
97 self.test.logger.info('Running {}'.format(tc[1]['name']))
98 self.configure_tc(tc[1])
100 packet = self.create_packet(self.test.pg0, self.test.pg1, dest,
102 self.test.pg0.add_stream(packet)
103 self.test.pg0.enable_capture()
104 self.test.pg1.enable_capture()
106 capture = self.test.pg1.get_capture(1)
107 self.verify_capture(self.test.pg1.local_ip4,
108 self.test.pg1.remote_ip4, capture)
109 self.test.pg0.assert_nothing_captured()
112 class TestLisp(VppTestCase):
113 """ Basic LISP test """
117 super(TestLisp, cls).setUpClass()
118 cls.faf = ForeignAddressFactory()
119 cls.create_pg_interfaces(range(2)) # create pg0 and pg1
120 for i in cls.pg_interfaces:
121 i.admin_up() # put the interface upsrc_if
122 i.config_ip4() # configure IPv4 address on the interface
123 i.resolve_arp() # resolve ARP, so that we know VPP MAC
126 super(TestLisp, self).setUp()
127 self.vapi.lisp_enable_disable(is_enabled=1)
129 def test_lisp_basic_encap(self):
130 """Test case for basic encapsulation"""
132 self.deid_ip4_net = self.faf.net
133 self.deid_ip4 = self.faf.get_ip4()
134 self.seid_ip4 = '{}/{}'.format(self.pg0.local_ip4, 32)
135 self.rloc_ip4 = self.pg1.remote_ip4n
139 'name': 'basic ip4 over ip4',
140 'locator-sets': [VppLispLocatorSet(self, 'ls-4o4')],
142 VppLispLocator(self, self.pg1.sw_if_index, 'ls-4o4')
145 VppLocalMapping(self, self.seid_ip4, 'ls-4o4')
148 VppRemoteMapping(self, self.deid_ip4_net,
153 "addr": self.rloc_ip4
157 VppLispAdjacency(self, self.seid_ip4, self.deid_ip4_net)
161 self.test_driver = SimpleDriver(self, test_cases)
162 self.test_driver.run(self.deid_ip4)
165 if __name__ == '__main__':
166 unittest.main(testRunner=VppTestRunner)