blob: 567b81de7c08bf6f46d42edc941df662c869999f [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
Neale Ranns6197cb72021-06-03 14:43:21 +000012from vpp_papi import VppEnum
Klement Sekeraf62ae122016-10-11 11:47:09 +020013
Matej Klotton0178d522016-11-04 11:11:44 +010014from scapy.layers.l2 import Ether, ARP
Klement Sekera0e3c0de2016-09-29 14:43:44 +020015from scapy.layers.inet6 import IPv6, ICMPv6ND_NS, ICMPv6ND_NA,\
Klement Sekera65cc8c02016-12-18 15:49:54 +010016 ICMPv6NDOptSrcLLAddr, ICMPv6NDOptDstLLAddr, ICMPv6ND_RA, RouterAlert, \
17 IPv6ExtHdrHopByHop
Klement Sekera9225dee2016-12-12 08:36:58 +010018from util import ppp, ppc
Neale Ranns75152282017-01-09 01:00:45 -080019from scapy.utils6 import in6_getnsma, in6_getnsmac, in6_ismaddr
Klement Sekeraf62ae122016-10-11 11:47:09 +020020
Klement Sekerada505f62017-01-04 12:58:53 +010021
Klement Sekeraacb9b8e2017-02-14 02:55:31 +010022class CaptureTimeoutError(Exception):
23 """ Exception raised if capture or packet doesn't appear within timeout """
24 pass
25
26
Klement Sekera65cc8c02016-12-18 15:49:54 +010027def is_ipv6_misc(p):
28 """ Is packet one of uninteresting IPv6 broadcasts? """
29 if p.haslayer(ICMPv6ND_RA):
Neale Ranns75152282017-01-09 01:00:45 -080030 if in6_ismaddr(p[IPv6].dst):
31 return True
Klement Sekera65cc8c02016-12-18 15:49:54 +010032 if p.haslayer(IPv6ExtHdrHopByHop):
33 for o in p[IPv6ExtHdrHopByHop].options:
34 if isinstance(o, RouterAlert):
35 return True
36 return False
37
38
Klement Sekeraf62ae122016-10-11 11:47:09 +020039class VppPGInterface(VppInterface):
40 """
41 VPP packet-generator interface
42 """
43
44 @property
45 def pg_index(self):
46 """packet-generator interface index assigned by VPP"""
47 return self._pg_index
48
49 @property
Mohsin Kazmi22e9cfd2019-07-23 11:54:48 +020050 def gso_enabled(self):
51 """gso enabled on packet-generator interface"""
52 if self._gso_enabled == 0:
53 return "gso-disabled"
54 return "gso-enabled"
55
56 @property
57 def gso_size(self):
58 """gso size on packet-generator interface"""
59 return self._gso_size
60
61 @property
Mohsin Kazmif382b062020-08-11 15:00:44 +020062 def coalesce_is_enabled(self):
63 """coalesce enabled on packet-generator interface"""
64 if self._coalesce_enabled == 0:
65 return "coalesce-disabled"
66 return "coalesce-enabled"
67
68 @property
Klement Sekeraf62ae122016-10-11 11:47:09 +020069 def out_path(self):
70 """pcap file path - captured packets"""
71 return self._out_path
72
Klement Sekera7ba9fae2021-03-31 13:36:38 +020073 def get_in_path(self, worker):
Klement Sekeraf62ae122016-10-11 11:47:09 +020074 """ pcap file path - injected packets"""
Klement Sekera7ba9fae2021-03-31 13:36:38 +020075 if worker is not None:
76 return "%s/pg%u_wrk%u_in.pcap" % (self.test.tempdir, self.pg_index,
77 worker)
78 return "%s/pg%u_in.pcap" % (self.test.tempdir, self.pg_index)
Klement Sekeraf62ae122016-10-11 11:47:09 +020079
80 @property
81 def capture_cli(self):
82 """CLI string to start capture on this interface"""
83 return self._capture_cli
84
Klement Sekera7ba9fae2021-03-31 13:36:38 +020085 def get_cap_name(self, worker=None):
86 """return capture name for this interface and given worker"""
87 if worker is not None:
88 return self._cap_name + "-worker%d" % worker
Klement Sekeraf62ae122016-10-11 11:47:09 +020089 return self._cap_name
90
Klement Sekera7ba9fae2021-03-31 13:36:38 +020091 def get_input_cli(self, nb_replays=None, worker=None):
92 """return CLI string to load the injected packets"""
93 input_cli = "packet-generator new pcap %s source pg%u name %s" % (
94 self.get_in_path(worker), self.pg_index, self.get_cap_name(worker))
95 if nb_replays is not None:
96 return "%s limit %d" % (input_cli, nb_replays)
97 if worker is not None:
98 return "%s worker %d" % (input_cli, worker)
99 return input_cli
Klement Sekeraf62ae122016-10-11 11:47:09 +0200100
Klement Sekera778c2762016-11-08 02:00:28 +0100101 @property
102 def in_history_counter(self):
103 """Self-incrementing counter used when renaming old pcap files"""
104 v = self._in_history_counter
105 self._in_history_counter += 1
106 return v
107
108 @property
109 def out_history_counter(self):
110 """Self-incrementing counter used when renaming old pcap files"""
111 v = self._out_history_counter
112 self._out_history_counter += 1
113 return v
114
Neale Ranns6197cb72021-06-03 14:43:21 +0000115 def __init__(self, test, pg_index, gso, gso_size, mode):
Matej Klottonc5bf07f2016-11-23 15:27:17 +0100116 """ Create VPP packet-generator interface """
Klement Sekera7ba9fae2021-03-31 13:36:38 +0200117 super().__init__(test)
Klement Sekeraa98346f2018-05-16 10:52:45 +0200118
Neale Ranns6197cb72021-06-03 14:43:21 +0000119 r = test.vapi.pg_create_interface_v2(pg_index, gso, gso_size, mode)
Klement Sekera31da2e32018-06-24 22:49:55 +0200120 self.set_sw_if_index(r.sw_if_index)
121
Matej Klottonc5bf07f2016-11-23 15:27:17 +0100122 self._in_history_counter = 0
123 self._out_history_counter = 0
Klement Sekera97f6edc2017-01-12 07:17:01 +0100124 self._out_assert_counter = 0
Matej Klottonc5bf07f2016-11-23 15:27:17 +0100125 self._pg_index = pg_index
Mohsin Kazmi22e9cfd2019-07-23 11:54:48 +0200126 self._gso_enabled = gso
127 self._gso_size = gso_size
Mohsin Kazmif382b062020-08-11 15:00:44 +0200128 self._coalesce_enabled = 0
Klement Sekera74dcdbf2016-11-14 09:49:09 +0100129 self._out_file = "pg%u_out.pcap" % self.pg_index
Klement Sekera778c2762016-11-08 02:00:28 +0100130 self._out_path = self.test.tempdir + "/" + self._out_file
Klement Sekeraf62ae122016-10-11 11:47:09 +0200131 self._capture_cli = "packet-generator capture pg%u pcap %s" % (
132 self.pg_index, self.out_path)
Paul Vinciguerra44b0b072019-06-25 20:51:31 -0400133 self._cap_name = "pcap%u-sw_if_index-%s" % (
134 self.pg_index, self.sw_if_index)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200135
Klement Sekera3ff6ffc2021-04-01 18:19:29 +0200136 def rename_old_pcap_file(self, path, counter):
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 Sekera3ff6ffc2021-04-01 18:19:29 +0200160 self.rename_old_pcap_file(self.out_path, self.out_history_counter)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200161 # FIXME this should be an API, but no such exists atm
162 self.test.vapi.cli(self.capture_cli)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200163 self._pcap_reader = None
Klement Sekeraf62ae122016-10-11 11:47:09 +0200164
Alexandre Poirriera618e202019-05-07 10:43:41 +0200165 def disable_capture(self):
166 self.test.vapi.cli("%s disable" % self.capture_cli)
167
Mohsin Kazmif382b062020-08-11 15:00:44 +0200168 def coalesce_enable(self):
169 """ Enable packet coalesce on this packet-generator interface"""
170 self._coalesce_enabled = 1
171 self.test.vapi.pg_interface_enable_disable_coalesce(self.sw_if_index,
172 1)
173
174 def coalesce_disable(self):
175 """ Disable packet coalesce on this packet-generator interface"""
176 self._coalesce_enabled = 0
177 self.test.vapi.pg_interface_enable_disable_coalesce(self.sw_if_index,
178 0)
179
Klement Sekera4ecbf102019-07-31 13:14:16 +0000180 def add_stream(self, pkts, nb_replays=None, worker=None):
Klement Sekeraf62ae122016-10-11 11:47:09 +0200181 """
182 Add a stream of packets to this packet-generator
183
184 :param pkts: iterable packets
185
186 """
Klement Sekera7ba9fae2021-03-31 13:36:38 +0200187 wrpcap(self.get_in_path(worker), pkts)
Klement Sekera3ff6ffc2021-04-01 18:19:29 +0200188 self.test.register_pcap(self, worker)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200189 # FIXME this should be an API, but no such exists atm
Klement Sekera7ba9fae2021-03-31 13:36:38 +0200190 self.test.vapi.cli(self.get_input_cli(nb_replays, worker))
Klement Sekeraf62ae122016-10-11 11:47:09 +0200191
Klement Sekera97f6edc2017-01-12 07:17:01 +0100192 def generate_debug_aid(self, kind):
193 """ Create a hardlink to the out file with a counter and a file
194 containing stack trace to ease debugging in case of multiple capture
195 files present. """
196 self.test.logger.debug("Generating debug aid for %s on %s" %
197 (kind, self._name))
198 link_path, stack_path = ["%s/debug_%s_%s_%s.%s" %
199 (self.test.tempdir, self._name,
200 self._out_assert_counter, kind, suffix)
201 for suffix in ["pcap", "stack"]
202 ]
203 os.link(self.out_path, link_path)
204 with open(stack_path, "w") as f:
205 f.writelines(format_stack())
206 self._out_assert_counter += 1
207
Klement Sekeradab231a2016-12-21 08:50:14 +0100208 def _get_capture(self, timeout, filter_out_fn=is_ipv6_misc):
209 """ Helper method to get capture and filter it """
Klement Sekeraf62ae122016-10-11 11:47:09 +0200210 try:
Klement Sekeradab231a2016-12-21 08:50:14 +0100211 if not self.wait_for_capture_file(timeout):
212 return None
Klement Sekeraf62ae122016-10-11 11:47:09 +0200213 output = rdpcap(self.out_path)
Klement Sekera65cc8c02016-12-18 15:49:54 +0100214 self.test.logger.debug("Capture has %s packets" % len(output.res))
Klement Sekeradab231a2016-12-21 08:50:14 +0100215 except:
Jane546d3b2016-12-08 13:10:03 +0100216 self.test.logger.debug("Exception in scapy.rdpcap (%s): %s" %
Klement Sekeradab231a2016-12-21 08:50:14 +0100217 (self.out_path, format_exc()))
218 return None
Klement Sekera65cc8c02016-12-18 15:49:54 +0100219 before = len(output.res)
Klement Sekeradab231a2016-12-21 08:50:14 +0100220 if filter_out_fn:
221 output.res = [p for p in output.res if not filter_out_fn(p)]
Klement Sekera97f6edc2017-01-12 07:17:01 +0100222 removed = before - len(output.res)
Klement Sekera65cc8c02016-12-18 15:49:54 +0100223 if removed:
224 self.test.logger.debug(
225 "Filtered out %s packets from capture (returning %s)" %
226 (removed, len(output.res)))
Klement Sekeraf62ae122016-10-11 11:47:09 +0200227 return output
Matej Klotton0178d522016-11-04 11:11:44 +0100228
Klement Sekeradab231a2016-12-21 08:50:14 +0100229 def get_capture(self, expected_count=None, remark=None, timeout=1,
230 filter_out_fn=is_ipv6_misc):
231 """ Get captured packets
232
233 :param expected_count: expected number of packets to capture, if None,
234 then self.test.packet_count_for_dst_pg_idx is
235 used to lookup the expected count
236 :param remark: remark printed into debug logs
237 :param timeout: how long to wait for packets
238 :param filter_out_fn: filter applied to each packet, packets for which
239 the filter returns True are removed from capture
240 :returns: iterable packets
241 """
242 remaining_time = timeout
243 capture = None
244 name = self.name if remark is None else "%s (%s)" % (self.name, remark)
245 based_on = "based on provided argument"
246 if expected_count is None:
247 expected_count = \
248 self.test.get_packet_count_for_if_idx(self.sw_if_index)
249 based_on = "based on stored packet_infos"
Klement Sekerac86fa022017-01-02 09:03:47 +0100250 if expected_count == 0:
251 raise Exception(
Klement Sekerada505f62017-01-04 12:58:53 +0100252 "Internal error, expected packet count for %s is 0!" %
253 name)
Jane546d3b2016-12-08 13:10:03 +0100254 self.test.logger.debug("Expecting to capture %s (%s) packets on %s" % (
Klement Sekeradab231a2016-12-21 08:50:14 +0100255 expected_count, based_on, name))
Klement Sekeradab231a2016-12-21 08:50:14 +0100256 while remaining_time > 0:
257 before = time.time()
258 capture = self._get_capture(remaining_time, filter_out_fn)
259 elapsed_time = time.time() - before
260 if capture:
261 if len(capture.res) == expected_count:
262 # bingo, got the packets we expected
263 return capture
Jan Gelety057bb8c2016-12-20 17:32:45 +0100264 elif len(capture.res) > expected_count:
265 self.test.logger.error(
266 ppc("Unexpected packets captured:", capture))
267 break
Klement Sekera97f6edc2017-01-12 07:17:01 +0100268 else:
269 self.test.logger.debug("Partial capture containing %s "
270 "packets doesn't match expected "
271 "count %s (yet?)" %
272 (len(capture.res), expected_count))
273 elif expected_count == 0:
274 # bingo, got None as we expected - return empty capture
275 return PacketList()
Klement Sekeradab231a2016-12-21 08:50:14 +0100276 remaining_time -= elapsed_time
277 if capture:
Klement Sekera97f6edc2017-01-12 07:17:01 +0100278 self.generate_debug_aid("count-mismatch")
Klement Sekeradab231a2016-12-21 08:50:14 +0100279 raise Exception("Captured packets mismatch, captured %s packets, "
280 "expected %s packets on %s" %
281 (len(capture.res), expected_count, name))
282 else:
283 raise Exception("No packets captured on %s" % name)
284
285 def assert_nothing_captured(self, remark=None, filter_out_fn=is_ipv6_misc):
286 """ Assert that nothing unfiltered was captured on interface
287
288 :param remark: remark printed into debug logs
289 :param filter_out_fn: filter applied to each packet, packets for which
290 the filter returns True are removed from capture
291 """
Klement Sekera9225dee2016-12-12 08:36:58 +0100292 if os.path.isfile(self.out_path):
293 try:
Klement Sekeradab231a2016-12-21 08:50:14 +0100294 capture = self.get_capture(
295 0, remark=remark, filter_out_fn=filter_out_fn)
Klement Sekera97f6edc2017-01-12 07:17:01 +0100296 if not capture or len(capture.res) == 0:
Jane546d3b2016-12-08 13:10:03 +0100297 # junk filtered out, we're good
298 return
Klement Sekera9225dee2016-12-12 08:36:58 +0100299 except:
300 pass
Klement Sekera97f6edc2017-01-12 07:17:01 +0100301 self.generate_debug_aid("empty-assert")
Klement Sekera9225dee2016-12-12 08:36:58 +0100302 if remark:
303 raise AssertionError(
Jane546d3b2016-12-08 13:10:03 +0100304 "Non-empty capture file present for interface %s (%s)" %
Klement Sekera9225dee2016-12-12 08:36:58 +0100305 (self.name, remark))
306 else:
Jane546d3b2016-12-08 13:10:03 +0100307 raise AssertionError("Capture file present for interface %s" %
308 self.name)
Klement Sekera9225dee2016-12-12 08:36:58 +0100309
Andrew Yourtchenko3d36f192019-10-11 12:34:12 +0000310 def wait_for_pg_stop(self):
311 # wait till packet-generator is stopped
312 # "show packet-generator" while it is still running gives this:
313 # Name Enabled Count Parameters
314 # pcap0-sw_if_inde Yes 64 limit 64, ...
315 #
316 # also have a 5-minute timeout just in case things go terribly wrong...
317 deadline = time.time() + 300
318 while self.test.vapi.cli('show packet-generator').find("Yes") != -1:
319 self._test.sleep(0.01) # yield
320 if time.time() > deadline:
321 self.test.logger.debug("Timeout waiting for pg to stop")
322 break
323
Klement Sekera9225dee2016-12-12 08:36:58 +0100324 def wait_for_capture_file(self, timeout=1):
325 """
326 Wait until pcap capture file appears
327
328 :param timeout: How long to wait for the packet (default 1s)
329
Klement Sekeradab231a2016-12-21 08:50:14 +0100330 :returns: True/False if the file is present or appears within timeout
Klement Sekera9225dee2016-12-12 08:36:58 +0100331 """
Andrew Yourtchenko3d36f192019-10-11 12:34:12 +0000332 self.wait_for_pg_stop()
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100333 deadline = time.time() + timeout
Klement Sekera9225dee2016-12-12 08:36:58 +0100334 if not os.path.isfile(self.out_path):
Klement Sekeradab231a2016-12-21 08:50:14 +0100335 self.test.logger.debug("Waiting for capture file %s to appear, "
336 "timeout is %ss" % (self.out_path, timeout))
Klement Sekera9225dee2016-12-12 08:36:58 +0100337 else:
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100338 self.test.logger.debug("Capture file %s already exists" %
339 self.out_path)
Klement Sekeradab231a2016-12-21 08:50:14 +0100340 return True
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100341 while time.time() < deadline:
Klement Sekera9225dee2016-12-12 08:36:58 +0100342 if os.path.isfile(self.out_path):
343 break
Paul Vinciguerra0f6602c2019-03-10 09:10:54 -0700344 self._test.sleep(0) # yield
Klement Sekera9225dee2016-12-12 08:36:58 +0100345 if os.path.isfile(self.out_path):
346 self.test.logger.debug("Capture file appeared after %fs" %
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100347 (time.time() - (deadline - timeout)))
Klement Sekera9225dee2016-12-12 08:36:58 +0100348 else:
349 self.test.logger.debug("Timeout - capture file still nowhere")
Klement Sekeradab231a2016-12-21 08:50:14 +0100350 return False
351 return True
Klement Sekera9225dee2016-12-12 08:36:58 +0100352
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100353 def verify_enough_packet_data_in_pcap(self):
Klement Sekerab91017a2017-02-09 06:04:36 +0100354 """
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100355 Check if enough data is available in file handled by internal pcap
356 reader so that a whole packet can be read.
Klement Sekerab91017a2017-02-09 06:04:36 +0100357
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100358 :returns: True if enough data present, else False
Klement Sekerab91017a2017-02-09 06:04:36 +0100359 """
360 orig_pos = self._pcap_reader.f.tell() # save file position
361 enough_data = False
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100362 # read packet header from pcap
363 packet_header_size = 16
364 caplen = None
365 end_pos = None
366 hdr = self._pcap_reader.f.read(packet_header_size)
367 if len(hdr) == packet_header_size:
368 # parse the capture length - caplen
Klement Sekerab91017a2017-02-09 06:04:36 +0100369 sec, usec, caplen, wirelen = struct.unpack(
370 self._pcap_reader.endian + "IIII", hdr)
371 self._pcap_reader.f.seek(0, 2) # seek to end of file
372 end_pos = self._pcap_reader.f.tell() # get position at end
373 if end_pos >= orig_pos + len(hdr) + caplen:
374 enough_data = True # yay, we have enough data
Klement Sekerab91017a2017-02-09 06:04:36 +0100375 self._pcap_reader.f.seek(orig_pos, 0) # restore original position
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100376 return enough_data
Klement Sekerab91017a2017-02-09 06:04:36 +0100377
Klement Sekeradab231a2016-12-21 08:50:14 +0100378 def wait_for_packet(self, timeout, filter_out_fn=is_ipv6_misc):
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200379 """
380 Wait for next packet captured with a timeout
381
382 :param timeout: How long to wait for the packet
383
384 :returns: Captured packet if no packet arrived within timeout
385 :raises Exception: if no packet arrives within timeout
386 """
Klement Sekeradab231a2016-12-21 08:50:14 +0100387 deadline = time.time() + timeout
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200388 if self._pcap_reader is None:
Klement Sekeradab231a2016-12-21 08:50:14 +0100389 if not self.wait_for_capture_file(timeout):
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100390 raise CaptureTimeoutError("Capture file %s did not appear "
391 "within timeout" % self.out_path)
Klement Sekeradab231a2016-12-21 08:50:14 +0100392 while time.time() < deadline:
393 try:
394 self._pcap_reader = PcapReader(self.out_path)
395 break
396 except:
Klement Sekerada505f62017-01-04 12:58:53 +0100397 self.test.logger.debug(
Klement Sekera97f6edc2017-01-12 07:17:01 +0100398 "Exception in scapy.PcapReader(%s): %s" %
Klement Sekerada505f62017-01-04 12:58:53 +0100399 (self.out_path, format_exc()))
Klement Sekeradab231a2016-12-21 08:50:14 +0100400 if not self._pcap_reader:
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100401 raise CaptureTimeoutError("Capture file %s did not appear within "
402 "timeout" % self.out_path)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200403
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100404 poll = False
405 if timeout > 0:
406 self.test.logger.debug("Waiting for packet")
407 else:
408 poll = True
409 self.test.logger.debug("Polling for packet")
410 while time.time() < deadline or poll:
411 if not self.verify_enough_packet_data_in_pcap():
Paul Vinciguerra0f6602c2019-03-10 09:10:54 -0700412 self._test.sleep(0) # yield
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100413 poll = False
414 continue
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200415 p = self._pcap_reader.recv()
416 if p is not None:
Klement Sekeradab231a2016-12-21 08:50:14 +0100417 if filter_out_fn is not None and filter_out_fn(p):
418 self.test.logger.debug(
419 "Packet received after %ss was filtered out" %
420 (time.time() - (deadline - timeout)))
421 else:
Klement Sekerada505f62017-01-04 12:58:53 +0100422 self.test.logger.debug(
423 "Packet received after %fs" %
424 (time.time() - (deadline - timeout)))
Klement Sekeradab231a2016-12-21 08:50:14 +0100425 return p
Paul Vinciguerra0f6602c2019-03-10 09:10:54 -0700426 self._test.sleep(0) # yield
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100427 poll = False
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200428 self.test.logger.debug("Timeout - no packets received")
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100429 raise CaptureTimeoutError("Packet didn't arrive within timeout")
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200430
Matej Klotton0178d522016-11-04 11:11:44 +0100431 def create_arp_req(self):
432 """Create ARP request applicable for this interface"""
433 return (Ether(dst="ff:ff:ff:ff:ff:ff", src=self.remote_mac) /
434 ARP(op=ARP.who_has, pdst=self.local_ip4,
435 psrc=self.remote_ip4, hwsrc=self.remote_mac))
436
Benoît Ganne2699fe22021-01-18 19:25:38 +0100437 def create_ndp_req(self, addr=None):
Matej Klotton0178d522016-11-04 11:11:44 +0100438 """Create NDP - NS applicable for this interface"""
Benoît Ganne2699fe22021-01-18 19:25:38 +0100439 if not addr:
440 addr = self.local_ip6
441 nsma = in6_getnsma(inet_pton(socket.AF_INET6, addr))
Neale Ranns465a1a32017-01-07 10:04:09 -0800442 d = inet_ntop(socket.AF_INET6, nsma)
443
444 return (Ether(dst=in6_getnsmac(nsma)) /
445 IPv6(dst=d, src=self.remote_ip6) /
Benoît Ganne2699fe22021-01-18 19:25:38 +0100446 ICMPv6ND_NS(tgt=addr) /
Klement Sekera74dcdbf2016-11-14 09:49:09 +0100447 ICMPv6NDOptSrcLLAddr(lladdr=self.remote_mac))
Matej Klotton0178d522016-11-04 11:11:44 +0100448
449 def resolve_arp(self, pg_interface=None):
450 """Resolve ARP using provided packet-generator interface
451
452 :param pg_interface: interface used to resolve, if None then this
453 interface is used
454
455 """
456 if pg_interface is None:
457 pg_interface = self
Klement Sekera7bb873a2016-11-18 07:38:42 +0100458 self.test.logger.info("Sending ARP request for %s on port %s" %
459 (self.local_ip4, pg_interface.name))
Matej Klotton0178d522016-11-04 11:11:44 +0100460 arp_req = self.create_arp_req()
461 pg_interface.add_stream(arp_req)
462 pg_interface.enable_capture()
463 self.test.pg_start()
Klement Sekera7bb873a2016-11-18 07:38:42 +0100464 self.test.logger.info(self.test.vapi.cli("show trace"))
Klement Sekera9225dee2016-12-12 08:36:58 +0100465 try:
Klement Sekeradab231a2016-12-21 08:50:14 +0100466 captured_packet = pg_interface.wait_for_packet(1)
Klement Sekera9225dee2016-12-12 08:36:58 +0100467 except:
468 self.test.logger.info("No ARP received on port %s" %
469 pg_interface.name)
Matej Klotton0178d522016-11-04 11:11:44 +0100470 return
Klement Sekeradab231a2016-12-21 08:50:14 +0100471 arp_reply = captured_packet.copy() # keep original for exception
Matej Klotton0178d522016-11-04 11:11:44 +0100472 try:
473 if arp_reply[ARP].op == ARP.is_at:
Klement Sekera7bb873a2016-11-18 07:38:42 +0100474 self.test.logger.info("VPP %s MAC address is %s " %
475 (self.name, arp_reply[ARP].hwsrc))
Matej Klotton0178d522016-11-04 11:11:44 +0100476 self._local_mac = arp_reply[ARP].hwsrc
477 else:
Klement Sekeradab231a2016-12-21 08:50:14 +0100478 self.test.logger.info("No ARP received on port %s" %
479 pg_interface.name)
Matej Klotton0178d522016-11-04 11:11:44 +0100480 except:
Klement Sekera7bb873a2016-11-18 07:38:42 +0100481 self.test.logger.error(
Klement Sekeradab231a2016-12-21 08:50:14 +0100482 ppp("Unexpected response to ARP request:", captured_packet))
Matej Klotton0178d522016-11-04 11:11:44 +0100483 raise
484
Benoît Ganne2699fe22021-01-18 19:25:38 +0100485 def resolve_ndp(self, pg_interface=None, timeout=1, link_layer=False):
Matej Klotton0178d522016-11-04 11:11:44 +0100486 """Resolve NDP using provided packet-generator interface
487
488 :param pg_interface: interface used to resolve, if None then this
489 interface is used
Klement Sekeradab231a2016-12-21 08:50:14 +0100490 :param timeout: how long to wait for response before giving up
Benoît Ganne2699fe22021-01-18 19:25:38 +0100491 :param link_layer: resolve for global address if False (default)
492 or for link-layer address if True
Matej Klotton0178d522016-11-04 11:11:44 +0100493
494 """
495 if pg_interface is None:
496 pg_interface = self
Benoît Ganne2699fe22021-01-18 19:25:38 +0100497 addr = self.local_ip6_ll if link_layer else self.local_ip6
Klement Sekera7bb873a2016-11-18 07:38:42 +0100498 self.test.logger.info("Sending NDP request for %s on port %s" %
Benoît Ganne2699fe22021-01-18 19:25:38 +0100499 (addr, pg_interface.name))
500 ndp_req = self.create_ndp_req(addr)
Matej Klotton0178d522016-11-04 11:11:44 +0100501 pg_interface.add_stream(ndp_req)
502 pg_interface.enable_capture()
503 self.test.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +0100504 now = time.time()
505 deadline = now + timeout
Neale Ranns82a06a92016-12-08 20:05:33 +0000506 # Enabling IPv6 on an interface can generate more than the
507 # ND reply we are looking for (namely MLD). So loop through
508 # the replies to look for want we want.
Klement Sekeradab231a2016-12-21 08:50:14 +0100509 while now < deadline:
510 try:
511 captured_packet = pg_interface.wait_for_packet(
512 deadline - now, filter_out_fn=None)
513 except:
Klement Sekerada505f62017-01-04 12:58:53 +0100514 self.test.logger.error(
515 "Timeout while waiting for NDP response")
Klement Sekeradab231a2016-12-21 08:50:14 +0100516 raise
517 ndp_reply = captured_packet.copy() # keep original for exception
Neale Ranns82a06a92016-12-08 20:05:33 +0000518 try:
519 ndp_na = ndp_reply[ICMPv6ND_NA]
520 opt = ndp_na[ICMPv6NDOptDstLLAddr]
521 self.test.logger.info("VPP %s MAC address is %s " %
522 (self.name, opt.lladdr))
523 self._local_mac = opt.lladdr
Klement Sekeradab231a2016-12-21 08:50:14 +0100524 self.test.logger.debug(self.test.vapi.cli("show trace"))
525 # we now have the MAC we've been after
526 return
Neale Ranns82a06a92016-12-08 20:05:33 +0000527 except:
528 self.test.logger.info(
Klement Sekerada505f62017-01-04 12:58:53 +0100529 ppp("Unexpected response to NDP request:",
530 captured_packet))
Klement Sekeradab231a2016-12-21 08:50:14 +0100531 now = time.time()
532
533 self.test.logger.debug(self.test.vapi.cli("show trace"))
534 raise Exception("Timeout while waiting for NDP response")