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