Renato Botelho do Couto | ead1e53 | 2019-10-31 13:31:07 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Paul Vinciguerra | 3bce8eb | 2018-11-24 21:46:05 -0800 | [diff] [blame] | 2 | |
| 3 | import abc |
Filip Tehlar | 770e89e | 2017-01-31 10:39:16 +0100 | [diff] [blame] | 4 | import unittest |
| 5 | |
Paul Vinciguerra | 4a144b4 | 2018-11-23 03:20:21 -0800 | [diff] [blame] | 6 | from scapy.fields import BitField, ByteField, FlagsField, IntField |
| 7 | from scapy.packet import bind_layers, Packet, Raw |
Filip Tehlar | 770e89e | 2017-01-31 10:39:16 +0100 | [diff] [blame] | 8 | from scapy.layers.inet import IP, UDP, Ether |
Paul Vinciguerra | 4a144b4 | 2018-11-23 03:20:21 -0800 | [diff] [blame] | 9 | from scapy.layers.inet6 import IPv6 |
Filip Tehlar | 770e89e | 2017-01-31 10:39:16 +0100 | [diff] [blame] | 10 | |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 11 | from framework import VppTestCase |
| 12 | from asfframework import VppTestRunner |
| 13 | from lisp import ( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 14 | VppLocalMapping, |
| 15 | VppLispAdjacency, |
| 16 | VppLispLocator, |
| 17 | VppLispLocatorSet, |
| 18 | VppRemoteMapping, |
| 19 | LispRemoteLocator, |
| 20 | ) |
Paul Vinciguerra | 1135f65 | 2020-12-04 17:50:26 -0500 | [diff] [blame] | 21 | from util import ppp |
Dmitry Valter | 34fa0ce | 2024-03-11 10:38:46 +0000 | [diff] [blame] | 22 | from config import config |
Paul Vinciguerra | 4a144b4 | 2018-11-23 03:20:21 -0800 | [diff] [blame] | 23 | |
| 24 | # From py_lispnetworking.lisp.py: # GNU General Public License v2.0 |
| 25 | |
| 26 | |
| 27 | class 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 35 | |
| 36 | |
Paul Vinciguerra | 4a144b4 | 2018-11-23 03:20:21 -0800 | [diff] [blame] | 37 | bind_layers(UDP, LISP_GPE_Header, dport=4341) |
| 38 | bind_layers(UDP, LISP_GPE_Header, sport=4341) |
| 39 | bind_layers(LISP_GPE_Header, IP, next_proto=1) |
| 40 | bind_layers(LISP_GPE_Header, IPv6, next_proto=2) |
| 41 | bind_layers(LISP_GPE_Header, Ether, next_proto=3) |
Filip Tehlar | 770e89e | 2017-01-31 10:39:16 +0100 | [diff] [blame] | 42 | |
| 43 | |
Paul Vinciguerra | 1135f65 | 2020-12-04 17:50:26 -0500 | [diff] [blame] | 44 | class ForeignAddressFactory(object): |
| 45 | count = 0 |
| 46 | prefix_len = 24 |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 47 | net_template = "10.10.10.{}" |
| 48 | net = net_template.format(0) + "/" + str(prefix_len) |
Paul Vinciguerra | 1135f65 | 2020-12-04 17:50:26 -0500 | [diff] [blame] | 49 | |
| 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 Vinciguerra | 090096b | 2020-12-03 00:42:46 -0500 | [diff] [blame] | 57 | class Driver(metaclass=abc.ABCMeta): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 58 | config_order = [ |
| 59 | "locator-sets", |
| 60 | "locators", |
| 61 | "local-mappings", |
| 62 | "remote-mappings", |
| 63 | "adjacencies", |
| 64 | ] |
Filip Tehlar | 770e89e | 2017-01-31 10:39:16 +0100 | [diff] [blame] | 65 | |
| 66 | """ Basic class for data driven testing """ |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 67 | |
Filip Tehlar | 770e89e | 2017-01-31 10:39:16 +0100 | [diff] [blame] | 68 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 80 | def create_packet(self, src_if, dst_if, deid, payload=""): |
Filip Tehlar | 770e89e | 2017-01-31 10:39:16 +0100 | [diff] [blame] | 81 | """ |
| 82 | Create IPv4 packet |
| 83 | |
| 84 | param: src_if |
| 85 | param: dst_if |
| 86 | """ |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 87 | 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 Tehlar | 770e89e | 2017-01-31 10:39:16 +0100 | [diff] [blame] | 92 | return packet |
| 93 | |
Paul Vinciguerra | 3bce8eb | 2018-11-24 21:46:05 -0800 | [diff] [blame] | 94 | @abc.abstractmethod |
Filip Tehlar | 770e89e | 2017-01-31 10:39:16 +0100 | [diff] [blame] | 95 | def run(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 96 | """testing procedure""" |
Filip Tehlar | 770e89e | 2017-01-31 10:39:16 +0100 | [diff] [blame] | 97 | pass |
| 98 | |
| 99 | |
| 100 | class SimpleDriver(Driver): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 101 | """Implements simple test procedure""" |
| 102 | |
Filip Tehlar | 770e89e | 2017-01-31 10:39:16 +0100 | [diff] [blame] | 103 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 114 | self.test.assertEqual( |
| 115 | len(capture), |
| 116 | 1, |
| 117 | "Unexpected number of " |
| 118 | "packets! Expected 1 but {} received".format(len(capture)), |
| 119 | ) |
Filip Tehlar | 770e89e | 2017-01-31 10:39:16 +0100 | [diff] [blame] | 120 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 125 | self.test.assertEqual(ip_hdr.dst, dst_loc, "IP destination address") |
Filip Tehlar | 770e89e | 2017-01-31 10:39:16 +0100 | [diff] [blame] | 126 | gpe_hdr = packet[LISP_GPE_Header] |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 127 | self.test.assertEqual(gpe_hdr.next_proto, 1, "next_proto is not ipv4!") |
Filip Tehlar | 770e89e | 2017-01-31 10:39:16 +0100 | [diff] [blame] | 128 | ih = gpe_hdr[IP] |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 129 | 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 Tehlar | 770e89e | 2017-01-31 10:39:16 +0100 | [diff] [blame] | 133 | except: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 134 | self.test.logger.error(ppp("Unexpected or invalid packet:", packet)) |
Filip Tehlar | 770e89e | 2017-01-31 10:39:16 +0100 | [diff] [blame] | 135 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 143 | """Send traffic for each test case and verify that it |
| 144 | is encapsulated""" |
Filip Tehlar | 770e89e | 2017-01-31 10:39:16 +0100 | [diff] [blame] | 145 | for tc in enumerate(self.test_cases): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 146 | self.test.logger.info("Running {}".format(tc[1]["name"])) |
Filip Tehlar | 770e89e | 2017-01-31 10:39:16 +0100 | [diff] [blame] | 147 | self.configure_tc(tc[1]) |
| 148 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 149 | packet = self.create_packet(self.test.pg0, self.test.pg1, dest, "data") |
Filip Tehlar | 770e89e | 2017-01-31 10:39:16 +0100 | [diff] [blame] | 150 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 155 | self.verify_capture( |
| 156 | self.test.pg1.local_ip4, self.test.pg1.remote_ip4, capture |
| 157 | ) |
Filip Tehlar | 770e89e | 2017-01-31 10:39:16 +0100 | [diff] [blame] | 158 | self.test.pg0.assert_nothing_captured() |
| 159 | |
| 160 | |
Dmitry Valter | 34fa0ce | 2024-03-11 10:38:46 +0000 | [diff] [blame] | 161 | @unittest.skipIf("lisp" in config.excluded_plugins, "Exclude LISP plugin tests") |
Filip Tehlar | 770e89e | 2017-01-31 10:39:16 +0100 | [diff] [blame] | 162 | class TestLisp(VppTestCase): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 163 | """Basic LISP test""" |
Filip Tehlar | 770e89e | 2017-01-31 10:39:16 +0100 | [diff] [blame] | 164 | |
| 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 Vinciguerra | 7f9b7f9 | 2019-03-12 19:23:27 -0700 | [diff] [blame] | 175 | @classmethod |
| 176 | def tearDownClass(cls): |
| 177 | super(TestLisp, cls).tearDownClass() |
| 178 | |
Filip Tehlar | 770e89e | 2017-01-31 10:39:16 +0100 | [diff] [blame] | 179 | def setUp(self): |
| 180 | super(TestLisp, self).setUp() |
Jakub Grajciar | 58db6e1 | 2020-01-30 13:26:43 +0100 | [diff] [blame] | 181 | self.vapi.lisp_enable_disable(is_enable=1) |
Filip Tehlar | 770e89e | 2017-01-31 10:39:16 +0100 | [diff] [blame] | 182 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 188 | self.seid_ip4 = "{!s}/{!s}".format(self.pg0.local_ip4, 32) |
Jakub Grajciar | 58db6e1 | 2020-01-30 13:26:43 +0100 | [diff] [blame] | 189 | self.rloc_ip4 = self.pg1.remote_ip4 |
Filip Tehlar | 770e89e | 2017-01-31 10:39:16 +0100 | [diff] [blame] | 190 | |
| 191 | test_cases = [ |
| 192 | { |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 193 | "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 Tehlar | 770e89e | 2017-01-31 10:39:16 +0100 | [diff] [blame] | 201 | ], |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 202 | "adjacencies": [ |
Filip Tehlar | 770e89e | 2017-01-31 10:39:16 +0100 | [diff] [blame] | 203 | VppLispAdjacency(self, self.seid_ip4, self.deid_ip4_net) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 204 | ], |
Filip Tehlar | 770e89e | 2017-01-31 10:39:16 +0100 | [diff] [blame] | 205 | } |
| 206 | ] |
| 207 | self.test_driver = SimpleDriver(self, test_cases) |
| 208 | self.test_driver.run(self.deid_ip4) |
| 209 | |
| 210 | |
Dmitry Valter | 34fa0ce | 2024-03-11 10:38:46 +0000 | [diff] [blame] | 211 | @unittest.skipIf("lisp" in config.excluded_plugins, "Exclude LISP plugin tests") |
Neale Ranns | ea93e48 | 2019-11-12 17:16:47 +0000 | [diff] [blame] | 212 | class TestLispUT(VppTestCase): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 213 | """Lisp UT""" |
Neale Ranns | ea93e48 | 2019-11-12 17:16:47 +0000 | [diff] [blame] | 214 | |
| 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 224 | """LISP Unit Tests""" |
Neale Ranns | ea93e48 | 2019-11-12 17:16:47 +0000 | [diff] [blame] | 225 | error = self.vapi.cli("test lisp cp") |
| 226 | |
| 227 | if error: |
| 228 | self.logger.critical(error) |
| 229 | self.assertNotIn("Failed", error) |
| 230 | |
| 231 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 232 | if __name__ == "__main__": |
Filip Tehlar | 770e89e | 2017-01-31 10:39:16 +0100 | [diff] [blame] | 233 | unittest.main(testRunner=VppTestRunner) |