Renato Botelho do Couto | ead1e53 | 2019-10-31 13:31:07 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 2 | """CRUD tests of APIs (Create, Read, Update, Delete) HLD: |
| 3 | |
| 4 | - interface up/down/add/delete - interface type: |
| 5 | - pg (TBD) |
| 6 | - loopback |
| 7 | - vhostuser (TBD) |
| 8 | - af_packet (TBD) |
| 9 | - netmap (TBD) |
| 10 | - tuntap (root privileges needed) |
| 11 | - vxlan (TBD) |
| 12 | """ |
| 13 | |
| 14 | import unittest |
| 15 | |
| 16 | from scapy.layers.inet import IP, ICMP |
| 17 | from scapy.layers.l2 import Ether |
| 18 | |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 19 | from framework import VppTestCase |
| 20 | from asfframework import VppTestRunner |
Dmitry Valter | 34fa0ce | 2024-03-11 10:38:46 +0000 | [diff] [blame] | 21 | from config import config |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 22 | |
| 23 | |
| 24 | class TestLoopbackInterfaceCRUD(VppTestCase): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 25 | """CRUD Loopback""" |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 26 | |
| 27 | @classmethod |
| 28 | def setUpClass(cls): |
| 29 | super(TestLoopbackInterfaceCRUD, cls).setUpClass() |
| 30 | try: |
| 31 | cls.create_pg_interfaces(range(1)) |
| 32 | for i in cls.pg_interfaces: |
Paul Vinciguerra | 207083f | 2019-12-02 22:38:00 -0500 | [diff] [blame] | 33 | i.config_ip4().resolve_arp() |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 34 | except: |
| 35 | cls.tearDownClass() |
| 36 | raise |
| 37 | |
Paul Vinciguerra | 7f9b7f9 | 2019-03-12 19:23:27 -0700 | [diff] [blame] | 38 | @classmethod |
| 39 | def tearDownClass(cls): |
| 40 | super(TestLoopbackInterfaceCRUD, cls).tearDownClass() |
| 41 | |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 42 | @staticmethod |
| 43 | def create_icmp_stream(src_if, dst_ifs): |
| 44 | """ |
| 45 | |
| 46 | :param VppInterface src_if: Packets are send to this interface, |
| 47 | using this interfaces remote host. |
| 48 | :param list dst_ifs: IPv4 ICMP requests are send to interfaces |
| 49 | addresses. |
| 50 | :return: List of generated packets. |
| 51 | """ |
| 52 | pkts = [] |
| 53 | for i in dst_ifs: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 54 | p = ( |
| 55 | Ether(dst=src_if.local_mac, src=src_if.remote_mac) |
| 56 | / IP(src=src_if.remote_ip4, dst=i.local_ip4) |
| 57 | / ICMP(id=i.sw_if_index, type="echo-request") |
| 58 | ) |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 59 | pkts.append(p) |
| 60 | return pkts |
| 61 | |
| 62 | def verify_icmp(self, capture, request_src_if, dst_ifs): |
| 63 | """ |
| 64 | |
| 65 | :param capture: Capture to verify. |
| 66 | :param VppInterface request_src_if: Interface where was send packets. |
| 67 | :param list dst_ifs: Interfaces where was generated IPv4 ICMP requests. |
| 68 | """ |
| 69 | rcvd_icmp_pkts = [] |
| 70 | for pkt in capture: |
| 71 | try: |
| 72 | ip = pkt[IP] |
| 73 | icmp = pkt[ICMP] |
| 74 | except IndexError: |
| 75 | pass |
| 76 | else: |
| 77 | info = (ip.src, ip.dst, icmp.type, icmp.id) |
| 78 | rcvd_icmp_pkts.append(info) |
| 79 | |
| 80 | for i in dst_ifs: |
| 81 | # 0 - icmp echo response |
| 82 | info = (i.local_ip4, request_src_if.remote_ip4, 0, i.sw_if_index) |
| 83 | self.assertIn(info, rcvd_icmp_pkts) |
| 84 | |
Dmitry Valter | 34fa0ce | 2024-03-11 10:38:46 +0000 | [diff] [blame] | 85 | @unittest.skipIf( |
| 86 | "ping" in config.excluded_plugins, "Exclude tests requiring Ping plugin" |
| 87 | ) |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 88 | def test_crud(self): |
| 89 | # create |
Klement Sekera | b9ef273 | 2018-06-24 22:49:33 +0200 | [diff] [blame] | 90 | loopbacks = self.create_loopback_interfaces(20) |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 91 | for i in loopbacks: |
Neale Ranns | efd7bc2 | 2019-11-11 08:32:34 +0000 | [diff] [blame] | 92 | i.local_ip4_prefix_len = 32 |
Paul Vinciguerra | 207083f | 2019-12-02 22:38:00 -0500 | [diff] [blame] | 93 | i.config_ip4().admin_up() |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 94 | |
| 95 | # read (check sw if dump, ip4 fib, ip6 fib) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 96 | if_dump = self.vapi.sw_interface_dump( |
| 97 | name_filter_valid=True, name_filter="loop" |
| 98 | ) |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 99 | fib4_dump = self.vapi.ip_route_dump(0) |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 100 | for i in loopbacks: |
| 101 | self.assertTrue(i.is_interface_config_in_dump(if_dump)) |
| 102 | self.assertTrue(i.is_ip4_entry_in_fib_dump(fib4_dump)) |
| 103 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 104 | if_dump = self.vapi.sw_interface_dump( |
| 105 | name_filter_valid=True, name_filter="loopXYZ" |
| 106 | ) |
Aleksander Djuric | c12eae7 | 2019-10-31 14:35:21 +0300 | [diff] [blame] | 107 | self.assertEqual(len(if_dump), 0) |
| 108 | |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 109 | # check ping |
| 110 | stream = self.create_icmp_stream(self.pg0, loopbacks) |
| 111 | self.pg0.add_stream(stream) |
| 112 | self.pg_enable_capture(self.pg_interfaces) |
| 113 | self.pg_start() |
| 114 | capture = self.pg0.get_capture(expected_count=len(stream)) |
| 115 | |
| 116 | self.verify_icmp(capture, self.pg0, loopbacks) |
| 117 | |
| 118 | # delete |
| 119 | for i in loopbacks: |
| 120 | i.remove_vpp_config() |
| 121 | |
| 122 | # read (check not in sw if dump, ip4 fib, ip6 fib) |
| 123 | if_dump = self.vapi.sw_interface_dump() |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 124 | fib4_dump = self.vapi.ip_route_dump(0) |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 125 | for i in loopbacks: |
| 126 | self.assertFalse(i.is_interface_config_in_dump(if_dump)) |
| 127 | self.assertFalse(i.is_ip4_entry_in_fib_dump(fib4_dump)) |
| 128 | |
| 129 | # check not ping |
| 130 | stream = self.create_icmp_stream(self.pg0, loopbacks) |
| 131 | self.pg0.add_stream(stream) |
| 132 | self.pg_enable_capture(self.pg_interfaces) |
| 133 | self.pg_start() |
| 134 | self.pg0.assert_nothing_captured() |
| 135 | |
| 136 | def test_down(self): |
| 137 | # create |
Klement Sekera | b9ef273 | 2018-06-24 22:49:33 +0200 | [diff] [blame] | 138 | loopbacks = self.create_loopback_interfaces(20) |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 139 | for i in loopbacks: |
Neale Ranns | efd7bc2 | 2019-11-11 08:32:34 +0000 | [diff] [blame] | 140 | i.local_ip4_prefix_len = 32 |
Paul Vinciguerra | 207083f | 2019-12-02 22:38:00 -0500 | [diff] [blame] | 141 | i.config_ip4().admin_up() |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 142 | |
| 143 | # disable |
| 144 | for i in loopbacks: |
Paul Vinciguerra | 207083f | 2019-12-02 22:38:00 -0500 | [diff] [blame] | 145 | i.admin_down().unconfig_ip4() |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 146 | |
| 147 | # read (check not in sw if dump, ip4 fib, ip6 fib) |
| 148 | if_dump = self.vapi.sw_interface_dump() |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 149 | fib4_dump = self.vapi.ip_route_dump(0) |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 150 | for i in loopbacks: |
| 151 | self.assertTrue(i.is_interface_config_in_dump(if_dump)) |
| 152 | self.assertFalse(i.is_ip4_entry_in_fib_dump(fib4_dump)) |
| 153 | |
| 154 | # check not ping |
| 155 | stream = self.create_icmp_stream(self.pg0, loopbacks) |
| 156 | self.pg0.add_stream(stream) |
| 157 | self.pg_enable_capture(self.pg_interfaces) |
| 158 | self.pg_start() |
| 159 | self.pg0.assert_nothing_captured() |
| 160 | |
| 161 | |
Paul Vinciguerra | 6407ba5 | 2019-04-04 13:22:20 -0700 | [diff] [blame] | 162 | class TestInterfaceDumpApiLocalOnly(VppTestCase): |
| 163 | """test_interface_crud.TestInterfaceDumpApiLocalOnly""" |
| 164 | |
| 165 | def test_sw_if_index_0(self): |
| 166 | rv = self.vapi.sw_interface_dump(sw_if_index=0) |
| 167 | self.assertEqual(rv[0].sw_if_index, 0) |
| 168 | |
| 169 | def test_sw_if_index_twiddle0(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 170 | rv = self.vapi.sw_interface_dump(sw_if_index=0xFFFFFFFF) |
Paul Vinciguerra | 6407ba5 | 2019-04-04 13:22:20 -0700 | [diff] [blame] | 171 | self.assertEqual(rv[0].sw_if_index, 0) |
| 172 | |
| 173 | def test_sw_if_index_1_not_existing(self): |
| 174 | rv = self.vapi.sw_interface_dump(sw_if_index=1) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 175 | self.assertEqual(len(rv), 0, "expected no records.") |
Paul Vinciguerra | 6407ba5 | 2019-04-04 13:22:20 -0700 | [diff] [blame] | 176 | |
| 177 | |
| 178 | class TestInterfaceDumpApi(VppTestCase): |
| 179 | """test_interface_crud.TestInterfaceDumpApi""" |
| 180 | |
| 181 | def test_sw_if_index_1(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 182 | self.vapi.create_loopback_instance(is_specified=1, user_instance=10) |
| 183 | self.vapi.create_loopback_instance(is_specified=1, user_instance=5) |
Paul Vinciguerra | 6407ba5 | 2019-04-04 13:22:20 -0700 | [diff] [blame] | 184 | |
| 185 | # Can I get back the specified record? |
| 186 | rv = self.vapi.sw_interface_dump(sw_if_index=1) |
| 187 | self.assertEqual(rv[0].sw_if_index, 1, rv) |
| 188 | |
| 189 | # verify 3 interfaces |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 190 | rv = self.vapi.sw_interface_dump(sw_if_index=0xFFFFFFFF) |
| 191 | self.assertEqual(len(rv), 3, "Expected 3 interfaces.") |
Paul Vinciguerra | 6407ba5 | 2019-04-04 13:22:20 -0700 | [diff] [blame] | 192 | |
Paul Vinciguerra | 207083f | 2019-12-02 22:38:00 -0500 | [diff] [blame] | 193 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 194 | if __name__ == "__main__": |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 195 | unittest.main(testRunner=VppTestRunner) |