blob: f3da7eb96ca84681b17da0e7c538078f9a698789 [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
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +01006
7""" TestPing is a subclass of VPPTestCase classes.
8
9Basic test for sanity check of the ping.
10
11"""
12
13
14class TestPing(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020015 """Ping Test Case"""
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +010016
17 @classmethod
18 def setUpClass(cls):
19 super(TestPing, cls).setUpClass()
20 try:
21 cls.create_pg_interfaces(range(2))
22 cls.interfaces = list(cls.pg_interfaces)
23
24 for i in cls.interfaces:
25 i.admin_up()
26 i.config_ip4()
27 i.config_ip6()
28 i.disable_ipv6_ra()
29 i.resolve_arp()
30 i.resolve_ndp()
31 except Exception:
32 super(TestPing, cls).tearDownClass()
33 raise
34
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -070035 @classmethod
36 def tearDownClass(cls):
37 super(TestPing, cls).tearDownClass()
38
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +010039 def tearDown(self):
40 super(TestPing, self).tearDown()
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -070041
42 def show_commands_at_teardown(self):
43 self.logger.info(self.vapi.cli("show hardware"))
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +010044
Neale Rannse2fe0972020-11-26 08:37:27 +000045 def verify_ping_request(self, p, src, dst, seq):
46 ip = p[IP]
47 self.assertEqual(ip.version, 4)
48 self.assertEqual(ip.flags, 0)
49 self.assertEqual(ip.src, src)
50 self.assertEqual(ip.dst, dst)
51 self.assertEqual(ip.proto, 1)
52 self.assertEqual(len(ip.options), 0)
53 self.assertGreaterEqual(ip.ttl, 254)
54 icmp = p[ICMP]
55 self.assertEqual(icmp.type, 8)
56 self.assertEqual(icmp.code, 0)
57 self.assertEqual(icmp.seq, seq)
58 return icmp
59
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +010060 def test_ping_basic(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020061 """basic ping test"""
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +010062 try:
63 self.pg_enable_capture(self.pg_interfaces)
64 self.pg_start()
Neale Rannscbe25aa2019-09-30 10:53:31 +000065 self.logger.info(self.vapi.cli("show ip4 neighbors"))
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +010066 self.logger.info(self.vapi.cli("show ip6 neighbors"))
67
68 remote_ip4 = self.pg1.remote_ip4
69 ping_cmd = "ping " + remote_ip4 + " interval 0.01 repeat 10"
70 ret = self.vapi.cli(ping_cmd)
71 self.logger.info(ret)
72 out = self.pg1.get_capture(10)
73 icmp_id = None
74 icmp_seq = 1
75 for p in out:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020076 icmp = self.verify_ping_request(
77 p, self.pg1.local_ip4, self.pg1.remote_ip4, icmp_seq
78 )
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +010079 icmp_seq = icmp_seq + 1
80 if icmp_id is None:
81 icmp_id = icmp.id
82 else:
83 self.assertEqual(icmp.id, icmp_id)
84 finally:
85 self.vapi.cli("show error")
86
87 def test_ping_burst(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020088 """burst ping test"""
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +010089 try:
90 self.pg_enable_capture(self.pg_interfaces)
91 self.pg_start()
Neale Rannscbe25aa2019-09-30 10:53:31 +000092 self.logger.info(self.vapi.cli("show ip neighbors"))
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +010093
94 remote_ip4 = self.pg1.remote_ip4
95 ping_cmd = "ping " + remote_ip4 + " interval 0.01 burst 3"
96 ret = self.vapi.cli(ping_cmd)
97 self.logger.info(ret)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020098 out = self.pg1.get_capture(3 * 5)
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +010099 icmp_id = None
100 icmp_seq = 1
101 count = 0
102 for p in out:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200103 icmp = self.verify_ping_request(
104 p, self.pg1.local_ip4, self.pg1.remote_ip4, icmp_seq
105 )
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +0100106 count = count + 1
107 if count >= 3:
108 icmp_seq = icmp_seq + 1
109 count = 0
110 if icmp_id is None:
111 icmp_id = icmp.id
112 else:
113 self.assertEqual(icmp.id, icmp_id)
114 finally:
115 self.vapi.cli("show error")
Neale Rannse2fe0972020-11-26 08:37:27 +0000116
117 def test_ping_src(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200118 """ping with source address set"""
Neale Rannse2fe0972020-11-26 08:37:27 +0000119
120 self.pg_enable_capture(self.pg_interfaces)
121 self.pg_start()
122 self.logger.info(self.vapi.cli("show ip4 neighbors"))
123 self.logger.info(self.vapi.cli("show ip6 neighbors"))
124
125 nbr_addr = "10.0.0.2"
126 VppIpInterfaceAddress(self, self.pg1, "10.0.0.1", 24).add_vpp_config()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200127 VppNeighbor(
128 self, self.pg1.sw_if_index, "00:11:22:33:44:55", nbr_addr
129 ).add_vpp_config()
Neale Rannse2fe0972020-11-26 08:37:27 +0000130
131 ping_cmd = "ping %s interval 0.01 repeat 3" % self.pg1.remote_ip4
132 ret = self.vapi.cli(ping_cmd)
133 out = self.pg1.get_capture(3)
134 icmp_seq = 1
135 for p in out:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200136 icmp = self.verify_ping_request(
137 p, self.pg1.local_ip4, self.pg1.remote_ip4, icmp_seq
138 )
Neale Rannse2fe0972020-11-26 08:37:27 +0000139 icmp_seq = icmp_seq + 1
140
141 self.pg_enable_capture(self.pg_interfaces)
142 self.pg_start()
143 ping_cmd = "ping %s interval 0.01 repeat 3" % nbr_addr
144 ret = self.vapi.cli(ping_cmd)
145 out = self.pg1.get_capture(3)
146 icmp_seq = 1
147 for p in out:
148 icmp = self.verify_ping_request(p, "10.0.0.1", nbr_addr, icmp_seq)
149 icmp_seq = icmp_seq + 1
Július Milan98874cd2021-02-16 19:20:47 +0100150
151 def test_ping_fib_routed_dst(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200152 """ping destination routed according to FIB table"""
Július Milan98874cd2021-02-16 19:20:47 +0100153
154 try:
155 self.pg1.generate_remote_hosts(1)
156 self.pg_enable_capture(self.pg_interfaces)
157 self.pg_start()
158 routed_dst = "10.0.2.0"
159 self.logger.info(self.vapi.cli("show ip4 neighbors"))
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200160 VppIpRoute(
161 self,
162 routed_dst,
163 24,
164 [VppRoutePath(self.pg1.remote_hosts[0].ip4, self.pg1.sw_if_index)],
165 ).add_vpp_config()
Július Milan98874cd2021-02-16 19:20:47 +0100166 ping_cmd = "ping %s interval 0.01 repeat 3" % routed_dst
167 ret = self.vapi.cli(ping_cmd)
168 self.logger.info(ret)
169 out = self.pg1.get_capture(3)
170 icmp_seq = 1
171 for p in out:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200172 self.verify_ping_request(p, self.pg1.local_ip4, routed_dst, icmp_seq)
Július Milan98874cd2021-02-16 19:20:47 +0100173 icmp_seq = icmp_seq + 1
174 finally:
175 self.vapi.cli("show error")
NikitaSkrynnikbb1cde62023-09-14 16:00:38 +0700176
177 def test_ping_api(self):
178 """ping api"""
179
180 try:
181 self.pg_enable_capture(self.pg_interfaces)
182 self.pg_start()
183
184 ret = self.vapi.want_ping_finished_events(
185 address=self.pg1.remote_ip4,
186 repeat=4,
187 interval=0.2,
188 )
189 self.logger.info(ret)
190 timeout = 1
191
192 ev = self.vapi.wait_for_event(timeout, "ping_finished_event")
193 self.logger.info(ev)
194 self.assertEqual(ev.request_count, 4)
195
196 out = self.pg1.get_capture(4)
197 icmp_id = None
198 icmp_seq = 1
199 for p in out:
200 icmp = self.verify_ping_request(
201 p, self.pg1.local_ip4, self.pg1.remote_ip4, icmp_seq
202 )
203 icmp_seq = icmp_seq + 1
204 if icmp_id is None:
205 icmp_id = icmp.id
206 else:
207 self.assertEqual(icmp.id, icmp_id)
208 finally:
209 self.vapi.cli("show error")