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