blob: d586c849b02f1a86b1371b4d0401c0b092d0a861 [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
Neale Ranns37029302018-08-10 05:30:06 -07009from vpp_papi import mac_ntop
Jan65209ed2016-12-05 23:29:17 +010010
Klement Sekeraf62ae122016-10-11 11:47:09 +020011
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -080012@six.add_metaclass(abc.ABCMeta)
Klement Sekeraf62ae122016-10-11 11:47:09 +020013class VppInterface(object):
Matej Klotton86d87c42016-11-11 11:38:55 +010014 """Generic VPP interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +020015
16 @property
17 def sw_if_index(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010018 """Interface index assigned by VPP."""
Klement Sekeraf62ae122016-10-11 11:47:09 +020019 return self._sw_if_index
20
21 @property
22 def remote_mac(self):
Klement Sekerada505f62017-01-04 12:58:53 +010023 """MAC-address of the remote interface "connected" to this interface"""
Matej Klotton0178d522016-11-04 11:11:44 +010024 return self._remote_hosts[0].mac
Klement Sekeraf62ae122016-10-11 11:47:09 +020025
26 @property
27 def local_mac(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010028 """MAC-address of the VPP interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +020029 return self._local_mac
30
31 @property
Klement Sekera611864f2018-09-26 11:19:00 +020032 def local_addr(self):
33 return self._local_addr
34
35 @property
36 def remote_addr(self):
37 return self._remote_addr
38
39 @property
40 def local_addr_n(self):
41 return self._local_addr_n
42
43 @property
44 def remote_addr_n(self):
45 return self._remote_addr_n
46
47 @property
Klement Sekeraf62ae122016-10-11 11:47:09 +020048 def local_ip4(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010049 """Local IPv4 address on VPP interface (string)."""
Klement Sekeraf62ae122016-10-11 11:47:09 +020050 return self._local_ip4
51
52 @property
53 def local_ip4n(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010054 """Local IPv4 address - raw, suitable as API parameter."""
Matej Klotton0178d522016-11-04 11:11:44 +010055 return socket.inet_pton(socket.AF_INET, self._local_ip4)
Klement Sekeraf62ae122016-10-11 11:47:09 +020056
57 @property
58 def remote_ip4(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010059 """IPv4 address of remote peer "connected" to this interface."""
Matej Klotton0178d522016-11-04 11:11:44 +010060 return self._remote_hosts[0].ip4
Klement Sekeraf62ae122016-10-11 11:47:09 +020061
62 @property
63 def remote_ip4n(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010064 """IPv4 address of remote peer - raw, suitable as API parameter."""
Matej Klotton0178d522016-11-04 11:11:44 +010065 return socket.inet_pton(socket.AF_INET, self.remote_ip4)
Klement Sekeraf62ae122016-10-11 11:47:09 +020066
67 @property
68 def local_ip6(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010069 """Local IPv6 address on VPP interface (string)."""
Klement Sekeraf62ae122016-10-11 11:47:09 +020070 return self._local_ip6
71
72 @property
73 def local_ip6n(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010074 """Local IPv6 address - raw, suitable as API parameter."""
Matej Klotton0178d522016-11-04 11:11:44 +010075 return socket.inet_pton(socket.AF_INET6, self.local_ip6)
Klement Sekeraf62ae122016-10-11 11:47:09 +020076
77 @property
78 def remote_ip6(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010079 """IPv6 address of remote peer "connected" to this interface."""
Matej Klotton0178d522016-11-04 11:11:44 +010080 return self._remote_hosts[0].ip6
Klement Sekeraf62ae122016-10-11 11:47:09 +020081
82 @property
83 def remote_ip6n(self):
84 """IPv6 address of remote peer - raw, suitable as API parameter"""
Matej Klotton0178d522016-11-04 11:11:44 +010085 return socket.inet_pton(socket.AF_INET6, self.remote_ip6)
Klement Sekeraf62ae122016-10-11 11:47:09 +020086
87 @property
Juraj Slobodac0374232018-02-01 15:18:49 +010088 def local_ip6_ll(self):
Klement Sekera611864f2018-09-26 11:19:00 +020089 """Local IPv6 link-local address on VPP interface (string)."""
Juraj Slobodac0374232018-02-01 15:18:49 +010090 return self._local_ip6_ll
91
92 @property
93 def local_ip6n_ll(self):
94 """Local IPv6 link-local address - raw, suitable as API parameter."""
95 return self._local_ip6n_ll
96
97 @property
98 def remote_ip6_ll(self):
99 """Link-local IPv6 address of remote peer
100 "connected" to this interface."""
101 return self._remote_ip6_ll
102
103 @property
104 def remote_ip6n_ll(self):
105 """Link-local IPv6 address of remote peer
106 - raw, suitable as API parameter"""
107 return self._remote_ip6n_ll
108
109 @property
Klement Sekeraf62ae122016-10-11 11:47:09 +0200110 def name(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100111 """Name of the interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200112 return self._name
113
114 @property
115 def dump(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100116 """RAW result of sw_interface_dump for this interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200117 return self._dump
118
119 @property
120 def test(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100121 """Test case creating this interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200122 return self._test
123
Matej Klotton0178d522016-11-04 11:11:44 +0100124 @property
125 def remote_hosts(self):
126 """Remote hosts list"""
127 return self._remote_hosts
128
129 @remote_hosts.setter
130 def remote_hosts(self, value):
Matej Klotton86d87c42016-11-11 11:38:55 +0100131 """
132 :param list value: List of remote hosts.
133 """
Matej Klotton0178d522016-11-04 11:11:44 +0100134 self._remote_hosts = value
Matej Klotton86d87c42016-11-11 11:38:55 +0100135 self._hosts_by_mac = {}
136 self._hosts_by_ip4 = {}
137 self._hosts_by_ip6 = {}
138 for host in self._remote_hosts:
139 self._hosts_by_mac[host.mac] = host
140 self._hosts_by_ip4[host.ip4] = host
141 self._hosts_by_ip6[host.ip6] = host
Matej Klotton0178d522016-11-04 11:11:44 +0100142
143 def host_by_mac(self, mac):
Matej Klotton86d87c42016-11-11 11:38:55 +0100144 """
Matej Klottonc5bf07f2016-11-23 15:27:17 +0100145 :param mac: MAC address to find host by.
Matej Klotton86d87c42016-11-11 11:38:55 +0100146 :return: Host object assigned to interface.
147 """
Matej Klotton0178d522016-11-04 11:11:44 +0100148 return self._hosts_by_mac[mac]
149
150 def host_by_ip4(self, ip):
Matej Klotton86d87c42016-11-11 11:38:55 +0100151 """
152 :param ip: IPv4 address to find host by.
153 :return: Host object assigned to interface.
154 """
Matej Klotton0178d522016-11-04 11:11:44 +0100155 return self._hosts_by_ip4[ip]
156
157 def host_by_ip6(self, ip):
Matej Klotton86d87c42016-11-11 11:38:55 +0100158 """
159 :param ip: IPv6 address to find host by.
160 :return: Host object assigned to interface.
161 """
Matej Klotton0178d522016-11-04 11:11:44 +0100162 return self._hosts_by_ip6[ip]
163
164 def generate_remote_hosts(self, count=1):
Matej Klotton86d87c42016-11-11 11:38:55 +0100165 """Generate and add remote hosts for the interface.
166
167 :param int count: Number of generated remote hosts.
168 """
Matej Klotton0178d522016-11-04 11:11:44 +0100169 self._remote_hosts = []
170 self._hosts_by_mac = {}
171 self._hosts_by_ip4 = {}
172 self._hosts_by_ip6 = {}
Klement Sekera7bb873a2016-11-18 07:38:42 +0100173 for i in range(
174 2, count + 2): # 0: network address, 1: local vpp address
Matej Klotton0178d522016-11-04 11:11:44 +0100175 mac = "02:%02x:00:00:ff:%02x" % (self.sw_if_index, i)
176 ip4 = "172.16.%u.%u" % (self.sw_if_index, i)
Klement Sekera46a87ad2017-01-02 08:22:23 +0100177 ip6 = "fd01:%x::%x" % (self.sw_if_index, i)
Neale Ranns2a3ea492017-04-19 05:24:40 -0700178 ip6_ll = mk_ll_addr(mac)
179 host = Host(mac, ip4, ip6, ip6_ll)
Matej Klotton0178d522016-11-04 11:11:44 +0100180 self._remote_hosts.append(host)
181 self._hosts_by_mac[mac] = host
182 self._hosts_by_ip4[ip4] = host
183 self._hosts_by_ip6[ip6] = host
184
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -0800185 @abc.abstractmethod
Matej Klottonc5bf07f2016-11-23 15:27:17 +0100186 def __init__(self, test):
187 self._test = test
188
189 self._remote_hosts = []
190 self._hosts_by_mac = {}
191 self._hosts_by_ip4 = {}
192 self._hosts_by_ip6 = {}
Matej Klotton0178d522016-11-04 11:11:44 +0100193
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700194 def set_mac(self, mac):
Ole Troan8006c6a2018-12-17 12:02:26 +0100195 self._local_mac = str(mac)
196 self._local_ip6_ll = mk_ll_addr(self._local_mac)
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700197 self.test.vapi.sw_interface_set_mac_address(
Ole Troan8006c6a2018-12-17 12:02:26 +0100198 self.sw_if_index, mac.packed)
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700199
Klement Sekera31da2e32018-06-24 22:49:55 +0200200 def set_sw_if_index(self, sw_if_index):
201 self._sw_if_index = sw_if_index
202
Matej Klotton0178d522016-11-04 11:11:44 +0100203 self.generate_remote_hosts()
Klement Sekeraf62ae122016-10-11 11:47:09 +0200204
205 self._local_ip4 = "172.16.%u.1" % self.sw_if_index
206 self._local_ip4n = socket.inet_pton(socket.AF_INET, self.local_ip4)
Neale Ranns24b170a2017-08-15 05:33:11 -0700207 self._local_ip4_subnet = "172.16.%u.0" % self.sw_if_index
208 self._local_ip4n_subnet = socket.inet_pton(socket.AF_INET,
209 self._local_ip4_subnet)
210 self._local_ip4_bcast = "172.16.%u.255" % self.sw_if_index
211 self._local_ip4n_bcast = socket.inet_pton(socket.AF_INET,
212 self._local_ip4_bcast)
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100213 self.local_ip4_prefix_len = 24
214 self.has_ip4_config = False
215 self.ip4_table_id = 0
Klement Sekeraf62ae122016-10-11 11:47:09 +0200216
Klement Sekera46a87ad2017-01-02 08:22:23 +0100217 self._local_ip6 = "fd01:%x::1" % self.sw_if_index
Klement Sekeraf62ae122016-10-11 11:47:09 +0200218 self._local_ip6n = socket.inet_pton(socket.AF_INET6, self.local_ip6)
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100219 self.local_ip6_prefix_len = 64
220 self.has_ip6_config = False
221 self.ip6_table_id = 0
Klement Sekeraf62ae122016-10-11 11:47:09 +0200222
Klement Sekera611864f2018-09-26 11:19:00 +0200223 self._local_addr = {socket.AF_INET: self.local_ip4,
224 socket.AF_INET6: self.local_ip6}
225 self._local_addr_n = {socket.AF_INET: self.local_ip4n,
226 socket.AF_INET6: self.local_ip6n}
227 self._remote_addr = {socket.AF_INET: self.remote_ip4,
228 socket.AF_INET6: self.remote_ip6}
229 self._remote_addr_n = {socket.AF_INET: self.remote_ip4n,
230 socket.AF_INET6: self.remote_ip6n}
231
Klement Sekeraf62ae122016-10-11 11:47:09 +0200232 r = self.test.vapi.sw_interface_dump()
233 for intf in r:
234 if intf.sw_if_index == self.sw_if_index:
Ole Troan7f991832018-12-06 17:35:12 +0100235 self._name = intf.interface_name.split(b'\0',
236 1)[0].decode('utf8')
Ole Troan8006c6a2018-12-17 12:02:26 +0100237 self._local_mac = mac_ntop(intf.l2_address)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200238 self._dump = intf
239 break
240 else:
241 raise Exception(
242 "Could not find interface with sw_if_index %d "
243 "in interface dump %s" %
Paul Vinciguerra6c746172018-11-26 09:57:21 -0800244 (self.sw_if_index, moves.reprlib.repr(r)))
Neale Ranns2a3ea492017-04-19 05:24:40 -0700245 self._local_ip6_ll = mk_ll_addr(self.local_mac)
246 self._local_ip6n_ll = socket.inet_pton(socket.AF_INET6,
247 self.local_ip6_ll)
Juraj Slobodac0374232018-02-01 15:18:49 +0100248 self._remote_ip6_ll = mk_ll_addr(self.remote_mac)
249 self._remote_ip6n_ll = socket.inet_pton(socket.AF_INET6,
250 self.remote_ip6_ll)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200251
Klement Sekeraf62ae122016-10-11 11:47:09 +0200252 def config_ip4(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100253 """Configure IPv4 address on the VPP interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200254 self.test.vapi.sw_interface_add_del_address(
Ole Troan9a475372019-03-05 16:58:24 +0100255 sw_if_index=self.sw_if_index, address=self.local_ip4n,
256 address_length=self.local_ip4_prefix_len)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000257 self.has_ip4_config = True
258
259 def unconfig_ip4(self):
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100260 """Remove IPv4 address on the VPP interface."""
Neale Ranns177bbdc2016-11-15 09:46:51 +0000261 try:
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100262 if self.has_ip4_config:
Neale Ranns177bbdc2016-11-15 09:46:51 +0000263 self.test.vapi.sw_interface_add_del_address(
Ole Troan9a475372019-03-05 16:58:24 +0100264 sw_if_index=self.sw_if_index, address=self.local_ip4n,
265 address_length=self.local_ip4_prefix_len, is_add=0)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000266 except AttributeError:
267 self.has_ip4_config = False
268 self.has_ip4_config = False
Klement Sekeraf62ae122016-10-11 11:47:09 +0200269
Neale Rannsbaf2e902017-02-25 04:20:00 -0800270 def configure_ipv4_neighbors(self):
Jane546d3b2016-12-08 13:10:03 +0100271 """For every remote host assign neighbor's MAC to IPv4 addresses.
272
273 :param vrf_id: The FIB table / VRF ID. (Default value = 0)
274 """
Matej Klotton0178d522016-11-04 11:11:44 +0100275 for host in self._remote_hosts:
Neale Ranns37029302018-08-10 05:30:06 -0700276 self.test.vapi.ip_neighbor_add_del(self.sw_if_index,
277 host.mac,
278 host.ip4)
Matej Klotton0178d522016-11-04 11:11:44 +0100279
Klement Sekeraf62ae122016-10-11 11:47:09 +0200280 def config_ip6(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100281 """Configure IPv6 address on the VPP interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200282 self.test.vapi.sw_interface_add_del_address(
Ole Troan9a475372019-03-05 16:58:24 +0100283 sw_if_index=self.sw_if_index, address=self._local_ip6n,
284 address_length=self.local_ip6_prefix_len, is_ipv6=1)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000285 self.has_ip6_config = True
286
287 def unconfig_ip6(self):
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100288 """Remove IPv6 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_ip6_config:
Neale Ranns177bbdc2016-11-15 09:46:51 +0000291 self.test.vapi.sw_interface_add_del_address(
Ole Troan9a475372019-03-05 16:58:24 +0100292 sw_if_index=self.sw_if_index, address=self.local_ip6n,
293 address_length=self.local_ip6_prefix_len, is_ipv6=1,
294 is_add=0)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000295 except AttributeError:
296 self.has_ip6_config = False
297 self.has_ip6_config = False
298
Neale Rannsbaf2e902017-02-25 04:20:00 -0800299 def configure_ipv6_neighbors(self):
Jan Gelety057bb8c2016-12-20 17:32:45 +0100300 """For every remote host assign neighbor's MAC to IPv6 addresses.
301
302 :param vrf_id: The FIB table / VRF ID. (Default value = 0)
303 """
Klement Sekera46a87ad2017-01-02 08:22:23 +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.ip6)
Klement Sekera46a87ad2017-01-02 08:22:23 +0100308
Neale Ranns177bbdc2016-11-15 09:46:51 +0000309 def unconfig(self):
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100310 """Unconfigure IPv6 and IPv4 address on the VPP interface."""
Neale Ranns177bbdc2016-11-15 09:46:51 +0000311 self.unconfig_ip4()
312 self.unconfig_ip6()
Klement Sekeraf62ae122016-10-11 11:47:09 +0200313
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000314 def set_table_ip4(self, table_id):
315 """Set the interface in a IPv4 Table.
Matej Klotton86d87c42016-11-11 11:38:55 +0100316
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100317 .. note:: Must be called before configuring IP4 addresses.
318 """
319 self.ip4_table_id = table_id
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000320 self.test.vapi.sw_interface_set_table(
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100321 self.sw_if_index, 0, self.ip4_table_id)
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000322
323 def set_table_ip6(self, table_id):
324 """Set the interface in a IPv6 Table.
Matej Klotton86d87c42016-11-11 11:38:55 +0100325
326 .. note:: Must be called before configuring IP6 addresses.
327 """
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100328 self.ip6_table_id = table_id
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000329 self.test.vapi.sw_interface_set_table(
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100330 self.sw_if_index, 1, self.ip6_table_id)
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000331
Klement Sekeraf62ae122016-10-11 11:47:09 +0200332 def disable_ipv6_ra(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100333 """Configure IPv6 RA suppress on the VPP interface."""
Ole Troane1ade682019-03-04 23:55:43 +0100334 self.test.vapi.sw_interface_ip6nd_ra_config(
335 sw_if_index=self.sw_if_index,
336 suppress=1)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200337
Neale Ranns32e1c012016-11-22 17:07:28 +0000338 def ip6_ra_config(self, no=0, suppress=0, send_unicast=0):
Neale Ranns75152282017-01-09 01:00:45 -0800339 """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 is_no=no,
343 suppress=suppress,
344 send_unicast=send_unicast)
Neale Ranns75152282017-01-09 01:00:45 -0800345
Ole Troana5b2eec2019-03-11 19:23:25 +0100346 # TODO: This should accept ipaddress object.
Neale Ranns87df12d2017-02-18 08:16:41 -0800347 def ip6_ra_prefix(self, address, address_length, is_no=0,
348 off_link=0, no_autoconfig=0, use_default=0):
349 """Configure IPv6 RA suppress on the VPP interface."""
Ole Troane1ade682019-03-04 23:55:43 +0100350 self.test.vapi.sw_interface_ip6nd_ra_prefix(
Ole Troana5b2eec2019-03-11 19:23:25 +0100351 sw_if_index=self.sw_if_index,
352 prefix={'address': address,
353 'address_length': address_length},
354 use_default=use_default,
355 off_link=off_link, no_autoconfig=no_autoconfig,
356 is_no=is_no)
Neale Ranns87df12d2017-02-18 08:16:41 -0800357
Klement Sekeraf62ae122016-10-11 11:47:09 +0200358 def admin_up(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100359 """Put interface ADMIN-UP."""
Klement Sekerada505f62017-01-04 12:58:53 +0100360 self.test.vapi.sw_interface_set_flags(self.sw_if_index,
361 admin_up_down=1)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200362
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100363 def admin_down(self):
364 """Put interface ADMIN-down."""
Klement Sekerada505f62017-01-04 12:58:53 +0100365 self.test.vapi.sw_interface_set_flags(self.sw_if_index,
366 admin_up_down=0)
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100367
Neale Ranns63480742019-03-13 06:41:52 -0700368 def link_up(self):
369 """Put interface link-state-UP."""
370 self.test.vapi.cli("test interface link-state %s up" % self.name)
371
372 def link_down(self):
373 """Put interface link-state-down."""
374 self.test.vapi.cli("test interface link-state %s down" % self.name)
375
Neale Ranns75152282017-01-09 01:00:45 -0800376 def ip6_enable(self):
377 """IPv6 Enable interface"""
Ole Troane1ade682019-03-04 23:55:43 +0100378 self.test.vapi.sw_interface_ip6_enable_disable(self.sw_if_index,
Klement Sekerada505f62017-01-04 12:58:53 +0100379 enable=1)
Neale Ranns75152282017-01-09 01:00:45 -0800380
381 def ip6_disable(self):
382 """Put interface ADMIN-DOWN."""
Ole Troane1ade682019-03-04 23:55:43 +0100383 self.test.vapi.sw_interface_ip6_enable_disable(self.sw_if_index,
Klement Sekerada505f62017-01-04 12:58:53 +0100384 enable=0)
Neale Ranns75152282017-01-09 01:00:45 -0800385
Klement Sekeraf62ae122016-10-11 11:47:09 +0200386 def add_sub_if(self, sub_if):
Matej Klotton86d87c42016-11-11 11:38:55 +0100387 """Register a sub-interface with this interface.
Klement Sekeraf62ae122016-10-11 11:47:09 +0200388
389 :param sub_if: sub-interface
Klement Sekeraf62ae122016-10-11 11:47:09 +0200390 """
391 if not hasattr(self, 'sub_if'):
392 self.sub_if = sub_if
393 else:
394 if isinstance(self.sub_if, list):
395 self.sub_if.append(sub_if)
396 else:
397 self.sub_if = sub_if
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000398
399 def enable_mpls(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100400 """Enable MPLS on the VPP interface."""
Ole Troan9a475372019-03-05 16:58:24 +0100401 self.test.vapi.sw_interface_set_mpls_enable(self.sw_if_index)
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100402
Neale Ranns180279b2017-03-16 15:49:09 -0400403 def disable_mpls(self):
404 """Enable MPLS on the VPP interface."""
Ole Troan9a475372019-03-05 16:58:24 +0100405 self.test.vapi.sw_interface_set_mpls_enable(self.sw_if_index, 0)
Neale Ranns180279b2017-03-16 15:49:09 -0400406
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100407 def is_ip4_entry_in_fib_dump(self, dump):
408 for i in dump:
409 if i.address == self.local_ip4n and \
Ole Troan9a475372019-03-05 16:58:24 +0100410 i.address_length == self.local_ip4_prefix_len and \
411 i.table_id == self.ip4_table_id:
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100412 return True
413 return False
Neale Ranns39f9d8b2017-02-16 21:57:05 -0800414
415 def set_unnumbered(self, ip_sw_if_index):
416 """ Set the interface to unnumbered via ip_sw_if_index """
Ole Troan9a475372019-03-05 16:58:24 +0100417 self.test.vapi.sw_interface_set_unnumbered(ip_sw_if_index,
418 self.sw_if_index)
Neale Ranns39f9d8b2017-02-16 21:57:05 -0800419
Neale Ranns37be7362017-02-21 17:30:26 -0800420 def unset_unnumbered(self, ip_sw_if_index):
Neale Ranns4b919a52017-03-11 05:55:21 -0800421 """ Unset the interface to unnumbered via ip_sw_if_index """
Ole Troan9a475372019-03-05 16:58:24 +0100422 self.test.vapi.sw_interface_set_unnumbered(ip_sw_if_index,
423 self.sw_if_index, is_add=0)
Neale Ranns37be7362017-02-21 17:30:26 -0800424
Neale Ranns39f9d8b2017-02-16 21:57:05 -0800425 def set_proxy_arp(self, enable=1):
426 """ Set the interface to enable/disable Proxy ARP """
427 self.test.vapi.proxy_arp_intfc_enable_disable(
428 self.sw_if_index,
429 enable)
Klement Sekera75e7d132017-09-20 08:26:30 +0200430
431 def query_vpp_config(self):
432 dump = self.test.vapi.sw_interface_dump()
433 return self.is_interface_config_in_dump(dump)
434
Juraj Slobodab3f90502018-10-04 15:15:16 +0200435 def get_interface_config_from_dump(self, dump):
Klement Sekera75e7d132017-09-20 08:26:30 +0200436 for i in dump:
437 if i.interface_name.rstrip(' \t\r\n\0') == self.name and \
Ole Troan9a475372019-03-05 16:58:24 +0100438 i.sw_if_index == self.sw_if_index:
Juraj Slobodab3f90502018-10-04 15:15:16 +0200439 return i
Klement Sekera75e7d132017-09-20 08:26:30 +0200440 else:
Juraj Slobodab3f90502018-10-04 15:15:16 +0200441 return None
442
443 def is_interface_config_in_dump(self, dump):
444 return self.get_interface_config_from_dump(dump) is not None
445
446 def assert_interface_state(self, admin_up_down, link_up_down,
447 expect_event=False):
448 if expect_event:
449 event = self.test.vapi.wait_for_event(timeout=1,
450 name='sw_interface_event')
451 self.test.assert_equal(event.sw_if_index, self.sw_if_index,
452 "sw_if_index")
453 self.test.assert_equal(event.admin_up_down, admin_up_down,
454 "admin state")
455 self.test.assert_equal(event.link_up_down, link_up_down,
456 "link state")
457 dump = self.test.vapi.sw_interface_dump()
458 if_state = self.get_interface_config_from_dump(dump)
459 self.test.assert_equal(if_state.admin_up_down, admin_up_down,
460 "admin state")
461 self.test.assert_equal(if_state.link_up_down, link_up_down,
462 "link state")
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700463
464 def __str__(self):
465 return self.name