blob: edc316ea7b26d42bef130810029b2d0a2fe9dec2 [file] [log] [blame]
Renato Botelho do Coutoead1e532019-10-31 13:31:07 -05001#!/usr/bin/env python3
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -08002
3import abc
Filip Tehlar770e89e2017-01-31 10:39:16 +01004import unittest
5
Paul Vinciguerra4a144b42018-11-23 03:20:21 -08006from scapy.fields import BitField, ByteField, FlagsField, IntField
7from scapy.packet import bind_layers, Packet, Raw
Filip Tehlar770e89e2017-01-31 10:39:16 +01008from scapy.layers.inet import IP, UDP, Ether
Paul Vinciguerra4a144b42018-11-23 03:20:21 -08009from scapy.layers.inet6 import IPv6
Filip Tehlar770e89e2017-01-31 10:39:16 +010010
Dave Wallace8800f732023-08-31 00:47:44 -040011from framework import VppTestCase
12from asfframework import VppTestRunner
13from lisp import (
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020014 VppLocalMapping,
15 VppLispAdjacency,
16 VppLispLocator,
17 VppLispLocatorSet,
18 VppRemoteMapping,
19 LispRemoteLocator,
20)
Paul Vinciguerra1135f652020-12-04 17:50:26 -050021from util import ppp
Paul Vinciguerra4a144b42018-11-23 03:20:21 -080022
23# From py_lispnetworking.lisp.py: # GNU General Public License v2.0
24
25
26class LISP_GPE_Header(Packet):
27 name = "LISP GPE Header"
28 fields_desc = [
29 FlagsField("gpe_flags", None, 6, ["N", "L", "E", "V", "I", "P"]),
30 BitField("reserved", 0, 18),
31 ByteField("next_proto", 0),
32 IntField("iid", 0),
33 ]
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020034
35
Paul Vinciguerra4a144b42018-11-23 03:20:21 -080036bind_layers(UDP, LISP_GPE_Header, dport=4341)
37bind_layers(UDP, LISP_GPE_Header, sport=4341)
38bind_layers(LISP_GPE_Header, IP, next_proto=1)
39bind_layers(LISP_GPE_Header, IPv6, next_proto=2)
40bind_layers(LISP_GPE_Header, Ether, next_proto=3)
Filip Tehlar770e89e2017-01-31 10:39:16 +010041
42
Paul Vinciguerra1135f652020-12-04 17:50:26 -050043class ForeignAddressFactory(object):
44 count = 0
45 prefix_len = 24
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020046 net_template = "10.10.10.{}"
47 net = net_template.format(0) + "/" + str(prefix_len)
Paul Vinciguerra1135f652020-12-04 17:50:26 -050048
49 def get_ip4(self):
50 if self.count > 255:
51 raise Exception("Network host address exhaustion")
52 self.count += 1
53 return self.net_template.format(self.count)
54
55
Paul Vinciguerra090096b2020-12-03 00:42:46 -050056class Driver(metaclass=abc.ABCMeta):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020057 config_order = [
58 "locator-sets",
59 "locators",
60 "local-mappings",
61 "remote-mappings",
62 "adjacencies",
63 ]
Filip Tehlar770e89e2017-01-31 10:39:16 +010064
65 """ Basic class for data driven testing """
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020066
Filip Tehlar770e89e2017-01-31 10:39:16 +010067 def __init__(self, test, test_cases):
68 self._test_cases = test_cases
69 self._test = test
70
71 @property
72 def test_cases(self):
73 return self._test_cases
74
75 @property
76 def test(self):
77 return self._test
78
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020079 def create_packet(self, src_if, dst_if, deid, payload=""):
Filip Tehlar770e89e2017-01-31 10:39:16 +010080 """
81 Create IPv4 packet
82
83 param: src_if
84 param: dst_if
85 """
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020086 packet = (
87 Ether(dst=src_if.local_mac, src=src_if.remote_mac)
88 / IP(src=src_if.remote_ip4, dst=deid)
89 / Raw(payload)
90 )
Filip Tehlar770e89e2017-01-31 10:39:16 +010091 return packet
92
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -080093 @abc.abstractmethod
Filip Tehlar770e89e2017-01-31 10:39:16 +010094 def run(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020095 """testing procedure"""
Filip Tehlar770e89e2017-01-31 10:39:16 +010096 pass
97
98
99class SimpleDriver(Driver):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200100 """Implements simple test procedure"""
101
Filip Tehlar770e89e2017-01-31 10:39:16 +0100102 def __init__(self, test, test_cases):
103 super(SimpleDriver, self).__init__(test, test_cases)
104
105 def verify_capture(self, src_loc, dst_loc, capture):
106 """
107 Verify captured packet
108
109 :param src_loc: source locator address
110 :param dst_loc: destination locator address
111 :param capture: list of captured packets
112 """
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200113 self.test.assertEqual(
114 len(capture),
115 1,
116 "Unexpected number of "
117 "packets! Expected 1 but {} received".format(len(capture)),
118 )
Filip Tehlar770e89e2017-01-31 10:39:16 +0100119 packet = capture[0]
120 try:
121 ip_hdr = packet[IP]
122 # assert the values match
123 self.test.assertEqual(ip_hdr.src, src_loc, "IP source address")
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200124 self.test.assertEqual(ip_hdr.dst, dst_loc, "IP destination address")
Filip Tehlar770e89e2017-01-31 10:39:16 +0100125 gpe_hdr = packet[LISP_GPE_Header]
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200126 self.test.assertEqual(gpe_hdr.next_proto, 1, "next_proto is not ipv4!")
Filip Tehlar770e89e2017-01-31 10:39:16 +0100127 ih = gpe_hdr[IP]
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200128 self.test.assertEqual(
129 ih.src, self.test.pg0.remote_ip4, "unexpected source EID!"
130 )
131 self.test.assertEqual(ih.dst, self.test.deid_ip4, "unexpected dest EID!")
Filip Tehlar770e89e2017-01-31 10:39:16 +0100132 except:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200133 self.test.logger.error(ppp("Unexpected or invalid packet:", packet))
Filip Tehlar770e89e2017-01-31 10:39:16 +0100134 raise
135
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()
140
141 def run(self, dest):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200142 """Send traffic for each test case and verify that it
143 is encapsulated"""
Filip Tehlar770e89e2017-01-31 10:39:16 +0100144 for tc in enumerate(self.test_cases):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200145 self.test.logger.info("Running {}".format(tc[1]["name"]))
Filip Tehlar770e89e2017-01-31 10:39:16 +0100146 self.configure_tc(tc[1])
147
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200148 packet = self.create_packet(self.test.pg0, self.test.pg1, dest, "data")
Filip Tehlar770e89e2017-01-31 10:39:16 +0100149 self.test.pg0.add_stream(packet)
150 self.test.pg0.enable_capture()
151 self.test.pg1.enable_capture()
152 self.test.pg_start()
153 capture = self.test.pg1.get_capture(1)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200154 self.verify_capture(
155 self.test.pg1.local_ip4, self.test.pg1.remote_ip4, capture
156 )
Filip Tehlar770e89e2017-01-31 10:39:16 +0100157 self.test.pg0.assert_nothing_captured()
158
159
160class TestLisp(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200161 """Basic LISP test"""
Filip Tehlar770e89e2017-01-31 10:39:16 +0100162
163 @classmethod
164 def setUpClass(cls):
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
172
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -0700173 @classmethod
174 def tearDownClass(cls):
175 super(TestLisp, cls).tearDownClass()
176
Filip Tehlar770e89e2017-01-31 10:39:16 +0100177 def setUp(self):
178 super(TestLisp, self).setUp()
Jakub Grajciar58db6e12020-01-30 13:26:43 +0100179 self.vapi.lisp_enable_disable(is_enable=1)
Filip Tehlar770e89e2017-01-31 10:39:16 +0100180
181 def test_lisp_basic_encap(self):
182 """Test case for basic encapsulation"""
183
184 self.deid_ip4_net = self.faf.net
185 self.deid_ip4 = self.faf.get_ip4()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200186 self.seid_ip4 = "{!s}/{!s}".format(self.pg0.local_ip4, 32)
Jakub Grajciar58db6e12020-01-30 13:26:43 +0100187 self.rloc_ip4 = self.pg1.remote_ip4
Filip Tehlar770e89e2017-01-31 10:39:16 +0100188
189 test_cases = [
190 {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200191 "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")],
195 "remote-mappings": [
196 VppRemoteMapping(
197 self, self.deid_ip4_net, [LispRemoteLocator(self.rloc_ip4)]
198 )
Filip Tehlar770e89e2017-01-31 10:39:16 +0100199 ],
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200200 "adjacencies": [
Filip Tehlar770e89e2017-01-31 10:39:16 +0100201 VppLispAdjacency(self, self.seid_ip4, self.deid_ip4_net)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200202 ],
Filip Tehlar770e89e2017-01-31 10:39:16 +0100203 }
204 ]
205 self.test_driver = SimpleDriver(self, test_cases)
206 self.test_driver.run(self.deid_ip4)
207
208
Neale Rannsea93e482019-11-12 17:16:47 +0000209class TestLispUT(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200210 """Lisp UT"""
Neale Rannsea93e482019-11-12 17:16:47 +0000211
212 @classmethod
213 def setUpClass(cls):
214 super(TestLispUT, cls).setUpClass()
215
216 @classmethod
217 def tearDownClass(cls):
218 super(TestLispUT, cls).tearDownClass()
219
220 def test_fib(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200221 """LISP Unit Tests"""
Neale Rannsea93e482019-11-12 17:16:47 +0000222 error = self.vapi.cli("test lisp cp")
223
224 if error:
225 self.logger.critical(error)
226 self.assertNotIn("Failed", error)
227
228
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200229if __name__ == "__main__":
Filip Tehlar770e89e2017-01-31 10:39:16 +0100230 unittest.main(testRunner=VppTestRunner)