5 from util import Host, mk_ll_addr
6 from vpp_papi import mac_ntop, VppEnum
7 from ipaddress import IPv4Network, IPv6Network
15 class VppInterface(metaclass=abc.ABCMeta):
16 """Generic VPP interface."""
19 def sw_if_index(self):
20 """Interface index assigned by VPP."""
21 return self._sw_if_index
25 """MAC-address of the remote interface "connected" to this interface"""
26 return self._remote_hosts[0].mac
30 """MAC-address of the VPP interface."""
31 return str(self._local_mac)
35 return self._local_addr
38 def remote_addr(self):
39 return self._remote_addr
43 """Local IPv4 address on VPP interface (string)."""
44 return self._local_ip4
47 def local_ip4(self, value):
48 self._local_ip4 = value
51 def local_ip4_prefix_len(self):
52 """Local IPv4 prefix length """
53 return self._local_ip4_len
55 @local_ip4_prefix_len.setter
56 def local_ip4_prefix_len(self, value):
57 self._local_ip4_len = value
60 def local_ip4_prefix(self):
61 """Local IPv4 prefix """
62 return ("%s/%d" % (self._local_ip4, self._local_ip4_len))
66 """IPv4 address of remote peer "connected" to this interface."""
67 return self._remote_hosts[0].ip4
71 """Local IPv6 address on VPP interface (string)."""
72 return self._local_ip6
75 def local_ip6(self, value):
76 self._local_ip6 = value
79 def local_ip6_prefix_len(self):
80 """Local IPv6 prefix length """
81 return self._local_ip6_len
83 @local_ip6_prefix_len.setter
84 def local_ip6_prefix_len(self, value):
85 self._local_ip6_len = value
88 def local_ip6_prefix(self):
89 """Local IPv4 prefix """
90 return ("%s/%d" % (self._local_ip6, self._local_ip6_len))
94 """IPv6 address of remote peer "connected" to this interface."""
95 return self._remote_hosts[0].ip6
98 def local_ip6_ll(self):
99 """Local IPv6 link-local address on VPP interface (string)."""
100 return self._local_ip6_ll
103 def remote_ip6_ll(self):
104 """Link-local IPv6 address of remote peer
105 "connected" to this interface."""
106 return self._remote_ip6_ll
110 """Name of the interface."""
115 """RAW result of sw_interface_dump for this interface."""
120 """Test case creating this interface."""
124 def remote_hosts(self):
125 """Remote hosts list"""
126 return self._remote_hosts
129 def remote_hosts(self, value):
131 :param list value: List of remote hosts.
133 self._remote_hosts = value
134 self._hosts_by_mac = {}
135 self._hosts_by_ip4 = {}
136 self._hosts_by_ip6 = {}
137 for host in self._remote_hosts:
138 self._hosts_by_mac[host.mac] = host
139 self._hosts_by_ip4[host.ip4] = host
140 self._hosts_by_ip6[host.ip6] = host
142 def host_by_mac(self, mac):
144 :param mac: MAC address to find host by.
145 :return: Host object assigned to interface.
147 return self._hosts_by_mac[mac]
149 def host_by_ip4(self, ip):
151 :param ip: IPv4 address to find host by.
152 :return: Host object assigned to interface.
154 return self._hosts_by_ip4[ip]
156 def host_by_ip6(self, ip):
158 :param ip: IPv6 address to find host by.
159 :return: Host object assigned to interface.
161 return self._hosts_by_ip6[ip]
163 def generate_remote_hosts(self, count=1):
164 """Generate and add remote hosts for the interface.
166 :param int count: Number of generated remote hosts.
168 self._remote_hosts = []
169 self._hosts_by_mac = {}
170 self._hosts_by_ip4 = {}
171 self._hosts_by_ip6 = {}
173 2, count + 2): # 0: network address, 1: local vpp address
174 mac = "02:%02x:00:00:ff:%02x" % (self.sw_if_index, i)
175 ip4 = "172.16.%u.%u" % (self.sw_if_index, i)
176 ip6 = "fd01:%x::%x" % (self.sw_if_index, i)
177 ip6_ll = mk_ll_addr(mac)
178 host = Host(mac, ip4, ip6, ip6_ll)
179 self._remote_hosts.append(host)
180 self._hosts_by_mac[mac] = host
181 self._hosts_by_ip4[ip4] = host
182 self._hosts_by_ip6[ip6] = host
185 def __init__(self, test):
188 self._remote_hosts = []
189 self._hosts_by_mac = {}
190 self._hosts_by_ip4 = {}
191 self._hosts_by_ip6 = {}
193 def set_mac(self, mac):
194 self._local_mac = str(mac)
195 self._local_ip6_ll = mk_ll_addr(self._local_mac)
196 self.test.vapi.sw_interface_set_mac_address(
197 self.sw_if_index, mac.packed)
200 def set_sw_if_index(self, sw_if_index):
201 if sw_if_index > 255:
202 raise RuntimeError("Don't support sw_if_index values "
204 self._sw_if_index = sw_if_index
206 self.generate_remote_hosts()
208 self._local_ip4 = "172.16.%u.1" % self.sw_if_index
209 self._local_ip4_len = 24
210 self._local_ip4_subnet = "172.16.%u.0" % self.sw_if_index
211 self._local_ip4_bcast = "172.16.%u.255" % self.sw_if_index
212 self.has_ip4_config = False
213 self.ip4_table_id = 0
215 self._local_ip6 = "fd01:%x::1" % self.sw_if_index
216 self._local_ip6_len = 64
217 self.has_ip6_config = False
218 self.ip6_table_id = 0
220 self._local_addr = {socket.AF_INET: self.local_ip4,
221 socket.AF_INET6: self.local_ip6}
222 self._remote_addr = {socket.AF_INET: self.remote_ip4,
223 socket.AF_INET6: self.remote_ip6}
225 r = self.test.vapi.sw_interface_dump(sw_if_index=self.sw_if_index)
227 if intf.sw_if_index == self.sw_if_index:
228 self._name = intf.interface_name
229 self._local_mac = intf.l2_address
234 "Could not find interface with sw_if_index %d "
235 "in interface dump %s" %
236 (self.sw_if_index, moves.reprlib.repr(r)))
237 self._local_ip6_ll = mk_ll_addr(self.local_mac)
238 self._remote_ip6_ll = mk_ll_addr(self.remote_mac)
240 def config_ip4(self):
241 """Configure IPv4 address on the VPP interface."""
242 self.test.vapi.sw_interface_add_del_address(
243 sw_if_index=self.sw_if_index, prefix=self.local_ip4_prefix)
244 self.has_ip4_config = True
247 def unconfig_ip4(self):
248 """Remove IPv4 address on the VPP interface."""
250 if self.has_ip4_config:
251 self.test.vapi.sw_interface_add_del_address(
252 sw_if_index=self.sw_if_index,
253 prefix=self.local_ip4_prefix, is_add=0)
254 except AttributeError:
255 self.has_ip4_config = False
256 self.has_ip4_config = False
259 def configure_ipv4_neighbors(self):
260 """For every remote host assign neighbor's MAC to IPv4 addresses.
262 :param vrf_id: The FIB table / VRF ID. (Default value = 0)
264 for host in self._remote_hosts:
265 self.test.vapi.ip_neighbor_add_del(self.sw_if_index,
270 def config_ip6(self):
271 """Configure IPv6 address on the VPP interface."""
272 self.test.vapi.sw_interface_add_del_address(
273 sw_if_index=self.sw_if_index, prefix=self.local_ip6_prefix)
274 self.has_ip6_config = True
277 def unconfig_ip6(self):
278 """Remove IPv6 address on the VPP interface."""
280 if self.has_ip6_config:
281 self.test.vapi.sw_interface_add_del_address(
282 sw_if_index=self.sw_if_index,
283 prefix=self.local_ip6_prefix, is_add=0)
284 except AttributeError:
285 self.has_ip6_config = False
286 self.has_ip6_config = False
289 def configure_ipv6_neighbors(self):
290 """For every remote host assign neighbor's MAC to IPv6 addresses.
292 :param vrf_id: The FIB table / VRF ID. (Default value = 0)
294 for host in self._remote_hosts:
295 self.test.vapi.ip_neighbor_add_del(self.sw_if_index,
300 """Unconfigure IPv6 and IPv4 address on the VPP interface."""
305 def set_table_ip4(self, table_id):
306 """Set the interface in a IPv4 Table.
308 .. note:: Must be called before configuring IP4 addresses.
310 self.ip4_table_id = table_id
311 self.test.vapi.sw_interface_set_table(
312 self.sw_if_index, 0, self.ip4_table_id)
315 def set_table_ip6(self, table_id):
316 """Set the interface in a IPv6 Table.
318 .. note:: Must be called before configuring IP6 addresses.
320 self.ip6_table_id = table_id
321 self.test.vapi.sw_interface_set_table(
322 self.sw_if_index, 1, self.ip6_table_id)
325 def disable_ipv6_ra(self):
326 """Configure IPv6 RA suppress on the VPP interface."""
327 self.test.vapi.sw_interface_ip6nd_ra_config(
328 sw_if_index=self.sw_if_index,
332 def ip6_ra_config(self, no=0, suppress=0, send_unicast=0):
333 """Configure IPv6 RA suppress on the VPP interface."""
334 self.test.vapi.sw_interface_ip6nd_ra_config(
335 sw_if_index=self.sw_if_index,
338 send_unicast=send_unicast)
341 def ip6_ra_prefix(self, prefix, is_no=0,
342 off_link=0, no_autoconfig=0, use_default=0):
343 """Configure IPv6 RA suppress on the VPP interface.
345 prefix can be a string in the format of '<address>/<length_in_bits>'
346 or ipaddress.ipnetwork object (if strict.)"""
348 self.test.vapi.sw_interface_ip6nd_ra_prefix(
349 sw_if_index=self.sw_if_index,
351 use_default=use_default,
352 off_link=off_link, no_autoconfig=no_autoconfig,
357 """Put interface ADMIN-UP."""
358 self.test.vapi.sw_interface_set_flags(
360 flags=VppEnum.vl_api_if_status_flags_t.IF_STATUS_API_FLAG_ADMIN_UP)
363 def admin_down(self):
364 """Put interface ADMIN-down."""
365 self.test.vapi.sw_interface_set_flags(self.sw_if_index,
370 """Put interface link-state-UP."""
371 self.test.vapi.cli("test interface link-state %s up" % self.name)
374 """Put interface link-state-down."""
375 self.test.vapi.cli("test interface link-state %s down" % self.name)
377 def ip6_enable(self):
378 """IPv6 Enable interface"""
379 self.test.vapi.sw_interface_ip6_enable_disable(self.sw_if_index,
383 def ip6_disable(self):
384 """Put interface ADMIN-DOWN."""
385 self.test.vapi.sw_interface_ip6_enable_disable(self.sw_if_index,
389 def add_sub_if(self, sub_if):
390 """Register a sub-interface with this interface.
392 :param sub_if: sub-interface
394 if not hasattr(self, 'sub_if'):
397 if isinstance(self.sub_if, list):
398 self.sub_if.append(sub_if)
403 def enable_mpls(self):
404 """Enable MPLS on the VPP interface."""
405 self.test.vapi.sw_interface_set_mpls_enable(self.sw_if_index)
408 def disable_mpls(self):
409 """Enable MPLS on the VPP interface."""
410 self.test.vapi.sw_interface_set_mpls_enable(self.sw_if_index, 0)
413 def is_ip4_entry_in_fib_dump(self, dump):
415 n = IPv4Network(text_type("%s/%d" % (self.local_ip4,
416 self.local_ip4_prefix_len)))
417 if i.route.prefix == n and \
418 i.route.table_id == self.ip4_table_id:
422 def set_unnumbered(self, ip_sw_if_index):
423 """ Set the interface to unnumbered via ip_sw_if_index """
424 self.test.vapi.sw_interface_set_unnumbered(ip_sw_if_index,
428 def unset_unnumbered(self, ip_sw_if_index):
429 """ Unset the interface to unnumbered via ip_sw_if_index """
430 self.test.vapi.sw_interface_set_unnumbered(ip_sw_if_index,
431 self.sw_if_index, is_add=0)
434 def set_proxy_arp(self, enable=1):
435 """ Set the interface to enable/disable Proxy ARP """
436 self.test.vapi.proxy_arp_intfc_enable_disable(
437 sw_if_index=self.sw_if_index,
441 def query_vpp_config(self):
442 dump = self.test.vapi.sw_interface_dump(sw_if_index=self.sw_if_index)
443 return self.is_interface_config_in_dump(dump)
445 def get_interface_config_from_dump(self, dump):
447 if i.interface_name.rstrip(' \t\r\n\0') == self.name and \
448 i.sw_if_index == self.sw_if_index:
453 def is_interface_config_in_dump(self, dump):
454 return self.get_interface_config_from_dump(dump) is not None
456 def assert_interface_state(self, admin_up_down, link_up_down,
459 event = self.test.vapi.wait_for_event(timeout=1,
460 name='sw_interface_event')
461 self.test.assert_equal(event.sw_if_index, self.sw_if_index,
463 self.test.assert_equal((event.flags & 1), admin_up_down,
465 self.test.assert_equal((event.flags & 2), link_up_down,
467 dump = self.test.vapi.sw_interface_dump()
468 if_state = self.get_interface_config_from_dump(dump)
469 self.test.assert_equal((if_state.flags & 1), admin_up_down,
471 self.test.assert_equal((if_state.flags & 2), link_up_down,
477 def get_rx_stats(self):
478 c = self.test.statistics.get_counter("^/if/rx$")
479 return c[0][self.sw_if_index]
481 def get_tx_stats(self):
482 c = self.test.statistics.get_counter("^/if/tx$")
483 return c[0][self.sw_if_index]
485 def set_l3_mtu(self, mtu):
486 self.test.vapi.sw_interface_set_mtu(self.sw_if_index, [mtu, 0, 0, 0])
489 def set_ip4_mtu(self, mtu):
490 self.test.vapi.sw_interface_set_mtu(self.sw_if_index, [0, mtu, 0, 0])
493 def set_ip6_mtu(self, mtu):
494 self.test.vapi.sw_interface_set_mtu(self.sw_if_index, [0, 0, mtu, 0])
497 def set_mpls_mtu(self, mtu):
498 self.test.vapi.sw_interface_set_mtu(self.sw_if_index, [0, 0, 0, mtu])