IPv6 NS RS tests and fixes
[vpp.git] / test / vpp_interface.py
1 from abc import abstractmethod, ABCMeta
2 import socket
3
4 from util import Host
5
6
7 class VppInterface(object):
8     """Generic VPP interface."""
9     __metaclass__ = ABCMeta
10
11     @property
12     def sw_if_index(self):
13         """Interface index assigned by VPP."""
14         return self._sw_if_index
15
16     @property
17     def remote_mac(self):
18         """MAC-address of the remote interface "connected" to this interface."""
19         return self._remote_hosts[0].mac
20
21     @property
22     def local_mac(self):
23         """MAC-address of the VPP interface."""
24         return self._local_mac
25
26     @property
27     def local_ip4(self):
28         """Local IPv4 address on VPP interface (string)."""
29         return self._local_ip4
30
31     @property
32     def local_ip4n(self):
33         """Local IPv4 address - raw, suitable as API parameter."""
34         return socket.inet_pton(socket.AF_INET, self._local_ip4)
35
36     @property
37     def remote_ip4(self):
38         """IPv4 address of remote peer "connected" to this interface."""
39         return self._remote_hosts[0].ip4
40
41     @property
42     def remote_ip4n(self):
43         """IPv4 address of remote peer - raw, suitable as API parameter."""
44         return socket.inet_pton(socket.AF_INET, self.remote_ip4)
45
46     @property
47     def local_ip6(self):
48         """Local IPv6 address on VPP interface (string)."""
49         return self._local_ip6
50
51     @property
52     def local_ip6n(self):
53         """Local IPv6 address - raw, suitable as API parameter."""
54         return socket.inet_pton(socket.AF_INET6, self.local_ip6)
55
56     @property
57     def remote_ip6(self):
58         """IPv6 address of remote peer "connected" to this interface."""
59         return self._remote_hosts[0].ip6
60
61     @property
62     def remote_ip6n(self):
63         """IPv6 address of remote peer - raw, suitable as API parameter"""
64         return socket.inet_pton(socket.AF_INET6, self.remote_ip6)
65
66     @property
67     def name(self):
68         """Name of the interface."""
69         return self._name
70
71     @property
72     def dump(self):
73         """RAW result of sw_interface_dump for this interface."""
74         return self._dump
75
76     @property
77     def test(self):
78         """Test case creating this interface."""
79         return self._test
80
81     @property
82     def remote_hosts(self):
83         """Remote hosts list"""
84         return self._remote_hosts
85
86     @remote_hosts.setter
87     def remote_hosts(self, value):
88         """
89         :param list value: List of remote hosts.
90         """
91         self._remote_hosts = value
92         self._hosts_by_mac = {}
93         self._hosts_by_ip4 = {}
94         self._hosts_by_ip6 = {}
95         for host in self._remote_hosts:
96             self._hosts_by_mac[host.mac] = host
97             self._hosts_by_ip4[host.ip4] = host
98             self._hosts_by_ip6[host.ip6] = host
99
100     def host_by_mac(self, mac):
101         """
102         :param mac: MAC address to find host by.
103         :return: Host object assigned to interface.
104         """
105         return self._hosts_by_mac[mac]
106
107     def host_by_ip4(self, ip):
108         """
109         :param ip: IPv4 address to find host by.
110         :return: Host object assigned to interface.
111         """
112         return self._hosts_by_ip4[ip]
113
114     def host_by_ip6(self, ip):
115         """
116         :param ip: IPv6 address to find host by.
117         :return: Host object assigned to interface.
118         """
119         return self._hosts_by_ip6[ip]
120
121     def generate_remote_hosts(self, count=1):
122         """Generate and add remote hosts for the interface.
123
124         :param int count: Number of generated remote hosts.
125         """
126         self._remote_hosts = []
127         self._hosts_by_mac = {}
128         self._hosts_by_ip4 = {}
129         self._hosts_by_ip6 = {}
130         for i in range(
131                 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
140
141     @abstractmethod
142     def __init__(self, test):
143         self._test = test
144
145         self._remote_hosts = []
146         self._hosts_by_mac = {}
147         self._hosts_by_ip4 = {}
148         self._hosts_by_ip6 = {}
149
150         self.generate_remote_hosts()
151
152         self._local_ip4 = "172.16.%u.1" % self.sw_if_index
153         self._local_ip4n = socket.inet_pton(socket.AF_INET, self.local_ip4)
154         self.local_ip4_prefix_len = 24
155         self.has_ip4_config = False
156         self.ip4_table_id = 0
157
158         self._local_ip6 = "fd01:%04x::1" % self.sw_if_index
159         self._local_ip6n = socket.inet_pton(socket.AF_INET6, self.local_ip6)
160         self.local_ip6_prefix_len = 64
161         self.has_ip6_config = False
162         self.ip6_table_id = 0
163
164         r = self.test.vapi.sw_interface_dump()
165         for intf in r:
166             if intf.sw_if_index == self.sw_if_index:
167                 self._name = intf.interface_name.split(b'\0', 1)[0]
168                 self._local_mac = \
169                     ':'.join(intf.l2_address.encode('hex')[i:i + 2]
170                              for i in range(0, 12, 2))
171                 self._dump = intf
172                 break
173         else:
174             raise Exception(
175                 "Could not find interface with sw_if_index %d "
176                 "in interface dump %s" %
177                 (self.sw_if_index, repr(r)))
178
179     def config_ip4(self):
180         """Configure IPv4 address on the VPP interface."""
181         self.test.vapi.sw_interface_add_del_address(
182             self.sw_if_index, self.local_ip4n, self.local_ip4_prefix_len)
183         self.has_ip4_config = True
184
185     def unconfig_ip4(self):
186         """Remove IPv4 address on the VPP interface."""
187         try:
188             if self.has_ip4_config:
189                 self.test.vapi.sw_interface_add_del_address(
190                     self.sw_if_index,
191                     self.local_ip4n,
192                     self.local_ip4_prefix_len,
193                     is_add=0)
194         except AttributeError:
195             self.has_ip4_config = False
196         self.has_ip4_config = False
197
198     def configure_ipv4_neighbors(self):
199         """For every remote host assign neighbor's MAC to IPv4 addresses."""
200         for host in self._remote_hosts:
201             macn = host.mac.replace(":", "").decode('hex')
202             ipn = host.ip4n
203             self.test.vapi.ip_neighbor_add_del(self.sw_if_index, macn, ipn)
204
205     def config_ip6(self):
206         """Configure IPv6 address on the VPP interface."""
207         self.test.vapi.sw_interface_add_del_address(
208             self.sw_if_index, self._local_ip6n, self.local_ip6_prefix_len,
209             is_ipv6=1)
210         self.has_ip6_config = True
211
212     def unconfig_ip6(self):
213         """Remove IPv6 address on the VPP interface."""
214         try:
215             if self.has_ip6_config:
216                 self.test.vapi.sw_interface_add_del_address(
217                     self.sw_if_index,
218                     self.local_ip6n,
219                     self.local_ip6_prefix_len,
220                     is_ipv6=1, is_add=0)
221         except AttributeError:
222             self.has_ip6_config = False
223         self.has_ip6_config = False
224
225     def unconfig(self):
226         """Unconfigure IPv6 and IPv4 address on the VPP interface."""
227         self.unconfig_ip4()
228         self.unconfig_ip6()
229
230     def set_table_ip4(self, table_id):
231         """Set the interface in a IPv4 Table.
232
233         .. note:: Must be called before configuring IP4 addresses.
234         """
235         self.ip4_table_id = table_id
236         self.test.vapi.sw_interface_set_table(
237             self.sw_if_index, 0, self.ip4_table_id)
238
239     def set_table_ip6(self, table_id):
240         """Set the interface in a IPv6 Table.
241
242         .. note:: Must be called before configuring IP6 addresses.
243         """
244         self.ip6_table_id = table_id
245         self.test.vapi.sw_interface_set_table(
246             self.sw_if_index, 1, self.ip6_table_id)
247
248     def disable_ipv6_ra(self):
249         """Configure IPv6 RA suppress on the VPP interface."""
250         self.test.vapi.sw_interface_ra_suppress(self.sw_if_index)
251
252     def ip6_ra_config(self, suppress=0, send_unicast=0):
253         """Configure IPv6 RA suppress on the VPP interface."""
254         self.test.vapi.ip6_sw_interface_ra_config(self.sw_if_index,
255                                                   suppress,
256                                                   send_unicast)
257
258     def admin_up(self):
259         """Put interface ADMIN-UP."""
260         self.test.vapi.sw_interface_set_flags(self.sw_if_index, admin_up_down=1)
261
262     def admin_down(self):
263         """Put interface ADMIN-down."""
264         self.test.vapi.sw_interface_set_flags(self.sw_if_index, admin_up_down=0)
265
266     def ip6_enable(self):
267         """IPv6 Enable interface"""
268         self.test.vapi.ip6_sw_interface_enable_disable(self.sw_if_index, enable=1)
269
270     def ip6_disable(self):
271         """Put interface ADMIN-DOWN."""
272         self.test.vapi.ip6_sw_interface_enable_disable(self.sw_if_index, enable=0)
273
274     def add_sub_if(self, sub_if):
275         """Register a sub-interface with this interface.
276
277         :param sub_if: sub-interface
278         """
279         if not hasattr(self, 'sub_if'):
280             self.sub_if = sub_if
281         else:
282             if isinstance(self.sub_if, list):
283                 self.sub_if.append(sub_if)
284             else:
285                 self.sub_if = sub_if
286
287     def enable_mpls(self):
288         """Enable MPLS on the VPP interface."""
289         self.test.vapi.sw_interface_enable_disable_mpls(
290             self.sw_if_index)
291
292     def is_ip4_entry_in_fib_dump(self, dump):
293         for i in dump:
294             if i.address == self.local_ip4n and \
295                i.address_length == self.local_ip4_prefix_len and \
296                i.table_id == self.ip4_table_id:
297                 return True
298         return False