blob: e6dae66feecfd3818d304f1c99841a37d164928b [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 Sekeraf62ae122016-10-11 11:47:09 +020085 return self._input_cli
86
Klement Sekera778c2762016-11-08 02:00:28 +010087 @property
88 def in_history_counter(self):
89 """Self-incrementing counter used when renaming old pcap files"""
90 v = self._in_history_counter
91 self._in_history_counter += 1
92 return v
93
94 @property
95 def out_history_counter(self):
96 """Self-incrementing counter used when renaming old pcap files"""
97 v = self._out_history_counter
98 self._out_history_counter += 1
99 return v
100
Mohsin Kazmi22e9cfd2019-07-23 11:54:48 +0200101 def __init__(self, test, pg_index, gso, gso_size):
Matej Klottonc5bf07f2016-11-23 15:27:17 +0100102 """ Create VPP packet-generator interface """
Ole Troane0d2bd62018-06-22 22:36:46 +0200103 super(VppPGInterface, self).__init__(test)
Klement Sekeraa98346f2018-05-16 10:52:45 +0200104
Mohsin Kazmi22e9cfd2019-07-23 11:54:48 +0200105 r = test.vapi.pg_create_interface(pg_index, gso, gso_size)
Klement Sekera31da2e32018-06-24 22:49:55 +0200106 self.set_sw_if_index(r.sw_if_index)
107
Matej Klottonc5bf07f2016-11-23 15:27:17 +0100108 self._in_history_counter = 0
109 self._out_history_counter = 0
Klement Sekera97f6edc2017-01-12 07:17:01 +0100110 self._out_assert_counter = 0
Matej Klottonc5bf07f2016-11-23 15:27:17 +0100111 self._pg_index = pg_index
Mohsin Kazmi22e9cfd2019-07-23 11:54:48 +0200112 self._gso_enabled = gso
113 self._gso_size = gso_size
Klement Sekera74dcdbf2016-11-14 09:49:09 +0100114 self._out_file = "pg%u_out.pcap" % self.pg_index
Klement Sekera778c2762016-11-08 02:00:28 +0100115 self._out_path = self.test.tempdir + "/" + self._out_file
Klement Sekera74dcdbf2016-11-14 09:49:09 +0100116 self._in_file = "pg%u_in.pcap" % self.pg_index
Klement Sekera778c2762016-11-08 02:00:28 +0100117 self._in_path = self.test.tempdir + "/" + self._in_file
Klement Sekeraf62ae122016-10-11 11:47:09 +0200118 self._capture_cli = "packet-generator capture pg%u pcap %s" % (
119 self.pg_index, self.out_path)
Paul Vinciguerra44b0b072019-06-25 20:51:31 -0400120 self._cap_name = "pcap%u-sw_if_index-%s" % (
121 self.pg_index, self.sw_if_index)
Klement Sekerada505f62017-01-04 12:58:53 +0100122 self._input_cli = \
123 "packet-generator new pcap %s source pg%u name %s" % (
124 self.in_path, self.pg_index, self.cap_name)
Alexandre Poirriera618e202019-05-07 10:43:41 +0200125 self._nb_replays = None
Klement Sekeraf62ae122016-10-11 11:47:09 +0200126
Paul Vinciguerra4b58a862019-05-28 15:40:47 -0400127 def _rename_previous_capture_file(self, path, counter, file):
128 # if a file from a previous capture exists, rename it.
129 try:
130 if os.path.isfile(path):
131 name = "%s/history.[timestamp:%f].[%s-counter:%04d].%s" % \
132 (self.test.tempdir,
133 time.time(),
134 self.name,
135 counter,
136 file)
137 self.test.logger.debug("Renaming %s->%s" %
138 (path, name))
139 os.rename(path, name)
140 except OSError:
141 self.test.logger.debug("OSError: Could not rename %s %s" %
142 (path, file))
143
Klement Sekerada505f62017-01-04 12:58:53 +0100144 def enable_capture(self):
Alexandre Poirriera618e202019-05-07 10:43:41 +0200145 """ Enable capture on this packet-generator interface
146 of at most n packets.
147 If n < 0, this is no limit
148 """
Paul Vinciguerra4b58a862019-05-28 15:40:47 -0400149
150 self._rename_previous_capture_file(self.out_path,
151 self.out_history_counter,
152 self._out_file)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200153 # FIXME this should be an API, but no such exists atm
154 self.test.vapi.cli(self.capture_cli)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200155 self._pcap_reader = None
Klement Sekeraf62ae122016-10-11 11:47:09 +0200156
Alexandre Poirriera618e202019-05-07 10:43:41 +0200157 def disable_capture(self):
158 self.test.vapi.cli("%s disable" % self.capture_cli)
159
160 def add_stream(self, pkts, nb_replays=None):
Klement Sekeraf62ae122016-10-11 11:47:09 +0200161 """
162 Add a stream of packets to this packet-generator
163
164 :param pkts: iterable packets
165
166 """
Alexandre Poirriera618e202019-05-07 10:43:41 +0200167 self._nb_replays = nb_replays
Paul Vinciguerra4b58a862019-05-28 15:40:47 -0400168 self._rename_previous_capture_file(self.in_path,
169 self.in_history_counter,
170 self._in_file)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200171 wrpcap(self.in_path, pkts)
Klement Sekera9225dee2016-12-12 08:36:58 +0100172 self.test.register_capture(self.cap_name)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200173 # FIXME this should be an API, but no such exists atm
174 self.test.vapi.cli(self.input_cli)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200175
Klement Sekera97f6edc2017-01-12 07:17:01 +0100176 def generate_debug_aid(self, kind):
177 """ Create a hardlink to the out file with a counter and a file
178 containing stack trace to ease debugging in case of multiple capture
179 files present. """
180 self.test.logger.debug("Generating debug aid for %s on %s" %
181 (kind, self._name))
182 link_path, stack_path = ["%s/debug_%s_%s_%s.%s" %
183 (self.test.tempdir, self._name,
184 self._out_assert_counter, kind, suffix)
185 for suffix in ["pcap", "stack"]
186 ]
187 os.link(self.out_path, link_path)
188 with open(stack_path, "w") as f:
189 f.writelines(format_stack())
190 self._out_assert_counter += 1
191
Klement Sekeradab231a2016-12-21 08:50:14 +0100192 def _get_capture(self, timeout, filter_out_fn=is_ipv6_misc):
193 """ Helper method to get capture and filter it """
Klement Sekeraf62ae122016-10-11 11:47:09 +0200194 try:
Klement Sekeradab231a2016-12-21 08:50:14 +0100195 if not self.wait_for_capture_file(timeout):
196 return None
Klement Sekeraf62ae122016-10-11 11:47:09 +0200197 output = rdpcap(self.out_path)
Klement Sekera65cc8c02016-12-18 15:49:54 +0100198 self.test.logger.debug("Capture has %s packets" % len(output.res))
Klement Sekeradab231a2016-12-21 08:50:14 +0100199 except:
Jane546d3b2016-12-08 13:10:03 +0100200 self.test.logger.debug("Exception in scapy.rdpcap (%s): %s" %
Klement Sekeradab231a2016-12-21 08:50:14 +0100201 (self.out_path, format_exc()))
202 return None
Klement Sekera65cc8c02016-12-18 15:49:54 +0100203 before = len(output.res)
Klement Sekeradab231a2016-12-21 08:50:14 +0100204 if filter_out_fn:
205 output.res = [p for p in output.res if not filter_out_fn(p)]
Klement Sekera97f6edc2017-01-12 07:17:01 +0100206 removed = before - len(output.res)
Klement Sekera65cc8c02016-12-18 15:49:54 +0100207 if removed:
208 self.test.logger.debug(
209 "Filtered out %s packets from capture (returning %s)" %
210 (removed, len(output.res)))
Klement Sekeraf62ae122016-10-11 11:47:09 +0200211 return output
Matej Klotton0178d522016-11-04 11:11:44 +0100212
Klement Sekeradab231a2016-12-21 08:50:14 +0100213 def get_capture(self, expected_count=None, remark=None, timeout=1,
214 filter_out_fn=is_ipv6_misc):
215 """ Get captured packets
216
217 :param expected_count: expected number of packets to capture, if None,
218 then self.test.packet_count_for_dst_pg_idx is
219 used to lookup the expected count
220 :param remark: remark printed into debug logs
221 :param timeout: how long to wait for packets
222 :param filter_out_fn: filter applied to each packet, packets for which
223 the filter returns True are removed from capture
224 :returns: iterable packets
225 """
226 remaining_time = timeout
227 capture = None
228 name = self.name if remark is None else "%s (%s)" % (self.name, remark)
229 based_on = "based on provided argument"
230 if expected_count is None:
231 expected_count = \
232 self.test.get_packet_count_for_if_idx(self.sw_if_index)
233 based_on = "based on stored packet_infos"
Klement Sekerac86fa022017-01-02 09:03:47 +0100234 if expected_count == 0:
235 raise Exception(
Klement Sekerada505f62017-01-04 12:58:53 +0100236 "Internal error, expected packet count for %s is 0!" %
237 name)
Jane546d3b2016-12-08 13:10:03 +0100238 self.test.logger.debug("Expecting to capture %s (%s) packets on %s" % (
Klement Sekeradab231a2016-12-21 08:50:14 +0100239 expected_count, based_on, name))
Klement Sekeradab231a2016-12-21 08:50:14 +0100240 while remaining_time > 0:
241 before = time.time()
242 capture = self._get_capture(remaining_time, filter_out_fn)
243 elapsed_time = time.time() - before
244 if capture:
245 if len(capture.res) == expected_count:
246 # bingo, got the packets we expected
247 return capture
Jan Gelety057bb8c2016-12-20 17:32:45 +0100248 elif len(capture.res) > expected_count:
249 self.test.logger.error(
250 ppc("Unexpected packets captured:", capture))
251 break
Klement Sekera97f6edc2017-01-12 07:17:01 +0100252 else:
253 self.test.logger.debug("Partial capture containing %s "
254 "packets doesn't match expected "
255 "count %s (yet?)" %
256 (len(capture.res), expected_count))
257 elif expected_count == 0:
258 # bingo, got None as we expected - return empty capture
259 return PacketList()
Klement Sekeradab231a2016-12-21 08:50:14 +0100260 remaining_time -= elapsed_time
261 if capture:
Klement Sekera97f6edc2017-01-12 07:17:01 +0100262 self.generate_debug_aid("count-mismatch")
Klement Sekeradab231a2016-12-21 08:50:14 +0100263 raise Exception("Captured packets mismatch, captured %s packets, "
264 "expected %s packets on %s" %
265 (len(capture.res), expected_count, name))
266 else:
267 raise Exception("No packets captured on %s" % name)
268
269 def assert_nothing_captured(self, remark=None, filter_out_fn=is_ipv6_misc):
270 """ Assert that nothing unfiltered was captured on interface
271
272 :param remark: remark printed into debug logs
273 :param filter_out_fn: filter applied to each packet, packets for which
274 the filter returns True are removed from capture
275 """
Klement Sekera9225dee2016-12-12 08:36:58 +0100276 if os.path.isfile(self.out_path):
277 try:
Klement Sekeradab231a2016-12-21 08:50:14 +0100278 capture = self.get_capture(
279 0, remark=remark, filter_out_fn=filter_out_fn)
Klement Sekera97f6edc2017-01-12 07:17:01 +0100280 if not capture or len(capture.res) == 0:
Jane546d3b2016-12-08 13:10:03 +0100281 # junk filtered out, we're good
282 return
Klement Sekera9225dee2016-12-12 08:36:58 +0100283 except:
284 pass
Klement Sekera97f6edc2017-01-12 07:17:01 +0100285 self.generate_debug_aid("empty-assert")
Klement Sekera9225dee2016-12-12 08:36:58 +0100286 if remark:
287 raise AssertionError(
Jane546d3b2016-12-08 13:10:03 +0100288 "Non-empty capture file present for interface %s (%s)" %
Klement Sekera9225dee2016-12-12 08:36:58 +0100289 (self.name, remark))
290 else:
Jane546d3b2016-12-08 13:10:03 +0100291 raise AssertionError("Capture file present for interface %s" %
292 self.name)
Klement Sekera9225dee2016-12-12 08:36:58 +0100293
294 def wait_for_capture_file(self, timeout=1):
295 """
296 Wait until pcap capture file appears
297
298 :param timeout: How long to wait for the packet (default 1s)
299
Klement Sekeradab231a2016-12-21 08:50:14 +0100300 :returns: True/False if the file is present or appears within timeout
Klement Sekera9225dee2016-12-12 08:36:58 +0100301 """
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100302 deadline = time.time() + timeout
Klement Sekera9225dee2016-12-12 08:36:58 +0100303 if not os.path.isfile(self.out_path):
Klement Sekeradab231a2016-12-21 08:50:14 +0100304 self.test.logger.debug("Waiting for capture file %s to appear, "
305 "timeout is %ss" % (self.out_path, timeout))
Klement Sekera9225dee2016-12-12 08:36:58 +0100306 else:
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100307 self.test.logger.debug("Capture file %s already exists" %
308 self.out_path)
Klement Sekeradab231a2016-12-21 08:50:14 +0100309 return True
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100310 while time.time() < deadline:
Klement Sekera9225dee2016-12-12 08:36:58 +0100311 if os.path.isfile(self.out_path):
312 break
Paul Vinciguerra0f6602c2019-03-10 09:10:54 -0700313 self._test.sleep(0) # yield
Klement Sekera9225dee2016-12-12 08:36:58 +0100314 if os.path.isfile(self.out_path):
315 self.test.logger.debug("Capture file appeared after %fs" %
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100316 (time.time() - (deadline - timeout)))
Klement Sekera9225dee2016-12-12 08:36:58 +0100317 else:
318 self.test.logger.debug("Timeout - capture file still nowhere")
Klement Sekeradab231a2016-12-21 08:50:14 +0100319 return False
320 return True
Klement Sekera9225dee2016-12-12 08:36:58 +0100321
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100322 def verify_enough_packet_data_in_pcap(self):
Klement Sekerab91017a2017-02-09 06:04:36 +0100323 """
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100324 Check if enough data is available in file handled by internal pcap
325 reader so that a whole packet can be read.
Klement Sekerab91017a2017-02-09 06:04:36 +0100326
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100327 :returns: True if enough data present, else False
Klement Sekerab91017a2017-02-09 06:04:36 +0100328 """
329 orig_pos = self._pcap_reader.f.tell() # save file position
330 enough_data = False
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100331 # read packet header from pcap
332 packet_header_size = 16
333 caplen = None
334 end_pos = None
335 hdr = self._pcap_reader.f.read(packet_header_size)
336 if len(hdr) == packet_header_size:
337 # parse the capture length - caplen
Klement Sekerab91017a2017-02-09 06:04:36 +0100338 sec, usec, caplen, wirelen = struct.unpack(
339 self._pcap_reader.endian + "IIII", hdr)
340 self._pcap_reader.f.seek(0, 2) # seek to end of file
341 end_pos = self._pcap_reader.f.tell() # get position at end
342 if end_pos >= orig_pos + len(hdr) + caplen:
343 enough_data = True # yay, we have enough data
Klement Sekerab91017a2017-02-09 06:04:36 +0100344 self._pcap_reader.f.seek(orig_pos, 0) # restore original position
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100345 return enough_data
Klement Sekerab91017a2017-02-09 06:04:36 +0100346
Klement Sekeradab231a2016-12-21 08:50:14 +0100347 def wait_for_packet(self, timeout, filter_out_fn=is_ipv6_misc):
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200348 """
349 Wait for next packet captured with a timeout
350
351 :param timeout: How long to wait for the packet
352
353 :returns: Captured packet if no packet arrived within timeout
354 :raises Exception: if no packet arrives within timeout
355 """
Klement Sekeradab231a2016-12-21 08:50:14 +0100356 deadline = time.time() + timeout
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200357 if self._pcap_reader is None:
Klement Sekeradab231a2016-12-21 08:50:14 +0100358 if not self.wait_for_capture_file(timeout):
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100359 raise CaptureTimeoutError("Capture file %s did not appear "
360 "within timeout" % self.out_path)
Klement Sekeradab231a2016-12-21 08:50:14 +0100361 while time.time() < deadline:
362 try:
363 self._pcap_reader = PcapReader(self.out_path)
364 break
365 except:
Klement Sekerada505f62017-01-04 12:58:53 +0100366 self.test.logger.debug(
Klement Sekera97f6edc2017-01-12 07:17:01 +0100367 "Exception in scapy.PcapReader(%s): %s" %
Klement Sekerada505f62017-01-04 12:58:53 +0100368 (self.out_path, format_exc()))
Klement Sekeradab231a2016-12-21 08:50:14 +0100369 if not self._pcap_reader:
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100370 raise CaptureTimeoutError("Capture file %s did not appear within "
371 "timeout" % self.out_path)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200372
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100373 poll = False
374 if timeout > 0:
375 self.test.logger.debug("Waiting for packet")
376 else:
377 poll = True
378 self.test.logger.debug("Polling for packet")
379 while time.time() < deadline or poll:
380 if not self.verify_enough_packet_data_in_pcap():
Paul Vinciguerra0f6602c2019-03-10 09:10:54 -0700381 self._test.sleep(0) # yield
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100382 poll = False
383 continue
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200384 p = self._pcap_reader.recv()
385 if p is not None:
Klement Sekeradab231a2016-12-21 08:50:14 +0100386 if filter_out_fn is not None and filter_out_fn(p):
387 self.test.logger.debug(
388 "Packet received after %ss was filtered out" %
389 (time.time() - (deadline - timeout)))
390 else:
Klement Sekerada505f62017-01-04 12:58:53 +0100391 self.test.logger.debug(
392 "Packet received after %fs" %
393 (time.time() - (deadline - timeout)))
Klement Sekeradab231a2016-12-21 08:50:14 +0100394 return p
Paul Vinciguerra0f6602c2019-03-10 09:10:54 -0700395 self._test.sleep(0) # yield
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100396 poll = False
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200397 self.test.logger.debug("Timeout - no packets received")
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100398 raise CaptureTimeoutError("Packet didn't arrive within timeout")
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200399
Matej Klotton0178d522016-11-04 11:11:44 +0100400 def create_arp_req(self):
401 """Create ARP request applicable for this interface"""
402 return (Ether(dst="ff:ff:ff:ff:ff:ff", src=self.remote_mac) /
403 ARP(op=ARP.who_has, pdst=self.local_ip4,
404 psrc=self.remote_ip4, hwsrc=self.remote_mac))
405
406 def create_ndp_req(self):
407 """Create NDP - NS applicable for this interface"""
Neale Ranns465a1a32017-01-07 10:04:09 -0800408 nsma = in6_getnsma(inet_pton(socket.AF_INET6, self.local_ip6))
409 d = inet_ntop(socket.AF_INET6, nsma)
410
411 return (Ether(dst=in6_getnsmac(nsma)) /
412 IPv6(dst=d, src=self.remote_ip6) /
Klement Sekera74dcdbf2016-11-14 09:49:09 +0100413 ICMPv6ND_NS(tgt=self.local_ip6) /
414 ICMPv6NDOptSrcLLAddr(lladdr=self.remote_mac))
Matej Klotton0178d522016-11-04 11:11:44 +0100415
416 def resolve_arp(self, pg_interface=None):
417 """Resolve ARP using provided packet-generator interface
418
419 :param pg_interface: interface used to resolve, if None then this
420 interface is used
421
422 """
423 if pg_interface is None:
424 pg_interface = self
Klement Sekera7bb873a2016-11-18 07:38:42 +0100425 self.test.logger.info("Sending ARP request for %s on port %s" %
426 (self.local_ip4, pg_interface.name))
Matej Klotton0178d522016-11-04 11:11:44 +0100427 arp_req = self.create_arp_req()
428 pg_interface.add_stream(arp_req)
429 pg_interface.enable_capture()
430 self.test.pg_start()
Klement Sekera7bb873a2016-11-18 07:38:42 +0100431 self.test.logger.info(self.test.vapi.cli("show trace"))
Klement Sekera9225dee2016-12-12 08:36:58 +0100432 try:
Klement Sekeradab231a2016-12-21 08:50:14 +0100433 captured_packet = pg_interface.wait_for_packet(1)
Klement Sekera9225dee2016-12-12 08:36:58 +0100434 except:
435 self.test.logger.info("No ARP received on port %s" %
436 pg_interface.name)
Matej Klotton0178d522016-11-04 11:11:44 +0100437 return
Klement Sekeradab231a2016-12-21 08:50:14 +0100438 arp_reply = captured_packet.copy() # keep original for exception
Matej Klotton0178d522016-11-04 11:11:44 +0100439 try:
440 if arp_reply[ARP].op == ARP.is_at:
Klement Sekera7bb873a2016-11-18 07:38:42 +0100441 self.test.logger.info("VPP %s MAC address is %s " %
442 (self.name, arp_reply[ARP].hwsrc))
Matej Klotton0178d522016-11-04 11:11:44 +0100443 self._local_mac = arp_reply[ARP].hwsrc
444 else:
Klement Sekeradab231a2016-12-21 08:50:14 +0100445 self.test.logger.info("No ARP received on port %s" %
446 pg_interface.name)
Matej Klotton0178d522016-11-04 11:11:44 +0100447 except:
Klement Sekera7bb873a2016-11-18 07:38:42 +0100448 self.test.logger.error(
Klement Sekeradab231a2016-12-21 08:50:14 +0100449 ppp("Unexpected response to ARP request:", captured_packet))
Matej Klotton0178d522016-11-04 11:11:44 +0100450 raise
451
Klement Sekeradab231a2016-12-21 08:50:14 +0100452 def resolve_ndp(self, pg_interface=None, timeout=1):
Matej Klotton0178d522016-11-04 11:11:44 +0100453 """Resolve NDP using provided packet-generator interface
454
455 :param pg_interface: interface used to resolve, if None then this
456 interface is used
Klement Sekeradab231a2016-12-21 08:50:14 +0100457 :param timeout: how long to wait for response before giving up
Matej Klotton0178d522016-11-04 11:11:44 +0100458
459 """
460 if pg_interface is None:
461 pg_interface = self
Klement Sekera7bb873a2016-11-18 07:38:42 +0100462 self.test.logger.info("Sending NDP request for %s on port %s" %
463 (self.local_ip6, pg_interface.name))
Matej Klotton0178d522016-11-04 11:11:44 +0100464 ndp_req = self.create_ndp_req()
465 pg_interface.add_stream(ndp_req)
466 pg_interface.enable_capture()
467 self.test.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +0100468 now = time.time()
469 deadline = now + timeout
Neale Ranns82a06a92016-12-08 20:05:33 +0000470 # Enabling IPv6 on an interface can generate more than the
471 # ND reply we are looking for (namely MLD). So loop through
472 # the replies to look for want we want.
Klement Sekeradab231a2016-12-21 08:50:14 +0100473 while now < deadline:
474 try:
475 captured_packet = pg_interface.wait_for_packet(
476 deadline - now, filter_out_fn=None)
477 except:
Klement Sekerada505f62017-01-04 12:58:53 +0100478 self.test.logger.error(
479 "Timeout while waiting for NDP response")
Klement Sekeradab231a2016-12-21 08:50:14 +0100480 raise
481 ndp_reply = captured_packet.copy() # keep original for exception
Neale Ranns82a06a92016-12-08 20:05:33 +0000482 try:
483 ndp_na = ndp_reply[ICMPv6ND_NA]
484 opt = ndp_na[ICMPv6NDOptDstLLAddr]
485 self.test.logger.info("VPP %s MAC address is %s " %
486 (self.name, opt.lladdr))
487 self._local_mac = opt.lladdr
Klement Sekeradab231a2016-12-21 08:50:14 +0100488 self.test.logger.debug(self.test.vapi.cli("show trace"))
489 # we now have the MAC we've been after
490 return
Neale Ranns82a06a92016-12-08 20:05:33 +0000491 except:
492 self.test.logger.info(
Klement Sekerada505f62017-01-04 12:58:53 +0100493 ppp("Unexpected response to NDP request:",
494 captured_packet))
Klement Sekeradab231a2016-12-21 08:50:14 +0100495 now = time.time()
496
497 self.test.logger.debug(self.test.vapi.cli("show trace"))
498 raise Exception("Timeout while waiting for NDP response")