blob: 380c1fa2595345e06bb129d83f6b631fc286bd8a [file] [log] [blame]
Klement Sekeraf62ae122016-10-11 11:47:09 +02001import os
Klement Sekera778c2762016-11-08 02:00:28 +01002import time
Neale Ranns465a1a32017-01-07 10:04:09 -08003import socket
Klement Sekerab91017a2017-02-09 06:04:36 +01004import struct
Klement Sekera97f6edc2017-01-12 07:17:01 +01005from traceback import format_exc, format_stack
Paul Vinciguerraa7427ec2019-03-10 10:04:23 -07006
7import scapy.compat
Klement Sekera0e3c0de2016-09-29 14:43:44 +02008from scapy.utils import wrpcap, rdpcap, PcapReader
Klement Sekera97f6edc2017-01-12 07:17:01 +01009from scapy.plist import PacketList
Klement Sekeraf62ae122016-10-11 11:47:09 +020010from vpp_interface import VppInterface
11
Matej Klotton0178d522016-11-04 11:11:44 +010012from scapy.layers.l2 import Ether, ARP
Klement Sekera0e3c0de2016-09-29 14:43:44 +020013from scapy.layers.inet6 import IPv6, ICMPv6ND_NS, ICMPv6ND_NA,\
Klement Sekera65cc8c02016-12-18 15:49:54 +010014 ICMPv6NDOptSrcLLAddr, ICMPv6NDOptDstLLAddr, ICMPv6ND_RA, RouterAlert, \
15 IPv6ExtHdrHopByHop
Klement Sekera9225dee2016-12-12 08:36:58 +010016from util import ppp, ppc
Neale Ranns75152282017-01-09 01:00:45 -080017from scapy.utils6 import in6_getnsma, in6_getnsmac, in6_ismaddr
Neale Ranns465a1a32017-01-07 10:04:09 -080018from scapy.utils import inet_pton, inet_ntop
Klement Sekeraf62ae122016-10-11 11:47:09 +020019
Klement Sekerada505f62017-01-04 12:58:53 +010020
Klement Sekeraacb9b8e2017-02-14 02:55:31 +010021class CaptureTimeoutError(Exception):
22 """ Exception raised if capture or packet doesn't appear within timeout """
23 pass
24
25
Klement Sekera65cc8c02016-12-18 15:49:54 +010026def is_ipv6_misc(p):
27 """ Is packet one of uninteresting IPv6 broadcasts? """
28 if p.haslayer(ICMPv6ND_RA):
Neale Ranns75152282017-01-09 01:00:45 -080029 if in6_ismaddr(p[IPv6].dst):
30 return True
Klement Sekera65cc8c02016-12-18 15:49:54 +010031 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 Sekeraf62ae122016-10-11 11:47:09 +020038class VppPGInterface(VppInterface):
39 """
40 VPP packet-generator interface
41 """
42
43 @property
44 def pg_index(self):
45 """packet-generator interface index assigned by VPP"""
46 return self._pg_index
47
48 @property
49 def out_path(self):
50 """pcap file path - captured packets"""
51 return self._out_path
52
53 @property
54 def in_path(self):
55 """ pcap file path - injected packets"""
56 return self._in_path
57
58 @property
59 def capture_cli(self):
60 """CLI string to start capture on this interface"""
61 return self._capture_cli
62
63 @property
64 def cap_name(self):
65 """capture name for this interface"""
66 return self._cap_name
67
68 @property
69 def input_cli(self):
70 """CLI string to load the injected packets"""
Alexandre Poirriera618e202019-05-07 10:43:41 +020071 if self._nb_replays is not None:
72 return "%s limit %d" % (self._input_cli, self._nb_replays)
Klement Sekeraf62ae122016-10-11 11:47:09 +020073 return self._input_cli
74
Klement Sekera778c2762016-11-08 02:00:28 +010075 @property
76 def in_history_counter(self):
77 """Self-incrementing counter used when renaming old pcap files"""
78 v = self._in_history_counter
79 self._in_history_counter += 1
80 return v
81
82 @property
83 def out_history_counter(self):
84 """Self-incrementing counter used when renaming old pcap files"""
85 v = self._out_history_counter
86 self._out_history_counter += 1
87 return v
88
Matej Klottonc5bf07f2016-11-23 15:27:17 +010089 def __init__(self, test, pg_index):
90 """ Create VPP packet-generator interface """
Ole Troane0d2bd62018-06-22 22:36:46 +020091 super(VppPGInterface, self).__init__(test)
Klement Sekeraa98346f2018-05-16 10:52:45 +020092
Klement Sekera31da2e32018-06-24 22:49:55 +020093 r = test.vapi.pg_create_interface(pg_index)
94 self.set_sw_if_index(r.sw_if_index)
95
Matej Klottonc5bf07f2016-11-23 15:27:17 +010096 self._in_history_counter = 0
97 self._out_history_counter = 0
Klement Sekera97f6edc2017-01-12 07:17:01 +010098 self._out_assert_counter = 0
Matej Klottonc5bf07f2016-11-23 15:27:17 +010099 self._pg_index = pg_index
Klement Sekera74dcdbf2016-11-14 09:49:09 +0100100 self._out_file = "pg%u_out.pcap" % self.pg_index
Klement Sekera778c2762016-11-08 02:00:28 +0100101 self._out_path = self.test.tempdir + "/" + self._out_file
Klement Sekera74dcdbf2016-11-14 09:49:09 +0100102 self._in_file = "pg%u_in.pcap" % self.pg_index
Klement Sekera778c2762016-11-08 02:00:28 +0100103 self._in_path = self.test.tempdir + "/" + self._in_file
Klement Sekeraf62ae122016-10-11 11:47:09 +0200104 self._capture_cli = "packet-generator capture pg%u pcap %s" % (
105 self.pg_index, self.out_path)
106 self._cap_name = "pcap%u" % self.sw_if_index
Klement Sekerada505f62017-01-04 12:58:53 +0100107 self._input_cli = \
108 "packet-generator new pcap %s source pg%u name %s" % (
109 self.in_path, self.pg_index, self.cap_name)
Alexandre Poirriera618e202019-05-07 10:43:41 +0200110 self._nb_replays = None
Klement Sekeraf62ae122016-10-11 11:47:09 +0200111
Paul Vinciguerra4b58a862019-05-28 15:40:47 -0400112 def _rename_previous_capture_file(self, path, counter, file):
113 # if a file from a previous capture exists, rename it.
114 try:
115 if os.path.isfile(path):
116 name = "%s/history.[timestamp:%f].[%s-counter:%04d].%s" % \
117 (self.test.tempdir,
118 time.time(),
119 self.name,
120 counter,
121 file)
122 self.test.logger.debug("Renaming %s->%s" %
123 (path, name))
124 os.rename(path, name)
125 except OSError:
126 self.test.logger.debug("OSError: Could not rename %s %s" %
127 (path, file))
128
Klement Sekerada505f62017-01-04 12:58:53 +0100129 def enable_capture(self):
Alexandre Poirriera618e202019-05-07 10:43:41 +0200130 """ Enable capture on this packet-generator interface
131 of at most n packets.
132 If n < 0, this is no limit
133 """
Paul Vinciguerra4b58a862019-05-28 15:40:47 -0400134
135 self._rename_previous_capture_file(self.out_path,
136 self.out_history_counter,
137 self._out_file)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200138 # FIXME this should be an API, but no such exists atm
139 self.test.vapi.cli(self.capture_cli)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200140 self._pcap_reader = None
Klement Sekeraf62ae122016-10-11 11:47:09 +0200141
Alexandre Poirriera618e202019-05-07 10:43:41 +0200142 def disable_capture(self):
143 self.test.vapi.cli("%s disable" % self.capture_cli)
144
145 def add_stream(self, pkts, nb_replays=None):
Klement Sekeraf62ae122016-10-11 11:47:09 +0200146 """
147 Add a stream of packets to this packet-generator
148
149 :param pkts: iterable packets
150
151 """
Alexandre Poirriera618e202019-05-07 10:43:41 +0200152 self._nb_replays = nb_replays
Paul Vinciguerra4b58a862019-05-28 15:40:47 -0400153 self._rename_previous_capture_file(self.in_path,
154 self.in_history_counter,
155 self._in_file)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200156 wrpcap(self.in_path, pkts)
Klement Sekera9225dee2016-12-12 08:36:58 +0100157 self.test.register_capture(self.cap_name)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200158 # FIXME this should be an API, but no such exists atm
159 self.test.vapi.cli(self.input_cli)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200160
Klement Sekera97f6edc2017-01-12 07:17:01 +0100161 def generate_debug_aid(self, kind):
162 """ Create a hardlink to the out file with a counter and a file
163 containing stack trace to ease debugging in case of multiple capture
164 files present. """
165 self.test.logger.debug("Generating debug aid for %s on %s" %
166 (kind, self._name))
167 link_path, stack_path = ["%s/debug_%s_%s_%s.%s" %
168 (self.test.tempdir, self._name,
169 self._out_assert_counter, kind, suffix)
170 for suffix in ["pcap", "stack"]
171 ]
172 os.link(self.out_path, link_path)
173 with open(stack_path, "w") as f:
174 f.writelines(format_stack())
175 self._out_assert_counter += 1
176
Klement Sekeradab231a2016-12-21 08:50:14 +0100177 def _get_capture(self, timeout, filter_out_fn=is_ipv6_misc):
178 """ Helper method to get capture and filter it """
Klement Sekeraf62ae122016-10-11 11:47:09 +0200179 try:
Klement Sekeradab231a2016-12-21 08:50:14 +0100180 if not self.wait_for_capture_file(timeout):
181 return None
Klement Sekeraf62ae122016-10-11 11:47:09 +0200182 output = rdpcap(self.out_path)
Klement Sekera65cc8c02016-12-18 15:49:54 +0100183 self.test.logger.debug("Capture has %s packets" % len(output.res))
Klement Sekeradab231a2016-12-21 08:50:14 +0100184 except:
Jane546d3b2016-12-08 13:10:03 +0100185 self.test.logger.debug("Exception in scapy.rdpcap (%s): %s" %
Klement Sekeradab231a2016-12-21 08:50:14 +0100186 (self.out_path, format_exc()))
187 return None
Klement Sekera65cc8c02016-12-18 15:49:54 +0100188 before = len(output.res)
Klement Sekeradab231a2016-12-21 08:50:14 +0100189 if filter_out_fn:
190 output.res = [p for p in output.res if not filter_out_fn(p)]
Klement Sekera97f6edc2017-01-12 07:17:01 +0100191 removed = before - len(output.res)
Klement Sekera65cc8c02016-12-18 15:49:54 +0100192 if removed:
193 self.test.logger.debug(
194 "Filtered out %s packets from capture (returning %s)" %
195 (removed, len(output.res)))
Klement Sekeraf62ae122016-10-11 11:47:09 +0200196 return output
Matej Klotton0178d522016-11-04 11:11:44 +0100197
Klement Sekeradab231a2016-12-21 08:50:14 +0100198 def get_capture(self, expected_count=None, remark=None, timeout=1,
199 filter_out_fn=is_ipv6_misc):
200 """ Get captured packets
201
202 :param expected_count: expected number of packets to capture, if None,
203 then self.test.packet_count_for_dst_pg_idx is
204 used to lookup the expected count
205 :param remark: remark printed into debug logs
206 :param timeout: how long to wait for packets
207 :param filter_out_fn: filter applied to each packet, packets for which
208 the filter returns True are removed from capture
209 :returns: iterable packets
210 """
211 remaining_time = timeout
212 capture = None
213 name = self.name if remark is None else "%s (%s)" % (self.name, remark)
214 based_on = "based on provided argument"
215 if expected_count is None:
216 expected_count = \
217 self.test.get_packet_count_for_if_idx(self.sw_if_index)
218 based_on = "based on stored packet_infos"
Klement Sekerac86fa022017-01-02 09:03:47 +0100219 if expected_count == 0:
220 raise Exception(
Klement Sekerada505f62017-01-04 12:58:53 +0100221 "Internal error, expected packet count for %s is 0!" %
222 name)
Jane546d3b2016-12-08 13:10:03 +0100223 self.test.logger.debug("Expecting to capture %s (%s) packets on %s" % (
Klement Sekeradab231a2016-12-21 08:50:14 +0100224 expected_count, based_on, name))
Klement Sekeradab231a2016-12-21 08:50:14 +0100225 while remaining_time > 0:
226 before = time.time()
227 capture = self._get_capture(remaining_time, filter_out_fn)
228 elapsed_time = time.time() - before
229 if capture:
230 if len(capture.res) == expected_count:
231 # bingo, got the packets we expected
232 return capture
Jan Gelety057bb8c2016-12-20 17:32:45 +0100233 elif len(capture.res) > expected_count:
234 self.test.logger.error(
235 ppc("Unexpected packets captured:", capture))
236 break
Klement Sekera97f6edc2017-01-12 07:17:01 +0100237 else:
238 self.test.logger.debug("Partial capture containing %s "
239 "packets doesn't match expected "
240 "count %s (yet?)" %
241 (len(capture.res), expected_count))
242 elif expected_count == 0:
243 # bingo, got None as we expected - return empty capture
244 return PacketList()
Klement Sekeradab231a2016-12-21 08:50:14 +0100245 remaining_time -= elapsed_time
246 if capture:
Klement Sekera97f6edc2017-01-12 07:17:01 +0100247 self.generate_debug_aid("count-mismatch")
Klement Sekeradab231a2016-12-21 08:50:14 +0100248 raise Exception("Captured packets mismatch, captured %s packets, "
249 "expected %s packets on %s" %
250 (len(capture.res), expected_count, name))
251 else:
252 raise Exception("No packets captured on %s" % name)
253
254 def assert_nothing_captured(self, remark=None, filter_out_fn=is_ipv6_misc):
255 """ Assert that nothing unfiltered was captured on interface
256
257 :param remark: remark printed into debug logs
258 :param filter_out_fn: filter applied to each packet, packets for which
259 the filter returns True are removed from capture
260 """
Klement Sekera9225dee2016-12-12 08:36:58 +0100261 if os.path.isfile(self.out_path):
262 try:
Klement Sekeradab231a2016-12-21 08:50:14 +0100263 capture = self.get_capture(
264 0, remark=remark, filter_out_fn=filter_out_fn)
Klement Sekera97f6edc2017-01-12 07:17:01 +0100265 if not capture or len(capture.res) == 0:
Jane546d3b2016-12-08 13:10:03 +0100266 # junk filtered out, we're good
267 return
Klement Sekera9225dee2016-12-12 08:36:58 +0100268 except:
269 pass
Klement Sekera97f6edc2017-01-12 07:17:01 +0100270 self.generate_debug_aid("empty-assert")
Klement Sekera9225dee2016-12-12 08:36:58 +0100271 if remark:
272 raise AssertionError(
Jane546d3b2016-12-08 13:10:03 +0100273 "Non-empty capture file present for interface %s (%s)" %
Klement Sekera9225dee2016-12-12 08:36:58 +0100274 (self.name, remark))
275 else:
Jane546d3b2016-12-08 13:10:03 +0100276 raise AssertionError("Capture file present for interface %s" %
277 self.name)
Klement Sekera9225dee2016-12-12 08:36:58 +0100278
279 def wait_for_capture_file(self, timeout=1):
280 """
281 Wait until pcap capture file appears
282
283 :param timeout: How long to wait for the packet (default 1s)
284
Klement Sekeradab231a2016-12-21 08:50:14 +0100285 :returns: True/False if the file is present or appears within timeout
Klement Sekera9225dee2016-12-12 08:36:58 +0100286 """
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100287 deadline = time.time() + timeout
Klement Sekera9225dee2016-12-12 08:36:58 +0100288 if not os.path.isfile(self.out_path):
Klement Sekeradab231a2016-12-21 08:50:14 +0100289 self.test.logger.debug("Waiting for capture file %s to appear, "
290 "timeout is %ss" % (self.out_path, timeout))
Klement Sekera9225dee2016-12-12 08:36:58 +0100291 else:
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100292 self.test.logger.debug("Capture file %s already exists" %
293 self.out_path)
Klement Sekeradab231a2016-12-21 08:50:14 +0100294 return True
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100295 while time.time() < deadline:
Klement Sekera9225dee2016-12-12 08:36:58 +0100296 if os.path.isfile(self.out_path):
297 break
Paul Vinciguerra0f6602c2019-03-10 09:10:54 -0700298 self._test.sleep(0) # yield
Klement Sekera9225dee2016-12-12 08:36:58 +0100299 if os.path.isfile(self.out_path):
300 self.test.logger.debug("Capture file appeared after %fs" %
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100301 (time.time() - (deadline - timeout)))
Klement Sekera9225dee2016-12-12 08:36:58 +0100302 else:
303 self.test.logger.debug("Timeout - capture file still nowhere")
Klement Sekeradab231a2016-12-21 08:50:14 +0100304 return False
305 return True
Klement Sekera9225dee2016-12-12 08:36:58 +0100306
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100307 def verify_enough_packet_data_in_pcap(self):
Klement Sekerab91017a2017-02-09 06:04:36 +0100308 """
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100309 Check if enough data is available in file handled by internal pcap
310 reader so that a whole packet can be read.
Klement Sekerab91017a2017-02-09 06:04:36 +0100311
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100312 :returns: True if enough data present, else False
Klement Sekerab91017a2017-02-09 06:04:36 +0100313 """
314 orig_pos = self._pcap_reader.f.tell() # save file position
315 enough_data = False
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100316 # read packet header from pcap
317 packet_header_size = 16
318 caplen = None
319 end_pos = None
320 hdr = self._pcap_reader.f.read(packet_header_size)
321 if len(hdr) == packet_header_size:
322 # parse the capture length - caplen
Klement Sekerab91017a2017-02-09 06:04:36 +0100323 sec, usec, caplen, wirelen = struct.unpack(
324 self._pcap_reader.endian + "IIII", hdr)
325 self._pcap_reader.f.seek(0, 2) # seek to end of file
326 end_pos = self._pcap_reader.f.tell() # get position at end
327 if end_pos >= orig_pos + len(hdr) + caplen:
328 enough_data = True # yay, we have enough data
Klement Sekerab91017a2017-02-09 06:04:36 +0100329 self._pcap_reader.f.seek(orig_pos, 0) # restore original position
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100330 return enough_data
Klement Sekerab91017a2017-02-09 06:04:36 +0100331
Klement Sekeradab231a2016-12-21 08:50:14 +0100332 def wait_for_packet(self, timeout, filter_out_fn=is_ipv6_misc):
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200333 """
334 Wait for next packet captured with a timeout
335
336 :param timeout: How long to wait for the packet
337
338 :returns: Captured packet if no packet arrived within timeout
339 :raises Exception: if no packet arrives within timeout
340 """
Klement Sekeradab231a2016-12-21 08:50:14 +0100341 deadline = time.time() + timeout
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200342 if self._pcap_reader is None:
Klement Sekeradab231a2016-12-21 08:50:14 +0100343 if not self.wait_for_capture_file(timeout):
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100344 raise CaptureTimeoutError("Capture file %s did not appear "
345 "within timeout" % self.out_path)
Klement Sekeradab231a2016-12-21 08:50:14 +0100346 while time.time() < deadline:
347 try:
348 self._pcap_reader = PcapReader(self.out_path)
349 break
350 except:
Klement Sekerada505f62017-01-04 12:58:53 +0100351 self.test.logger.debug(
Klement Sekera97f6edc2017-01-12 07:17:01 +0100352 "Exception in scapy.PcapReader(%s): %s" %
Klement Sekerada505f62017-01-04 12:58:53 +0100353 (self.out_path, format_exc()))
Klement Sekeradab231a2016-12-21 08:50:14 +0100354 if not self._pcap_reader:
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100355 raise CaptureTimeoutError("Capture file %s did not appear within "
356 "timeout" % self.out_path)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200357
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100358 poll = False
359 if timeout > 0:
360 self.test.logger.debug("Waiting for packet")
361 else:
362 poll = True
363 self.test.logger.debug("Polling for packet")
364 while time.time() < deadline or poll:
365 if not self.verify_enough_packet_data_in_pcap():
Paul Vinciguerra0f6602c2019-03-10 09:10:54 -0700366 self._test.sleep(0) # yield
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100367 poll = False
368 continue
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200369 p = self._pcap_reader.recv()
370 if p is not None:
Klement Sekeradab231a2016-12-21 08:50:14 +0100371 if filter_out_fn is not None and filter_out_fn(p):
372 self.test.logger.debug(
373 "Packet received after %ss was filtered out" %
374 (time.time() - (deadline - timeout)))
375 else:
Klement Sekerada505f62017-01-04 12:58:53 +0100376 self.test.logger.debug(
377 "Packet received after %fs" %
378 (time.time() - (deadline - timeout)))
Klement Sekeradab231a2016-12-21 08:50:14 +0100379 return p
Paul Vinciguerra0f6602c2019-03-10 09:10:54 -0700380 self._test.sleep(0) # yield
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100381 poll = False
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200382 self.test.logger.debug("Timeout - no packets received")
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100383 raise CaptureTimeoutError("Packet didn't arrive within timeout")
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200384
Matej Klotton0178d522016-11-04 11:11:44 +0100385 def create_arp_req(self):
386 """Create ARP request applicable for this interface"""
387 return (Ether(dst="ff:ff:ff:ff:ff:ff", src=self.remote_mac) /
388 ARP(op=ARP.who_has, pdst=self.local_ip4,
389 psrc=self.remote_ip4, hwsrc=self.remote_mac))
390
391 def create_ndp_req(self):
392 """Create NDP - NS applicable for this interface"""
Neale Ranns465a1a32017-01-07 10:04:09 -0800393 nsma = in6_getnsma(inet_pton(socket.AF_INET6, self.local_ip6))
394 d = inet_ntop(socket.AF_INET6, nsma)
395
396 return (Ether(dst=in6_getnsmac(nsma)) /
397 IPv6(dst=d, src=self.remote_ip6) /
Klement Sekera74dcdbf2016-11-14 09:49:09 +0100398 ICMPv6ND_NS(tgt=self.local_ip6) /
399 ICMPv6NDOptSrcLLAddr(lladdr=self.remote_mac))
Matej Klotton0178d522016-11-04 11:11:44 +0100400
401 def resolve_arp(self, pg_interface=None):
402 """Resolve ARP using provided packet-generator interface
403
404 :param pg_interface: interface used to resolve, if None then this
405 interface is used
406
407 """
408 if pg_interface is None:
409 pg_interface = self
Klement Sekera7bb873a2016-11-18 07:38:42 +0100410 self.test.logger.info("Sending ARP request for %s on port %s" %
411 (self.local_ip4, pg_interface.name))
Matej Klotton0178d522016-11-04 11:11:44 +0100412 arp_req = self.create_arp_req()
413 pg_interface.add_stream(arp_req)
414 pg_interface.enable_capture()
415 self.test.pg_start()
Klement Sekera7bb873a2016-11-18 07:38:42 +0100416 self.test.logger.info(self.test.vapi.cli("show trace"))
Klement Sekera9225dee2016-12-12 08:36:58 +0100417 try:
Klement Sekeradab231a2016-12-21 08:50:14 +0100418 captured_packet = pg_interface.wait_for_packet(1)
Klement Sekera9225dee2016-12-12 08:36:58 +0100419 except:
420 self.test.logger.info("No ARP received on port %s" %
421 pg_interface.name)
Matej Klotton0178d522016-11-04 11:11:44 +0100422 return
Klement Sekeradab231a2016-12-21 08:50:14 +0100423 arp_reply = captured_packet.copy() # keep original for exception
Matej Klotton0178d522016-11-04 11:11:44 +0100424 try:
425 if arp_reply[ARP].op == ARP.is_at:
Klement Sekera7bb873a2016-11-18 07:38:42 +0100426 self.test.logger.info("VPP %s MAC address is %s " %
427 (self.name, arp_reply[ARP].hwsrc))
Matej Klotton0178d522016-11-04 11:11:44 +0100428 self._local_mac = arp_reply[ARP].hwsrc
429 else:
Klement Sekeradab231a2016-12-21 08:50:14 +0100430 self.test.logger.info("No ARP received on port %s" %
431 pg_interface.name)
Matej Klotton0178d522016-11-04 11:11:44 +0100432 except:
Klement Sekera7bb873a2016-11-18 07:38:42 +0100433 self.test.logger.error(
Klement Sekeradab231a2016-12-21 08:50:14 +0100434 ppp("Unexpected response to ARP request:", captured_packet))
Matej Klotton0178d522016-11-04 11:11:44 +0100435 raise
436
Klement Sekeradab231a2016-12-21 08:50:14 +0100437 def resolve_ndp(self, pg_interface=None, timeout=1):
Matej Klotton0178d522016-11-04 11:11:44 +0100438 """Resolve NDP using provided packet-generator interface
439
440 :param pg_interface: interface used to resolve, if None then this
441 interface is used
Klement Sekeradab231a2016-12-21 08:50:14 +0100442 :param timeout: how long to wait for response before giving up
Matej Klotton0178d522016-11-04 11:11:44 +0100443
444 """
445 if pg_interface is None:
446 pg_interface = self
Klement Sekera7bb873a2016-11-18 07:38:42 +0100447 self.test.logger.info("Sending NDP request for %s on port %s" %
448 (self.local_ip6, pg_interface.name))
Matej Klotton0178d522016-11-04 11:11:44 +0100449 ndp_req = self.create_ndp_req()
450 pg_interface.add_stream(ndp_req)
451 pg_interface.enable_capture()
452 self.test.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +0100453 now = time.time()
454 deadline = now + timeout
Neale Ranns82a06a92016-12-08 20:05:33 +0000455 # Enabling IPv6 on an interface can generate more than the
456 # ND reply we are looking for (namely MLD). So loop through
457 # the replies to look for want we want.
Klement Sekeradab231a2016-12-21 08:50:14 +0100458 while now < deadline:
459 try:
460 captured_packet = pg_interface.wait_for_packet(
461 deadline - now, filter_out_fn=None)
462 except:
Klement Sekerada505f62017-01-04 12:58:53 +0100463 self.test.logger.error(
464 "Timeout while waiting for NDP response")
Klement Sekeradab231a2016-12-21 08:50:14 +0100465 raise
466 ndp_reply = captured_packet.copy() # keep original for exception
Neale Ranns82a06a92016-12-08 20:05:33 +0000467 try:
468 ndp_na = ndp_reply[ICMPv6ND_NA]
469 opt = ndp_na[ICMPv6NDOptDstLLAddr]
470 self.test.logger.info("VPP %s MAC address is %s " %
471 (self.name, opt.lladdr))
472 self._local_mac = opt.lladdr
Klement Sekeradab231a2016-12-21 08:50:14 +0100473 self.test.logger.debug(self.test.vapi.cli("show trace"))
474 # we now have the MAC we've been after
475 return
Neale Ranns82a06a92016-12-08 20:05:33 +0000476 except:
477 self.test.logger.info(
Klement Sekerada505f62017-01-04 12:58:53 +0100478 ppp("Unexpected response to NDP request:",
479 captured_packet))
Klement Sekeradab231a2016-12-21 08:50:14 +0100480 now = time.time()
481
482 self.test.logger.debug(self.test.vapi.cli("show trace"))
483 raise Exception("Timeout while waiting for NDP response")