3 from logging import error, info
4 from scapy.utils import wrpcap, rdpcap
5 from vpp_interface import VppInterface
7 from scapy.layers.l2 import Ether, ARP
8 from scapy.layers.inet6 import IPv6, ICMPv6ND_NS, ICMPv6ND_NA, ICMPv6NDOptSrcLLAddr, ICMPv6NDOptDstLLAddr
11 class VppPGInterface(VppInterface):
13 VPP packet-generator interface
18 """packet-generator interface index assigned by VPP"""
23 """pcap file path - captured packets"""
28 """ pcap file path - injected packets"""
32 def capture_cli(self):
33 """CLI string to start capture on this interface"""
34 return self._capture_cli
38 """capture name for this interface"""
43 """CLI string to load the injected packets"""
44 return self._input_cli
47 def in_history_counter(self):
48 """Self-incrementing counter used when renaming old pcap files"""
49 v = self._in_history_counter
50 self._in_history_counter += 1
54 def out_history_counter(self):
55 """Self-incrementing counter used when renaming old pcap files"""
56 v = self._out_history_counter
57 self._out_history_counter += 1
60 def post_init_setup(self):
61 """ Perform post-init setup for super class and add our own setup """
62 super(VppPGInterface, self).post_init_setup()
63 self._out_file = "pg%u_out.pcap" % self.sw_if_index
64 self._out_path = self.test.tempdir + "/" + self._out_file
65 self._in_file = "pg%u_in.pcap" % self.sw_if_index
66 self._in_path = self.test.tempdir + "/" + self._in_file
67 self._capture_cli = "packet-generator capture pg%u pcap %s" % (
68 self.pg_index, self.out_path)
69 self._cap_name = "pcap%u" % self.sw_if_index
70 self._input_cli = "packet-generator new pcap %s source pg%u name %s" % (
71 self.in_path, self.pg_index, self.cap_name)
73 def __init__(self, test, pg_index):
74 """ Create VPP packet-generator interface """
75 self._in_history_counter = 0
76 self._out_history_counter = 0
77 self._pg_index = pg_index
79 r = self.test.vapi.pg_create_interface(self.pg_index)
80 self._sw_if_index = r.sw_if_index
81 self.post_init_setup()
83 def enable_capture(self):
84 """ Enable capture on this packet-generator interface"""
86 if os.path.isfile(self.out_path):
87 os.rename(self.out_path,
88 "%s/history.[timestamp:%f].[%s-counter:%04d].%s" %
92 self.out_history_counter,
96 # FIXME this should be an API, but no such exists atm
97 self.test.vapi.cli(self.capture_cli)
99 def add_stream(self, pkts):
101 Add a stream of packets to this packet-generator
103 :param pkts: iterable packets
107 if os.path.isfile(self.in_path):
108 os.rename(self.in_path,
109 "%s/history.[timestamp:%f].[%s-counter:%04d].%s" %
113 self.in_history_counter,
117 wrpcap(self.in_path, pkts)
118 # FIXME this should be an API, but no such exists atm
119 self.test.vapi.cli(self.input_cli)
120 self.test.pg_streams.append(self.cap_name)
121 self.test.vapi.cli("trace add pg-input %d" % len(pkts))
123 def get_capture(self):
127 :returns: iterable packets
130 output = rdpcap(self.out_path)
131 except IOError: # TODO
132 error("File %s does not exist, probably because no"
133 " packets arrived" % self.out_path)
137 def create_arp_req(self):
138 """Create ARP request applicable for this interface"""
139 return (Ether(dst="ff:ff:ff:ff:ff:ff", src=self.remote_mac) /
140 ARP(op=ARP.who_has, pdst=self.local_ip4,
141 psrc=self.remote_ip4, hwsrc=self.remote_mac))
143 def create_ndp_req(self):
144 """Create NDP - NS applicable for this interface"""
145 return (Ether(dst="ff:ff:ff:ff:ff:ff", src=self.remote_mac) /
146 IPv6(src=self.remote_ip6, dst=self.local_ip6) /
147 ICMPv6ND_NS(tgt=self.local_ip6) /
148 ICMPv6NDOptSrcLLAddr(lladdr=self.remote_mac))
150 def resolve_arp(self, pg_interface=None):
151 """Resolve ARP using provided packet-generator interface
153 :param pg_interface: interface used to resolve, if None then this
157 if pg_interface is None:
159 info("Sending ARP request for %s on port %s" %
160 (self.local_ip4, pg_interface.name))
161 arp_req = self.create_arp_req()
162 pg_interface.add_stream(arp_req)
163 pg_interface.enable_capture()
165 info(self.test.vapi.cli("show trace"))
166 arp_reply = pg_interface.get_capture()
167 if arp_reply is None or len(arp_reply) == 0:
168 info("No ARP received on port %s" % pg_interface.name)
170 arp_reply = arp_reply[0]
171 # Make Dot1AD packet content recognizable to scapy
172 if arp_reply.type == 0x88a8:
173 arp_reply.type = 0x8100
174 arp_reply = Ether(str(arp_reply))
176 if arp_reply[ARP].op == ARP.is_at:
177 info("VPP %s MAC address is %s " %
178 (self.name, arp_reply[ARP].hwsrc))
179 self._local_mac = arp_reply[ARP].hwsrc
181 info("No ARP received on port %s" % pg_interface.name)
183 error("Unexpected response to ARP request:")
184 error(arp_reply.show())
187 def resolve_ndp(self, pg_interface=None):
188 """Resolve NDP using provided packet-generator interface
190 :param pg_interface: interface used to resolve, if None then this
194 if pg_interface is None:
196 info("Sending NDP request for %s on port %s" %
197 (self.local_ip6, pg_interface.name))
198 ndp_req = self.create_ndp_req()
199 pg_interface.add_stream(ndp_req)
200 pg_interface.enable_capture()
202 info(self.test.vapi.cli("show trace"))
203 ndp_reply = pg_interface.get_capture()
204 if ndp_reply is None or len(ndp_reply) == 0:
205 info("No NDP received on port %s" % pg_interface.name)
207 ndp_reply = ndp_reply[0]
208 # Make Dot1AD packet content recognizable to scapy
209 if ndp_reply.type == 0x88a8:
210 ndp_reply.type = 0x8100
211 ndp_reply = Ether(str(ndp_reply))
213 ndp_na = ndp_reply[ICMPv6ND_NA]
214 opt = ndp_na[ICMPv6NDOptDstLLAddr]
215 info("VPP %s MAC address is %s " %
216 (self.name, opt.lladdr))
217 self._local_mac = opt.lladdr
219 error("Unexpected response to NDP request:")
220 error(ndp_reply.show())