Dave Barach | d9ca7c9 | 2019-11-12 17:51:18 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | import unittest |
Dave Barach | 3a49130 | 2020-06-15 17:38:12 -0400 | [diff] [blame] | 4 | import pexpect |
| 5 | import time |
| 6 | import signal |
Klement Sekera | b23ffd7 | 2021-05-31 16:08:53 +0200 | [diff] [blame] | 7 | from config import config |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 8 | from framework import VppTestCase |
| 9 | from asfframework import VppTestRunner |
Dmitry Valter | 9f5b369 | 2022-09-05 15:30:18 +0000 | [diff] [blame] | 10 | from scapy.layers.inet import IP, ICMP |
| 11 | from scapy.layers.l2 import Ether |
| 12 | from scapy.packet import Raw |
Dave Barach | d9ca7c9 | 2019-11-12 17:51:18 -0500 | [diff] [blame] | 13 | |
| 14 | |
Klement Sekera | b23ffd7 | 2021-05-31 16:08:53 +0200 | [diff] [blame] | 15 | @unittest.skipUnless(config.gcov, "part of code coverage tests") |
Dave Barach | d9ca7c9 | 2019-11-12 17:51:18 -0500 | [diff] [blame] | 16 | class TestVlib(VppTestCase): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 17 | """Vlib Unit Test Cases""" |
| 18 | |
Klement Sekera | 8d81502 | 2021-03-15 16:58:10 +0100 | [diff] [blame] | 19 | vpp_worker_count = 1 |
Dave Barach | d9ca7c9 | 2019-11-12 17:51:18 -0500 | [diff] [blame] | 20 | |
| 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 Barach | d9ca7c9 | 2019-11-12 17:51:18 -0500 | [diff] [blame] | 35 | def test_vlib_main_unittest(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 36 | """Vlib main.c Code Coverage Test""" |
Dave Barach | d9ca7c9 | 2019-11-12 17:51:18 -0500 | [diff] [blame] | 37 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 38 | 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 Barach | d9ca7c9 | 2019-11-12 17:51:18 -0500 | [diff] [blame] | 67 | |
| 68 | for cmd in cmds: |
| 69 | r = self.vapi.cli_return_response(cmd) |
| 70 | if r.retval != 0: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 71 | if hasattr(r, "reply"): |
Dave Barach | d9ca7c9 | 2019-11-12 17:51:18 -0500 | [diff] [blame] | 72 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 77 | """Vlib node_cli.c Code Coverage Test""" |
Dave Barach | d9ca7c9 | 2019-11-12 17:51:18 -0500 | [diff] [blame] | 78 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 79 | 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 Barach | d9ca7c9 | 2019-11-12 17:51:18 -0500 | [diff] [blame] | 112 | |
| 113 | for cmd in cmds: |
| 114 | r = self.vapi.cli_return_response(cmd) |
| 115 | if r.retval != 0: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 116 | if hasattr(r, "reply"): |
Dave Barach | d9ca7c9 | 2019-11-12 17:51:18 -0500 | [diff] [blame] | 117 | self.logger.info(cmd + " FAIL reply " + r.reply) |
| 118 | else: |
| 119 | self.logger.info(cmd + " FAIL retval " + str(r.retval)) |
| 120 | |
Dave Barach | c17fc44 | 2019-11-18 17:16:49 -0500 | [diff] [blame] | 121 | def test_vlib_buffer_c_unittest(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 122 | """Vlib buffer.c Code Coverage Test""" |
Dave Barach | c17fc44 | 2019-11-18 17:16:49 -0500 | [diff] [blame] | 123 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 124 | 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 Barach | c17fc44 | 2019-11-18 17:16:49 -0500 | [diff] [blame] | 168 | |
| 169 | for cmd in cmds: |
| 170 | r = self.vapi.cli_return_response(cmd) |
| 171 | if r.retval != 0: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 172 | if hasattr(r, "reply"): |
Dave Barach | c17fc44 | 2019-11-18 17:16:49 -0500 | [diff] [blame] | 173 | self.logger.info(cmd + " FAIL reply " + r.reply) |
| 174 | else: |
| 175 | self.logger.info(cmd + " FAIL retval " + str(r.retval)) |
| 176 | |
Dave Barach | e51a9bb | 2019-11-19 10:36:41 -0500 | [diff] [blame] | 177 | def test_vlib_format_unittest(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 178 | """Vlib format.c Code Coverage Test""" |
Dave Barach | e51a9bb | 2019-11-19 10:36:41 -0500 | [diff] [blame] | 179 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 180 | 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 Barach | e51a9bb | 2019-11-19 10:36:41 -0500 | [diff] [blame] | 186 | |
| 187 | for cmd in cmds: |
| 188 | r = self.vapi.cli_return_response(cmd) |
| 189 | if r.retval != 0: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 190 | if hasattr(r, "reply"): |
Dave Barach | e51a9bb | 2019-11-19 10:36:41 -0500 | [diff] [blame] | 191 | self.logger.info(cmd + " FAIL reply " + r.reply) |
| 192 | else: |
| 193 | self.logger.info(cmd + " FAIL retval " + str(r.retval)) |
| 194 | |
Dave Barach | 3a49130 | 2020-06-15 17:38:12 -0400 | [diff] [blame] | 195 | def test_vlib_main_unittest(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 196 | """Private Binary API Segment Test (takes 70 seconds)""" |
Dave Barach | 3a49130 | 2020-06-15 17:38:12 -0400 | [diff] [blame] | 197 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 198 | vat_path = config.vpp + "_api_test" |
| 199 | vat = pexpect.spawn(vat_path, ["socket-name", self.get_api_sock_path()]) |
Dave Barach | 3a49130 | 2020-06-15 17:38:12 -0400 | [diff] [blame] | 200 | vat.expect("vat# ", timeout=10) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 201 | vat.sendline("sock_init_shm") |
Dave Barach | 3a49130 | 2020-06-15 17:38:12 -0400 | [diff] [blame] | 202 | vat.expect("vat# ", timeout=10) |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 203 | vat.sendline("sh api cli") |
Dave Barach | 3a49130 | 2020-06-15 17:38:12 -0400 | [diff] [blame] | 204 | 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 Barach | 4de5f9b | 2021-06-02 18:18:18 -0400 | [diff] [blame] | 210 | def test_pool(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 211 | """Fixed-size Pool Test""" |
Dave Barach | 4de5f9b | 2021-06-02 18:18:18 -0400 | [diff] [blame] | 212 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 213 | cmds = [ |
| 214 | "test pool", |
| 215 | ] |
Dave Barach | 4de5f9b | 2021-06-02 18:18:18 -0400 | [diff] [blame] | 216 | |
| 217 | for cmd in cmds: |
| 218 | r = self.vapi.cli_return_response(cmd) |
| 219 | if r.retval != 0: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 220 | if hasattr(r, "reply"): |
Dave Barach | 4de5f9b | 2021-06-02 18:18:18 -0400 | [diff] [blame] | 221 | self.logger.info(cmd + " FAIL reply " + r.reply) |
| 222 | else: |
| 223 | self.logger.info(cmd + " FAIL retval " + str(r.retval)) |
Klement Sekera | e263685 | 2021-03-16 12:52:12 +0100 | [diff] [blame] | 224 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 225 | |
Dmitry Valter | 9f5b369 | 2022-09-05 15:30:18 +0000 | [diff] [blame] | 226 | class 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 Valter | 34fa0ce | 2024-03-11 10:38:46 +0000 | [diff] [blame] | 255 | @unittest.skipIf( |
| 256 | "ping" in config.excluded_plugins, "Exclude tests requiring Ping plugin" |
| 257 | ) |
Dmitry Valter | 9f5b369 | 2022-09-05 15:30:18 +0000 | [diff] [blame] | 258 | 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 | |
| 277 | self.assertEquals(len(rx), 1) |
| 278 | 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 | |
| 308 | self.assertEquals(len(rx), 1) |
| 309 | 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 Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 329 | if __name__ == "__main__": |
Dave Barach | d9ca7c9 | 2019-11-12 17:51:18 -0500 | [diff] [blame] | 330 | unittest.main(testRunner=VppTestRunner) |