1 from abc import abstractmethod, ABCMeta
5 class VppInterface(object):
6 """Generic VPP interface."""
7 __metaclass__ = ABCMeta
10 def sw_if_index(self):
11 """Interface index assigned by VPP."""
12 return self._sw_if_index
16 """MAC-address of the remote interface "connected" to this interface."""
17 return self._remote_hosts[0].mac
21 """MAC-address of the VPP interface."""
22 return self._local_mac
26 """Local IPv4 address on VPP interface (string)."""
27 return self._local_ip4
31 """Local IPv4 address - raw, suitable as API parameter."""
32 return socket.inet_pton(socket.AF_INET, self._local_ip4)
36 """IPv4 address of remote peer "connected" to this interface."""
37 return self._remote_hosts[0].ip4
40 def remote_ip4n(self):
41 """IPv4 address of remote peer - raw, suitable as API parameter."""
42 return socket.inet_pton(socket.AF_INET, self.remote_ip4)
46 """Local IPv6 address on VPP interface (string)."""
47 return self._local_ip6
51 """Local IPv6 address - raw, suitable as API parameter."""
52 return socket.inet_pton(socket.AF_INET6, self.local_ip6)
56 """IPv6 address of remote peer "connected" to this interface."""
57 return self._remote_hosts[0].ip6
60 def remote_ip6n(self):
61 """IPv6 address of remote peer - raw, suitable as API parameter"""
62 return socket.inet_pton(socket.AF_INET6, self.remote_ip6)
66 """Name of the interface."""
71 """RAW result of sw_interface_dump for this interface."""
76 """Test case creating this interface."""
80 def remote_hosts(self):
81 """Remote hosts list"""
82 return self._remote_hosts
85 def remote_hosts(self, value):
87 :param list value: List of remote hosts.
89 self._remote_hosts = value
90 self._hosts_by_mac = {}
91 self._hosts_by_ip4 = {}
92 self._hosts_by_ip6 = {}
93 for host in self._remote_hosts:
94 self._hosts_by_mac[host.mac] = host
95 self._hosts_by_ip4[host.ip4] = host
96 self._hosts_by_ip6[host.ip6] = host
98 def host_by_mac(self, mac):
100 :param mac: MAC address to find host by.
101 :return: Host object assigned to interface.
103 return self._hosts_by_mac[mac]
105 def host_by_ip4(self, ip):
107 :param ip: IPv4 address to find host by.
108 :return: Host object assigned to interface.
110 return self._hosts_by_ip4[ip]
112 def host_by_ip6(self, ip):
114 :param ip: IPv6 address to find host by.
115 :return: Host object assigned to interface.
117 return self._hosts_by_ip6[ip]
119 def generate_remote_hosts(self, count=1):
120 """Generate and add remote hosts for the interface.
122 :param int count: Number of generated remote hosts.
124 self._remote_hosts = []
125 self._hosts_by_mac = {}
126 self._hosts_by_ip4 = {}
127 self._hosts_by_ip6 = {}
129 2, count + 2): # 0: network address, 1: local vpp address
130 mac = "02:%02x:00:00:ff:%02x" % (self.sw_if_index, i)
131 ip4 = "172.16.%u.%u" % (self.sw_if_index, i)
132 ip6 = "fd01:%04x::%04x" % (self.sw_if_index, i)
133 host = Host(mac, ip4, ip6)
134 self._remote_hosts.append(host)
135 self._hosts_by_mac[mac] = host
136 self._hosts_by_ip4[ip4] = host
137 self._hosts_by_ip6[ip6] = host
140 def __init__(self, test):
143 self._remote_hosts = []
144 self._hosts_by_mac = {}
145 self._hosts_by_ip4 = {}
146 self._hosts_by_ip6 = {}
148 self.generate_remote_hosts()
150 self._local_ip4 = "172.16.%u.1" % self.sw_if_index
151 self._local_ip4n = socket.inet_pton(socket.AF_INET, self.local_ip4)
153 self._local_ip6 = "fd01:%04x::1" % self.sw_if_index
154 self._local_ip6n = socket.inet_pton(socket.AF_INET6, self.local_ip6)
156 r = self.test.vapi.sw_interface_dump()
158 if intf.sw_if_index == self.sw_if_index:
159 self._name = intf.interface_name.split(b'\0', 1)[0]
161 ':'.join(intf.l2_address.encode('hex')[i:i + 2]
162 for i in range(0, 12, 2))
167 "Could not find interface with sw_if_index %d "
168 "in interface dump %s" %
169 (self.sw_if_index, repr(r)))
171 def config_ip4(self):
172 """Configure IPv4 address on the VPP interface."""
173 addr = self.local_ip4n
175 self.test.vapi.sw_interface_add_del_address(
176 self.sw_if_index, addr, addr_len)
177 self.has_ip4_config = True
179 def unconfig_ip4(self):
180 """Remove IPv4 address on the VPP interface"""
182 if (self.has_ip4_config):
183 self.test.vapi.sw_interface_add_del_address(
187 except AttributeError:
188 self.has_ip4_config = False
189 self.has_ip4_config = False
191 def configure_ipv4_neighbors(self):
192 """For every remote host assign neighbor's MAC to IPv4 addresses."""
193 for host in self._remote_hosts:
194 macn = host.mac.replace(":", "").decode('hex')
196 self.test.vapi.ip_neighbor_add_del(self.sw_if_index, macn, ipn)
198 def config_ip6(self):
199 """Configure IPv6 address on the VPP interface."""
200 addr = self._local_ip6n
202 self.test.vapi.sw_interface_add_del_address(
203 self.sw_if_index, addr, addr_len, is_ipv6=1)
204 self.has_ip6_config = True
206 def unconfig_ip6(self):
207 """Remove IPv6 address on the VPP interface"""
209 if (self.has_ip6_config):
210 self.test.vapi.sw_interface_add_del_address(
213 64, is_ipv6=1, is_add=0)
214 except AttributeError:
215 self.has_ip6_config = False
216 self.has_ip6_config = False
219 """Unconfigure IPv6 and IPv4 address on the VPP interface"""
223 def set_table_ip4(self, table_id):
224 """Set the interface in a IPv4 Table.
226 .. note:: Must be called before configuring IP4 addresses."""
227 self.test.vapi.sw_interface_set_table(
228 self.sw_if_index, 0, table_id)
230 def set_table_ip6(self, table_id):
231 """Set the interface in a IPv6 Table.
233 .. note:: Must be called before configuring IP6 addresses.
235 self.test.vapi.sw_interface_set_table(
236 self.sw_if_index, 1, table_id)
238 def disable_ipv6_ra(self):
239 """Configure IPv6 RA suppress on the VPP interface."""
240 self.test.vapi.sw_interface_ra_suppress(self.sw_if_index)
243 """Put interface ADMIN-UP."""
244 self.test.vapi.sw_interface_set_flags(self.sw_if_index, admin_up_down=1)
246 def add_sub_if(self, sub_if):
247 """Register a sub-interface with this interface.
249 :param sub_if: sub-interface
251 if not hasattr(self, 'sub_if'):
254 if isinstance(self.sub_if, list):
255 self.sub_if.append(sub_if)
259 def enable_mpls(self):
260 """Enable MPLS on the VPP interface."""
261 self.test.vapi.sw_interface_enable_disable_mpls(