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