api: Cleanup APIs interface.api
[vpp.git] / test / vpp_interface.py
1 import binascii
2 import socket
3 import abc
4
5 import six
6 from six import moves
7
8 from util import Host, mk_ll_addr
9 from vpp_papi import mac_ntop, VppEnum
10 from vpp_ip import VppIpAddress, VppIpPrefix
11 from ipaddress import IPv4Network
12
13 try:
14     text_type = unicode
15 except NameError:
16     text_type = str
17
18
19 @six.add_metaclass(abc.ABCMeta)
20 class VppInterface(object):
21     """Generic VPP interface."""
22
23     @property
24     def sw_if_index(self):
25         """Interface index assigned by VPP."""
26         return self._sw_if_index
27
28     @property
29     def remote_mac(self):
30         """MAC-address of the remote interface "connected" to this interface"""
31         return self._remote_hosts[0].mac
32
33     @property
34     def local_mac(self):
35         """MAC-address of the VPP interface."""
36         return self._local_mac
37
38     @property
39     def local_addr(self):
40         return self._local_addr
41
42     @property
43     def remote_addr(self):
44         return self._remote_addr
45
46     @property
47     def local_ip4(self):
48         """Local IPv4 address on VPP interface (string)."""
49         return self._local_ip4.address
50
51     @local_ip4.setter
52     def local_ip4(self, value):
53         self._local_ip4.address = value
54
55     @property
56     def local_ip4_prefix_len(self):
57         """Local IPv4 prefix length """
58         return self._local_ip4.len
59
60     @local_ip4_prefix_len.setter
61     def local_ip4_prefix_len(self, value):
62         self._local_ip4.len = value
63
64     @property
65     def local_ip4_prefix(self):
66         """Local IPv4 prefix """
67         return self._local_ip4
68
69     @property
70     def local_ip4n(self):
71         """DEPRECATED """
72         """Local IPv4 address - raw, suitable as API parameter."""
73         return socket.inet_pton(socket.AF_INET, self._local_ip4.address)
74
75     @property
76     def remote_ip4(self):
77         """IPv4 address of remote peer "connected" to this interface."""
78         return self._remote_hosts[0].ip4
79
80     @property
81     def remote_ip4n(self):
82         """DEPRECATED """
83         """Local IPv6 address - raw, suitable as API parameter."""
84         return socket.inet_pton(socket.AF_INET, self._remote_hosts[0].ip4)
85
86     @property
87     def local_ip6(self):
88         """Local IPv6 address on VPP interface (string)."""
89         return self._local_ip6.address
90
91     @local_ip6.setter
92     def local_ip6(self, value):
93         self._local_ip6.address = value
94
95     @property
96     def local_ip6_prefix_len(self):
97         """Local IPv6 prefix length """
98         return self._local_ip6.len
99
100     @local_ip6_prefix_len.setter
101     def local_ip6_prefix_len(self, value):
102         self._local_ip6.len = value
103
104     @property
105     def local_ip6_prefix(self):
106         """Local IPv6 prefix """
107         return self._local_ip6
108
109     @property
110     def local_ip6n(self):
111         """DEPRECATED """
112         """Local IPv6 address - raw, suitable as API parameter."""
113         return socket.inet_pton(socket.AF_INET6, self._local_ip6.address)
114
115     @property
116     def remote_ip6(self):
117         """IPv6 address of remote peer "connected" to this interface."""
118         return self._remote_hosts[0].ip6
119
120     @property
121     def remote_ip6n(self):
122         """DEPRECATED """
123         """Local IPv6 address - raw, suitable as API parameter."""
124         return socket.inet_pton(socket.AF_INET6, self._remote_hosts[0].ip6)
125
126     @property
127     def local_ip6_ll(self):
128         """Local IPv6 link-local address on VPP interface (string)."""
129         return self._local_ip6_ll.address
130
131     @property
132     def local_ip6n_ll(self):
133         """DEPRECATED """
134         """Local IPv6 link-local address on VPP interface (string)."""
135         return socket.inet_pton(socket.AF_INET6, self._local_ip6_ll.address)
136
137     @property
138     def remote_ip6_ll(self):
139         """Link-local IPv6 address of remote peer
140         "connected" to this interface."""
141         return self._remote_ip6_ll
142
143     @property
144     def remote_ip6n_ll(self):
145         """DEPRECATED """
146         """Local IPv6 link-local address on VPP interface (string)."""
147         return socket.inet_pton(socket.AF_INET6, self._remote_ip6_ll)
148
149     @property
150     def name(self):
151         """Name of the interface."""
152         return self._name
153
154     @property
155     def dump(self):
156         """RAW result of sw_interface_dump for this interface."""
157         return self._dump
158
159     @property
160     def test(self):
161         """Test case creating this interface."""
162         return self._test
163
164     @property
165     def remote_hosts(self):
166         """Remote hosts list"""
167         return self._remote_hosts
168
169     @remote_hosts.setter
170     def remote_hosts(self, value):
171         """
172         :param list value: List of remote hosts.
173         """
174         self._remote_hosts = value
175         self._hosts_by_mac = {}
176         self._hosts_by_ip4 = {}
177         self._hosts_by_ip6 = {}
178         for host in self._remote_hosts:
179             self._hosts_by_mac[host.mac] = host
180             self._hosts_by_ip4[host.ip4] = host
181             self._hosts_by_ip6[host.ip6] = host
182
183     def host_by_mac(self, mac):
184         """
185         :param mac: MAC address to find host by.
186         :return: Host object assigned to interface.
187         """
188         return self._hosts_by_mac[mac]
189
190     def host_by_ip4(self, ip):
191         """
192         :param ip: IPv4 address to find host by.
193         :return: Host object assigned to interface.
194         """
195         return self._hosts_by_ip4[ip]
196
197     def host_by_ip6(self, ip):
198         """
199         :param ip: IPv6 address to find host by.
200         :return: Host object assigned to interface.
201         """
202         return self._hosts_by_ip6[ip]
203
204     def generate_remote_hosts(self, count=1):
205         """Generate and add remote hosts for the interface.
206
207         :param int count: Number of generated remote hosts.
208         """
209         self._remote_hosts = []
210         self._hosts_by_mac = {}
211         self._hosts_by_ip4 = {}
212         self._hosts_by_ip6 = {}
213         for i in range(
214                 2, count + 2):  # 0: network address, 1: local vpp address
215             mac = "02:%02x:00:00:ff:%02x" % (self.sw_if_index, i)
216             ip4 = "172.16.%u.%u" % (self.sw_if_index, i)
217             ip6 = "fd01:%x::%x" % (self.sw_if_index, i)
218             ip6_ll = mk_ll_addr(mac)
219             host = Host(mac, ip4, ip6, ip6_ll)
220             self._remote_hosts.append(host)
221             self._hosts_by_mac[mac] = host
222             self._hosts_by_ip4[ip4] = host
223             self._hosts_by_ip6[ip6] = host
224
225     @abc.abstractmethod
226     def __init__(self, test):
227         self._test = test
228
229         self._remote_hosts = []
230         self._hosts_by_mac = {}
231         self._hosts_by_ip4 = {}
232         self._hosts_by_ip6 = {}
233
234     def set_mac(self, mac):
235         self._local_mac = str(mac)
236         self._local_ip6_ll = VppIpAddress(mk_ll_addr(self._local_mac))
237         self.test.vapi.sw_interface_set_mac_address(
238             self.sw_if_index, mac.packed)
239
240     def set_sw_if_index(self, sw_if_index):
241         self._sw_if_index = sw_if_index
242
243         self.generate_remote_hosts()
244
245         self._local_ip4 = VppIpPrefix("172.16.%u.1" % self.sw_if_index, 24)
246         self._local_ip4_subnet = "172.16.%u.0" % self.sw_if_index
247         self._local_ip4_bcast = "172.16.%u.255" % self.sw_if_index
248         self.has_ip4_config = False
249         self.ip4_table_id = 0
250
251         self._local_ip6 = VppIpPrefix("fd01:%x::1" % self.sw_if_index, 64)
252         self.has_ip6_config = False
253         self.ip6_table_id = 0
254
255         self._local_addr = {socket.AF_INET: self.local_ip4,
256                             socket.AF_INET6: self.local_ip6}
257         self._remote_addr = {socket.AF_INET: self.remote_ip4,
258                              socket.AF_INET6: self.remote_ip6}
259
260         r = self.test.vapi.sw_interface_dump(sw_if_index=self.sw_if_index)
261         for intf in r:
262             if intf.sw_if_index == self.sw_if_index:
263                 self._name = intf.interface_name.split(b'\0',
264                                                        1)[0].decode('utf8')
265                 self._local_mac = bytes(intf.l2_address)
266                 self._dump = intf
267                 break
268         else:
269             raise Exception(
270                 "Could not find interface with sw_if_index %d "
271                 "in interface dump %s" %
272                 (self.sw_if_index, moves.reprlib.repr(r)))
273         self._local_ip6_ll = VppIpAddress(mk_ll_addr(self.local_mac))
274         self._remote_ip6_ll = mk_ll_addr(self.remote_mac)
275
276     def config_ip4(self):
277         """Configure IPv4 address on the VPP interface."""
278         self.test.vapi.sw_interface_add_del_address(
279             sw_if_index=self.sw_if_index, prefix=self._local_ip4.encode())
280         self.has_ip4_config = True
281
282     def unconfig_ip4(self):
283         """Remove IPv4 address on the VPP interface."""
284         try:
285             if self.has_ip4_config:
286                 self.test.vapi.sw_interface_add_del_address(
287                     sw_if_index=self.sw_if_index,
288                     prefix=self._local_ip4.encode(), is_add=0)
289         except AttributeError:
290             self.has_ip4_config = False
291         self.has_ip4_config = False
292
293     def configure_ipv4_neighbors(self):
294         """For every remote host assign neighbor's MAC to IPv4 addresses.
295
296         :param vrf_id: The FIB table / VRF ID. (Default value = 0)
297         """
298         for host in self._remote_hosts:
299             self.test.vapi.ip_neighbor_add_del(self.sw_if_index,
300                                                host.mac,
301                                                host.ip4)
302
303     def config_ip6(self):
304         """Configure IPv6 address on the VPP interface."""
305         self.test.vapi.sw_interface_add_del_address(
306             sw_if_index=self.sw_if_index, prefix=self._local_ip6.encode())
307         self.has_ip6_config = True
308
309     def unconfig_ip6(self):
310         """Remove IPv6 address on the VPP interface."""
311         try:
312             if self.has_ip6_config:
313                 self.test.vapi.sw_interface_add_del_address(
314                     sw_if_index=self.sw_if_index,
315                     prefix=self._local_ip6.encode(), is_add=0)
316         except AttributeError:
317             self.has_ip6_config = False
318         self.has_ip6_config = False
319
320     def configure_ipv6_neighbors(self):
321         """For every remote host assign neighbor's MAC to IPv6 addresses.
322
323         :param vrf_id: The FIB table / VRF ID. (Default value = 0)
324         """
325         for host in self._remote_hosts:
326             self.test.vapi.ip_neighbor_add_del(self.sw_if_index,
327                                                host.mac,
328                                                host.ip6)
329
330     def unconfig(self):
331         """Unconfigure IPv6 and IPv4 address on the VPP interface."""
332         self.unconfig_ip4()
333         self.unconfig_ip6()
334
335     def set_table_ip4(self, table_id):
336         """Set the interface in a IPv4 Table.
337
338         .. note:: Must be called before configuring IP4 addresses.
339         """
340         self.ip4_table_id = table_id
341         self.test.vapi.sw_interface_set_table(
342             self.sw_if_index, 0, self.ip4_table_id)
343
344     def set_table_ip6(self, table_id):
345         """Set the interface in a IPv6 Table.
346
347         .. note:: Must be called before configuring IP6 addresses.
348         """
349         self.ip6_table_id = table_id
350         self.test.vapi.sw_interface_set_table(
351             self.sw_if_index, 1, self.ip6_table_id)
352
353     def disable_ipv6_ra(self):
354         """Configure IPv6 RA suppress on the VPP interface."""
355         self.test.vapi.sw_interface_ip6nd_ra_config(
356             sw_if_index=self.sw_if_index,
357             suppress=1)
358
359     def ip6_ra_config(self, no=0, suppress=0, send_unicast=0):
360         """Configure IPv6 RA suppress on the VPP interface."""
361         self.test.vapi.sw_interface_ip6nd_ra_config(
362             sw_if_index=self.sw_if_index,
363             is_no=no,
364             suppress=suppress,
365             send_unicast=send_unicast)
366
367     def ip6_ra_prefix(self, prefix, is_no=0,
368                       off_link=0, no_autoconfig=0, use_default=0):
369         """Configure IPv6 RA suppress on the VPP interface.
370
371         prefix can be a string in the format of '<address>/<length_in_bits>'
372         or ipaddress.ipnetwork object (if strict.)"""
373
374         self.test.vapi.sw_interface_ip6nd_ra_prefix(
375             sw_if_index=self.sw_if_index,
376             prefix=prefix,
377             use_default=use_default,
378             off_link=off_link, no_autoconfig=no_autoconfig,
379             is_no=is_no)
380
381     def admin_up(self):
382         """Put interface ADMIN-UP."""
383         self.test.vapi.sw_interface_set_flags(
384             self.sw_if_index,
385             flags=VppEnum.vl_api_if_status_flags_t.IF_STATUS_API_FLAG_ADMIN_UP)
386
387     def admin_down(self):
388         """Put interface ADMIN-down."""
389         self.test.vapi.sw_interface_set_flags(self.sw_if_index,
390                                               flags=0)
391
392     def link_up(self):
393         """Put interface link-state-UP."""
394         self.test.vapi.cli("test interface link-state %s up" % self.name)
395
396     def link_down(self):
397         """Put interface link-state-down."""
398         self.test.vapi.cli("test interface link-state %s down" % self.name)
399
400     def ip6_enable(self):
401         """IPv6 Enable interface"""
402         self.test.vapi.sw_interface_ip6_enable_disable(self.sw_if_index,
403                                                        enable=1)
404
405     def ip6_disable(self):
406         """Put interface ADMIN-DOWN."""
407         self.test.vapi.sw_interface_ip6_enable_disable(self.sw_if_index,
408                                                        enable=0)
409
410     def add_sub_if(self, sub_if):
411         """Register a sub-interface with this interface.
412
413         :param sub_if: sub-interface
414         """
415         if not hasattr(self, 'sub_if'):
416             self.sub_if = sub_if
417         else:
418             if isinstance(self.sub_if, list):
419                 self.sub_if.append(sub_if)
420             else:
421                 self.sub_if = sub_if
422
423     def enable_mpls(self):
424         """Enable MPLS on the VPP interface."""
425         self.test.vapi.sw_interface_set_mpls_enable(self.sw_if_index)
426
427     def disable_mpls(self):
428         """Enable MPLS on the VPP interface."""
429         self.test.vapi.sw_interface_set_mpls_enable(self.sw_if_index, 0)
430
431     def is_ip4_entry_in_fib_dump(self, dump):
432         for i in dump:
433             n = IPv4Network(text_type("%s/%d" % (self.local_ip4,
434                                                  self.local_ip4_prefix_len)))
435             if i.route.prefix == n and \
436                i.route.table_id == self.ip4_table_id:
437                 return True
438         return False
439
440     def set_unnumbered(self, ip_sw_if_index):
441         """ Set the interface to unnumbered via ip_sw_if_index """
442         self.test.vapi.sw_interface_set_unnumbered(ip_sw_if_index,
443                                                    self.sw_if_index)
444
445     def unset_unnumbered(self, ip_sw_if_index):
446         """ Unset the interface to unnumbered via ip_sw_if_index """
447         self.test.vapi.sw_interface_set_unnumbered(ip_sw_if_index,
448                                                    self.sw_if_index, is_add=0)
449
450     def set_proxy_arp(self, enable=1):
451         """ Set the interface to enable/disable Proxy ARP """
452         self.test.vapi.proxy_arp_intfc_enable_disable(
453             self.sw_if_index,
454             enable)
455
456     def query_vpp_config(self):
457         dump = self.test.vapi.sw_interface_dump(sw_if_index=self.sw_if_index)
458         return self.is_interface_config_in_dump(dump)
459
460     def get_interface_config_from_dump(self, dump):
461         for i in dump:
462             if i.interface_name.rstrip(' \t\r\n\0') == self.name and \
463                     i.sw_if_index == self.sw_if_index:
464                 return i
465         else:
466             return None
467
468     def is_interface_config_in_dump(self, dump):
469         return self.get_interface_config_from_dump(dump) is not None
470
471     def assert_interface_state(self, admin_up_down, link_up_down,
472                                expect_event=False):
473         if expect_event:
474             event = self.test.vapi.wait_for_event(timeout=1,
475                                                   name='sw_interface_event')
476             self.test.assert_equal(event.sw_if_index, self.sw_if_index,
477                                    "sw_if_index")
478             self.test.assert_equal((event.flags & 1), admin_up_down,
479                                    "admin state")
480             self.test.assert_equal((event.flags & 2), link_up_down,
481                                    "link state")
482         dump = self.test.vapi.sw_interface_dump()
483         if_state = self.get_interface_config_from_dump(dump)
484         self.test.assert_equal((if_state.flags & 1), admin_up_down,
485                                "admin state")
486         self.test.assert_equal((if_state.flags & 2), link_up_down,
487                                "link state")
488
489     def __str__(self):
490         return self.name
491
492     def get_rx_stats(self):
493         c = self.test.statistics.get_counter("^/if/rx$")
494         return c[0][self.sw_if_index]
495
496     def get_tx_stats(self):
497         c = self.test.statistics.get_counter("^/if/tx$")
498         return c[0][self.sw_if_index]