1 from framework import VppTestCase, VppTestRunner
3 from config import config
4 from scapy.layers.l2 import Ether
5 from scapy.packet import Raw
6 from scapy.layers.inet import IP, UDP
7 from random import randint
11 @unittest.skipIf("bufmon" in config.excluded_plugins, "Exclude bufmon plugin tests")
12 class TestBufmon(VppTestCase):
13 """bufmon plugin test"""
17 super(TestBufmon, cls).setUpClass()
19 cls.create_pg_interfaces(range(2))
20 for i in cls.pg_interfaces:
29 def tearDownClass(cls):
30 for i in cls.pg_interfaces:
33 super(TestBufmon, cls).tearDownClass()
35 # https://fd.io/docs/vpp/master/developer/tests/overview.html#example-how-to-add-a-new-test
36 def create_stream(self, src_if, dst_if, count):
38 for i in range(count):
39 info = self.create_packet_info(src_if, dst_if)
40 payload = self.info_to_payload(info)
42 Ether(dst=src_if.local_mac, src=src_if.remote_mac)
43 / IP(src=src_if.remote_ip4, dst=dst_if.remote_ip4)
44 / UDP(sport=randint(1000, 2000), dport=5678)
51 def verify_capture(self, src_if, dst_if, capture):
53 for packet in capture:
57 # convert the payload to packet info object
58 payload_info = self.payload_to_info(packet[Raw])
59 # make sure the indexes match
61 payload_info.src, src_if.sw_if_index, "source sw_if_index"
64 payload_info.dst, dst_if.sw_if_index, "destination sw_if_index"
66 packet_info = self.get_next_packet_info_for_interface2(
67 src_if.sw_if_index, dst_if.sw_if_index, packet_info
69 # make sure we didn't run out of saved packets
70 self.assertIsNotNone(packet_info)
72 payload_info.index, packet_info.index, "packet info index"
74 saved_packet = packet_info.data # fetch the saved packet
75 # assert the values match
76 self.assert_equal(ip.src, saved_packet[IP].src, "IP source address")
77 # ... more assertions here
78 self.assert_equal(udp.sport, saved_packet[UDP].sport, "UDP source port")
80 self.logger.error(ppp("Unexpected or invalid packet:", packet))
82 remaining_packet = self.get_next_packet_info_for_interface2(
83 src_if.sw_if_index, dst_if.sw_if_index, packet_info
87 "Interface %s: Packet expected from interface "
88 "%s didn't arrive" % (dst_if.name, src_if.name),
91 def test_bufmon(self):
92 self.vapi.cli("set buffer traces on")
94 reply = self.vapi.cli("show buffer traces status")
95 self.assertIn("buffers tracing is on", reply)
97 packets = self.create_stream(self.pg0, self.pg1, 5)
98 self.pg0.add_stream(packets)
99 self.pg0.enable_capture()
100 self.pg1.enable_capture()
103 capture = self.pg1.get_capture()
104 self.pg0.assert_nothing_captured()
105 self.verify_capture(self.pg0, self.pg1, capture)
116 reply = self.vapi.cli("show buffer traces verbose")
117 for entry in expected:
118 self.assertIn(entry, reply)
120 # not verbose, skips nodes w/o buffered buffers
121 reply = self.vapi.cli("show buffer traces")
122 self.assertNotIn("pg-input", reply)
124 self.vapi.cli("clear buffer traces")
125 reply = self.vapi.cli("show buffer traces verbose")
126 self.assertNotIn("pg-input", reply)
129 if __name__ == "__main__":
130 unittest.main(testRunner=VppTestRunner)