Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 1 | import os |
Klement Sekera | 778c276 | 2016-11-08 02:00:28 +0100 | [diff] [blame] | 2 | import time |
Neale Ranns | 465a1a3 | 2017-01-07 10:04:09 -0800 | [diff] [blame] | 3 | import socket |
Klement Sekera | b91017a | 2017-02-09 06:04:36 +0100 | [diff] [blame] | 4 | import struct |
Klement Sekera | 97f6edc | 2017-01-12 07:17:01 +0100 | [diff] [blame] | 5 | from traceback import format_exc, format_stack |
Paul Vinciguerra | a7427ec | 2019-03-10 10:04:23 -0700 | [diff] [blame] | 6 | |
| 7 | import scapy.compat |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 8 | from scapy.utils import wrpcap, rdpcap, PcapReader |
Klement Sekera | 97f6edc | 2017-01-12 07:17:01 +0100 | [diff] [blame] | 9 | from scapy.plist import PacketList |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 10 | from vpp_interface import VppInterface |
| 11 | |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 12 | from scapy.layers.l2 import Ether, ARP |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 13 | from scapy.layers.inet6 import IPv6, ICMPv6ND_NS, ICMPv6ND_NA,\ |
Klement Sekera | 65cc8c0 | 2016-12-18 15:49:54 +0100 | [diff] [blame] | 14 | ICMPv6NDOptSrcLLAddr, ICMPv6NDOptDstLLAddr, ICMPv6ND_RA, RouterAlert, \ |
| 15 | IPv6ExtHdrHopByHop |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 16 | from util import ppp, ppc |
Neale Ranns | 7515228 | 2017-01-09 01:00:45 -0800 | [diff] [blame] | 17 | from scapy.utils6 import in6_getnsma, in6_getnsmac, in6_ismaddr |
Neale Ranns | 465a1a3 | 2017-01-07 10:04:09 -0800 | [diff] [blame] | 18 | from scapy.utils import inet_pton, inet_ntop |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 19 | |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 20 | |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 21 | class CaptureTimeoutError(Exception): |
| 22 | """ Exception raised if capture or packet doesn't appear within timeout """ |
| 23 | pass |
| 24 | |
| 25 | |
Klement Sekera | 65cc8c0 | 2016-12-18 15:49:54 +0100 | [diff] [blame] | 26 | def is_ipv6_misc(p): |
| 27 | """ Is packet one of uninteresting IPv6 broadcasts? """ |
| 28 | if p.haslayer(ICMPv6ND_RA): |
Neale Ranns | 7515228 | 2017-01-09 01:00:45 -0800 | [diff] [blame] | 29 | if in6_ismaddr(p[IPv6].dst): |
| 30 | return True |
Klement Sekera | 65cc8c0 | 2016-12-18 15:49:54 +0100 | [diff] [blame] | 31 | if p.haslayer(IPv6ExtHdrHopByHop): |
| 32 | for o in p[IPv6ExtHdrHopByHop].options: |
| 33 | if isinstance(o, RouterAlert): |
| 34 | return True |
| 35 | return False |
| 36 | |
| 37 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 38 | class VppPGInterface(VppInterface): |
| 39 | """ |
| 40 | VPP packet-generator interface |
| 41 | """ |
| 42 | |
| 43 | @property |
| 44 | def pg_index(self): |
| 45 | """packet-generator interface index assigned by VPP""" |
| 46 | return self._pg_index |
| 47 | |
| 48 | @property |
| 49 | def out_path(self): |
| 50 | """pcap file path - captured packets""" |
| 51 | return self._out_path |
| 52 | |
| 53 | @property |
| 54 | def in_path(self): |
| 55 | """ pcap file path - injected packets""" |
| 56 | return self._in_path |
| 57 | |
| 58 | @property |
| 59 | def capture_cli(self): |
| 60 | """CLI string to start capture on this interface""" |
| 61 | return self._capture_cli |
| 62 | |
| 63 | @property |
| 64 | def cap_name(self): |
| 65 | """capture name for this interface""" |
| 66 | return self._cap_name |
| 67 | |
| 68 | @property |
| 69 | def input_cli(self): |
| 70 | """CLI string to load the injected packets""" |
Alexandre Poirrier | a618e20 | 2019-05-07 10:43:41 +0200 | [diff] [blame] | 71 | if self._nb_replays is not None: |
| 72 | return "%s limit %d" % (self._input_cli, self._nb_replays) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 73 | return self._input_cli |
| 74 | |
Klement Sekera | 778c276 | 2016-11-08 02:00:28 +0100 | [diff] [blame] | 75 | @property |
| 76 | def in_history_counter(self): |
| 77 | """Self-incrementing counter used when renaming old pcap files""" |
| 78 | v = self._in_history_counter |
| 79 | self._in_history_counter += 1 |
| 80 | return v |
| 81 | |
| 82 | @property |
| 83 | def out_history_counter(self): |
| 84 | """Self-incrementing counter used when renaming old pcap files""" |
| 85 | v = self._out_history_counter |
| 86 | self._out_history_counter += 1 |
| 87 | return v |
| 88 | |
Matej Klotton | c5bf07f | 2016-11-23 15:27:17 +0100 | [diff] [blame] | 89 | def __init__(self, test, pg_index): |
| 90 | """ Create VPP packet-generator interface """ |
Ole Troan | e0d2bd6 | 2018-06-22 22:36:46 +0200 | [diff] [blame] | 91 | super(VppPGInterface, self).__init__(test) |
Klement Sekera | a98346f | 2018-05-16 10:52:45 +0200 | [diff] [blame] | 92 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 93 | r = test.vapi.pg_create_interface(pg_index) |
| 94 | self.set_sw_if_index(r.sw_if_index) |
| 95 | |
Matej Klotton | c5bf07f | 2016-11-23 15:27:17 +0100 | [diff] [blame] | 96 | self._in_history_counter = 0 |
| 97 | self._out_history_counter = 0 |
Klement Sekera | 97f6edc | 2017-01-12 07:17:01 +0100 | [diff] [blame] | 98 | self._out_assert_counter = 0 |
Matej Klotton | c5bf07f | 2016-11-23 15:27:17 +0100 | [diff] [blame] | 99 | self._pg_index = pg_index |
Klement Sekera | 74dcdbf | 2016-11-14 09:49:09 +0100 | [diff] [blame] | 100 | self._out_file = "pg%u_out.pcap" % self.pg_index |
Klement Sekera | 778c276 | 2016-11-08 02:00:28 +0100 | [diff] [blame] | 101 | self._out_path = self.test.tempdir + "/" + self._out_file |
Klement Sekera | 74dcdbf | 2016-11-14 09:49:09 +0100 | [diff] [blame] | 102 | self._in_file = "pg%u_in.pcap" % self.pg_index |
Klement Sekera | 778c276 | 2016-11-08 02:00:28 +0100 | [diff] [blame] | 103 | self._in_path = self.test.tempdir + "/" + self._in_file |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 104 | self._capture_cli = "packet-generator capture pg%u pcap %s" % ( |
| 105 | self.pg_index, self.out_path) |
Paul Vinciguerra | 44b0b07 | 2019-06-25 20:51:31 -0400 | [diff] [blame] | 106 | self._cap_name = "pcap%u-sw_if_index-%s" % ( |
| 107 | self.pg_index, self.sw_if_index) |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 108 | self._input_cli = \ |
| 109 | "packet-generator new pcap %s source pg%u name %s" % ( |
| 110 | self.in_path, self.pg_index, self.cap_name) |
Alexandre Poirrier | a618e20 | 2019-05-07 10:43:41 +0200 | [diff] [blame] | 111 | self._nb_replays = None |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 112 | |
Paul Vinciguerra | 4b58a86 | 2019-05-28 15:40:47 -0400 | [diff] [blame] | 113 | def _rename_previous_capture_file(self, path, counter, file): |
| 114 | # if a file from a previous capture exists, rename it. |
| 115 | try: |
| 116 | if os.path.isfile(path): |
| 117 | name = "%s/history.[timestamp:%f].[%s-counter:%04d].%s" % \ |
| 118 | (self.test.tempdir, |
| 119 | time.time(), |
| 120 | self.name, |
| 121 | counter, |
| 122 | file) |
| 123 | self.test.logger.debug("Renaming %s->%s" % |
| 124 | (path, name)) |
| 125 | os.rename(path, name) |
| 126 | except OSError: |
| 127 | self.test.logger.debug("OSError: Could not rename %s %s" % |
| 128 | (path, file)) |
| 129 | |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 130 | def enable_capture(self): |
Alexandre Poirrier | a618e20 | 2019-05-07 10:43:41 +0200 | [diff] [blame] | 131 | """ Enable capture on this packet-generator interface |
| 132 | of at most n packets. |
| 133 | If n < 0, this is no limit |
| 134 | """ |
Paul Vinciguerra | 4b58a86 | 2019-05-28 15:40:47 -0400 | [diff] [blame] | 135 | |
| 136 | self._rename_previous_capture_file(self.out_path, |
| 137 | self.out_history_counter, |
| 138 | self._out_file) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 139 | # FIXME this should be an API, but no such exists atm |
| 140 | self.test.vapi.cli(self.capture_cli) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 141 | self._pcap_reader = None |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 142 | |
Alexandre Poirrier | a618e20 | 2019-05-07 10:43:41 +0200 | [diff] [blame] | 143 | def disable_capture(self): |
| 144 | self.test.vapi.cli("%s disable" % self.capture_cli) |
| 145 | |
| 146 | def add_stream(self, pkts, nb_replays=None): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 147 | """ |
| 148 | Add a stream of packets to this packet-generator |
| 149 | |
| 150 | :param pkts: iterable packets |
| 151 | |
| 152 | """ |
Alexandre Poirrier | a618e20 | 2019-05-07 10:43:41 +0200 | [diff] [blame] | 153 | self._nb_replays = nb_replays |
Paul Vinciguerra | 4b58a86 | 2019-05-28 15:40:47 -0400 | [diff] [blame] | 154 | self._rename_previous_capture_file(self.in_path, |
| 155 | self.in_history_counter, |
| 156 | self._in_file) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 157 | wrpcap(self.in_path, pkts) |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 158 | self.test.register_capture(self.cap_name) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 159 | # FIXME this should be an API, but no such exists atm |
| 160 | self.test.vapi.cli(self.input_cli) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 161 | |
Klement Sekera | 97f6edc | 2017-01-12 07:17:01 +0100 | [diff] [blame] | 162 | def generate_debug_aid(self, kind): |
| 163 | """ Create a hardlink to the out file with a counter and a file |
| 164 | containing stack trace to ease debugging in case of multiple capture |
| 165 | files present. """ |
| 166 | self.test.logger.debug("Generating debug aid for %s on %s" % |
| 167 | (kind, self._name)) |
| 168 | link_path, stack_path = ["%s/debug_%s_%s_%s.%s" % |
| 169 | (self.test.tempdir, self._name, |
| 170 | self._out_assert_counter, kind, suffix) |
| 171 | for suffix in ["pcap", "stack"] |
| 172 | ] |
| 173 | os.link(self.out_path, link_path) |
| 174 | with open(stack_path, "w") as f: |
| 175 | f.writelines(format_stack()) |
| 176 | self._out_assert_counter += 1 |
| 177 | |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 178 | def _get_capture(self, timeout, filter_out_fn=is_ipv6_misc): |
| 179 | """ Helper method to get capture and filter it """ |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 180 | try: |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 181 | if not self.wait_for_capture_file(timeout): |
| 182 | return None |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 183 | output = rdpcap(self.out_path) |
Klement Sekera | 65cc8c0 | 2016-12-18 15:49:54 +0100 | [diff] [blame] | 184 | self.test.logger.debug("Capture has %s packets" % len(output.res)) |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 185 | except: |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 186 | self.test.logger.debug("Exception in scapy.rdpcap (%s): %s" % |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 187 | (self.out_path, format_exc())) |
| 188 | return None |
Klement Sekera | 65cc8c0 | 2016-12-18 15:49:54 +0100 | [diff] [blame] | 189 | before = len(output.res) |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 190 | if filter_out_fn: |
| 191 | output.res = [p for p in output.res if not filter_out_fn(p)] |
Klement Sekera | 97f6edc | 2017-01-12 07:17:01 +0100 | [diff] [blame] | 192 | removed = before - len(output.res) |
Klement Sekera | 65cc8c0 | 2016-12-18 15:49:54 +0100 | [diff] [blame] | 193 | if removed: |
| 194 | self.test.logger.debug( |
| 195 | "Filtered out %s packets from capture (returning %s)" % |
| 196 | (removed, len(output.res))) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 197 | return output |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 198 | |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 199 | def get_capture(self, expected_count=None, remark=None, timeout=1, |
| 200 | filter_out_fn=is_ipv6_misc): |
| 201 | """ Get captured packets |
| 202 | |
| 203 | :param expected_count: expected number of packets to capture, if None, |
| 204 | then self.test.packet_count_for_dst_pg_idx is |
| 205 | used to lookup the expected count |
| 206 | :param remark: remark printed into debug logs |
| 207 | :param timeout: how long to wait for packets |
| 208 | :param filter_out_fn: filter applied to each packet, packets for which |
| 209 | the filter returns True are removed from capture |
| 210 | :returns: iterable packets |
| 211 | """ |
| 212 | remaining_time = timeout |
| 213 | capture = None |
| 214 | name = self.name if remark is None else "%s (%s)" % (self.name, remark) |
| 215 | based_on = "based on provided argument" |
| 216 | if expected_count is None: |
| 217 | expected_count = \ |
| 218 | self.test.get_packet_count_for_if_idx(self.sw_if_index) |
| 219 | based_on = "based on stored packet_infos" |
Klement Sekera | c86fa02 | 2017-01-02 09:03:47 +0100 | [diff] [blame] | 220 | if expected_count == 0: |
| 221 | raise Exception( |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 222 | "Internal error, expected packet count for %s is 0!" % |
| 223 | name) |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 224 | self.test.logger.debug("Expecting to capture %s (%s) packets on %s" % ( |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 225 | expected_count, based_on, name)) |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 226 | while remaining_time > 0: |
| 227 | before = time.time() |
| 228 | capture = self._get_capture(remaining_time, filter_out_fn) |
| 229 | elapsed_time = time.time() - before |
| 230 | if capture: |
| 231 | if len(capture.res) == expected_count: |
| 232 | # bingo, got the packets we expected |
| 233 | return capture |
Jan Gelety | 057bb8c | 2016-12-20 17:32:45 +0100 | [diff] [blame] | 234 | elif len(capture.res) > expected_count: |
| 235 | self.test.logger.error( |
| 236 | ppc("Unexpected packets captured:", capture)) |
| 237 | break |
Klement Sekera | 97f6edc | 2017-01-12 07:17:01 +0100 | [diff] [blame] | 238 | else: |
| 239 | self.test.logger.debug("Partial capture containing %s " |
| 240 | "packets doesn't match expected " |
| 241 | "count %s (yet?)" % |
| 242 | (len(capture.res), expected_count)) |
| 243 | elif expected_count == 0: |
| 244 | # bingo, got None as we expected - return empty capture |
| 245 | return PacketList() |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 246 | remaining_time -= elapsed_time |
| 247 | if capture: |
Klement Sekera | 97f6edc | 2017-01-12 07:17:01 +0100 | [diff] [blame] | 248 | self.generate_debug_aid("count-mismatch") |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 249 | raise Exception("Captured packets mismatch, captured %s packets, " |
| 250 | "expected %s packets on %s" % |
| 251 | (len(capture.res), expected_count, name)) |
| 252 | else: |
| 253 | raise Exception("No packets captured on %s" % name) |
| 254 | |
| 255 | def assert_nothing_captured(self, remark=None, filter_out_fn=is_ipv6_misc): |
| 256 | """ Assert that nothing unfiltered was captured on interface |
| 257 | |
| 258 | :param remark: remark printed into debug logs |
| 259 | :param filter_out_fn: filter applied to each packet, packets for which |
| 260 | the filter returns True are removed from capture |
| 261 | """ |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 262 | if os.path.isfile(self.out_path): |
| 263 | try: |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 264 | capture = self.get_capture( |
| 265 | 0, remark=remark, filter_out_fn=filter_out_fn) |
Klement Sekera | 97f6edc | 2017-01-12 07:17:01 +0100 | [diff] [blame] | 266 | if not capture or len(capture.res) == 0: |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 267 | # junk filtered out, we're good |
| 268 | return |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 269 | except: |
| 270 | pass |
Klement Sekera | 97f6edc | 2017-01-12 07:17:01 +0100 | [diff] [blame] | 271 | self.generate_debug_aid("empty-assert") |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 272 | if remark: |
| 273 | raise AssertionError( |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 274 | "Non-empty capture file present for interface %s (%s)" % |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 275 | (self.name, remark)) |
| 276 | else: |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 277 | raise AssertionError("Capture file present for interface %s" % |
| 278 | self.name) |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 279 | |
| 280 | def wait_for_capture_file(self, timeout=1): |
| 281 | """ |
| 282 | Wait until pcap capture file appears |
| 283 | |
| 284 | :param timeout: How long to wait for the packet (default 1s) |
| 285 | |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 286 | :returns: True/False if the file is present or appears within timeout |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 287 | """ |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 288 | deadline = time.time() + timeout |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 289 | if not os.path.isfile(self.out_path): |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 290 | self.test.logger.debug("Waiting for capture file %s to appear, " |
| 291 | "timeout is %ss" % (self.out_path, timeout)) |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 292 | else: |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 293 | self.test.logger.debug("Capture file %s already exists" % |
| 294 | self.out_path) |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 295 | return True |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 296 | while time.time() < deadline: |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 297 | if os.path.isfile(self.out_path): |
| 298 | break |
Paul Vinciguerra | 0f6602c | 2019-03-10 09:10:54 -0700 | [diff] [blame] | 299 | self._test.sleep(0) # yield |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 300 | if os.path.isfile(self.out_path): |
| 301 | self.test.logger.debug("Capture file appeared after %fs" % |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 302 | (time.time() - (deadline - timeout))) |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 303 | else: |
| 304 | self.test.logger.debug("Timeout - capture file still nowhere") |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 305 | return False |
| 306 | return True |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 307 | |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 308 | def verify_enough_packet_data_in_pcap(self): |
Klement Sekera | b91017a | 2017-02-09 06:04:36 +0100 | [diff] [blame] | 309 | """ |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 310 | Check if enough data is available in file handled by internal pcap |
| 311 | reader so that a whole packet can be read. |
Klement Sekera | b91017a | 2017-02-09 06:04:36 +0100 | [diff] [blame] | 312 | |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 313 | :returns: True if enough data present, else False |
Klement Sekera | b91017a | 2017-02-09 06:04:36 +0100 | [diff] [blame] | 314 | """ |
| 315 | orig_pos = self._pcap_reader.f.tell() # save file position |
| 316 | enough_data = False |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 317 | # read packet header from pcap |
| 318 | packet_header_size = 16 |
| 319 | caplen = None |
| 320 | end_pos = None |
| 321 | hdr = self._pcap_reader.f.read(packet_header_size) |
| 322 | if len(hdr) == packet_header_size: |
| 323 | # parse the capture length - caplen |
Klement Sekera | b91017a | 2017-02-09 06:04:36 +0100 | [diff] [blame] | 324 | sec, usec, caplen, wirelen = struct.unpack( |
| 325 | self._pcap_reader.endian + "IIII", hdr) |
| 326 | self._pcap_reader.f.seek(0, 2) # seek to end of file |
| 327 | end_pos = self._pcap_reader.f.tell() # get position at end |
| 328 | if end_pos >= orig_pos + len(hdr) + caplen: |
| 329 | enough_data = True # yay, we have enough data |
Klement Sekera | b91017a | 2017-02-09 06:04:36 +0100 | [diff] [blame] | 330 | self._pcap_reader.f.seek(orig_pos, 0) # restore original position |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 331 | return enough_data |
Klement Sekera | b91017a | 2017-02-09 06:04:36 +0100 | [diff] [blame] | 332 | |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 333 | def wait_for_packet(self, timeout, filter_out_fn=is_ipv6_misc): |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 334 | """ |
| 335 | Wait for next packet captured with a timeout |
| 336 | |
| 337 | :param timeout: How long to wait for the packet |
| 338 | |
| 339 | :returns: Captured packet if no packet arrived within timeout |
| 340 | :raises Exception: if no packet arrives within timeout |
| 341 | """ |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 342 | deadline = time.time() + timeout |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 343 | if self._pcap_reader is None: |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 344 | if not self.wait_for_capture_file(timeout): |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 345 | raise CaptureTimeoutError("Capture file %s did not appear " |
| 346 | "within timeout" % self.out_path) |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 347 | while time.time() < deadline: |
| 348 | try: |
| 349 | self._pcap_reader = PcapReader(self.out_path) |
| 350 | break |
| 351 | except: |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 352 | self.test.logger.debug( |
Klement Sekera | 97f6edc | 2017-01-12 07:17:01 +0100 | [diff] [blame] | 353 | "Exception in scapy.PcapReader(%s): %s" % |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 354 | (self.out_path, format_exc())) |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 355 | if not self._pcap_reader: |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 356 | raise CaptureTimeoutError("Capture file %s did not appear within " |
| 357 | "timeout" % self.out_path) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 358 | |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 359 | poll = False |
| 360 | if timeout > 0: |
| 361 | self.test.logger.debug("Waiting for packet") |
| 362 | else: |
| 363 | poll = True |
| 364 | self.test.logger.debug("Polling for packet") |
| 365 | while time.time() < deadline or poll: |
| 366 | if not self.verify_enough_packet_data_in_pcap(): |
Paul Vinciguerra | 0f6602c | 2019-03-10 09:10:54 -0700 | [diff] [blame] | 367 | self._test.sleep(0) # yield |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 368 | poll = False |
| 369 | continue |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 370 | p = self._pcap_reader.recv() |
| 371 | if p is not None: |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 372 | if filter_out_fn is not None and filter_out_fn(p): |
| 373 | self.test.logger.debug( |
| 374 | "Packet received after %ss was filtered out" % |
| 375 | (time.time() - (deadline - timeout))) |
| 376 | else: |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 377 | self.test.logger.debug( |
| 378 | "Packet received after %fs" % |
| 379 | (time.time() - (deadline - timeout))) |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 380 | return p |
Paul Vinciguerra | 0f6602c | 2019-03-10 09:10:54 -0700 | [diff] [blame] | 381 | self._test.sleep(0) # yield |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 382 | poll = False |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 383 | self.test.logger.debug("Timeout - no packets received") |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 384 | raise CaptureTimeoutError("Packet didn't arrive within timeout") |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 385 | |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 386 | def create_arp_req(self): |
| 387 | """Create ARP request applicable for this interface""" |
| 388 | return (Ether(dst="ff:ff:ff:ff:ff:ff", src=self.remote_mac) / |
| 389 | ARP(op=ARP.who_has, pdst=self.local_ip4, |
| 390 | psrc=self.remote_ip4, hwsrc=self.remote_mac)) |
| 391 | |
| 392 | def create_ndp_req(self): |
| 393 | """Create NDP - NS applicable for this interface""" |
Neale Ranns | 465a1a3 | 2017-01-07 10:04:09 -0800 | [diff] [blame] | 394 | nsma = in6_getnsma(inet_pton(socket.AF_INET6, self.local_ip6)) |
| 395 | d = inet_ntop(socket.AF_INET6, nsma) |
| 396 | |
| 397 | return (Ether(dst=in6_getnsmac(nsma)) / |
| 398 | IPv6(dst=d, src=self.remote_ip6) / |
Klement Sekera | 74dcdbf | 2016-11-14 09:49:09 +0100 | [diff] [blame] | 399 | ICMPv6ND_NS(tgt=self.local_ip6) / |
| 400 | ICMPv6NDOptSrcLLAddr(lladdr=self.remote_mac)) |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 401 | |
| 402 | def resolve_arp(self, pg_interface=None): |
| 403 | """Resolve ARP using provided packet-generator interface |
| 404 | |
| 405 | :param pg_interface: interface used to resolve, if None then this |
| 406 | interface is used |
| 407 | |
| 408 | """ |
| 409 | if pg_interface is None: |
| 410 | pg_interface = self |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 411 | self.test.logger.info("Sending ARP request for %s on port %s" % |
| 412 | (self.local_ip4, pg_interface.name)) |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 413 | arp_req = self.create_arp_req() |
| 414 | pg_interface.add_stream(arp_req) |
| 415 | pg_interface.enable_capture() |
| 416 | self.test.pg_start() |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 417 | self.test.logger.info(self.test.vapi.cli("show trace")) |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 418 | try: |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 419 | captured_packet = pg_interface.wait_for_packet(1) |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 420 | except: |
| 421 | self.test.logger.info("No ARP received on port %s" % |
| 422 | pg_interface.name) |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 423 | return |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 424 | arp_reply = captured_packet.copy() # keep original for exception |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 425 | try: |
| 426 | if arp_reply[ARP].op == ARP.is_at: |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 427 | self.test.logger.info("VPP %s MAC address is %s " % |
| 428 | (self.name, arp_reply[ARP].hwsrc)) |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 429 | self._local_mac = arp_reply[ARP].hwsrc |
| 430 | else: |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 431 | self.test.logger.info("No ARP received on port %s" % |
| 432 | pg_interface.name) |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 433 | except: |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 434 | self.test.logger.error( |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 435 | ppp("Unexpected response to ARP request:", captured_packet)) |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 436 | raise |
| 437 | |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 438 | def resolve_ndp(self, pg_interface=None, timeout=1): |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 439 | """Resolve NDP using provided packet-generator interface |
| 440 | |
| 441 | :param pg_interface: interface used to resolve, if None then this |
| 442 | interface is used |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 443 | :param timeout: how long to wait for response before giving up |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 444 | |
| 445 | """ |
| 446 | if pg_interface is None: |
| 447 | pg_interface = self |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 448 | self.test.logger.info("Sending NDP request for %s on port %s" % |
| 449 | (self.local_ip6, pg_interface.name)) |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 450 | ndp_req = self.create_ndp_req() |
| 451 | pg_interface.add_stream(ndp_req) |
| 452 | pg_interface.enable_capture() |
| 453 | self.test.pg_start() |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 454 | now = time.time() |
| 455 | deadline = now + timeout |
Neale Ranns | 82a06a9 | 2016-12-08 20:05:33 +0000 | [diff] [blame] | 456 | # Enabling IPv6 on an interface can generate more than the |
| 457 | # ND reply we are looking for (namely MLD). So loop through |
| 458 | # the replies to look for want we want. |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 459 | while now < deadline: |
| 460 | try: |
| 461 | captured_packet = pg_interface.wait_for_packet( |
| 462 | deadline - now, filter_out_fn=None) |
| 463 | except: |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 464 | self.test.logger.error( |
| 465 | "Timeout while waiting for NDP response") |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 466 | raise |
| 467 | ndp_reply = captured_packet.copy() # keep original for exception |
Neale Ranns | 82a06a9 | 2016-12-08 20:05:33 +0000 | [diff] [blame] | 468 | try: |
| 469 | ndp_na = ndp_reply[ICMPv6ND_NA] |
| 470 | opt = ndp_na[ICMPv6NDOptDstLLAddr] |
| 471 | self.test.logger.info("VPP %s MAC address is %s " % |
| 472 | (self.name, opt.lladdr)) |
| 473 | self._local_mac = opt.lladdr |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 474 | self.test.logger.debug(self.test.vapi.cli("show trace")) |
| 475 | # we now have the MAC we've been after |
| 476 | return |
Neale Ranns | 82a06a9 | 2016-12-08 20:05:33 +0000 | [diff] [blame] | 477 | except: |
| 478 | self.test.logger.info( |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 479 | ppp("Unexpected response to NDP request:", |
| 480 | captured_packet)) |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 481 | now = time.time() |
| 482 | |
| 483 | self.test.logger.debug(self.test.vapi.cli("show trace")) |
| 484 | raise Exception("Timeout while waiting for NDP response") |