blob: b7f1881d5167e07c7aee1a26c7a9b7a6d70a8143 [file] [log] [blame]
Klement Sekeraf62ae122016-10-11 11:47:09 +02001import os
Klement Sekera778c2762016-11-08 02:00:28 +01002import time
snaramre5d4b8912019-12-13 23:39:35 +00003from socket import inet_pton, inet_ntop
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
Klement Sekeraf62ae122016-10-11 11:47:09 +020018
Klement Sekerada505f62017-01-04 12:58:53 +010019
Klement Sekeraacb9b8e2017-02-14 02:55:31 +010020class CaptureTimeoutError(Exception):
21 """ Exception raised if capture or packet doesn't appear within timeout """
22 pass
23
24
Klement Sekera65cc8c02016-12-18 15:49:54 +010025def is_ipv6_misc(p):
26 """ Is packet one of uninteresting IPv6 broadcasts? """
27 if p.haslayer(ICMPv6ND_RA):
Neale Ranns75152282017-01-09 01:00:45 -080028 if in6_ismaddr(p[IPv6].dst):
29 return True
Klement Sekera65cc8c02016-12-18 15:49:54 +010030 if p.haslayer(IPv6ExtHdrHopByHop):
31 for o in p[IPv6ExtHdrHopByHop].options:
32 if isinstance(o, RouterAlert):
33 return True
34 return False
35
36
Klement Sekeraf62ae122016-10-11 11:47:09 +020037class VppPGInterface(VppInterface):
38 """
39 VPP packet-generator interface
40 """
41
42 @property
43 def pg_index(self):
44 """packet-generator interface index assigned by VPP"""
45 return self._pg_index
46
47 @property
Mohsin Kazmi22e9cfd2019-07-23 11:54:48 +020048 def gso_enabled(self):
49 """gso enabled on packet-generator interface"""
50 if self._gso_enabled == 0:
51 return "gso-disabled"
52 return "gso-enabled"
53
54 @property
55 def gso_size(self):
56 """gso size on packet-generator interface"""
57 return self._gso_size
58
59 @property
Klement Sekeraf62ae122016-10-11 11:47:09 +020060 def out_path(self):
61 """pcap file path - captured packets"""
62 return self._out_path
63
64 @property
65 def in_path(self):
66 """ pcap file path - injected packets"""
67 return self._in_path
68
69 @property
70 def capture_cli(self):
71 """CLI string to start capture on this interface"""
72 return self._capture_cli
73
74 @property
75 def cap_name(self):
76 """capture name for this interface"""
77 return self._cap_name
78
79 @property
80 def input_cli(self):
81 """CLI string to load the injected packets"""
Alexandre Poirriera618e202019-05-07 10:43:41 +020082 if self._nb_replays is not None:
83 return "%s limit %d" % (self._input_cli, self._nb_replays)
Klement Sekera4ecbf102019-07-31 13:14:16 +000084 if self._worker is not None:
85 return "%s worker %d" % (self._input_cli, self._worker)
Klement Sekeraf62ae122016-10-11 11:47:09 +020086 return self._input_cli
87
Klement Sekera778c2762016-11-08 02:00:28 +010088 @property
89 def in_history_counter(self):
90 """Self-incrementing counter used when renaming old pcap files"""
91 v = self._in_history_counter
92 self._in_history_counter += 1
93 return v
94
95 @property
96 def out_history_counter(self):
97 """Self-incrementing counter used when renaming old pcap files"""
98 v = self._out_history_counter
99 self._out_history_counter += 1
100 return v
101
Mohsin Kazmi22e9cfd2019-07-23 11:54:48 +0200102 def __init__(self, test, pg_index, gso, gso_size):
Matej Klottonc5bf07f2016-11-23 15:27:17 +0100103 """ Create VPP packet-generator interface """
Ole Troane0d2bd62018-06-22 22:36:46 +0200104 super(VppPGInterface, self).__init__(test)
Klement Sekeraa98346f2018-05-16 10:52:45 +0200105
Mohsin Kazmi22e9cfd2019-07-23 11:54:48 +0200106 r = test.vapi.pg_create_interface(pg_index, gso, gso_size)
Klement Sekera31da2e32018-06-24 22:49:55 +0200107 self.set_sw_if_index(r.sw_if_index)
108
Matej Klottonc5bf07f2016-11-23 15:27:17 +0100109 self._in_history_counter = 0
110 self._out_history_counter = 0
Klement Sekera97f6edc2017-01-12 07:17:01 +0100111 self._out_assert_counter = 0
Matej Klottonc5bf07f2016-11-23 15:27:17 +0100112 self._pg_index = pg_index
Mohsin Kazmi22e9cfd2019-07-23 11:54:48 +0200113 self._gso_enabled = gso
114 self._gso_size = gso_size
Klement Sekera74dcdbf2016-11-14 09:49:09 +0100115 self._out_file = "pg%u_out.pcap" % self.pg_index
Klement Sekera778c2762016-11-08 02:00:28 +0100116 self._out_path = self.test.tempdir + "/" + self._out_file
Klement Sekera74dcdbf2016-11-14 09:49:09 +0100117 self._in_file = "pg%u_in.pcap" % self.pg_index
Klement Sekera778c2762016-11-08 02:00:28 +0100118 self._in_path = self.test.tempdir + "/" + self._in_file
Klement Sekeraf62ae122016-10-11 11:47:09 +0200119 self._capture_cli = "packet-generator capture pg%u pcap %s" % (
120 self.pg_index, self.out_path)
Paul Vinciguerra44b0b072019-06-25 20:51:31 -0400121 self._cap_name = "pcap%u-sw_if_index-%s" % (
122 self.pg_index, self.sw_if_index)
Klement Sekerada505f62017-01-04 12:58:53 +0100123 self._input_cli = \
124 "packet-generator new pcap %s source pg%u name %s" % (
125 self.in_path, self.pg_index, self.cap_name)
Alexandre Poirriera618e202019-05-07 10:43:41 +0200126 self._nb_replays = None
Klement Sekeraf62ae122016-10-11 11:47:09 +0200127
Paul Vinciguerra4b58a862019-05-28 15:40:47 -0400128 def _rename_previous_capture_file(self, path, counter, file):
129 # if a file from a previous capture exists, rename it.
130 try:
131 if os.path.isfile(path):
132 name = "%s/history.[timestamp:%f].[%s-counter:%04d].%s" % \
133 (self.test.tempdir,
134 time.time(),
135 self.name,
136 counter,
137 file)
138 self.test.logger.debug("Renaming %s->%s" %
139 (path, name))
140 os.rename(path, name)
141 except OSError:
142 self.test.logger.debug("OSError: Could not rename %s %s" %
143 (path, file))
144
Klement Sekerada505f62017-01-04 12:58:53 +0100145 def enable_capture(self):
Alexandre Poirriera618e202019-05-07 10:43:41 +0200146 """ Enable capture on this packet-generator interface
147 of at most n packets.
148 If n < 0, this is no limit
149 """
Andrew Yourtchenkocb265c62019-07-25 10:03:51 +0000150 # disable the capture to flush the capture
151 self.disable_capture()
Paul Vinciguerra4b58a862019-05-28 15:40:47 -0400152 self._rename_previous_capture_file(self.out_path,
153 self.out_history_counter,
154 self._out_file)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200155 # FIXME this should be an API, but no such exists atm
156 self.test.vapi.cli(self.capture_cli)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200157 self._pcap_reader = None
Klement Sekeraf62ae122016-10-11 11:47:09 +0200158
Alexandre Poirriera618e202019-05-07 10:43:41 +0200159 def disable_capture(self):
160 self.test.vapi.cli("%s disable" % self.capture_cli)
161
Klement Sekera4ecbf102019-07-31 13:14:16 +0000162 def add_stream(self, pkts, nb_replays=None, worker=None):
Klement Sekeraf62ae122016-10-11 11:47:09 +0200163 """
164 Add a stream of packets to this packet-generator
165
166 :param pkts: iterable packets
167
168 """
Klement Sekera4ecbf102019-07-31 13:14:16 +0000169 self._worker = worker
Alexandre Poirriera618e202019-05-07 10:43:41 +0200170 self._nb_replays = nb_replays
Paul Vinciguerra4b58a862019-05-28 15:40:47 -0400171 self._rename_previous_capture_file(self.in_path,
172 self.in_history_counter,
173 self._in_file)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200174 wrpcap(self.in_path, pkts)
Klement Sekera9225dee2016-12-12 08:36:58 +0100175 self.test.register_capture(self.cap_name)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200176 # FIXME this should be an API, but no such exists atm
177 self.test.vapi.cli(self.input_cli)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200178
Klement Sekera97f6edc2017-01-12 07:17:01 +0100179 def generate_debug_aid(self, kind):
180 """ Create a hardlink to the out file with a counter and a file
181 containing stack trace to ease debugging in case of multiple capture
182 files present. """
183 self.test.logger.debug("Generating debug aid for %s on %s" %
184 (kind, self._name))
185 link_path, stack_path = ["%s/debug_%s_%s_%s.%s" %
186 (self.test.tempdir, self._name,
187 self._out_assert_counter, kind, suffix)
188 for suffix in ["pcap", "stack"]
189 ]
190 os.link(self.out_path, link_path)
191 with open(stack_path, "w") as f:
192 f.writelines(format_stack())
193 self._out_assert_counter += 1
194
Klement Sekeradab231a2016-12-21 08:50:14 +0100195 def _get_capture(self, timeout, filter_out_fn=is_ipv6_misc):
196 """ Helper method to get capture and filter it """
Klement Sekeraf62ae122016-10-11 11:47:09 +0200197 try:
Klement Sekeradab231a2016-12-21 08:50:14 +0100198 if not self.wait_for_capture_file(timeout):
199 return None
Klement Sekeraf62ae122016-10-11 11:47:09 +0200200 output = rdpcap(self.out_path)
Klement Sekera65cc8c02016-12-18 15:49:54 +0100201 self.test.logger.debug("Capture has %s packets" % len(output.res))
Klement Sekeradab231a2016-12-21 08:50:14 +0100202 except:
Jane546d3b2016-12-08 13:10:03 +0100203 self.test.logger.debug("Exception in scapy.rdpcap (%s): %s" %
Klement Sekeradab231a2016-12-21 08:50:14 +0100204 (self.out_path, format_exc()))
205 return None
Klement Sekera65cc8c02016-12-18 15:49:54 +0100206 before = len(output.res)
Klement Sekeradab231a2016-12-21 08:50:14 +0100207 if filter_out_fn:
208 output.res = [p for p in output.res if not filter_out_fn(p)]
Klement Sekera97f6edc2017-01-12 07:17:01 +0100209 removed = before - len(output.res)
Klement Sekera65cc8c02016-12-18 15:49:54 +0100210 if removed:
211 self.test.logger.debug(
212 "Filtered out %s packets from capture (returning %s)" %
213 (removed, len(output.res)))
Klement Sekeraf62ae122016-10-11 11:47:09 +0200214 return output
Matej Klotton0178d522016-11-04 11:11:44 +0100215
Klement Sekeradab231a2016-12-21 08:50:14 +0100216 def get_capture(self, expected_count=None, remark=None, timeout=1,
217 filter_out_fn=is_ipv6_misc):
218 """ Get captured packets
219
220 :param expected_count: expected number of packets to capture, if None,
221 then self.test.packet_count_for_dst_pg_idx is
222 used to lookup the expected count
223 :param remark: remark printed into debug logs
224 :param timeout: how long to wait for packets
225 :param filter_out_fn: filter applied to each packet, packets for which
226 the filter returns True are removed from capture
227 :returns: iterable packets
228 """
229 remaining_time = timeout
230 capture = None
231 name = self.name if remark is None else "%s (%s)" % (self.name, remark)
232 based_on = "based on provided argument"
233 if expected_count is None:
234 expected_count = \
235 self.test.get_packet_count_for_if_idx(self.sw_if_index)
236 based_on = "based on stored packet_infos"
Klement Sekerac86fa022017-01-02 09:03:47 +0100237 if expected_count == 0:
238 raise Exception(
Klement Sekerada505f62017-01-04 12:58:53 +0100239 "Internal error, expected packet count for %s is 0!" %
240 name)
Jane546d3b2016-12-08 13:10:03 +0100241 self.test.logger.debug("Expecting to capture %s (%s) packets on %s" % (
Klement Sekeradab231a2016-12-21 08:50:14 +0100242 expected_count, based_on, name))
Klement Sekeradab231a2016-12-21 08:50:14 +0100243 while remaining_time > 0:
244 before = time.time()
245 capture = self._get_capture(remaining_time, filter_out_fn)
246 elapsed_time = time.time() - before
247 if capture:
248 if len(capture.res) == expected_count:
249 # bingo, got the packets we expected
250 return capture
Jan Gelety057bb8c2016-12-20 17:32:45 +0100251 elif len(capture.res) > expected_count:
252 self.test.logger.error(
253 ppc("Unexpected packets captured:", capture))
254 break
Klement Sekera97f6edc2017-01-12 07:17:01 +0100255 else:
256 self.test.logger.debug("Partial capture containing %s "
257 "packets doesn't match expected "
258 "count %s (yet?)" %
259 (len(capture.res), expected_count))
260 elif expected_count == 0:
261 # bingo, got None as we expected - return empty capture
262 return PacketList()
Klement Sekeradab231a2016-12-21 08:50:14 +0100263 remaining_time -= elapsed_time
264 if capture:
Klement Sekera97f6edc2017-01-12 07:17:01 +0100265 self.generate_debug_aid("count-mismatch")
Klement Sekeradab231a2016-12-21 08:50:14 +0100266 raise Exception("Captured packets mismatch, captured %s packets, "
267 "expected %s packets on %s" %
268 (len(capture.res), expected_count, name))
269 else:
270 raise Exception("No packets captured on %s" % name)
271
272 def assert_nothing_captured(self, remark=None, filter_out_fn=is_ipv6_misc):
273 """ Assert that nothing unfiltered was captured on interface
274
275 :param remark: remark printed into debug logs
276 :param filter_out_fn: filter applied to each packet, packets for which
277 the filter returns True are removed from capture
278 """
Klement Sekera9225dee2016-12-12 08:36:58 +0100279 if os.path.isfile(self.out_path):
280 try:
Klement Sekeradab231a2016-12-21 08:50:14 +0100281 capture = self.get_capture(
282 0, remark=remark, filter_out_fn=filter_out_fn)
Klement Sekera97f6edc2017-01-12 07:17:01 +0100283 if not capture or len(capture.res) == 0:
Jane546d3b2016-12-08 13:10:03 +0100284 # junk filtered out, we're good
285 return
Klement Sekera9225dee2016-12-12 08:36:58 +0100286 except:
287 pass
Klement Sekera97f6edc2017-01-12 07:17:01 +0100288 self.generate_debug_aid("empty-assert")
Klement Sekera9225dee2016-12-12 08:36:58 +0100289 if remark:
290 raise AssertionError(
Jane546d3b2016-12-08 13:10:03 +0100291 "Non-empty capture file present for interface %s (%s)" %
Klement Sekera9225dee2016-12-12 08:36:58 +0100292 (self.name, remark))
293 else:
Jane546d3b2016-12-08 13:10:03 +0100294 raise AssertionError("Capture file present for interface %s" %
295 self.name)
Klement Sekera9225dee2016-12-12 08:36:58 +0100296
Andrew Yourtchenko3d36f192019-10-11 12:34:12 +0000297 def wait_for_pg_stop(self):
298 # wait till packet-generator is stopped
299 # "show packet-generator" while it is still running gives this:
300 # Name Enabled Count Parameters
301 # pcap0-sw_if_inde Yes 64 limit 64, ...
302 #
303 # also have a 5-minute timeout just in case things go terribly wrong...
304 deadline = time.time() + 300
305 while self.test.vapi.cli('show packet-generator').find("Yes") != -1:
306 self._test.sleep(0.01) # yield
307 if time.time() > deadline:
308 self.test.logger.debug("Timeout waiting for pg to stop")
309 break
310
Klement Sekera9225dee2016-12-12 08:36:58 +0100311 def wait_for_capture_file(self, timeout=1):
312 """
313 Wait until pcap capture file appears
314
315 :param timeout: How long to wait for the packet (default 1s)
316
Klement Sekeradab231a2016-12-21 08:50:14 +0100317 :returns: True/False if the file is present or appears within timeout
Klement Sekera9225dee2016-12-12 08:36:58 +0100318 """
Andrew Yourtchenko3d36f192019-10-11 12:34:12 +0000319 self.wait_for_pg_stop()
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100320 deadline = time.time() + timeout
Klement Sekera9225dee2016-12-12 08:36:58 +0100321 if not os.path.isfile(self.out_path):
Klement Sekeradab231a2016-12-21 08:50:14 +0100322 self.test.logger.debug("Waiting for capture file %s to appear, "
323 "timeout is %ss" % (self.out_path, timeout))
Klement Sekera9225dee2016-12-12 08:36:58 +0100324 else:
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100325 self.test.logger.debug("Capture file %s already exists" %
326 self.out_path)
Klement Sekeradab231a2016-12-21 08:50:14 +0100327 return True
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100328 while time.time() < deadline:
Klement Sekera9225dee2016-12-12 08:36:58 +0100329 if os.path.isfile(self.out_path):
330 break
Paul Vinciguerra0f6602c2019-03-10 09:10:54 -0700331 self._test.sleep(0) # yield
Klement Sekera9225dee2016-12-12 08:36:58 +0100332 if os.path.isfile(self.out_path):
333 self.test.logger.debug("Capture file appeared after %fs" %
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100334 (time.time() - (deadline - timeout)))
Klement Sekera9225dee2016-12-12 08:36:58 +0100335 else:
336 self.test.logger.debug("Timeout - capture file still nowhere")
Klement Sekeradab231a2016-12-21 08:50:14 +0100337 return False
338 return True
Klement Sekera9225dee2016-12-12 08:36:58 +0100339
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100340 def verify_enough_packet_data_in_pcap(self):
Klement Sekerab91017a2017-02-09 06:04:36 +0100341 """
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100342 Check if enough data is available in file handled by internal pcap
343 reader so that a whole packet can be read.
Klement Sekerab91017a2017-02-09 06:04:36 +0100344
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100345 :returns: True if enough data present, else False
Klement Sekerab91017a2017-02-09 06:04:36 +0100346 """
347 orig_pos = self._pcap_reader.f.tell() # save file position
348 enough_data = False
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100349 # read packet header from pcap
350 packet_header_size = 16
351 caplen = None
352 end_pos = None
353 hdr = self._pcap_reader.f.read(packet_header_size)
354 if len(hdr) == packet_header_size:
355 # parse the capture length - caplen
Klement Sekerab91017a2017-02-09 06:04:36 +0100356 sec, usec, caplen, wirelen = struct.unpack(
357 self._pcap_reader.endian + "IIII", hdr)
358 self._pcap_reader.f.seek(0, 2) # seek to end of file
359 end_pos = self._pcap_reader.f.tell() # get position at end
360 if end_pos >= orig_pos + len(hdr) + caplen:
361 enough_data = True # yay, we have enough data
Klement Sekerab91017a2017-02-09 06:04:36 +0100362 self._pcap_reader.f.seek(orig_pos, 0) # restore original position
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100363 return enough_data
Klement Sekerab91017a2017-02-09 06:04:36 +0100364
Klement Sekeradab231a2016-12-21 08:50:14 +0100365 def wait_for_packet(self, timeout, filter_out_fn=is_ipv6_misc):
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200366 """
367 Wait for next packet captured with a timeout
368
369 :param timeout: How long to wait for the packet
370
371 :returns: Captured packet if no packet arrived within timeout
372 :raises Exception: if no packet arrives within timeout
373 """
Klement Sekeradab231a2016-12-21 08:50:14 +0100374 deadline = time.time() + timeout
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200375 if self._pcap_reader is None:
Klement Sekeradab231a2016-12-21 08:50:14 +0100376 if not self.wait_for_capture_file(timeout):
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100377 raise CaptureTimeoutError("Capture file %s did not appear "
378 "within timeout" % self.out_path)
Klement Sekeradab231a2016-12-21 08:50:14 +0100379 while time.time() < deadline:
380 try:
381 self._pcap_reader = PcapReader(self.out_path)
382 break
383 except:
Klement Sekerada505f62017-01-04 12:58:53 +0100384 self.test.logger.debug(
Klement Sekera97f6edc2017-01-12 07:17:01 +0100385 "Exception in scapy.PcapReader(%s): %s" %
Klement Sekerada505f62017-01-04 12:58:53 +0100386 (self.out_path, format_exc()))
Klement Sekeradab231a2016-12-21 08:50:14 +0100387 if not self._pcap_reader:
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100388 raise CaptureTimeoutError("Capture file %s did not appear within "
389 "timeout" % self.out_path)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200390
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100391 poll = False
392 if timeout > 0:
393 self.test.logger.debug("Waiting for packet")
394 else:
395 poll = True
396 self.test.logger.debug("Polling for packet")
397 while time.time() < deadline or poll:
398 if not self.verify_enough_packet_data_in_pcap():
Paul Vinciguerra0f6602c2019-03-10 09:10:54 -0700399 self._test.sleep(0) # yield
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100400 poll = False
401 continue
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200402 p = self._pcap_reader.recv()
403 if p is not None:
Klement Sekeradab231a2016-12-21 08:50:14 +0100404 if filter_out_fn is not None and filter_out_fn(p):
405 self.test.logger.debug(
406 "Packet received after %ss was filtered out" %
407 (time.time() - (deadline - timeout)))
408 else:
Klement Sekerada505f62017-01-04 12:58:53 +0100409 self.test.logger.debug(
410 "Packet received after %fs" %
411 (time.time() - (deadline - timeout)))
Klement Sekeradab231a2016-12-21 08:50:14 +0100412 return p
Paul Vinciguerra0f6602c2019-03-10 09:10:54 -0700413 self._test.sleep(0) # yield
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100414 poll = False
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200415 self.test.logger.debug("Timeout - no packets received")
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100416 raise CaptureTimeoutError("Packet didn't arrive within timeout")
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200417
Matej Klotton0178d522016-11-04 11:11:44 +0100418 def create_arp_req(self):
419 """Create ARP request applicable for this interface"""
420 return (Ether(dst="ff:ff:ff:ff:ff:ff", src=self.remote_mac) /
421 ARP(op=ARP.who_has, pdst=self.local_ip4,
422 psrc=self.remote_ip4, hwsrc=self.remote_mac))
423
424 def create_ndp_req(self):
425 """Create NDP - NS applicable for this interface"""
Neale Ranns465a1a32017-01-07 10:04:09 -0800426 nsma = in6_getnsma(inet_pton(socket.AF_INET6, self.local_ip6))
427 d = inet_ntop(socket.AF_INET6, nsma)
428
429 return (Ether(dst=in6_getnsmac(nsma)) /
430 IPv6(dst=d, src=self.remote_ip6) /
Klement Sekera74dcdbf2016-11-14 09:49:09 +0100431 ICMPv6ND_NS(tgt=self.local_ip6) /
432 ICMPv6NDOptSrcLLAddr(lladdr=self.remote_mac))
Matej Klotton0178d522016-11-04 11:11:44 +0100433
434 def resolve_arp(self, pg_interface=None):
435 """Resolve ARP using provided packet-generator interface
436
437 :param pg_interface: interface used to resolve, if None then this
438 interface is used
439
440 """
441 if pg_interface is None:
442 pg_interface = self
Klement Sekera7bb873a2016-11-18 07:38:42 +0100443 self.test.logger.info("Sending ARP request for %s on port %s" %
444 (self.local_ip4, pg_interface.name))
Matej Klotton0178d522016-11-04 11:11:44 +0100445 arp_req = self.create_arp_req()
446 pg_interface.add_stream(arp_req)
447 pg_interface.enable_capture()
448 self.test.pg_start()
Klement Sekera7bb873a2016-11-18 07:38:42 +0100449 self.test.logger.info(self.test.vapi.cli("show trace"))
Klement Sekera9225dee2016-12-12 08:36:58 +0100450 try:
Klement Sekeradab231a2016-12-21 08:50:14 +0100451 captured_packet = pg_interface.wait_for_packet(1)
Klement Sekera9225dee2016-12-12 08:36:58 +0100452 except:
453 self.test.logger.info("No ARP received on port %s" %
454 pg_interface.name)
Matej Klotton0178d522016-11-04 11:11:44 +0100455 return
Klement Sekeradab231a2016-12-21 08:50:14 +0100456 arp_reply = captured_packet.copy() # keep original for exception
Matej Klotton0178d522016-11-04 11:11:44 +0100457 try:
458 if arp_reply[ARP].op == ARP.is_at:
Klement Sekera7bb873a2016-11-18 07:38:42 +0100459 self.test.logger.info("VPP %s MAC address is %s " %
460 (self.name, arp_reply[ARP].hwsrc))
Matej Klotton0178d522016-11-04 11:11:44 +0100461 self._local_mac = arp_reply[ARP].hwsrc
462 else:
Klement Sekeradab231a2016-12-21 08:50:14 +0100463 self.test.logger.info("No ARP received on port %s" %
464 pg_interface.name)
Matej Klotton0178d522016-11-04 11:11:44 +0100465 except:
Klement Sekera7bb873a2016-11-18 07:38:42 +0100466 self.test.logger.error(
Klement Sekeradab231a2016-12-21 08:50:14 +0100467 ppp("Unexpected response to ARP request:", captured_packet))
Matej Klotton0178d522016-11-04 11:11:44 +0100468 raise
469
Klement Sekeradab231a2016-12-21 08:50:14 +0100470 def resolve_ndp(self, pg_interface=None, timeout=1):
Matej Klotton0178d522016-11-04 11:11:44 +0100471 """Resolve NDP using provided packet-generator interface
472
473 :param pg_interface: interface used to resolve, if None then this
474 interface is used
Klement Sekeradab231a2016-12-21 08:50:14 +0100475 :param timeout: how long to wait for response before giving up
Matej Klotton0178d522016-11-04 11:11:44 +0100476
477 """
478 if pg_interface is None:
479 pg_interface = self
Klement Sekera7bb873a2016-11-18 07:38:42 +0100480 self.test.logger.info("Sending NDP request for %s on port %s" %
481 (self.local_ip6, pg_interface.name))
Matej Klotton0178d522016-11-04 11:11:44 +0100482 ndp_req = self.create_ndp_req()
483 pg_interface.add_stream(ndp_req)
484 pg_interface.enable_capture()
485 self.test.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +0100486 now = time.time()
487 deadline = now + timeout
Neale Ranns82a06a92016-12-08 20:05:33 +0000488 # Enabling IPv6 on an interface can generate more than the
489 # ND reply we are looking for (namely MLD). So loop through
490 # the replies to look for want we want.
Klement Sekeradab231a2016-12-21 08:50:14 +0100491 while now < deadline:
492 try:
493 captured_packet = pg_interface.wait_for_packet(
494 deadline - now, filter_out_fn=None)
495 except:
Klement Sekerada505f62017-01-04 12:58:53 +0100496 self.test.logger.error(
497 "Timeout while waiting for NDP response")
Klement Sekeradab231a2016-12-21 08:50:14 +0100498 raise
499 ndp_reply = captured_packet.copy() # keep original for exception
Neale Ranns82a06a92016-12-08 20:05:33 +0000500 try:
501 ndp_na = ndp_reply[ICMPv6ND_NA]
502 opt = ndp_na[ICMPv6NDOptDstLLAddr]
503 self.test.logger.info("VPP %s MAC address is %s " %
504 (self.name, opt.lladdr))
505 self._local_mac = opt.lladdr
Klement Sekeradab231a2016-12-21 08:50:14 +0100506 self.test.logger.debug(self.test.vapi.cli("show trace"))
507 # we now have the MAC we've been after
508 return
Neale Ranns82a06a92016-12-08 20:05:33 +0000509 except:
510 self.test.logger.info(
Klement Sekerada505f62017-01-04 12:58:53 +0100511 ppp("Unexpected response to NDP request:",
512 captured_packet))
Klement Sekeradab231a2016-12-21 08:50:14 +0100513 now = time.time()
514
515 self.test.logger.debug(self.test.vapi.cli("show trace"))
516 raise Exception("Timeout while waiting for NDP response")