blob: 48e32b6e6692b65b7f2f9951e092b4c2e8ff9982 [file] [log] [blame]
Dave Barachd9ca7c92019-11-12 17:51:18 -05001#!/usr/bin/env python3
2
3import unittest
Dave Barach3a491302020-06-15 17:38:12 -04004import pexpect
5import time
6import signal
Klement Sekerab23ffd72021-05-31 16:08:53 +02007from config import config
Dave Wallace8800f732023-08-31 00:47:44 -04008from framework import VppTestCase
9from asfframework import VppTestRunner
Dmitry Valter9f5b3692022-09-05 15:30:18 +000010from scapy.layers.inet import IP, ICMP
11from scapy.layers.l2 import Ether
12from scapy.packet import Raw
Dave Barachd9ca7c92019-11-12 17:51:18 -050013
14
Klement Sekerab23ffd72021-05-31 16:08:53 +020015@unittest.skipUnless(config.gcov, "part of code coverage tests")
Dave Barachd9ca7c92019-11-12 17:51:18 -050016class TestVlib(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020017 """Vlib Unit Test Cases"""
18
Klement Sekera8d815022021-03-15 16:58:10 +010019 vpp_worker_count = 1
Dave Barachd9ca7c92019-11-12 17:51:18 -050020
21 @classmethod
22 def setUpClass(cls):
23 super(TestVlib, cls).setUpClass()
24
25 @classmethod
26 def tearDownClass(cls):
27 super(TestVlib, cls).tearDownClass()
28
29 def setUp(self):
30 super(TestVlib, self).setUp()
31
32 def tearDown(self):
33 super(TestVlib, self).tearDown()
34
Dave Barachd9ca7c92019-11-12 17:51:18 -050035 def test_vlib_main_unittest(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020036 """Vlib main.c Code Coverage Test"""
Dave Barachd9ca7c92019-11-12 17:51:18 -050037
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020038 cmds = [
39 "loopback create",
40 "packet-generator new {\n"
41 " name vlib\n"
42 " limit 15\n"
43 " size 128-128\n"
44 " interface loop0\n"
45 " node ethernet-input\n"
46 " data {\n"
47 " IP6: 00:d0:2d:5e:86:85 -> 00:0d:ea:d0:00:00\n"
48 " ICMP: db00::1 -> db00::2\n"
49 " incrementing 30\n"
50 " }\n"
51 "}\n",
52 "event-logger trace dispatch",
53 "event-logger stop",
54 "event-logger clear",
55 "event-logger resize 102400",
56 "event-logger restart",
57 "pcap dispatch trace on max 100 buffer-trace pg-input 15",
58 "pa en",
59 "show event-log 100 all",
60 "event-log save",
61 "event-log save foo",
62 "pcap dispatch trace",
63 "pcap dispatch trace status",
64 "pcap dispatch trace off",
65 "show vlib frame-allocation",
66 ]
Dave Barachd9ca7c92019-11-12 17:51:18 -050067
68 for cmd in cmds:
69 r = self.vapi.cli_return_response(cmd)
70 if r.retval != 0:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020071 if hasattr(r, "reply"):
Dave Barachd9ca7c92019-11-12 17:51:18 -050072 self.logger.info(cmd + " FAIL reply " + r.reply)
73 else:
74 self.logger.info(cmd + " FAIL retval " + str(r.retval))
75
76 def test_vlib_node_cli_unittest(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020077 """Vlib node_cli.c Code Coverage Test"""
Dave Barachd9ca7c92019-11-12 17:51:18 -050078
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020079 cmds = [
80 "loopback create",
81 "packet-generator new {\n"
82 " name vlib\n"
83 " limit 15\n"
84 " size 128-128\n"
85 " interface loop0\n"
86 " node ethernet-input\n"
87 " data {\n"
88 " IP6: 00:d0:2d:5e:86:85 -> 00:0d:ea:d0:00:00\n"
89 " ICMP: db00::1 -> db00::2\n"
90 " incrementing 30\n"
91 " }\n"
92 "}\n",
93 "show vlib graph",
94 "show vlib graph ethernet-input",
95 "show vlib graphviz",
96 "show vlib graphviz graphviz.dot",
97 "pa en",
98 "show runtime ethernet-input",
99 "show runtime brief verbose max summary",
100 "clear runtime",
101 "show node index 1",
102 "show node ethernet-input",
103 "show node pg-input",
104 "set node function",
105 "set node function no-such-node",
106 "set node function cdp-input default",
107 "set node function ethernet-input default",
108 "set node function ethernet-input bozo",
109 "set node function ethernet-input",
110 "show \t",
111 ]
Dave Barachd9ca7c92019-11-12 17:51:18 -0500112
113 for cmd in cmds:
114 r = self.vapi.cli_return_response(cmd)
115 if r.retval != 0:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200116 if hasattr(r, "reply"):
Dave Barachd9ca7c92019-11-12 17:51:18 -0500117 self.logger.info(cmd + " FAIL reply " + r.reply)
118 else:
119 self.logger.info(cmd + " FAIL retval " + str(r.retval))
120
Dave Barachc17fc442019-11-18 17:16:49 -0500121 def test_vlib_buffer_c_unittest(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200122 """Vlib buffer.c Code Coverage Test"""
Dave Barachc17fc442019-11-18 17:16:49 -0500123
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200124 cmds = [
125 "loopback create",
126 "packet-generator new {\n"
127 " name vlib\n"
128 " limit 15\n"
129 " size 128-128\n"
130 " interface loop0\n"
131 " node ethernet-input\n"
132 " data {\n"
133 " IP6: 00:d0:2d:5e:86:85 -> 00:0d:ea:d0:00:00\n"
134 " ICMP: db00::1 -> db00::2\n"
135 " incrementing 30\n"
136 " }\n"
137 "}\n",
138 "event-logger trace",
139 "event-logger trace enable",
140 "event-logger trace api cli barrier",
141 "pa en",
142 "show interface bogus",
143 "event-logger trace disable api cli barrier",
144 "event-logger trace circuit-node ethernet-input",
145 "event-logger trace circuit-node ethernet-input disable",
146 "clear interfaces",
147 "test vlib",
148 "test vlib2",
149 "show memory api-segment stats-segment main-heap verbose",
150 "leak-check { show memory }",
151 "show cpu",
152 "memory-trace main-heap",
153 "memory-trace main-heap api-segment stats-segment",
154 "leak-check { show version }",
155 "show version ?",
156 "comment { show version }",
157 "uncomment { show version }",
158 "show memory main-heap",
159 "show memory bogus",
160 "choices",
161 "test heap-validate",
162 "memory-trace main-heap disable",
163 "show buffers",
164 "show eve",
165 "show help",
166 "show ip ",
167 ]
Dave Barachc17fc442019-11-18 17:16:49 -0500168
169 for cmd in cmds:
170 r = self.vapi.cli_return_response(cmd)
171 if r.retval != 0:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200172 if hasattr(r, "reply"):
Dave Barachc17fc442019-11-18 17:16:49 -0500173 self.logger.info(cmd + " FAIL reply " + r.reply)
174 else:
175 self.logger.info(cmd + " FAIL retval " + str(r.retval))
176
Dave Barache51a9bb2019-11-19 10:36:41 -0500177 def test_vlib_format_unittest(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200178 """Vlib format.c Code Coverage Test"""
Dave Barache51a9bb2019-11-19 10:36:41 -0500179
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200180 cmds = [
181 "loopback create",
182 "classify filter pcap mask l2 proto match l2 proto 0x86dd",
183 "classify filter pcap del",
184 "test format-vlib",
185 ]
Dave Barache51a9bb2019-11-19 10:36:41 -0500186
187 for cmd in cmds:
188 r = self.vapi.cli_return_response(cmd)
189 if r.retval != 0:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200190 if hasattr(r, "reply"):
Dave Barache51a9bb2019-11-19 10:36:41 -0500191 self.logger.info(cmd + " FAIL reply " + r.reply)
192 else:
193 self.logger.info(cmd + " FAIL retval " + str(r.retval))
194
Dave Barach3a491302020-06-15 17:38:12 -0400195 def test_vlib_main_unittest(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200196 """Private Binary API Segment Test (takes 70 seconds)"""
Dave Barach3a491302020-06-15 17:38:12 -0400197
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200198 vat_path = config.vpp + "_api_test"
199 vat = pexpect.spawn(vat_path, ["socket-name", self.get_api_sock_path()])
Dave Barach3a491302020-06-15 17:38:12 -0400200 vat.expect("vat# ", timeout=10)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200201 vat.sendline("sock_init_shm")
Dave Barach3a491302020-06-15 17:38:12 -0400202 vat.expect("vat# ", timeout=10)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200203 vat.sendline("sh api cli")
Dave Barach3a491302020-06-15 17:38:12 -0400204 vat.kill(signal.SIGKILL)
205 vat.wait()
206 self.logger.info("vat terminated, 70 second wait for the Reaper")
207 time.sleep(70)
208 self.logger.info("Reaper should be complete...")
209
Dave Barach4de5f9b2021-06-02 18:18:18 -0400210 def test_pool(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200211 """Fixed-size Pool Test"""
Dave Barach4de5f9b2021-06-02 18:18:18 -0400212
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200213 cmds = [
214 "test pool",
215 ]
Dave Barach4de5f9b2021-06-02 18:18:18 -0400216
217 for cmd in cmds:
218 r = self.vapi.cli_return_response(cmd)
219 if r.retval != 0:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200220 if hasattr(r, "reply"):
Dave Barach4de5f9b2021-06-02 18:18:18 -0400221 self.logger.info(cmd + " FAIL reply " + r.reply)
222 else:
223 self.logger.info(cmd + " FAIL retval " + str(r.retval))
Klement Sekerae2636852021-03-16 12:52:12 +0100224
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200225
Dmitry Valter9f5b3692022-09-05 15:30:18 +0000226class TestVlibFrameLeak(VppTestCase):
227 """Vlib Frame Leak Test Cases"""
228
229 vpp_worker_count = 1
230
231 @classmethod
232 def setUpClass(cls):
233 super(TestVlibFrameLeak, cls).setUpClass()
234
235 @classmethod
236 def tearDownClass(cls):
237 super(TestVlibFrameLeak, cls).tearDownClass()
238
239 def setUp(self):
240 super(TestVlibFrameLeak, self).setUp()
241 # create 1 pg interface
242 self.create_pg_interfaces(range(1))
243
244 for i in self.pg_interfaces:
245 i.admin_up()
246 i.config_ip4()
247 i.resolve_arp()
248
249 def tearDown(self):
250 super(TestVlibFrameLeak, self).tearDown()
251 for i in self.pg_interfaces:
252 i.unconfig_ip4()
253 i.admin_down()
254
Dmitry Valter34fa0ce2024-03-11 10:38:46 +0000255 @unittest.skipIf(
256 "ping" in config.excluded_plugins, "Exclude tests requiring Ping plugin"
257 )
Dmitry Valter9f5b3692022-09-05 15:30:18 +0000258 def test_vlib_mw_refork_frame_leak(self):
259 """Vlib worker thread refork leak test case"""
260 icmp_id = 0xB
261 icmp_seq = 5
262 icmp_load = b"\x0a" * 18
263 pkt = (
264 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
265 / IP(src=self.pg0.remote_ip4, dst=self.pg0.local_ip4)
266 / ICMP(id=icmp_id, seq=icmp_seq)
267 / Raw(load=icmp_load)
268 )
269
270 # Send a packet
271 self.pg0.add_stream(pkt)
272 self.pg_enable_capture(self.pg_interfaces)
273 self.pg_start()
274
275 rx = self.pg0.get_capture(1)
276
Dave Wallace85ce9312024-08-19 18:47:55 -0400277 self.assertEqual(len(rx), 1)
Dmitry Valter9f5b3692022-09-05 15:30:18 +0000278 rx = rx[0]
279 ether = rx[Ether]
280 ipv4 = rx[IP]
281
282 self.assertEqual(ether.src, self.pg0.local_mac)
283 self.assertEqual(ether.dst, self.pg0.remote_mac)
284
285 self.assertEqual(ipv4.src, self.pg0.local_ip4)
286 self.assertEqual(ipv4.dst, self.pg0.remote_ip4)
287
288 # Save allocated frame count
289 frame_allocated = {}
290 for fs in self.vapi.cli("show vlib frame-allocation").splitlines()[1:]:
291 spl = fs.split()
292 thread = int(spl[0])
293 size = int(spl[1])
294 alloc = int(spl[2])
295 key = (thread, size)
296 frame_allocated[key] = alloc
297
298 # cause reforks
299 _ = self.create_loopback_interfaces(1)
300
301 # send the same packet
302 self.pg0.add_stream(pkt)
303 self.pg_enable_capture(self.pg_interfaces)
304 self.pg_start()
305
306 rx = self.pg0.get_capture(1)
307
Dave Wallace85ce9312024-08-19 18:47:55 -0400308 self.assertEqual(len(rx), 1)
Dmitry Valter9f5b3692022-09-05 15:30:18 +0000309 rx = rx[0]
310 ether = rx[Ether]
311 ipv4 = rx[IP]
312
313 self.assertEqual(ether.src, self.pg0.local_mac)
314 self.assertEqual(ether.dst, self.pg0.remote_mac)
315
316 self.assertEqual(ipv4.src, self.pg0.local_ip4)
317 self.assertEqual(ipv4.dst, self.pg0.remote_ip4)
318
319 # Check that no frame were leaked during refork
320 for fs in self.vapi.cli("show vlib frame-allocation").splitlines()[1:]:
321 spl = fs.split()
322 thread = int(spl[0])
323 size = int(spl[1])
324 alloc = int(spl[2])
325 key = (thread, size)
326 self.assertEqual(frame_allocated[key], alloc)
327
328
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200329if __name__ == "__main__":
Dave Barachd9ca7c92019-11-12 17:51:18 -0500330 unittest.main(testRunner=VppTestRunner)