blob: 654d47be161d81dc13fdd4b8cec360b74138dcc3 [file] [log] [blame]
Dave Wallace8800f732023-08-31 00:47:44 -04001from scapy.layers.inet import IP, ICMP
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +01002
3from framework import VppTestCase
Július Milan98874cd2021-02-16 19:20:47 +01004from vpp_ip_route import VppIpInterfaceAddress, VppIpRoute, VppRoutePath
Neale Rannse2fe0972020-11-26 08:37:27 +00005from vpp_neighbor import VppNeighbor
Dmitry Valter34fa0ce2024-03-11 10:38:46 +00006from config import config
7
8import unittest
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +01009
10""" TestPing is a subclass of VPPTestCase classes.
11
12Basic test for sanity check of the ping.
13
14"""
15
16
Dmitry Valter34fa0ce2024-03-11 10:38:46 +000017@unittest.skipIf("ping" in config.excluded_plugins, "Exclude Ping plugin tests")
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +010018class TestPing(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020019 """Ping Test Case"""
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +010020
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 Vinciguerra7f9b7f92019-03-12 19:23:27 -070039 @classmethod
40 def tearDownClass(cls):
41 super(TestPing, cls).tearDownClass()
42
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +010043 def tearDown(self):
44 super(TestPing, self).tearDown()
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -070045
46 def show_commands_at_teardown(self):
47 self.logger.info(self.vapi.cli("show hardware"))
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +010048
Neale Rannse2fe0972020-11-26 08:37:27 +000049 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 Yourtchenkoe3d52802017-03-24 17:46:42 +010064 def test_ping_basic(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020065 """basic ping test"""
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +010066 try:
67 self.pg_enable_capture(self.pg_interfaces)
68 self.pg_start()
Neale Rannscbe25aa2019-09-30 10:53:31 +000069 self.logger.info(self.vapi.cli("show ip4 neighbors"))
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +010070 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 Sekerad9b0c6f2022-04-26 19:02:15 +020080 icmp = self.verify_ping_request(
81 p, self.pg1.local_ip4, self.pg1.remote_ip4, icmp_seq
82 )
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +010083 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 Sekerad9b0c6f2022-04-26 19:02:15 +020092 """burst ping test"""
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +010093 try:
94 self.pg_enable_capture(self.pg_interfaces)
95 self.pg_start()
Neale Rannscbe25aa2019-09-30 10:53:31 +000096 self.logger.info(self.vapi.cli("show ip neighbors"))
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +010097
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 Sekerad9b0c6f2022-04-26 19:02:15 +0200102 out = self.pg1.get_capture(3 * 5)
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +0100103 icmp_id = None
104 icmp_seq = 1
105 count = 0
106 for p in out:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200107 icmp = self.verify_ping_request(
108 p, self.pg1.local_ip4, self.pg1.remote_ip4, icmp_seq
109 )
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +0100110 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 Rannse2fe0972020-11-26 08:37:27 +0000120
121 def test_ping_src(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200122 """ping with source address set"""
Neale Rannse2fe0972020-11-26 08:37:27 +0000123
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 Sekerad9b0c6f2022-04-26 19:02:15 +0200131 VppNeighbor(
132 self, self.pg1.sw_if_index, "00:11:22:33:44:55", nbr_addr
133 ).add_vpp_config()
Neale Rannse2fe0972020-11-26 08:37:27 +0000134
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 Sekerad9b0c6f2022-04-26 19:02:15 +0200140 icmp = self.verify_ping_request(
141 p, self.pg1.local_ip4, self.pg1.remote_ip4, icmp_seq
142 )
Neale Rannse2fe0972020-11-26 08:37:27 +0000143 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 Milan98874cd2021-02-16 19:20:47 +0100154
155 def test_ping_fib_routed_dst(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200156 """ping destination routed according to FIB table"""
Július Milan98874cd2021-02-16 19:20:47 +0100157
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 Sekerad9b0c6f2022-04-26 19:02:15 +0200164 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 Milan98874cd2021-02-16 19:20:47 +0100170 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 Sekerad9b0c6f2022-04-26 19:02:15 +0200176 self.verify_ping_request(p, self.pg1.local_ip4, routed_dst, icmp_seq)
Július Milan98874cd2021-02-16 19:20:47 +0100177 icmp_seq = icmp_seq + 1
178 finally:
179 self.vapi.cli("show error")
NikitaSkrynnikbb1cde62023-09-14 16:00:38 +0700180
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")