+ def get_capture(self, expected_count=None, remark=None, timeout=1,
+ filter_out_fn=is_ipv6_misc):
+ """ Get captured packets
+
+ :param expected_count: expected number of packets to capture, if None,
+ then self.test.packet_count_for_dst_pg_idx is
+ used to lookup the expected count
+ :param remark: remark printed into debug logs
+ :param timeout: how long to wait for packets
+ :param filter_out_fn: filter applied to each packet, packets for which
+ the filter returns True are removed from capture
+ :returns: iterable packets
+ """
+ remaining_time = timeout
+ capture = None
+ name = self.name if remark is None else "%s (%s)" % (self.name, remark)
+ based_on = "based on provided argument"
+ if expected_count is None:
+ expected_count = \
+ self.test.get_packet_count_for_if_idx(self.sw_if_index)
+ based_on = "based on stored packet_infos"
+ if expected_count == 0:
+ raise Exception(
+ "Internal error, expected packet count for %s is 0!" %
+ name)
+ self.test.logger.debug("Expecting to capture %s (%s) packets on %s" % (
+ expected_count, based_on, name))
+ if expected_count == 0:
+ raise Exception(
+ "Internal error, expected packet count for %s is 0!" % name)
+ while remaining_time > 0:
+ before = time.time()
+ capture = self._get_capture(remaining_time, filter_out_fn)
+ elapsed_time = time.time() - before
+ if capture:
+ if len(capture.res) == expected_count:
+ # bingo, got the packets we expected
+ return capture
+ remaining_time -= elapsed_time
+ if capture:
+ raise Exception("Captured packets mismatch, captured %s packets, "
+ "expected %s packets on %s" %
+ (len(capture.res), expected_count, name))
+ else:
+ raise Exception("No packets captured on %s" % name)
+
+ def assert_nothing_captured(self, remark=None, filter_out_fn=is_ipv6_misc):
+ """ Assert that nothing unfiltered was captured on interface
+
+ :param remark: remark printed into debug logs
+ :param filter_out_fn: filter applied to each packet, packets for which
+ the filter returns True are removed from capture
+ """
+ if os.path.isfile(self.out_path):
+ try:
+ capture = self.get_capture(
+ 0, remark=remark, filter_out_fn=filter_out_fn)
+ if not capture:
+ # junk filtered out, we're good
+ return
+ self.test.logger.error(
+ ppc("Unexpected packets captured:", capture))
+ except:
+ pass
+ if remark:
+ raise AssertionError(
+ "Non-empty capture file present for interface %s (%s)" %
+ (self.name, remark))
+ else:
+ raise AssertionError("Capture file present for interface %s" %
+ self.name)
+
+ def wait_for_capture_file(self, timeout=1):
+ """
+ Wait until pcap capture file appears
+
+ :param timeout: How long to wait for the packet (default 1s)
+
+ :returns: True/False if the file is present or appears within timeout
+ """
+ limit = time.time() + timeout
+ if not os.path.isfile(self.out_path):
+ self.test.logger.debug("Waiting for capture file %s to appear, "
+ "timeout is %ss" % (self.out_path, timeout))
+ else:
+ self.test.logger.debug(
+ "Capture file %s already exists" %
+ self.out_path)
+ return True
+ while time.time() < limit:
+ if os.path.isfile(self.out_path):
+ break
+ time.sleep(0) # yield
+ if os.path.isfile(self.out_path):
+ self.test.logger.debug("Capture file appeared after %fs" %
+ (time.time() - (limit - timeout)))
+ else:
+ self.test.logger.debug("Timeout - capture file still nowhere")
+ return False
+ return True
+
+ def wait_for_packet(self, timeout, filter_out_fn=is_ipv6_misc):
+ """
+ Wait for next packet captured with a timeout
+
+ :param timeout: How long to wait for the packet
+
+ :returns: Captured packet if no packet arrived within timeout
+ :raises Exception: if no packet arrives within timeout
+ """
+ deadline = time.time() + timeout
+ if self._pcap_reader is None:
+ if not self.wait_for_capture_file(timeout):
+ raise Exception("Capture file %s did not appear within "
+ "timeout" % self.out_path)
+ while time.time() < deadline:
+ try:
+ self._pcap_reader = PcapReader(self.out_path)
+ break
+ except:
+ self.test.logger.debug(
+ "Exception in scapy.PcapReader(%s): "
+ "%s" %
+ (self.out_path, format_exc()))
+ if not self._pcap_reader:
+ raise Exception("Capture file %s did not appear within "
+ "timeout" % self.out_path)
+
+ self.test.logger.debug("Waiting for packet")
+ while time.time() < deadline:
+ p = self._pcap_reader.recv()
+ if p is not None:
+ if filter_out_fn is not None and filter_out_fn(p):
+ self.test.logger.debug(
+ "Packet received after %ss was filtered out" %
+ (time.time() - (deadline - timeout)))
+ else:
+ self.test.logger.debug(
+ "Packet received after %fs" %
+ (time.time() - (deadline - timeout)))
+ return p
+ time.sleep(0) # yield
+ self.test.logger.debug("Timeout - no packets received")
+ raise Exception("Packet didn't arrive within timeout")
+