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