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