make test: fix missing log/packet messages
[vpp.git] / test / vpp_interface.py
1 from abc import abstractmethod, ABCMeta
2 import socket
3
4
5 class VppInterface(object):
6     """Generic VPP interface."""
7     __metaclass__ = ABCMeta
8
9     @property
10     def sw_if_index(self):
11         """Interface index assigned by VPP."""
12         return self._sw_if_index
13
14     @property
15     def remote_mac(self):
16         """MAC-address of the remote interface "connected" to this interface."""
17         return self._remote_hosts[0].mac
18
19     @property
20     def local_mac(self):
21         """MAC-address of the VPP interface."""
22         return self._local_mac
23
24     @property
25     def local_ip4(self):
26         """Local IPv4 address on VPP interface (string)."""
27         return self._local_ip4
28
29     @property
30     def local_ip4n(self):
31         """Local IPv4 address - raw, suitable as API parameter."""
32         return socket.inet_pton(socket.AF_INET, self._local_ip4)
33
34     @property
35     def remote_ip4(self):
36         """IPv4 address of remote peer "connected" to this interface."""
37         return self._remote_hosts[0].ip4
38
39     @property
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)
43
44     @property
45     def local_ip6(self):
46         """Local IPv6 address on VPP interface (string)."""
47         return self._local_ip6
48
49     @property
50     def local_ip6n(self):
51         """Local IPv6 address - raw, suitable as API parameter."""
52         return socket.inet_pton(socket.AF_INET6, self.local_ip6)
53
54     @property
55     def remote_ip6(self):
56         """IPv6 address of remote peer "connected" to this interface."""
57         return self._remote_hosts[0].ip6
58
59     @property
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)
63
64     @property
65     def name(self):
66         """Name of the interface."""
67         return self._name
68
69     @property
70     def dump(self):
71         """RAW result of sw_interface_dump for this interface."""
72         return self._dump
73
74     @property
75     def test(self):
76         """Test case creating this interface."""
77         return self._test
78
79     @property
80     def remote_hosts(self):
81         """Remote hosts list"""
82         return self._remote_hosts
83
84     @remote_hosts.setter
85     def remote_hosts(self, value):
86         """
87         :param list value: List of remote hosts.
88         """
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
97
98     def host_by_mac(self, mac):
99         """
100         :param mac: MAC address to find host by.
101         :return: Host object assigned to interface.
102         """
103         return self._hosts_by_mac[mac]
104
105     def host_by_ip4(self, ip):
106         """
107         :param ip: IPv4 address to find host by.
108         :return: Host object assigned to interface.
109         """
110         return self._hosts_by_ip4[ip]
111
112     def host_by_ip6(self, ip):
113         """
114         :param ip: IPv6 address to find host by.
115         :return: Host object assigned to interface.
116         """
117         return self._hosts_by_ip6[ip]
118
119     def generate_remote_hosts(self, count=1):
120         """Generate and add remote hosts for the interface.
121
122         :param int count: Number of generated remote hosts.
123         """
124         self._remote_hosts = []
125         self._hosts_by_mac = {}
126         self._hosts_by_ip4 = {}
127         self._hosts_by_ip6 = {}
128         for i in range(
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
138
139     @abstractmethod
140     def __init__(self, test):
141         self._test = test
142
143         self._remote_hosts = []
144         self._hosts_by_mac = {}
145         self._hosts_by_ip4 = {}
146         self._hosts_by_ip6 = {}
147
148         self.generate_remote_hosts()
149
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)
152
153         self._local_ip6 = "fd01:%04x::1" % self.sw_if_index
154         self._local_ip6n = socket.inet_pton(socket.AF_INET6, self.local_ip6)
155
156         r = self.test.vapi.sw_interface_dump()
157         for intf in r:
158             if intf.sw_if_index == self.sw_if_index:
159                 self._name = intf.interface_name.split(b'\0', 1)[0]
160                 self._local_mac =\
161                     ':'.join(intf.l2_address.encode('hex')[i:i + 2]
162                              for i in range(0, 12, 2))
163                 self._dump = intf
164                 break
165         else:
166             raise Exception(
167                 "Could not find interface with sw_if_index %d "
168                 "in interface dump %s" %
169                 (self.sw_if_index, repr(r)))
170
171     def config_ip4(self):
172         """Configure IPv4 address on the VPP interface."""
173         addr = self.local_ip4n
174         addr_len = 24
175         self.test.vapi.sw_interface_add_del_address(
176             self.sw_if_index, addr, addr_len)
177         self.has_ip4_config = True
178
179     def unconfig_ip4(self):
180         """Remove IPv4 address on the VPP interface"""
181         try:
182             if (self.has_ip4_config):
183                 self.test.vapi.sw_interface_add_del_address(
184                     self.sw_if_index,
185                     self.local_ip4n,
186                     24, is_add=0)
187         except AttributeError:
188             self.has_ip4_config = False
189         self.has_ip4_config = False
190
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')
195             ipn = host.ip4n
196             self.test.vapi.ip_neighbor_add_del(self.sw_if_index, macn, ipn)
197
198     def config_ip6(self):
199         """Configure IPv6 address on the VPP interface."""
200         addr = self._local_ip6n
201         addr_len = 64
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
205
206     def unconfig_ip6(self):
207         """Remove IPv6 address on the VPP interface"""
208         try:
209             if (self.has_ip6_config):
210                 self.test.vapi.sw_interface_add_del_address(
211                     self.sw_if_index,
212                     self.local_ip6n,
213                     64, is_ipv6=1, is_add=0)
214         except AttributeError:
215             self.has_ip6_config = False
216         self.has_ip6_config = False
217
218     def unconfig(self):
219         """Unconfigure IPv6 and IPv4 address on the VPP interface"""
220         self.unconfig_ip4()
221         self.unconfig_ip6()
222
223     def set_table_ip4(self, table_id):
224         """Set the interface in a IPv4 Table.
225
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)
229
230     def set_table_ip6(self, table_id):
231         """Set the interface in a IPv6 Table.
232
233         .. note:: Must be called before configuring IP6 addresses.
234         """
235         self.test.vapi.sw_interface_set_table(
236             self.sw_if_index, 1, table_id)
237
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)
241
242     def admin_up(self):
243         """Put interface ADMIN-UP."""
244         self.test.vapi.sw_interface_set_flags(self.sw_if_index, admin_up_down=1)
245
246     def add_sub_if(self, sub_if):
247         """Register a sub-interface with this interface.
248
249         :param sub_if: sub-interface
250         """
251         if not hasattr(self, 'sub_if'):
252             self.sub_if = sub_if
253         else:
254             if isinstance(self.sub_if, list):
255                 self.sub_if.append(sub_if)
256             else:
257                 self.sub_if = sub_if
258
259     def enable_mpls(self):
260         """Enable MPLS on the VPP interface."""
261         self.test.vapi.sw_interface_enable_disable_mpls(
262             self.sw_if_index)