blob: c596ab5abf709b799d6e033a865416fa71a0d1ed [file] [log] [blame]
Klement Sekeraf62ae122016-10-11 11:47:09 +02001from abc import abstractmethod, ABCMeta
2import socket
3from logging import info, error
4from scapy.layers.l2 import Ether, ARP
5
6from scapy.layers.inet6 import IPv6, ICMPv6ND_NS, ICMPv6ND_NA, ICMPv6NDOptSrcLLAddr, ICMPv6NDOptDstLLAddr
7
8
9class VppInterface(object):
10 """
11 Generic VPP interface
12 """
13 __metaclass__ = ABCMeta
14
15 @property
16 def sw_if_index(self):
17 """Interface index assigned by VPP"""
18 return self._sw_if_index
19
20 @property
21 def remote_mac(self):
22 """MAC-address of the remote interface "connected" to this interface"""
23 return self._remote_mac
24
25 @property
26 def local_mac(self):
27 """MAC-address of the VPP interface"""
28 return self._local_mac
29
30 @property
31 def local_ip4(self):
32 """Local IPv4 address on VPP interface (string)"""
33 return self._local_ip4
34
35 @property
36 def local_ip4n(self):
37 """Local IPv4 address - raw, suitable as API parameter"""
38 return self._local_ip4n
39
40 @property
41 def remote_ip4(self):
42 """IPv4 address of remote peer "connected" to this interface"""
43 return self._remote_ip4
44
45 @property
46 def remote_ip4n(self):
47 """IPv4 address of remote peer - raw, suitable as API parameter"""
48 return self._remote_ip4n
49
50 @property
51 def local_ip6(self):
52 """Local IPv6 address on VPP interface (string)"""
53 return self._local_ip6
54
55 @property
56 def local_ip6n(self):
57 """Local IPv6 address - raw, suitable as API parameter"""
58 return self._local_ip6n
59
60 @property
61 def remote_ip6(self):
62 """IPv6 address of remote peer "connected" to this interface"""
63 return self._remote_ip6
64
65 @property
66 def remote_ip6n(self):
67 """IPv6 address of remote peer - raw, suitable as API parameter"""
68 return self._remote_ip6n
69
70 @property
71 def name(self):
72 """Name of the interface"""
73 return self._name
74
75 @property
76 def dump(self):
77 """Raw result of sw_interface_dump for this interface"""
78 return self._dump
79
80 @property
81 def test(self):
82 """Test case creating this interface"""
83 return self._test
84
85 def post_init_setup(self):
86 """Additional setup run after creating an interface object"""
87 self._remote_mac = "02:00:00:00:ff:%02x" % self.sw_if_index
88
89 self._local_ip4 = "172.16.%u.1" % self.sw_if_index
90 self._local_ip4n = socket.inet_pton(socket.AF_INET, self.local_ip4)
91 self._remote_ip4 = "172.16.%u.2" % self.sw_if_index
92 self._remote_ip4n = socket.inet_pton(socket.AF_INET, self.remote_ip4)
93
94 self._local_ip6 = "fd01:%u::1" % self.sw_if_index
95 self._local_ip6n = socket.inet_pton(socket.AF_INET6, self.local_ip6)
96 self._remote_ip6 = "fd01:%u::2" % self.sw_if_index
97 self._remote_ip6n = socket.inet_pton(socket.AF_INET6, self.remote_ip6)
98
99 r = self.test.vapi.sw_interface_dump()
100 for intf in r:
101 if intf.sw_if_index == self.sw_if_index:
102 self._name = intf.interface_name.split(b'\0', 1)[0]
103 self._local_mac = ':'.join(intf.l2_address.encode('hex')[i:i + 2]
104 for i in range(0, 12, 2))
105 self._dump = intf
106 break
107 else:
108 raise Exception(
109 "Could not find interface with sw_if_index %d "
110 "in interface dump %s" %
111 (self.sw_if_index, repr(r)))
112
113 @abstractmethod
114 def __init__(self, test, index):
115 self._test = test
116 self.post_init_setup()
117 info("New %s, MAC=%s, remote_ip4=%s, local_ip4=%s" %
118 (self.__name__, self.remote_mac, self.remote_ip4, self.local_ip4))
119
120 def config_ip4(self):
121 """Configure IPv4 address on the VPP interface"""
122 addr = self.local_ip4n
123 addr_len = 24
124 self.test.vapi.sw_interface_add_del_address(
125 self.sw_if_index, addr, addr_len)
126
127 def config_ip6(self):
128 """Configure IPv6 address on the VPP interface"""
129 addr = self._local_ip6n
130 addr_len = 64
131 self.test.vapi.sw_interface_add_del_address(
132 self.sw_if_index, addr, addr_len, is_ipv6=1)
133
134 def disable_ipv6_ra(self):
135 """Configure IPv6 RA suppress on the VPP interface"""
136 self.test.vapi.sw_interface_ra_suppress(self.sw_if_index)
137
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 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
222
223 def admin_up(self):
224 """ Put interface ADMIN-UP """
225 self.test.vapi.sw_interface_set_flags(self.sw_if_index, admin_up_down=1)
226
227 def add_sub_if(self, sub_if):
228 """
229 Register a sub-interface with this interface
230
231 :param sub_if: sub-interface
232
233 """
234 if not hasattr(self, 'sub_if'):
235 self.sub_if = sub_if
236 else:
237 if isinstance(self.sub_if, list):
238 self.sub_if.append(sub_if)
239 else:
240 self.sub_if = sub_if