3 from logging import error, info
4 from scapy.utils import wrpcap, rdpcap
5 from vpp_interface import VppInterface
7 from scapy.layers.l2 import Ether, ARP
8 from scapy.layers.inet6 import IPv6, ICMPv6ND_NS, ICMPv6ND_NA, \
9 ICMPv6NDOptSrcLLAddr, ICMPv6NDOptDstLLAddr
12 class VppPGInterface(VppInterface):
14 VPP packet-generator interface
19 """packet-generator interface index assigned by VPP"""
24 """pcap file path - captured packets"""
29 """ pcap file path - injected packets"""
33 def capture_cli(self):
34 """CLI string to start capture on this interface"""
35 return self._capture_cli
39 """capture name for this interface"""
44 """CLI string to load the injected packets"""
45 return self._input_cli
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
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
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
66 super(VppPGInterface, self).__init__(test)
68 self._in_history_counter = 0
69 self._out_history_counter = 0
70 self._pg_index = pg_index
71 self._out_file = "pg%u_out.pcap" % self.pg_index
72 self._out_path = self.test.tempdir + "/" + self._out_file
73 self._in_file = "pg%u_in.pcap" % self.pg_index
74 self._in_path = self.test.tempdir + "/" + self._in_file
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)
81 def enable_capture(self):
82 """ Enable capture on this packet-generator interface"""
84 if os.path.isfile(self.out_path):
85 os.rename(self.out_path,
86 "%s/history.[timestamp:%f].[%s-counter:%04d].%s" %
90 self.out_history_counter,
94 # FIXME this should be an API, but no such exists atm
95 self.test.vapi.cli(self.capture_cli)
97 def add_stream(self, pkts):
99 Add a stream of packets to this packet-generator
101 :param pkts: iterable packets
105 if os.path.isfile(self.in_path):
106 os.rename(self.in_path,
107 "%s/history.[timestamp:%f].[%s-counter:%04d].%s" %
111 self.in_history_counter,
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))
121 def get_capture(self):
125 :returns: iterable packets
128 output = rdpcap(self.out_path)
129 except IOError: # TODO
130 error("File %s does not exist, probably because no"
131 " packets arrived" % self.out_path)
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))
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) /
144 IPv6(src=self.remote_ip6, dst=self.local_ip6) /
145 ICMPv6ND_NS(tgt=self.local_ip6) /
146 ICMPv6NDOptSrcLLAddr(lladdr=self.remote_mac))
148 def resolve_arp(self, pg_interface=None):
149 """Resolve ARP using provided packet-generator interface
151 :param pg_interface: interface used to resolve, if None then this
155 if pg_interface is None:
157 info("Sending ARP request for %s on port %s" %
158 (self.local_ip4, pg_interface.name))
159 arp_req = self.create_arp_req()
160 pg_interface.add_stream(arp_req)
161 pg_interface.enable_capture()
163 info(self.test.vapi.cli("show trace"))
164 arp_reply = pg_interface.get_capture()
165 if arp_reply is None or len(arp_reply) == 0:
166 info("No ARP received on port %s" % pg_interface.name)
168 arp_reply = arp_reply[0]
169 # Make Dot1AD packet content recognizable to scapy
170 if arp_reply.type == 0x88a8:
171 arp_reply.type = 0x8100
172 arp_reply = Ether(str(arp_reply))
174 if arp_reply[ARP].op == ARP.is_at:
175 info("VPP %s MAC address is %s " %
176 (self.name, arp_reply[ARP].hwsrc))
177 self._local_mac = arp_reply[ARP].hwsrc
179 info("No ARP received on port %s" % pg_interface.name)
181 error("Unexpected response to ARP request:")
182 error(arp_reply.show())
185 def resolve_ndp(self, pg_interface=None):
186 """Resolve NDP using provided packet-generator interface
188 :param pg_interface: interface used to resolve, if None then this
192 if pg_interface is None:
194 info("Sending NDP request for %s on port %s" %
195 (self.local_ip6, pg_interface.name))
196 ndp_req = self.create_ndp_req()
197 pg_interface.add_stream(ndp_req)
198 pg_interface.enable_capture()
200 info(self.test.vapi.cli("show trace"))
201 ndp_reply = pg_interface.get_capture()
202 if ndp_reply is None or len(ndp_reply) == 0:
203 info("No NDP received on port %s" % pg_interface.name)
205 ndp_reply = ndp_reply[0]
206 # Make Dot1AD packet content recognizable to scapy
207 if ndp_reply.type == 0x88a8:
208 ndp_reply.type = 0x8100
209 ndp_reply = Ether(str(ndp_reply))
211 ndp_na = ndp_reply[ICMPv6ND_NA]
212 opt = ndp_na[ICMPv6NDOptDstLLAddr]
213 info("VPP %s MAC address is %s " %
214 (self.name, opt.lladdr))
215 self._local_mac = opt.lladdr
217 error("Unexpected response to NDP request:")
218 error(ndp_reply.show())