blob: b73a601bcc8d722ba3d9c7ae08f3595e65a58808 [file] [log] [blame]
Dave Barach6d6711b2020-04-27 09:11:19 -04001#!/usr/bin/env python3
2
Paul Vinciguerraae936082020-05-06 16:38:40 -04003import os
Dave Barach6d6711b2020-04-27 09:11:19 -04004import unittest
5
Maxime Peimddc16cf2023-01-13 08:04:55 +00006from scapy.layers.l2 import Ether
7from scapy.layers.inet import IP, UDP
8from scapy.packet import Raw
9
Dave Wallace8800f732023-08-31 00:47:44 -040010from framework import VppTestCase
11from asfframework import VppTestRunner
Dmitry Valter34fa0ce2024-03-11 10:38:46 +000012from config import config
Dave Barach6d6711b2020-04-27 09:11:19 -040013
14
Dmitry Valter34fa0ce2024-03-11 10:38:46 +000015@unittest.skipIf(
16 "dispatch-trace" in config.excluded_plugins, "Exclude dispatch trace plugin tests"
17)
Dave Barach6d6711b2020-04-27 09:11:19 -040018class TestPcap(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020019 """Pcap Unit Test Cases"""
Dave Barach6d6711b2020-04-27 09:11:19 -040020
21 @classmethod
22 def setUpClass(cls):
23 super(TestPcap, cls).setUpClass()
24
Maxime Peimddc16cf2023-01-13 08:04:55 +000025 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 Barach6d6711b2020-04-27 09:11:19 -040031 @classmethod
32 def tearDownClass(cls):
Maxime Peimddc16cf2023-01-13 08:04:55 +000033 for i in cls.pg_interfaces:
34 i.admin_down()
35
Dave Barach6d6711b2020-04-27 09:11:19 -040036 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 Sekerad9b0c6f2022-04-26 19:02:15 +020044 # This is a code coverage test, but it only runs for 0.3 seconds
45 # might as well just run it...
Dave Barach6d6711b2020-04-27 09:11:19 -040046 def test_pcap_unittest(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020047 """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 Barach6d6711b2020-04-27 09:11:19 -040090
91 for cmd in cmds:
92 r = self.vapi.cli_return_response(cmd)
93 if r.retval != 0:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020094 if hasattr(r, "reply"):
Dave Barach6d6711b2020-04-27 09:11:19 -040095 self.logger.info(cmd + " FAIL reply " + r.reply)
96 else:
97 self.logger.info(cmd + " FAIL retval " + str(r.retval))
98
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020099 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 Barach6d6711b2020-04-27 09:11:19 -0400105
Maxime Peimddc16cf2023-01-13 08:04:55 +0000106 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 Luonge4238aa2024-04-19 09:49:20 -0700142 Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac)
Maxime Peimddc16cf2023-01-13 08:04:55 +0000143 # 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 Vinciguerraae936082020-05-06 16:38:40 -0400166
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200167if __name__ == "__main__":
Dave Barach6d6711b2020-04-27 09:11:19 -0400168 unittest.main(testRunner=VppTestRunner)