blob: 2fb36e5d7b633988dc04ea1969e96aaf478e6a9b [file] [log] [blame]
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +01001import socket
2
3from scapy.layers.inet import IP, UDP, ICMP
4from scapy.layers.inet6 import IPv6
5from scapy.layers.l2 import Ether, GRE
6from scapy.packet import Raw
7
8from framework import VppTestCase
9from util import ppp
Július Milan98874cd2021-02-16 19:20:47 +010010from vpp_ip_route import VppIpInterfaceAddress, VppIpRoute, VppRoutePath
Neale Rannse2fe0972020-11-26 08:37:27 +000011from vpp_neighbor import VppNeighbor
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +010012
13""" TestPing is a subclass of VPPTestCase classes.
14
15Basic test for sanity check of the ping.
16
17"""
18
19
20class TestPing(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020021 """Ping Test Case"""
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +010022
23 @classmethod
24 def setUpClass(cls):
25 super(TestPing, cls).setUpClass()
26 try:
27 cls.create_pg_interfaces(range(2))
28 cls.interfaces = list(cls.pg_interfaces)
29
30 for i in cls.interfaces:
31 i.admin_up()
32 i.config_ip4()
33 i.config_ip6()
34 i.disable_ipv6_ra()
35 i.resolve_arp()
36 i.resolve_ndp()
37 except Exception:
38 super(TestPing, cls).tearDownClass()
39 raise
40
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -070041 @classmethod
42 def tearDownClass(cls):
43 super(TestPing, cls).tearDownClass()
44
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +010045 def tearDown(self):
46 super(TestPing, self).tearDown()
Paul Vinciguerra90cf21b2019-03-13 09:23:05 -070047
48 def show_commands_at_teardown(self):
49 self.logger.info(self.vapi.cli("show hardware"))
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +010050
Neale Rannse2fe0972020-11-26 08:37:27 +000051 def verify_ping_request(self, p, src, dst, seq):
52 ip = p[IP]
53 self.assertEqual(ip.version, 4)
54 self.assertEqual(ip.flags, 0)
55 self.assertEqual(ip.src, src)
56 self.assertEqual(ip.dst, dst)
57 self.assertEqual(ip.proto, 1)
58 self.assertEqual(len(ip.options), 0)
59 self.assertGreaterEqual(ip.ttl, 254)
60 icmp = p[ICMP]
61 self.assertEqual(icmp.type, 8)
62 self.assertEqual(icmp.code, 0)
63 self.assertEqual(icmp.seq, seq)
64 return icmp
65
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +010066 def test_ping_basic(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020067 """basic ping test"""
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +010068 try:
69 self.pg_enable_capture(self.pg_interfaces)
70 self.pg_start()
Neale Rannscbe25aa2019-09-30 10:53:31 +000071 self.logger.info(self.vapi.cli("show ip4 neighbors"))
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +010072 self.logger.info(self.vapi.cli("show ip6 neighbors"))
73
74 remote_ip4 = self.pg1.remote_ip4
75 ping_cmd = "ping " + remote_ip4 + " interval 0.01 repeat 10"
76 ret = self.vapi.cli(ping_cmd)
77 self.logger.info(ret)
78 out = self.pg1.get_capture(10)
79 icmp_id = None
80 icmp_seq = 1
81 for p in out:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020082 icmp = self.verify_ping_request(
83 p, self.pg1.local_ip4, self.pg1.remote_ip4, icmp_seq
84 )
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +010085 icmp_seq = icmp_seq + 1
86 if icmp_id is None:
87 icmp_id = icmp.id
88 else:
89 self.assertEqual(icmp.id, icmp_id)
90 finally:
91 self.vapi.cli("show error")
92
93 def test_ping_burst(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020094 """burst ping test"""
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +010095 try:
96 self.pg_enable_capture(self.pg_interfaces)
97 self.pg_start()
Neale Rannscbe25aa2019-09-30 10:53:31 +000098 self.logger.info(self.vapi.cli("show ip neighbors"))
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +010099
100 remote_ip4 = self.pg1.remote_ip4
101 ping_cmd = "ping " + remote_ip4 + " interval 0.01 burst 3"
102 ret = self.vapi.cli(ping_cmd)
103 self.logger.info(ret)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200104 out = self.pg1.get_capture(3 * 5)
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +0100105 icmp_id = None
106 icmp_seq = 1
107 count = 0
108 for p in out:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200109 icmp = self.verify_ping_request(
110 p, self.pg1.local_ip4, self.pg1.remote_ip4, icmp_seq
111 )
Andrew Yourtchenkoe3d52802017-03-24 17:46:42 +0100112 count = count + 1
113 if count >= 3:
114 icmp_seq = icmp_seq + 1
115 count = 0
116 if icmp_id is None:
117 icmp_id = icmp.id
118 else:
119 self.assertEqual(icmp.id, icmp_id)
120 finally:
121 self.vapi.cli("show error")
Neale Rannse2fe0972020-11-26 08:37:27 +0000122
123 def test_ping_src(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200124 """ping with source address set"""
Neale Rannse2fe0972020-11-26 08:37:27 +0000125
126 self.pg_enable_capture(self.pg_interfaces)
127 self.pg_start()
128 self.logger.info(self.vapi.cli("show ip4 neighbors"))
129 self.logger.info(self.vapi.cli("show ip6 neighbors"))
130
131 nbr_addr = "10.0.0.2"
132 VppIpInterfaceAddress(self, self.pg1, "10.0.0.1", 24).add_vpp_config()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200133 VppNeighbor(
134 self, self.pg1.sw_if_index, "00:11:22:33:44:55", nbr_addr
135 ).add_vpp_config()
Neale Rannse2fe0972020-11-26 08:37:27 +0000136
137 ping_cmd = "ping %s interval 0.01 repeat 3" % self.pg1.remote_ip4
138 ret = self.vapi.cli(ping_cmd)
139 out = self.pg1.get_capture(3)
140 icmp_seq = 1
141 for p in out:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200142 icmp = self.verify_ping_request(
143 p, self.pg1.local_ip4, self.pg1.remote_ip4, icmp_seq
144 )
Neale Rannse2fe0972020-11-26 08:37:27 +0000145 icmp_seq = icmp_seq + 1
146
147 self.pg_enable_capture(self.pg_interfaces)
148 self.pg_start()
149 ping_cmd = "ping %s interval 0.01 repeat 3" % nbr_addr
150 ret = self.vapi.cli(ping_cmd)
151 out = self.pg1.get_capture(3)
152 icmp_seq = 1
153 for p in out:
154 icmp = self.verify_ping_request(p, "10.0.0.1", nbr_addr, icmp_seq)
155 icmp_seq = icmp_seq + 1
Július Milan98874cd2021-02-16 19:20:47 +0100156
157 def test_ping_fib_routed_dst(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200158 """ping destination routed according to FIB table"""
Július Milan98874cd2021-02-16 19:20:47 +0100159
160 try:
161 self.pg1.generate_remote_hosts(1)
162 self.pg_enable_capture(self.pg_interfaces)
163 self.pg_start()
164 routed_dst = "10.0.2.0"
165 self.logger.info(self.vapi.cli("show ip4 neighbors"))
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200166 VppIpRoute(
167 self,
168 routed_dst,
169 24,
170 [VppRoutePath(self.pg1.remote_hosts[0].ip4, self.pg1.sw_if_index)],
171 ).add_vpp_config()
Július Milan98874cd2021-02-16 19:20:47 +0100172 ping_cmd = "ping %s interval 0.01 repeat 3" % routed_dst
173 ret = self.vapi.cli(ping_cmd)
174 self.logger.info(ret)
175 out = self.pg1.get_capture(3)
176 icmp_seq = 1
177 for p in out:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200178 self.verify_ping_request(p, self.pg1.local_ip4, routed_dst, icmp_seq)
Július Milan98874cd2021-02-16 19:20:47 +0100179 icmp_seq = icmp_seq + 1
180 finally:
181 self.vapi.cli("show error")
NikitaSkrynnikbb1cde62023-09-14 16:00:38 +0700182
183 def test_ping_api(self):
184 """ping api"""
185
186 try:
187 self.pg_enable_capture(self.pg_interfaces)
188 self.pg_start()
189
190 ret = self.vapi.want_ping_finished_events(
191 address=self.pg1.remote_ip4,
192 repeat=4,
193 interval=0.2,
194 )
195 self.logger.info(ret)
196 timeout = 1
197
198 ev = self.vapi.wait_for_event(timeout, "ping_finished_event")
199 self.logger.info(ev)
200 self.assertEqual(ev.request_count, 4)
201
202 out = self.pg1.get_capture(4)
203 icmp_id = None
204 icmp_seq = 1
205 for p in out:
206 icmp = self.verify_ping_request(
207 p, self.pg1.local_ip4, self.pg1.remote_ip4, icmp_seq
208 )
209 icmp_seq = icmp_seq + 1
210 if icmp_id is None:
211 icmp_id = icmp.id
212 else:
213 self.assertEqual(icmp.id, icmp_id)
214 finally:
215 self.vapi.cli("show error")