3 from traceback import format_exc
4 from scapy.utils import wrpcap, rdpcap, PcapReader
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,\
9 ICMPv6NDOptSrcLLAddr, ICMPv6NDOptDstLLAddr, ICMPv6ND_RA, RouterAlert, \
11 from util import ppp, ppc
15 """ Is packet one of uninteresting IPv6 broadcasts? """
16 if p.haslayer(ICMPv6ND_RA):
18 if p.haslayer(IPv6ExtHdrHopByHop):
19 for o in p[IPv6ExtHdrHopByHop].options:
20 if isinstance(o, RouterAlert):
25 class VppPGInterface(VppInterface):
27 VPP packet-generator interface
32 """packet-generator interface index assigned by VPP"""
37 """pcap file path - captured packets"""
42 """ pcap file path - injected packets"""
46 def capture_cli(self):
47 """CLI string to start capture on this interface"""
48 return self._capture_cli
52 """capture name for this interface"""
57 """CLI string to load the injected packets"""
58 return self._input_cli
61 def in_history_counter(self):
62 """Self-incrementing counter used when renaming old pcap files"""
63 v = self._in_history_counter
64 self._in_history_counter += 1
68 def out_history_counter(self):
69 """Self-incrementing counter used when renaming old pcap files"""
70 v = self._out_history_counter
71 self._out_history_counter += 1
74 def __init__(self, test, pg_index):
75 """ Create VPP packet-generator interface """
76 r = test.vapi.pg_create_interface(pg_index)
77 self._sw_if_index = r.sw_if_index
79 super(VppPGInterface, self).__init__(test)
81 self._in_history_counter = 0
82 self._out_history_counter = 0
83 self._pg_index = pg_index
84 self._out_file = "pg%u_out.pcap" % self.pg_index
85 self._out_path = self.test.tempdir + "/" + self._out_file
86 self._in_file = "pg%u_in.pcap" % self.pg_index
87 self._in_path = self.test.tempdir + "/" + self._in_file
88 self._capture_cli = "packet-generator capture pg%u pcap %s" % (
89 self.pg_index, self.out_path)
90 self._cap_name = "pcap%u" % self.sw_if_index
91 self._input_cli = "packet-generator new pcap %s source pg%u name %s" % (
92 self.in_path, self.pg_index, self.cap_name)
94 def enable_capture(self):
95 """ Enable capture on this packet-generator interface"""
97 if os.path.isfile(self.out_path):
98 os.rename(self.out_path,
99 "%s/history.[timestamp:%f].[%s-counter:%04d].%s" %
103 self.out_history_counter,
107 # FIXME this should be an API, but no such exists atm
108 self.test.vapi.cli(self.capture_cli)
109 self._pcap_reader = None
111 def add_stream(self, pkts):
113 Add a stream of packets to this packet-generator
115 :param pkts: iterable packets
119 if os.path.isfile(self.in_path):
120 os.rename(self.in_path,
121 "%s/history.[timestamp:%f].[%s-counter:%04d].%s" %
125 self.in_history_counter,
129 wrpcap(self.in_path, pkts)
130 self.test.register_capture(self.cap_name)
131 # FIXME this should be an API, but no such exists atm
132 self.test.vapi.cli(self.input_cli)
134 def _get_capture(self, timeout, filter_out_fn=is_ipv6_misc):
135 """ Helper method to get capture and filter it """
137 if not self.wait_for_capture_file(timeout):
139 output = rdpcap(self.out_path)
140 self.test.logger.debug("Capture has %s packets" % len(output.res))
142 self.test.logger.debug("Exception in scapy.rdpcap(%s): %s" %
143 (self.out_path, format_exc()))
145 before = len(output.res)
147 output.res = [p for p in output.res if not filter_out_fn(p)]
148 removed = len(output.res) - before
150 self.test.logger.debug(
151 "Filtered out %s packets from capture (returning %s)" %
152 (removed, len(output.res)))
155 def get_capture(self, expected_count=None, remark=None, timeout=1,
156 filter_out_fn=is_ipv6_misc):
157 """ Get captured packets
159 :param expected_count: expected number of packets to capture, if None,
160 then self.test.packet_count_for_dst_pg_idx is
161 used to lookup the expected count
162 :param remark: remark printed into debug logs
163 :param timeout: how long to wait for packets
164 :param filter_out_fn: filter applied to each packet, packets for which
165 the filter returns True are removed from capture
166 :returns: iterable packets
168 remaining_time = timeout
170 name = self.name if remark is None else "%s (%s)" % (self.name, remark)
171 based_on = "based on provided argument"
172 if expected_count is None:
174 self.test.get_packet_count_for_if_idx(self.sw_if_index)
175 based_on = "based on stored packet_infos"
176 self.test.logger.debug("Expecting to capture %s(%s) packets on %s" % (
177 expected_count, based_on, name))
178 if expected_count == 0:
180 "Internal error, expected packet count for %s is 0!" % name)
181 while remaining_time > 0:
183 capture = self._get_capture(remaining_time, filter_out_fn)
184 elapsed_time = time.time() - before
186 if len(capture.res) == expected_count:
187 # bingo, got the packets we expected
189 remaining_time -= elapsed_time
191 raise Exception("Captured packets mismatch, captured %s packets, "
192 "expected %s packets on %s" %
193 (len(capture.res), expected_count, name))
195 raise Exception("No packets captured on %s" % name)
197 def assert_nothing_captured(self, remark=None, filter_out_fn=is_ipv6_misc):
198 """ Assert that nothing unfiltered was captured on interface
200 :param remark: remark printed into debug logs
201 :param filter_out_fn: filter applied to each packet, packets for which
202 the filter returns True are removed from capture
204 if os.path.isfile(self.out_path):
206 capture = self.get_capture(
207 0, remark=remark, filter_out_fn=filter_out_fn)
209 if len(capture.res) == 0:
210 # junk filtered out, we're good
212 self.test.logger.error(
213 ppc("Unexpected packets captured:", capture))
217 raise AssertionError(
218 "Non-empty capture file present for interface %s(%s)" %
221 raise AssertionError(
222 "Non-empty capture file present for interface %s" %
225 def wait_for_capture_file(self, timeout=1):
227 Wait until pcap capture file appears
229 :param timeout: How long to wait for the packet (default 1s)
231 :returns: True/False if the file is present or appears within timeout
233 limit = time.time() + timeout
234 if not os.path.isfile(self.out_path):
235 self.test.logger.debug("Waiting for capture file %s to appear, "
236 "timeout is %ss" % (self.out_path, timeout))
238 self.test.logger.debug(
239 "Capture file %s already exists" %
242 while time.time() < limit:
243 if os.path.isfile(self.out_path):
245 time.sleep(0) # yield
246 if os.path.isfile(self.out_path):
247 self.test.logger.debug("Capture file appeared after %fs" %
248 (time.time() - (limit - timeout)))
250 self.test.logger.debug("Timeout - capture file still nowhere")
254 def wait_for_packet(self, timeout, filter_out_fn=is_ipv6_misc):
256 Wait for next packet captured with a timeout
258 :param timeout: How long to wait for the packet
260 :returns: Captured packet if no packet arrived within timeout
261 :raises Exception: if no packet arrives within timeout
263 deadline = time.time() + timeout
264 if self._pcap_reader is None:
265 if not self.wait_for_capture_file(timeout):
266 raise Exception("Capture file %s did not appear within "
267 "timeout" % self.out_path)
268 while time.time() < deadline:
270 self._pcap_reader = PcapReader(self.out_path)
273 self.test.logger.debug("Exception in scapy.PcapReader(%s): "
274 "%s" % (self.out_path, format_exc()))
275 if not self._pcap_reader:
276 raise Exception("Capture file %s did not appear within "
277 "timeout" % self.out_path)
279 self.test.logger.debug("Waiting for packet")
280 while time.time() < deadline:
281 p = self._pcap_reader.recv()
283 if filter_out_fn is not None and filter_out_fn(p):
284 self.test.logger.debug(
285 "Packet received after %ss was filtered out" %
286 (time.time() - (deadline - timeout)))
288 self.test.logger.debug("Packet received after %fs" %
289 (time.time() - (deadline - timeout)))
291 time.sleep(0) # yield
292 self.test.logger.debug("Timeout - no packets received")
293 raise Exception("Packet didn't arrive within timeout")
295 def create_arp_req(self):
296 """Create ARP request applicable for this interface"""
297 return (Ether(dst="ff:ff:ff:ff:ff:ff", src=self.remote_mac) /
298 ARP(op=ARP.who_has, pdst=self.local_ip4,
299 psrc=self.remote_ip4, hwsrc=self.remote_mac))
301 def create_ndp_req(self):
302 """Create NDP - NS applicable for this interface"""
303 return (Ether(dst="ff:ff:ff:ff:ff:ff", src=self.remote_mac) /
304 IPv6(src=self.remote_ip6, dst=self.local_ip6) /
305 ICMPv6ND_NS(tgt=self.local_ip6) /
306 ICMPv6NDOptSrcLLAddr(lladdr=self.remote_mac))
308 def resolve_arp(self, pg_interface=None):
309 """Resolve ARP using provided packet-generator interface
311 :param pg_interface: interface used to resolve, if None then this
315 if pg_interface is None:
317 self.test.logger.info("Sending ARP request for %s on port %s" %
318 (self.local_ip4, pg_interface.name))
319 arp_req = self.create_arp_req()
320 pg_interface.add_stream(arp_req)
321 pg_interface.enable_capture()
323 self.test.logger.info(self.test.vapi.cli("show trace"))
325 captured_packet = pg_interface.wait_for_packet(1)
327 self.test.logger.info("No ARP received on port %s" %
330 arp_reply = captured_packet.copy() # keep original for exception
331 # Make Dot1AD packet content recognizable to scapy
332 if arp_reply.type == 0x88a8:
333 arp_reply.type = 0x8100
334 arp_reply = Ether(str(arp_reply))
336 if arp_reply[ARP].op == ARP.is_at:
337 self.test.logger.info("VPP %s MAC address is %s " %
338 (self.name, arp_reply[ARP].hwsrc))
339 self._local_mac = arp_reply[ARP].hwsrc
341 self.test.logger.info("No ARP received on port %s" %
344 self.test.logger.error(
345 ppp("Unexpected response to ARP request:", captured_packet))
348 def resolve_ndp(self, pg_interface=None, timeout=1):
349 """Resolve NDP using provided packet-generator interface
351 :param pg_interface: interface used to resolve, if None then this
353 :param timeout: how long to wait for response before giving up
356 if pg_interface is None:
358 self.test.logger.info("Sending NDP request for %s on port %s" %
359 (self.local_ip6, pg_interface.name))
360 ndp_req = self.create_ndp_req()
361 pg_interface.add_stream(ndp_req)
362 pg_interface.enable_capture()
365 deadline = now + timeout
366 # Enabling IPv6 on an interface can generate more than the
367 # ND reply we are looking for (namely MLD). So loop through
368 # the replies to look for want we want.
369 while now < deadline:
371 captured_packet = pg_interface.wait_for_packet(
372 deadline - now, filter_out_fn=None)
374 self.test.logger.error("Timeout while waiting for NDP response")
376 ndp_reply = captured_packet.copy() # keep original for exception
377 # Make Dot1AD packet content recognizable to scapy
378 if ndp_reply.type == 0x88a8:
379 ndp_reply.type = 0x8100
380 ndp_reply = Ether(str(ndp_reply))
382 ndp_na = ndp_reply[ICMPv6ND_NA]
383 opt = ndp_na[ICMPv6NDOptDstLLAddr]
384 self.test.logger.info("VPP %s MAC address is %s " %
385 (self.name, opt.lladdr))
386 self._local_mac = opt.lladdr
387 self.test.logger.debug(self.test.vapi.cli("show trace"))
388 # we now have the MAC we've been after
391 self.test.logger.info(
392 ppp("Unexpected response to NDP request:", captured_packet))
395 self.test.logger.debug(self.test.vapi.cli("show trace"))
396 raise Exception("Timeout while waiting for NDP response")