1 from abc import abstractmethod, ABCMeta
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)
59 """IPv6 address of remote peer "connected" to this interface."""
60 return self._remote_hosts[0].ip6
63 def remote_ip6n(self):
64 """IPv6 address of remote peer - raw, suitable as API parameter"""
65 return socket.inet_pton(socket.AF_INET6, self.remote_ip6)
69 """Name of the interface."""
74 """RAW result of sw_interface_dump for this interface."""
79 """Test case creating this interface."""
83 def remote_hosts(self):
84 """Remote hosts list"""
85 return self._remote_hosts
88 def remote_hosts(self, value):
90 :param list value: List of remote hosts.
92 self._remote_hosts = value
93 self._hosts_by_mac = {}
94 self._hosts_by_ip4 = {}
95 self._hosts_by_ip6 = {}
96 for host in self._remote_hosts:
97 self._hosts_by_mac[host.mac] = host
98 self._hosts_by_ip4[host.ip4] = host
99 self._hosts_by_ip6[host.ip6] = host
101 def host_by_mac(self, mac):
103 :param mac: MAC address to find host by.
104 :return: Host object assigned to interface.
106 return self._hosts_by_mac[mac]
108 def host_by_ip4(self, ip):
110 :param ip: IPv4 address to find host by.
111 :return: Host object assigned to interface.
113 return self._hosts_by_ip4[ip]
115 def host_by_ip6(self, ip):
117 :param ip: IPv6 address to find host by.
118 :return: Host object assigned to interface.
120 return self._hosts_by_ip6[ip]
122 def generate_remote_hosts(self, count=1):
123 """Generate and add remote hosts for the interface.
125 :param int count: Number of generated remote hosts.
127 self._remote_hosts = []
128 self._hosts_by_mac = {}
129 self._hosts_by_ip4 = {}
130 self._hosts_by_ip6 = {}
132 2, count + 2): # 0: network address, 1: local vpp address
133 mac = "02:%02x:00:00:ff:%02x" % (self.sw_if_index, i)
134 ip4 = "172.16.%u.%u" % (self.sw_if_index, i)
135 ip6 = "fd01:%x::%x" % (self.sw_if_index, i)
136 host = Host(mac, ip4, ip6)
137 self._remote_hosts.append(host)
138 self._hosts_by_mac[mac] = host
139 self._hosts_by_ip4[ip4] = host
140 self._hosts_by_ip6[ip6] = host
143 def __init__(self, test):
146 self._remote_hosts = []
147 self._hosts_by_mac = {}
148 self._hosts_by_ip4 = {}
149 self._hosts_by_ip6 = {}
151 self.generate_remote_hosts()
153 self._local_ip4 = "172.16.%u.1" % self.sw_if_index
154 self._local_ip4n = socket.inet_pton(socket.AF_INET, self.local_ip4)
155 self.local_ip4_prefix_len = 24
156 self.has_ip4_config = False
157 self.ip4_table_id = 0
159 self._local_ip6 = "fd01:%x::1" % self.sw_if_index
160 self._local_ip6n = socket.inet_pton(socket.AF_INET6, self.local_ip6)
161 self.local_ip6_prefix_len = 64
162 self.has_ip6_config = False
163 self.ip6_table_id = 0
165 r = self.test.vapi.sw_interface_dump()
167 if intf.sw_if_index == self.sw_if_index:
168 self._name = intf.interface_name.split(b'\0', 1)[0]
170 ':'.join(intf.l2_address.encode('hex')[i:i + 2]
171 for i in range(0, 12, 2))
176 "Could not find interface with sw_if_index %d "
177 "in interface dump %s" %
178 (self.sw_if_index, repr(r)))
180 def config_ip4(self):
181 """Configure IPv4 address on the VPP interface."""
182 self.test.vapi.sw_interface_add_del_address(
183 self.sw_if_index, self.local_ip4n, self.local_ip4_prefix_len)
184 self.has_ip4_config = True
186 def unconfig_ip4(self):
187 """Remove IPv4 address on the VPP interface."""
189 if self.has_ip4_config:
190 self.test.vapi.sw_interface_add_del_address(
193 self.local_ip4_prefix_len,
195 except AttributeError:
196 self.has_ip4_config = False
197 self.has_ip4_config = False
199 def configure_ipv4_neighbors(self):
200 """For every remote host assign neighbor's MAC to IPv4 addresses.
202 :param vrf_id: The FIB table / VRF ID. (Default value = 0)
204 for host in self._remote_hosts:
205 macn = host.mac.replace(":", "").decode('hex')
207 self.test.vapi.ip_neighbor_add_del(
208 self.sw_if_index, macn, ipn)
210 def config_ip6(self):
211 """Configure IPv6 address on the VPP interface."""
212 self.test.vapi.sw_interface_add_del_address(
213 self.sw_if_index, self._local_ip6n, self.local_ip6_prefix_len,
215 self.has_ip6_config = True
217 def unconfig_ip6(self):
218 """Remove IPv6 address on the VPP interface."""
220 if self.has_ip6_config:
221 self.test.vapi.sw_interface_add_del_address(
224 self.local_ip6_prefix_len,
226 except AttributeError:
227 self.has_ip6_config = False
228 self.has_ip6_config = False
230 def configure_ipv6_neighbors(self):
231 """For every remote host assign neighbor's MAC to IPv6 addresses.
233 :param vrf_id: The FIB table / VRF ID. (Default value = 0)
235 for host in self._remote_hosts:
236 macn = host.mac.replace(":", "").decode('hex')
238 self.test.vapi.ip_neighbor_add_del(
239 self.sw_if_index, macn, ipn, is_ipv6=1)
242 """Unconfigure IPv6 and IPv4 address on the VPP interface."""
246 def set_table_ip4(self, table_id):
247 """Set the interface in a IPv4 Table.
249 .. note:: Must be called before configuring IP4 addresses.
251 self.ip4_table_id = table_id
252 self.test.vapi.sw_interface_set_table(
253 self.sw_if_index, 0, self.ip4_table_id)
255 def set_table_ip6(self, table_id):
256 """Set the interface in a IPv6 Table.
258 .. note:: Must be called before configuring IP6 addresses.
260 self.ip6_table_id = table_id
261 self.test.vapi.sw_interface_set_table(
262 self.sw_if_index, 1, self.ip6_table_id)
264 def disable_ipv6_ra(self):
265 """Configure IPv6 RA suppress on the VPP interface."""
266 self.test.vapi.sw_interface_ra_suppress(self.sw_if_index)
268 def ip6_ra_config(self, no=0, suppress=0, send_unicast=0):
269 """Configure IPv6 RA suppress on the VPP interface."""
270 self.test.vapi.ip6_sw_interface_ra_config(self.sw_if_index,
275 def ip6_ra_prefix(self, address, address_length, is_no=0,
276 off_link=0, no_autoconfig=0, use_default=0):
277 """Configure IPv6 RA suppress on the VPP interface."""
278 self.test.vapi.ip6_sw_interface_ra_prefix(self.sw_if_index,
283 no_autoconfig=no_autoconfig,
284 use_default=use_default)
287 """Put interface ADMIN-UP."""
288 self.test.vapi.sw_interface_set_flags(self.sw_if_index,
291 def admin_down(self):
292 """Put interface ADMIN-down."""
293 self.test.vapi.sw_interface_set_flags(self.sw_if_index,
296 def ip6_enable(self):
297 """IPv6 Enable interface"""
298 self.test.vapi.ip6_sw_interface_enable_disable(self.sw_if_index,
301 def ip6_disable(self):
302 """Put interface ADMIN-DOWN."""
303 self.test.vapi.ip6_sw_interface_enable_disable(self.sw_if_index,
306 def add_sub_if(self, sub_if):
307 """Register a sub-interface with this interface.
309 :param sub_if: sub-interface
311 if not hasattr(self, 'sub_if'):
314 if isinstance(self.sub_if, list):
315 self.sub_if.append(sub_if)
319 def enable_mpls(self):
320 """Enable MPLS on the VPP interface."""
321 self.test.vapi.sw_interface_enable_disable_mpls(
324 def disable_mpls(self):
325 """Enable MPLS on the VPP interface."""
326 self.test.vapi.sw_interface_enable_disable_mpls(
329 def is_ip4_entry_in_fib_dump(self, dump):
331 if i.address == self.local_ip4n and \
332 i.address_length == self.local_ip4_prefix_len and \
333 i.table_id == self.ip4_table_id:
337 def set_unnumbered(self, ip_sw_if_index):
338 """ Set the interface to unnumbered via ip_sw_if_index """
339 self.test.vapi.sw_interface_set_unnumbered(
343 def unset_unnumbered(self, ip_sw_if_index):
344 """ Unset the interface to unnumbered via ip_sw_if_index """
345 self.test.vapi.sw_interface_set_unnumbered(
350 def set_proxy_arp(self, enable=1):
351 """ Set the interface to enable/disable Proxy ARP """
352 self.test.vapi.proxy_arp_intfc_enable_disable(