Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 1 | import os |
Klement Sekera | 778c276 | 2016-11-08 02:00:28 +0100 | [diff] [blame] | 2 | import time |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame^] | 3 | from scapy.utils import wrpcap, rdpcap, PcapReader |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 4 | from vpp_interface import VppInterface |
| 5 | |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 6 | from scapy.layers.l2 import Ether, ARP |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame^] | 7 | from scapy.layers.inet6 import IPv6, ICMPv6ND_NS, ICMPv6ND_NA,\ |
Klement Sekera | 74dcdbf | 2016-11-14 09:49:09 +0100 | [diff] [blame] | 8 | ICMPv6NDOptSrcLLAddr, ICMPv6NDOptDstLLAddr |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 9 | from util import ppp |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 10 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 11 | |
| 12 | class VppPGInterface(VppInterface): |
| 13 | """ |
| 14 | VPP packet-generator interface |
| 15 | """ |
| 16 | |
| 17 | @property |
| 18 | def pg_index(self): |
| 19 | """packet-generator interface index assigned by VPP""" |
| 20 | return self._pg_index |
| 21 | |
| 22 | @property |
| 23 | def out_path(self): |
| 24 | """pcap file path - captured packets""" |
| 25 | return self._out_path |
| 26 | |
| 27 | @property |
| 28 | def in_path(self): |
| 29 | """ pcap file path - injected packets""" |
| 30 | return self._in_path |
| 31 | |
| 32 | @property |
| 33 | def capture_cli(self): |
| 34 | """CLI string to start capture on this interface""" |
| 35 | return self._capture_cli |
| 36 | |
| 37 | @property |
| 38 | def cap_name(self): |
| 39 | """capture name for this interface""" |
| 40 | return self._cap_name |
| 41 | |
| 42 | @property |
| 43 | def input_cli(self): |
| 44 | """CLI string to load the injected packets""" |
| 45 | return self._input_cli |
| 46 | |
Klement Sekera | 778c276 | 2016-11-08 02:00:28 +0100 | [diff] [blame] | 47 | @property |
| 48 | def in_history_counter(self): |
| 49 | """Self-incrementing counter used when renaming old pcap files""" |
| 50 | v = self._in_history_counter |
| 51 | self._in_history_counter += 1 |
| 52 | return v |
| 53 | |
| 54 | @property |
| 55 | def out_history_counter(self): |
| 56 | """Self-incrementing counter used when renaming old pcap files""" |
| 57 | v = self._out_history_counter |
| 58 | self._out_history_counter += 1 |
| 59 | return v |
| 60 | |
Matej Klotton | c5bf07f | 2016-11-23 15:27:17 +0100 | [diff] [blame] | 61 | def __init__(self, test, pg_index): |
| 62 | """ Create VPP packet-generator interface """ |
| 63 | r = test.vapi.pg_create_interface(pg_index) |
| 64 | self._sw_if_index = r.sw_if_index |
| 65 | |
| 66 | super(VppPGInterface, self).__init__(test) |
| 67 | |
| 68 | self._in_history_counter = 0 |
| 69 | self._out_history_counter = 0 |
| 70 | self._pg_index = pg_index |
Klement Sekera | 74dcdbf | 2016-11-14 09:49:09 +0100 | [diff] [blame] | 71 | self._out_file = "pg%u_out.pcap" % self.pg_index |
Klement Sekera | 778c276 | 2016-11-08 02:00:28 +0100 | [diff] [blame] | 72 | self._out_path = self.test.tempdir + "/" + self._out_file |
Klement Sekera | 74dcdbf | 2016-11-14 09:49:09 +0100 | [diff] [blame] | 73 | self._in_file = "pg%u_in.pcap" % self.pg_index |
Klement Sekera | 778c276 | 2016-11-08 02:00:28 +0100 | [diff] [blame] | 74 | self._in_path = self.test.tempdir + "/" + self._in_file |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 75 | self._capture_cli = "packet-generator capture pg%u pcap %s" % ( |
| 76 | self.pg_index, self.out_path) |
| 77 | self._cap_name = "pcap%u" % self.sw_if_index |
| 78 | self._input_cli = "packet-generator new pcap %s source pg%u name %s" % ( |
| 79 | self.in_path, self.pg_index, self.cap_name) |
| 80 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 81 | def enable_capture(self): |
| 82 | """ Enable capture on this packet-generator interface""" |
| 83 | try: |
Klement Sekera | 778c276 | 2016-11-08 02:00:28 +0100 | [diff] [blame] | 84 | if os.path.isfile(self.out_path): |
| 85 | os.rename(self.out_path, |
| 86 | "%s/history.[timestamp:%f].[%s-counter:%04d].%s" % |
| 87 | (self.test.tempdir, |
| 88 | time.time(), |
| 89 | self.name, |
| 90 | self.out_history_counter, |
| 91 | self._out_file)) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 92 | except: |
| 93 | pass |
| 94 | # FIXME this should be an API, but no such exists atm |
| 95 | self.test.vapi.cli(self.capture_cli) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame^] | 96 | self._pcap_reader = None |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 97 | |
| 98 | def add_stream(self, pkts): |
| 99 | """ |
| 100 | Add a stream of packets to this packet-generator |
| 101 | |
| 102 | :param pkts: iterable packets |
| 103 | |
| 104 | """ |
| 105 | try: |
Klement Sekera | 778c276 | 2016-11-08 02:00:28 +0100 | [diff] [blame] | 106 | if os.path.isfile(self.in_path): |
| 107 | os.rename(self.in_path, |
| 108 | "%s/history.[timestamp:%f].[%s-counter:%04d].%s" % |
| 109 | (self.test.tempdir, |
| 110 | time.time(), |
| 111 | self.name, |
| 112 | self.in_history_counter, |
| 113 | self._in_file)) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 114 | except: |
| 115 | pass |
| 116 | wrpcap(self.in_path, pkts) |
| 117 | # FIXME this should be an API, but no such exists atm |
| 118 | self.test.vapi.cli(self.input_cli) |
| 119 | self.test.pg_streams.append(self.cap_name) |
| 120 | self.test.vapi.cli("trace add pg-input %d" % len(pkts)) |
| 121 | |
| 122 | def get_capture(self): |
| 123 | """ |
| 124 | Get captured packets |
| 125 | |
| 126 | :returns: iterable packets |
| 127 | """ |
| 128 | try: |
| 129 | output = rdpcap(self.out_path) |
| 130 | except IOError: # TODO |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 131 | self.test.logger.error("File %s does not exist, probably because no" |
| 132 | " packets arrived" % self.out_path) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 133 | return [] |
| 134 | return output |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 135 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame^] | 136 | def wait_for_packet(self, timeout): |
| 137 | """ |
| 138 | Wait for next packet captured with a timeout |
| 139 | |
| 140 | :param timeout: How long to wait for the packet |
| 141 | |
| 142 | :returns: Captured packet if no packet arrived within timeout |
| 143 | :raises Exception: if no packet arrives within timeout |
| 144 | """ |
| 145 | limit = time.time() + timeout |
| 146 | if self._pcap_reader is None: |
| 147 | self.test.logger.debug("Waiting for the capture file to appear") |
| 148 | while time.time() < limit: |
| 149 | if os.path.isfile(self.out_path): |
| 150 | break |
| 151 | time.sleep(0) # yield |
| 152 | if os.path.isfile(self.out_path): |
| 153 | self.test.logger.debug("Capture file appeared after %fs" % |
| 154 | (time.time() - (limit - timeout))) |
| 155 | self._pcap_reader = PcapReader(self.out_path) |
| 156 | else: |
| 157 | self.test.logger.debug("Timeout - capture file still nowhere") |
| 158 | raise Exception("Packet didn't arrive within timeout") |
| 159 | |
| 160 | self.test.logger.debug("Waiting for packet") |
| 161 | while time.time() < limit: |
| 162 | p = self._pcap_reader.recv() |
| 163 | if p is not None: |
| 164 | self.test.logger.debug("Packet received after %fs", |
| 165 | (time.time() - (limit - timeout))) |
| 166 | return p |
| 167 | time.sleep(0) # yield |
| 168 | self.test.logger.debug("Timeout - no packets received") |
| 169 | raise Exception("Packet didn't arrive within timeout") |
| 170 | |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 171 | def create_arp_req(self): |
| 172 | """Create ARP request applicable for this interface""" |
| 173 | return (Ether(dst="ff:ff:ff:ff:ff:ff", src=self.remote_mac) / |
| 174 | ARP(op=ARP.who_has, pdst=self.local_ip4, |
| 175 | psrc=self.remote_ip4, hwsrc=self.remote_mac)) |
| 176 | |
| 177 | def create_ndp_req(self): |
| 178 | """Create NDP - NS applicable for this interface""" |
| 179 | return (Ether(dst="ff:ff:ff:ff:ff:ff", src=self.remote_mac) / |
Klement Sekera | 74dcdbf | 2016-11-14 09:49:09 +0100 | [diff] [blame] | 180 | IPv6(src=self.remote_ip6, dst=self.local_ip6) / |
| 181 | ICMPv6ND_NS(tgt=self.local_ip6) / |
| 182 | ICMPv6NDOptSrcLLAddr(lladdr=self.remote_mac)) |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 183 | |
| 184 | def resolve_arp(self, pg_interface=None): |
| 185 | """Resolve ARP using provided packet-generator interface |
| 186 | |
| 187 | :param pg_interface: interface used to resolve, if None then this |
| 188 | interface is used |
| 189 | |
| 190 | """ |
| 191 | if pg_interface is None: |
| 192 | pg_interface = self |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 193 | self.test.logger.info("Sending ARP request for %s on port %s" % |
| 194 | (self.local_ip4, pg_interface.name)) |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 195 | arp_req = self.create_arp_req() |
| 196 | pg_interface.add_stream(arp_req) |
| 197 | pg_interface.enable_capture() |
| 198 | self.test.pg_start() |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 199 | self.test.logger.info(self.test.vapi.cli("show trace")) |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 200 | arp_reply = pg_interface.get_capture() |
| 201 | if arp_reply is None or len(arp_reply) == 0: |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 202 | self.test.logger.info( |
| 203 | "No ARP received on port %s" % |
| 204 | pg_interface.name) |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 205 | return |
| 206 | arp_reply = arp_reply[0] |
| 207 | # Make Dot1AD packet content recognizable to scapy |
| 208 | if arp_reply.type == 0x88a8: |
| 209 | arp_reply.type = 0x8100 |
| 210 | arp_reply = Ether(str(arp_reply)) |
| 211 | try: |
| 212 | if arp_reply[ARP].op == ARP.is_at: |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 213 | self.test.logger.info("VPP %s MAC address is %s " % |
| 214 | (self.name, arp_reply[ARP].hwsrc)) |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 215 | self._local_mac = arp_reply[ARP].hwsrc |
| 216 | else: |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 217 | self.test.logger.info( |
| 218 | "No ARP received on port %s" % |
| 219 | pg_interface.name) |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 220 | except: |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 221 | self.test.logger.error( |
| 222 | ppp("Unexpected response to ARP request:", arp_reply)) |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 223 | raise |
| 224 | |
| 225 | def resolve_ndp(self, pg_interface=None): |
| 226 | """Resolve NDP using provided packet-generator interface |
| 227 | |
| 228 | :param pg_interface: interface used to resolve, if None then this |
| 229 | interface is used |
| 230 | |
| 231 | """ |
| 232 | if pg_interface is None: |
| 233 | pg_interface = self |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 234 | self.test.logger.info("Sending NDP request for %s on port %s" % |
| 235 | (self.local_ip6, pg_interface.name)) |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 236 | ndp_req = self.create_ndp_req() |
| 237 | pg_interface.add_stream(ndp_req) |
| 238 | pg_interface.enable_capture() |
| 239 | self.test.pg_start() |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 240 | self.test.logger.info(self.test.vapi.cli("show trace")) |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 241 | ndp_reply = pg_interface.get_capture() |
| 242 | if ndp_reply is None or len(ndp_reply) == 0: |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 243 | self.test.logger.info( |
| 244 | "No NDP received on port %s" % |
| 245 | pg_interface.name) |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 246 | return |
| 247 | ndp_reply = ndp_reply[0] |
| 248 | # Make Dot1AD packet content recognizable to scapy |
| 249 | if ndp_reply.type == 0x88a8: |
| 250 | ndp_reply.type = 0x8100 |
| 251 | ndp_reply = Ether(str(ndp_reply)) |
| 252 | try: |
| 253 | ndp_na = ndp_reply[ICMPv6ND_NA] |
| 254 | opt = ndp_na[ICMPv6NDOptDstLLAddr] |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 255 | self.test.logger.info("VPP %s MAC address is %s " % |
| 256 | (self.name, opt.lladdr)) |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 257 | self._local_mac = opt.lladdr |
| 258 | except: |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 259 | self.test.logger.error( |
| 260 | ppp("Unexpected response to NDP request:", ndp_reply)) |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 261 | raise |