d74248a39b34f05d48c33aabb40eed14d07da1d4
[vpp.git] / test / vpp_interface.py
1 from abc import abstractmethod, ABCMeta
2 import socket
3 from logging import info
4
5 from util import Host
6
7
8 class VppInterface(object):
9     """
10     Generic VPP interface
11     """
12     __metaclass__ = ABCMeta
13
14     @property
15     def sw_if_index(self):
16         """Interface index assigned by VPP"""
17         return self._sw_if_index
18
19     @property
20     def remote_mac(self):
21         """MAC-address of the remote interface "connected" to this interface"""
22         return self._remote_hosts[0].mac
23
24     @property
25     def local_mac(self):
26         """MAC-address of the VPP interface"""
27         return self._local_mac
28
29     @property
30     def local_ip4(self):
31         """Local IPv4 address on VPP interface (string)"""
32         return self._local_ip4
33
34     @property
35     def local_ip4n(self):
36         """Local IPv4 address - raw, suitable as API parameter"""
37         return socket.inet_pton(socket.AF_INET, self._local_ip4)
38
39     @property
40     def remote_ip4(self):
41         """IPv4 address of remote peer "connected" to this interface"""
42         return self._remote_hosts[0].ip4
43
44     @property
45     def remote_ip4n(self):
46         """IPv4 address of remote peer - raw, suitable as API parameter"""
47         return socket.inet_pton(socket.AF_INET, self.remote_ip4)
48
49     @property
50     def local_ip6(self):
51         """Local IPv6 address on VPP interface (string)"""
52         return self._local_ip6
53
54     @property
55     def local_ip6n(self):
56         """Local IPv6 address - raw, suitable as API parameter"""
57         return socket.inet_pton(socket.AF_INET6, self.local_ip6)
58
59     @property
60     def remote_ip6(self):
61         """IPv6 address of remote peer "connected" to this interface"""
62         return self._remote_hosts[0].ip6
63
64     @property
65     def remote_ip6n(self):
66         """IPv6 address of remote peer - raw, suitable as API parameter"""
67         return socket.inet_pton(socket.AF_INET6, self.remote_ip6)
68
69     @property
70     def name(self):
71         """Name of the interface"""
72         return self._name
73
74     @property
75     def dump(self):
76         """Raw result of sw_interface_dump for this interface"""
77         return self._dump
78
79     @property
80     def test(self):
81         """Test case creating this interface"""
82         return self._test
83
84     @property
85     def remote_hosts(self):
86         """Remote hosts list"""
87         return self._remote_hosts
88
89     @remote_hosts.setter
90     def remote_hosts(self, value):
91         self._remote_hosts = value
92         #TODO: set hosts_by dicts
93
94     def host_by_mac(self, mac):
95         return self._hosts_by_mac[mac]
96
97     def host_by_ip4(self, ip):
98         return self._hosts_by_ip4[ip]
99
100     def host_by_ip6(self, ip):
101         return self._hosts_by_ip6[ip]
102
103     def generate_remote_hosts(self, count=1):
104         """Generate and add remote hosts for the interface."""
105         self._remote_hosts = []
106         self._hosts_by_mac = {}
107         self._hosts_by_ip4 = {}
108         self._hosts_by_ip6 = {}
109         for i in range(2, count+2):  # 0: network address, 1: local vpp address
110             mac = "02:%02x:00:00:ff:%02x" % (self.sw_if_index, i)
111             ip4 = "172.16.%u.%u" % (self.sw_if_index, i)
112             ip6 = "fd01:%04x::%04x" % (self.sw_if_index, i)
113             host = Host(mac, ip4, ip6)
114             self._remote_hosts.append(host)
115             self._hosts_by_mac[mac] = host
116             self._hosts_by_ip4[ip4] = host
117             self._hosts_by_ip6[ip6] = host
118
119     def post_init_setup(self):
120         """Additional setup run after creating an interface object"""
121
122         self.generate_remote_hosts()
123
124         self._local_ip4 = "172.16.%u.1" % self.sw_if_index
125         self._local_ip4n = socket.inet_pton(socket.AF_INET, self.local_ip4)
126
127         self._local_ip6 = "fd01:%04x::1" % self.sw_if_index
128         self._local_ip6n = socket.inet_pton(socket.AF_INET6, self.local_ip6)
129
130         r = self.test.vapi.sw_interface_dump()
131         for intf in r:
132             if intf.sw_if_index == self.sw_if_index:
133                 self._name = intf.interface_name.split(b'\0', 1)[0]
134                 self._local_mac = ':'.join(intf.l2_address.encode('hex')[i:i + 2]
135                                            for i in range(0, 12, 2))
136                 self._dump = intf
137                 break
138         else:
139             raise Exception(
140                 "Could not find interface with sw_if_index %d "
141                 "in interface dump %s" %
142                 (self.sw_if_index, repr(r)))
143
144     @abstractmethod
145     def __init__(self, test, index):
146         self._test = test
147         self.post_init_setup()
148         info("New %s, MAC=%s, remote_ip4=%s, local_ip4=%s" %
149              (self.__name__, self.remote_mac, self.remote_ip4, self.local_ip4))
150
151     def config_ip4(self):
152         """Configure IPv4 address on the VPP interface"""
153         addr = self.local_ip4n
154         addr_len = 24
155         self.test.vapi.sw_interface_add_del_address(
156             self.sw_if_index, addr, addr_len)
157
158     def configure_extend_ipv4_mac_binding(self):
159         """Configure neighbor MAC to IPv4 addresses."""
160         for host in self._remote_hosts:
161             macn = host.mac.replace(":", "").decode('hex')
162             ipn = host.ip4n
163             self.test.vapi.ip_neighbor_add_del(self.sw_if_index, macn, ipn)
164
165     def config_ip6(self):
166         """Configure IPv6 address on the VPP interface"""
167         addr = self._local_ip6n
168         addr_len = 64
169         self.test.vapi.sw_interface_add_del_address(
170             self.sw_if_index, addr, addr_len, is_ipv6=1)
171
172     def set_table_ip4(self, table_id):
173         """Set the interface in a IPv4 Table.
174         Must be called before configuring IP4 addresses"""
175         self.test.vapi.sw_interface_set_table(
176             self.sw_if_index, 0, table_id)
177
178     def set_table_ip6(self, table_id):
179         """Set the interface in a IPv6 Table.
180         Must be called before configuring IP6 addresses"""
181         self.test.vapi.sw_interface_set_table(
182             self.sw_if_index, 1, table_id)
183
184     def disable_ipv6_ra(self):
185         """Configure IPv6 RA suppress on the VPP interface"""
186         self.test.vapi.sw_interface_ra_suppress(self.sw_if_index)
187
188     def admin_up(self):
189         """ Put interface ADMIN-UP """
190         self.test.vapi.sw_interface_set_flags(self.sw_if_index, admin_up_down=1)
191
192     def add_sub_if(self, sub_if):
193         """
194         Register a sub-interface with this interface
195
196         :param sub_if: sub-interface
197
198         """
199         if not hasattr(self, 'sub_if'):
200             self.sub_if = sub_if
201         else:
202             if isinstance(self.sub_if, list):
203                 self.sub_if.append(sub_if)
204             else:
205                 self.sub_if = sub_if
206
207     def enable_mpls(self):
208         """Enable MPLS on the VPP interface"""
209         self.test.vapi.sw_interface_enable_disable_mpls(
210             self.sw_if_index)