1 from abc import abstractmethod, ABCMeta
4 from util import Host, mk_ll_addr
7 class VppInterface(object):
8 """Generic VPP interface."""
9 __metaclass__ = ABCMeta
12 def sw_if_index(self):
13 """Interface index assigned by VPP."""
14 return self._sw_if_index
18 """MAC-address of the remote interface "connected" to this interface"""
19 return self._remote_hosts[0].mac
23 """MAC-address of the VPP interface."""
24 return self._local_mac
28 """Local IPv4 address on VPP interface (string)."""
29 return self._local_ip4
33 """Local IPv4 address - raw, suitable as API parameter."""
34 return socket.inet_pton(socket.AF_INET, self._local_ip4)
38 """IPv4 address of remote peer "connected" to this interface."""
39 return self._remote_hosts[0].ip4
42 def remote_ip4n(self):
43 """IPv4 address of remote peer - raw, suitable as API parameter."""
44 return socket.inet_pton(socket.AF_INET, self.remote_ip4)
48 """Local IPv6 address on VPP interface (string)."""
49 return self._local_ip6
53 """Local IPv6 address - raw, suitable as API parameter."""
54 return socket.inet_pton(socket.AF_INET6, self.local_ip6)
58 """IPv6 address of remote peer "connected" to this interface."""
59 return self._remote_hosts[0].ip6
62 def remote_ip6n(self):
63 """IPv6 address of remote peer - raw, suitable as API parameter"""
64 return socket.inet_pton(socket.AF_INET6, self.remote_ip6)
67 def local_ip6_ll(self):
68 """Local IPv6 linnk-local address on VPP interface (string)."""
69 return self._local_ip6_ll
72 def local_ip6n_ll(self):
73 """Local IPv6 link-local address - raw, suitable as API parameter."""
74 return self._local_ip6n_ll
77 def remote_ip6_ll(self):
78 """Link-local IPv6 address of remote peer
79 "connected" to this interface."""
80 return self._remote_ip6_ll
83 def remote_ip6n_ll(self):
84 """Link-local IPv6 address of remote peer
85 - raw, suitable as API parameter"""
86 return self._remote_ip6n_ll
90 """Name of the interface."""
95 """RAW result of sw_interface_dump for this interface."""
100 """Test case creating this interface."""
104 def remote_hosts(self):
105 """Remote hosts list"""
106 return self._remote_hosts
109 def remote_hosts(self, value):
111 :param list value: List of remote hosts.
113 self._remote_hosts = value
114 self._hosts_by_mac = {}
115 self._hosts_by_ip4 = {}
116 self._hosts_by_ip6 = {}
117 for host in self._remote_hosts:
118 self._hosts_by_mac[host.mac] = host
119 self._hosts_by_ip4[host.ip4] = host
120 self._hosts_by_ip6[host.ip6] = host
122 def host_by_mac(self, mac):
124 :param mac: MAC address to find host by.
125 :return: Host object assigned to interface.
127 return self._hosts_by_mac[mac]
129 def host_by_ip4(self, ip):
131 :param ip: IPv4 address to find host by.
132 :return: Host object assigned to interface.
134 return self._hosts_by_ip4[ip]
136 def host_by_ip6(self, ip):
138 :param ip: IPv6 address to find host by.
139 :return: Host object assigned to interface.
141 return self._hosts_by_ip6[ip]
143 def generate_remote_hosts(self, count=1):
144 """Generate and add remote hosts for the interface.
146 :param int count: Number of generated remote hosts.
148 self._remote_hosts = []
149 self._hosts_by_mac = {}
150 self._hosts_by_ip4 = {}
151 self._hosts_by_ip6 = {}
153 2, count + 2): # 0: network address, 1: local vpp address
154 mac = "02:%02x:00:00:ff:%02x" % (self.sw_if_index, i)
155 ip4 = "172.16.%u.%u" % (self.sw_if_index, i)
156 ip6 = "fd01:%x::%x" % (self.sw_if_index, i)
157 ip6_ll = mk_ll_addr(mac)
158 host = Host(mac, ip4, ip6, ip6_ll)
159 self._remote_hosts.append(host)
160 self._hosts_by_mac[mac] = host
161 self._hosts_by_ip4[ip4] = host
162 self._hosts_by_ip6[ip6] = host
165 def __init__(self, test):
168 self._remote_hosts = []
169 self._hosts_by_mac = {}
170 self._hosts_by_ip4 = {}
171 self._hosts_by_ip6 = {}
173 def set_sw_if_index(self, sw_if_index):
174 self._sw_if_index = sw_if_index
176 self.generate_remote_hosts()
178 self._local_ip4 = "172.16.%u.1" % self.sw_if_index
179 self._local_ip4n = socket.inet_pton(socket.AF_INET, self.local_ip4)
180 self._local_ip4_subnet = "172.16.%u.0" % self.sw_if_index
181 self._local_ip4n_subnet = socket.inet_pton(socket.AF_INET,
182 self._local_ip4_subnet)
183 self._local_ip4_bcast = "172.16.%u.255" % self.sw_if_index
184 self._local_ip4n_bcast = socket.inet_pton(socket.AF_INET,
185 self._local_ip4_bcast)
186 self.local_ip4_prefix_len = 24
187 self.has_ip4_config = False
188 self.ip4_table_id = 0
190 self._local_ip6 = "fd01:%x::1" % self.sw_if_index
191 self._local_ip6n = socket.inet_pton(socket.AF_INET6, self.local_ip6)
192 self.local_ip6_prefix_len = 64
193 self.has_ip6_config = False
194 self.ip6_table_id = 0
196 r = self.test.vapi.sw_interface_dump()
198 if intf.sw_if_index == self.sw_if_index:
199 self._name = intf.interface_name.split(b'\0', 1)[0]
201 ':'.join(intf.l2_address.encode('hex')[i:i + 2]
202 for i in range(0, 12, 2))
207 "Could not find interface with sw_if_index %d "
208 "in interface dump %s" %
209 (self.sw_if_index, repr(r)))
210 self._local_ip6_ll = mk_ll_addr(self.local_mac)
211 self._local_ip6n_ll = socket.inet_pton(socket.AF_INET6,
213 self._remote_ip6_ll = mk_ll_addr(self.remote_mac)
214 self._remote_ip6n_ll = socket.inet_pton(socket.AF_INET6,
217 def config_ip4(self):
218 """Configure IPv4 address on the VPP interface."""
219 self.test.vapi.sw_interface_add_del_address(
220 self.sw_if_index, self.local_ip4n, self.local_ip4_prefix_len)
221 self.has_ip4_config = True
223 def unconfig_ip4(self):
224 """Remove IPv4 address on the VPP interface."""
226 if self.has_ip4_config:
227 self.test.vapi.sw_interface_add_del_address(
230 self.local_ip4_prefix_len,
232 except AttributeError:
233 self.has_ip4_config = False
234 self.has_ip4_config = False
236 def configure_ipv4_neighbors(self):
237 """For every remote host assign neighbor's MAC to IPv4 addresses.
239 :param vrf_id: The FIB table / VRF ID. (Default value = 0)
241 for host in self._remote_hosts:
242 macn = host.mac.replace(":", "").decode('hex')
244 self.test.vapi.ip_neighbor_add_del(
245 self.sw_if_index, macn, ipn)
247 def config_ip6(self):
248 """Configure IPv6 address on the VPP interface."""
249 self.test.vapi.sw_interface_add_del_address(
250 self.sw_if_index, self._local_ip6n, self.local_ip6_prefix_len,
252 self.has_ip6_config = True
254 def unconfig_ip6(self):
255 """Remove IPv6 address on the VPP interface."""
257 if self.has_ip6_config:
258 self.test.vapi.sw_interface_add_del_address(
261 self.local_ip6_prefix_len,
263 except AttributeError:
264 self.has_ip6_config = False
265 self.has_ip6_config = False
267 def configure_ipv6_neighbors(self):
268 """For every remote host assign neighbor's MAC to IPv6 addresses.
270 :param vrf_id: The FIB table / VRF ID. (Default value = 0)
272 for host in self._remote_hosts:
273 macn = host.mac.replace(":", "").decode('hex')
275 self.test.vapi.ip_neighbor_add_del(
276 self.sw_if_index, macn, ipn, is_ipv6=1)
279 """Unconfigure IPv6 and IPv4 address on the VPP interface."""
283 def set_table_ip4(self, table_id):
284 """Set the interface in a IPv4 Table.
286 .. note:: Must be called before configuring IP4 addresses.
288 self.ip4_table_id = table_id
289 self.test.vapi.sw_interface_set_table(
290 self.sw_if_index, 0, self.ip4_table_id)
292 def set_table_ip6(self, table_id):
293 """Set the interface in a IPv6 Table.
295 .. note:: Must be called before configuring IP6 addresses.
297 self.ip6_table_id = table_id
298 self.test.vapi.sw_interface_set_table(
299 self.sw_if_index, 1, self.ip6_table_id)
301 def disable_ipv6_ra(self):
302 """Configure IPv6 RA suppress on the VPP interface."""
303 self.test.vapi.sw_interface_ra_suppress(self.sw_if_index)
305 def ip6_ra_config(self, no=0, suppress=0, send_unicast=0):
306 """Configure IPv6 RA suppress on the VPP interface."""
307 self.test.vapi.ip6_sw_interface_ra_config(self.sw_if_index,
312 def ip6_ra_prefix(self, address, address_length, is_no=0,
313 off_link=0, no_autoconfig=0, use_default=0):
314 """Configure IPv6 RA suppress on the VPP interface."""
315 self.test.vapi.ip6_sw_interface_ra_prefix(self.sw_if_index,
320 no_autoconfig=no_autoconfig,
321 use_default=use_default)
324 """Put interface ADMIN-UP."""
325 self.test.vapi.sw_interface_set_flags(self.sw_if_index,
328 def admin_down(self):
329 """Put interface ADMIN-down."""
330 self.test.vapi.sw_interface_set_flags(self.sw_if_index,
333 def ip6_enable(self):
334 """IPv6 Enable interface"""
335 self.test.vapi.ip6_sw_interface_enable_disable(self.sw_if_index,
338 def ip6_disable(self):
339 """Put interface ADMIN-DOWN."""
340 self.test.vapi.ip6_sw_interface_enable_disable(self.sw_if_index,
343 def add_sub_if(self, sub_if):
344 """Register a sub-interface with this interface.
346 :param sub_if: sub-interface
348 if not hasattr(self, 'sub_if'):
351 if isinstance(self.sub_if, list):
352 self.sub_if.append(sub_if)
356 def enable_mpls(self):
357 """Enable MPLS on the VPP interface."""
358 self.test.vapi.sw_interface_enable_disable_mpls(
361 def disable_mpls(self):
362 """Enable MPLS on the VPP interface."""
363 self.test.vapi.sw_interface_enable_disable_mpls(
366 def is_ip4_entry_in_fib_dump(self, dump):
368 if i.address == self.local_ip4n and \
369 i.address_length == self.local_ip4_prefix_len and \
370 i.table_id == self.ip4_table_id:
374 def set_unnumbered(self, ip_sw_if_index):
375 """ Set the interface to unnumbered via ip_sw_if_index """
376 self.test.vapi.sw_interface_set_unnumbered(
380 def unset_unnumbered(self, ip_sw_if_index):
381 """ Unset the interface to unnumbered via ip_sw_if_index """
382 self.test.vapi.sw_interface_set_unnumbered(
387 def set_proxy_arp(self, enable=1):
388 """ Set the interface to enable/disable Proxy ARP """
389 self.test.vapi.proxy_arp_intfc_enable_disable(
393 def query_vpp_config(self):
394 dump = self.test.vapi.sw_interface_dump()
395 return self.is_interface_config_in_dump(dump)
397 def is_interface_config_in_dump(self, dump):
399 if i.interface_name.rstrip(' \t\r\n\0') == self.name and \
400 i.sw_if_index == self.sw_if_index: