2 from logging import error
3 from scapy.utils import wrpcap, rdpcap
4 from vpp_interface import VppInterface
7 class VppPGInterface(VppInterface):
9 VPP packet-generator interface
14 """packet-generator interface index assigned by VPP"""
19 """pcap file path - captured packets"""
24 """ pcap file path - injected packets"""
28 def capture_cli(self):
29 """CLI string to start capture on this interface"""
30 return self._capture_cli
34 """capture name for this interface"""
39 """CLI string to load the injected packets"""
40 return self._input_cli
42 def post_init_setup(self):
43 """ Perform post-init setup for super class and add our own setup """
44 super(VppPGInterface, self).post_init_setup()
45 self._out_path = self.test.tempdir + "/pg%u_out.pcap" % self.sw_if_index
46 self._in_path = self.test.tempdir + "/pg%u_in.pcap" % self.sw_if_index
47 self._capture_cli = "packet-generator capture pg%u pcap %s" % (
48 self.pg_index, self.out_path)
49 self._cap_name = "pcap%u" % self.sw_if_index
50 self._input_cli = "packet-generator new pcap %s source pg%u name %s" % (
51 self.in_path, self.pg_index, self.cap_name)
53 def __init__(self, test, pg_index):
54 """ Create VPP packet-generator interface """
55 self._pg_index = pg_index
57 r = self.test.vapi.pg_create_interface(self.pg_index)
58 self._sw_if_index = r.sw_if_index
59 self.post_init_setup()
61 def enable_capture(self):
62 """ Enable capture on this packet-generator interface"""
64 os.unlink(self.out_path)
67 # FIXME this should be an API, but no such exists atm
68 self.test.vapi.cli(self.capture_cli)
70 def add_stream(self, pkts):
72 Add a stream of packets to this packet-generator
74 :param pkts: iterable packets
78 os.remove(self.in_path)
81 wrpcap(self.in_path, pkts)
82 # FIXME this should be an API, but no such exists atm
83 self.test.vapi.cli(self.input_cli)
84 self.test.pg_streams.append(self.cap_name)
85 self.test.vapi.cli("trace add pg-input %d" % len(pkts))
87 def get_capture(self):
91 :returns: iterable packets
94 output = rdpcap(self.out_path)
95 except IOError: # TODO
96 error("File %s does not exist, probably because no"
97 " packets arrived" % self.out_path)