blob: a344ec613b016ae1f1279f674dc3762f0cf0ac30 [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
Jakub Grajciar053204a2019-03-18 13:17:53 +01009from vpp_papi import mac_ntop, VppEnum
Neale Rannsefd7bc22019-11-11 08:32:34 +000010from ipaddress import IPv4Network, IPv6Network
Neale Ranns097fa662018-05-01 05:17:55 -070011
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."""
Ole Troan6ed154f2019-10-15 19:31:55 +020035 return str(self._local_mac)
Klement Sekeraf62ae122016-10-11 11:47:09 +020036
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
Klement Sekeraf62ae122016-10-11 11:47:09 +020046 def local_ip4(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010047 """Local IPv4 address on VPP interface (string)."""
Neale Rannsefd7bc22019-11-11 08:32:34 +000048 return self._local_ip4
Jakub Grajciar053204a2019-03-18 13:17:53 +010049
50 @local_ip4.setter
51 def local_ip4(self, value):
Neale Rannsefd7bc22019-11-11 08:32:34 +000052 self._local_ip4 = value
Jakub Grajciar053204a2019-03-18 13:17:53 +010053
54 @property
55 def local_ip4_prefix_len(self):
56 """Local IPv4 prefix length """
Neale Rannsefd7bc22019-11-11 08:32:34 +000057 return self._local_ip4_len
Jakub Grajciar053204a2019-03-18 13:17:53 +010058
59 @local_ip4_prefix_len.setter
60 def local_ip4_prefix_len(self, value):
Neale Rannsefd7bc22019-11-11 08:32:34 +000061 self._local_ip4_len = value
Jakub Grajciar053204a2019-03-18 13:17:53 +010062
63 @property
64 def local_ip4_prefix(self):
65 """Local IPv4 prefix """
Neale Rannsefd7bc22019-11-11 08:32:34 +000066 return ("%s/%d" % (self._local_ip4, self._local_ip4_len))
Klement Sekeraf62ae122016-10-11 11:47:09 +020067
68 @property
69 def local_ip4n(self):
Jakub Grajciar053204a2019-03-18 13:17:53 +010070 """DEPRECATED """
Matej Klotton86d87c42016-11-11 11:38:55 +010071 """Local IPv4 address - raw, suitable as API parameter."""
Neale Rannsefd7bc22019-11-11 08:32:34 +000072 return socket.inet_pton(socket.AF_INET, self._local_ip4)
Klement Sekeraf62ae122016-10-11 11:47:09 +020073
74 @property
75 def remote_ip4(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010076 """IPv4 address of remote peer "connected" to this interface."""
Matej Klotton0178d522016-11-04 11:11:44 +010077 return self._remote_hosts[0].ip4
Klement Sekeraf62ae122016-10-11 11:47:09 +020078
79 @property
80 def remote_ip4n(self):
Jakub Grajciar053204a2019-03-18 13:17:53 +010081 """DEPRECATED """
82 """Local IPv6 address - raw, suitable as API parameter."""
83 return socket.inet_pton(socket.AF_INET, self._remote_hosts[0].ip4)
Klement Sekeraf62ae122016-10-11 11:47:09 +020084
85 @property
86 def local_ip6(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010087 """Local IPv6 address on VPP interface (string)."""
Neale Rannsefd7bc22019-11-11 08:32:34 +000088 return self._local_ip6
Jakub Grajciar053204a2019-03-18 13:17:53 +010089
90 @local_ip6.setter
91 def local_ip6(self, value):
Paul Vinciguerra5d440d92019-12-01 21:55:52 -050092 self._local_ip6 = value
Jakub Grajciar053204a2019-03-18 13:17:53 +010093
94 @property
95 def local_ip6_prefix_len(self):
96 """Local IPv6 prefix length """
Neale Rannsefd7bc22019-11-11 08:32:34 +000097 return self._local_ip6_len
Jakub Grajciar053204a2019-03-18 13:17:53 +010098
99 @local_ip6_prefix_len.setter
100 def local_ip6_prefix_len(self, value):
Neale Rannsefd7bc22019-11-11 08:32:34 +0000101 self._local_ip6_len = value
Jakub Grajciar053204a2019-03-18 13:17:53 +0100102
103 @property
104 def local_ip6_prefix(self):
Neale Rannsefd7bc22019-11-11 08:32:34 +0000105 """Local IPv4 prefix """
106 return ("%s/%d" % (self._local_ip6, self._local_ip6_len))
Klement Sekeraf62ae122016-10-11 11:47:09 +0200107
108 @property
109 def local_ip6n(self):
Jakub Grajciar053204a2019-03-18 13:17:53 +0100110 """DEPRECATED """
Matej Klotton86d87c42016-11-11 11:38:55 +0100111 """Local IPv6 address - raw, suitable as API parameter."""
Neale Rannsefd7bc22019-11-11 08:32:34 +0000112 return socket.inet_pton(socket.AF_INET6, self._local_ip6)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200113
114 @property
115 def remote_ip6(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100116 """IPv6 address of remote peer "connected" to this interface."""
Matej Klotton0178d522016-11-04 11:11:44 +0100117 return self._remote_hosts[0].ip6
Klement Sekeraf62ae122016-10-11 11:47:09 +0200118
119 @property
120 def remote_ip6n(self):
Jakub Grajciar053204a2019-03-18 13:17:53 +0100121 """DEPRECATED """
122 """Local IPv6 address - raw, suitable as API parameter."""
123 return socket.inet_pton(socket.AF_INET6, self._remote_hosts[0].ip6)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200124
125 @property
Juraj Slobodac0374232018-02-01 15:18:49 +0100126 def local_ip6_ll(self):
Klement Sekera611864f2018-09-26 11:19:00 +0200127 """Local IPv6 link-local address on VPP interface (string)."""
Neale Rannsefd7bc22019-11-11 08:32:34 +0000128 return self._local_ip6_ll
Juraj Slobodac0374232018-02-01 15:18:49 +0100129
130 @property
131 def local_ip6n_ll(self):
Jakub Grajciar053204a2019-03-18 13:17:53 +0100132 """DEPRECATED """
133 """Local IPv6 link-local address on VPP interface (string)."""
134 return socket.inet_pton(socket.AF_INET6, self._local_ip6_ll.address)
Juraj Slobodac0374232018-02-01 15:18:49 +0100135
136 @property
137 def remote_ip6_ll(self):
138 """Link-local IPv6 address of remote peer
139 "connected" to this interface."""
140 return self._remote_ip6_ll
141
142 @property
143 def remote_ip6n_ll(self):
Jakub Grajciar053204a2019-03-18 13:17:53 +0100144 """DEPRECATED """
145 """Local IPv6 link-local address on VPP interface (string)."""
146 return socket.inet_pton(socket.AF_INET6, self._remote_ip6_ll)
Juraj Slobodac0374232018-02-01 15:18:49 +0100147
148 @property
Klement Sekeraf62ae122016-10-11 11:47:09 +0200149 def name(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100150 """Name of the interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200151 return self._name
152
153 @property
154 def dump(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100155 """RAW result of sw_interface_dump for this interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200156 return self._dump
157
158 @property
159 def test(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100160 """Test case creating this interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200161 return self._test
162
Matej Klotton0178d522016-11-04 11:11:44 +0100163 @property
164 def remote_hosts(self):
165 """Remote hosts list"""
166 return self._remote_hosts
167
168 @remote_hosts.setter
169 def remote_hosts(self, value):
Matej Klotton86d87c42016-11-11 11:38:55 +0100170 """
171 :param list value: List of remote hosts.
172 """
Matej Klotton0178d522016-11-04 11:11:44 +0100173 self._remote_hosts = value
Matej Klotton86d87c42016-11-11 11:38:55 +0100174 self._hosts_by_mac = {}
175 self._hosts_by_ip4 = {}
176 self._hosts_by_ip6 = {}
177 for host in self._remote_hosts:
178 self._hosts_by_mac[host.mac] = host
179 self._hosts_by_ip4[host.ip4] = host
180 self._hosts_by_ip6[host.ip6] = host
Matej Klotton0178d522016-11-04 11:11:44 +0100181
182 def host_by_mac(self, mac):
Matej Klotton86d87c42016-11-11 11:38:55 +0100183 """
Matej Klottonc5bf07f2016-11-23 15:27:17 +0100184 :param mac: MAC address to find host by.
Matej Klotton86d87c42016-11-11 11:38:55 +0100185 :return: Host object assigned to interface.
186 """
Matej Klotton0178d522016-11-04 11:11:44 +0100187 return self._hosts_by_mac[mac]
188
189 def host_by_ip4(self, ip):
Matej Klotton86d87c42016-11-11 11:38:55 +0100190 """
191 :param ip: IPv4 address to find host by.
192 :return: Host object assigned to interface.
193 """
Matej Klotton0178d522016-11-04 11:11:44 +0100194 return self._hosts_by_ip4[ip]
195
196 def host_by_ip6(self, ip):
Matej Klotton86d87c42016-11-11 11:38:55 +0100197 """
198 :param ip: IPv6 address to find host by.
199 :return: Host object assigned to interface.
200 """
Matej Klotton0178d522016-11-04 11:11:44 +0100201 return self._hosts_by_ip6[ip]
202
203 def generate_remote_hosts(self, count=1):
Matej Klotton86d87c42016-11-11 11:38:55 +0100204 """Generate and add remote hosts for the interface.
205
206 :param int count: Number of generated remote hosts.
207 """
Matej Klotton0178d522016-11-04 11:11:44 +0100208 self._remote_hosts = []
209 self._hosts_by_mac = {}
210 self._hosts_by_ip4 = {}
211 self._hosts_by_ip6 = {}
Klement Sekera7bb873a2016-11-18 07:38:42 +0100212 for i in range(
213 2, count + 2): # 0: network address, 1: local vpp address
Matej Klotton0178d522016-11-04 11:11:44 +0100214 mac = "02:%02x:00:00:ff:%02x" % (self.sw_if_index, i)
215 ip4 = "172.16.%u.%u" % (self.sw_if_index, i)
Klement Sekera46a87ad2017-01-02 08:22:23 +0100216 ip6 = "fd01:%x::%x" % (self.sw_if_index, i)
Neale Ranns2a3ea492017-04-19 05:24:40 -0700217 ip6_ll = mk_ll_addr(mac)
218 host = Host(mac, ip4, ip6, ip6_ll)
Matej Klotton0178d522016-11-04 11:11:44 +0100219 self._remote_hosts.append(host)
220 self._hosts_by_mac[mac] = host
221 self._hosts_by_ip4[ip4] = host
222 self._hosts_by_ip6[ip6] = host
223
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -0800224 @abc.abstractmethod
Matej Klottonc5bf07f2016-11-23 15:27:17 +0100225 def __init__(self, test):
226 self._test = test
227
228 self._remote_hosts = []
229 self._hosts_by_mac = {}
230 self._hosts_by_ip4 = {}
231 self._hosts_by_ip6 = {}
Matej Klotton0178d522016-11-04 11:11:44 +0100232
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700233 def set_mac(self, mac):
Ole Troan8006c6a2018-12-17 12:02:26 +0100234 self._local_mac = str(mac)
Neale Rannsefd7bc22019-11-11 08:32:34 +0000235 self._local_ip6_ll = mk_ll_addr(self._local_mac)
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700236 self.test.vapi.sw_interface_set_mac_address(
Ole Troan8006c6a2018-12-17 12:02:26 +0100237 self.sw_if_index, mac.packed)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500238 return self
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700239
Klement Sekera31da2e32018-06-24 22:49:55 +0200240 def set_sw_if_index(self, sw_if_index):
Paul Vinciguerra5ef9ca62019-12-01 22:16:54 -0500241 if sw_if_index > 255:
242 raise RuntimeError("Don't support sw_if_index values "
243 "greater than 255.")
Klement Sekera31da2e32018-06-24 22:49:55 +0200244 self._sw_if_index = sw_if_index
245
Matej Klotton0178d522016-11-04 11:11:44 +0100246 self.generate_remote_hosts()
Klement Sekeraf62ae122016-10-11 11:47:09 +0200247
Neale Rannsefd7bc22019-11-11 08:32:34 +0000248 self._local_ip4 = "172.16.%u.1" % self.sw_if_index
249 self._local_ip4_len = 24
Neale Ranns24b170a2017-08-15 05:33:11 -0700250 self._local_ip4_subnet = "172.16.%u.0" % self.sw_if_index
Neale Ranns24b170a2017-08-15 05:33:11 -0700251 self._local_ip4_bcast = "172.16.%u.255" % self.sw_if_index
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100252 self.has_ip4_config = False
253 self.ip4_table_id = 0
Klement Sekeraf62ae122016-10-11 11:47:09 +0200254
Neale Rannsefd7bc22019-11-11 08:32:34 +0000255 self._local_ip6 = "fd01:%x::1" % self.sw_if_index
256 self._local_ip6_len = 64
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100257 self.has_ip6_config = False
258 self.ip6_table_id = 0
Klement Sekeraf62ae122016-10-11 11:47:09 +0200259
Klement Sekera611864f2018-09-26 11:19:00 +0200260 self._local_addr = {socket.AF_INET: self.local_ip4,
261 socket.AF_INET6: self.local_ip6}
Klement Sekera611864f2018-09-26 11:19:00 +0200262 self._remote_addr = {socket.AF_INET: self.remote_ip4,
263 socket.AF_INET6: self.remote_ip6}
Klement Sekera611864f2018-09-26 11:19:00 +0200264
Paul Vinciguerra7a998232019-06-07 15:01:12 -0400265 r = self.test.vapi.sw_interface_dump(sw_if_index=self.sw_if_index)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200266 for intf in r:
267 if intf.sw_if_index == self.sw_if_index:
Ole Troan6ed154f2019-10-15 19:31:55 +0200268 self._name = intf.interface_name
269 self._local_mac = intf.l2_address
Klement Sekeraf62ae122016-10-11 11:47:09 +0200270 self._dump = intf
271 break
272 else:
273 raise Exception(
274 "Could not find interface with sw_if_index %d "
275 "in interface dump %s" %
Paul Vinciguerra6c746172018-11-26 09:57:21 -0800276 (self.sw_if_index, moves.reprlib.repr(r)))
Neale Rannsefd7bc22019-11-11 08:32:34 +0000277 self._local_ip6_ll = mk_ll_addr(self.local_mac)
Juraj Slobodac0374232018-02-01 15:18:49 +0100278 self._remote_ip6_ll = mk_ll_addr(self.remote_mac)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200279
Klement Sekeraf62ae122016-10-11 11:47:09 +0200280 def config_ip4(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100281 """Configure IPv4 address on the VPP interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200282 self.test.vapi.sw_interface_add_del_address(
Neale Rannsefd7bc22019-11-11 08:32:34 +0000283 sw_if_index=self.sw_if_index, prefix=self.local_ip4_prefix)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000284 self.has_ip4_config = True
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500285 return self
Neale Ranns177bbdc2016-11-15 09:46:51 +0000286
287 def unconfig_ip4(self):
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100288 """Remove IPv4 address on the VPP interface."""
Neale Ranns177bbdc2016-11-15 09:46:51 +0000289 try:
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100290 if self.has_ip4_config:
Neale Ranns177bbdc2016-11-15 09:46:51 +0000291 self.test.vapi.sw_interface_add_del_address(
Jakub Grajciar053204a2019-03-18 13:17:53 +0100292 sw_if_index=self.sw_if_index,
Neale Rannsefd7bc22019-11-11 08:32:34 +0000293 prefix=self.local_ip4_prefix, is_add=0)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000294 except AttributeError:
295 self.has_ip4_config = False
296 self.has_ip4_config = False
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500297 return self
Klement Sekeraf62ae122016-10-11 11:47:09 +0200298
Neale Rannsbaf2e902017-02-25 04:20:00 -0800299 def configure_ipv4_neighbors(self):
Jane546d3b2016-12-08 13:10:03 +0100300 """For every remote host assign neighbor's MAC to IPv4 addresses.
301
302 :param vrf_id: The FIB table / VRF ID. (Default value = 0)
303 """
Matej Klotton0178d522016-11-04 11:11:44 +0100304 for host in self._remote_hosts:
Neale Ranns37029302018-08-10 05:30:06 -0700305 self.test.vapi.ip_neighbor_add_del(self.sw_if_index,
306 host.mac,
307 host.ip4)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500308 return self
Matej Klotton0178d522016-11-04 11:11:44 +0100309
Klement Sekeraf62ae122016-10-11 11:47:09 +0200310 def config_ip6(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100311 """Configure IPv6 address on the VPP interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200312 self.test.vapi.sw_interface_add_del_address(
Neale Rannsefd7bc22019-11-11 08:32:34 +0000313 sw_if_index=self.sw_if_index, prefix=self.local_ip6_prefix)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000314 self.has_ip6_config = True
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500315 return self
Neale Ranns177bbdc2016-11-15 09:46:51 +0000316
317 def unconfig_ip6(self):
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100318 """Remove IPv6 address on the VPP interface."""
Neale Ranns177bbdc2016-11-15 09:46:51 +0000319 try:
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100320 if self.has_ip6_config:
Neale Ranns177bbdc2016-11-15 09:46:51 +0000321 self.test.vapi.sw_interface_add_del_address(
Jakub Grajciar053204a2019-03-18 13:17:53 +0100322 sw_if_index=self.sw_if_index,
Neale Rannsefd7bc22019-11-11 08:32:34 +0000323 prefix=self.local_ip6_prefix, is_add=0)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000324 except AttributeError:
325 self.has_ip6_config = False
326 self.has_ip6_config = False
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500327 return self
Neale Ranns177bbdc2016-11-15 09:46:51 +0000328
Neale Rannsbaf2e902017-02-25 04:20:00 -0800329 def configure_ipv6_neighbors(self):
Jan Gelety057bb8c2016-12-20 17:32:45 +0100330 """For every remote host assign neighbor's MAC to IPv6 addresses.
331
332 :param vrf_id: The FIB table / VRF ID. (Default value = 0)
333 """
Klement Sekera46a87ad2017-01-02 08:22:23 +0100334 for host in self._remote_hosts:
Neale Ranns37029302018-08-10 05:30:06 -0700335 self.test.vapi.ip_neighbor_add_del(self.sw_if_index,
336 host.mac,
337 host.ip6)
Klement Sekera46a87ad2017-01-02 08:22:23 +0100338
Neale Ranns177bbdc2016-11-15 09:46:51 +0000339 def unconfig(self):
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100340 """Unconfigure IPv6 and IPv4 address on the VPP interface."""
Neale Ranns177bbdc2016-11-15 09:46:51 +0000341 self.unconfig_ip4()
342 self.unconfig_ip6()
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500343 return self
Klement Sekeraf62ae122016-10-11 11:47:09 +0200344
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000345 def set_table_ip4(self, table_id):
346 """Set the interface in a IPv4 Table.
Matej Klotton86d87c42016-11-11 11:38:55 +0100347
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100348 .. note:: Must be called before configuring IP4 addresses.
349 """
350 self.ip4_table_id = table_id
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000351 self.test.vapi.sw_interface_set_table(
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100352 self.sw_if_index, 0, self.ip4_table_id)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500353 return self
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000354
355 def set_table_ip6(self, table_id):
356 """Set the interface in a IPv6 Table.
Matej Klotton86d87c42016-11-11 11:38:55 +0100357
358 .. note:: Must be called before configuring IP6 addresses.
359 """
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100360 self.ip6_table_id = table_id
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000361 self.test.vapi.sw_interface_set_table(
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100362 self.sw_if_index, 1, self.ip6_table_id)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500363 return self
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000364
Klement Sekeraf62ae122016-10-11 11:47:09 +0200365 def disable_ipv6_ra(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100366 """Configure IPv6 RA suppress on the VPP interface."""
Ole Troane1ade682019-03-04 23:55:43 +0100367 self.test.vapi.sw_interface_ip6nd_ra_config(
368 sw_if_index=self.sw_if_index,
369 suppress=1)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500370 return self
Klement Sekeraf62ae122016-10-11 11:47:09 +0200371
Neale Ranns32e1c012016-11-22 17:07:28 +0000372 def ip6_ra_config(self, no=0, suppress=0, send_unicast=0):
Neale Ranns75152282017-01-09 01:00:45 -0800373 """Configure IPv6 RA suppress on the VPP interface."""
Ole Troane1ade682019-03-04 23:55:43 +0100374 self.test.vapi.sw_interface_ip6nd_ra_config(
375 sw_if_index=self.sw_if_index,
376 is_no=no,
377 suppress=suppress,
378 send_unicast=send_unicast)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500379 return self
Neale Ranns75152282017-01-09 01:00:45 -0800380
Paul Vinciguerraab055082019-06-06 14:07:55 -0400381 def ip6_ra_prefix(self, prefix, is_no=0,
Neale Ranns87df12d2017-02-18 08:16:41 -0800382 off_link=0, no_autoconfig=0, use_default=0):
Paul Vinciguerraab055082019-06-06 14:07:55 -0400383 """Configure IPv6 RA suppress on the VPP interface.
384
385 prefix can be a string in the format of '<address>/<length_in_bits>'
386 or ipaddress.ipnetwork object (if strict.)"""
387
Ole Troane1ade682019-03-04 23:55:43 +0100388 self.test.vapi.sw_interface_ip6nd_ra_prefix(
Ole Troana5b2eec2019-03-11 19:23:25 +0100389 sw_if_index=self.sw_if_index,
Paul Vinciguerraab055082019-06-06 14:07:55 -0400390 prefix=prefix,
Ole Troana5b2eec2019-03-11 19:23:25 +0100391 use_default=use_default,
392 off_link=off_link, no_autoconfig=no_autoconfig,
393 is_no=is_no)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500394 return self
Neale Ranns87df12d2017-02-18 08:16:41 -0800395
Klement Sekeraf62ae122016-10-11 11:47:09 +0200396 def admin_up(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100397 """Put interface ADMIN-UP."""
Jakub Grajciar053204a2019-03-18 13:17:53 +0100398 self.test.vapi.sw_interface_set_flags(
399 self.sw_if_index,
400 flags=VppEnum.vl_api_if_status_flags_t.IF_STATUS_API_FLAG_ADMIN_UP)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500401 return self
Klement Sekeraf62ae122016-10-11 11:47:09 +0200402
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100403 def admin_down(self):
404 """Put interface ADMIN-down."""
Klement Sekerada505f62017-01-04 12:58:53 +0100405 self.test.vapi.sw_interface_set_flags(self.sw_if_index,
Jakub Grajciar053204a2019-03-18 13:17:53 +0100406 flags=0)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500407 return self
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100408
Neale Ranns63480742019-03-13 06:41:52 -0700409 def link_up(self):
410 """Put interface link-state-UP."""
411 self.test.vapi.cli("test interface link-state %s up" % self.name)
412
413 def link_down(self):
414 """Put interface link-state-down."""
415 self.test.vapi.cli("test interface link-state %s down" % self.name)
416
Neale Ranns75152282017-01-09 01:00:45 -0800417 def ip6_enable(self):
418 """IPv6 Enable interface"""
Ole Troane1ade682019-03-04 23:55:43 +0100419 self.test.vapi.sw_interface_ip6_enable_disable(self.sw_if_index,
Klement Sekerada505f62017-01-04 12:58:53 +0100420 enable=1)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500421 return self
Neale Ranns75152282017-01-09 01:00:45 -0800422
423 def ip6_disable(self):
424 """Put interface ADMIN-DOWN."""
Ole Troane1ade682019-03-04 23:55:43 +0100425 self.test.vapi.sw_interface_ip6_enable_disable(self.sw_if_index,
Klement Sekerada505f62017-01-04 12:58:53 +0100426 enable=0)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500427 return self
Neale Ranns75152282017-01-09 01:00:45 -0800428
Klement Sekeraf62ae122016-10-11 11:47:09 +0200429 def add_sub_if(self, sub_if):
Matej Klotton86d87c42016-11-11 11:38:55 +0100430 """Register a sub-interface with this interface.
Klement Sekeraf62ae122016-10-11 11:47:09 +0200431
432 :param sub_if: sub-interface
Klement Sekeraf62ae122016-10-11 11:47:09 +0200433 """
434 if not hasattr(self, 'sub_if'):
435 self.sub_if = sub_if
436 else:
437 if isinstance(self.sub_if, list):
438 self.sub_if.append(sub_if)
439 else:
440 self.sub_if = sub_if
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500441 return self
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000442
443 def enable_mpls(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100444 """Enable MPLS on the VPP interface."""
Ole Troan9a475372019-03-05 16:58:24 +0100445 self.test.vapi.sw_interface_set_mpls_enable(self.sw_if_index)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500446 return self
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100447
Neale Ranns180279b2017-03-16 15:49:09 -0400448 def disable_mpls(self):
449 """Enable MPLS on the VPP interface."""
Ole Troan9a475372019-03-05 16:58:24 +0100450 self.test.vapi.sw_interface_set_mpls_enable(self.sw_if_index, 0)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500451 return self
Neale Ranns180279b2017-03-16 15:49:09 -0400452
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100453 def is_ip4_entry_in_fib_dump(self, dump):
454 for i in dump:
Neale Ranns097fa662018-05-01 05:17:55 -0700455 n = IPv4Network(text_type("%s/%d" % (self.local_ip4,
456 self.local_ip4_prefix_len)))
457 if i.route.prefix == n and \
458 i.route.table_id == self.ip4_table_id:
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100459 return True
460 return False
Neale Ranns39f9d8b2017-02-16 21:57:05 -0800461
462 def set_unnumbered(self, ip_sw_if_index):
463 """ Set the interface to unnumbered via ip_sw_if_index """
Ole Troan9a475372019-03-05 16:58:24 +0100464 self.test.vapi.sw_interface_set_unnumbered(ip_sw_if_index,
465 self.sw_if_index)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500466 return self
Neale Ranns39f9d8b2017-02-16 21:57:05 -0800467
Neale Ranns37be7362017-02-21 17:30:26 -0800468 def unset_unnumbered(self, ip_sw_if_index):
Neale Ranns4b919a52017-03-11 05:55:21 -0800469 """ Unset the interface to unnumbered via ip_sw_if_index """
Ole Troan9a475372019-03-05 16:58:24 +0100470 self.test.vapi.sw_interface_set_unnumbered(ip_sw_if_index,
471 self.sw_if_index, is_add=0)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500472 return self
Neale Ranns37be7362017-02-21 17:30:26 -0800473
Neale Ranns39f9d8b2017-02-16 21:57:05 -0800474 def set_proxy_arp(self, enable=1):
475 """ Set the interface to enable/disable Proxy ARP """
476 self.test.vapi.proxy_arp_intfc_enable_disable(
477 self.sw_if_index,
478 enable)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500479 return self
Klement Sekera75e7d132017-09-20 08:26:30 +0200480
481 def query_vpp_config(self):
Paul Vinciguerra7a998232019-06-07 15:01:12 -0400482 dump = self.test.vapi.sw_interface_dump(sw_if_index=self.sw_if_index)
Klement Sekera75e7d132017-09-20 08:26:30 +0200483 return self.is_interface_config_in_dump(dump)
484
Juraj Slobodab3f90502018-10-04 15:15:16 +0200485 def get_interface_config_from_dump(self, dump):
Klement Sekera75e7d132017-09-20 08:26:30 +0200486 for i in dump:
487 if i.interface_name.rstrip(' \t\r\n\0') == self.name and \
Ole Troan9a475372019-03-05 16:58:24 +0100488 i.sw_if_index == self.sw_if_index:
Juraj Slobodab3f90502018-10-04 15:15:16 +0200489 return i
Klement Sekera75e7d132017-09-20 08:26:30 +0200490 else:
Juraj Slobodab3f90502018-10-04 15:15:16 +0200491 return None
492
493 def is_interface_config_in_dump(self, dump):
494 return self.get_interface_config_from_dump(dump) is not None
495
496 def assert_interface_state(self, admin_up_down, link_up_down,
497 expect_event=False):
498 if expect_event:
499 event = self.test.vapi.wait_for_event(timeout=1,
500 name='sw_interface_event')
501 self.test.assert_equal(event.sw_if_index, self.sw_if_index,
502 "sw_if_index")
Jakub Grajciar053204a2019-03-18 13:17:53 +0100503 self.test.assert_equal((event.flags & 1), admin_up_down,
Juraj Slobodab3f90502018-10-04 15:15:16 +0200504 "admin state")
Jakub Grajciar053204a2019-03-18 13:17:53 +0100505 self.test.assert_equal((event.flags & 2), link_up_down,
Juraj Slobodab3f90502018-10-04 15:15:16 +0200506 "link state")
507 dump = self.test.vapi.sw_interface_dump()
508 if_state = self.get_interface_config_from_dump(dump)
Jakub Grajciar053204a2019-03-18 13:17:53 +0100509 self.test.assert_equal((if_state.flags & 1), admin_up_down,
Juraj Slobodab3f90502018-10-04 15:15:16 +0200510 "admin state")
Jakub Grajciar053204a2019-03-18 13:17:53 +0100511 self.test.assert_equal((if_state.flags & 2), link_up_down,
Juraj Slobodab3f90502018-10-04 15:15:16 +0200512 "link state")
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700513
514 def __str__(self):
515 return self.name
Neale Ranns2ac885c2019-03-20 18:24:43 +0000516
517 def get_rx_stats(self):
518 c = self.test.statistics.get_counter("^/if/rx$")
519 return c[0][self.sw_if_index]
520
521 def get_tx_stats(self):
522 c = self.test.statistics.get_counter("^/if/tx$")
523 return c[0][self.sw_if_index]
Ole Troaneb284a12019-10-09 13:33:19 +0200524
525 def set_l3_mtu(self, mtu):
526 self.test.vapi.sw_interface_set_mtu(self.sw_if_index, [mtu, 0, 0, 0])
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500527 return self
Ole Troaneb284a12019-10-09 13:33:19 +0200528
529 def set_ip4_mtu(self, mtu):
530 self.test.vapi.sw_interface_set_mtu(self.sw_if_index, [0, mtu, 0, 0])
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500531 return self
Ole Troaneb284a12019-10-09 13:33:19 +0200532
533 def set_ip6_mtu(self, mtu):
534 self.test.vapi.sw_interface_set_mtu(self.sw_if_index, [0, 0, mtu, 0])
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500535 return self
Ole Troaneb284a12019-10-09 13:33:19 +0200536
537 def set_mpls_mtu(self, mtu):
538 self.test.vapi.sw_interface_set_mtu(self.sw_if_index, [0, 0, 0, mtu])
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500539 return self