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