blob: 81c7192e07efa63499753383adb5286af9cc24fe [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
8from scapy.layers.inet6 import IPv6, ICMPv6ND_NS, ICMPv6ND_NA, ICMPv6NDOptSrcLLAddr, ICMPv6NDOptDstLLAddr
9
Klement Sekeraf62ae122016-10-11 11:47:09 +020010
11class VppPGInterface(VppInterface):
12 """
13 VPP packet-generator interface
14 """
15
16 @property
17 def pg_index(self):
18 """packet-generator interface index assigned by VPP"""
19 return self._pg_index
20
21 @property
22 def out_path(self):
23 """pcap file path - captured packets"""
24 return self._out_path
25
26 @property
27 def in_path(self):
28 """ pcap file path - injected packets"""
29 return self._in_path
30
31 @property
32 def capture_cli(self):
33 """CLI string to start capture on this interface"""
34 return self._capture_cli
35
36 @property
37 def cap_name(self):
38 """capture name for this interface"""
39 return self._cap_name
40
41 @property
42 def input_cli(self):
43 """CLI string to load the injected packets"""
44 return self._input_cli
45
Klement Sekera778c2762016-11-08 02:00:28 +010046 @property
47 def in_history_counter(self):
48 """Self-incrementing counter used when renaming old pcap files"""
49 v = self._in_history_counter
50 self._in_history_counter += 1
51 return v
52
53 @property
54 def out_history_counter(self):
55 """Self-incrementing counter used when renaming old pcap files"""
56 v = self._out_history_counter
57 self._out_history_counter += 1
58 return v
59
Klement Sekeraf62ae122016-10-11 11:47:09 +020060 def post_init_setup(self):
61 """ Perform post-init setup for super class and add our own setup """
62 super(VppPGInterface, self).post_init_setup()
Klement Sekera778c2762016-11-08 02:00:28 +010063 self._out_file = "pg%u_out.pcap" % self.sw_if_index
64 self._out_path = self.test.tempdir + "/" + self._out_file
65 self._in_file = "pg%u_in.pcap" % self.sw_if_index
66 self._in_path = self.test.tempdir + "/" + self._in_file
Klement Sekeraf62ae122016-10-11 11:47:09 +020067 self._capture_cli = "packet-generator capture pg%u pcap %s" % (
68 self.pg_index, self.out_path)
69 self._cap_name = "pcap%u" % self.sw_if_index
70 self._input_cli = "packet-generator new pcap %s source pg%u name %s" % (
71 self.in_path, self.pg_index, self.cap_name)
72
73 def __init__(self, test, pg_index):
74 """ Create VPP packet-generator interface """
Klement Sekera778c2762016-11-08 02:00:28 +010075 self._in_history_counter = 0
76 self._out_history_counter = 0
Klement Sekeraf62ae122016-10-11 11:47:09 +020077 self._pg_index = pg_index
78 self._test = test
79 r = self.test.vapi.pg_create_interface(self.pg_index)
80 self._sw_if_index = r.sw_if_index
81 self.post_init_setup()
82
83 def enable_capture(self):
84 """ Enable capture on this packet-generator interface"""
85 try:
Klement Sekera778c2762016-11-08 02:00:28 +010086 if os.path.isfile(self.out_path):
87 os.rename(self.out_path,
88 "%s/history.[timestamp:%f].[%s-counter:%04d].%s" %
89 (self.test.tempdir,
90 time.time(),
91 self.name,
92 self.out_history_counter,
93 self._out_file))
Klement Sekeraf62ae122016-10-11 11:47:09 +020094 except:
95 pass
96 # FIXME this should be an API, but no such exists atm
97 self.test.vapi.cli(self.capture_cli)
98
99 def add_stream(self, pkts):
100 """
101 Add a stream of packets to this packet-generator
102
103 :param pkts: iterable packets
104
105 """
106 try:
Klement Sekera778c2762016-11-08 02:00:28 +0100107 if os.path.isfile(self.in_path):
108 os.rename(self.in_path,
109 "%s/history.[timestamp:%f].[%s-counter:%04d].%s" %
110 (self.test.tempdir,
111 time.time(),
112 self.name,
113 self.in_history_counter,
114 self._in_file))
Klement Sekeraf62ae122016-10-11 11:47:09 +0200115 except:
116 pass
117 wrpcap(self.in_path, pkts)
118 # FIXME this should be an API, but no such exists atm
119 self.test.vapi.cli(self.input_cli)
120 self.test.pg_streams.append(self.cap_name)
121 self.test.vapi.cli("trace add pg-input %d" % len(pkts))
122
123 def get_capture(self):
124 """
125 Get captured packets
126
127 :returns: iterable packets
128 """
129 try:
130 output = rdpcap(self.out_path)
131 except IOError: # TODO
132 error("File %s does not exist, probably because no"
133 " packets arrived" % self.out_path)
134 return []
135 return output
Matej Klotton0178d522016-11-04 11:11:44 +0100136
137 def create_arp_req(self):
138 """Create ARP request applicable for this interface"""
139 return (Ether(dst="ff:ff:ff:ff:ff:ff", src=self.remote_mac) /
140 ARP(op=ARP.who_has, pdst=self.local_ip4,
141 psrc=self.remote_ip4, hwsrc=self.remote_mac))
142
143 def create_ndp_req(self):
144 """Create NDP - NS applicable for this interface"""
145 return (Ether(dst="ff:ff:ff:ff:ff:ff", src=self.remote_mac) /
146 IPv6(src=self.remote_ip6, dst=self.local_ip6) /
147 ICMPv6ND_NS(tgt=self.local_ip6) /
148 ICMPv6NDOptSrcLLAddr(lladdr=self.remote_mac))
149
150 def resolve_arp(self, pg_interface=None):
151 """Resolve ARP using provided packet-generator interface
152
153 :param pg_interface: interface used to resolve, if None then this
154 interface is used
155
156 """
157 if pg_interface is None:
158 pg_interface = self
159 info("Sending ARP request for %s on port %s" %
160 (self.local_ip4, pg_interface.name))
161 arp_req = self.create_arp_req()
162 pg_interface.add_stream(arp_req)
163 pg_interface.enable_capture()
164 self.test.pg_start()
165 info(self.test.vapi.cli("show trace"))
166 arp_reply = pg_interface.get_capture()
167 if arp_reply is None or len(arp_reply) == 0:
168 info("No ARP received on port %s" % pg_interface.name)
169 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:
177 info("VPP %s MAC address is %s " %
178 (self.name, arp_reply[ARP].hwsrc))
179 self._local_mac = arp_reply[ARP].hwsrc
180 else:
181 info("No ARP received on port %s" % pg_interface.name)
182 except:
183 error("Unexpected response to ARP request:")
184 error(arp_reply.show())
185 raise
186
187 def resolve_ndp(self, pg_interface=None):
188 """Resolve NDP using provided packet-generator interface
189
190 :param pg_interface: interface used to resolve, if None then this
191 interface is used
192
193 """
194 if pg_interface is None:
195 pg_interface = self
196 info("Sending NDP request for %s on port %s" %
197 (self.local_ip6, pg_interface.name))
198 ndp_req = self.create_ndp_req()
199 pg_interface.add_stream(ndp_req)
200 pg_interface.enable_capture()
201 self.test.pg_start()
202 info(self.test.vapi.cli("show trace"))
203 ndp_reply = pg_interface.get_capture()
204 if ndp_reply is None or len(ndp_reply) == 0:
205 info("No NDP received on port %s" % pg_interface.name)
206 return
207 ndp_reply = ndp_reply[0]
208 # Make Dot1AD packet content recognizable to scapy
209 if ndp_reply.type == 0x88a8:
210 ndp_reply.type = 0x8100
211 ndp_reply = Ether(str(ndp_reply))
212 try:
213 ndp_na = ndp_reply[ICMPv6ND_NA]
214 opt = ndp_na[ICMPv6NDOptDstLLAddr]
215 info("VPP %s MAC address is %s " %
216 (self.name, opt.lladdr))
217 self._local_mac = opt.lladdr
218 except:
219 error("Unexpected response to NDP request:")
220 error(ndp_reply.show())
221 raise