blob: 719f77b36c7a31ee2821f71f256c4e60cfc9f772 [file] [log] [blame]
Paul Vinciguerra6e4c6ad2018-11-25 10:35:29 -08001import binascii
Klement Sekeraf62ae122016-10-11 11:47:09 +02002import socket
Paul Vinciguerra00671cf2018-11-25 12:47:04 -08003from abc import abstractmethod, ABCMeta
4
Paul Vinciguerra6c746172018-11-26 09:57:21 -08005from six import moves
Klement Sekeraf62ae122016-10-11 11:47:09 +02006
Ole Troan7f991832018-12-06 17:35:12 +01007from util import Host, mk_ll_addr
Ole Troan8006c6a2018-12-17 12:02:26 +01008from vpp_papi import mac_pton, mac_ntop
Jan65209ed2016-12-05 23:29:17 +01009
Klement Sekeraf62ae122016-10-11 11:47:09 +020010
11class VppInterface(object):
Matej Klotton86d87c42016-11-11 11:38:55 +010012 """Generic VPP interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +020013 __metaclass__ = ABCMeta
14
15 @property
16 def sw_if_index(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010017 """Interface index assigned by VPP."""
Klement Sekeraf62ae122016-10-11 11:47:09 +020018 return self._sw_if_index
19
20 @property
21 def remote_mac(self):
Klement Sekerada505f62017-01-04 12:58:53 +010022 """MAC-address of the remote interface "connected" to this interface"""
Matej Klotton0178d522016-11-04 11:11:44 +010023 return self._remote_hosts[0].mac
Klement Sekeraf62ae122016-10-11 11:47:09 +020024
25 @property
26 def local_mac(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010027 """MAC-address of the VPP interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +020028 return self._local_mac
29
30 @property
Klement Sekera611864f2018-09-26 11:19:00 +020031 def local_addr(self):
32 return self._local_addr
33
34 @property
35 def remote_addr(self):
36 return self._remote_addr
37
38 @property
39 def local_addr_n(self):
40 return self._local_addr_n
41
42 @property
43 def remote_addr_n(self):
44 return self._remote_addr_n
45
46 @property
Klement Sekeraf62ae122016-10-11 11:47:09 +020047 def local_ip4(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010048 """Local IPv4 address on VPP interface (string)."""
Klement Sekeraf62ae122016-10-11 11:47:09 +020049 return self._local_ip4
50
51 @property
52 def local_ip4n(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010053 """Local IPv4 address - raw, suitable as API parameter."""
Matej Klotton0178d522016-11-04 11:11:44 +010054 return socket.inet_pton(socket.AF_INET, self._local_ip4)
Klement Sekeraf62ae122016-10-11 11:47:09 +020055
56 @property
57 def remote_ip4(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010058 """IPv4 address of remote peer "connected" to this interface."""
Matej Klotton0178d522016-11-04 11:11:44 +010059 return self._remote_hosts[0].ip4
Klement Sekeraf62ae122016-10-11 11:47:09 +020060
61 @property
62 def remote_ip4n(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010063 """IPv4 address of remote peer - raw, suitable as API parameter."""
Matej Klotton0178d522016-11-04 11:11:44 +010064 return socket.inet_pton(socket.AF_INET, self.remote_ip4)
Klement Sekeraf62ae122016-10-11 11:47:09 +020065
66 @property
67 def local_ip6(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010068 """Local IPv6 address on VPP interface (string)."""
Klement Sekeraf62ae122016-10-11 11:47:09 +020069 return self._local_ip6
70
71 @property
72 def local_ip6n(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010073 """Local IPv6 address - raw, suitable as API parameter."""
Matej Klotton0178d522016-11-04 11:11:44 +010074 return socket.inet_pton(socket.AF_INET6, self.local_ip6)
Klement Sekeraf62ae122016-10-11 11:47:09 +020075
76 @property
77 def remote_ip6(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010078 """IPv6 address of remote peer "connected" to this interface."""
Matej Klotton0178d522016-11-04 11:11:44 +010079 return self._remote_hosts[0].ip6
Klement Sekeraf62ae122016-10-11 11:47:09 +020080
81 @property
82 def remote_ip6n(self):
83 """IPv6 address of remote peer - raw, suitable as API parameter"""
Matej Klotton0178d522016-11-04 11:11:44 +010084 return socket.inet_pton(socket.AF_INET6, self.remote_ip6)
Klement Sekeraf62ae122016-10-11 11:47:09 +020085
86 @property
Juraj Slobodac0374232018-02-01 15:18:49 +010087 def local_ip6_ll(self):
Klement Sekera611864f2018-09-26 11:19:00 +020088 """Local IPv6 link-local address on VPP interface (string)."""
Juraj Slobodac0374232018-02-01 15:18:49 +010089 return self._local_ip6_ll
90
91 @property
92 def local_ip6n_ll(self):
93 """Local IPv6 link-local address - raw, suitable as API parameter."""
94 return self._local_ip6n_ll
95
96 @property
97 def remote_ip6_ll(self):
98 """Link-local IPv6 address of remote peer
99 "connected" to this interface."""
100 return self._remote_ip6_ll
101
102 @property
103 def remote_ip6n_ll(self):
104 """Link-local IPv6 address of remote peer
105 - raw, suitable as API parameter"""
106 return self._remote_ip6n_ll
107
108 @property
Klement Sekeraf62ae122016-10-11 11:47:09 +0200109 def name(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100110 """Name of the interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200111 return self._name
112
113 @property
114 def dump(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100115 """RAW result of sw_interface_dump for this interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200116 return self._dump
117
118 @property
119 def test(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100120 """Test case creating this interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200121 return self._test
122
Matej Klotton0178d522016-11-04 11:11:44 +0100123 @property
124 def remote_hosts(self):
125 """Remote hosts list"""
126 return self._remote_hosts
127
128 @remote_hosts.setter
129 def remote_hosts(self, value):
Matej Klotton86d87c42016-11-11 11:38:55 +0100130 """
131 :param list value: List of remote hosts.
132 """
Matej Klotton0178d522016-11-04 11:11:44 +0100133 self._remote_hosts = value
Matej Klotton86d87c42016-11-11 11:38:55 +0100134 self._hosts_by_mac = {}
135 self._hosts_by_ip4 = {}
136 self._hosts_by_ip6 = {}
137 for host in self._remote_hosts:
138 self._hosts_by_mac[host.mac] = host
139 self._hosts_by_ip4[host.ip4] = host
140 self._hosts_by_ip6[host.ip6] = host
Matej Klotton0178d522016-11-04 11:11:44 +0100141
142 def host_by_mac(self, mac):
Matej Klotton86d87c42016-11-11 11:38:55 +0100143 """
Matej Klottonc5bf07f2016-11-23 15:27:17 +0100144 :param mac: MAC address to find host by.
Matej Klotton86d87c42016-11-11 11:38:55 +0100145 :return: Host object assigned to interface.
146 """
Matej Klotton0178d522016-11-04 11:11:44 +0100147 return self._hosts_by_mac[mac]
148
149 def host_by_ip4(self, ip):
Matej Klotton86d87c42016-11-11 11:38:55 +0100150 """
151 :param ip: IPv4 address to find host by.
152 :return: Host object assigned to interface.
153 """
Matej Klotton0178d522016-11-04 11:11:44 +0100154 return self._hosts_by_ip4[ip]
155
156 def host_by_ip6(self, ip):
Matej Klotton86d87c42016-11-11 11:38:55 +0100157 """
158 :param ip: IPv6 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_ip6[ip]
162
163 def generate_remote_hosts(self, count=1):
Matej Klotton86d87c42016-11-11 11:38:55 +0100164 """Generate and add remote hosts for the interface.
165
166 :param int count: Number of generated remote hosts.
167 """
Matej Klotton0178d522016-11-04 11:11:44 +0100168 self._remote_hosts = []
169 self._hosts_by_mac = {}
170 self._hosts_by_ip4 = {}
171 self._hosts_by_ip6 = {}
Klement Sekera7bb873a2016-11-18 07:38:42 +0100172 for i in range(
173 2, count + 2): # 0: network address, 1: local vpp address
Matej Klotton0178d522016-11-04 11:11:44 +0100174 mac = "02:%02x:00:00:ff:%02x" % (self.sw_if_index, i)
175 ip4 = "172.16.%u.%u" % (self.sw_if_index, i)
Klement Sekera46a87ad2017-01-02 08:22:23 +0100176 ip6 = "fd01:%x::%x" % (self.sw_if_index, i)
Neale Ranns2a3ea492017-04-19 05:24:40 -0700177 ip6_ll = mk_ll_addr(mac)
178 host = Host(mac, ip4, ip6, ip6_ll)
Matej Klotton0178d522016-11-04 11:11:44 +0100179 self._remote_hosts.append(host)
180 self._hosts_by_mac[mac] = host
181 self._hosts_by_ip4[ip4] = host
182 self._hosts_by_ip6[ip6] = host
183
Matej Klottonc5bf07f2016-11-23 15:27:17 +0100184 @abstractmethod
185 def __init__(self, test):
186 self._test = test
187
188 self._remote_hosts = []
189 self._hosts_by_mac = {}
190 self._hosts_by_ip4 = {}
191 self._hosts_by_ip6 = {}
Matej Klotton0178d522016-11-04 11:11:44 +0100192
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700193 def set_mac(self, mac):
Ole Troan8006c6a2018-12-17 12:02:26 +0100194 self._local_mac = str(mac)
195 self._local_ip6_ll = mk_ll_addr(self._local_mac)
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700196 self.test.vapi.sw_interface_set_mac_address(
Ole Troan8006c6a2018-12-17 12:02:26 +0100197 self.sw_if_index, mac.packed)
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700198
Klement Sekera31da2e32018-06-24 22:49:55 +0200199 def set_sw_if_index(self, sw_if_index):
200 self._sw_if_index = sw_if_index
201
Matej Klotton0178d522016-11-04 11:11:44 +0100202 self.generate_remote_hosts()
Klement Sekeraf62ae122016-10-11 11:47:09 +0200203
204 self._local_ip4 = "172.16.%u.1" % self.sw_if_index
205 self._local_ip4n = socket.inet_pton(socket.AF_INET, self.local_ip4)
Neale Ranns24b170a2017-08-15 05:33:11 -0700206 self._local_ip4_subnet = "172.16.%u.0" % self.sw_if_index
207 self._local_ip4n_subnet = socket.inet_pton(socket.AF_INET,
208 self._local_ip4_subnet)
209 self._local_ip4_bcast = "172.16.%u.255" % self.sw_if_index
210 self._local_ip4n_bcast = socket.inet_pton(socket.AF_INET,
211 self._local_ip4_bcast)
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100212 self.local_ip4_prefix_len = 24
213 self.has_ip4_config = False
214 self.ip4_table_id = 0
Klement Sekeraf62ae122016-10-11 11:47:09 +0200215
Klement Sekera46a87ad2017-01-02 08:22:23 +0100216 self._local_ip6 = "fd01:%x::1" % self.sw_if_index
Klement Sekeraf62ae122016-10-11 11:47:09 +0200217 self._local_ip6n = socket.inet_pton(socket.AF_INET6, self.local_ip6)
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100218 self.local_ip6_prefix_len = 64
219 self.has_ip6_config = False
220 self.ip6_table_id = 0
Klement Sekeraf62ae122016-10-11 11:47:09 +0200221
Klement Sekera611864f2018-09-26 11:19:00 +0200222 self._local_addr = {socket.AF_INET: self.local_ip4,
223 socket.AF_INET6: self.local_ip6}
224 self._local_addr_n = {socket.AF_INET: self.local_ip4n,
225 socket.AF_INET6: self.local_ip6n}
226 self._remote_addr = {socket.AF_INET: self.remote_ip4,
227 socket.AF_INET6: self.remote_ip6}
228 self._remote_addr_n = {socket.AF_INET: self.remote_ip4n,
229 socket.AF_INET6: self.remote_ip6n}
230
Klement Sekeraf62ae122016-10-11 11:47:09 +0200231 r = self.test.vapi.sw_interface_dump()
232 for intf in r:
233 if intf.sw_if_index == self.sw_if_index:
Ole Troan7f991832018-12-06 17:35:12 +0100234 self._name = intf.interface_name.split(b'\0',
235 1)[0].decode('utf8')
Ole Troan8006c6a2018-12-17 12:02:26 +0100236 self._local_mac = mac_ntop(intf.l2_address)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200237 self._dump = intf
238 break
239 else:
240 raise Exception(
241 "Could not find interface with sw_if_index %d "
242 "in interface dump %s" %
Paul Vinciguerra6c746172018-11-26 09:57:21 -0800243 (self.sw_if_index, moves.reprlib.repr(r)))
Neale Ranns2a3ea492017-04-19 05:24:40 -0700244 self._local_ip6_ll = mk_ll_addr(self.local_mac)
245 self._local_ip6n_ll = socket.inet_pton(socket.AF_INET6,
246 self.local_ip6_ll)
Juraj Slobodac0374232018-02-01 15:18:49 +0100247 self._remote_ip6_ll = mk_ll_addr(self.remote_mac)
248 self._remote_ip6n_ll = socket.inet_pton(socket.AF_INET6,
249 self.remote_ip6_ll)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200250
Klement Sekeraf62ae122016-10-11 11:47:09 +0200251 def config_ip4(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100252 """Configure IPv4 address on the VPP interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200253 self.test.vapi.sw_interface_add_del_address(
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100254 self.sw_if_index, self.local_ip4n, self.local_ip4_prefix_len)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000255 self.has_ip4_config = True
256
257 def unconfig_ip4(self):
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100258 """Remove IPv4 address on the VPP interface."""
Neale Ranns177bbdc2016-11-15 09:46:51 +0000259 try:
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100260 if self.has_ip4_config:
Neale Ranns177bbdc2016-11-15 09:46:51 +0000261 self.test.vapi.sw_interface_add_del_address(
262 self.sw_if_index,
263 self.local_ip4n,
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100264 self.local_ip4_prefix_len,
265 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:
Ole Troan8006c6a2018-12-17 12:02:26 +0100276 macn = mac_pton(host.mac)
Matej Klotton0178d522016-11-04 11:11:44 +0100277 ipn = host.ip4n
Jane546d3b2016-12-08 13:10:03 +0100278 self.test.vapi.ip_neighbor_add_del(
Neale Rannsbaf2e902017-02-25 04:20:00 -0800279 self.sw_if_index, macn, ipn)
Matej Klotton0178d522016-11-04 11:11:44 +0100280
Klement Sekeraf62ae122016-10-11 11:47:09 +0200281 def config_ip6(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100282 """Configure IPv6 address on the VPP interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200283 self.test.vapi.sw_interface_add_del_address(
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100284 self.sw_if_index, self._local_ip6n, self.local_ip6_prefix_len,
285 is_ipv6=1)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000286 self.has_ip6_config = True
287
288 def unconfig_ip6(self):
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100289 """Remove IPv6 address on the VPP interface."""
Neale Ranns177bbdc2016-11-15 09:46:51 +0000290 try:
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100291 if self.has_ip6_config:
Neale Ranns177bbdc2016-11-15 09:46:51 +0000292 self.test.vapi.sw_interface_add_del_address(
293 self.sw_if_index,
294 self.local_ip6n,
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100295 self.local_ip6_prefix_len,
296 is_ipv6=1, is_add=0)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000297 except AttributeError:
298 self.has_ip6_config = False
299 self.has_ip6_config = False
300
Neale Rannsbaf2e902017-02-25 04:20:00 -0800301 def configure_ipv6_neighbors(self):
Jan Gelety057bb8c2016-12-20 17:32:45 +0100302 """For every remote host assign neighbor's MAC to IPv6 addresses.
303
304 :param vrf_id: The FIB table / VRF ID. (Default value = 0)
305 """
Klement Sekera46a87ad2017-01-02 08:22:23 +0100306 for host in self._remote_hosts:
Ole Troan8006c6a2018-12-17 12:02:26 +0100307 macn = mac_pton(host.mac)
Jan Gelety057bb8c2016-12-20 17:32:45 +0100308 ipn = host.ip6n
Klement Sekera46a87ad2017-01-02 08:22:23 +0100309 self.test.vapi.ip_neighbor_add_del(
Neale Rannsbaf2e902017-02-25 04:20:00 -0800310 self.sw_if_index, macn, ipn, is_ipv6=1)
Klement Sekera46a87ad2017-01-02 08:22:23 +0100311
Neale Ranns177bbdc2016-11-15 09:46:51 +0000312 def unconfig(self):
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100313 """Unconfigure IPv6 and IPv4 address on the VPP interface."""
Neale Ranns177bbdc2016-11-15 09:46:51 +0000314 self.unconfig_ip4()
315 self.unconfig_ip6()
Klement Sekeraf62ae122016-10-11 11:47:09 +0200316
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000317 def set_table_ip4(self, table_id):
318 """Set the interface in a IPv4 Table.
Matej Klotton86d87c42016-11-11 11:38:55 +0100319
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100320 .. note:: Must be called before configuring IP4 addresses.
321 """
322 self.ip4_table_id = table_id
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000323 self.test.vapi.sw_interface_set_table(
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100324 self.sw_if_index, 0, self.ip4_table_id)
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000325
326 def set_table_ip6(self, table_id):
327 """Set the interface in a IPv6 Table.
Matej Klotton86d87c42016-11-11 11:38:55 +0100328
329 .. note:: Must be called before configuring IP6 addresses.
330 """
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100331 self.ip6_table_id = table_id
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000332 self.test.vapi.sw_interface_set_table(
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100333 self.sw_if_index, 1, self.ip6_table_id)
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000334
Klement Sekeraf62ae122016-10-11 11:47:09 +0200335 def disable_ipv6_ra(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100336 """Configure IPv6 RA suppress on the VPP interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200337 self.test.vapi.sw_interface_ra_suppress(self.sw_if_index)
338
Neale Ranns32e1c012016-11-22 17:07:28 +0000339 def ip6_ra_config(self, no=0, suppress=0, send_unicast=0):
Neale Ranns75152282017-01-09 01:00:45 -0800340 """Configure IPv6 RA suppress on the VPP interface."""
341 self.test.vapi.ip6_sw_interface_ra_config(self.sw_if_index,
Neale Ranns32e1c012016-11-22 17:07:28 +0000342 no,
Neale Ranns75152282017-01-09 01:00:45 -0800343 suppress,
344 send_unicast)
345
Neale Ranns87df12d2017-02-18 08:16:41 -0800346 def ip6_ra_prefix(self, address, address_length, is_no=0,
347 off_link=0, no_autoconfig=0, use_default=0):
348 """Configure IPv6 RA suppress on the VPP interface."""
349 self.test.vapi.ip6_sw_interface_ra_prefix(self.sw_if_index,
350 address,
351 address_length,
352 is_no=is_no,
353 off_link=off_link,
354 no_autoconfig=no_autoconfig,
355 use_default=use_default)
356
Klement Sekeraf62ae122016-10-11 11:47:09 +0200357 def admin_up(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100358 """Put interface ADMIN-UP."""
Klement Sekerada505f62017-01-04 12:58:53 +0100359 self.test.vapi.sw_interface_set_flags(self.sw_if_index,
360 admin_up_down=1)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200361
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100362 def admin_down(self):
363 """Put interface ADMIN-down."""
Klement Sekerada505f62017-01-04 12:58:53 +0100364 self.test.vapi.sw_interface_set_flags(self.sw_if_index,
365 admin_up_down=0)
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100366
Neale Ranns75152282017-01-09 01:00:45 -0800367 def ip6_enable(self):
368 """IPv6 Enable interface"""
Klement Sekerada505f62017-01-04 12:58:53 +0100369 self.test.vapi.ip6_sw_interface_enable_disable(self.sw_if_index,
370 enable=1)
Neale Ranns75152282017-01-09 01:00:45 -0800371
372 def ip6_disable(self):
373 """Put interface ADMIN-DOWN."""
Klement Sekerada505f62017-01-04 12:58:53 +0100374 self.test.vapi.ip6_sw_interface_enable_disable(self.sw_if_index,
375 enable=0)
Neale Ranns75152282017-01-09 01:00:45 -0800376
Klement Sekeraf62ae122016-10-11 11:47:09 +0200377 def add_sub_if(self, sub_if):
Matej Klotton86d87c42016-11-11 11:38:55 +0100378 """Register a sub-interface with this interface.
Klement Sekeraf62ae122016-10-11 11:47:09 +0200379
380 :param sub_if: sub-interface
Klement Sekeraf62ae122016-10-11 11:47:09 +0200381 """
382 if not hasattr(self, 'sub_if'):
383 self.sub_if = sub_if
384 else:
385 if isinstance(self.sub_if, list):
386 self.sub_if.append(sub_if)
387 else:
388 self.sub_if = sub_if
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000389
390 def enable_mpls(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100391 """Enable MPLS on the VPP interface."""
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000392 self.test.vapi.sw_interface_enable_disable_mpls(
393 self.sw_if_index)
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100394
Neale Ranns180279b2017-03-16 15:49:09 -0400395 def disable_mpls(self):
396 """Enable MPLS on the VPP interface."""
397 self.test.vapi.sw_interface_enable_disable_mpls(
398 self.sw_if_index, 0)
399
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100400 def is_ip4_entry_in_fib_dump(self, dump):
401 for i in dump:
402 if i.address == self.local_ip4n and \
403 i.address_length == self.local_ip4_prefix_len and \
404 i.table_id == self.ip4_table_id:
405 return True
406 return False
Neale Ranns39f9d8b2017-02-16 21:57:05 -0800407
408 def set_unnumbered(self, ip_sw_if_index):
409 """ Set the interface to unnumbered via ip_sw_if_index """
410 self.test.vapi.sw_interface_set_unnumbered(
411 self.sw_if_index,
412 ip_sw_if_index)
413
Neale Ranns37be7362017-02-21 17:30:26 -0800414 def unset_unnumbered(self, ip_sw_if_index):
Neale Ranns4b919a52017-03-11 05:55:21 -0800415 """ Unset the interface to unnumbered via ip_sw_if_index """
Neale Ranns37be7362017-02-21 17:30:26 -0800416 self.test.vapi.sw_interface_set_unnumbered(
417 self.sw_if_index,
418 ip_sw_if_index,
419 is_add=0)
420
Neale Ranns39f9d8b2017-02-16 21:57:05 -0800421 def set_proxy_arp(self, enable=1):
422 """ Set the interface to enable/disable Proxy ARP """
423 self.test.vapi.proxy_arp_intfc_enable_disable(
424 self.sw_if_index,
425 enable)
Klement Sekera75e7d132017-09-20 08:26:30 +0200426
427 def query_vpp_config(self):
428 dump = self.test.vapi.sw_interface_dump()
429 return self.is_interface_config_in_dump(dump)
430
Juraj Slobodab3f90502018-10-04 15:15:16 +0200431 def get_interface_config_from_dump(self, dump):
Klement Sekera75e7d132017-09-20 08:26:30 +0200432 for i in dump:
433 if i.interface_name.rstrip(' \t\r\n\0') == self.name and \
434 i.sw_if_index == self.sw_if_index:
Juraj Slobodab3f90502018-10-04 15:15:16 +0200435 return i
Klement Sekera75e7d132017-09-20 08:26:30 +0200436 else:
Juraj Slobodab3f90502018-10-04 15:15:16 +0200437 return None
438
439 def is_interface_config_in_dump(self, dump):
440 return self.get_interface_config_from_dump(dump) is not None
441
442 def assert_interface_state(self, admin_up_down, link_up_down,
443 expect_event=False):
444 if expect_event:
445 event = self.test.vapi.wait_for_event(timeout=1,
446 name='sw_interface_event')
447 self.test.assert_equal(event.sw_if_index, self.sw_if_index,
448 "sw_if_index")
449 self.test.assert_equal(event.admin_up_down, admin_up_down,
450 "admin state")
451 self.test.assert_equal(event.link_up_down, link_up_down,
452 "link state")
453 dump = self.test.vapi.sw_interface_dump()
454 if_state = self.get_interface_config_from_dump(dump)
455 self.test.assert_equal(if_state.admin_up_down, admin_up_down,
456 "admin state")
457 self.test.assert_equal(if_state.link_up_down, link_up_down,
458 "link state")
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700459
460 def __str__(self):
461 return self.name