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