blob: e5926fac509a5529963058d0c33e76f1d3ff8cbb [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
Mohsin Kazmi22e9cfd2019-07-23 11:54:48 +020049 def gso_enabled(self):
50 """gso enabled on packet-generator interface"""
51 if self._gso_enabled == 0:
52 return "gso-disabled"
53 return "gso-enabled"
54
55 @property
56 def gso_size(self):
57 """gso size on packet-generator interface"""
58 return self._gso_size
59
60 @property
Klement Sekeraf62ae122016-10-11 11:47:09 +020061 def out_path(self):
62 """pcap file path - captured packets"""
63 return self._out_path
64
65 @property
66 def in_path(self):
67 """ pcap file path - injected packets"""
68 return self._in_path
69
70 @property
71 def capture_cli(self):
72 """CLI string to start capture on this interface"""
73 return self._capture_cli
74
75 @property
76 def cap_name(self):
77 """capture name for this interface"""
78 return self._cap_name
79
80 @property
81 def input_cli(self):
82 """CLI string to load the injected packets"""
Alexandre Poirriera618e202019-05-07 10:43:41 +020083 if self._nb_replays is not None:
84 return "%s limit %d" % (self._input_cli, self._nb_replays)
Klement Sekera4ecbf102019-07-31 13:14:16 +000085 if self._worker is not None:
86 return "%s worker %d" % (self._input_cli, self._worker)
Klement Sekeraf62ae122016-10-11 11:47:09 +020087 return self._input_cli
88
Klement Sekera778c2762016-11-08 02:00:28 +010089 @property
90 def in_history_counter(self):
91 """Self-incrementing counter used when renaming old pcap files"""
92 v = self._in_history_counter
93 self._in_history_counter += 1
94 return v
95
96 @property
97 def out_history_counter(self):
98 """Self-incrementing counter used when renaming old pcap files"""
99 v = self._out_history_counter
100 self._out_history_counter += 1
101 return v
102
Mohsin Kazmi22e9cfd2019-07-23 11:54:48 +0200103 def __init__(self, test, pg_index, gso, gso_size):
Matej Klottonc5bf07f2016-11-23 15:27:17 +0100104 """ Create VPP packet-generator interface """
Ole Troane0d2bd62018-06-22 22:36:46 +0200105 super(VppPGInterface, self).__init__(test)
Klement Sekeraa98346f2018-05-16 10:52:45 +0200106
Mohsin Kazmi22e9cfd2019-07-23 11:54:48 +0200107 r = test.vapi.pg_create_interface(pg_index, gso, gso_size)
Klement Sekera31da2e32018-06-24 22:49:55 +0200108 self.set_sw_if_index(r.sw_if_index)
109
Matej Klottonc5bf07f2016-11-23 15:27:17 +0100110 self._in_history_counter = 0
111 self._out_history_counter = 0
Klement Sekera97f6edc2017-01-12 07:17:01 +0100112 self._out_assert_counter = 0
Matej Klottonc5bf07f2016-11-23 15:27:17 +0100113 self._pg_index = pg_index
Mohsin Kazmi22e9cfd2019-07-23 11:54:48 +0200114 self._gso_enabled = gso
115 self._gso_size = gso_size
Klement Sekera74dcdbf2016-11-14 09:49:09 +0100116 self._out_file = "pg%u_out.pcap" % self.pg_index
Klement Sekera778c2762016-11-08 02:00:28 +0100117 self._out_path = self.test.tempdir + "/" + self._out_file
Klement Sekera74dcdbf2016-11-14 09:49:09 +0100118 self._in_file = "pg%u_in.pcap" % self.pg_index
Klement Sekera778c2762016-11-08 02:00:28 +0100119 self._in_path = self.test.tempdir + "/" + self._in_file
Klement Sekeraf62ae122016-10-11 11:47:09 +0200120 self._capture_cli = "packet-generator capture pg%u pcap %s" % (
121 self.pg_index, self.out_path)
Paul Vinciguerra44b0b072019-06-25 20:51:31 -0400122 self._cap_name = "pcap%u-sw_if_index-%s" % (
123 self.pg_index, self.sw_if_index)
Klement Sekerada505f62017-01-04 12:58:53 +0100124 self._input_cli = \
125 "packet-generator new pcap %s source pg%u name %s" % (
126 self.in_path, self.pg_index, self.cap_name)
Alexandre Poirriera618e202019-05-07 10:43:41 +0200127 self._nb_replays = None
Klement Sekeraf62ae122016-10-11 11:47:09 +0200128
Paul Vinciguerra4b58a862019-05-28 15:40:47 -0400129 def _rename_previous_capture_file(self, path, counter, file):
130 # if a file from a previous capture exists, rename it.
131 try:
132 if os.path.isfile(path):
133 name = "%s/history.[timestamp:%f].[%s-counter:%04d].%s" % \
134 (self.test.tempdir,
135 time.time(),
136 self.name,
137 counter,
138 file)
139 self.test.logger.debug("Renaming %s->%s" %
140 (path, name))
141 os.rename(path, name)
142 except OSError:
143 self.test.logger.debug("OSError: Could not rename %s %s" %
144 (path, file))
145
Klement Sekerada505f62017-01-04 12:58:53 +0100146 def enable_capture(self):
Alexandre Poirriera618e202019-05-07 10:43:41 +0200147 """ Enable capture on this packet-generator interface
148 of at most n packets.
149 If n < 0, this is no limit
150 """
Andrew Yourtchenkocb265c62019-07-25 10:03:51 +0000151 # disable the capture to flush the capture
152 self.disable_capture()
Paul Vinciguerra4b58a862019-05-28 15:40:47 -0400153 self._rename_previous_capture_file(self.out_path,
154 self.out_history_counter,
155 self._out_file)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200156 # FIXME this should be an API, but no such exists atm
157 self.test.vapi.cli(self.capture_cli)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200158 self._pcap_reader = None
Klement Sekeraf62ae122016-10-11 11:47:09 +0200159
Alexandre Poirriera618e202019-05-07 10:43:41 +0200160 def disable_capture(self):
161 self.test.vapi.cli("%s disable" % self.capture_cli)
162
Klement Sekera4ecbf102019-07-31 13:14:16 +0000163 def add_stream(self, pkts, nb_replays=None, worker=None):
Klement Sekeraf62ae122016-10-11 11:47:09 +0200164 """
165 Add a stream of packets to this packet-generator
166
167 :param pkts: iterable packets
168
169 """
Klement Sekera4ecbf102019-07-31 13:14:16 +0000170 self._worker = worker
Alexandre Poirriera618e202019-05-07 10:43:41 +0200171 self._nb_replays = nb_replays
Paul Vinciguerra4b58a862019-05-28 15:40:47 -0400172 self._rename_previous_capture_file(self.in_path,
173 self.in_history_counter,
174 self._in_file)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200175 wrpcap(self.in_path, pkts)
Klement Sekera9225dee2016-12-12 08:36:58 +0100176 self.test.register_capture(self.cap_name)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200177 # FIXME this should be an API, but no such exists atm
178 self.test.vapi.cli(self.input_cli)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200179
Klement Sekera97f6edc2017-01-12 07:17:01 +0100180 def generate_debug_aid(self, kind):
181 """ Create a hardlink to the out file with a counter and a file
182 containing stack trace to ease debugging in case of multiple capture
183 files present. """
184 self.test.logger.debug("Generating debug aid for %s on %s" %
185 (kind, self._name))
186 link_path, stack_path = ["%s/debug_%s_%s_%s.%s" %
187 (self.test.tempdir, self._name,
188 self._out_assert_counter, kind, suffix)
189 for suffix in ["pcap", "stack"]
190 ]
191 os.link(self.out_path, link_path)
192 with open(stack_path, "w") as f:
193 f.writelines(format_stack())
194 self._out_assert_counter += 1
195
Klement Sekeradab231a2016-12-21 08:50:14 +0100196 def _get_capture(self, timeout, filter_out_fn=is_ipv6_misc):
197 """ Helper method to get capture and filter it """
Klement Sekeraf62ae122016-10-11 11:47:09 +0200198 try:
Klement Sekeradab231a2016-12-21 08:50:14 +0100199 if not self.wait_for_capture_file(timeout):
200 return None
Klement Sekeraf62ae122016-10-11 11:47:09 +0200201 output = rdpcap(self.out_path)
Klement Sekera65cc8c02016-12-18 15:49:54 +0100202 self.test.logger.debug("Capture has %s packets" % len(output.res))
Klement Sekeradab231a2016-12-21 08:50:14 +0100203 except:
Jane546d3b2016-12-08 13:10:03 +0100204 self.test.logger.debug("Exception in scapy.rdpcap (%s): %s" %
Klement Sekeradab231a2016-12-21 08:50:14 +0100205 (self.out_path, format_exc()))
206 return None
Klement Sekera65cc8c02016-12-18 15:49:54 +0100207 before = len(output.res)
Klement Sekeradab231a2016-12-21 08:50:14 +0100208 if filter_out_fn:
209 output.res = [p for p in output.res if not filter_out_fn(p)]
Klement Sekera97f6edc2017-01-12 07:17:01 +0100210 removed = before - len(output.res)
Klement Sekera65cc8c02016-12-18 15:49:54 +0100211 if removed:
212 self.test.logger.debug(
213 "Filtered out %s packets from capture (returning %s)" %
214 (removed, len(output.res)))
Klement Sekeraf62ae122016-10-11 11:47:09 +0200215 return output
Matej Klotton0178d522016-11-04 11:11:44 +0100216
Klement Sekeradab231a2016-12-21 08:50:14 +0100217 def get_capture(self, expected_count=None, remark=None, timeout=1,
218 filter_out_fn=is_ipv6_misc):
219 """ Get captured packets
220
221 :param expected_count: expected number of packets to capture, if None,
222 then self.test.packet_count_for_dst_pg_idx is
223 used to lookup the expected count
224 :param remark: remark printed into debug logs
225 :param timeout: how long to wait for packets
226 :param filter_out_fn: filter applied to each packet, packets for which
227 the filter returns True are removed from capture
228 :returns: iterable packets
229 """
230 remaining_time = timeout
231 capture = None
232 name = self.name if remark is None else "%s (%s)" % (self.name, remark)
233 based_on = "based on provided argument"
234 if expected_count is None:
235 expected_count = \
236 self.test.get_packet_count_for_if_idx(self.sw_if_index)
237 based_on = "based on stored packet_infos"
Klement Sekerac86fa022017-01-02 09:03:47 +0100238 if expected_count == 0:
239 raise Exception(
Klement Sekerada505f62017-01-04 12:58:53 +0100240 "Internal error, expected packet count for %s is 0!" %
241 name)
Jane546d3b2016-12-08 13:10:03 +0100242 self.test.logger.debug("Expecting to capture %s (%s) packets on %s" % (
Klement Sekeradab231a2016-12-21 08:50:14 +0100243 expected_count, based_on, name))
Klement Sekeradab231a2016-12-21 08:50:14 +0100244 while remaining_time > 0:
245 before = time.time()
246 capture = self._get_capture(remaining_time, filter_out_fn)
247 elapsed_time = time.time() - before
248 if capture:
249 if len(capture.res) == expected_count:
250 # bingo, got the packets we expected
251 return capture
Jan Gelety057bb8c2016-12-20 17:32:45 +0100252 elif len(capture.res) > expected_count:
253 self.test.logger.error(
254 ppc("Unexpected packets captured:", capture))
255 break
Klement Sekera97f6edc2017-01-12 07:17:01 +0100256 else:
257 self.test.logger.debug("Partial capture containing %s "
258 "packets doesn't match expected "
259 "count %s (yet?)" %
260 (len(capture.res), expected_count))
261 elif expected_count == 0:
262 # bingo, got None as we expected - return empty capture
263 return PacketList()
Klement Sekeradab231a2016-12-21 08:50:14 +0100264 remaining_time -= elapsed_time
265 if capture:
Klement Sekera97f6edc2017-01-12 07:17:01 +0100266 self.generate_debug_aid("count-mismatch")
Klement Sekeradab231a2016-12-21 08:50:14 +0100267 raise Exception("Captured packets mismatch, captured %s packets, "
268 "expected %s packets on %s" %
269 (len(capture.res), expected_count, name))
270 else:
271 raise Exception("No packets captured on %s" % name)
272
273 def assert_nothing_captured(self, remark=None, filter_out_fn=is_ipv6_misc):
274 """ Assert that nothing unfiltered was captured on interface
275
276 :param remark: remark printed into debug logs
277 :param filter_out_fn: filter applied to each packet, packets for which
278 the filter returns True are removed from capture
279 """
Klement Sekera9225dee2016-12-12 08:36:58 +0100280 if os.path.isfile(self.out_path):
281 try:
Klement Sekeradab231a2016-12-21 08:50:14 +0100282 capture = self.get_capture(
283 0, remark=remark, filter_out_fn=filter_out_fn)
Klement Sekera97f6edc2017-01-12 07:17:01 +0100284 if not capture or len(capture.res) == 0:
Jane546d3b2016-12-08 13:10:03 +0100285 # junk filtered out, we're good
286 return
Klement Sekera9225dee2016-12-12 08:36:58 +0100287 except:
288 pass
Klement Sekera97f6edc2017-01-12 07:17:01 +0100289 self.generate_debug_aid("empty-assert")
Klement Sekera9225dee2016-12-12 08:36:58 +0100290 if remark:
291 raise AssertionError(
Jane546d3b2016-12-08 13:10:03 +0100292 "Non-empty capture file present for interface %s (%s)" %
Klement Sekera9225dee2016-12-12 08:36:58 +0100293 (self.name, remark))
294 else:
Jane546d3b2016-12-08 13:10:03 +0100295 raise AssertionError("Capture file present for interface %s" %
296 self.name)
Klement Sekera9225dee2016-12-12 08:36:58 +0100297
Andrew Yourtchenko3d36f192019-10-11 12:34:12 +0000298 def wait_for_pg_stop(self):
299 # wait till packet-generator is stopped
300 # "show packet-generator" while it is still running gives this:
301 # Name Enabled Count Parameters
302 # pcap0-sw_if_inde Yes 64 limit 64, ...
303 #
304 # also have a 5-minute timeout just in case things go terribly wrong...
305 deadline = time.time() + 300
306 while self.test.vapi.cli('show packet-generator').find("Yes") != -1:
307 self._test.sleep(0.01) # yield
308 if time.time() > deadline:
309 self.test.logger.debug("Timeout waiting for pg to stop")
310 break
311
Klement Sekera9225dee2016-12-12 08:36:58 +0100312 def wait_for_capture_file(self, timeout=1):
313 """
314 Wait until pcap capture file appears
315
316 :param timeout: How long to wait for the packet (default 1s)
317
Klement Sekeradab231a2016-12-21 08:50:14 +0100318 :returns: True/False if the file is present or appears within timeout
Klement Sekera9225dee2016-12-12 08:36:58 +0100319 """
Andrew Yourtchenko3d36f192019-10-11 12:34:12 +0000320 self.wait_for_pg_stop()
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100321 deadline = time.time() + timeout
Klement Sekera9225dee2016-12-12 08:36:58 +0100322 if not os.path.isfile(self.out_path):
Klement Sekeradab231a2016-12-21 08:50:14 +0100323 self.test.logger.debug("Waiting for capture file %s to appear, "
324 "timeout is %ss" % (self.out_path, timeout))
Klement Sekera9225dee2016-12-12 08:36:58 +0100325 else:
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100326 self.test.logger.debug("Capture file %s already exists" %
327 self.out_path)
Klement Sekeradab231a2016-12-21 08:50:14 +0100328 return True
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100329 while time.time() < deadline:
Klement Sekera9225dee2016-12-12 08:36:58 +0100330 if os.path.isfile(self.out_path):
331 break
Paul Vinciguerra0f6602c2019-03-10 09:10:54 -0700332 self._test.sleep(0) # yield
Klement Sekera9225dee2016-12-12 08:36:58 +0100333 if os.path.isfile(self.out_path):
334 self.test.logger.debug("Capture file appeared after %fs" %
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100335 (time.time() - (deadline - timeout)))
Klement Sekera9225dee2016-12-12 08:36:58 +0100336 else:
337 self.test.logger.debug("Timeout - capture file still nowhere")
Klement Sekeradab231a2016-12-21 08:50:14 +0100338 return False
339 return True
Klement Sekera9225dee2016-12-12 08:36:58 +0100340
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100341 def verify_enough_packet_data_in_pcap(self):
Klement Sekerab91017a2017-02-09 06:04:36 +0100342 """
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100343 Check if enough data is available in file handled by internal pcap
344 reader so that a whole packet can be read.
Klement Sekerab91017a2017-02-09 06:04:36 +0100345
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100346 :returns: True if enough data present, else False
Klement Sekerab91017a2017-02-09 06:04:36 +0100347 """
348 orig_pos = self._pcap_reader.f.tell() # save file position
349 enough_data = False
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100350 # read packet header from pcap
351 packet_header_size = 16
352 caplen = None
353 end_pos = None
354 hdr = self._pcap_reader.f.read(packet_header_size)
355 if len(hdr) == packet_header_size:
356 # parse the capture length - caplen
Klement Sekerab91017a2017-02-09 06:04:36 +0100357 sec, usec, caplen, wirelen = struct.unpack(
358 self._pcap_reader.endian + "IIII", hdr)
359 self._pcap_reader.f.seek(0, 2) # seek to end of file
360 end_pos = self._pcap_reader.f.tell() # get position at end
361 if end_pos >= orig_pos + len(hdr) + caplen:
362 enough_data = True # yay, we have enough data
Klement Sekerab91017a2017-02-09 06:04:36 +0100363 self._pcap_reader.f.seek(orig_pos, 0) # restore original position
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100364 return enough_data
Klement Sekerab91017a2017-02-09 06:04:36 +0100365
Klement Sekeradab231a2016-12-21 08:50:14 +0100366 def wait_for_packet(self, timeout, filter_out_fn=is_ipv6_misc):
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200367 """
368 Wait for next packet captured with a timeout
369
370 :param timeout: How long to wait for the packet
371
372 :returns: Captured packet if no packet arrived within timeout
373 :raises Exception: if no packet arrives within timeout
374 """
Klement Sekeradab231a2016-12-21 08:50:14 +0100375 deadline = time.time() + timeout
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200376 if self._pcap_reader is None:
Klement Sekeradab231a2016-12-21 08:50:14 +0100377 if not self.wait_for_capture_file(timeout):
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100378 raise CaptureTimeoutError("Capture file %s did not appear "
379 "within timeout" % self.out_path)
Klement Sekeradab231a2016-12-21 08:50:14 +0100380 while time.time() < deadline:
381 try:
382 self._pcap_reader = PcapReader(self.out_path)
383 break
384 except:
Klement Sekerada505f62017-01-04 12:58:53 +0100385 self.test.logger.debug(
Klement Sekera97f6edc2017-01-12 07:17:01 +0100386 "Exception in scapy.PcapReader(%s): %s" %
Klement Sekerada505f62017-01-04 12:58:53 +0100387 (self.out_path, format_exc()))
Klement Sekeradab231a2016-12-21 08:50:14 +0100388 if not self._pcap_reader:
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100389 raise CaptureTimeoutError("Capture file %s did not appear within "
390 "timeout" % self.out_path)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200391
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100392 poll = False
393 if timeout > 0:
394 self.test.logger.debug("Waiting for packet")
395 else:
396 poll = True
397 self.test.logger.debug("Polling for packet")
398 while time.time() < deadline or poll:
399 if not self.verify_enough_packet_data_in_pcap():
Paul Vinciguerra0f6602c2019-03-10 09:10:54 -0700400 self._test.sleep(0) # yield
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100401 poll = False
402 continue
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200403 p = self._pcap_reader.recv()
404 if p is not None:
Klement Sekeradab231a2016-12-21 08:50:14 +0100405 if filter_out_fn is not None and filter_out_fn(p):
406 self.test.logger.debug(
407 "Packet received after %ss was filtered out" %
408 (time.time() - (deadline - timeout)))
409 else:
Klement Sekerada505f62017-01-04 12:58:53 +0100410 self.test.logger.debug(
411 "Packet received after %fs" %
412 (time.time() - (deadline - timeout)))
Klement Sekeradab231a2016-12-21 08:50:14 +0100413 return p
Paul Vinciguerra0f6602c2019-03-10 09:10:54 -0700414 self._test.sleep(0) # yield
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100415 poll = False
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200416 self.test.logger.debug("Timeout - no packets received")
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100417 raise CaptureTimeoutError("Packet didn't arrive within timeout")
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200418
Matej Klotton0178d522016-11-04 11:11:44 +0100419 def create_arp_req(self):
420 """Create ARP request applicable for this interface"""
421 return (Ether(dst="ff:ff:ff:ff:ff:ff", src=self.remote_mac) /
422 ARP(op=ARP.who_has, pdst=self.local_ip4,
423 psrc=self.remote_ip4, hwsrc=self.remote_mac))
424
425 def create_ndp_req(self):
426 """Create NDP - NS applicable for this interface"""
Neale Ranns465a1a32017-01-07 10:04:09 -0800427 nsma = in6_getnsma(inet_pton(socket.AF_INET6, self.local_ip6))
428 d = inet_ntop(socket.AF_INET6, nsma)
429
430 return (Ether(dst=in6_getnsmac(nsma)) /
431 IPv6(dst=d, src=self.remote_ip6) /
Klement Sekera74dcdbf2016-11-14 09:49:09 +0100432 ICMPv6ND_NS(tgt=self.local_ip6) /
433 ICMPv6NDOptSrcLLAddr(lladdr=self.remote_mac))
Matej Klotton0178d522016-11-04 11:11:44 +0100434
435 def resolve_arp(self, pg_interface=None):
436 """Resolve ARP using provided packet-generator interface
437
438 :param pg_interface: interface used to resolve, if None then this
439 interface is used
440
441 """
442 if pg_interface is None:
443 pg_interface = self
Klement Sekera7bb873a2016-11-18 07:38:42 +0100444 self.test.logger.info("Sending ARP request for %s on port %s" %
445 (self.local_ip4, pg_interface.name))
Matej Klotton0178d522016-11-04 11:11:44 +0100446 arp_req = self.create_arp_req()
447 pg_interface.add_stream(arp_req)
448 pg_interface.enable_capture()
449 self.test.pg_start()
Klement Sekera7bb873a2016-11-18 07:38:42 +0100450 self.test.logger.info(self.test.vapi.cli("show trace"))
Klement Sekera9225dee2016-12-12 08:36:58 +0100451 try:
Klement Sekeradab231a2016-12-21 08:50:14 +0100452 captured_packet = pg_interface.wait_for_packet(1)
Klement Sekera9225dee2016-12-12 08:36:58 +0100453 except:
454 self.test.logger.info("No ARP received on port %s" %
455 pg_interface.name)
Matej Klotton0178d522016-11-04 11:11:44 +0100456 return
Klement Sekeradab231a2016-12-21 08:50:14 +0100457 arp_reply = captured_packet.copy() # keep original for exception
Matej Klotton0178d522016-11-04 11:11:44 +0100458 try:
459 if arp_reply[ARP].op == ARP.is_at:
Klement Sekera7bb873a2016-11-18 07:38:42 +0100460 self.test.logger.info("VPP %s MAC address is %s " %
461 (self.name, arp_reply[ARP].hwsrc))
Matej Klotton0178d522016-11-04 11:11:44 +0100462 self._local_mac = arp_reply[ARP].hwsrc
463 else:
Klement Sekeradab231a2016-12-21 08:50:14 +0100464 self.test.logger.info("No ARP received on port %s" %
465 pg_interface.name)
Matej Klotton0178d522016-11-04 11:11:44 +0100466 except:
Klement Sekera7bb873a2016-11-18 07:38:42 +0100467 self.test.logger.error(
Klement Sekeradab231a2016-12-21 08:50:14 +0100468 ppp("Unexpected response to ARP request:", captured_packet))
Matej Klotton0178d522016-11-04 11:11:44 +0100469 raise
470
Klement Sekeradab231a2016-12-21 08:50:14 +0100471 def resolve_ndp(self, pg_interface=None, timeout=1):
Matej Klotton0178d522016-11-04 11:11:44 +0100472 """Resolve NDP using provided packet-generator interface
473
474 :param pg_interface: interface used to resolve, if None then this
475 interface is used
Klement Sekeradab231a2016-12-21 08:50:14 +0100476 :param timeout: how long to wait for response before giving up
Matej Klotton0178d522016-11-04 11:11:44 +0100477
478 """
479 if pg_interface is None:
480 pg_interface = self
Klement Sekera7bb873a2016-11-18 07:38:42 +0100481 self.test.logger.info("Sending NDP request for %s on port %s" %
482 (self.local_ip6, pg_interface.name))
Matej Klotton0178d522016-11-04 11:11:44 +0100483 ndp_req = self.create_ndp_req()
484 pg_interface.add_stream(ndp_req)
485 pg_interface.enable_capture()
486 self.test.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +0100487 now = time.time()
488 deadline = now + timeout
Neale Ranns82a06a92016-12-08 20:05:33 +0000489 # Enabling IPv6 on an interface can generate more than the
490 # ND reply we are looking for (namely MLD). So loop through
491 # the replies to look for want we want.
Klement Sekeradab231a2016-12-21 08:50:14 +0100492 while now < deadline:
493 try:
494 captured_packet = pg_interface.wait_for_packet(
495 deadline - now, filter_out_fn=None)
496 except:
Klement Sekerada505f62017-01-04 12:58:53 +0100497 self.test.logger.error(
498 "Timeout while waiting for NDP response")
Klement Sekeradab231a2016-12-21 08:50:14 +0100499 raise
500 ndp_reply = captured_packet.copy() # keep original for exception
Neale Ranns82a06a92016-12-08 20:05:33 +0000501 try:
502 ndp_na = ndp_reply[ICMPv6ND_NA]
503 opt = ndp_na[ICMPv6NDOptDstLLAddr]
504 self.test.logger.info("VPP %s MAC address is %s " %
505 (self.name, opt.lladdr))
506 self._local_mac = opt.lladdr
Klement Sekeradab231a2016-12-21 08:50:14 +0100507 self.test.logger.debug(self.test.vapi.cli("show trace"))
508 # we now have the MAC we've been after
509 return
Neale Ranns82a06a92016-12-08 20:05:33 +0000510 except:
511 self.test.logger.info(
Klement Sekerada505f62017-01-04 12:58:53 +0100512 ppp("Unexpected response to NDP request:",
513 captured_packet))
Klement Sekeradab231a2016-12-21 08:50:14 +0100514 now = time.time()
515
516 self.test.logger.debug(self.test.vapi.cli("show trace"))
517 raise Exception("Timeout while waiting for NDP response")