1 from abc import abstractmethod, ABCMeta
3 from logging import info
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 ip: 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 = {}
131 for i in range(2, count+2): # 0: network address, 1: local vpp address
132 mac = "02:%02x:00:00:ff:%02x" % (self.sw_if_index, i)
133 ip4 = "172.16.%u.%u" % (self.sw_if_index, i)
134 ip6 = "fd01:%04x::%04x" % (self.sw_if_index, i)
135 host = Host(mac, ip4, ip6)
136 self._remote_hosts.append(host)
137 self._hosts_by_mac[mac] = host
138 self._hosts_by_ip4[ip4] = host
139 self._hosts_by_ip6[ip6] = host
141 def post_init_setup(self):
142 """Additional setup run after creating an interface object."""
144 self.generate_remote_hosts()
146 self._local_ip4 = "172.16.%u.1" % self.sw_if_index
147 self._local_ip4n = socket.inet_pton(socket.AF_INET, self.local_ip4)
149 self._local_ip6 = "fd01:%04x::1" % self.sw_if_index
150 self._local_ip6n = socket.inet_pton(socket.AF_INET6, self.local_ip6)
152 r = self.test.vapi.sw_interface_dump()
154 if intf.sw_if_index == self.sw_if_index:
155 self._name = intf.interface_name.split(b'\0', 1)[0]
156 self._local_mac = ':'.join(intf.l2_address.encode('hex')[i:i + 2]
157 for i in range(0, 12, 2))
162 "Could not find interface with sw_if_index %d "
163 "in interface dump %s" %
164 (self.sw_if_index, repr(r)))
167 def __init__(self, test, index):
169 self.post_init_setup()
170 info("New %s, MAC=%s, remote_ip4=%s, local_ip4=%s" %
171 (self.__name__, self.remote_mac, self.remote_ip4, self.local_ip4))
173 def config_ip4(self):
174 """Configure IPv4 address on the VPP interface."""
175 addr = self.local_ip4n
177 self.test.vapi.sw_interface_add_del_address(
178 self.sw_if_index, addr, addr_len)
180 def configure_ipv4_neighbors(self):
181 """For every remote host assign neighbor's MAC to IPv4 addresses."""
182 for host in self._remote_hosts:
183 macn = host.mac.replace(":", "").decode('hex')
185 self.test.vapi.ip_neighbor_add_del(self.sw_if_index, macn, ipn)
187 def config_ip6(self):
188 """Configure IPv6 address on the VPP interface."""
189 addr = self._local_ip6n
191 self.test.vapi.sw_interface_add_del_address(
192 self.sw_if_index, addr, addr_len, is_ipv6=1)
194 def set_table_ip4(self, table_id):
195 """Set the interface in a IPv4 Table.
197 .. note:: Must be called before configuring IP4 addresses."""
198 self.test.vapi.sw_interface_set_table(
199 self.sw_if_index, 0, table_id)
201 def set_table_ip6(self, table_id):
202 """Set the interface in a IPv6 Table.
204 .. note:: Must be called before configuring IP6 addresses.
206 self.test.vapi.sw_interface_set_table(
207 self.sw_if_index, 1, table_id)
209 def disable_ipv6_ra(self):
210 """Configure IPv6 RA suppress on the VPP interface."""
211 self.test.vapi.sw_interface_ra_suppress(self.sw_if_index)
214 """Put interface ADMIN-UP."""
215 self.test.vapi.sw_interface_set_flags(self.sw_if_index, admin_up_down=1)
217 def add_sub_if(self, sub_if):
218 """Register a sub-interface with this interface.
220 :param sub_if: sub-interface
222 if not hasattr(self, 'sub_if'):
225 if isinstance(self.sub_if, list):
226 self.sub_if.append(sub_if)
230 def enable_mpls(self):
231 """Enable MPLS on the VPP interface."""
232 self.test.vapi.sw_interface_enable_disable_mpls(