blob: 662015eacf731b90e0b9093bfafd6b1c323999fa [file] [log] [blame]
Klement Sekeraf62ae122016-10-11 11:47:09 +02001from abc import abstractmethod, ABCMeta
2import socket
Klement Sekeraf62ae122016-10-11 11:47:09 +02003
Neale Ranns2a3ea492017-04-19 05:24:40 -07004from util import Host, mk_ll_addr
Neale Ranns39f9d8b2017-02-16 21:57:05 -08005from vpp_neighbor import VppNeighbor
Jan65209ed2016-12-05 23:29:17 +01006
Klement Sekeraf62ae122016-10-11 11:47:09 +02007
8class VppInterface(object):
Matej Klotton86d87c42016-11-11 11:38:55 +01009 """Generic VPP interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +020010 __metaclass__ = ABCMeta
11
12 @property
13 def sw_if_index(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010014 """Interface index assigned by VPP."""
Klement Sekeraf62ae122016-10-11 11:47:09 +020015 return self._sw_if_index
16
17 @property
18 def remote_mac(self):
Klement Sekerada505f62017-01-04 12:58:53 +010019 """MAC-address of the remote interface "connected" to this interface"""
Matej Klotton0178d522016-11-04 11:11:44 +010020 return self._remote_hosts[0].mac
Klement Sekeraf62ae122016-10-11 11:47:09 +020021
22 @property
23 def local_mac(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010024 """MAC-address of the VPP interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +020025 return self._local_mac
26
27 @property
28 def local_ip4(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010029 """Local IPv4 address on VPP interface (string)."""
Klement Sekeraf62ae122016-10-11 11:47:09 +020030 return self._local_ip4
31
32 @property
33 def local_ip4n(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010034 """Local IPv4 address - raw, suitable as API parameter."""
Matej Klotton0178d522016-11-04 11:11:44 +010035 return socket.inet_pton(socket.AF_INET, self._local_ip4)
Klement Sekeraf62ae122016-10-11 11:47:09 +020036
37 @property
38 def remote_ip4(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010039 """IPv4 address of remote peer "connected" to this interface."""
Matej Klotton0178d522016-11-04 11:11:44 +010040 return self._remote_hosts[0].ip4
Klement Sekeraf62ae122016-10-11 11:47:09 +020041
42 @property
43 def remote_ip4n(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010044 """IPv4 address of remote peer - raw, suitable as API parameter."""
Matej Klotton0178d522016-11-04 11:11:44 +010045 return socket.inet_pton(socket.AF_INET, self.remote_ip4)
Klement Sekeraf62ae122016-10-11 11:47:09 +020046
47 @property
48 def local_ip6(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010049 """Local IPv6 address on VPP interface (string)."""
Klement Sekeraf62ae122016-10-11 11:47:09 +020050 return self._local_ip6
51
52 @property
53 def local_ip6n(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010054 """Local IPv6 address - raw, suitable as API parameter."""
Matej Klotton0178d522016-11-04 11:11:44 +010055 return socket.inet_pton(socket.AF_INET6, self.local_ip6)
Klement Sekeraf62ae122016-10-11 11:47:09 +020056
57 @property
Neale Ranns2a3ea492017-04-19 05:24:40 -070058 def local_ip6_ll(self):
59 """Local IPv6 linnk-local address on VPP interface (string)."""
60 return self._local_ip6_ll
61
62 @property
63 def local_ip6n_ll(self):
64 """Local IPv6 link-local address - raw, suitable as API parameter."""
65 return self.local_ip6n_ll
66
67 @property
Klement Sekeraf62ae122016-10-11 11:47:09 +020068 def remote_ip6(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010069 """IPv6 address of remote peer "connected" to this interface."""
Matej Klotton0178d522016-11-04 11:11:44 +010070 return self._remote_hosts[0].ip6
Klement Sekeraf62ae122016-10-11 11:47:09 +020071
72 @property
73 def remote_ip6n(self):
74 """IPv6 address of remote peer - raw, suitable as API parameter"""
Matej Klotton0178d522016-11-04 11:11:44 +010075 return socket.inet_pton(socket.AF_INET6, self.remote_ip6)
Klement Sekeraf62ae122016-10-11 11:47:09 +020076
77 @property
78 def name(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010079 """Name of the interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +020080 return self._name
81
82 @property
83 def dump(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010084 """RAW result of sw_interface_dump for this interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +020085 return self._dump
86
87 @property
88 def test(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010089 """Test case creating this interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +020090 return self._test
91
Matej Klotton0178d522016-11-04 11:11:44 +010092 @property
93 def remote_hosts(self):
94 """Remote hosts list"""
95 return self._remote_hosts
96
97 @remote_hosts.setter
98 def remote_hosts(self, value):
Matej Klotton86d87c42016-11-11 11:38:55 +010099 """
100 :param list value: List of remote hosts.
101 """
Matej Klotton0178d522016-11-04 11:11:44 +0100102 self._remote_hosts = value
Matej Klotton86d87c42016-11-11 11:38:55 +0100103 self._hosts_by_mac = {}
104 self._hosts_by_ip4 = {}
105 self._hosts_by_ip6 = {}
106 for host in self._remote_hosts:
107 self._hosts_by_mac[host.mac] = host
108 self._hosts_by_ip4[host.ip4] = host
109 self._hosts_by_ip6[host.ip6] = host
Matej Klotton0178d522016-11-04 11:11:44 +0100110
111 def host_by_mac(self, mac):
Matej Klotton86d87c42016-11-11 11:38:55 +0100112 """
Matej Klottonc5bf07f2016-11-23 15:27:17 +0100113 :param mac: MAC address to find host by.
Matej Klotton86d87c42016-11-11 11:38:55 +0100114 :return: Host object assigned to interface.
115 """
Matej Klotton0178d522016-11-04 11:11:44 +0100116 return self._hosts_by_mac[mac]
117
118 def host_by_ip4(self, ip):
Matej Klotton86d87c42016-11-11 11:38:55 +0100119 """
120 :param ip: IPv4 address to find host by.
121 :return: Host object assigned to interface.
122 """
Matej Klotton0178d522016-11-04 11:11:44 +0100123 return self._hosts_by_ip4[ip]
124
125 def host_by_ip6(self, ip):
Matej Klotton86d87c42016-11-11 11:38:55 +0100126 """
127 :param ip: IPv6 address to find host by.
128 :return: Host object assigned to interface.
129 """
Matej Klotton0178d522016-11-04 11:11:44 +0100130 return self._hosts_by_ip6[ip]
131
132 def generate_remote_hosts(self, count=1):
Matej Klotton86d87c42016-11-11 11:38:55 +0100133 """Generate and add remote hosts for the interface.
134
135 :param int count: Number of generated remote hosts.
136 """
Matej Klotton0178d522016-11-04 11:11:44 +0100137 self._remote_hosts = []
138 self._hosts_by_mac = {}
139 self._hosts_by_ip4 = {}
140 self._hosts_by_ip6 = {}
Klement Sekera7bb873a2016-11-18 07:38:42 +0100141 for i in range(
142 2, count + 2): # 0: network address, 1: local vpp address
Matej Klotton0178d522016-11-04 11:11:44 +0100143 mac = "02:%02x:00:00:ff:%02x" % (self.sw_if_index, i)
144 ip4 = "172.16.%u.%u" % (self.sw_if_index, i)
Klement Sekera46a87ad2017-01-02 08:22:23 +0100145 ip6 = "fd01:%x::%x" % (self.sw_if_index, i)
Neale Ranns2a3ea492017-04-19 05:24:40 -0700146 ip6_ll = mk_ll_addr(mac)
147 host = Host(mac, ip4, ip6, ip6_ll)
Matej Klotton0178d522016-11-04 11:11:44 +0100148 self._remote_hosts.append(host)
149 self._hosts_by_mac[mac] = host
150 self._hosts_by_ip4[ip4] = host
151 self._hosts_by_ip6[ip6] = host
152
Matej Klottonc5bf07f2016-11-23 15:27:17 +0100153 @abstractmethod
154 def __init__(self, test):
155 self._test = test
156
157 self._remote_hosts = []
158 self._hosts_by_mac = {}
159 self._hosts_by_ip4 = {}
160 self._hosts_by_ip6 = {}
Matej Klotton0178d522016-11-04 11:11:44 +0100161
162 self.generate_remote_hosts()
Klement Sekeraf62ae122016-10-11 11:47:09 +0200163
164 self._local_ip4 = "172.16.%u.1" % self.sw_if_index
165 self._local_ip4n = socket.inet_pton(socket.AF_INET, self.local_ip4)
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100166 self.local_ip4_prefix_len = 24
167 self.has_ip4_config = False
168 self.ip4_table_id = 0
Klement Sekeraf62ae122016-10-11 11:47:09 +0200169
Klement Sekera46a87ad2017-01-02 08:22:23 +0100170 self._local_ip6 = "fd01:%x::1" % self.sw_if_index
Klement Sekeraf62ae122016-10-11 11:47:09 +0200171 self._local_ip6n = socket.inet_pton(socket.AF_INET6, self.local_ip6)
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100172 self.local_ip6_prefix_len = 64
173 self.has_ip6_config = False
174 self.ip6_table_id = 0
Klement Sekeraf62ae122016-10-11 11:47:09 +0200175
176 r = self.test.vapi.sw_interface_dump()
177 for intf in r:
178 if intf.sw_if_index == self.sw_if_index:
179 self._name = intf.interface_name.split(b'\0', 1)[0]
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100180 self._local_mac = \
Klement Sekera7bb873a2016-11-18 07:38:42 +0100181 ':'.join(intf.l2_address.encode('hex')[i:i + 2]
182 for i in range(0, 12, 2))
Klement Sekeraf62ae122016-10-11 11:47:09 +0200183 self._dump = intf
184 break
185 else:
186 raise Exception(
187 "Could not find interface with sw_if_index %d "
188 "in interface dump %s" %
189 (self.sw_if_index, repr(r)))
Neale Ranns2a3ea492017-04-19 05:24:40 -0700190 self._local_ip6_ll = mk_ll_addr(self.local_mac)
191 self._local_ip6n_ll = socket.inet_pton(socket.AF_INET6,
192 self.local_ip6_ll)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200193
Klement Sekeraf62ae122016-10-11 11:47:09 +0200194 def config_ip4(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100195 """Configure IPv4 address on the VPP interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200196 self.test.vapi.sw_interface_add_del_address(
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100197 self.sw_if_index, self.local_ip4n, self.local_ip4_prefix_len)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000198 self.has_ip4_config = True
199
200 def unconfig_ip4(self):
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100201 """Remove IPv4 address on the VPP interface."""
Neale Ranns177bbdc2016-11-15 09:46:51 +0000202 try:
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100203 if self.has_ip4_config:
Neale Ranns177bbdc2016-11-15 09:46:51 +0000204 self.test.vapi.sw_interface_add_del_address(
205 self.sw_if_index,
206 self.local_ip4n,
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100207 self.local_ip4_prefix_len,
208 is_add=0)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000209 except AttributeError:
210 self.has_ip4_config = False
211 self.has_ip4_config = False
Klement Sekeraf62ae122016-10-11 11:47:09 +0200212
Neale Rannsbaf2e902017-02-25 04:20:00 -0800213 def configure_ipv4_neighbors(self):
Jane546d3b2016-12-08 13:10:03 +0100214 """For every remote host assign neighbor's MAC to IPv4 addresses.
215
216 :param vrf_id: The FIB table / VRF ID. (Default value = 0)
217 """
Matej Klotton0178d522016-11-04 11:11:44 +0100218 for host in self._remote_hosts:
219 macn = host.mac.replace(":", "").decode('hex')
220 ipn = host.ip4n
Jane546d3b2016-12-08 13:10:03 +0100221 self.test.vapi.ip_neighbor_add_del(
Neale Rannsbaf2e902017-02-25 04:20:00 -0800222 self.sw_if_index, macn, ipn)
Matej Klotton0178d522016-11-04 11:11:44 +0100223
Klement Sekeraf62ae122016-10-11 11:47:09 +0200224 def config_ip6(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100225 """Configure IPv6 address on the VPP interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200226 self.test.vapi.sw_interface_add_del_address(
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100227 self.sw_if_index, self._local_ip6n, self.local_ip6_prefix_len,
228 is_ipv6=1)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000229 self.has_ip6_config = True
230
231 def unconfig_ip6(self):
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100232 """Remove IPv6 address on the VPP interface."""
Neale Ranns177bbdc2016-11-15 09:46:51 +0000233 try:
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100234 if self.has_ip6_config:
Neale Ranns177bbdc2016-11-15 09:46:51 +0000235 self.test.vapi.sw_interface_add_del_address(
236 self.sw_if_index,
237 self.local_ip6n,
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100238 self.local_ip6_prefix_len,
239 is_ipv6=1, is_add=0)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000240 except AttributeError:
241 self.has_ip6_config = False
242 self.has_ip6_config = False
243
Neale Rannsbaf2e902017-02-25 04:20:00 -0800244 def configure_ipv6_neighbors(self):
Jan Gelety057bb8c2016-12-20 17:32:45 +0100245 """For every remote host assign neighbor's MAC to IPv6 addresses.
246
247 :param vrf_id: The FIB table / VRF ID. (Default value = 0)
248 """
Klement Sekera46a87ad2017-01-02 08:22:23 +0100249 for host in self._remote_hosts:
250 macn = host.mac.replace(":", "").decode('hex')
Jan Gelety057bb8c2016-12-20 17:32:45 +0100251 ipn = host.ip6n
Klement Sekera46a87ad2017-01-02 08:22:23 +0100252 self.test.vapi.ip_neighbor_add_del(
Neale Rannsbaf2e902017-02-25 04:20:00 -0800253 self.sw_if_index, macn, ipn, is_ipv6=1)
Klement Sekera46a87ad2017-01-02 08:22:23 +0100254
Neale Ranns177bbdc2016-11-15 09:46:51 +0000255 def unconfig(self):
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100256 """Unconfigure IPv6 and IPv4 address on the VPP interface."""
Neale Ranns177bbdc2016-11-15 09:46:51 +0000257 self.unconfig_ip4()
258 self.unconfig_ip6()
Klement Sekeraf62ae122016-10-11 11:47:09 +0200259
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000260 def set_table_ip4(self, table_id):
261 """Set the interface in a IPv4 Table.
Matej Klotton86d87c42016-11-11 11:38:55 +0100262
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100263 .. note:: Must be called before configuring IP4 addresses.
264 """
265 self.ip4_table_id = table_id
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000266 self.test.vapi.sw_interface_set_table(
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100267 self.sw_if_index, 0, self.ip4_table_id)
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000268
269 def set_table_ip6(self, table_id):
270 """Set the interface in a IPv6 Table.
Matej Klotton86d87c42016-11-11 11:38:55 +0100271
272 .. note:: Must be called before configuring IP6 addresses.
273 """
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100274 self.ip6_table_id = table_id
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000275 self.test.vapi.sw_interface_set_table(
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100276 self.sw_if_index, 1, self.ip6_table_id)
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000277
Klement Sekeraf62ae122016-10-11 11:47:09 +0200278 def disable_ipv6_ra(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100279 """Configure IPv6 RA suppress on the VPP interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200280 self.test.vapi.sw_interface_ra_suppress(self.sw_if_index)
281
Neale Ranns32e1c012016-11-22 17:07:28 +0000282 def ip6_ra_config(self, no=0, suppress=0, send_unicast=0):
Neale Ranns75152282017-01-09 01:00:45 -0800283 """Configure IPv6 RA suppress on the VPP interface."""
284 self.test.vapi.ip6_sw_interface_ra_config(self.sw_if_index,
Neale Ranns32e1c012016-11-22 17:07:28 +0000285 no,
Neale Ranns75152282017-01-09 01:00:45 -0800286 suppress,
287 send_unicast)
288
Neale Ranns87df12d2017-02-18 08:16:41 -0800289 def ip6_ra_prefix(self, address, address_length, is_no=0,
290 off_link=0, no_autoconfig=0, use_default=0):
291 """Configure IPv6 RA suppress on the VPP interface."""
292 self.test.vapi.ip6_sw_interface_ra_prefix(self.sw_if_index,
293 address,
294 address_length,
295 is_no=is_no,
296 off_link=off_link,
297 no_autoconfig=no_autoconfig,
298 use_default=use_default)
299
Klement Sekeraf62ae122016-10-11 11:47:09 +0200300 def admin_up(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100301 """Put interface ADMIN-UP."""
Klement Sekerada505f62017-01-04 12:58:53 +0100302 self.test.vapi.sw_interface_set_flags(self.sw_if_index,
303 admin_up_down=1)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200304
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100305 def admin_down(self):
306 """Put interface ADMIN-down."""
Klement Sekerada505f62017-01-04 12:58:53 +0100307 self.test.vapi.sw_interface_set_flags(self.sw_if_index,
308 admin_up_down=0)
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100309
Neale Ranns75152282017-01-09 01:00:45 -0800310 def ip6_enable(self):
311 """IPv6 Enable interface"""
Klement Sekerada505f62017-01-04 12:58:53 +0100312 self.test.vapi.ip6_sw_interface_enable_disable(self.sw_if_index,
313 enable=1)
Neale Ranns75152282017-01-09 01:00:45 -0800314
315 def ip6_disable(self):
316 """Put interface ADMIN-DOWN."""
Klement Sekerada505f62017-01-04 12:58:53 +0100317 self.test.vapi.ip6_sw_interface_enable_disable(self.sw_if_index,
318 enable=0)
Neale Ranns75152282017-01-09 01:00:45 -0800319
Klement Sekeraf62ae122016-10-11 11:47:09 +0200320 def add_sub_if(self, sub_if):
Matej Klotton86d87c42016-11-11 11:38:55 +0100321 """Register a sub-interface with this interface.
Klement Sekeraf62ae122016-10-11 11:47:09 +0200322
323 :param sub_if: sub-interface
Klement Sekeraf62ae122016-10-11 11:47:09 +0200324 """
325 if not hasattr(self, 'sub_if'):
326 self.sub_if = sub_if
327 else:
328 if isinstance(self.sub_if, list):
329 self.sub_if.append(sub_if)
330 else:
331 self.sub_if = sub_if
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000332
333 def enable_mpls(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100334 """Enable MPLS on the VPP interface."""
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000335 self.test.vapi.sw_interface_enable_disable_mpls(
336 self.sw_if_index)
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100337
Neale Ranns180279b2017-03-16 15:49:09 -0400338 def disable_mpls(self):
339 """Enable MPLS on the VPP interface."""
340 self.test.vapi.sw_interface_enable_disable_mpls(
341 self.sw_if_index, 0)
342
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100343 def is_ip4_entry_in_fib_dump(self, dump):
344 for i in dump:
345 if i.address == self.local_ip4n and \
346 i.address_length == self.local_ip4_prefix_len and \
347 i.table_id == self.ip4_table_id:
348 return True
349 return False
Neale Ranns39f9d8b2017-02-16 21:57:05 -0800350
351 def set_unnumbered(self, ip_sw_if_index):
352 """ Set the interface to unnumbered via ip_sw_if_index """
353 self.test.vapi.sw_interface_set_unnumbered(
354 self.sw_if_index,
355 ip_sw_if_index)
356
Neale Ranns37be7362017-02-21 17:30:26 -0800357 def unset_unnumbered(self, ip_sw_if_index):
Neale Ranns4b919a52017-03-11 05:55:21 -0800358 """ Unset the interface to unnumbered via ip_sw_if_index """
Neale Ranns37be7362017-02-21 17:30:26 -0800359 self.test.vapi.sw_interface_set_unnumbered(
360 self.sw_if_index,
361 ip_sw_if_index,
362 is_add=0)
363
Neale Ranns39f9d8b2017-02-16 21:57:05 -0800364 def set_proxy_arp(self, enable=1):
365 """ Set the interface to enable/disable Proxy ARP """
366 self.test.vapi.proxy_arp_intfc_enable_disable(
367 self.sw_if_index,
368 enable)