blob: c2ba13845335da674b6b48b47fc4c1206937379e [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
Pratikshya Prasai657bdf72022-08-18 11:09:38 -040010from asfframework import VppTestCase, VppTestRunner
Dave Barach6d6711b2020-04-27 09:11:19 -040011
12
13class TestPcap(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020014 """Pcap Unit Test Cases"""
Dave Barach6d6711b2020-04-27 09:11:19 -040015
16 @classmethod
17 def setUpClass(cls):
18 super(TestPcap, cls).setUpClass()
19
Maxime Peimddc16cf2023-01-13 08:04:55 +000020 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 Barach6d6711b2020-04-27 09:11:19 -040026 @classmethod
27 def tearDownClass(cls):
Maxime Peimddc16cf2023-01-13 08:04:55 +000028 for i in cls.pg_interfaces:
29 i.admin_down()
30
Dave Barach6d6711b2020-04-27 09:11:19 -040031 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 Sekerad9b0c6f2022-04-26 19:02:15 +020039 # This is a code coverage test, but it only runs for 0.3 seconds
40 # might as well just run it...
Dave Barach6d6711b2020-04-27 09:11:19 -040041 def test_pcap_unittest(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020042 """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 Barach6d6711b2020-04-27 09:11:19 -040085
86 for cmd in cmds:
87 r = self.vapi.cli_return_response(cmd)
88 if r.retval != 0:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020089 if hasattr(r, "reply"):
Dave Barach6d6711b2020-04-27 09:11:19 -040090 self.logger.info(cmd + " FAIL reply " + r.reply)
91 else:
92 self.logger.info(cmd + " FAIL retval " + str(r.retval))
93
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020094 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 Barach6d6711b2020-04-27 09:11:19 -0400100
Maxime Peimddc16cf2023-01-13 08:04:55 +0000101 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 Vinciguerraae936082020-05-06 16:38:40 -0400161
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200162if __name__ == "__main__":
Dave Barach6d6711b2020-04-27 09:11:19 -0400163 unittest.main(testRunner=VppTestRunner)