blob: 8792e6c3767793261e2186829e96c090407d81ea [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
49 def out_path(self):
50 """pcap file path - captured packets"""
51 return self._out_path
52
53 @property
54 def in_path(self):
55 """ pcap file path - injected packets"""
56 return self._in_path
57
58 @property
59 def capture_cli(self):
60 """CLI string to start capture on this interface"""
61 return self._capture_cli
62
63 @property
64 def cap_name(self):
65 """capture name for this interface"""
66 return self._cap_name
67
68 @property
69 def input_cli(self):
70 """CLI string to load the injected packets"""
71 return self._input_cli
72
Klement Sekera778c2762016-11-08 02:00:28 +010073 @property
74 def in_history_counter(self):
75 """Self-incrementing counter used when renaming old pcap files"""
76 v = self._in_history_counter
77 self._in_history_counter += 1
78 return v
79
80 @property
81 def out_history_counter(self):
82 """Self-incrementing counter used when renaming old pcap files"""
83 v = self._out_history_counter
84 self._out_history_counter += 1
85 return v
86
Matej Klottonc5bf07f2016-11-23 15:27:17 +010087 def __init__(self, test, pg_index):
88 """ Create VPP packet-generator interface """
Ole Troane0d2bd62018-06-22 22:36:46 +020089 super(VppPGInterface, self).__init__(test)
Klement Sekeraa98346f2018-05-16 10:52:45 +020090
Klement Sekera31da2e32018-06-24 22:49:55 +020091 r = test.vapi.pg_create_interface(pg_index)
92 self.set_sw_if_index(r.sw_if_index)
93
Matej Klottonc5bf07f2016-11-23 15:27:17 +010094 self._in_history_counter = 0
95 self._out_history_counter = 0
Klement Sekera97f6edc2017-01-12 07:17:01 +010096 self._out_assert_counter = 0
Matej Klottonc5bf07f2016-11-23 15:27:17 +010097 self._pg_index = pg_index
Klement Sekera74dcdbf2016-11-14 09:49:09 +010098 self._out_file = "pg%u_out.pcap" % self.pg_index
Klement Sekera778c2762016-11-08 02:00:28 +010099 self._out_path = self.test.tempdir + "/" + self._out_file
Klement Sekera74dcdbf2016-11-14 09:49:09 +0100100 self._in_file = "pg%u_in.pcap" % self.pg_index
Klement Sekera778c2762016-11-08 02:00:28 +0100101 self._in_path = self.test.tempdir + "/" + self._in_file
Klement Sekeraf62ae122016-10-11 11:47:09 +0200102 self._capture_cli = "packet-generator capture pg%u pcap %s" % (
103 self.pg_index, self.out_path)
104 self._cap_name = "pcap%u" % self.sw_if_index
Klement Sekerada505f62017-01-04 12:58:53 +0100105 self._input_cli = \
106 "packet-generator new pcap %s source pg%u name %s" % (
107 self.in_path, self.pg_index, self.cap_name)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200108
Klement Sekerada505f62017-01-04 12:58:53 +0100109 def enable_capture(self):
110 """ Enable capture on this packet-generator interface"""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200111 try:
Klement Sekera778c2762016-11-08 02:00:28 +0100112 if os.path.isfile(self.out_path):
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100113 name = "%s/history.[timestamp:%f].[%s-counter:%04d].%s" % \
114 (self.test.tempdir,
115 time.time(),
116 self.name,
117 self.out_history_counter,
118 self._out_file)
119 self.test.logger.debug("Renaming %s->%s" %
120 (self.out_path, name))
121 os.rename(self.out_path, name)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200122 except:
123 pass
124 # FIXME this should be an API, but no such exists atm
125 self.test.vapi.cli(self.capture_cli)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200126 self._pcap_reader = None
Klement Sekeraf62ae122016-10-11 11:47:09 +0200127
128 def add_stream(self, pkts):
129 """
130 Add a stream of packets to this packet-generator
131
132 :param pkts: iterable packets
133
134 """
135 try:
Klement Sekera778c2762016-11-08 02:00:28 +0100136 if os.path.isfile(self.in_path):
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100137 name = "%s/history.[timestamp:%f].[%s-counter:%04d].%s" %\
138 (self.test.tempdir,
139 time.time(),
140 self.name,
141 self.in_history_counter,
142 self._in_file)
143 self.test.logger.debug("Renaming %s->%s" %
144 (self.in_path, name))
145 os.rename(self.in_path, name)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200146 except:
147 pass
148 wrpcap(self.in_path, pkts)
Klement Sekera9225dee2016-12-12 08:36:58 +0100149 self.test.register_capture(self.cap_name)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200150 # FIXME this should be an API, but no such exists atm
151 self.test.vapi.cli(self.input_cli)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200152
Klement Sekera97f6edc2017-01-12 07:17:01 +0100153 def generate_debug_aid(self, kind):
154 """ Create a hardlink to the out file with a counter and a file
155 containing stack trace to ease debugging in case of multiple capture
156 files present. """
157 self.test.logger.debug("Generating debug aid for %s on %s" %
158 (kind, self._name))
159 link_path, stack_path = ["%s/debug_%s_%s_%s.%s" %
160 (self.test.tempdir, self._name,
161 self._out_assert_counter, kind, suffix)
162 for suffix in ["pcap", "stack"]
163 ]
164 os.link(self.out_path, link_path)
165 with open(stack_path, "w") as f:
166 f.writelines(format_stack())
167 self._out_assert_counter += 1
168
Klement Sekeradab231a2016-12-21 08:50:14 +0100169 def _get_capture(self, timeout, filter_out_fn=is_ipv6_misc):
170 """ Helper method to get capture and filter it """
Klement Sekeraf62ae122016-10-11 11:47:09 +0200171 try:
Klement Sekeradab231a2016-12-21 08:50:14 +0100172 if not self.wait_for_capture_file(timeout):
173 return None
Klement Sekeraf62ae122016-10-11 11:47:09 +0200174 output = rdpcap(self.out_path)
Klement Sekera65cc8c02016-12-18 15:49:54 +0100175 self.test.logger.debug("Capture has %s packets" % len(output.res))
Klement Sekeradab231a2016-12-21 08:50:14 +0100176 except:
Jane546d3b2016-12-08 13:10:03 +0100177 self.test.logger.debug("Exception in scapy.rdpcap (%s): %s" %
Klement Sekeradab231a2016-12-21 08:50:14 +0100178 (self.out_path, format_exc()))
179 return None
Klement Sekera65cc8c02016-12-18 15:49:54 +0100180 before = len(output.res)
Klement Sekeradab231a2016-12-21 08:50:14 +0100181 if filter_out_fn:
182 output.res = [p for p in output.res if not filter_out_fn(p)]
Klement Sekera97f6edc2017-01-12 07:17:01 +0100183 removed = before - len(output.res)
Klement Sekera65cc8c02016-12-18 15:49:54 +0100184 if removed:
185 self.test.logger.debug(
186 "Filtered out %s packets from capture (returning %s)" %
187 (removed, len(output.res)))
Klement Sekeraf62ae122016-10-11 11:47:09 +0200188 return output
Matej Klotton0178d522016-11-04 11:11:44 +0100189
Klement Sekeradab231a2016-12-21 08:50:14 +0100190 def get_capture(self, expected_count=None, remark=None, timeout=1,
191 filter_out_fn=is_ipv6_misc):
192 """ Get captured packets
193
194 :param expected_count: expected number of packets to capture, if None,
195 then self.test.packet_count_for_dst_pg_idx is
196 used to lookup the expected count
197 :param remark: remark printed into debug logs
198 :param timeout: how long to wait for packets
199 :param filter_out_fn: filter applied to each packet, packets for which
200 the filter returns True are removed from capture
201 :returns: iterable packets
202 """
203 remaining_time = timeout
204 capture = None
205 name = self.name if remark is None else "%s (%s)" % (self.name, remark)
206 based_on = "based on provided argument"
207 if expected_count is None:
208 expected_count = \
209 self.test.get_packet_count_for_if_idx(self.sw_if_index)
210 based_on = "based on stored packet_infos"
Klement Sekerac86fa022017-01-02 09:03:47 +0100211 if expected_count == 0:
212 raise Exception(
Klement Sekerada505f62017-01-04 12:58:53 +0100213 "Internal error, expected packet count for %s is 0!" %
214 name)
Jane546d3b2016-12-08 13:10:03 +0100215 self.test.logger.debug("Expecting to capture %s (%s) packets on %s" % (
Klement Sekeradab231a2016-12-21 08:50:14 +0100216 expected_count, based_on, name))
Klement Sekeradab231a2016-12-21 08:50:14 +0100217 while remaining_time > 0:
218 before = time.time()
219 capture = self._get_capture(remaining_time, filter_out_fn)
220 elapsed_time = time.time() - before
221 if capture:
222 if len(capture.res) == expected_count:
223 # bingo, got the packets we expected
224 return capture
Jan Gelety057bb8c2016-12-20 17:32:45 +0100225 elif len(capture.res) > expected_count:
226 self.test.logger.error(
227 ppc("Unexpected packets captured:", capture))
228 break
Klement Sekera97f6edc2017-01-12 07:17:01 +0100229 else:
230 self.test.logger.debug("Partial capture containing %s "
231 "packets doesn't match expected "
232 "count %s (yet?)" %
233 (len(capture.res), expected_count))
234 elif expected_count == 0:
235 # bingo, got None as we expected - return empty capture
236 return PacketList()
Klement Sekeradab231a2016-12-21 08:50:14 +0100237 remaining_time -= elapsed_time
238 if capture:
Klement Sekera97f6edc2017-01-12 07:17:01 +0100239 self.generate_debug_aid("count-mismatch")
Klement Sekeradab231a2016-12-21 08:50:14 +0100240 raise Exception("Captured packets mismatch, captured %s packets, "
241 "expected %s packets on %s" %
242 (len(capture.res), expected_count, name))
243 else:
244 raise Exception("No packets captured on %s" % name)
245
246 def assert_nothing_captured(self, remark=None, filter_out_fn=is_ipv6_misc):
247 """ Assert that nothing unfiltered was captured on interface
248
249 :param remark: remark printed into debug logs
250 :param filter_out_fn: filter applied to each packet, packets for which
251 the filter returns True are removed from capture
252 """
Klement Sekera9225dee2016-12-12 08:36:58 +0100253 if os.path.isfile(self.out_path):
254 try:
Klement Sekeradab231a2016-12-21 08:50:14 +0100255 capture = self.get_capture(
256 0, remark=remark, filter_out_fn=filter_out_fn)
Klement Sekera97f6edc2017-01-12 07:17:01 +0100257 if not capture or len(capture.res) == 0:
Jane546d3b2016-12-08 13:10:03 +0100258 # junk filtered out, we're good
259 return
Klement Sekera9225dee2016-12-12 08:36:58 +0100260 except:
261 pass
Klement Sekera97f6edc2017-01-12 07:17:01 +0100262 self.generate_debug_aid("empty-assert")
Klement Sekera9225dee2016-12-12 08:36:58 +0100263 if remark:
264 raise AssertionError(
Jane546d3b2016-12-08 13:10:03 +0100265 "Non-empty capture file present for interface %s (%s)" %
Klement Sekera9225dee2016-12-12 08:36:58 +0100266 (self.name, remark))
267 else:
Jane546d3b2016-12-08 13:10:03 +0100268 raise AssertionError("Capture file present for interface %s" %
269 self.name)
Klement Sekera9225dee2016-12-12 08:36:58 +0100270
271 def wait_for_capture_file(self, timeout=1):
272 """
273 Wait until pcap capture file appears
274
275 :param timeout: How long to wait for the packet (default 1s)
276
Klement Sekeradab231a2016-12-21 08:50:14 +0100277 :returns: True/False if the file is present or appears within timeout
Klement Sekera9225dee2016-12-12 08:36:58 +0100278 """
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100279 deadline = time.time() + timeout
Klement Sekera9225dee2016-12-12 08:36:58 +0100280 if not os.path.isfile(self.out_path):
Klement Sekeradab231a2016-12-21 08:50:14 +0100281 self.test.logger.debug("Waiting for capture file %s to appear, "
282 "timeout is %ss" % (self.out_path, timeout))
Klement Sekera9225dee2016-12-12 08:36:58 +0100283 else:
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100284 self.test.logger.debug("Capture file %s already exists" %
285 self.out_path)
Klement Sekeradab231a2016-12-21 08:50:14 +0100286 return True
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100287 while time.time() < deadline:
Klement Sekera9225dee2016-12-12 08:36:58 +0100288 if os.path.isfile(self.out_path):
289 break
Paul Vinciguerra0f6602c2019-03-10 09:10:54 -0700290 self._test.sleep(0) # yield
Klement Sekera9225dee2016-12-12 08:36:58 +0100291 if os.path.isfile(self.out_path):
292 self.test.logger.debug("Capture file appeared after %fs" %
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100293 (time.time() - (deadline - timeout)))
Klement Sekera9225dee2016-12-12 08:36:58 +0100294 else:
295 self.test.logger.debug("Timeout - capture file still nowhere")
Klement Sekeradab231a2016-12-21 08:50:14 +0100296 return False
297 return True
Klement Sekera9225dee2016-12-12 08:36:58 +0100298
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100299 def verify_enough_packet_data_in_pcap(self):
Klement Sekerab91017a2017-02-09 06:04:36 +0100300 """
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100301 Check if enough data is available in file handled by internal pcap
302 reader so that a whole packet can be read.
Klement Sekerab91017a2017-02-09 06:04:36 +0100303
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100304 :returns: True if enough data present, else False
Klement Sekerab91017a2017-02-09 06:04:36 +0100305 """
306 orig_pos = self._pcap_reader.f.tell() # save file position
307 enough_data = False
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100308 # read packet header from pcap
309 packet_header_size = 16
310 caplen = None
311 end_pos = None
312 hdr = self._pcap_reader.f.read(packet_header_size)
313 if len(hdr) == packet_header_size:
314 # parse the capture length - caplen
Klement Sekerab91017a2017-02-09 06:04:36 +0100315 sec, usec, caplen, wirelen = struct.unpack(
316 self._pcap_reader.endian + "IIII", hdr)
317 self._pcap_reader.f.seek(0, 2) # seek to end of file
318 end_pos = self._pcap_reader.f.tell() # get position at end
319 if end_pos >= orig_pos + len(hdr) + caplen:
320 enough_data = True # yay, we have enough data
Klement Sekerab91017a2017-02-09 06:04:36 +0100321 self._pcap_reader.f.seek(orig_pos, 0) # restore original position
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100322 return enough_data
Klement Sekerab91017a2017-02-09 06:04:36 +0100323
Klement Sekeradab231a2016-12-21 08:50:14 +0100324 def wait_for_packet(self, timeout, filter_out_fn=is_ipv6_misc):
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200325 """
326 Wait for next packet captured with a timeout
327
328 :param timeout: How long to wait for the packet
329
330 :returns: Captured packet if no packet arrived within timeout
331 :raises Exception: if no packet arrives within timeout
332 """
Klement Sekeradab231a2016-12-21 08:50:14 +0100333 deadline = time.time() + timeout
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200334 if self._pcap_reader is None:
Klement Sekeradab231a2016-12-21 08:50:14 +0100335 if not self.wait_for_capture_file(timeout):
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100336 raise CaptureTimeoutError("Capture file %s did not appear "
337 "within timeout" % self.out_path)
Klement Sekeradab231a2016-12-21 08:50:14 +0100338 while time.time() < deadline:
339 try:
340 self._pcap_reader = PcapReader(self.out_path)
341 break
342 except:
Klement Sekerada505f62017-01-04 12:58:53 +0100343 self.test.logger.debug(
Klement Sekera97f6edc2017-01-12 07:17:01 +0100344 "Exception in scapy.PcapReader(%s): %s" %
Klement Sekerada505f62017-01-04 12:58:53 +0100345 (self.out_path, format_exc()))
Klement Sekeradab231a2016-12-21 08:50:14 +0100346 if not self._pcap_reader:
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100347 raise CaptureTimeoutError("Capture file %s did not appear within "
348 "timeout" % self.out_path)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200349
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100350 poll = False
351 if timeout > 0:
352 self.test.logger.debug("Waiting for packet")
353 else:
354 poll = True
355 self.test.logger.debug("Polling for packet")
356 while time.time() < deadline or poll:
357 if not self.verify_enough_packet_data_in_pcap():
Paul Vinciguerra0f6602c2019-03-10 09:10:54 -0700358 self._test.sleep(0) # yield
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100359 poll = False
360 continue
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200361 p = self._pcap_reader.recv()
362 if p is not None:
Klement Sekeradab231a2016-12-21 08:50:14 +0100363 if filter_out_fn is not None and filter_out_fn(p):
364 self.test.logger.debug(
365 "Packet received after %ss was filtered out" %
366 (time.time() - (deadline - timeout)))
367 else:
Klement Sekerada505f62017-01-04 12:58:53 +0100368 self.test.logger.debug(
369 "Packet received after %fs" %
370 (time.time() - (deadline - timeout)))
Klement Sekeradab231a2016-12-21 08:50:14 +0100371 return p
Paul Vinciguerra0f6602c2019-03-10 09:10:54 -0700372 self._test.sleep(0) # yield
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100373 poll = False
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200374 self.test.logger.debug("Timeout - no packets received")
Klement Sekeraacb9b8e2017-02-14 02:55:31 +0100375 raise CaptureTimeoutError("Packet didn't arrive within timeout")
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200376
Matej Klotton0178d522016-11-04 11:11:44 +0100377 def create_arp_req(self):
378 """Create ARP request applicable for this interface"""
379 return (Ether(dst="ff:ff:ff:ff:ff:ff", src=self.remote_mac) /
380 ARP(op=ARP.who_has, pdst=self.local_ip4,
381 psrc=self.remote_ip4, hwsrc=self.remote_mac))
382
383 def create_ndp_req(self):
384 """Create NDP - NS applicable for this interface"""
Neale Ranns465a1a32017-01-07 10:04:09 -0800385 nsma = in6_getnsma(inet_pton(socket.AF_INET6, self.local_ip6))
386 d = inet_ntop(socket.AF_INET6, nsma)
387
388 return (Ether(dst=in6_getnsmac(nsma)) /
389 IPv6(dst=d, src=self.remote_ip6) /
Klement Sekera74dcdbf2016-11-14 09:49:09 +0100390 ICMPv6ND_NS(tgt=self.local_ip6) /
391 ICMPv6NDOptSrcLLAddr(lladdr=self.remote_mac))
Matej Klotton0178d522016-11-04 11:11:44 +0100392
393 def resolve_arp(self, pg_interface=None):
394 """Resolve ARP using provided packet-generator interface
395
396 :param pg_interface: interface used to resolve, if None then this
397 interface is used
398
399 """
400 if pg_interface is None:
401 pg_interface = self
Klement Sekera7bb873a2016-11-18 07:38:42 +0100402 self.test.logger.info("Sending ARP request for %s on port %s" %
403 (self.local_ip4, pg_interface.name))
Matej Klotton0178d522016-11-04 11:11:44 +0100404 arp_req = self.create_arp_req()
405 pg_interface.add_stream(arp_req)
406 pg_interface.enable_capture()
407 self.test.pg_start()
Klement Sekera7bb873a2016-11-18 07:38:42 +0100408 self.test.logger.info(self.test.vapi.cli("show trace"))
Klement Sekera9225dee2016-12-12 08:36:58 +0100409 try:
Klement Sekeradab231a2016-12-21 08:50:14 +0100410 captured_packet = pg_interface.wait_for_packet(1)
Klement Sekera9225dee2016-12-12 08:36:58 +0100411 except:
412 self.test.logger.info("No ARP received on port %s" %
413 pg_interface.name)
Matej Klotton0178d522016-11-04 11:11:44 +0100414 return
Klement Sekeradab231a2016-12-21 08:50:14 +0100415 arp_reply = captured_packet.copy() # keep original for exception
Matej Klotton0178d522016-11-04 11:11:44 +0100416 # Make Dot1AD packet content recognizable to scapy
417 if arp_reply.type == 0x88a8:
418 arp_reply.type = 0x8100
Paul Vinciguerraa7427ec2019-03-10 10:04:23 -0700419 arp_reply = Ether(scapy.compat.raw(arp_reply))
Matej Klotton0178d522016-11-04 11:11:44 +0100420 try:
421 if arp_reply[ARP].op == ARP.is_at:
Klement Sekera7bb873a2016-11-18 07:38:42 +0100422 self.test.logger.info("VPP %s MAC address is %s " %
423 (self.name, arp_reply[ARP].hwsrc))
Matej Klotton0178d522016-11-04 11:11:44 +0100424 self._local_mac = arp_reply[ARP].hwsrc
425 else:
Klement Sekeradab231a2016-12-21 08:50:14 +0100426 self.test.logger.info("No ARP received on port %s" %
427 pg_interface.name)
Matej Klotton0178d522016-11-04 11:11:44 +0100428 except:
Klement Sekera7bb873a2016-11-18 07:38:42 +0100429 self.test.logger.error(
Klement Sekeradab231a2016-12-21 08:50:14 +0100430 ppp("Unexpected response to ARP request:", captured_packet))
Matej Klotton0178d522016-11-04 11:11:44 +0100431 raise
432
Klement Sekeradab231a2016-12-21 08:50:14 +0100433 def resolve_ndp(self, pg_interface=None, timeout=1):
Matej Klotton0178d522016-11-04 11:11:44 +0100434 """Resolve NDP using provided packet-generator interface
435
436 :param pg_interface: interface used to resolve, if None then this
437 interface is used
Klement Sekeradab231a2016-12-21 08:50:14 +0100438 :param timeout: how long to wait for response before giving up
Matej Klotton0178d522016-11-04 11:11:44 +0100439
440 """
441 if pg_interface is None:
442 pg_interface = self
Klement Sekera7bb873a2016-11-18 07:38:42 +0100443 self.test.logger.info("Sending NDP request for %s on port %s" %
444 (self.local_ip6, pg_interface.name))
Matej Klotton0178d522016-11-04 11:11:44 +0100445 ndp_req = self.create_ndp_req()
446 pg_interface.add_stream(ndp_req)
447 pg_interface.enable_capture()
448 self.test.pg_start()
Klement Sekeradab231a2016-12-21 08:50:14 +0100449 now = time.time()
450 deadline = now + timeout
Neale Ranns82a06a92016-12-08 20:05:33 +0000451 # Enabling IPv6 on an interface can generate more than the
452 # ND reply we are looking for (namely MLD). So loop through
453 # the replies to look for want we want.
Klement Sekeradab231a2016-12-21 08:50:14 +0100454 while now < deadline:
455 try:
456 captured_packet = pg_interface.wait_for_packet(
457 deadline - now, filter_out_fn=None)
458 except:
Klement Sekerada505f62017-01-04 12:58:53 +0100459 self.test.logger.error(
460 "Timeout while waiting for NDP response")
Klement Sekeradab231a2016-12-21 08:50:14 +0100461 raise
462 ndp_reply = captured_packet.copy() # keep original for exception
Neale Ranns82a06a92016-12-08 20:05:33 +0000463 # Make Dot1AD packet content recognizable to scapy
464 if ndp_reply.type == 0x88a8:
Paul Vinciguerraa7427ec2019-03-10 10:04:23 -0700465 self._test.logger.info(
466 "Replacing EtherType: 0x88a8 with "
467 "0x8100 and regenerating Ethernet header. ")
Neale Ranns82a06a92016-12-08 20:05:33 +0000468 ndp_reply.type = 0x8100
Paul Vinciguerraa7427ec2019-03-10 10:04:23 -0700469 ndp_reply = Ether(scapy.compat.raw(ndp_reply))
Neale Ranns82a06a92016-12-08 20:05:33 +0000470 try:
471 ndp_na = ndp_reply[ICMPv6ND_NA]
472 opt = ndp_na[ICMPv6NDOptDstLLAddr]
473 self.test.logger.info("VPP %s MAC address is %s " %
474 (self.name, opt.lladdr))
475 self._local_mac = opt.lladdr
Klement Sekeradab231a2016-12-21 08:50:14 +0100476 self.test.logger.debug(self.test.vapi.cli("show trace"))
477 # we now have the MAC we've been after
478 return
Neale Ranns82a06a92016-12-08 20:05:33 +0000479 except:
480 self.test.logger.info(
Klement Sekerada505f62017-01-04 12:58:53 +0100481 ppp("Unexpected response to NDP request:",
482 captured_packet))
Klement Sekeradab231a2016-12-21 08:50:14 +0100483 now = time.time()
484
485 self.test.logger.debug(self.test.vapi.cli("show trace"))
486 raise Exception("Timeout while waiting for NDP response")