blob: 5dba0978cb94941e3cbd9a9079ebf82ccce4786f [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
Jan65209ed2016-12-05 23:29:17 +01004from util import Host
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
58 def remote_ip6(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010059 """IPv6 address of remote peer "connected" to this interface."""
Matej Klotton0178d522016-11-04 11:11:44 +010060 return self._remote_hosts[0].ip6
Klement Sekeraf62ae122016-10-11 11:47:09 +020061
62 @property
63 def remote_ip6n(self):
64 """IPv6 address of remote peer - raw, suitable as API parameter"""
Matej Klotton0178d522016-11-04 11:11:44 +010065 return socket.inet_pton(socket.AF_INET6, self.remote_ip6)
Klement Sekeraf62ae122016-10-11 11:47:09 +020066
67 @property
68 def name(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010069 """Name of the interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +020070 return self._name
71
72 @property
73 def dump(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010074 """RAW result of sw_interface_dump for this interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +020075 return self._dump
76
77 @property
78 def test(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010079 """Test case creating this interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +020080 return self._test
81
Matej Klotton0178d522016-11-04 11:11:44 +010082 @property
83 def remote_hosts(self):
84 """Remote hosts list"""
85 return self._remote_hosts
86
87 @remote_hosts.setter
88 def remote_hosts(self, value):
Matej Klotton86d87c42016-11-11 11:38:55 +010089 """
90 :param list value: List of remote hosts.
91 """
Matej Klotton0178d522016-11-04 11:11:44 +010092 self._remote_hosts = value
Matej Klotton86d87c42016-11-11 11:38:55 +010093 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
Matej Klotton0178d522016-11-04 11:11:44 +0100100
101 def host_by_mac(self, mac):
Matej Klotton86d87c42016-11-11 11:38:55 +0100102 """
Matej Klottonc5bf07f2016-11-23 15:27:17 +0100103 :param mac: MAC address to find host by.
Matej Klotton86d87c42016-11-11 11:38:55 +0100104 :return: Host object assigned to interface.
105 """
Matej Klotton0178d522016-11-04 11:11:44 +0100106 return self._hosts_by_mac[mac]
107
108 def host_by_ip4(self, ip):
Matej Klotton86d87c42016-11-11 11:38:55 +0100109 """
110 :param ip: IPv4 address to find host by.
111 :return: Host object assigned to interface.
112 """
Matej Klotton0178d522016-11-04 11:11:44 +0100113 return self._hosts_by_ip4[ip]
114
115 def host_by_ip6(self, ip):
Matej Klotton86d87c42016-11-11 11:38:55 +0100116 """
117 :param ip: IPv6 address to find host by.
118 :return: Host object assigned to interface.
119 """
Matej Klotton0178d522016-11-04 11:11:44 +0100120 return self._hosts_by_ip6[ip]
121
122 def generate_remote_hosts(self, count=1):
Matej Klotton86d87c42016-11-11 11:38:55 +0100123 """Generate and add remote hosts for the interface.
124
125 :param int count: Number of generated remote hosts.
126 """
Matej Klotton0178d522016-11-04 11:11:44 +0100127 self._remote_hosts = []
128 self._hosts_by_mac = {}
129 self._hosts_by_ip4 = {}
130 self._hosts_by_ip6 = {}
Klement Sekera7bb873a2016-11-18 07:38:42 +0100131 for i in range(
132 2, count + 2): # 0: network address, 1: local vpp address
Matej Klotton0178d522016-11-04 11:11:44 +0100133 mac = "02:%02x:00:00:ff:%02x" % (self.sw_if_index, i)
134 ip4 = "172.16.%u.%u" % (self.sw_if_index, i)
Klement Sekera46a87ad2017-01-02 08:22:23 +0100135 ip6 = "fd01:%x::%x" % (self.sw_if_index, i)
Matej Klotton0178d522016-11-04 11:11:44 +0100136 host = Host(mac, ip4, ip6)
137 self._remote_hosts.append(host)
138 self._hosts_by_mac[mac] = host
139 self._hosts_by_ip4[ip4] = host
140 self._hosts_by_ip6[ip6] = host
141
Matej Klottonc5bf07f2016-11-23 15:27:17 +0100142 @abstractmethod
143 def __init__(self, test):
144 self._test = test
145
146 self._remote_hosts = []
147 self._hosts_by_mac = {}
148 self._hosts_by_ip4 = {}
149 self._hosts_by_ip6 = {}
Matej Klotton0178d522016-11-04 11:11:44 +0100150
151 self.generate_remote_hosts()
Klement Sekeraf62ae122016-10-11 11:47:09 +0200152
153 self._local_ip4 = "172.16.%u.1" % self.sw_if_index
154 self._local_ip4n = socket.inet_pton(socket.AF_INET, self.local_ip4)
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100155 self.local_ip4_prefix_len = 24
156 self.has_ip4_config = False
157 self.ip4_table_id = 0
Klement Sekeraf62ae122016-10-11 11:47:09 +0200158
Klement Sekera46a87ad2017-01-02 08:22:23 +0100159 self._local_ip6 = "fd01:%x::1" % self.sw_if_index
Klement Sekeraf62ae122016-10-11 11:47:09 +0200160 self._local_ip6n = socket.inet_pton(socket.AF_INET6, self.local_ip6)
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100161 self.local_ip6_prefix_len = 64
162 self.has_ip6_config = False
163 self.ip6_table_id = 0
Klement Sekeraf62ae122016-10-11 11:47:09 +0200164
165 r = self.test.vapi.sw_interface_dump()
166 for intf in r:
167 if intf.sw_if_index == self.sw_if_index:
168 self._name = intf.interface_name.split(b'\0', 1)[0]
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100169 self._local_mac = \
Klement Sekera7bb873a2016-11-18 07:38:42 +0100170 ':'.join(intf.l2_address.encode('hex')[i:i + 2]
171 for i in range(0, 12, 2))
Klement Sekeraf62ae122016-10-11 11:47:09 +0200172 self._dump = intf
173 break
174 else:
175 raise Exception(
176 "Could not find interface with sw_if_index %d "
177 "in interface dump %s" %
178 (self.sw_if_index, repr(r)))
179
Klement Sekeraf62ae122016-10-11 11:47:09 +0200180 def config_ip4(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100181 """Configure IPv4 address on the VPP interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200182 self.test.vapi.sw_interface_add_del_address(
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100183 self.sw_if_index, self.local_ip4n, self.local_ip4_prefix_len)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000184 self.has_ip4_config = True
185
186 def unconfig_ip4(self):
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100187 """Remove IPv4 address on the VPP interface."""
Neale Ranns177bbdc2016-11-15 09:46:51 +0000188 try:
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100189 if self.has_ip4_config:
Neale Ranns177bbdc2016-11-15 09:46:51 +0000190 self.test.vapi.sw_interface_add_del_address(
191 self.sw_if_index,
192 self.local_ip4n,
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100193 self.local_ip4_prefix_len,
194 is_add=0)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000195 except AttributeError:
196 self.has_ip4_config = False
197 self.has_ip4_config = False
Klement Sekeraf62ae122016-10-11 11:47:09 +0200198
Neale Rannsbaf2e902017-02-25 04:20:00 -0800199 def configure_ipv4_neighbors(self):
Jane546d3b2016-12-08 13:10:03 +0100200 """For every remote host assign neighbor's MAC to IPv4 addresses.
201
202 :param vrf_id: The FIB table / VRF ID. (Default value = 0)
203 """
Matej Klotton0178d522016-11-04 11:11:44 +0100204 for host in self._remote_hosts:
205 macn = host.mac.replace(":", "").decode('hex')
206 ipn = host.ip4n
Jane546d3b2016-12-08 13:10:03 +0100207 self.test.vapi.ip_neighbor_add_del(
Neale Rannsbaf2e902017-02-25 04:20:00 -0800208 self.sw_if_index, macn, ipn)
Matej Klotton0178d522016-11-04 11:11:44 +0100209
Klement Sekeraf62ae122016-10-11 11:47:09 +0200210 def config_ip6(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100211 """Configure IPv6 address on the VPP interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200212 self.test.vapi.sw_interface_add_del_address(
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100213 self.sw_if_index, self._local_ip6n, self.local_ip6_prefix_len,
214 is_ipv6=1)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000215 self.has_ip6_config = True
216
217 def unconfig_ip6(self):
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100218 """Remove IPv6 address on the VPP interface."""
Neale Ranns177bbdc2016-11-15 09:46:51 +0000219 try:
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100220 if self.has_ip6_config:
Neale Ranns177bbdc2016-11-15 09:46:51 +0000221 self.test.vapi.sw_interface_add_del_address(
222 self.sw_if_index,
223 self.local_ip6n,
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100224 self.local_ip6_prefix_len,
225 is_ipv6=1, is_add=0)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000226 except AttributeError:
227 self.has_ip6_config = False
228 self.has_ip6_config = False
229
Neale Rannsbaf2e902017-02-25 04:20:00 -0800230 def configure_ipv6_neighbors(self):
Jan Gelety057bb8c2016-12-20 17:32:45 +0100231 """For every remote host assign neighbor's MAC to IPv6 addresses.
232
233 :param vrf_id: The FIB table / VRF ID. (Default value = 0)
234 """
Klement Sekera46a87ad2017-01-02 08:22:23 +0100235 for host in self._remote_hosts:
236 macn = host.mac.replace(":", "").decode('hex')
Jan Gelety057bb8c2016-12-20 17:32:45 +0100237 ipn = host.ip6n
Klement Sekera46a87ad2017-01-02 08:22:23 +0100238 self.test.vapi.ip_neighbor_add_del(
Neale Rannsbaf2e902017-02-25 04:20:00 -0800239 self.sw_if_index, macn, ipn, is_ipv6=1)
Klement Sekera46a87ad2017-01-02 08:22:23 +0100240
Neale Ranns177bbdc2016-11-15 09:46:51 +0000241 def unconfig(self):
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100242 """Unconfigure IPv6 and IPv4 address on the VPP interface."""
Neale Ranns177bbdc2016-11-15 09:46:51 +0000243 self.unconfig_ip4()
244 self.unconfig_ip6()
Klement Sekeraf62ae122016-10-11 11:47:09 +0200245
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000246 def set_table_ip4(self, table_id):
247 """Set the interface in a IPv4 Table.
Matej Klotton86d87c42016-11-11 11:38:55 +0100248
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100249 .. note:: Must be called before configuring IP4 addresses.
250 """
251 self.ip4_table_id = table_id
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000252 self.test.vapi.sw_interface_set_table(
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100253 self.sw_if_index, 0, self.ip4_table_id)
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000254
255 def set_table_ip6(self, table_id):
256 """Set the interface in a IPv6 Table.
Matej Klotton86d87c42016-11-11 11:38:55 +0100257
258 .. note:: Must be called before configuring IP6 addresses.
259 """
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100260 self.ip6_table_id = table_id
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000261 self.test.vapi.sw_interface_set_table(
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100262 self.sw_if_index, 1, self.ip6_table_id)
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000263
Klement Sekeraf62ae122016-10-11 11:47:09 +0200264 def disable_ipv6_ra(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100265 """Configure IPv6 RA suppress on the VPP interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200266 self.test.vapi.sw_interface_ra_suppress(self.sw_if_index)
267
Neale Ranns32e1c012016-11-22 17:07:28 +0000268 def ip6_ra_config(self, no=0, suppress=0, send_unicast=0):
Neale Ranns75152282017-01-09 01:00:45 -0800269 """Configure IPv6 RA suppress on the VPP interface."""
270 self.test.vapi.ip6_sw_interface_ra_config(self.sw_if_index,
Neale Ranns32e1c012016-11-22 17:07:28 +0000271 no,
Neale Ranns75152282017-01-09 01:00:45 -0800272 suppress,
273 send_unicast)
274
Neale Ranns87df12d2017-02-18 08:16:41 -0800275 def ip6_ra_prefix(self, address, address_length, is_no=0,
276 off_link=0, no_autoconfig=0, use_default=0):
277 """Configure IPv6 RA suppress on the VPP interface."""
278 self.test.vapi.ip6_sw_interface_ra_prefix(self.sw_if_index,
279 address,
280 address_length,
281 is_no=is_no,
282 off_link=off_link,
283 no_autoconfig=no_autoconfig,
284 use_default=use_default)
285
Klement Sekeraf62ae122016-10-11 11:47:09 +0200286 def admin_up(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100287 """Put interface ADMIN-UP."""
Klement Sekerada505f62017-01-04 12:58:53 +0100288 self.test.vapi.sw_interface_set_flags(self.sw_if_index,
289 admin_up_down=1)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200290
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100291 def admin_down(self):
292 """Put interface ADMIN-down."""
Klement Sekerada505f62017-01-04 12:58:53 +0100293 self.test.vapi.sw_interface_set_flags(self.sw_if_index,
294 admin_up_down=0)
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100295
Neale Ranns75152282017-01-09 01:00:45 -0800296 def ip6_enable(self):
297 """IPv6 Enable interface"""
Klement Sekerada505f62017-01-04 12:58:53 +0100298 self.test.vapi.ip6_sw_interface_enable_disable(self.sw_if_index,
299 enable=1)
Neale Ranns75152282017-01-09 01:00:45 -0800300
301 def ip6_disable(self):
302 """Put interface ADMIN-DOWN."""
Klement Sekerada505f62017-01-04 12:58:53 +0100303 self.test.vapi.ip6_sw_interface_enable_disable(self.sw_if_index,
304 enable=0)
Neale Ranns75152282017-01-09 01:00:45 -0800305
Klement Sekeraf62ae122016-10-11 11:47:09 +0200306 def add_sub_if(self, sub_if):
Matej Klotton86d87c42016-11-11 11:38:55 +0100307 """Register a sub-interface with this interface.
Klement Sekeraf62ae122016-10-11 11:47:09 +0200308
309 :param sub_if: sub-interface
Klement Sekeraf62ae122016-10-11 11:47:09 +0200310 """
311 if not hasattr(self, 'sub_if'):
312 self.sub_if = sub_if
313 else:
314 if isinstance(self.sub_if, list):
315 self.sub_if.append(sub_if)
316 else:
317 self.sub_if = sub_if
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000318
319 def enable_mpls(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100320 """Enable MPLS on the VPP interface."""
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000321 self.test.vapi.sw_interface_enable_disable_mpls(
322 self.sw_if_index)
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100323
Neale Ranns180279b2017-03-16 15:49:09 -0400324 def disable_mpls(self):
325 """Enable MPLS on the VPP interface."""
326 self.test.vapi.sw_interface_enable_disable_mpls(
327 self.sw_if_index, 0)
328
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100329 def is_ip4_entry_in_fib_dump(self, dump):
330 for i in dump:
331 if i.address == self.local_ip4n and \
332 i.address_length == self.local_ip4_prefix_len and \
333 i.table_id == self.ip4_table_id:
334 return True
335 return False
Neale Ranns39f9d8b2017-02-16 21:57:05 -0800336
337 def set_unnumbered(self, ip_sw_if_index):
338 """ Set the interface to unnumbered via ip_sw_if_index """
339 self.test.vapi.sw_interface_set_unnumbered(
340 self.sw_if_index,
341 ip_sw_if_index)
342
Neale Ranns37be7362017-02-21 17:30:26 -0800343 def unset_unnumbered(self, ip_sw_if_index):
Neale Ranns4b919a52017-03-11 05:55:21 -0800344 """ Unset the interface to unnumbered via ip_sw_if_index """
Neale Ranns37be7362017-02-21 17:30:26 -0800345 self.test.vapi.sw_interface_set_unnumbered(
346 self.sw_if_index,
347 ip_sw_if_index,
348 is_add=0)
349
Neale Ranns39f9d8b2017-02-16 21:57:05 -0800350 def set_proxy_arp(self, enable=1):
351 """ Set the interface to enable/disable Proxy ARP """
352 self.test.vapi.proxy_arp_intfc_enable_disable(
353 self.sw_if_index,
354 enable)