blob: c8debff030174979b4cc4f6532c52382b661b03d [file] [log] [blame]
Klement Sekeraf62ae122016-10-11 11:47:09 +02001import os
Paul Vinciguerra582eac52020-04-03 12:18:40 -04002import socket
snaramre5d4b8912019-12-13 23:39:35 +00003from socket import inet_pton, inet_ntop
Klement Sekerab91017a2017-02-09 06:04:36 +01004import struct
Paul Vinciguerra582eac52020-04-03 12:18:40 -04005import time
Klement Sekera97f6edc2017-01-12 07:17:01 +01006from traceback import format_exc, format_stack
Paul Vinciguerraa7427ec2019-03-10 10:04:23 -07007
8import scapy.compat
Klement Sekera0e3c0de2016-09-29 14:43:44 +02009from scapy.utils import wrpcap, rdpcap, PcapReader
Klement Sekera97f6edc2017-01-12 07:17:01 +010010from scapy.plist import PacketList
Klement Sekeraf62ae122016-10-11 11:47:09 +020011from vpp_interface import VppInterface
12
Matej Klotton0178d522016-11-04 11:11:44 +010013from scapy.layers.l2 import Ether, ARP
Klement Sekera0e3c0de2016-09-29 14:43:44 +020014from scapy.layers.inet6 import IPv6, ICMPv6ND_NS, ICMPv6ND_NA,\
Klement Sekera65cc8c02016-12-18 15:49:54 +010015 ICMPv6NDOptSrcLLAddr, ICMPv6NDOptDstLLAddr, ICMPv6ND_RA, RouterAlert, \
16 IPv6ExtHdrHopByHop
Klement Sekera9225dee2016-12-12 08:36:58 +010017from util import ppp, ppc
Neale Ranns75152282017-01-09 01:00:45 -080018from scapy.utils6 import in6_getnsma, in6_getnsmac, in6_ismaddr
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
Mohsin Kazmif382b062020-08-11 15:00:44 +020061 def coalesce_is_enabled(self):
62 """coalesce enabled on packet-generator interface"""
63 if self._coalesce_enabled == 0:
64 return "coalesce-disabled"
65 return "coalesce-enabled"
66
67 @property
Klement Sekeraf62ae122016-10-11 11:47:09 +020068 def out_path(self):
69 """pcap file path - captured packets"""
70 return self._out_path
71
Klement Sekera7ba9fae2021-03-31 13:36:38 +020072 def get_in_path(self, worker):
Klement Sekeraf62ae122016-10-11 11:47:09 +020073 """ pcap file path - injected packets"""
Klement Sekera7ba9fae2021-03-31 13:36:38 +020074 if worker is not None:
75 return "%s/pg%u_wrk%u_in.pcap" % (self.test.tempdir, self.pg_index,
76 worker)
77 return "%s/pg%u_in.pcap" % (self.test.tempdir, self.pg_index)
Klement Sekeraf62ae122016-10-11 11:47:09 +020078
79 @property
80 def capture_cli(self):
81 """CLI string to start capture on this interface"""
82 return self._capture_cli
83
Klement Sekera7ba9fae2021-03-31 13:36:38 +020084 def get_cap_name(self, worker=None):
85 """return capture name for this interface and given worker"""
86 if worker is not None:
87 return self._cap_name + "-worker%d" % worker
Klement Sekeraf62ae122016-10-11 11:47:09 +020088 return self._cap_name
89
Klement Sekera7ba9fae2021-03-31 13:36:38 +020090 def get_input_cli(self, nb_replays=None, worker=None):
91 """return CLI string to load the injected packets"""
92 input_cli = "packet-generator new pcap %s source pg%u name %s" % (
93 self.get_in_path(worker), self.pg_index, self.get_cap_name(worker))
94 if nb_replays is not None:
95 return "%s limit %d" % (input_cli, nb_replays)
96 if worker is not None:
97 return "%s worker %d" % (input_cli, worker)
98 return input_cli
Klement Sekeraf62ae122016-10-11 11:47:09 +020099
Klement Sekera778c2762016-11-08 02:00:28 +0100100 @property
101 def in_history_counter(self):
102 """Self-incrementing counter used when renaming old pcap files"""
103 v = self._in_history_counter
104 self._in_history_counter += 1
105 return v
106
107 @property
108 def out_history_counter(self):
109 """Self-incrementing counter used when renaming old pcap files"""
110 v = self._out_history_counter
111 self._out_history_counter += 1
112 return v
113
Mohsin Kazmi22e9cfd2019-07-23 11:54:48 +0200114 def __init__(self, test, pg_index, gso, gso_size):
Matej Klottonc5bf07f2016-11-23 15:27:17 +0100115 """ Create VPP packet-generator interface """
Klement Sekera7ba9fae2021-03-31 13:36:38 +0200116 super().__init__(test)
Klement Sekeraa98346f2018-05-16 10:52:45 +0200117
Mohsin Kazmi22e9cfd2019-07-23 11:54:48 +0200118 r = test.vapi.pg_create_interface(pg_index, gso, gso_size)
Klement Sekera31da2e32018-06-24 22:49:55 +0200119 self.set_sw_if_index(r.sw_if_index)
120
Matej Klottonc5bf07f2016-11-23 15:27:17 +0100121 self._in_history_counter = 0
122 self._out_history_counter = 0
Klement Sekera97f6edc2017-01-12 07:17:01 +0100123 self._out_assert_counter = 0
Matej Klottonc5bf07f2016-11-23 15:27:17 +0100124 self._pg_index = pg_index
Mohsin Kazmi22e9cfd2019-07-23 11:54:48 +0200125 self._gso_enabled = gso
126 self._gso_size = gso_size
Mohsin Kazmif382b062020-08-11 15:00:44 +0200127 self._coalesce_enabled = 0
Klement Sekera74dcdbf2016-11-14 09:49:09 +0100128 self._out_file = "pg%u_out.pcap" % self.pg_index
Klement Sekera778c2762016-11-08 02:00:28 +0100129 self._out_path = self.test.tempdir + "/" + self._out_file
Klement Sekeraf62ae122016-10-11 11:47:09 +0200130 self._capture_cli = "packet-generator capture pg%u pcap %s" % (
131 self.pg_index, self.out_path)
Paul Vinciguerra44b0b072019-06-25 20:51:31 -0400132 self._cap_name = "pcap%u-sw_if_index-%s" % (
133 self.pg_index, self.sw_if_index)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200134
Klement Sekera7ba9fae2021-03-31 13:36:38 +0200135 def rename_previous_capture_file(self, path, counter):
Paul Vinciguerra4b58a862019-05-28 15:40:47 -0400136 # if a file from a previous capture exists, rename it.
Klement Sekera7ba9fae2021-03-31 13:36:38 +0200137 filename = os.path.basename(path)
Paul Vinciguerra4b58a862019-05-28 15:40:47 -0400138 try:
139 if os.path.isfile(path):
140 name = "%s/history.[timestamp:%f].[%s-counter:%04d].%s" % \
141 (self.test.tempdir,
142 time.time(),
143 self.name,
144 counter,
Klement Sekera7ba9fae2021-03-31 13:36:38 +0200145 filename)
Paul Vinciguerra4b58a862019-05-28 15:40:47 -0400146 self.test.logger.debug("Renaming %s->%s" %
147 (path, name))
148 os.rename(path, name)
149 except OSError:
150 self.test.logger.debug("OSError: Could not rename %s %s" %
Klement Sekera7ba9fae2021-03-31 13:36:38 +0200151 (path, filename))
Paul Vinciguerra4b58a862019-05-28 15:40:47 -0400152
Klement Sekerada505f62017-01-04 12:58:53 +0100153 def enable_capture(self):
Alexandre Poirriera618e202019-05-07 10:43:41 +0200154 """ Enable capture on this packet-generator interface
155 of at most n packets.
156 If n < 0, this is no limit
157 """
Andrew Yourtchenkocb265c62019-07-25 10:03:51 +0000158 # disable the capture to flush the capture
159 self.disable_capture()
Klement Sekera7ba9fae2021-03-31 13:36:38 +0200160 self.rename_previous_capture_file(self.out_path,
161 self.out_history_counter)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200162 # FIXME this should be an API, but no such exists atm
163 self.test.vapi.cli(self.capture_cli)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200164 self._pcap_reader = None
Klement Sekeraf62ae122016-10-11 11:47:09 +0200165
Alexandre Poirriera618e202019-05-07 10:43:41 +0200166 def disable_capture(self):
167 self.test.vapi.cli("%s disable" % self.capture_cli)
168
Mohsin Kazmif382b062020-08-11 15:00:44 +0200169 def coalesce_enable(self):
170 """ Enable packet coalesce on this packet-generator interface"""
171 self._coalesce_enabled = 1
172 self.test.vapi.pg_interface_enable_disable_coalesce(self.sw_if_index,
173 1)
174
175 def coalesce_disable(self):
176 """ Disable packet coalesce on this packet-generator interface"""
177 self._coalesce_enabled = 0
178 self.test.vapi.pg_interface_enable_disable_coalesce(self.sw_if_index,
179 0)
180
Klement Sekera4ecbf102019-07-31 13:14:16 +0000181 def add_stream(self, pkts, nb_replays=None, worker=None):
Klement Sekeraf62ae122016-10-11 11:47:09 +0200182 """
183 Add a stream of packets to this packet-generator
184
185 :param pkts: iterable packets
186
187 """
Klement Sekera7ba9fae2021-03-31 13:36:38 +0200188 wrpcap(self.get_in_path(worker), pkts)
189 self.test.register_capture(self, worker)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200190 # FIXME this should be an API, but no such exists atm
Klement Sekera7ba9fae2021-03-31 13:36:38 +0200191 self.test.vapi.cli(self.get_input_cli(nb_replays, worker))
Klement Sekeraf62ae122016-10-11 11:47:09 +0200192
Klement Sekera97f6edc2017-01-12 07:17:01 +0100193 def generate_debug_aid(self, kind):
194 """ Create a hardlink to the out file with a counter and a file
195 containing stack trace to ease debugging in case of multiple capture
196 files present. """
197 self.test.logger.debug("Generating debug aid for %s on %s" %
198 (kind, self._name))
199 link_path, stack_path = ["%s/debug_%s_%s_%s.%s" %
200 (self.test.tempdir, self._name,
201 self._out_assert_counter, kind, suffix)
202 for suffix in ["pcap", "stack"]
203 ]
204 os.link(self.out_path, link_path)
205 with open(stack_path, "w") as f:
206 f.writelines(format_stack())
207 self._out_assert_counter += 1
208
Klement Sekeradab231a2016-12-21 08:50:14 +0100209 def _get_capture(self, timeout, filter_out_fn=is_ipv6_misc):
210 """ Helper method to get capture and filter it """
Klement Sekeraf62ae122016-10-11 11:47:09 +0200211 try:
Klement Sekeradab231a2016-12-21 08:50:14 +0100212 if not self.wait_for_capture_file(timeout):
213 return None
Klement Sekeraf62ae122016-10-11 11:47:09 +0200214 output = rdpcap(self.out_path)
Klement Sekera65cc8c02016-12-18 15:49:54 +0100215 self.test.logger.debug("Capture has %s packets" % len(output.res))
Klement Sekeradab231a2016-12-21 08:50:14 +0100216 except:
Jane546d3b2016-12-08 13:10:03 +0100217 self.test.logger.debug("Exception in scapy.rdpcap (%s): %s" %
Klement Sekeradab231a2016-12-21 08:50:14 +0100218 (self.out_path, format_exc()))
219 return None
Klement Sekera65cc8c02016-12-18 15:49:54 +0100220 before = len(output.res)
Klement Sekeradab231a2016-12-21 08:50:14 +0100221 if filter_out_fn:
222 output.res = [p for p in output.res if not filter_out_fn(p)]
Klement Sekera97f6edc2017-01-12 07:17:01 +0100223 removed = before - len(output.res)
Klement Sekera65cc8c02016-12-18 15:49:54 +0100224 if removed:
225 self.test.logger.debug(
226 "Filtered out %s packets from capture (returning %s)" %
227 (removed, len(output.res)))
Klement Sekeraf62ae122016-10-11 11:47:09 +0200228 return output
Matej Klotton0178d522016-11-04 11:11:44 +0100229
Klement Sekeradab231a2016-12-21 08:50:14 +0100230 def get_capture(self, expected_count=None, remark=None, timeout=1,
231 filter_out_fn=is_ipv6_misc):
232 """ Get captured packets
233
234 :param expected_count: expected number of packets to capture, if None,
235 then self.test.packet_count_for_dst_pg_idx is
236 used to lookup the expected count
237 :param remark: remark printed into debug logs
238 :param timeout: how long to wait for packets
239 :param filter_out_fn: filter applied to each packet, packets for which
240 the filter returns True are removed from capture
241 :returns: iterable packets
242 """
243 remaining_time = timeout
244 capture = None
245 name = self.name if remark is None else "%s (%s)" % (self.name, remark)
246 based_on = "based on provided argument"
247 if expected_count is None:
248 expected_count = \
249 self.test.get_packet_count_for_if_idx(self.sw_if_index)
250 based_on = "based on stored packet_infos"
Klement Sekerac86fa022017-01-02 09:03:47 +0100251 if expected_count == 0:
252 raise Exception(
Klement Sekerada505f62017-01-04 12:58:53 +0100253 "Internal error, expected packet count for %s is 0!" %
254 name)
Jane546d3b2016-12-08 13:10:03 +0100255 self.test.logger.debug("Expecting to capture %s (%s) packets on %s" % (
Klement Sekeradab231a2016-12-21 08:50:14 +0100256 expected_count, based_on, name))
Klement Sekeradab231a2016-12-21 08:50:14 +0100257 while remaining_time > 0:
258 before = time.time()
259 capture = self._get_capture(remaining_time, filter_out_fn)
260 elapsed_time = time.time() - before
261 if capture:
262 if len(capture.res) == expected_count:
263 # bingo, got the packets we expected
264 return capture
Jan Gelety057bb8c2016-12-20 17:32:45 +0100265 elif len(capture.res) > expected_count:
266 self.test.logger.error(
267 ppc("Unexpected packets captured:", capture))
268 break
Klement Sekera97f6edc2017-01-12 07:17:01 +0100269 else:
270 self.test.logger.debug("Partial capture containing %s "
271 "packets doesn't match expected "
272 "count %s (yet?)" %
273 (len(capture.res), expected_count))
274 elif expected_count == 0:
275 # bingo, got None as we expected - return empty capture
276 return PacketList()
Klement Sekeradab231a2016-12-21 08:50:14 +0100277 remaining_time -= elapsed_time
278 if capture:
Klement Sekera97f6edc2017-01-12 07:17:01 +0100279 self.generate_debug_aid("count-mismatch")
Klement Sekeradab231a2016-12-21 08:50:14 +0100280 raise Exception("Captured packets mismatch, captured %s packets, "
281 "expected %s packets on %s" %
282 (len(capture.res), expected_count, name))
283 else:
284 raise Exception("No packets captured on %s" % name)
285
286 def assert_nothing_captured(self, remark=None, filter_out_fn=is_ipv6_misc):
287 """ Assert that nothing unfiltered was captured on interface
288
289 :param remark: remark printed into debug logs
290 :param filter_out_fn: filter applied to each packet, packets for which
291 the filter returns True are removed from capture
292 """
Klement Sekera9225dee2016-12-12 08:36:58 +0100293 if os.path.isfile(self.out_path):
294 try:
Klement Sekeradab231a2016-12-21 08:50:14 +0100295 capture = self.get_capture(
296 0, remark=remark, filter_out_fn=filter_out_fn)
Klement Sekera97f6edc2017-01-12 07:17:01 +0100297 if not capture or len(capture.res) == 0:
Jane546d3b2016-12-08 13:10:03 +0100298 # junk filtered out, we're good
299 return
Klement Sekera9225dee2016-12-12 08:36:58 +0100300 except:
301 pass
Klement Sekera97f6edc2017-01-12 07:17:01 +0100302 self.generate_debug_aid("empty-assert")
Klement Sekera9225dee2016-12-12 08:36:58 +0100303 if remark:
304 raise AssertionError(
Jane546d3b2016-12-08 13:10:03 +0100305 "Non-empty capture file present for interface %s (%s)" %
Klement Sekera9225dee2016-12-12 08:36:58 +0100306 (self.name, remark))
307 else:
Jane546d3b2016-12-08 13:10:03 +0100308 raise AssertionError("Capture file present for interface %s" %
309 self.name)
Klement Sekera9225dee2016-12-12 08:36:58 +0100310
Andrew Yourtchenko3d36f192019-10-11 12:34:12 +0000311 def wait_for_pg_stop(self):
312 # wait till packet-generator is stopped
313 # "show packet-generator" while it is still running gives this:
314 # Name Enabled Count Parameters
315 # pcap0-sw_if_inde Yes 64 limit 64, ...
316 #
317 # also have a 5-minute timeout just in case things go terribly wrong...
318 deadline = time.time() + 300
319 while self.test.vapi.cli('show packet-generator').find("Yes") != -1:
320 self._test.sleep(0.01) # yield
321 if time.time() > deadline:
322 self.test.logger.debug("Timeout waiting for pg to stop")
323 break
324
Klement Sekera9225dee2016-12-12 08:36:58 +0100325 def wait_for_capture_file(self, timeout=1):
326 """
327 Wait until pcap capture file appears
328
329 :param timeout: How long to wait for the packet (default 1s)
330
Klement Sekeradab231a2016-12-21 08:50:14 +0100331 :returns: True/False if the file is present or appears within timeout
Klement Sekera9225dee2016-12-12 08:36:58 +0100332 """
Andrew Yourtchenko3d36f192019-10-11 12:34:12 +0000333 self.wait_for_pg_stop()
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100334 deadline = time.time() + timeout
Klement Sekera9225dee2016-12-12 08:36:58 +0100335 if not os.path.isfile(self.out_path):
Klement Sekeradab231a2016-12-21 08:50:14 +0100336 self.test.logger.debug("Waiting for capture file %s to appear, "
337 "timeout is %ss" % (self.out_path, timeout))
Klement Sekera9225dee2016-12-12 08:36:58 +0100338 else:
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100339 self.test.logger.debug("Capture file %s already exists" %
340 self.out_path)
Klement Sekeradab231a2016-12-21 08:50:14 +0100341 return True
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100342 while time.time() < deadline:
Klement Sekera9225dee2016-12-12 08:36:58 +0100343 if os.path.isfile(self.out_path):
344 break
Paul Vinciguerra0f6602c2019-03-10 09:10:54 -0700345 self._test.sleep(0) # yield
Klement Sekera9225dee2016-12-12 08:36:58 +0100346 if os.path.isfile(self.out_path):
347 self.test.logger.debug("Capture file appeared after %fs" %
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100348 (time.time() - (deadline - timeout)))
Klement Sekera9225dee2016-12-12 08:36:58 +0100349 else:
350 self.test.logger.debug("Timeout - capture file still nowhere")
Klement Sekeradab231a2016-12-21 08:50:14 +0100351 return False
352 return True
Klement Sekera9225dee2016-12-12 08:36:58 +0100353
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100354 def verify_enough_packet_data_in_pcap(self):
Klement Sekerab91017a2017-02-09 06:04:36 +0100355 """
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100356 Check if enough data is available in file handled by internal pcap
357 reader so that a whole packet can be read.
Klement Sekerab91017a2017-02-09 06:04:36 +0100358
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100359 :returns: True if enough data present, else False
Klement Sekerab91017a2017-02-09 06:04:36 +0100360 """
361 orig_pos = self._pcap_reader.f.tell() # save file position
362 enough_data = False
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100363 # read packet header from pcap
364 packet_header_size = 16
365 caplen = None
366 end_pos = None
367 hdr = self._pcap_reader.f.read(packet_header_size)
368 if len(hdr) == packet_header_size:
369 # parse the capture length - caplen
Klement Sekerab91017a2017-02-09 06:04:36 +0100370 sec, usec, caplen, wirelen = struct.unpack(
371 self._pcap_reader.endian + "IIII", hdr)
372 self._pcap_reader.f.seek(0, 2) # seek to end of file
373 end_pos = self._pcap_reader.f.tell() # get position at end
374 if end_pos >= orig_pos + len(hdr) + caplen:
375 enough_data = True # yay, we have enough data
Klement Sekerab91017a2017-02-09 06:04:36 +0100376 self._pcap_reader.f.seek(orig_pos, 0) # restore original position
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100377 return enough_data
Klement Sekerab91017a2017-02-09 06:04:36 +0100378
Klement Sekeradab231a2016-12-21 08:50:14 +0100379 def wait_for_packet(self, timeout, filter_out_fn=is_ipv6_misc):
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200380 """
381 Wait for next packet captured with a timeout
382
383 :param timeout: How long to wait for the packet
384
385 :returns: Captured packet if no packet arrived within timeout
386 :raises Exception: if no packet arrives within timeout
387 """
Klement Sekeradab231a2016-12-21 08:50:14 +0100388 deadline = time.time() + timeout
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200389 if self._pcap_reader is None:
Klement Sekeradab231a2016-12-21 08:50:14 +0100390 if not self.wait_for_capture_file(timeout):
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100391 raise CaptureTimeoutError("Capture file %s did not appear "
392 "within timeout" % self.out_path)
Klement Sekeradab231a2016-12-21 08:50:14 +0100393 while time.time() < deadline:
394 try:
395 self._pcap_reader = PcapReader(self.out_path)
396 break
397 except:
Klement Sekerada505f62017-01-04 12:58:53 +0100398 self.test.logger.debug(
Klement Sekera97f6edc2017-01-12 07:17:01 +0100399 "Exception in scapy.PcapReader(%s): %s" %
Klement Sekerada505f62017-01-04 12:58:53 +0100400 (self.out_path, format_exc()))
Klement Sekeradab231a2016-12-21 08:50:14 +0100401 if not self._pcap_reader:
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100402 raise CaptureTimeoutError("Capture file %s did not appear within "
403 "timeout" % self.out_path)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200404
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100405 poll = False
406 if timeout > 0:
407 self.test.logger.debug("Waiting for packet")
408 else:
409 poll = True
410 self.test.logger.debug("Polling for packet")
411 while time.time() < deadline or poll:
412 if not self.verify_enough_packet_data_in_pcap():
Paul Vinciguerra0f6602c2019-03-10 09:10:54 -0700413 self._test.sleep(0) # yield
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100414 poll = False
415 continue
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200416 p = self._pcap_reader.recv()
417 if p is not None:
Klement Sekeradab231a2016-12-21 08:50:14 +0100418 if filter_out_fn is not None and filter_out_fn(p):
419 self.test.logger.debug(
420 "Packet received after %ss was filtered out" %
421 (time.time() - (deadline - timeout)))
422 else:
Klement Sekerada505f62017-01-04 12:58:53 +0100423 self.test.logger.debug(
424 "Packet received after %fs" %
425 (time.time() - (deadline - timeout)))
Klement Sekeradab231a2016-12-21 08:50:14 +0100426 return p
Paul Vinciguerra0f6602c2019-03-10 09:10:54 -0700427 self._test.sleep(0) # yield
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100428 poll = False
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200429 self.test.logger.debug("Timeout - no packets received")
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100430 raise CaptureTimeoutError("Packet didn't arrive within timeout")
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200431
Matej Klotton0178d522016-11-04 11:11:44 +0100432 def create_arp_req(self):
433 """Create ARP request applicable for this interface"""
434 return (Ether(dst="ff:ff:ff:ff:ff:ff", src=self.remote_mac) /
435 ARP(op=ARP.who_has, pdst=self.local_ip4,
436 psrc=self.remote_ip4, hwsrc=self.remote_mac))
437
Benoît Ganne2699fe22021-01-18 19:25:38 +0100438 def create_ndp_req(self, addr=None):
Matej Klotton0178d522016-11-04 11:11:44 +0100439 """Create NDP - NS applicable for this interface"""
Benoît Ganne2699fe22021-01-18 19:25:38 +0100440 if not addr:
441 addr = self.local_ip6
442 nsma = in6_getnsma(inet_pton(socket.AF_INET6, addr))
Neale Ranns465a1a32017-01-07 10:04:09 -0800443 d = inet_ntop(socket.AF_INET6, nsma)
444
445 return (Ether(dst=in6_getnsmac(nsma)) /
446 IPv6(dst=d, src=self.remote_ip6) /
Benoît Ganne2699fe22021-01-18 19:25:38 +0100447 ICMPv6ND_NS(tgt=addr) /
Klement Sekera74dcdbf2016-11-14 09:49:09 +0100448 ICMPv6NDOptSrcLLAddr(lladdr=self.remote_mac))
Matej Klotton0178d522016-11-04 11:11:44 +0100449
450 def resolve_arp(self, pg_interface=None):
451 """Resolve ARP using provided packet-generator interface
452
453 :param pg_interface: interface used to resolve, if None then this
454 interface is used
455
456 """
457 if pg_interface is None:
458 pg_interface = self
Klement Sekera7bb873a2016-11-18 07:38:42 +0100459 self.test.logger.info("Sending ARP request for %s on port %s" %
460 (self.local_ip4, pg_interface.name))
Matej Klotton0178d522016-11-04 11:11:44 +0100461 arp_req = self.create_arp_req()
462 pg_interface.add_stream(arp_req)
463 pg_interface.enable_capture()
464 self.test.pg_start()
Klement Sekera7bb873a2016-11-18 07:38:42 +0100465 self.test.logger.info(self.test.vapi.cli("show trace"))
Klement Sekera9225dee2016-12-12 08:36:58 +0100466 try:
Klement Sekeradab231a2016-12-21 08:50:14 +0100467 captured_packet = pg_interface.wait_for_packet(1)
Klement Sekera9225dee2016-12-12 08:36:58 +0100468 except:
469 self.test.logger.info("No ARP received on port %s" %
470 pg_interface.name)
Matej Klotton0178d522016-11-04 11:11:44 +0100471 return
Klement Sekeradab231a2016-12-21 08:50:14 +0100472 arp_reply = captured_packet.copy() # keep original for exception
Matej Klotton0178d522016-11-04 11:11:44 +0100473 try:
474 if arp_reply[ARP].op == ARP.is_at:
Klement Sekera7bb873a2016-11-18 07:38:42 +0100475 self.test.logger.info("VPP %s MAC address is %s " %
476 (self.name, arp_reply[ARP].hwsrc))
Matej Klotton0178d522016-11-04 11:11:44 +0100477 self._local_mac = arp_reply[ARP].hwsrc
478 else:
Klement Sekeradab231a2016-12-21 08:50:14 +0100479 self.test.logger.info("No ARP received on port %s" %
480 pg_interface.name)
Matej Klotton0178d522016-11-04 11:11:44 +0100481 except:
Klement Sekera7bb873a2016-11-18 07:38:42 +0100482 self.test.logger.error(
Klement Sekeradab231a2016-12-21 08:50:14 +0100483 ppp("Unexpected response to ARP request:", captured_packet))
Matej Klotton0178d522016-11-04 11:11:44 +0100484 raise
485
Benoît Ganne2699fe22021-01-18 19:25:38 +0100486 def resolve_ndp(self, pg_interface=None, timeout=1, link_layer=False):
Matej Klotton0178d522016-11-04 11:11:44 +0100487 """Resolve NDP using provided packet-generator interface
488
489 :param pg_interface: interface used to resolve, if None then this
490 interface is used
Klement Sekeradab231a2016-12-21 08:50:14 +0100491 :param timeout: how long to wait for response before giving up
Benoît Ganne2699fe22021-01-18 19:25:38 +0100492 :param link_layer: resolve for global address if False (default)
493 or for link-layer address if True
Matej Klotton0178d522016-11-04 11:11:44 +0100494
495 """
496 if pg_interface is None:
497 pg_interface = self
Benoît Ganne2699fe22021-01-18 19:25:38 +0100498 addr = self.local_ip6_ll if link_layer else self.local_ip6
Klement Sekera7bb873a2016-11-18 07:38:42 +0100499 self.test.logger.info("Sending NDP request for %s on port %s" %
Benoît Ganne2699fe22021-01-18 19:25:38 +0100500 (addr, pg_interface.name))
501 ndp_req = self.create_ndp_req(addr)
Matej Klotton0178d522016-11-04 11:11:44 +0100502 pg_interface.add_stream(ndp_req)
503 pg_interface.enable_capture()
504 self.test.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +0100505 now = time.time()
506 deadline = now + timeout
Neale Ranns82a06a92016-12-08 20:05:33 +0000507 # Enabling IPv6 on an interface can generate more than the
508 # ND reply we are looking for (namely MLD). So loop through
509 # the replies to look for want we want.
Klement Sekeradab231a2016-12-21 08:50:14 +0100510 while now < deadline:
511 try:
512 captured_packet = pg_interface.wait_for_packet(
513 deadline - now, filter_out_fn=None)
514 except:
Klement Sekerada505f62017-01-04 12:58:53 +0100515 self.test.logger.error(
516 "Timeout while waiting for NDP response")
Klement Sekeradab231a2016-12-21 08:50:14 +0100517 raise
518 ndp_reply = captured_packet.copy() # keep original for exception
Neale Ranns82a06a92016-12-08 20:05:33 +0000519 try:
520 ndp_na = ndp_reply[ICMPv6ND_NA]
521 opt = ndp_na[ICMPv6NDOptDstLLAddr]
522 self.test.logger.info("VPP %s MAC address is %s " %
523 (self.name, opt.lladdr))
524 self._local_mac = opt.lladdr
Klement Sekeradab231a2016-12-21 08:50:14 +0100525 self.test.logger.debug(self.test.vapi.cli("show trace"))
526 # we now have the MAC we've been after
527 return
Neale Ranns82a06a92016-12-08 20:05:33 +0000528 except:
529 self.test.logger.info(
Klement Sekerada505f62017-01-04 12:58:53 +0100530 ppp("Unexpected response to NDP request:",
531 captured_packet))
Klement Sekeradab231a2016-12-21 08:50:14 +0100532 now = time.time()
533
534 self.test.logger.debug(self.test.vapi.cli("show trace"))
535 raise Exception("Timeout while waiting for NDP response")