Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 1 | from scapy.layers.inet import IP, ICMP |
Andrew Yourtchenko | e3d5280 | 2017-03-24 17:46:42 +0100 | [diff] [blame] | 2 | |
| 3 | from framework import VppTestCase |
Július Milan | 98874cd | 2021-02-16 19:20:47 +0100 | [diff] [blame] | 4 | from vpp_ip_route import VppIpInterfaceAddress, VppIpRoute, VppRoutePath |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 5 | from vpp_neighbor import VppNeighbor |
Dmitry Valter | 34fa0ce | 2024-03-11 10:38:46 +0000 | [diff] [blame] | 6 | from config import config |
| 7 | |
| 8 | import unittest |
Andrew Yourtchenko | e3d5280 | 2017-03-24 17:46:42 +0100 | [diff] [blame] | 9 | |
| 10 | """ TestPing is a subclass of VPPTestCase classes. |
| 11 | |
| 12 | Basic test for sanity check of the ping. |
| 13 | |
| 14 | """ |
| 15 | |
| 16 | |
Dmitry Valter | 34fa0ce | 2024-03-11 10:38:46 +0000 | [diff] [blame] | 17 | @unittest.skipIf("ping" in config.excluded_plugins, "Exclude Ping plugin tests") |
Andrew Yourtchenko | e3d5280 | 2017-03-24 17:46:42 +0100 | [diff] [blame] | 18 | class TestPing(VppTestCase): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 19 | """Ping Test Case""" |
Andrew Yourtchenko | e3d5280 | 2017-03-24 17:46:42 +0100 | [diff] [blame] | 20 | |
| 21 | @classmethod |
| 22 | def setUpClass(cls): |
| 23 | super(TestPing, cls).setUpClass() |
| 24 | try: |
| 25 | cls.create_pg_interfaces(range(2)) |
| 26 | cls.interfaces = list(cls.pg_interfaces) |
| 27 | |
| 28 | for i in cls.interfaces: |
| 29 | i.admin_up() |
| 30 | i.config_ip4() |
| 31 | i.config_ip6() |
| 32 | i.disable_ipv6_ra() |
| 33 | i.resolve_arp() |
| 34 | i.resolve_ndp() |
| 35 | except Exception: |
| 36 | super(TestPing, cls).tearDownClass() |
| 37 | raise |
| 38 | |
Paul Vinciguerra | 7f9b7f9 | 2019-03-12 19:23:27 -0700 | [diff] [blame] | 39 | @classmethod |
| 40 | def tearDownClass(cls): |
| 41 | super(TestPing, cls).tearDownClass() |
| 42 | |
Andrew Yourtchenko | e3d5280 | 2017-03-24 17:46:42 +0100 | [diff] [blame] | 43 | def tearDown(self): |
| 44 | super(TestPing, self).tearDown() |
Paul Vinciguerra | 90cf21b | 2019-03-13 09:23:05 -0700 | [diff] [blame] | 45 | |
| 46 | def show_commands_at_teardown(self): |
| 47 | self.logger.info(self.vapi.cli("show hardware")) |
Andrew Yourtchenko | e3d5280 | 2017-03-24 17:46:42 +0100 | [diff] [blame] | 48 | |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 49 | def verify_ping_request(self, p, src, dst, seq): |
| 50 | ip = p[IP] |
| 51 | self.assertEqual(ip.version, 4) |
| 52 | self.assertEqual(ip.flags, 0) |
| 53 | self.assertEqual(ip.src, src) |
| 54 | self.assertEqual(ip.dst, dst) |
| 55 | self.assertEqual(ip.proto, 1) |
| 56 | self.assertEqual(len(ip.options), 0) |
| 57 | self.assertGreaterEqual(ip.ttl, 254) |
| 58 | icmp = p[ICMP] |
| 59 | self.assertEqual(icmp.type, 8) |
| 60 | self.assertEqual(icmp.code, 0) |
| 61 | self.assertEqual(icmp.seq, seq) |
| 62 | return icmp |
| 63 | |
Andrew Yourtchenko | e3d5280 | 2017-03-24 17:46:42 +0100 | [diff] [blame] | 64 | def test_ping_basic(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 65 | """basic ping test""" |
Andrew Yourtchenko | e3d5280 | 2017-03-24 17:46:42 +0100 | [diff] [blame] | 66 | try: |
| 67 | self.pg_enable_capture(self.pg_interfaces) |
| 68 | self.pg_start() |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 69 | self.logger.info(self.vapi.cli("show ip4 neighbors")) |
Andrew Yourtchenko | e3d5280 | 2017-03-24 17:46:42 +0100 | [diff] [blame] | 70 | self.logger.info(self.vapi.cli("show ip6 neighbors")) |
| 71 | |
| 72 | remote_ip4 = self.pg1.remote_ip4 |
| 73 | ping_cmd = "ping " + remote_ip4 + " interval 0.01 repeat 10" |
| 74 | ret = self.vapi.cli(ping_cmd) |
| 75 | self.logger.info(ret) |
| 76 | out = self.pg1.get_capture(10) |
| 77 | icmp_id = None |
| 78 | icmp_seq = 1 |
| 79 | for p in out: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 80 | icmp = self.verify_ping_request( |
| 81 | p, self.pg1.local_ip4, self.pg1.remote_ip4, icmp_seq |
| 82 | ) |
Andrew Yourtchenko | e3d5280 | 2017-03-24 17:46:42 +0100 | [diff] [blame] | 83 | icmp_seq = icmp_seq + 1 |
| 84 | if icmp_id is None: |
| 85 | icmp_id = icmp.id |
| 86 | else: |
| 87 | self.assertEqual(icmp.id, icmp_id) |
| 88 | finally: |
| 89 | self.vapi.cli("show error") |
| 90 | |
| 91 | def test_ping_burst(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 92 | """burst ping test""" |
Andrew Yourtchenko | e3d5280 | 2017-03-24 17:46:42 +0100 | [diff] [blame] | 93 | try: |
| 94 | self.pg_enable_capture(self.pg_interfaces) |
| 95 | self.pg_start() |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 96 | self.logger.info(self.vapi.cli("show ip neighbors")) |
Andrew Yourtchenko | e3d5280 | 2017-03-24 17:46:42 +0100 | [diff] [blame] | 97 | |
| 98 | remote_ip4 = self.pg1.remote_ip4 |
| 99 | ping_cmd = "ping " + remote_ip4 + " interval 0.01 burst 3" |
| 100 | ret = self.vapi.cli(ping_cmd) |
| 101 | self.logger.info(ret) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 102 | out = self.pg1.get_capture(3 * 5) |
Andrew Yourtchenko | e3d5280 | 2017-03-24 17:46:42 +0100 | [diff] [blame] | 103 | icmp_id = None |
| 104 | icmp_seq = 1 |
| 105 | count = 0 |
| 106 | for p in out: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 107 | icmp = self.verify_ping_request( |
| 108 | p, self.pg1.local_ip4, self.pg1.remote_ip4, icmp_seq |
| 109 | ) |
Andrew Yourtchenko | e3d5280 | 2017-03-24 17:46:42 +0100 | [diff] [blame] | 110 | count = count + 1 |
| 111 | if count >= 3: |
| 112 | icmp_seq = icmp_seq + 1 |
| 113 | count = 0 |
| 114 | if icmp_id is None: |
| 115 | icmp_id = icmp.id |
| 116 | else: |
| 117 | self.assertEqual(icmp.id, icmp_id) |
| 118 | finally: |
| 119 | self.vapi.cli("show error") |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 120 | |
| 121 | def test_ping_src(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 122 | """ping with source address set""" |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 123 | |
| 124 | self.pg_enable_capture(self.pg_interfaces) |
| 125 | self.pg_start() |
| 126 | self.logger.info(self.vapi.cli("show ip4 neighbors")) |
| 127 | self.logger.info(self.vapi.cli("show ip6 neighbors")) |
| 128 | |
| 129 | nbr_addr = "10.0.0.2" |
| 130 | VppIpInterfaceAddress(self, self.pg1, "10.0.0.1", 24).add_vpp_config() |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 131 | VppNeighbor( |
| 132 | self, self.pg1.sw_if_index, "00:11:22:33:44:55", nbr_addr |
| 133 | ).add_vpp_config() |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 134 | |
| 135 | ping_cmd = "ping %s interval 0.01 repeat 3" % self.pg1.remote_ip4 |
| 136 | ret = self.vapi.cli(ping_cmd) |
| 137 | out = self.pg1.get_capture(3) |
| 138 | icmp_seq = 1 |
| 139 | for p in out: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 140 | icmp = self.verify_ping_request( |
| 141 | p, self.pg1.local_ip4, self.pg1.remote_ip4, icmp_seq |
| 142 | ) |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 143 | icmp_seq = icmp_seq + 1 |
| 144 | |
| 145 | self.pg_enable_capture(self.pg_interfaces) |
| 146 | self.pg_start() |
| 147 | ping_cmd = "ping %s interval 0.01 repeat 3" % nbr_addr |
| 148 | ret = self.vapi.cli(ping_cmd) |
| 149 | out = self.pg1.get_capture(3) |
| 150 | icmp_seq = 1 |
| 151 | for p in out: |
| 152 | icmp = self.verify_ping_request(p, "10.0.0.1", nbr_addr, icmp_seq) |
| 153 | icmp_seq = icmp_seq + 1 |
Július Milan | 98874cd | 2021-02-16 19:20:47 +0100 | [diff] [blame] | 154 | |
| 155 | def test_ping_fib_routed_dst(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 156 | """ping destination routed according to FIB table""" |
Július Milan | 98874cd | 2021-02-16 19:20:47 +0100 | [diff] [blame] | 157 | |
| 158 | try: |
| 159 | self.pg1.generate_remote_hosts(1) |
| 160 | self.pg_enable_capture(self.pg_interfaces) |
| 161 | self.pg_start() |
| 162 | routed_dst = "10.0.2.0" |
| 163 | self.logger.info(self.vapi.cli("show ip4 neighbors")) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 164 | VppIpRoute( |
| 165 | self, |
| 166 | routed_dst, |
| 167 | 24, |
| 168 | [VppRoutePath(self.pg1.remote_hosts[0].ip4, self.pg1.sw_if_index)], |
| 169 | ).add_vpp_config() |
Július Milan | 98874cd | 2021-02-16 19:20:47 +0100 | [diff] [blame] | 170 | ping_cmd = "ping %s interval 0.01 repeat 3" % routed_dst |
| 171 | ret = self.vapi.cli(ping_cmd) |
| 172 | self.logger.info(ret) |
| 173 | out = self.pg1.get_capture(3) |
| 174 | icmp_seq = 1 |
| 175 | for p in out: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 176 | self.verify_ping_request(p, self.pg1.local_ip4, routed_dst, icmp_seq) |
Július Milan | 98874cd | 2021-02-16 19:20:47 +0100 | [diff] [blame] | 177 | icmp_seq = icmp_seq + 1 |
| 178 | finally: |
| 179 | self.vapi.cli("show error") |
NikitaSkrynnik | bb1cde6 | 2023-09-14 16:00:38 +0700 | [diff] [blame] | 180 | |
| 181 | def test_ping_api(self): |
| 182 | """ping api""" |
| 183 | |
| 184 | try: |
| 185 | self.pg_enable_capture(self.pg_interfaces) |
| 186 | self.pg_start() |
| 187 | |
| 188 | ret = self.vapi.want_ping_finished_events( |
| 189 | address=self.pg1.remote_ip4, |
| 190 | repeat=4, |
| 191 | interval=0.2, |
| 192 | ) |
| 193 | self.logger.info(ret) |
| 194 | timeout = 1 |
| 195 | |
| 196 | ev = self.vapi.wait_for_event(timeout, "ping_finished_event") |
| 197 | self.logger.info(ev) |
| 198 | self.assertEqual(ev.request_count, 4) |
| 199 | |
| 200 | out = self.pg1.get_capture(4) |
| 201 | icmp_id = None |
| 202 | icmp_seq = 1 |
| 203 | for p in out: |
| 204 | icmp = self.verify_ping_request( |
| 205 | p, self.pg1.local_ip4, self.pg1.remote_ip4, icmp_seq |
| 206 | ) |
| 207 | icmp_seq = icmp_seq + 1 |
| 208 | if icmp_id is None: |
| 209 | icmp_id = icmp.id |
| 210 | else: |
| 211 | self.assertEqual(icmp.id, icmp_id) |
| 212 | finally: |
| 213 | self.vapi.cli("show error") |