1 from abc import abstractmethod, ABCMeta
4 from util import Host, mk_ll_addr
5 from vpp_neighbor import VppNeighbor
8 class VppInterface(object):
9 """Generic VPP interface."""
10 __metaclass__ = ABCMeta
13 def sw_if_index(self):
14 """Interface index assigned by VPP."""
15 return self._sw_if_index
19 """MAC-address of the remote interface "connected" to this interface"""
20 return self._remote_hosts[0].mac
24 """MAC-address of the VPP interface."""
25 return self._local_mac
29 """Local IPv4 address on VPP interface (string)."""
30 return self._local_ip4
34 """Local IPv4 address - raw, suitable as API parameter."""
35 return socket.inet_pton(socket.AF_INET, self._local_ip4)
39 """IPv4 address of remote peer "connected" to this interface."""
40 return self._remote_hosts[0].ip4
43 def remote_ip4n(self):
44 """IPv4 address of remote peer - raw, suitable as API parameter."""
45 return socket.inet_pton(socket.AF_INET, self.remote_ip4)
49 """Local IPv6 address on VPP interface (string)."""
50 return self._local_ip6
54 """Local IPv6 address - raw, suitable as API parameter."""
55 return socket.inet_pton(socket.AF_INET6, self.local_ip6)
58 def local_ip6_ll(self):
59 """Local IPv6 linnk-local address on VPP interface (string)."""
60 return self._local_ip6_ll
63 def local_ip6n_ll(self):
64 """Local IPv6 link-local address - raw, suitable as API parameter."""
65 return self.local_ip6n_ll
69 """IPv6 address of remote peer "connected" to this interface."""
70 return self._remote_hosts[0].ip6
73 def remote_ip6n(self):
74 """IPv6 address of remote peer - raw, suitable as API parameter"""
75 return socket.inet_pton(socket.AF_INET6, self.remote_ip6)
79 """Name of the interface."""
84 """RAW result of sw_interface_dump for this interface."""
89 """Test case creating this interface."""
93 def remote_hosts(self):
94 """Remote hosts list"""
95 return self._remote_hosts
98 def remote_hosts(self, value):
100 :param list value: List of remote hosts.
102 self._remote_hosts = value
103 self._hosts_by_mac = {}
104 self._hosts_by_ip4 = {}
105 self._hosts_by_ip6 = {}
106 for host in self._remote_hosts:
107 self._hosts_by_mac[host.mac] = host
108 self._hosts_by_ip4[host.ip4] = host
109 self._hosts_by_ip6[host.ip6] = host
111 def host_by_mac(self, mac):
113 :param mac: MAC address to find host by.
114 :return: Host object assigned to interface.
116 return self._hosts_by_mac[mac]
118 def host_by_ip4(self, ip):
120 :param ip: IPv4 address to find host by.
121 :return: Host object assigned to interface.
123 return self._hosts_by_ip4[ip]
125 def host_by_ip6(self, ip):
127 :param ip: IPv6 address to find host by.
128 :return: Host object assigned to interface.
130 return self._hosts_by_ip6[ip]
132 def generate_remote_hosts(self, count=1):
133 """Generate and add remote hosts for the interface.
135 :param int count: Number of generated remote hosts.
137 self._remote_hosts = []
138 self._hosts_by_mac = {}
139 self._hosts_by_ip4 = {}
140 self._hosts_by_ip6 = {}
142 2, count + 2): # 0: network address, 1: local vpp address
143 mac = "02:%02x:00:00:ff:%02x" % (self.sw_if_index, i)
144 ip4 = "172.16.%u.%u" % (self.sw_if_index, i)
145 ip6 = "fd01:%x::%x" % (self.sw_if_index, i)
146 ip6_ll = mk_ll_addr(mac)
147 host = Host(mac, ip4, ip6, ip6_ll)
148 self._remote_hosts.append(host)
149 self._hosts_by_mac[mac] = host
150 self._hosts_by_ip4[ip4] = host
151 self._hosts_by_ip6[ip6] = host
154 def __init__(self, test):
157 self._remote_hosts = []
158 self._hosts_by_mac = {}
159 self._hosts_by_ip4 = {}
160 self._hosts_by_ip6 = {}
162 self.generate_remote_hosts()
164 self._local_ip4 = "172.16.%u.1" % self.sw_if_index
165 self._local_ip4n = socket.inet_pton(socket.AF_INET, self.local_ip4)
166 self._local_ip4_subnet = "172.16.%u.0" % self.sw_if_index
167 self._local_ip4n_subnet = socket.inet_pton(socket.AF_INET,
168 self._local_ip4_subnet)
169 self._local_ip4_bcast = "172.16.%u.255" % self.sw_if_index
170 self._local_ip4n_bcast = socket.inet_pton(socket.AF_INET,
171 self._local_ip4_bcast)
172 self.local_ip4_prefix_len = 24
173 self.has_ip4_config = False
174 self.ip4_table_id = 0
176 self._local_ip6 = "fd01:%x::1" % self.sw_if_index
177 self._local_ip6n = socket.inet_pton(socket.AF_INET6, self.local_ip6)
178 self.local_ip6_prefix_len = 64
179 self.has_ip6_config = False
180 self.ip6_table_id = 0
182 r = self.test.vapi.sw_interface_dump()
184 if intf.sw_if_index == self.sw_if_index:
185 self._name = intf.interface_name.split(b'\0', 1)[0]
187 ':'.join(intf.l2_address.encode('hex')[i:i + 2]
188 for i in range(0, 12, 2))
193 "Could not find interface with sw_if_index %d "
194 "in interface dump %s" %
195 (self.sw_if_index, repr(r)))
196 self._local_ip6_ll = mk_ll_addr(self.local_mac)
197 self._local_ip6n_ll = socket.inet_pton(socket.AF_INET6,
200 def config_ip4(self):
201 """Configure IPv4 address on the VPP interface."""
202 self.test.vapi.sw_interface_add_del_address(
203 self.sw_if_index, self.local_ip4n, self.local_ip4_prefix_len)
204 self.has_ip4_config = True
206 def unconfig_ip4(self):
207 """Remove IPv4 address on the VPP interface."""
209 if self.has_ip4_config:
210 self.test.vapi.sw_interface_add_del_address(
213 self.local_ip4_prefix_len,
215 except AttributeError:
216 self.has_ip4_config = False
217 self.has_ip4_config = False
219 def configure_ipv4_neighbors(self):
220 """For every remote host assign neighbor's MAC to IPv4 addresses.
222 :param vrf_id: The FIB table / VRF ID. (Default value = 0)
224 for host in self._remote_hosts:
225 macn = host.mac.replace(":", "").decode('hex')
227 self.test.vapi.ip_neighbor_add_del(
228 self.sw_if_index, macn, ipn)
230 def config_ip6(self):
231 """Configure IPv6 address on the VPP interface."""
232 self.test.vapi.sw_interface_add_del_address(
233 self.sw_if_index, self._local_ip6n, self.local_ip6_prefix_len,
235 self.has_ip6_config = True
237 def unconfig_ip6(self):
238 """Remove IPv6 address on the VPP interface."""
240 if self.has_ip6_config:
241 self.test.vapi.sw_interface_add_del_address(
244 self.local_ip6_prefix_len,
246 except AttributeError:
247 self.has_ip6_config = False
248 self.has_ip6_config = False
250 def configure_ipv6_neighbors(self):
251 """For every remote host assign neighbor's MAC to IPv6 addresses.
253 :param vrf_id: The FIB table / VRF ID. (Default value = 0)
255 for host in self._remote_hosts:
256 macn = host.mac.replace(":", "").decode('hex')
258 self.test.vapi.ip_neighbor_add_del(
259 self.sw_if_index, macn, ipn, is_ipv6=1)
262 """Unconfigure IPv6 and IPv4 address on the VPP interface."""
266 def set_table_ip4(self, table_id):
267 """Set the interface in a IPv4 Table.
269 .. note:: Must be called before configuring IP4 addresses.
271 self.ip4_table_id = table_id
272 self.test.vapi.sw_interface_set_table(
273 self.sw_if_index, 0, self.ip4_table_id)
275 def set_table_ip6(self, table_id):
276 """Set the interface in a IPv6 Table.
278 .. note:: Must be called before configuring IP6 addresses.
280 self.ip6_table_id = table_id
281 self.test.vapi.sw_interface_set_table(
282 self.sw_if_index, 1, self.ip6_table_id)
284 def disable_ipv6_ra(self):
285 """Configure IPv6 RA suppress on the VPP interface."""
286 self.test.vapi.sw_interface_ra_suppress(self.sw_if_index)
288 def ip6_ra_config(self, no=0, suppress=0, send_unicast=0):
289 """Configure IPv6 RA suppress on the VPP interface."""
290 self.test.vapi.ip6_sw_interface_ra_config(self.sw_if_index,
295 def ip6_ra_prefix(self, address, address_length, is_no=0,
296 off_link=0, no_autoconfig=0, use_default=0):
297 """Configure IPv6 RA suppress on the VPP interface."""
298 self.test.vapi.ip6_sw_interface_ra_prefix(self.sw_if_index,
303 no_autoconfig=no_autoconfig,
304 use_default=use_default)
307 """Put interface ADMIN-UP."""
308 self.test.vapi.sw_interface_set_flags(self.sw_if_index,
311 def admin_down(self):
312 """Put interface ADMIN-down."""
313 self.test.vapi.sw_interface_set_flags(self.sw_if_index,
316 def ip6_enable(self):
317 """IPv6 Enable interface"""
318 self.test.vapi.ip6_sw_interface_enable_disable(self.sw_if_index,
321 def ip6_disable(self):
322 """Put interface ADMIN-DOWN."""
323 self.test.vapi.ip6_sw_interface_enable_disable(self.sw_if_index,
326 def add_sub_if(self, sub_if):
327 """Register a sub-interface with this interface.
329 :param sub_if: sub-interface
331 if not hasattr(self, 'sub_if'):
334 if isinstance(self.sub_if, list):
335 self.sub_if.append(sub_if)
339 def enable_mpls(self):
340 """Enable MPLS on the VPP interface."""
341 self.test.vapi.sw_interface_enable_disable_mpls(
344 def disable_mpls(self):
345 """Enable MPLS on the VPP interface."""
346 self.test.vapi.sw_interface_enable_disable_mpls(
349 def is_ip4_entry_in_fib_dump(self, dump):
351 if i.address == self.local_ip4n and \
352 i.address_length == self.local_ip4_prefix_len and \
353 i.table_id == self.ip4_table_id:
357 def set_unnumbered(self, ip_sw_if_index):
358 """ Set the interface to unnumbered via ip_sw_if_index """
359 self.test.vapi.sw_interface_set_unnumbered(
363 def unset_unnumbered(self, ip_sw_if_index):
364 """ Unset the interface to unnumbered via ip_sw_if_index """
365 self.test.vapi.sw_interface_set_unnumbered(
370 def set_proxy_arp(self, enable=1):
371 """ Set the interface to enable/disable Proxy ARP """
372 self.test.vapi.proxy_arp_intfc_enable_disable(
376 def query_vpp_config(self):
377 dump = self.test.vapi.sw_interface_dump()
378 return self.is_interface_config_in_dump(dump)
380 def is_interface_config_in_dump(self, dump):
382 if i.interface_name.rstrip(' \t\r\n\0') == self.name and \
383 i.sw_if_index == self.sw_if_index: