Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 1 | import os |
Dmitry Valter | 71d02aa | 2023-01-27 12:49:55 +0000 | [diff] [blame] | 2 | import shutil |
Paul Vinciguerra | 582eac5 | 2020-04-03 12:18:40 -0400 | [diff] [blame] | 3 | import socket |
snaramre | 5d4b891 | 2019-12-13 23:39:35 +0000 | [diff] [blame] | 4 | from socket import inet_pton, inet_ntop |
Klement Sekera | b91017a | 2017-02-09 06:04:36 +0100 | [diff] [blame] | 5 | import struct |
Paul Vinciguerra | 582eac5 | 2020-04-03 12:18:40 -0400 | [diff] [blame] | 6 | import time |
Klement Sekera | 97f6edc | 2017-01-12 07:17:01 +0100 | [diff] [blame] | 7 | from traceback import format_exc, format_stack |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 8 | from sh import tshark |
| 9 | from pathlib import Path |
Paul Vinciguerra | a7427ec | 2019-03-10 10:04:23 -0700 | [diff] [blame] | 10 | |
Klement Sekera | b23ffd7 | 2021-05-31 16:08:53 +0200 | [diff] [blame] | 11 | from config import config |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 12 | from scapy.utils import wrpcap, rdpcap, PcapReader |
Klement Sekera | 97f6edc | 2017-01-12 07:17:01 +0100 | [diff] [blame] | 13 | from scapy.plist import PacketList |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 14 | from vpp_interface import VppInterface |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 15 | from vpp_papi import VppEnum |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 16 | |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 17 | from scapy.layers.l2 import Ether, ARP |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 18 | from scapy.layers.inet6 import ( |
| 19 | IPv6, |
| 20 | ICMPv6ND_NS, |
| 21 | ICMPv6ND_NA, |
| 22 | ICMPv6NDOptSrcLLAddr, |
| 23 | ICMPv6NDOptDstLLAddr, |
| 24 | ICMPv6ND_RA, |
| 25 | RouterAlert, |
| 26 | IPv6ExtHdrHopByHop, |
| 27 | ) |
Klement Sekera | 26cd024 | 2022-02-18 10:35:08 +0000 | [diff] [blame] | 28 | from util import ppp, ppc, UnexpectedPacketError |
Neale Ranns | 7515228 | 2017-01-09 01:00:45 -0800 | [diff] [blame] | 29 | from scapy.utils6 import in6_getnsma, in6_getnsmac, in6_ismaddr |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 30 | |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 31 | |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 32 | class CaptureTimeoutError(Exception): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 33 | """Exception raised if capture or packet doesn't appear within timeout""" |
| 34 | |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 35 | pass |
| 36 | |
| 37 | |
Klement Sekera | 65cc8c0 | 2016-12-18 15:49:54 +0100 | [diff] [blame] | 38 | def is_ipv6_misc(p): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 39 | """Is packet one of uninteresting IPv6 broadcasts?""" |
Klement Sekera | 65cc8c0 | 2016-12-18 15:49:54 +0100 | [diff] [blame] | 40 | if p.haslayer(ICMPv6ND_RA): |
Neale Ranns | 7515228 | 2017-01-09 01:00:45 -0800 | [diff] [blame] | 41 | if in6_ismaddr(p[IPv6].dst): |
| 42 | return True |
Klement Sekera | 65cc8c0 | 2016-12-18 15:49:54 +0100 | [diff] [blame] | 43 | if p.haslayer(IPv6ExtHdrHopByHop): |
| 44 | for o in p[IPv6ExtHdrHopByHop].options: |
| 45 | if isinstance(o, RouterAlert): |
| 46 | return True |
| 47 | return False |
| 48 | |
| 49 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 50 | class VppPGInterface(VppInterface): |
| 51 | """ |
| 52 | VPP packet-generator interface |
| 53 | """ |
| 54 | |
| 55 | @property |
| 56 | def pg_index(self): |
| 57 | """packet-generator interface index assigned by VPP""" |
| 58 | return self._pg_index |
| 59 | |
| 60 | @property |
Mohsin Kazmi | 22e9cfd | 2019-07-23 11:54:48 +0200 | [diff] [blame] | 61 | def gso_enabled(self): |
| 62 | """gso enabled on packet-generator interface""" |
| 63 | if self._gso_enabled == 0: |
| 64 | return "gso-disabled" |
| 65 | return "gso-enabled" |
| 66 | |
| 67 | @property |
| 68 | def gso_size(self): |
| 69 | """gso size on packet-generator interface""" |
| 70 | return self._gso_size |
| 71 | |
| 72 | @property |
Mohsin Kazmi | f382b06 | 2020-08-11 15:00:44 +0200 | [diff] [blame] | 73 | def coalesce_is_enabled(self): |
| 74 | """coalesce enabled on packet-generator interface""" |
| 75 | if self._coalesce_enabled == 0: |
| 76 | return "coalesce-disabled" |
| 77 | return "coalesce-enabled" |
| 78 | |
| 79 | @property |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 80 | def out_path(self): |
| 81 | """pcap file path - captured packets""" |
| 82 | return self._out_path |
| 83 | |
Klement Sekera | 7ba9fae | 2021-03-31 13:36:38 +0200 | [diff] [blame] | 84 | def get_in_path(self, worker): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 85 | """pcap file path - injected packets""" |
Klement Sekera | 7ba9fae | 2021-03-31 13:36:38 +0200 | [diff] [blame] | 86 | if worker is not None: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 87 | return "%s/pg%u_wrk%u_in.pcap" % (self.test.tempdir, self.pg_index, worker) |
Klement Sekera | 7ba9fae | 2021-03-31 13:36:38 +0200 | [diff] [blame] | 88 | return "%s/pg%u_in.pcap" % (self.test.tempdir, self.pg_index) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 89 | |
| 90 | @property |
| 91 | def capture_cli(self): |
| 92 | """CLI string to start capture on this interface""" |
| 93 | return self._capture_cli |
| 94 | |
Klement Sekera | 7ba9fae | 2021-03-31 13:36:38 +0200 | [diff] [blame] | 95 | def get_cap_name(self, worker=None): |
| 96 | """return capture name for this interface and given worker""" |
| 97 | if worker is not None: |
| 98 | return self._cap_name + "-worker%d" % worker |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 99 | return self._cap_name |
| 100 | |
Klement Sekera | 7ba9fae | 2021-03-31 13:36:38 +0200 | [diff] [blame] | 101 | def get_input_cli(self, nb_replays=None, worker=None): |
| 102 | """return CLI string to load the injected packets""" |
| 103 | input_cli = "packet-generator new pcap %s source pg%u name %s" % ( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 104 | self.get_in_path(worker), |
| 105 | self.pg_index, |
| 106 | self.get_cap_name(worker), |
| 107 | ) |
Klement Sekera | 7ba9fae | 2021-03-31 13:36:38 +0200 | [diff] [blame] | 108 | if nb_replays is not None: |
| 109 | return "%s limit %d" % (input_cli, nb_replays) |
| 110 | if worker is not None: |
| 111 | return "%s worker %d" % (input_cli, worker) |
| 112 | return input_cli |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 113 | |
Klement Sekera | 778c276 | 2016-11-08 02:00:28 +0100 | [diff] [blame] | 114 | @property |
| 115 | def in_history_counter(self): |
| 116 | """Self-incrementing counter used when renaming old pcap files""" |
| 117 | v = self._in_history_counter |
| 118 | self._in_history_counter += 1 |
| 119 | return v |
| 120 | |
| 121 | @property |
| 122 | def out_history_counter(self): |
| 123 | """Self-incrementing counter used when renaming old pcap files""" |
| 124 | v = self._out_history_counter |
| 125 | self._out_history_counter += 1 |
| 126 | return v |
| 127 | |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 128 | def __init__(self, test, pg_index, gso, gso_size, mode): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 129 | """Create VPP packet-generator interface""" |
Klement Sekera | 7ba9fae | 2021-03-31 13:36:38 +0200 | [diff] [blame] | 130 | super().__init__(test) |
Klement Sekera | a98346f | 2018-05-16 10:52:45 +0200 | [diff] [blame] | 131 | |
Neale Ranns | 6197cb7 | 2021-06-03 14:43:21 +0000 | [diff] [blame] | 132 | r = test.vapi.pg_create_interface_v2(pg_index, gso, gso_size, mode) |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 133 | self.set_sw_if_index(r.sw_if_index) |
| 134 | |
Matej Klotton | c5bf07f | 2016-11-23 15:27:17 +0100 | [diff] [blame] | 135 | self._in_history_counter = 0 |
| 136 | self._out_history_counter = 0 |
Klement Sekera | 97f6edc | 2017-01-12 07:17:01 +0100 | [diff] [blame] | 137 | self._out_assert_counter = 0 |
Matej Klotton | c5bf07f | 2016-11-23 15:27:17 +0100 | [diff] [blame] | 138 | self._pg_index = pg_index |
Mohsin Kazmi | 22e9cfd | 2019-07-23 11:54:48 +0200 | [diff] [blame] | 139 | self._gso_enabled = gso |
| 140 | self._gso_size = gso_size |
Mohsin Kazmi | f382b06 | 2020-08-11 15:00:44 +0200 | [diff] [blame] | 141 | self._coalesce_enabled = 0 |
Klement Sekera | 74dcdbf | 2016-11-14 09:49:09 +0100 | [diff] [blame] | 142 | self._out_file = "pg%u_out.pcap" % self.pg_index |
Klement Sekera | 778c276 | 2016-11-08 02:00:28 +0100 | [diff] [blame] | 143 | self._out_path = self.test.tempdir + "/" + self._out_file |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 144 | self._capture_cli = "packet-generator capture pg%u pcap %s" % ( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 145 | self.pg_index, |
| 146 | self.out_path, |
| 147 | ) |
| 148 | self._cap_name = "pcap%u-sw_if_index-%s" % (self.pg_index, self.sw_if_index) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 149 | |
Mohsin Kazmi | 0b1bd9d | 2024-10-03 23:18:17 +0000 | [diff] [blame] | 150 | def remove_vpp_config(self): |
| 151 | """delete Pg interface""" |
| 152 | self.disable_capture() |
| 153 | self.test.vapi.pg_delete_interface(sw_if_index=self.sw_if_index) |
| 154 | |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 155 | def link_pcap_file(self, path, direction, counter): |
Klement Sekera | b23ffd7 | 2021-05-31 16:08:53 +0200 | [diff] [blame] | 156 | if not config.keep_pcaps: |
Klement Sekera | b23ffd7 | 2021-05-31 16:08:53 +0200 | [diff] [blame] | 157 | return |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 158 | filename = os.path.basename(path) |
| 159 | test_name = ( |
| 160 | self.test_name |
| 161 | if hasattr(self, "test_name") |
| 162 | else f"suite{self.test.__name__}" |
| 163 | ) |
| 164 | name = f"{self.test.tempdir}/{test_name}.[timestamp:{time.time():.8f}].{self.name}-{direction}-{counter:04}.{filename}" |
| 165 | if os.path.isfile(name): |
| 166 | self.test.logger.debug( |
| 167 | f"Skipping hard link creation: {name} already exists!" |
| 168 | ) |
| 169 | return |
Paul Vinciguerra | 4b58a86 | 2019-05-28 15:40:47 -0400 | [diff] [blame] | 170 | try: |
| 171 | if os.path.isfile(path): |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 172 | self.test.logger.debug(f"Creating hard link {path}->{name}") |
| 173 | os.link(path, name) |
Paul Vinciguerra | 4b58a86 | 2019-05-28 15:40:47 -0400 | [diff] [blame] | 174 | except OSError: |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 175 | self.test.logger.debug( |
| 176 | f"OSError: Could not create hard link {path}->{name}" |
| 177 | ) |
| 178 | |
| 179 | def remove_old_pcap_file(self, path): |
Dave Wallace | 87a8826 | 2023-11-27 20:23:03 -0500 | [diff] [blame] | 180 | self.wait_for_pg_stop() |
| 181 | self.test.unlink_testcase_file(self.test, Path(path)) |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 182 | return |
| 183 | |
| 184 | def decode_pcap_files(self, pcap_dir, filename_prefix): |
| 185 | # Generate tshark packet trace of testcase pcap files |
| 186 | pg_decode = f"{pcap_dir}/pcap-decode-{filename_prefix}.txt" |
| 187 | if os.path.isfile(pg_decode): |
| 188 | self.test.logger.debug( |
| 189 | f"The pg streams decode file already exists: {pg_decode}" |
| 190 | ) |
| 191 | return |
| 192 | self.test.logger.debug( |
| 193 | f"Generating testcase pg streams decode file: {pg_decode}" |
| 194 | ) |
Dave Wallace | 0da0883 | 2024-07-16 12:36:38 -0400 | [diff] [blame] | 195 | ts_opts = "-Vxr" |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 196 | for p in sorted(Path(pcap_dir).glob(f"{filename_prefix}*.pcap")): |
| 197 | self.test.logger.debug(f"Decoding {p}") |
| 198 | with open(f"{pg_decode}", "a", buffering=1) as f: |
| 199 | print(f"tshark {ts_opts} {p}", file=f) |
| 200 | tshark(ts_opts, f"{p}", _out=f) |
| 201 | print("", file=f) |
Paul Vinciguerra | 4b58a86 | 2019-05-28 15:40:47 -0400 | [diff] [blame] | 202 | |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 203 | def enable_capture(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 204 | """Enable capture on this packet-generator interface |
| 205 | of at most n packets. |
| 206 | If n < 0, this is no limit |
Alexandre Poirrier | a618e20 | 2019-05-07 10:43:41 +0200 | [diff] [blame] | 207 | """ |
Andrew Yourtchenko | cb265c6 | 2019-07-25 10:03:51 +0000 | [diff] [blame] | 208 | # disable the capture to flush the capture |
| 209 | self.disable_capture() |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 210 | self.remove_old_pcap_file(self.out_path) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 211 | # FIXME this should be an API, but no such exists atm |
| 212 | self.test.vapi.cli(self.capture_cli) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 213 | self._pcap_reader = None |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 214 | |
Alexandre Poirrier | a618e20 | 2019-05-07 10:43:41 +0200 | [diff] [blame] | 215 | def disable_capture(self): |
| 216 | self.test.vapi.cli("%s disable" % self.capture_cli) |
| 217 | |
Mohsin Kazmi | f382b06 | 2020-08-11 15:00:44 +0200 | [diff] [blame] | 218 | def coalesce_enable(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 219 | """Enable packet coalesce on this packet-generator interface""" |
Mohsin Kazmi | f382b06 | 2020-08-11 15:00:44 +0200 | [diff] [blame] | 220 | self._coalesce_enabled = 1 |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 221 | self.test.vapi.pg_interface_enable_disable_coalesce(self.sw_if_index, 1) |
Mohsin Kazmi | f382b06 | 2020-08-11 15:00:44 +0200 | [diff] [blame] | 222 | |
| 223 | def coalesce_disable(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 224 | """Disable packet coalesce on this packet-generator interface""" |
Mohsin Kazmi | f382b06 | 2020-08-11 15:00:44 +0200 | [diff] [blame] | 225 | self._coalesce_enabled = 0 |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 226 | self.test.vapi.pg_interface_enable_disable_coalesce(self.sw_if_index, 0) |
Mohsin Kazmi | f382b06 | 2020-08-11 15:00:44 +0200 | [diff] [blame] | 227 | |
Klement Sekera | 4ecbf10 | 2019-07-31 13:14:16 +0000 | [diff] [blame] | 228 | def add_stream(self, pkts, nb_replays=None, worker=None): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 229 | """ |
| 230 | Add a stream of packets to this packet-generator |
| 231 | |
| 232 | :param pkts: iterable packets |
| 233 | |
| 234 | """ |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 235 | in_pcap = self.get_in_path(worker) |
| 236 | if os.path.isfile(in_pcap): |
| 237 | self.remove_old_pcap_file(in_pcap) |
| 238 | wrpcap(in_pcap, pkts) |
Klement Sekera | 3ff6ffc | 2021-04-01 18:19:29 +0200 | [diff] [blame] | 239 | self.test.register_pcap(self, worker) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 240 | # FIXME this should be an API, but no such exists atm |
Klement Sekera | 7ba9fae | 2021-03-31 13:36:38 +0200 | [diff] [blame] | 241 | self.test.vapi.cli(self.get_input_cli(nb_replays, worker)) |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 242 | self.link_pcap_file(self.get_in_path(worker), "inp", self.in_history_counter) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 243 | |
Klement Sekera | 97f6edc | 2017-01-12 07:17:01 +0100 | [diff] [blame] | 244 | def generate_debug_aid(self, kind): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 245 | """Create a hardlink to the out file with a counter and a file |
Klement Sekera | 97f6edc | 2017-01-12 07:17:01 +0100 | [diff] [blame] | 246 | containing stack trace to ease debugging in case of multiple capture |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 247 | files present.""" |
| 248 | self.test.logger.debug("Generating debug aid for %s on %s" % (kind, self._name)) |
| 249 | link_path, stack_path = [ |
| 250 | "%s/debug_%s_%s_%s.%s" |
| 251 | % (self.test.tempdir, self._name, self._out_assert_counter, kind, suffix) |
| 252 | for suffix in ["pcap", "stack"] |
| 253 | ] |
Klement Sekera | 97f6edc | 2017-01-12 07:17:01 +0100 | [diff] [blame] | 254 | os.link(self.out_path, link_path) |
| 255 | with open(stack_path, "w") as f: |
| 256 | f.writelines(format_stack()) |
| 257 | self._out_assert_counter += 1 |
| 258 | |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 259 | def _get_capture(self, timeout, filter_out_fn=is_ipv6_misc): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 260 | """Helper method to get capture and filter it""" |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 261 | try: |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 262 | if not self.wait_for_capture_file(timeout): |
| 263 | return None |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 264 | output = rdpcap(self.out_path) |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 265 | self.test.logger.debug(f"Capture has {len(output.res)} packets") |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 266 | except: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 267 | self.test.logger.debug( |
| 268 | "Exception in scapy.rdpcap (%s): %s" % (self.out_path, format_exc()) |
| 269 | ) |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 270 | return None |
Klement Sekera | 65cc8c0 | 2016-12-18 15:49:54 +0100 | [diff] [blame] | 271 | before = len(output.res) |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 272 | if filter_out_fn: |
| 273 | output.res = [p for p in output.res if not filter_out_fn(p)] |
Klement Sekera | 97f6edc | 2017-01-12 07:17:01 +0100 | [diff] [blame] | 274 | removed = before - len(output.res) |
Klement Sekera | 65cc8c0 | 2016-12-18 15:49:54 +0100 | [diff] [blame] | 275 | if removed: |
| 276 | self.test.logger.debug( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 277 | "Filtered out %s packets from capture (returning %s)" |
| 278 | % (removed, len(output.res)) |
| 279 | ) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 280 | return output |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 281 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 282 | def get_capture( |
| 283 | self, expected_count=None, remark=None, timeout=1, filter_out_fn=is_ipv6_misc |
| 284 | ): |
| 285 | """Get captured packets |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 286 | |
| 287 | :param expected_count: expected number of packets to capture, if None, |
| 288 | then self.test.packet_count_for_dst_pg_idx is |
| 289 | used to lookup the expected count |
| 290 | :param remark: remark printed into debug logs |
| 291 | :param timeout: how long to wait for packets |
| 292 | :param filter_out_fn: filter applied to each packet, packets for which |
| 293 | the filter returns True are removed from capture |
| 294 | :returns: iterable packets |
| 295 | """ |
| 296 | remaining_time = timeout |
| 297 | capture = None |
| 298 | name = self.name if remark is None else "%s (%s)" % (self.name, remark) |
| 299 | based_on = "based on provided argument" |
| 300 | if expected_count is None: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 301 | expected_count = self.test.get_packet_count_for_if_idx(self.sw_if_index) |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 302 | based_on = "based on stored packet_infos" |
Klement Sekera | c86fa02 | 2017-01-02 09:03:47 +0100 | [diff] [blame] | 303 | if expected_count == 0: |
| 304 | raise Exception( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 305 | "Internal error, expected packet count for %s is 0!" % name |
| 306 | ) |
| 307 | self.test.logger.debug( |
| 308 | "Expecting to capture %s (%s) packets on %s" |
| 309 | % (expected_count, based_on, name) |
| 310 | ) |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 311 | while remaining_time > 0: |
| 312 | before = time.time() |
| 313 | capture = self._get_capture(remaining_time, filter_out_fn) |
| 314 | elapsed_time = time.time() - before |
| 315 | if capture: |
| 316 | if len(capture.res) == expected_count: |
| 317 | # bingo, got the packets we expected |
| 318 | return capture |
Jan Gelety | 057bb8c | 2016-12-20 17:32:45 +0100 | [diff] [blame] | 319 | elif len(capture.res) > expected_count: |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 320 | self.test.logger.error( |
| 321 | ppc( |
| 322 | f"Unexpected packets captured, got {len(capture.res)}, expected {expected_count}:", |
| 323 | capture, |
| 324 | ) |
| 325 | ) |
Jan Gelety | 057bb8c | 2016-12-20 17:32:45 +0100 | [diff] [blame] | 326 | break |
Klement Sekera | 97f6edc | 2017-01-12 07:17:01 +0100 | [diff] [blame] | 327 | else: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 328 | self.test.logger.debug( |
| 329 | "Partial capture containing %s " |
| 330 | "packets doesn't match expected " |
| 331 | "count %s (yet?)" % (len(capture.res), expected_count) |
| 332 | ) |
Klement Sekera | 97f6edc | 2017-01-12 07:17:01 +0100 | [diff] [blame] | 333 | elif expected_count == 0: |
| 334 | # bingo, got None as we expected - return empty capture |
| 335 | return PacketList() |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 336 | remaining_time -= elapsed_time |
| 337 | if capture: |
Klement Sekera | 97f6edc | 2017-01-12 07:17:01 +0100 | [diff] [blame] | 338 | self.generate_debug_aid("count-mismatch") |
Klement Sekera | 26cd024 | 2022-02-18 10:35:08 +0000 | [diff] [blame] | 339 | if len(capture) > 0 and 0 == expected_count: |
| 340 | rem = f"\n{remark}" if remark else "" |
| 341 | raise UnexpectedPacketError( |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 342 | capture[0], |
| 343 | f"\n({len(capture)} packets captured in total){rem} on {name}", |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 344 | ) |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 345 | msg = f"Captured packets mismatch, captured {len(capture.res)} packets, expected {expected_count} packets on {name}:" |
| 346 | raise Exception(f"{ppc(msg, capture)}") |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 347 | else: |
Klement Sekera | 16ce09d | 2022-04-23 11:34:29 +0200 | [diff] [blame] | 348 | if 0 == expected_count: |
| 349 | return |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 350 | raise Exception(f"No packets captured on {name} (timeout = {timeout}s)") |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 351 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 352 | def assert_nothing_captured( |
| 353 | self, timeout=1, remark=None, filter_out_fn=is_ipv6_misc |
| 354 | ): |
| 355 | """Assert that nothing unfiltered was captured on interface |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 356 | |
| 357 | :param remark: remark printed into debug logs |
| 358 | :param filter_out_fn: filter applied to each packet, packets for which |
| 359 | the filter returns True are removed from capture |
| 360 | """ |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 361 | capture = self.get_capture( |
| 362 | 0, timeout=timeout, remark=remark, filter_out_fn=filter_out_fn |
| 363 | ) |
Klement Sekera | 16ce09d | 2022-04-23 11:34:29 +0200 | [diff] [blame] | 364 | if not capture or len(capture.res) == 0: |
| 365 | # junk filtered out, we're good |
| 366 | return |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 367 | |
Andrew Yourtchenko | 3d36f19 | 2019-10-11 12:34:12 +0000 | [diff] [blame] | 368 | def wait_for_pg_stop(self): |
| 369 | # wait till packet-generator is stopped |
| 370 | # "show packet-generator" while it is still running gives this: |
| 371 | # Name Enabled Count Parameters |
| 372 | # pcap0-sw_if_inde Yes 64 limit 64, ... |
| 373 | # |
| 374 | # also have a 5-minute timeout just in case things go terribly wrong... |
| 375 | deadline = time.time() + 300 |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 376 | while self.test.vapi.cli("show packet-generator").find("Yes") != -1: |
Andrew Yourtchenko | 3d36f19 | 2019-10-11 12:34:12 +0000 | [diff] [blame] | 377 | self._test.sleep(0.01) # yield |
| 378 | if time.time() > deadline: |
| 379 | self.test.logger.debug("Timeout waiting for pg to stop") |
| 380 | break |
| 381 | |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 382 | def wait_for_capture_file(self, timeout=1): |
| 383 | """ |
| 384 | Wait until pcap capture file appears |
| 385 | |
| 386 | :param timeout: How long to wait for the packet (default 1s) |
| 387 | |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 388 | :returns: True/False if the file is present or appears within timeout |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 389 | """ |
Andrew Yourtchenko | 3d36f19 | 2019-10-11 12:34:12 +0000 | [diff] [blame] | 390 | self.wait_for_pg_stop() |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 391 | deadline = time.time() + timeout |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 392 | if not os.path.isfile(self.out_path): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 393 | self.test.logger.debug( |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 394 | f"Waiting for capture file {self.out_path} to appear, timeout is {timeout}s\n" |
| 395 | f"{' '.join(format_stack(limit=10))}" |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 396 | ) |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 397 | else: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 398 | self.test.logger.debug("Capture file %s already exists" % self.out_path) |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 399 | self.link_pcap_file(self.out_path, "out", self.out_history_counter) |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 400 | return True |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 401 | while time.time() < deadline: |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 402 | if os.path.isfile(self.out_path): |
| 403 | break |
Paul Vinciguerra | 0f6602c | 2019-03-10 09:10:54 -0700 | [diff] [blame] | 404 | self._test.sleep(0) # yield |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 405 | if os.path.isfile(self.out_path): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 406 | self.test.logger.debug( |
| 407 | "Capture file appeared after %fs" % (time.time() - (deadline - timeout)) |
| 408 | ) |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 409 | else: |
| 410 | self.test.logger.debug("Timeout - capture file still nowhere") |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 411 | return False |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 412 | self.link_pcap_file(self.out_path, "out", self.out_history_counter) |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 413 | return True |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 414 | |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 415 | def verify_enough_packet_data_in_pcap(self): |
Klement Sekera | b91017a | 2017-02-09 06:04:36 +0100 | [diff] [blame] | 416 | """ |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 417 | Check if enough data is available in file handled by internal pcap |
| 418 | reader so that a whole packet can be read. |
Klement Sekera | b91017a | 2017-02-09 06:04:36 +0100 | [diff] [blame] | 419 | |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 420 | :returns: True if enough data present, else False |
Klement Sekera | b91017a | 2017-02-09 06:04:36 +0100 | [diff] [blame] | 421 | """ |
| 422 | orig_pos = self._pcap_reader.f.tell() # save file position |
| 423 | enough_data = False |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 424 | # read packet header from pcap |
| 425 | packet_header_size = 16 |
| 426 | caplen = None |
| 427 | end_pos = None |
| 428 | hdr = self._pcap_reader.f.read(packet_header_size) |
| 429 | if len(hdr) == packet_header_size: |
| 430 | # parse the capture length - caplen |
Klement Sekera | b91017a | 2017-02-09 06:04:36 +0100 | [diff] [blame] | 431 | sec, usec, caplen, wirelen = struct.unpack( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 432 | self._pcap_reader.endian + "IIII", hdr |
| 433 | ) |
Klement Sekera | b91017a | 2017-02-09 06:04:36 +0100 | [diff] [blame] | 434 | self._pcap_reader.f.seek(0, 2) # seek to end of file |
| 435 | end_pos = self._pcap_reader.f.tell() # get position at end |
| 436 | if end_pos >= orig_pos + len(hdr) + caplen: |
| 437 | enough_data = True # yay, we have enough data |
Klement Sekera | b91017a | 2017-02-09 06:04:36 +0100 | [diff] [blame] | 438 | self._pcap_reader.f.seek(orig_pos, 0) # restore original position |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 439 | return enough_data |
Klement Sekera | b91017a | 2017-02-09 06:04:36 +0100 | [diff] [blame] | 440 | |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 441 | def wait_for_packet(self, timeout, filter_out_fn=is_ipv6_misc): |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 442 | """ |
| 443 | Wait for next packet captured with a timeout |
| 444 | |
| 445 | :param timeout: How long to wait for the packet |
| 446 | |
| 447 | :returns: Captured packet if no packet arrived within timeout |
| 448 | :raises Exception: if no packet arrives within timeout |
| 449 | """ |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 450 | deadline = time.time() + timeout |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 451 | if self._pcap_reader is None: |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 452 | if not self.wait_for_capture_file(timeout): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 453 | raise CaptureTimeoutError( |
| 454 | "Capture file %s did not appear within timeout" % self.out_path |
| 455 | ) |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 456 | while time.time() < deadline: |
| 457 | try: |
| 458 | self._pcap_reader = PcapReader(self.out_path) |
| 459 | break |
| 460 | except: |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 461 | self.test.logger.debug( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 462 | "Exception in scapy.PcapReader(%s): %s" |
| 463 | % (self.out_path, format_exc()) |
| 464 | ) |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 465 | if not self._pcap_reader: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 466 | raise CaptureTimeoutError( |
| 467 | "Capture file %s did not appear within timeout" % self.out_path |
| 468 | ) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 469 | |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 470 | poll = False |
| 471 | if timeout > 0: |
| 472 | self.test.logger.debug("Waiting for packet") |
| 473 | else: |
| 474 | poll = True |
| 475 | self.test.logger.debug("Polling for packet") |
| 476 | while time.time() < deadline or poll: |
| 477 | if not self.verify_enough_packet_data_in_pcap(): |
Paul Vinciguerra | 0f6602c | 2019-03-10 09:10:54 -0700 | [diff] [blame] | 478 | self._test.sleep(0) # yield |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 479 | poll = False |
| 480 | continue |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 481 | p = self._pcap_reader.recv() |
| 482 | if p is not None: |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 483 | if filter_out_fn is not None and filter_out_fn(p): |
| 484 | self.test.logger.debug( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 485 | "Packet received after %ss was filtered out" |
| 486 | % (time.time() - (deadline - timeout)) |
| 487 | ) |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 488 | else: |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 489 | self.test.logger.debug( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 490 | "Packet received after %fs" |
| 491 | % (time.time() - (deadline - timeout)) |
| 492 | ) |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 493 | return p |
Paul Vinciguerra | 0f6602c | 2019-03-10 09:10:54 -0700 | [diff] [blame] | 494 | self._test.sleep(0) # yield |
Klement Sekera | acb9b8e | 2017-02-14 02:55:31 +0100 | [diff] [blame] | 495 | poll = False |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 496 | self.test.logger.debug(f"Timeout ({timeout}) - no packets received") |
| 497 | raise CaptureTimeoutError(f"Packet didn't arrive within timeout ({timeout})") |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 498 | |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 499 | def create_arp_req(self): |
| 500 | """Create ARP request applicable for this interface""" |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 501 | return Ether(dst="ff:ff:ff:ff:ff:ff", src=self.remote_mac) / ARP( |
| 502 | op=ARP.who_has, |
| 503 | pdst=self.local_ip4, |
| 504 | psrc=self.remote_ip4, |
| 505 | hwsrc=self.remote_mac, |
| 506 | ) |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 507 | |
Benoît Ganne | 2699fe2 | 2021-01-18 19:25:38 +0100 | [diff] [blame] | 508 | def create_ndp_req(self, addr=None): |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 509 | """Create NDP - NS applicable for this interface""" |
Benoît Ganne | 2699fe2 | 2021-01-18 19:25:38 +0100 | [diff] [blame] | 510 | if not addr: |
| 511 | addr = self.local_ip6 |
| 512 | nsma = in6_getnsma(inet_pton(socket.AF_INET6, addr)) |
Neale Ranns | 465a1a3 | 2017-01-07 10:04:09 -0800 | [diff] [blame] | 513 | d = inet_ntop(socket.AF_INET6, nsma) |
| 514 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 515 | return ( |
| 516 | Ether(dst=in6_getnsmac(nsma)) |
| 517 | / IPv6(dst=d, src=self.remote_ip6) |
| 518 | / ICMPv6ND_NS(tgt=addr) |
| 519 | / ICMPv6NDOptSrcLLAddr(lladdr=self.remote_mac) |
| 520 | ) |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 521 | |
| 522 | def resolve_arp(self, pg_interface=None): |
| 523 | """Resolve ARP using provided packet-generator interface |
| 524 | |
| 525 | :param pg_interface: interface used to resolve, if None then this |
| 526 | interface is used |
| 527 | |
| 528 | """ |
| 529 | if pg_interface is None: |
| 530 | pg_interface = self |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 531 | self.test.logger.info( |
| 532 | "Sending ARP request for %s on port %s" |
| 533 | % (self.local_ip4, pg_interface.name) |
| 534 | ) |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 535 | arp_req = self.create_arp_req() |
| 536 | pg_interface.add_stream(arp_req) |
| 537 | pg_interface.enable_capture() |
| 538 | self.test.pg_start() |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 539 | self.test.logger.info(self.test.vapi.cli("show trace")) |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 540 | try: |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 541 | captured_packet = pg_interface.wait_for_packet(1) |
Klement Sekera | 9225dee | 2016-12-12 08:36:58 +0100 | [diff] [blame] | 542 | except: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 543 | self.test.logger.info("No ARP received on port %s" % pg_interface.name) |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 544 | return |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 545 | arp_reply = captured_packet.copy() # keep original for exception |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 546 | try: |
| 547 | if arp_reply[ARP].op == ARP.is_at: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 548 | self.test.logger.info( |
| 549 | "VPP %s MAC address is %s " % (self.name, arp_reply[ARP].hwsrc) |
| 550 | ) |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 551 | self._local_mac = arp_reply[ARP].hwsrc |
| 552 | else: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 553 | self.test.logger.info("No ARP received on port %s" % pg_interface.name) |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 554 | except: |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 555 | self.test.logger.error( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 556 | ppp("Unexpected response to ARP request:", captured_packet) |
| 557 | ) |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 558 | raise |
| 559 | |
Benoît Ganne | 2699fe2 | 2021-01-18 19:25:38 +0100 | [diff] [blame] | 560 | def resolve_ndp(self, pg_interface=None, timeout=1, link_layer=False): |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 561 | """Resolve NDP using provided packet-generator interface |
| 562 | |
| 563 | :param pg_interface: interface used to resolve, if None then this |
| 564 | interface is used |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 565 | :param timeout: how long to wait for response before giving up |
Benoît Ganne | 2699fe2 | 2021-01-18 19:25:38 +0100 | [diff] [blame] | 566 | :param link_layer: resolve for global address if False (default) |
| 567 | or for link-layer address if True |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 568 | |
| 569 | """ |
| 570 | if pg_interface is None: |
| 571 | pg_interface = self |
Benoît Ganne | 2699fe2 | 2021-01-18 19:25:38 +0100 | [diff] [blame] | 572 | addr = self.local_ip6_ll if link_layer else self.local_ip6 |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 573 | self.test.logger.info( |
| 574 | "Sending NDP request for %s on port %s" % (addr, pg_interface.name) |
| 575 | ) |
Benoît Ganne | 2699fe2 | 2021-01-18 19:25:38 +0100 | [diff] [blame] | 576 | ndp_req = self.create_ndp_req(addr) |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 577 | pg_interface.add_stream(ndp_req) |
| 578 | pg_interface.enable_capture() |
| 579 | self.test.pg_start() |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 580 | now = time.time() |
| 581 | deadline = now + timeout |
Neale Ranns | 82a06a9 | 2016-12-08 20:05:33 +0000 | [diff] [blame] | 582 | # Enabling IPv6 on an interface can generate more than the |
| 583 | # ND reply we are looking for (namely MLD). So loop through |
| 584 | # the replies to look for want we want. |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 585 | while now < deadline: |
| 586 | try: |
| 587 | captured_packet = pg_interface.wait_for_packet( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 588 | deadline - now, filter_out_fn=None |
| 589 | ) |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 590 | except: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 591 | self.test.logger.error("Timeout while waiting for NDP response") |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 592 | raise |
| 593 | ndp_reply = captured_packet.copy() # keep original for exception |
Neale Ranns | 82a06a9 | 2016-12-08 20:05:33 +0000 | [diff] [blame] | 594 | try: |
| 595 | ndp_na = ndp_reply[ICMPv6ND_NA] |
| 596 | opt = ndp_na[ICMPv6NDOptDstLLAddr] |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 597 | self.test.logger.info( |
| 598 | "VPP %s MAC address is %s " % (self.name, opt.lladdr) |
| 599 | ) |
Neale Ranns | 82a06a9 | 2016-12-08 20:05:33 +0000 | [diff] [blame] | 600 | self._local_mac = opt.lladdr |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 601 | self.test.logger.debug(self.test.vapi.cli("show trace")) |
| 602 | # we now have the MAC we've been after |
| 603 | return |
Neale Ranns | 82a06a9 | 2016-12-08 20:05:33 +0000 | [diff] [blame] | 604 | except: |
| 605 | self.test.logger.info( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 606 | ppp("Unexpected response to NDP request:", captured_packet) |
| 607 | ) |
Klement Sekera | dab231a | 2016-12-21 08:50:14 +0100 | [diff] [blame] | 608 | now = time.time() |
| 609 | |
| 610 | self.test.logger.debug(self.test.vapi.cli("show trace")) |
| 611 | raise Exception("Timeout while waiting for NDP response") |