blob: 7b6560eb3c2027d8c9441070e36046dac7922f4e [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
Dmitry Valter34fa0ce2024-03-11 10:38:46 +000022from config import config
Paul Vinciguerra4a144b42018-11-23 03:20:21 -080023
24# From py_lispnetworking.lisp.py: # GNU General Public License v2.0
25
26
27class LISP_GPE_Header(Packet):
28 name = "LISP GPE Header"
29 fields_desc = [
30 FlagsField("gpe_flags", None, 6, ["N", "L", "E", "V", "I", "P"]),
31 BitField("reserved", 0, 18),
32 ByteField("next_proto", 0),
33 IntField("iid", 0),
34 ]
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020035
36
Paul Vinciguerra4a144b42018-11-23 03:20:21 -080037bind_layers(UDP, LISP_GPE_Header, dport=4341)
38bind_layers(UDP, LISP_GPE_Header, sport=4341)
39bind_layers(LISP_GPE_Header, IP, next_proto=1)
40bind_layers(LISP_GPE_Header, IPv6, next_proto=2)
41bind_layers(LISP_GPE_Header, Ether, next_proto=3)
Filip Tehlar770e89e2017-01-31 10:39:16 +010042
43
Paul Vinciguerra1135f652020-12-04 17:50:26 -050044class ForeignAddressFactory(object):
45 count = 0
46 prefix_len = 24
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020047 net_template = "10.10.10.{}"
48 net = net_template.format(0) + "/" + str(prefix_len)
Paul Vinciguerra1135f652020-12-04 17:50:26 -050049
50 def get_ip4(self):
51 if self.count > 255:
52 raise Exception("Network host address exhaustion")
53 self.count += 1
54 return self.net_template.format(self.count)
55
56
Paul Vinciguerra090096b2020-12-03 00:42:46 -050057class Driver(metaclass=abc.ABCMeta):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020058 config_order = [
59 "locator-sets",
60 "locators",
61 "local-mappings",
62 "remote-mappings",
63 "adjacencies",
64 ]
Filip Tehlar770e89e2017-01-31 10:39:16 +010065
66 """ Basic class for data driven testing """
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020067
Filip Tehlar770e89e2017-01-31 10:39:16 +010068 def __init__(self, test, test_cases):
69 self._test_cases = test_cases
70 self._test = test
71
72 @property
73 def test_cases(self):
74 return self._test_cases
75
76 @property
77 def test(self):
78 return self._test
79
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020080 def create_packet(self, src_if, dst_if, deid, payload=""):
Filip Tehlar770e89e2017-01-31 10:39:16 +010081 """
82 Create IPv4 packet
83
84 param: src_if
85 param: dst_if
86 """
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020087 packet = (
88 Ether(dst=src_if.local_mac, src=src_if.remote_mac)
89 / IP(src=src_if.remote_ip4, dst=deid)
90 / Raw(payload)
91 )
Filip Tehlar770e89e2017-01-31 10:39:16 +010092 return packet
93
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -080094 @abc.abstractmethod
Filip Tehlar770e89e2017-01-31 10:39:16 +010095 def run(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020096 """testing procedure"""
Filip Tehlar770e89e2017-01-31 10:39:16 +010097 pass
98
99
100class SimpleDriver(Driver):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200101 """Implements simple test procedure"""
102
Filip Tehlar770e89e2017-01-31 10:39:16 +0100103 def __init__(self, test, test_cases):
104 super(SimpleDriver, self).__init__(test, test_cases)
105
106 def verify_capture(self, src_loc, dst_loc, capture):
107 """
108 Verify captured packet
109
110 :param src_loc: source locator address
111 :param dst_loc: destination locator address
112 :param capture: list of captured packets
113 """
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200114 self.test.assertEqual(
115 len(capture),
116 1,
117 "Unexpected number of "
118 "packets! Expected 1 but {} received".format(len(capture)),
119 )
Filip Tehlar770e89e2017-01-31 10:39:16 +0100120 packet = capture[0]
121 try:
122 ip_hdr = packet[IP]
123 # assert the values match
124 self.test.assertEqual(ip_hdr.src, src_loc, "IP source address")
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200125 self.test.assertEqual(ip_hdr.dst, dst_loc, "IP destination address")
Filip Tehlar770e89e2017-01-31 10:39:16 +0100126 gpe_hdr = packet[LISP_GPE_Header]
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200127 self.test.assertEqual(gpe_hdr.next_proto, 1, "next_proto is not ipv4!")
Filip Tehlar770e89e2017-01-31 10:39:16 +0100128 ih = gpe_hdr[IP]
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200129 self.test.assertEqual(
130 ih.src, self.test.pg0.remote_ip4, "unexpected source EID!"
131 )
132 self.test.assertEqual(ih.dst, self.test.deid_ip4, "unexpected dest EID!")
Filip Tehlar770e89e2017-01-31 10:39:16 +0100133 except:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200134 self.test.logger.error(ppp("Unexpected or invalid packet:", packet))
Filip Tehlar770e89e2017-01-31 10:39:16 +0100135 raise
136
137 def configure_tc(self, tc):
138 for config_item in self.config_order:
139 for vpp_object in tc[config_item]:
140 vpp_object.add_vpp_config()
141
142 def run(self, dest):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200143 """Send traffic for each test case and verify that it
144 is encapsulated"""
Filip Tehlar770e89e2017-01-31 10:39:16 +0100145 for tc in enumerate(self.test_cases):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200146 self.test.logger.info("Running {}".format(tc[1]["name"]))
Filip Tehlar770e89e2017-01-31 10:39:16 +0100147 self.configure_tc(tc[1])
148
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200149 packet = self.create_packet(self.test.pg0, self.test.pg1, dest, "data")
Filip Tehlar770e89e2017-01-31 10:39:16 +0100150 self.test.pg0.add_stream(packet)
151 self.test.pg0.enable_capture()
152 self.test.pg1.enable_capture()
153 self.test.pg_start()
154 capture = self.test.pg1.get_capture(1)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200155 self.verify_capture(
156 self.test.pg1.local_ip4, self.test.pg1.remote_ip4, capture
157 )
Filip Tehlar770e89e2017-01-31 10:39:16 +0100158 self.test.pg0.assert_nothing_captured()
159
160
Dmitry Valter34fa0ce2024-03-11 10:38:46 +0000161@unittest.skipIf("lisp" in config.excluded_plugins, "Exclude LISP plugin tests")
Filip Tehlar770e89e2017-01-31 10:39:16 +0100162class TestLisp(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200163 """Basic LISP test"""
Filip Tehlar770e89e2017-01-31 10:39:16 +0100164
165 @classmethod
166 def setUpClass(cls):
167 super(TestLisp, cls).setUpClass()
168 cls.faf = ForeignAddressFactory()
169 cls.create_pg_interfaces(range(2)) # create pg0 and pg1
170 for i in cls.pg_interfaces:
171 i.admin_up() # put the interface upsrc_if
172 i.config_ip4() # configure IPv4 address on the interface
173 i.resolve_arp() # resolve ARP, so that we know VPP MAC
174
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -0700175 @classmethod
176 def tearDownClass(cls):
177 super(TestLisp, cls).tearDownClass()
178
Filip Tehlar770e89e2017-01-31 10:39:16 +0100179 def setUp(self):
180 super(TestLisp, self).setUp()
Jakub Grajciar58db6e12020-01-30 13:26:43 +0100181 self.vapi.lisp_enable_disable(is_enable=1)
Filip Tehlar770e89e2017-01-31 10:39:16 +0100182
183 def test_lisp_basic_encap(self):
184 """Test case for basic encapsulation"""
185
186 self.deid_ip4_net = self.faf.net
187 self.deid_ip4 = self.faf.get_ip4()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200188 self.seid_ip4 = "{!s}/{!s}".format(self.pg0.local_ip4, 32)
Jakub Grajciar58db6e12020-01-30 13:26:43 +0100189 self.rloc_ip4 = self.pg1.remote_ip4
Filip Tehlar770e89e2017-01-31 10:39:16 +0100190
191 test_cases = [
192 {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200193 "name": "basic ip4 over ip4",
194 "locator-sets": [VppLispLocatorSet(self, "ls-4o4")],
195 "locators": [VppLispLocator(self, self.pg1.sw_if_index, "ls-4o4")],
196 "local-mappings": [VppLocalMapping(self, self.seid_ip4, "ls-4o4")],
197 "remote-mappings": [
198 VppRemoteMapping(
199 self, self.deid_ip4_net, [LispRemoteLocator(self.rloc_ip4)]
200 )
Filip Tehlar770e89e2017-01-31 10:39:16 +0100201 ],
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200202 "adjacencies": [
Filip Tehlar770e89e2017-01-31 10:39:16 +0100203 VppLispAdjacency(self, self.seid_ip4, self.deid_ip4_net)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200204 ],
Filip Tehlar770e89e2017-01-31 10:39:16 +0100205 }
206 ]
207 self.test_driver = SimpleDriver(self, test_cases)
208 self.test_driver.run(self.deid_ip4)
209
210
Dmitry Valter34fa0ce2024-03-11 10:38:46 +0000211@unittest.skipIf("lisp" in config.excluded_plugins, "Exclude LISP plugin tests")
Neale Rannsea93e482019-11-12 17:16:47 +0000212class TestLispUT(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200213 """Lisp UT"""
Neale Rannsea93e482019-11-12 17:16:47 +0000214
215 @classmethod
216 def setUpClass(cls):
217 super(TestLispUT, cls).setUpClass()
218
219 @classmethod
220 def tearDownClass(cls):
221 super(TestLispUT, cls).tearDownClass()
222
223 def test_fib(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200224 """LISP Unit Tests"""
Neale Rannsea93e482019-11-12 17:16:47 +0000225 error = self.vapi.cli("test lisp cp")
226
227 if error:
228 self.logger.critical(error)
229 self.assertNotIn("Failed", error)
230
231
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200232if __name__ == "__main__":
Filip Tehlar770e89e2017-01-31 10:39:16 +0100233 unittest.main(testRunner=VppTestRunner)