blob: 381dc1dad215b479d30e235ce9facf70303fa3a4 [file] [log] [blame]
Klement Sekeraf62ae122016-10-11 11:47:09 +02001import os
Klement Sekera778c2762016-11-08 02:00:28 +01002import time
Matej Klotton0178d522016-11-04 11:11:44 +01003from logging import error, info
Klement Sekeraf62ae122016-10-11 11:47:09 +02004from scapy.utils import wrpcap, rdpcap
5from vpp_interface import VppInterface
6
Matej Klotton0178d522016-11-04 11:11:44 +01007from scapy.layers.l2 import Ether, ARP
Klement Sekera74dcdbf2016-11-14 09:49:09 +01008from scapy.layers.inet6 import IPv6, ICMPv6ND_NS, ICMPv6ND_NA, \
9 ICMPv6NDOptSrcLLAddr, ICMPv6NDOptDstLLAddr
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
Klement Sekeraf62ae122016-10-11 11:47:09 +020061 def post_init_setup(self):
62 """ Perform post-init setup for super class and add our own setup """
63 super(VppPGInterface, self).post_init_setup()
Klement Sekera74dcdbf2016-11-14 09:49:09 +010064 self._out_file = "pg%u_out.pcap" % self.pg_index
Klement Sekera778c2762016-11-08 02:00:28 +010065 self._out_path = self.test.tempdir + "/" + self._out_file
Klement Sekera74dcdbf2016-11-14 09:49:09 +010066 self._in_file = "pg%u_in.pcap" % self.pg_index
Klement Sekera778c2762016-11-08 02:00:28 +010067 self._in_path = self.test.tempdir + "/" + self._in_file
Klement Sekeraf62ae122016-10-11 11:47:09 +020068 self._capture_cli = "packet-generator capture pg%u pcap %s" % (
69 self.pg_index, self.out_path)
70 self._cap_name = "pcap%u" % self.sw_if_index
71 self._input_cli = "packet-generator new pcap %s source pg%u name %s" % (
72 self.in_path, self.pg_index, self.cap_name)
73
74 def __init__(self, test, pg_index):
75 """ Create VPP packet-generator interface """
Klement Sekera778c2762016-11-08 02:00:28 +010076 self._in_history_counter = 0
77 self._out_history_counter = 0
Klement Sekeraf62ae122016-10-11 11:47:09 +020078 self._pg_index = pg_index
79 self._test = test
80 r = self.test.vapi.pg_create_interface(self.pg_index)
81 self._sw_if_index = r.sw_if_index
82 self.post_init_setup()
83
84 def enable_capture(self):
85 """ Enable capture on this packet-generator interface"""
86 try:
Klement Sekera778c2762016-11-08 02:00:28 +010087 if os.path.isfile(self.out_path):
88 os.rename(self.out_path,
89 "%s/history.[timestamp:%f].[%s-counter:%04d].%s" %
90 (self.test.tempdir,
91 time.time(),
92 self.name,
93 self.out_history_counter,
94 self._out_file))
Klement Sekeraf62ae122016-10-11 11:47:09 +020095 except:
96 pass
97 # FIXME this should be an API, but no such exists atm
98 self.test.vapi.cli(self.capture_cli)
99
100 def add_stream(self, pkts):
101 """
102 Add a stream of packets to this packet-generator
103
104 :param pkts: iterable packets
105
106 """
107 try:
Klement Sekera778c2762016-11-08 02:00:28 +0100108 if os.path.isfile(self.in_path):
109 os.rename(self.in_path,
110 "%s/history.[timestamp:%f].[%s-counter:%04d].%s" %
111 (self.test.tempdir,
112 time.time(),
113 self.name,
114 self.in_history_counter,
115 self._in_file))
Klement Sekeraf62ae122016-10-11 11:47:09 +0200116 except:
117 pass
118 wrpcap(self.in_path, pkts)
119 # FIXME this should be an API, but no such exists atm
120 self.test.vapi.cli(self.input_cli)
121 self.test.pg_streams.append(self.cap_name)
122 self.test.vapi.cli("trace add pg-input %d" % len(pkts))
123
124 def get_capture(self):
125 """
126 Get captured packets
127
128 :returns: iterable packets
129 """
130 try:
131 output = rdpcap(self.out_path)
132 except IOError: # TODO
133 error("File %s does not exist, probably because no"
134 " packets arrived" % self.out_path)
135 return []
136 return output
Matej Klotton0178d522016-11-04 11:11:44 +0100137
138 def create_arp_req(self):
139 """Create ARP request applicable for this interface"""
140 return (Ether(dst="ff:ff:ff:ff:ff:ff", src=self.remote_mac) /
141 ARP(op=ARP.who_has, pdst=self.local_ip4,
142 psrc=self.remote_ip4, hwsrc=self.remote_mac))
143
144 def create_ndp_req(self):
145 """Create NDP - NS applicable for this interface"""
146 return (Ether(dst="ff:ff:ff:ff:ff:ff", src=self.remote_mac) /
Klement Sekera74dcdbf2016-11-14 09:49:09 +0100147 IPv6(src=self.remote_ip6, dst=self.local_ip6) /
148 ICMPv6ND_NS(tgt=self.local_ip6) /
149 ICMPv6NDOptSrcLLAddr(lladdr=self.remote_mac))
Matej Klotton0178d522016-11-04 11:11:44 +0100150
151 def resolve_arp(self, pg_interface=None):
152 """Resolve ARP using provided packet-generator interface
153
154 :param pg_interface: interface used to resolve, if None then this
155 interface is used
156
157 """
158 if pg_interface is None:
159 pg_interface = self
160 info("Sending ARP request for %s on port %s" %
161 (self.local_ip4, pg_interface.name))
162 arp_req = self.create_arp_req()
163 pg_interface.add_stream(arp_req)
164 pg_interface.enable_capture()
165 self.test.pg_start()
166 info(self.test.vapi.cli("show trace"))
167 arp_reply = pg_interface.get_capture()
168 if arp_reply is None or len(arp_reply) == 0:
169 info("No ARP received on port %s" % pg_interface.name)
170 return
171 arp_reply = arp_reply[0]
172 # Make Dot1AD packet content recognizable to scapy
173 if arp_reply.type == 0x88a8:
174 arp_reply.type = 0x8100
175 arp_reply = Ether(str(arp_reply))
176 try:
177 if arp_reply[ARP].op == ARP.is_at:
178 info("VPP %s MAC address is %s " %
179 (self.name, arp_reply[ARP].hwsrc))
180 self._local_mac = arp_reply[ARP].hwsrc
181 else:
182 info("No ARP received on port %s" % pg_interface.name)
183 except:
184 error("Unexpected response to ARP request:")
185 error(arp_reply.show())
186 raise
187
188 def resolve_ndp(self, pg_interface=None):
189 """Resolve NDP using provided packet-generator interface
190
191 :param pg_interface: interface used to resolve, if None then this
192 interface is used
193
194 """
195 if pg_interface is None:
196 pg_interface = self
197 info("Sending NDP request for %s on port %s" %
198 (self.local_ip6, pg_interface.name))
199 ndp_req = self.create_ndp_req()
200 pg_interface.add_stream(ndp_req)
201 pg_interface.enable_capture()
202 self.test.pg_start()
203 info(self.test.vapi.cli("show trace"))
204 ndp_reply = pg_interface.get_capture()
205 if ndp_reply is None or len(ndp_reply) == 0:
206 info("No NDP received on port %s" % pg_interface.name)
207 return
208 ndp_reply = ndp_reply[0]
209 # Make Dot1AD packet content recognizable to scapy
210 if ndp_reply.type == 0x88a8:
211 ndp_reply.type = 0x8100
212 ndp_reply = Ether(str(ndp_reply))
213 try:
214 ndp_na = ndp_reply[ICMPv6ND_NA]
215 opt = ndp_na[ICMPv6NDOptDstLLAddr]
216 info("VPP %s MAC address is %s " %
217 (self.name, opt.lladdr))
218 self._local_mac = opt.lladdr
219 except:
220 error("Unexpected response to NDP request:")
221 error(ndp_reply.show())
222 raise