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 post_init_setup(self):
62 """ Perform post-init setup for super class and add our own setup """
63 super(VppPGInterface, self).post_init_setup()
64 self._out_file = "pg%u_out.pcap" % self.pg_index
65 self._out_path = self.test.tempdir + "/" + self._out_file
66 self._in_file = "pg%u_in.pcap" % self.pg_index
67 self._in_path = self.test.tempdir + "/" + self._in_file
68 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)
74 def __init__(self, test, pg_index):
75 """ Create VPP packet-generator interface """
76 self._in_history_counter = 0
77 self._out_history_counter = 0
78 self._pg_index = pg_index
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()
84 def enable_capture(self):
85 """ Enable capture on this packet-generator interface"""
87 if os.path.isfile(self.out_path):
88 os.rename(self.out_path,
89 "%s/history.[timestamp:%f].[%s-counter:%04d].%s" %
93 self.out_history_counter,
97 # FIXME this should be an API, but no such exists atm
98 self.test.vapi.cli(self.capture_cli)
100 def add_stream(self, pkts):
102 Add a stream of packets to this packet-generator
104 :param pkts: iterable packets
108 if os.path.isfile(self.in_path):
109 os.rename(self.in_path,
110 "%s/history.[timestamp:%f].[%s-counter:%04d].%s" %
114 self.in_history_counter,
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))
124 def get_capture(self):
128 :returns: iterable packets
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)
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))
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) /
147 IPv6(src=self.remote_ip6, dst=self.local_ip6) /
148 ICMPv6ND_NS(tgt=self.local_ip6) /
149 ICMPv6NDOptSrcLLAddr(lladdr=self.remote_mac))
151 def resolve_arp(self, pg_interface=None):
152 """Resolve ARP using provided packet-generator interface
154 :param pg_interface: interface used to resolve, if None then this
158 if pg_interface is None:
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()
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)
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))
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
182 info("No ARP received on port %s" % pg_interface.name)
184 error("Unexpected response to ARP request:")
185 error(arp_reply.show())
188 def resolve_ndp(self, pg_interface=None):
189 """Resolve NDP using provided packet-generator interface
191 :param pg_interface: interface used to resolve, if None then this
195 if pg_interface is None:
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()
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)
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))
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
220 error("Unexpected response to NDP request:")
221 error(ndp_reply.show())