Dave Barach | 6d6711b | 2020-04-27 09:11:19 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
Paul Vinciguerra | ae93608 | 2020-05-06 16:38:40 -0400 | [diff] [blame] | 3 | import os |
Dave Barach | 6d6711b | 2020-04-27 09:11:19 -0400 | [diff] [blame] | 4 | import unittest |
| 5 | |
Maxime Peim | ddc16cf | 2023-01-13 08:04:55 +0000 | [diff] [blame] | 6 | from scapy.layers.l2 import Ether |
| 7 | from scapy.layers.inet import IP, UDP |
| 8 | from scapy.packet import Raw |
| 9 | |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 10 | from framework import VppTestCase |
| 11 | from asfframework import VppTestRunner |
Dmitry Valter | 34fa0ce | 2024-03-11 10:38:46 +0000 | [diff] [blame^] | 12 | from config import config |
Dave Barach | 6d6711b | 2020-04-27 09:11:19 -0400 | [diff] [blame] | 13 | |
| 14 | |
Dmitry Valter | 34fa0ce | 2024-03-11 10:38:46 +0000 | [diff] [blame^] | 15 | @unittest.skipIf( |
| 16 | "dispatch-trace" in config.excluded_plugins, "Exclude dispatch trace plugin tests" |
| 17 | ) |
Dave Barach | 6d6711b | 2020-04-27 09:11:19 -0400 | [diff] [blame] | 18 | class TestPcap(VppTestCase): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 19 | """Pcap Unit Test Cases""" |
Dave Barach | 6d6711b | 2020-04-27 09:11:19 -0400 | [diff] [blame] | 20 | |
| 21 | @classmethod |
| 22 | def setUpClass(cls): |
| 23 | super(TestPcap, cls).setUpClass() |
| 24 | |
Maxime Peim | ddc16cf | 2023-01-13 08:04:55 +0000 | [diff] [blame] | 25 | cls.create_pg_interfaces(range(1)) |
| 26 | for i in cls.pg_interfaces: |
| 27 | i.admin_up() |
| 28 | i.config_ip4() |
| 29 | i.resolve_arp() |
| 30 | |
Dave Barach | 6d6711b | 2020-04-27 09:11:19 -0400 | [diff] [blame] | 31 | @classmethod |
| 32 | def tearDownClass(cls): |
Maxime Peim | ddc16cf | 2023-01-13 08:04:55 +0000 | [diff] [blame] | 33 | for i in cls.pg_interfaces: |
| 34 | i.admin_down() |
| 35 | |
Dave Barach | 6d6711b | 2020-04-27 09:11:19 -0400 | [diff] [blame] | 36 | super(TestPcap, cls).tearDownClass() |
| 37 | |
| 38 | def setUp(self): |
| 39 | super(TestPcap, self).setUp() |
| 40 | |
| 41 | def tearDown(self): |
| 42 | super(TestPcap, self).tearDown() |
| 43 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 44 | # This is a code coverage test, but it only runs for 0.3 seconds |
| 45 | # might as well just run it... |
Dave Barach | 6d6711b | 2020-04-27 09:11:19 -0400 | [diff] [blame] | 46 | def test_pcap_unittest(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 47 | """PCAP Capture Tests""" |
| 48 | cmds = [ |
| 49 | "loop create", |
| 50 | "set int ip address loop0 11.22.33.1/24", |
| 51 | "set int state loop0 up", |
| 52 | "loop create", |
| 53 | "set int ip address loop1 11.22.34.1/24", |
| 54 | "set int state loop1 up", |
| 55 | "set ip neighbor loop1 11.22.34.44 03:00:11:22:34:44", |
| 56 | "packet-generator new {\n" |
| 57 | " name s0\n" |
| 58 | " limit 10\n" |
| 59 | " size 128-128\n" |
| 60 | " interface loop0\n" |
| 61 | " tx-interface loop1\n" |
| 62 | " node loop1-output\n" |
| 63 | " buffer-flags ip4 offload\n" |
| 64 | " buffer-offload-flags offload-ip-cksum offload-udp-cksum\n" |
| 65 | " data {\n" |
| 66 | " IP4: 1.2.3 -> dead.0000.0001\n" |
| 67 | " UDP: 11.22.33.44 -> 11.22.34.44\n" |
| 68 | " ttl 2 checksum 13\n" |
| 69 | " UDP: 1234 -> 2345\n" |
| 70 | " checksum 11\n" |
| 71 | " incrementing 114\n" |
| 72 | " }\n" |
| 73 | "}", |
| 74 | "pcap dispatch trace on max 100 buffer-trace pg-input 10", |
| 75 | "pa en", |
| 76 | "pcap dispatch trace off", |
| 77 | "pcap trace rx tx max 1000 intfc any", |
| 78 | "pa en", |
| 79 | "pcap trace status", |
| 80 | "pcap trace rx tx off", |
| 81 | "classify filter pcap mask l3 ip4 src match l3 ip4 src 11.22.33.44", |
| 82 | "pcap trace rx tx max 1000 intfc any file filt.pcap filter", |
| 83 | "show cla t verbose 2", |
| 84 | "show cla t verbose", |
| 85 | "show cla t", |
| 86 | "pa en", |
| 87 | "pcap trace rx tx off", |
| 88 | "classify filter pcap del mask l3 ip4 src", |
| 89 | ] |
Dave Barach | 6d6711b | 2020-04-27 09:11:19 -0400 | [diff] [blame] | 90 | |
| 91 | for cmd in cmds: |
| 92 | r = self.vapi.cli_return_response(cmd) |
| 93 | if r.retval != 0: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 94 | if hasattr(r, "reply"): |
Dave Barach | 6d6711b | 2020-04-27 09:11:19 -0400 | [diff] [blame] | 95 | self.logger.info(cmd + " FAIL reply " + r.reply) |
| 96 | else: |
| 97 | self.logger.info(cmd + " FAIL retval " + str(r.retval)) |
| 98 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 99 | self.assertTrue(os.path.exists("/tmp/dispatch.pcap")) |
| 100 | self.assertTrue(os.path.exists("/tmp/rxtx.pcap")) |
| 101 | self.assertTrue(os.path.exists("/tmp/filt.pcap")) |
| 102 | os.remove("/tmp/dispatch.pcap") |
| 103 | os.remove("/tmp/rxtx.pcap") |
| 104 | os.remove("/tmp/filt.pcap") |
Dave Barach | 6d6711b | 2020-04-27 09:11:19 -0400 | [diff] [blame] | 105 | |
Maxime Peim | ddc16cf | 2023-01-13 08:04:55 +0000 | [diff] [blame] | 106 | def test_pcap_trace_api(self): |
| 107 | """PCAP API Tests""" |
| 108 | |
| 109 | pkt = ( |
| 110 | Ether(src=self.pg0.local_mac, dst=self.pg0.remote_mac) |
| 111 | / IP(src=self.pg0.local_ip4, dst=self.pg0.remote_ip4, ttl=2) |
| 112 | / UDP(sport=1234, dport=2345) |
| 113 | / Raw(b"\xa5" * 128) |
| 114 | ) |
| 115 | |
| 116 | self.vapi.pcap_trace_on( |
| 117 | capture_rx=True, |
| 118 | capture_tx=True, |
| 119 | max_packets=1000, |
| 120 | sw_if_index=0, |
| 121 | filename="trace_any.pcap", |
| 122 | ) |
| 123 | self.pg_send(self.pg0, pkt * 10) |
| 124 | self.vapi.pcap_trace_off() |
| 125 | |
| 126 | self.vapi.cli( |
| 127 | f"classify filter pcap mask l3 ip4 src match l3 ip4 src {self.pg0.local_ip4}" |
| 128 | ) |
| 129 | self.vapi.pcap_trace_on( |
| 130 | capture_rx=True, |
| 131 | capture_tx=True, |
| 132 | filter=True, |
| 133 | max_packets=1000, |
| 134 | sw_if_index=0, |
| 135 | filename="trace_any_filter.pcap", |
| 136 | ) |
| 137 | self.pg_send(self.pg0, pkt * 10) |
| 138 | self.vapi.pcap_trace_off() |
| 139 | self.vapi.cli("classify filter pcap del mask l3 ip4 src") |
| 140 | |
| 141 | pkt = ( |
Steven Luong | e4238aa | 2024-04-19 09:49:20 -0700 | [diff] [blame] | 142 | Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) |
Maxime Peim | ddc16cf | 2023-01-13 08:04:55 +0000 | [diff] [blame] | 143 | # wrong destination address |
| 144 | / IP(src=self.pg0.local_ip4, dst=self.pg0.local_ip4, ttl=2) |
| 145 | / UDP(sport=1234, dport=2345) |
| 146 | / Raw(b"\xa5" * 128) |
| 147 | ) |
| 148 | |
| 149 | self.vapi.pcap_trace_on( |
| 150 | capture_drop=True, |
| 151 | max_packets=1000, |
| 152 | sw_if_index=0, |
| 153 | error="{ip4-local}.{spoofed_local_packets}", |
| 154 | filename="trace_drop_err.pcap", |
| 155 | ) |
| 156 | self.pg_send(self.pg0, pkt * 10) |
| 157 | self.vapi.pcap_trace_off() |
| 158 | |
| 159 | self.assertTrue(os.path.exists("/tmp/trace_any.pcap")) |
| 160 | self.assertTrue(os.path.exists("/tmp/trace_any_filter.pcap")) |
| 161 | self.assertTrue(os.path.exists("/tmp/trace_drop_err.pcap")) |
| 162 | os.remove("/tmp/trace_any.pcap") |
| 163 | os.remove("/tmp/trace_any_filter.pcap") |
| 164 | os.remove("/tmp/trace_drop_err.pcap") |
| 165 | |
Paul Vinciguerra | ae93608 | 2020-05-06 16:38:40 -0400 | [diff] [blame] | 166 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 167 | if __name__ == "__main__": |
Dave Barach | 6d6711b | 2020-04-27 09:11:19 -0400 | [diff] [blame] | 168 | unittest.main(testRunner=VppTestRunner) |