blob: 533c4603a2eddf9c82790e92ceae1766b9a40738 [file] [log] [blame]
Klement Sekeraf62ae122016-10-11 11:47:09 +02001import os
Klement Sekera778c2762016-11-08 02:00:28 +01002import time
Klement Sekeraf62ae122016-10-11 11:47:09 +02003from scapy.utils import wrpcap, rdpcap
4from vpp_interface import VppInterface
5
Matej Klotton0178d522016-11-04 11:11:44 +01006from scapy.layers.l2 import Ether, ARP
Klement Sekera74dcdbf2016-11-14 09:49:09 +01007from scapy.layers.inet6 import IPv6, ICMPv6ND_NS, ICMPv6ND_NA, \
8 ICMPv6NDOptSrcLLAddr, ICMPv6NDOptDstLLAddr
Klement Sekera7bb873a2016-11-18 07:38:42 +01009from util import ppp
Matej Klotton0178d522016-11-04 11:11:44 +010010
Klement Sekeraf62ae122016-10-11 11:47:09 +020011
12class 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 Sekera778c2762016-11-08 02:00:28 +010047 @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 Klottonc5bf07f2016-11-23 15:27:17 +010061 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 Sekera74dcdbf2016-11-14 09:49:09 +010071 self._out_file = "pg%u_out.pcap" % self.pg_index
Klement Sekera778c2762016-11-08 02:00:28 +010072 self._out_path = self.test.tempdir + "/" + self._out_file
Klement Sekera74dcdbf2016-11-14 09:49:09 +010073 self._in_file = "pg%u_in.pcap" % self.pg_index
Klement Sekera778c2762016-11-08 02:00:28 +010074 self._in_path = self.test.tempdir + "/" + self._in_file
Klement Sekeraf62ae122016-10-11 11:47:09 +020075 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 Sekeraf62ae122016-10-11 11:47:09 +020081 def enable_capture(self):
82 """ Enable capture on this packet-generator interface"""
83 try:
Klement Sekera778c2762016-11-08 02:00:28 +010084 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 Sekeraf62ae122016-10-11 11:47:09 +020092 except:
93 pass
94 # FIXME this should be an API, but no such exists atm
95 self.test.vapi.cli(self.capture_cli)
96
97 def add_stream(self, pkts):
98 """
99 Add a stream of packets to this packet-generator
100
101 :param pkts: iterable packets
102
103 """
104 try:
Klement Sekera778c2762016-11-08 02:00:28 +0100105 if os.path.isfile(self.in_path):
106 os.rename(self.in_path,
107 "%s/history.[timestamp:%f].[%s-counter:%04d].%s" %
108 (self.test.tempdir,
109 time.time(),
110 self.name,
111 self.in_history_counter,
112 self._in_file))
Klement Sekeraf62ae122016-10-11 11:47:09 +0200113 except:
114 pass
115 wrpcap(self.in_path, pkts)
116 # FIXME this should be an API, but no such exists atm
117 self.test.vapi.cli(self.input_cli)
118 self.test.pg_streams.append(self.cap_name)
119 self.test.vapi.cli("trace add pg-input %d" % len(pkts))
120
121 def get_capture(self):
122 """
123 Get captured packets
124
125 :returns: iterable packets
126 """
127 try:
128 output = rdpcap(self.out_path)
129 except IOError: # TODO
Klement Sekera7bb873a2016-11-18 07:38:42 +0100130 self.test.logger.error("File %s does not exist, probably because no"
131 " packets arrived" % self.out_path)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200132 return []
133 return output
Matej Klotton0178d522016-11-04 11:11:44 +0100134
135 def create_arp_req(self):
136 """Create ARP request applicable for this interface"""
137 return (Ether(dst="ff:ff:ff:ff:ff:ff", src=self.remote_mac) /
138 ARP(op=ARP.who_has, pdst=self.local_ip4,
139 psrc=self.remote_ip4, hwsrc=self.remote_mac))
140
141 def create_ndp_req(self):
142 """Create NDP - NS applicable for this interface"""
143 return (Ether(dst="ff:ff:ff:ff:ff:ff", src=self.remote_mac) /
Klement Sekera74dcdbf2016-11-14 09:49:09 +0100144 IPv6(src=self.remote_ip6, dst=self.local_ip6) /
145 ICMPv6ND_NS(tgt=self.local_ip6) /
146 ICMPv6NDOptSrcLLAddr(lladdr=self.remote_mac))
Matej Klotton0178d522016-11-04 11:11:44 +0100147
148 def resolve_arp(self, pg_interface=None):
149 """Resolve ARP using provided packet-generator interface
150
151 :param pg_interface: interface used to resolve, if None then this
152 interface is used
153
154 """
155 if pg_interface is None:
156 pg_interface = self
Klement Sekera7bb873a2016-11-18 07:38:42 +0100157 self.test.logger.info("Sending ARP request for %s on port %s" %
158 (self.local_ip4, pg_interface.name))
Matej Klotton0178d522016-11-04 11:11:44 +0100159 arp_req = self.create_arp_req()
160 pg_interface.add_stream(arp_req)
161 pg_interface.enable_capture()
162 self.test.pg_start()
Klement Sekera7bb873a2016-11-18 07:38:42 +0100163 self.test.logger.info(self.test.vapi.cli("show trace"))
Matej Klotton0178d522016-11-04 11:11:44 +0100164 arp_reply = pg_interface.get_capture()
165 if arp_reply is None or len(arp_reply) == 0:
Klement Sekera7bb873a2016-11-18 07:38:42 +0100166 self.test.logger.info(
167 "No ARP received on port %s" %
168 pg_interface.name)
Matej Klotton0178d522016-11-04 11:11:44 +0100169 return
170 arp_reply = arp_reply[0]
171 # Make Dot1AD packet content recognizable to scapy
172 if arp_reply.type == 0x88a8:
173 arp_reply.type = 0x8100
174 arp_reply = Ether(str(arp_reply))
175 try:
176 if arp_reply[ARP].op == ARP.is_at:
Klement Sekera7bb873a2016-11-18 07:38:42 +0100177 self.test.logger.info("VPP %s MAC address is %s " %
178 (self.name, arp_reply[ARP].hwsrc))
Matej Klotton0178d522016-11-04 11:11:44 +0100179 self._local_mac = arp_reply[ARP].hwsrc
180 else:
Klement Sekera7bb873a2016-11-18 07:38:42 +0100181 self.test.logger.info(
182 "No ARP received on port %s" %
183 pg_interface.name)
Matej Klotton0178d522016-11-04 11:11:44 +0100184 except:
Klement Sekera7bb873a2016-11-18 07:38:42 +0100185 self.test.logger.error(
186 ppp("Unexpected response to ARP request:", arp_reply))
Matej Klotton0178d522016-11-04 11:11:44 +0100187 raise
188
189 def resolve_ndp(self, pg_interface=None):
190 """Resolve NDP using provided packet-generator interface
191
192 :param pg_interface: interface used to resolve, if None then this
193 interface is used
194
195 """
196 if pg_interface is None:
197 pg_interface = self
Klement Sekera7bb873a2016-11-18 07:38:42 +0100198 self.test.logger.info("Sending NDP request for %s on port %s" %
199 (self.local_ip6, pg_interface.name))
Matej Klotton0178d522016-11-04 11:11:44 +0100200 ndp_req = self.create_ndp_req()
201 pg_interface.add_stream(ndp_req)
202 pg_interface.enable_capture()
203 self.test.pg_start()
Klement Sekera7bb873a2016-11-18 07:38:42 +0100204 self.test.logger.info(self.test.vapi.cli("show trace"))
Matej Klotton0178d522016-11-04 11:11:44 +0100205 ndp_reply = pg_interface.get_capture()
206 if ndp_reply is None or len(ndp_reply) == 0:
Klement Sekera7bb873a2016-11-18 07:38:42 +0100207 self.test.logger.info(
208 "No NDP received on port %s" %
209 pg_interface.name)
Matej Klotton0178d522016-11-04 11:11:44 +0100210 return
211 ndp_reply = ndp_reply[0]
212 # Make Dot1AD packet content recognizable to scapy
213 if ndp_reply.type == 0x88a8:
214 ndp_reply.type = 0x8100
215 ndp_reply = Ether(str(ndp_reply))
216 try:
217 ndp_na = ndp_reply[ICMPv6ND_NA]
218 opt = ndp_na[ICMPv6NDOptDstLLAddr]
Klement Sekera7bb873a2016-11-18 07:38:42 +0100219 self.test.logger.info("VPP %s MAC address is %s " %
220 (self.name, opt.lladdr))
Matej Klotton0178d522016-11-04 11:11:44 +0100221 self._local_mac = opt.lladdr
222 except:
Klement Sekera7bb873a2016-11-18 07:38:42 +0100223 self.test.logger.error(
224 ppp("Unexpected response to NDP request:", ndp_reply))
Matej Klotton0178d522016-11-04 11:11:44 +0100225 raise