blob: 7b9de828e6c7ea7901aaa4a6ae10dd0d1cb16323 [file] [log] [blame]
Paul Vinciguerra6e4c6ad2018-11-25 10:35:29 -08001import binascii
Klement Sekeraf62ae122016-10-11 11:47:09 +02002import socket
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -08003import abc
Paul Vinciguerra00671cf2018-11-25 12:47:04 -08004
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -08005import six
Paul Vinciguerra6c746172018-11-26 09:57:21 -08006from six import moves
Klement Sekeraf62ae122016-10-11 11:47:09 +02007
Ole Troan7f991832018-12-06 17:35:12 +01008from util import Host, mk_ll_addr
Ole Trøan3b0d7e42019-03-15 16:14:41 +00009from vpp_papi import mac_ntop
Neale Ranns097fa662018-05-01 05:17:55 -070010from ipaddress import IPv4Network
11
12try:
13 text_type = unicode
14except NameError:
15 text_type = str
Jan65209ed2016-12-05 23:29:17 +010016
Klement Sekeraf62ae122016-10-11 11:47:09 +020017
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -080018@six.add_metaclass(abc.ABCMeta)
Klement Sekeraf62ae122016-10-11 11:47:09 +020019class VppInterface(object):
Matej Klotton86d87c42016-11-11 11:38:55 +010020 """Generic VPP interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +020021
22 @property
23 def sw_if_index(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010024 """Interface index assigned by VPP."""
Klement Sekeraf62ae122016-10-11 11:47:09 +020025 return self._sw_if_index
26
27 @property
28 def remote_mac(self):
Klement Sekerada505f62017-01-04 12:58:53 +010029 """MAC-address of the remote interface "connected" to this interface"""
Matej Klotton0178d522016-11-04 11:11:44 +010030 return self._remote_hosts[0].mac
Klement Sekeraf62ae122016-10-11 11:47:09 +020031
32 @property
33 def local_mac(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010034 """MAC-address of the VPP interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +020035 return self._local_mac
36
37 @property
Klement Sekera611864f2018-09-26 11:19:00 +020038 def local_addr(self):
39 return self._local_addr
40
41 @property
42 def remote_addr(self):
43 return self._remote_addr
44
45 @property
Ole Trøan3b0d7e42019-03-15 16:14:41 +000046 def local_addr_n(self):
47 return self._local_addr_n
48
49 @property
50 def remote_addr_n(self):
51 return self._remote_addr_n
52
53 @property
Klement Sekeraf62ae122016-10-11 11:47:09 +020054 def local_ip4(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010055 """Local IPv4 address on VPP interface (string)."""
Klement Sekeraf62ae122016-10-11 11:47:09 +020056 return self._local_ip4
57
58 @property
59 def local_ip4n(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010060 """Local IPv4 address - raw, suitable as API parameter."""
Ole Trøan3b0d7e42019-03-15 16:14:41 +000061 return socket.inet_pton(socket.AF_INET, self._local_ip4)
Klement Sekeraf62ae122016-10-11 11:47:09 +020062
63 @property
64 def remote_ip4(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010065 """IPv4 address of remote peer "connected" to this interface."""
Matej Klotton0178d522016-11-04 11:11:44 +010066 return self._remote_hosts[0].ip4
Klement Sekeraf62ae122016-10-11 11:47:09 +020067
68 @property
69 def remote_ip4n(self):
Ole Trøan3b0d7e42019-03-15 16:14:41 +000070 """IPv4 address of remote peer - raw, suitable as API parameter."""
71 return socket.inet_pton(socket.AF_INET, self.remote_ip4)
Klement Sekeraf62ae122016-10-11 11:47:09 +020072
73 @property
74 def local_ip6(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010075 """Local IPv6 address on VPP interface (string)."""
Klement Sekeraf62ae122016-10-11 11:47:09 +020076 return self._local_ip6
77
78 @property
79 def local_ip6n(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010080 """Local IPv6 address - raw, suitable as API parameter."""
Ole Trøan3b0d7e42019-03-15 16:14:41 +000081 return socket.inet_pton(socket.AF_INET6, self.local_ip6)
Klement Sekeraf62ae122016-10-11 11:47:09 +020082
83 @property
84 def remote_ip6(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010085 """IPv6 address of remote peer "connected" to this interface."""
Matej Klotton0178d522016-11-04 11:11:44 +010086 return self._remote_hosts[0].ip6
Klement Sekeraf62ae122016-10-11 11:47:09 +020087
88 @property
89 def remote_ip6n(self):
Ole Trøan3b0d7e42019-03-15 16:14:41 +000090 """IPv6 address of remote peer - raw, suitable as API parameter"""
91 return socket.inet_pton(socket.AF_INET6, self.remote_ip6)
Klement Sekeraf62ae122016-10-11 11:47:09 +020092
93 @property
Juraj Slobodac0374232018-02-01 15:18:49 +010094 def local_ip6_ll(self):
Klement Sekera611864f2018-09-26 11:19:00 +020095 """Local IPv6 link-local address on VPP interface (string)."""
Ole Trøan3b0d7e42019-03-15 16:14:41 +000096 return self._local_ip6_ll
Juraj Slobodac0374232018-02-01 15:18:49 +010097
98 @property
99 def local_ip6n_ll(self):
Ole Trøan3b0d7e42019-03-15 16:14:41 +0000100 """Local IPv6 link-local address - raw, suitable as API parameter."""
101 return self._local_ip6n_ll
Juraj Slobodac0374232018-02-01 15:18:49 +0100102
103 @property
104 def remote_ip6_ll(self):
105 """Link-local IPv6 address of remote peer
106 "connected" to this interface."""
107 return self._remote_ip6_ll
108
109 @property
110 def remote_ip6n_ll(self):
Ole Trøan3b0d7e42019-03-15 16:14:41 +0000111 """Link-local IPv6 address of remote peer
112 - raw, suitable as API parameter"""
113 return self._remote_ip6n_ll
Juraj Slobodac0374232018-02-01 15:18:49 +0100114
115 @property
Klement Sekeraf62ae122016-10-11 11:47:09 +0200116 def name(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100117 """Name of the interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200118 return self._name
119
120 @property
121 def dump(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100122 """RAW result of sw_interface_dump for this interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200123 return self._dump
124
125 @property
126 def test(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100127 """Test case creating this interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200128 return self._test
129
Matej Klotton0178d522016-11-04 11:11:44 +0100130 @property
131 def remote_hosts(self):
132 """Remote hosts list"""
133 return self._remote_hosts
134
135 @remote_hosts.setter
136 def remote_hosts(self, value):
Matej Klotton86d87c42016-11-11 11:38:55 +0100137 """
138 :param list value: List of remote hosts.
139 """
Matej Klotton0178d522016-11-04 11:11:44 +0100140 self._remote_hosts = value
Matej Klotton86d87c42016-11-11 11:38:55 +0100141 self._hosts_by_mac = {}
142 self._hosts_by_ip4 = {}
143 self._hosts_by_ip6 = {}
144 for host in self._remote_hosts:
145 self._hosts_by_mac[host.mac] = host
146 self._hosts_by_ip4[host.ip4] = host
147 self._hosts_by_ip6[host.ip6] = host
Matej Klotton0178d522016-11-04 11:11:44 +0100148
149 def host_by_mac(self, mac):
Matej Klotton86d87c42016-11-11 11:38:55 +0100150 """
Matej Klottonc5bf07f2016-11-23 15:27:17 +0100151 :param mac: MAC address to find host by.
Matej Klotton86d87c42016-11-11 11:38:55 +0100152 :return: Host object assigned to interface.
153 """
Matej Klotton0178d522016-11-04 11:11:44 +0100154 return self._hosts_by_mac[mac]
155
156 def host_by_ip4(self, ip):
Matej Klotton86d87c42016-11-11 11:38:55 +0100157 """
158 :param ip: IPv4 address to find host by.
159 :return: Host object assigned to interface.
160 """
Matej Klotton0178d522016-11-04 11:11:44 +0100161 return self._hosts_by_ip4[ip]
162
163 def host_by_ip6(self, ip):
Matej Klotton86d87c42016-11-11 11:38:55 +0100164 """
165 :param ip: IPv6 address to find host by.
166 :return: Host object assigned to interface.
167 """
Matej Klotton0178d522016-11-04 11:11:44 +0100168 return self._hosts_by_ip6[ip]
169
170 def generate_remote_hosts(self, count=1):
Matej Klotton86d87c42016-11-11 11:38:55 +0100171 """Generate and add remote hosts for the interface.
172
173 :param int count: Number of generated remote hosts.
174 """
Matej Klotton0178d522016-11-04 11:11:44 +0100175 self._remote_hosts = []
176 self._hosts_by_mac = {}
177 self._hosts_by_ip4 = {}
178 self._hosts_by_ip6 = {}
Klement Sekera7bb873a2016-11-18 07:38:42 +0100179 for i in range(
180 2, count + 2): # 0: network address, 1: local vpp address
Matej Klotton0178d522016-11-04 11:11:44 +0100181 mac = "02:%02x:00:00:ff:%02x" % (self.sw_if_index, i)
182 ip4 = "172.16.%u.%u" % (self.sw_if_index, i)
Klement Sekera46a87ad2017-01-02 08:22:23 +0100183 ip6 = "fd01:%x::%x" % (self.sw_if_index, i)
Neale Ranns2a3ea492017-04-19 05:24:40 -0700184 ip6_ll = mk_ll_addr(mac)
185 host = Host(mac, ip4, ip6, ip6_ll)
Matej Klotton0178d522016-11-04 11:11:44 +0100186 self._remote_hosts.append(host)
187 self._hosts_by_mac[mac] = host
188 self._hosts_by_ip4[ip4] = host
189 self._hosts_by_ip6[ip6] = host
190
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -0800191 @abc.abstractmethod
Matej Klottonc5bf07f2016-11-23 15:27:17 +0100192 def __init__(self, test):
193 self._test = test
194
195 self._remote_hosts = []
196 self._hosts_by_mac = {}
197 self._hosts_by_ip4 = {}
198 self._hosts_by_ip6 = {}
Matej Klotton0178d522016-11-04 11:11:44 +0100199
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700200 def set_mac(self, mac):
Ole Troan8006c6a2018-12-17 12:02:26 +0100201 self._local_mac = str(mac)
Ole Trøan3b0d7e42019-03-15 16:14:41 +0000202 self._local_ip6_ll = mk_ll_addr(self._local_mac)
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700203 self.test.vapi.sw_interface_set_mac_address(
Ole Troan8006c6a2018-12-17 12:02:26 +0100204 self.sw_if_index, mac.packed)
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700205
Klement Sekera31da2e32018-06-24 22:49:55 +0200206 def set_sw_if_index(self, sw_if_index):
207 self._sw_if_index = sw_if_index
208
Matej Klotton0178d522016-11-04 11:11:44 +0100209 self.generate_remote_hosts()
Klement Sekeraf62ae122016-10-11 11:47:09 +0200210
Ole Trøan3b0d7e42019-03-15 16:14:41 +0000211 self._local_ip4 = "172.16.%u.1" % self.sw_if_index
212 self._local_ip4n = socket.inet_pton(socket.AF_INET, self.local_ip4)
Neale Ranns24b170a2017-08-15 05:33:11 -0700213 self._local_ip4_subnet = "172.16.%u.0" % self.sw_if_index
Ole Trøan3b0d7e42019-03-15 16:14:41 +0000214 self._local_ip4n_subnet = socket.inet_pton(socket.AF_INET,
215 self._local_ip4_subnet)
Neale Ranns24b170a2017-08-15 05:33:11 -0700216 self._local_ip4_bcast = "172.16.%u.255" % self.sw_if_index
Ole Trøan3b0d7e42019-03-15 16:14:41 +0000217 self._local_ip4n_bcast = socket.inet_pton(socket.AF_INET,
218 self._local_ip4_bcast)
219 self.local_ip4_prefix_len = 24
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100220 self.has_ip4_config = False
221 self.ip4_table_id = 0
Klement Sekeraf62ae122016-10-11 11:47:09 +0200222
Ole Trøan3b0d7e42019-03-15 16:14:41 +0000223 self._local_ip6 = "fd01:%x::1" % self.sw_if_index
224 self._local_ip6n = socket.inet_pton(socket.AF_INET6, self.local_ip6)
225 self.local_ip6_prefix_len = 64
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100226 self.has_ip6_config = False
227 self.ip6_table_id = 0
Klement Sekeraf62ae122016-10-11 11:47:09 +0200228
Klement Sekera611864f2018-09-26 11:19:00 +0200229 self._local_addr = {socket.AF_INET: self.local_ip4,
230 socket.AF_INET6: self.local_ip6}
Ole Trøan3b0d7e42019-03-15 16:14:41 +0000231 self._local_addr_n = {socket.AF_INET: self.local_ip4n,
232 socket.AF_INET6: self.local_ip6n}
Klement Sekera611864f2018-09-26 11:19:00 +0200233 self._remote_addr = {socket.AF_INET: self.remote_ip4,
234 socket.AF_INET6: self.remote_ip6}
Ole Trøan3b0d7e42019-03-15 16:14:41 +0000235 self._remote_addr_n = {socket.AF_INET: self.remote_ip4n,
236 socket.AF_INET6: self.remote_ip6n}
Klement Sekera611864f2018-09-26 11:19:00 +0200237
Paul Vinciguerra7a998232019-06-07 15:01:12 -0400238 r = self.test.vapi.sw_interface_dump(sw_if_index=self.sw_if_index)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200239 for intf in r:
240 if intf.sw_if_index == self.sw_if_index:
Ole Troan7f991832018-12-06 17:35:12 +0100241 self._name = intf.interface_name.split(b'\0',
242 1)[0].decode('utf8')
Ole Trøan3b0d7e42019-03-15 16:14:41 +0000243 self._local_mac = mac_ntop(intf.l2_address)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200244 self._dump = intf
245 break
246 else:
247 raise Exception(
248 "Could not find interface with sw_if_index %d "
249 "in interface dump %s" %
Paul Vinciguerra6c746172018-11-26 09:57:21 -0800250 (self.sw_if_index, moves.reprlib.repr(r)))
Ole Trøan3b0d7e42019-03-15 16:14:41 +0000251 self._local_ip6_ll = mk_ll_addr(self.local_mac)
252 self._local_ip6n_ll = socket.inet_pton(socket.AF_INET6,
253 self.local_ip6_ll)
Juraj Slobodac0374232018-02-01 15:18:49 +0100254 self._remote_ip6_ll = mk_ll_addr(self.remote_mac)
Ole Trøan3b0d7e42019-03-15 16:14:41 +0000255 self._remote_ip6n_ll = socket.inet_pton(socket.AF_INET6,
256 self.remote_ip6_ll)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200257
Klement Sekeraf62ae122016-10-11 11:47:09 +0200258 def config_ip4(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100259 """Configure IPv4 address on the VPP interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200260 self.test.vapi.sw_interface_add_del_address(
Ole Trøan3b0d7e42019-03-15 16:14:41 +0000261 sw_if_index=self.sw_if_index, address=self.local_ip4n,
262 address_length=self.local_ip4_prefix_len)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000263 self.has_ip4_config = True
264
265 def unconfig_ip4(self):
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100266 """Remove IPv4 address on the VPP interface."""
Neale Ranns177bbdc2016-11-15 09:46:51 +0000267 try:
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100268 if self.has_ip4_config:
Neale Ranns177bbdc2016-11-15 09:46:51 +0000269 self.test.vapi.sw_interface_add_del_address(
Ole Trøan3b0d7e42019-03-15 16:14:41 +0000270 sw_if_index=self.sw_if_index, address=self.local_ip4n,
271 address_length=self.local_ip4_prefix_len, is_add=0)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000272 except AttributeError:
273 self.has_ip4_config = False
274 self.has_ip4_config = False
Klement Sekeraf62ae122016-10-11 11:47:09 +0200275
Neale Rannsbaf2e902017-02-25 04:20:00 -0800276 def configure_ipv4_neighbors(self):
Jane546d3b2016-12-08 13:10:03 +0100277 """For every remote host assign neighbor's MAC to IPv4 addresses.
278
279 :param vrf_id: The FIB table / VRF ID. (Default value = 0)
280 """
Matej Klotton0178d522016-11-04 11:11:44 +0100281 for host in self._remote_hosts:
Neale Ranns37029302018-08-10 05:30:06 -0700282 self.test.vapi.ip_neighbor_add_del(self.sw_if_index,
283 host.mac,
284 host.ip4)
Matej Klotton0178d522016-11-04 11:11:44 +0100285
Klement Sekeraf62ae122016-10-11 11:47:09 +0200286 def config_ip6(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100287 """Configure IPv6 address on the VPP interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200288 self.test.vapi.sw_interface_add_del_address(
Ole Trøan3b0d7e42019-03-15 16:14:41 +0000289 sw_if_index=self.sw_if_index, address=self._local_ip6n,
290 address_length=self.local_ip6_prefix_len, is_ipv6=1)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000291 self.has_ip6_config = True
292
293 def unconfig_ip6(self):
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100294 """Remove IPv6 address on the VPP interface."""
Neale Ranns177bbdc2016-11-15 09:46:51 +0000295 try:
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100296 if self.has_ip6_config:
Neale Ranns177bbdc2016-11-15 09:46:51 +0000297 self.test.vapi.sw_interface_add_del_address(
Ole Trøan3b0d7e42019-03-15 16:14:41 +0000298 sw_if_index=self.sw_if_index, address=self.local_ip6n,
299 address_length=self.local_ip6_prefix_len, is_ipv6=1,
300 is_add=0)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000301 except AttributeError:
302 self.has_ip6_config = False
303 self.has_ip6_config = False
304
Neale Rannsbaf2e902017-02-25 04:20:00 -0800305 def configure_ipv6_neighbors(self):
Jan Gelety057bb8c2016-12-20 17:32:45 +0100306 """For every remote host assign neighbor's MAC to IPv6 addresses.
307
308 :param vrf_id: The FIB table / VRF ID. (Default value = 0)
309 """
Klement Sekera46a87ad2017-01-02 08:22:23 +0100310 for host in self._remote_hosts:
Neale Ranns37029302018-08-10 05:30:06 -0700311 self.test.vapi.ip_neighbor_add_del(self.sw_if_index,
312 host.mac,
313 host.ip6)
Klement Sekera46a87ad2017-01-02 08:22:23 +0100314
Neale Ranns177bbdc2016-11-15 09:46:51 +0000315 def unconfig(self):
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100316 """Unconfigure IPv6 and IPv4 address on the VPP interface."""
Neale Ranns177bbdc2016-11-15 09:46:51 +0000317 self.unconfig_ip4()
318 self.unconfig_ip6()
Klement Sekeraf62ae122016-10-11 11:47:09 +0200319
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000320 def set_table_ip4(self, table_id):
321 """Set the interface in a IPv4 Table.
Matej Klotton86d87c42016-11-11 11:38:55 +0100322
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100323 .. note:: Must be called before configuring IP4 addresses.
324 """
325 self.ip4_table_id = table_id
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000326 self.test.vapi.sw_interface_set_table(
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100327 self.sw_if_index, 0, self.ip4_table_id)
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000328
329 def set_table_ip6(self, table_id):
330 """Set the interface in a IPv6 Table.
Matej Klotton86d87c42016-11-11 11:38:55 +0100331
332 .. note:: Must be called before configuring IP6 addresses.
333 """
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100334 self.ip6_table_id = table_id
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000335 self.test.vapi.sw_interface_set_table(
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100336 self.sw_if_index, 1, self.ip6_table_id)
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000337
Klement Sekeraf62ae122016-10-11 11:47:09 +0200338 def disable_ipv6_ra(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100339 """Configure IPv6 RA suppress on the VPP interface."""
Ole Troane1ade682019-03-04 23:55:43 +0100340 self.test.vapi.sw_interface_ip6nd_ra_config(
341 sw_if_index=self.sw_if_index,
342 suppress=1)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200343
Neale Ranns32e1c012016-11-22 17:07:28 +0000344 def ip6_ra_config(self, no=0, suppress=0, send_unicast=0):
Neale Ranns75152282017-01-09 01:00:45 -0800345 """Configure IPv6 RA suppress on the VPP interface."""
Ole Troane1ade682019-03-04 23:55:43 +0100346 self.test.vapi.sw_interface_ip6nd_ra_config(
347 sw_if_index=self.sw_if_index,
348 is_no=no,
349 suppress=suppress,
350 send_unicast=send_unicast)
Neale Ranns75152282017-01-09 01:00:45 -0800351
Ole Troana5b2eec2019-03-11 19:23:25 +0100352 # TODO: This should accept ipaddress object.
Neale Ranns87df12d2017-02-18 08:16:41 -0800353 def ip6_ra_prefix(self, address, address_length, is_no=0,
354 off_link=0, no_autoconfig=0, use_default=0):
355 """Configure IPv6 RA suppress on the VPP interface."""
Ole Troane1ade682019-03-04 23:55:43 +0100356 self.test.vapi.sw_interface_ip6nd_ra_prefix(
Ole Troana5b2eec2019-03-11 19:23:25 +0100357 sw_if_index=self.sw_if_index,
358 prefix={'address': address,
359 'address_length': address_length},
360 use_default=use_default,
361 off_link=off_link, no_autoconfig=no_autoconfig,
362 is_no=is_no)
Neale Ranns87df12d2017-02-18 08:16:41 -0800363
Klement Sekeraf62ae122016-10-11 11:47:09 +0200364 def admin_up(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100365 """Put interface ADMIN-UP."""
Ole Trøan3b0d7e42019-03-15 16:14:41 +0000366 self.test.vapi.sw_interface_set_flags(self.sw_if_index,
367 admin_up_down=1)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200368
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100369 def admin_down(self):
370 """Put interface ADMIN-down."""
Klement Sekerada505f62017-01-04 12:58:53 +0100371 self.test.vapi.sw_interface_set_flags(self.sw_if_index,
Ole Trøan3b0d7e42019-03-15 16:14:41 +0000372 admin_up_down=0)
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100373
Neale Ranns63480742019-03-13 06:41:52 -0700374 def link_up(self):
375 """Put interface link-state-UP."""
376 self.test.vapi.cli("test interface link-state %s up" % self.name)
377
378 def link_down(self):
379 """Put interface link-state-down."""
380 self.test.vapi.cli("test interface link-state %s down" % self.name)
381
Neale Ranns75152282017-01-09 01:00:45 -0800382 def ip6_enable(self):
383 """IPv6 Enable interface"""
Ole Troane1ade682019-03-04 23:55:43 +0100384 self.test.vapi.sw_interface_ip6_enable_disable(self.sw_if_index,
Klement Sekerada505f62017-01-04 12:58:53 +0100385 enable=1)
Neale Ranns75152282017-01-09 01:00:45 -0800386
387 def ip6_disable(self):
388 """Put interface ADMIN-DOWN."""
Ole Troane1ade682019-03-04 23:55:43 +0100389 self.test.vapi.sw_interface_ip6_enable_disable(self.sw_if_index,
Klement Sekerada505f62017-01-04 12:58:53 +0100390 enable=0)
Neale Ranns75152282017-01-09 01:00:45 -0800391
Klement Sekeraf62ae122016-10-11 11:47:09 +0200392 def add_sub_if(self, sub_if):
Matej Klotton86d87c42016-11-11 11:38:55 +0100393 """Register a sub-interface with this interface.
Klement Sekeraf62ae122016-10-11 11:47:09 +0200394
395 :param sub_if: sub-interface
Klement Sekeraf62ae122016-10-11 11:47:09 +0200396 """
397 if not hasattr(self, 'sub_if'):
398 self.sub_if = sub_if
399 else:
400 if isinstance(self.sub_if, list):
401 self.sub_if.append(sub_if)
402 else:
403 self.sub_if = sub_if
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000404
405 def enable_mpls(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100406 """Enable MPLS on the VPP interface."""
Ole Troan9a475372019-03-05 16:58:24 +0100407 self.test.vapi.sw_interface_set_mpls_enable(self.sw_if_index)
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100408
Neale Ranns180279b2017-03-16 15:49:09 -0400409 def disable_mpls(self):
410 """Enable MPLS on the VPP interface."""
Ole Troan9a475372019-03-05 16:58:24 +0100411 self.test.vapi.sw_interface_set_mpls_enable(self.sw_if_index, 0)
Neale Ranns180279b2017-03-16 15:49:09 -0400412
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100413 def is_ip4_entry_in_fib_dump(self, dump):
414 for i in dump:
Neale Ranns097fa662018-05-01 05:17:55 -0700415 n = IPv4Network(text_type("%s/%d" % (self.local_ip4,
416 self.local_ip4_prefix_len)))
417 if i.route.prefix == n and \
418 i.route.table_id == self.ip4_table_id:
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100419 return True
420 return False
Neale Ranns39f9d8b2017-02-16 21:57:05 -0800421
422 def set_unnumbered(self, ip_sw_if_index):
423 """ Set the interface to unnumbered via ip_sw_if_index """
Ole Troan9a475372019-03-05 16:58:24 +0100424 self.test.vapi.sw_interface_set_unnumbered(ip_sw_if_index,
425 self.sw_if_index)
Neale Ranns39f9d8b2017-02-16 21:57:05 -0800426
Neale Ranns37be7362017-02-21 17:30:26 -0800427 def unset_unnumbered(self, ip_sw_if_index):
Neale Ranns4b919a52017-03-11 05:55:21 -0800428 """ Unset the interface to unnumbered via ip_sw_if_index """
Ole Troan9a475372019-03-05 16:58:24 +0100429 self.test.vapi.sw_interface_set_unnumbered(ip_sw_if_index,
430 self.sw_if_index, is_add=0)
Neale Ranns37be7362017-02-21 17:30:26 -0800431
Neale Ranns39f9d8b2017-02-16 21:57:05 -0800432 def set_proxy_arp(self, enable=1):
433 """ Set the interface to enable/disable Proxy ARP """
434 self.test.vapi.proxy_arp_intfc_enable_disable(
435 self.sw_if_index,
436 enable)
Klement Sekera75e7d132017-09-20 08:26:30 +0200437
438 def query_vpp_config(self):
Paul Vinciguerra7a998232019-06-07 15:01:12 -0400439 dump = self.test.vapi.sw_interface_dump(sw_if_index=self.sw_if_index)
Klement Sekera75e7d132017-09-20 08:26:30 +0200440 return self.is_interface_config_in_dump(dump)
441
Juraj Slobodab3f90502018-10-04 15:15:16 +0200442 def get_interface_config_from_dump(self, dump):
Klement Sekera75e7d132017-09-20 08:26:30 +0200443 for i in dump:
444 if i.interface_name.rstrip(' \t\r\n\0') == self.name and \
Ole Troan9a475372019-03-05 16:58:24 +0100445 i.sw_if_index == self.sw_if_index:
Juraj Slobodab3f90502018-10-04 15:15:16 +0200446 return i
Klement Sekera75e7d132017-09-20 08:26:30 +0200447 else:
Juraj Slobodab3f90502018-10-04 15:15:16 +0200448 return None
449
450 def is_interface_config_in_dump(self, dump):
451 return self.get_interface_config_from_dump(dump) is not None
452
453 def assert_interface_state(self, admin_up_down, link_up_down,
454 expect_event=False):
455 if expect_event:
456 event = self.test.vapi.wait_for_event(timeout=1,
457 name='sw_interface_event')
458 self.test.assert_equal(event.sw_if_index, self.sw_if_index,
459 "sw_if_index")
Ole Trøan3b0d7e42019-03-15 16:14:41 +0000460 self.test.assert_equal(event.admin_up_down, admin_up_down,
Juraj Slobodab3f90502018-10-04 15:15:16 +0200461 "admin state")
Ole Trøan3b0d7e42019-03-15 16:14:41 +0000462 self.test.assert_equal(event.link_up_down, link_up_down,
Juraj Slobodab3f90502018-10-04 15:15:16 +0200463 "link state")
464 dump = self.test.vapi.sw_interface_dump()
465 if_state = self.get_interface_config_from_dump(dump)
Ole Trøan3b0d7e42019-03-15 16:14:41 +0000466 self.test.assert_equal(if_state.admin_up_down, admin_up_down,
Juraj Slobodab3f90502018-10-04 15:15:16 +0200467 "admin state")
Ole Trøan3b0d7e42019-03-15 16:14:41 +0000468 self.test.assert_equal(if_state.link_up_down, link_up_down,
Juraj Slobodab3f90502018-10-04 15:15:16 +0200469 "link state")
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700470
471 def __str__(self):
472 return self.name
Neale Ranns2ac885c2019-03-20 18:24:43 +0000473
474 def get_rx_stats(self):
475 c = self.test.statistics.get_counter("^/if/rx$")
476 return c[0][self.sw_if_index]
477
478 def get_tx_stats(self):
479 c = self.test.statistics.get_counter("^/if/tx$")
480 return c[0][self.sw_if_index]