blob: 3434c2c1a5d77c9b724bf599cf7f1f9479f0f256 [file] [log] [blame]
Klement Sekeraf62ae122016-10-11 11:47:09 +02001import socket
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -08002import abc
Dave Wallace8800f732023-08-31 00:47:44 -04003import reprlib
Paul Vinciguerra00671cf2018-11-25 12:47:04 -08004
Ole Troan7f991832018-12-06 17:35:12 +01005from util import Host, mk_ll_addr
Dave Wallace8800f732023-08-31 00:47:44 -04006from vpp_papi import VppEnum
7from ipaddress import IPv4Network
Neale Ranns097fa662018-05-01 05:17:55 -07008
9try:
10 text_type = unicode
11except NameError:
12 text_type = str
Jan65209ed2016-12-05 23:29:17 +010013
Klement Sekeraf62ae122016-10-11 11:47:09 +020014
Paul Vinciguerra090096b2020-12-03 00:42:46 -050015class VppInterface(metaclass=abc.ABCMeta):
Matej Klotton86d87c42016-11-11 11:38:55 +010016 """Generic VPP interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +020017
18 @property
19 def sw_if_index(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010020 """Interface index assigned by VPP."""
Klement Sekeraf62ae122016-10-11 11:47:09 +020021 return self._sw_if_index
22
23 @property
24 def remote_mac(self):
Klement Sekerada505f62017-01-04 12:58:53 +010025 """MAC-address of the remote interface "connected" to this interface"""
Matej Klotton0178d522016-11-04 11:11:44 +010026 return self._remote_hosts[0].mac
Klement Sekeraf62ae122016-10-11 11:47:09 +020027
28 @property
29 def local_mac(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010030 """MAC-address of the VPP interface."""
Ole Troan6ed154f2019-10-15 19:31:55 +020031 return str(self._local_mac)
Klement Sekeraf62ae122016-10-11 11:47:09 +020032
33 @property
Klement Sekera611864f2018-09-26 11:19:00 +020034 def local_addr(self):
35 return self._local_addr
36
37 @property
38 def remote_addr(self):
39 return self._remote_addr
40
41 @property
Klement Sekeraf62ae122016-10-11 11:47:09 +020042 def local_ip4(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010043 """Local IPv4 address on VPP interface (string)."""
Neale Rannsefd7bc22019-11-11 08:32:34 +000044 return self._local_ip4
Jakub Grajciar053204a2019-03-18 13:17:53 +010045
46 @local_ip4.setter
47 def local_ip4(self, value):
Neale Rannsefd7bc22019-11-11 08:32:34 +000048 self._local_ip4 = value
Jakub Grajciar053204a2019-03-18 13:17:53 +010049
50 @property
51 def local_ip4_prefix_len(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020052 """Local IPv4 prefix length"""
Neale Rannsefd7bc22019-11-11 08:32:34 +000053 return self._local_ip4_len
Jakub Grajciar053204a2019-03-18 13:17:53 +010054
55 @local_ip4_prefix_len.setter
56 def local_ip4_prefix_len(self, value):
Neale Rannsefd7bc22019-11-11 08:32:34 +000057 self._local_ip4_len = value
Jakub Grajciar053204a2019-03-18 13:17:53 +010058
59 @property
60 def local_ip4_prefix(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020061 """Local IPv4 prefix"""
62 return "%s/%d" % (self._local_ip4, self._local_ip4_len)
Klement Sekeraf62ae122016-10-11 11:47:09 +020063
64 @property
Klement Sekeraf62ae122016-10-11 11:47:09 +020065 def remote_ip4(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010066 """IPv4 address of remote peer "connected" to this interface."""
Matej Klotton0178d522016-11-04 11:11:44 +010067 return self._remote_hosts[0].ip4
Klement Sekeraf62ae122016-10-11 11:47:09 +020068
69 @property
Klement Sekeraf62ae122016-10-11 11:47:09 +020070 def local_ip6(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010071 """Local IPv6 address on VPP interface (string)."""
Neale Rannsefd7bc22019-11-11 08:32:34 +000072 return self._local_ip6
Jakub Grajciar053204a2019-03-18 13:17:53 +010073
74 @local_ip6.setter
75 def local_ip6(self, value):
Paul Vinciguerra5d440d92019-12-01 21:55:52 -050076 self._local_ip6 = value
Jakub Grajciar053204a2019-03-18 13:17:53 +010077
78 @property
79 def local_ip6_prefix_len(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020080 """Local IPv6 prefix length"""
Neale Rannsefd7bc22019-11-11 08:32:34 +000081 return self._local_ip6_len
Jakub Grajciar053204a2019-03-18 13:17:53 +010082
83 @local_ip6_prefix_len.setter
84 def local_ip6_prefix_len(self, value):
Neale Rannsefd7bc22019-11-11 08:32:34 +000085 self._local_ip6_len = value
Jakub Grajciar053204a2019-03-18 13:17:53 +010086
87 @property
88 def local_ip6_prefix(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020089 """Local IPv4 prefix"""
90 return "%s/%d" % (self._local_ip6, self._local_ip6_len)
Klement Sekeraf62ae122016-10-11 11:47:09 +020091
92 @property
Klement Sekeraf62ae122016-10-11 11:47:09 +020093 def remote_ip6(self):
Matej Klotton86d87c42016-11-11 11:38:55 +010094 """IPv6 address of remote peer "connected" to this interface."""
Matej Klotton0178d522016-11-04 11:11:44 +010095 return self._remote_hosts[0].ip6
Klement Sekeraf62ae122016-10-11 11:47:09 +020096
97 @property
Juraj Slobodac0374232018-02-01 15:18:49 +010098 def local_ip6_ll(self):
Klement Sekera611864f2018-09-26 11:19:00 +020099 """Local IPv6 link-local address on VPP interface (string)."""
BenoƮt Ganne58a19152021-01-18 19:24:34 +0100100 if not self._local_ip6_ll:
101 self._local_ip6_ll = str(
102 self.test.vapi.sw_interface_ip6_get_link_local_address(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200103 self.sw_if_index
104 ).ip
105 )
Neale Rannsefd7bc22019-11-11 08:32:34 +0000106 return self._local_ip6_ll
Juraj Slobodac0374232018-02-01 15:18:49 +0100107
108 @property
Juraj Slobodac0374232018-02-01 15:18:49 +0100109 def remote_ip6_ll(self):
110 """Link-local IPv6 address of remote peer
111 "connected" to this interface."""
112 return self._remote_ip6_ll
113
114 @property
Klement Sekeraf62ae122016-10-11 11:47:09 +0200115 def name(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100116 """Name of the interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200117 return self._name
118
119 @property
120 def dump(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100121 """RAW result of sw_interface_dump for this interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200122 return self._dump
123
124 @property
125 def test(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100126 """Test case creating this interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200127 return self._test
128
Matej Klotton0178d522016-11-04 11:11:44 +0100129 @property
130 def remote_hosts(self):
131 """Remote hosts list"""
132 return self._remote_hosts
133
134 @remote_hosts.setter
135 def remote_hosts(self, value):
Matej Klotton86d87c42016-11-11 11:38:55 +0100136 """
137 :param list value: List of remote hosts.
138 """
Matej Klotton0178d522016-11-04 11:11:44 +0100139 self._remote_hosts = value
Matej Klotton86d87c42016-11-11 11:38:55 +0100140 self._hosts_by_mac = {}
141 self._hosts_by_ip4 = {}
142 self._hosts_by_ip6 = {}
143 for host in self._remote_hosts:
144 self._hosts_by_mac[host.mac] = host
145 self._hosts_by_ip4[host.ip4] = host
146 self._hosts_by_ip6[host.ip6] = host
Matej Klotton0178d522016-11-04 11:11:44 +0100147
148 def host_by_mac(self, mac):
Matej Klotton86d87c42016-11-11 11:38:55 +0100149 """
Matej Klottonc5bf07f2016-11-23 15:27:17 +0100150 :param mac: MAC address to find host by.
Matej Klotton86d87c42016-11-11 11:38:55 +0100151 :return: Host object assigned to interface.
152 """
Matej Klotton0178d522016-11-04 11:11:44 +0100153 return self._hosts_by_mac[mac]
154
155 def host_by_ip4(self, ip):
Matej Klotton86d87c42016-11-11 11:38:55 +0100156 """
157 :param ip: IPv4 address to find host by.
158 :return: Host object assigned to interface.
159 """
Matej Klotton0178d522016-11-04 11:11:44 +0100160 return self._hosts_by_ip4[ip]
161
162 def host_by_ip6(self, ip):
Matej Klotton86d87c42016-11-11 11:38:55 +0100163 """
164 :param ip: IPv6 address to find host by.
165 :return: Host object assigned to interface.
166 """
Matej Klotton0178d522016-11-04 11:11:44 +0100167 return self._hosts_by_ip6[ip]
168
169 def generate_remote_hosts(self, count=1):
Matej Klotton86d87c42016-11-11 11:38:55 +0100170 """Generate and add remote hosts for the interface.
171
172 :param int count: Number of generated remote hosts.
173 """
Matej Klotton0178d522016-11-04 11:11:44 +0100174 self._remote_hosts = []
175 self._hosts_by_mac = {}
176 self._hosts_by_ip4 = {}
177 self._hosts_by_ip6 = {}
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200178 for i in range(2, count + 2): # 0: network address, 1: local vpp address
Matej Klotton0178d522016-11-04 11:11:44 +0100179 mac = "02:%02x:00:00:ff:%02x" % (self.sw_if_index, i)
180 ip4 = "172.16.%u.%u" % (self.sw_if_index, i)
Klement Sekera46a87ad2017-01-02 08:22:23 +0100181 ip6 = "fd01:%x::%x" % (self.sw_if_index, i)
Neale Ranns2a3ea492017-04-19 05:24:40 -0700182 ip6_ll = mk_ll_addr(mac)
183 host = Host(mac, ip4, ip6, ip6_ll)
Matej Klotton0178d522016-11-04 11:11:44 +0100184 self._remote_hosts.append(host)
185 self._hosts_by_mac[mac] = host
186 self._hosts_by_ip4[ip4] = host
187 self._hosts_by_ip6[ip6] = host
188
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -0800189 @abc.abstractmethod
Matej Klottonc5bf07f2016-11-23 15:27:17 +0100190 def __init__(self, test):
191 self._test = test
192
193 self._remote_hosts = []
194 self._hosts_by_mac = {}
195 self._hosts_by_ip4 = {}
196 self._hosts_by_ip6 = {}
Matej Klotton0178d522016-11-04 11:11:44 +0100197
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700198 def set_mac(self, mac):
Ole Troan8006c6a2018-12-17 12:02:26 +0100199 self._local_mac = str(mac)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200200 self.test.vapi.sw_interface_set_mac_address(self.sw_if_index, mac.packed)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500201 return self
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700202
Klement Sekera31da2e32018-06-24 22:49:55 +0200203 def set_sw_if_index(self, sw_if_index):
Paul Vinciguerra5ef9ca62019-12-01 22:16:54 -0500204 if sw_if_index > 255:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200205 raise RuntimeError("Don't support sw_if_index values greater than 255.")
Klement Sekera31da2e32018-06-24 22:49:55 +0200206 self._sw_if_index = sw_if_index
207
Matej Klotton0178d522016-11-04 11:11:44 +0100208 self.generate_remote_hosts()
Klement Sekeraf62ae122016-10-11 11:47:09 +0200209
Neale Rannsefd7bc22019-11-11 08:32:34 +0000210 self._local_ip4 = "172.16.%u.1" % self.sw_if_index
211 self._local_ip4_len = 24
Neale Ranns24b170a2017-08-15 05:33:11 -0700212 self._local_ip4_subnet = "172.16.%u.0" % self.sw_if_index
Neale Ranns24b170a2017-08-15 05:33:11 -0700213 self._local_ip4_bcast = "172.16.%u.255" % self.sw_if_index
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100214 self.has_ip4_config = False
215 self.ip4_table_id = 0
Klement Sekeraf62ae122016-10-11 11:47:09 +0200216
Neale Rannsefd7bc22019-11-11 08:32:34 +0000217 self._local_ip6 = "fd01:%x::1" % self.sw_if_index
218 self._local_ip6_len = 64
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100219 self.has_ip6_config = False
220 self.ip6_table_id = 0
Klement Sekeraf62ae122016-10-11 11:47:09 +0200221
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200222 self._local_addr = {
223 socket.AF_INET: self.local_ip4,
224 socket.AF_INET6: self.local_ip6,
225 }
226 self._remote_addr = {
227 socket.AF_INET: self.remote_ip4,
228 socket.AF_INET6: self.remote_ip6,
229 }
Klement Sekera611864f2018-09-26 11:19:00 +0200230
Paul Vinciguerra7a998232019-06-07 15:01:12 -0400231 r = self.test.vapi.sw_interface_dump(sw_if_index=self.sw_if_index)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200232 for intf in r:
233 if intf.sw_if_index == self.sw_if_index:
Ole Troan6ed154f2019-10-15 19:31:55 +0200234 self._name = intf.interface_name
235 self._local_mac = intf.l2_address
Klement Sekeraf62ae122016-10-11 11:47:09 +0200236 self._dump = intf
237 break
238 else:
239 raise Exception(
240 "Could not find interface with sw_if_index %d "
Dave Wallace8800f732023-08-31 00:47:44 -0400241 "in interface dump %s" % (self.sw_if_index, reprlib.repr(r))
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200242 )
Juraj Slobodac0374232018-02-01 15:18:49 +0100243 self._remote_ip6_ll = mk_ll_addr(self.remote_mac)
BenoƮt Ganne58a19152021-01-18 19:24:34 +0100244 self._local_ip6_ll = None
Klement Sekeraf62ae122016-10-11 11:47:09 +0200245
Klement Sekeraf62ae122016-10-11 11:47:09 +0200246 def config_ip4(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100247 """Configure IPv4 address on the VPP interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200248 self.test.vapi.sw_interface_add_del_address(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200249 sw_if_index=self.sw_if_index, prefix=self.local_ip4_prefix
250 )
Neale Ranns177bbdc2016-11-15 09:46:51 +0000251 self.has_ip4_config = True
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500252 return self
Neale Ranns177bbdc2016-11-15 09:46:51 +0000253
254 def unconfig_ip4(self):
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100255 """Remove IPv4 address on the VPP interface."""
Neale Ranns177bbdc2016-11-15 09:46:51 +0000256 try:
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100257 if self.has_ip4_config:
Neale Ranns177bbdc2016-11-15 09:46:51 +0000258 self.test.vapi.sw_interface_add_del_address(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200259 sw_if_index=self.sw_if_index, prefix=self.local_ip4_prefix, is_add=0
260 )
Neale Ranns177bbdc2016-11-15 09:46:51 +0000261 except AttributeError:
262 self.has_ip4_config = False
263 self.has_ip4_config = False
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500264 return self
Klement Sekeraf62ae122016-10-11 11:47:09 +0200265
Neale Rannsbaf2e902017-02-25 04:20:00 -0800266 def configure_ipv4_neighbors(self):
Jane546d3b2016-12-08 13:10:03 +0100267 """For every remote host assign neighbor's MAC to IPv4 addresses.
268
269 :param vrf_id: The FIB table / VRF ID. (Default value = 0)
270 """
Matej Klotton0178d522016-11-04 11:11:44 +0100271 for host in self._remote_hosts:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200272 self.test.vapi.ip_neighbor_add_del(self.sw_if_index, host.mac, host.ip4)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500273 return self
Matej Klotton0178d522016-11-04 11:11:44 +0100274
Klement Sekeraf62ae122016-10-11 11:47:09 +0200275 def config_ip6(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100276 """Configure IPv6 address on the VPP interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200277 self.test.vapi.sw_interface_add_del_address(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200278 sw_if_index=self.sw_if_index, prefix=self.local_ip6_prefix
279 )
Neale Ranns177bbdc2016-11-15 09:46:51 +0000280 self.has_ip6_config = True
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500281 return self
Neale Ranns177bbdc2016-11-15 09:46:51 +0000282
283 def unconfig_ip6(self):
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100284 """Remove IPv6 address on the VPP interface."""
Neale Ranns177bbdc2016-11-15 09:46:51 +0000285 try:
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100286 if self.has_ip6_config:
Neale Ranns177bbdc2016-11-15 09:46:51 +0000287 self.test.vapi.sw_interface_add_del_address(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200288 sw_if_index=self.sw_if_index, prefix=self.local_ip6_prefix, is_add=0
289 )
Neale Ranns177bbdc2016-11-15 09:46:51 +0000290 except AttributeError:
291 self.has_ip6_config = False
292 self.has_ip6_config = False
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500293 return self
Neale Ranns177bbdc2016-11-15 09:46:51 +0000294
Neale Rannsbaf2e902017-02-25 04:20:00 -0800295 def configure_ipv6_neighbors(self):
Jan Gelety057bb8c2016-12-20 17:32:45 +0100296 """For every remote host assign neighbor's MAC to IPv6 addresses.
297
298 :param vrf_id: The FIB table / VRF ID. (Default value = 0)
299 """
Klement Sekera46a87ad2017-01-02 08:22:23 +0100300 for host in self._remote_hosts:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200301 self.test.vapi.ip_neighbor_add_del(self.sw_if_index, host.mac, host.ip6)
Klement Sekera46a87ad2017-01-02 08:22:23 +0100302
Neale Ranns177bbdc2016-11-15 09:46:51 +0000303 def unconfig(self):
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100304 """Unconfigure IPv6 and IPv4 address on the VPP interface."""
Neale Ranns177bbdc2016-11-15 09:46:51 +0000305 self.unconfig_ip4()
306 self.unconfig_ip6()
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500307 return self
Klement Sekeraf62ae122016-10-11 11:47:09 +0200308
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000309 def set_table_ip4(self, table_id):
310 """Set the interface in a IPv4 Table.
Matej Klotton86d87c42016-11-11 11:38:55 +0100311
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100312 .. note:: Must be called before configuring IP4 addresses.
313 """
314 self.ip4_table_id = table_id
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200315 self.test.vapi.sw_interface_set_table(self.sw_if_index, 0, self.ip4_table_id)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500316 return self
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000317
318 def set_table_ip6(self, table_id):
319 """Set the interface in a IPv6 Table.
Matej Klotton86d87c42016-11-11 11:38:55 +0100320
321 .. note:: Must be called before configuring IP6 addresses.
322 """
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100323 self.ip6_table_id = table_id
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200324 self.test.vapi.sw_interface_set_table(self.sw_if_index, 1, self.ip6_table_id)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500325 return self
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000326
Klement Sekeraf62ae122016-10-11 11:47:09 +0200327 def disable_ipv6_ra(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100328 """Configure IPv6 RA suppress on the VPP interface."""
Ole Troane1ade682019-03-04 23:55:43 +0100329 self.test.vapi.sw_interface_ip6nd_ra_config(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200330 sw_if_index=self.sw_if_index, suppress=1
331 )
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500332 return self
Klement Sekeraf62ae122016-10-11 11:47:09 +0200333
Neale Ranns32e1c012016-11-22 17:07:28 +0000334 def ip6_ra_config(self, no=0, suppress=0, send_unicast=0):
Neale Ranns75152282017-01-09 01:00:45 -0800335 """Configure IPv6 RA suppress on the VPP interface."""
Ole Troane1ade682019-03-04 23:55:43 +0100336 self.test.vapi.sw_interface_ip6nd_ra_config(
337 sw_if_index=self.sw_if_index,
338 is_no=no,
339 suppress=suppress,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200340 send_unicast=send_unicast,
341 )
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500342 return self
Neale Ranns75152282017-01-09 01:00:45 -0800343
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200344 def ip6_ra_prefix(
345 self, prefix, is_no=0, off_link=0, no_autoconfig=0, use_default=0
346 ):
Paul Vinciguerraab055082019-06-06 14:07:55 -0400347 """Configure IPv6 RA suppress on the VPP interface.
348
349 prefix can be a string in the format of '<address>/<length_in_bits>'
350 or ipaddress.ipnetwork object (if strict.)"""
351
Ole Troane1ade682019-03-04 23:55:43 +0100352 self.test.vapi.sw_interface_ip6nd_ra_prefix(
Ole Troana5b2eec2019-03-11 19:23:25 +0100353 sw_if_index=self.sw_if_index,
Paul Vinciguerraab055082019-06-06 14:07:55 -0400354 prefix=prefix,
Ole Troana5b2eec2019-03-11 19:23:25 +0100355 use_default=use_default,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200356 off_link=off_link,
357 no_autoconfig=no_autoconfig,
358 is_no=is_no,
359 )
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500360 return self
Neale Ranns87df12d2017-02-18 08:16:41 -0800361
Klement Sekeraf62ae122016-10-11 11:47:09 +0200362 def admin_up(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100363 """Put interface ADMIN-UP."""
Jakub Grajciar053204a2019-03-18 13:17:53 +0100364 self.test.vapi.sw_interface_set_flags(
365 self.sw_if_index,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200366 flags=VppEnum.vl_api_if_status_flags_t.IF_STATUS_API_FLAG_ADMIN_UP,
367 )
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500368 return self
Klement Sekeraf62ae122016-10-11 11:47:09 +0200369
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100370 def admin_down(self):
371 """Put interface ADMIN-down."""
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200372 self.test.vapi.sw_interface_set_flags(self.sw_if_index, flags=0)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500373 return self
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100374
Neale Ranns63480742019-03-13 06:41:52 -0700375 def link_up(self):
376 """Put interface link-state-UP."""
377 self.test.vapi.cli("test interface link-state %s up" % self.name)
378
379 def link_down(self):
380 """Put interface link-state-down."""
381 self.test.vapi.cli("test interface link-state %s down" % self.name)
382
Neale Ranns75152282017-01-09 01:00:45 -0800383 def ip6_enable(self):
384 """IPv6 Enable interface"""
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200385 self.test.vapi.sw_interface_ip6_enable_disable(self.sw_if_index, enable=1)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500386 return self
Neale Ranns75152282017-01-09 01:00:45 -0800387
388 def ip6_disable(self):
389 """Put interface ADMIN-DOWN."""
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200390 self.test.vapi.sw_interface_ip6_enable_disable(self.sw_if_index, enable=0)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500391 return self
Neale Ranns75152282017-01-09 01:00:45 -0800392
Klement Sekeraf62ae122016-10-11 11:47:09 +0200393 def add_sub_if(self, sub_if):
Matej Klotton86d87c42016-11-11 11:38:55 +0100394 """Register a sub-interface with this interface.
Klement Sekeraf62ae122016-10-11 11:47:09 +0200395
396 :param sub_if: sub-interface
Klement Sekeraf62ae122016-10-11 11:47:09 +0200397 """
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200398 if not hasattr(self, "sub_if"):
Klement Sekeraf62ae122016-10-11 11:47:09 +0200399 self.sub_if = sub_if
400 else:
401 if isinstance(self.sub_if, list):
402 self.sub_if.append(sub_if)
403 else:
404 self.sub_if = sub_if
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500405 return self
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000406
407 def enable_mpls(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100408 """Enable MPLS on the VPP interface."""
Ole Troan9a475372019-03-05 16:58:24 +0100409 self.test.vapi.sw_interface_set_mpls_enable(self.sw_if_index)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500410 return self
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100411
Neale Ranns180279b2017-03-16 15:49:09 -0400412 def disable_mpls(self):
413 """Enable MPLS on the VPP interface."""
Ole Troan9a475372019-03-05 16:58:24 +0100414 self.test.vapi.sw_interface_set_mpls_enable(self.sw_if_index, 0)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500415 return self
Neale Ranns180279b2017-03-16 15:49:09 -0400416
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100417 def is_ip4_entry_in_fib_dump(self, dump):
418 for i in dump:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200419 n = IPv4Network(
420 text_type("%s/%d" % (self.local_ip4, self.local_ip4_prefix_len))
421 )
422 if i.route.prefix == n and i.route.table_id == self.ip4_table_id:
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100423 return True
424 return False
Neale Ranns39f9d8b2017-02-16 21:57:05 -0800425
426 def set_unnumbered(self, ip_sw_if_index):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200427 """Set the interface to unnumbered via ip_sw_if_index"""
428 self.test.vapi.sw_interface_set_unnumbered(ip_sw_if_index, self.sw_if_index)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500429 return self
Neale Ranns39f9d8b2017-02-16 21:57:05 -0800430
Neale Ranns37be7362017-02-21 17:30:26 -0800431 def unset_unnumbered(self, ip_sw_if_index):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200432 """Unset the interface to unnumbered via ip_sw_if_index"""
433 self.test.vapi.sw_interface_set_unnumbered(
434 ip_sw_if_index, self.sw_if_index, is_add=0
435 )
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500436 return self
Neale Ranns37be7362017-02-21 17:30:26 -0800437
Neale Ranns39f9d8b2017-02-16 21:57:05 -0800438 def set_proxy_arp(self, enable=1):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200439 """Set the interface to enable/disable Proxy ARP"""
Neale Ranns39f9d8b2017-02-16 21:57:05 -0800440 self.test.vapi.proxy_arp_intfc_enable_disable(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200441 sw_if_index=self.sw_if_index, enable=enable
442 )
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500443 return self
Klement Sekera75e7d132017-09-20 08:26:30 +0200444
445 def query_vpp_config(self):
Paul Vinciguerra7a998232019-06-07 15:01:12 -0400446 dump = self.test.vapi.sw_interface_dump(sw_if_index=self.sw_if_index)
Klement Sekera75e7d132017-09-20 08:26:30 +0200447 return self.is_interface_config_in_dump(dump)
448
Juraj Slobodab3f90502018-10-04 15:15:16 +0200449 def get_interface_config_from_dump(self, dump):
Klement Sekera75e7d132017-09-20 08:26:30 +0200450 for i in dump:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200451 if (
452 i.interface_name.rstrip(" \t\r\n\0") == self.name
453 and i.sw_if_index == self.sw_if_index
454 ):
Juraj Slobodab3f90502018-10-04 15:15:16 +0200455 return i
Klement Sekera75e7d132017-09-20 08:26:30 +0200456 else:
Juraj Slobodab3f90502018-10-04 15:15:16 +0200457 return None
458
459 def is_interface_config_in_dump(self, dump):
460 return self.get_interface_config_from_dump(dump) is not None
461
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200462 def assert_interface_state(self, admin_up_down, link_up_down, expect_event=False):
Juraj Slobodab3f90502018-10-04 15:15:16 +0200463 if expect_event:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200464 event = self.test.vapi.wait_for_event(timeout=1, name="sw_interface_event")
465 self.test.assert_equal(event.sw_if_index, self.sw_if_index, "sw_if_index")
466 self.test.assert_equal((event.flags & 1), admin_up_down, "admin state")
467 self.test.assert_equal((event.flags & 2), link_up_down, "link state")
Juraj Slobodab3f90502018-10-04 15:15:16 +0200468 dump = self.test.vapi.sw_interface_dump()
469 if_state = self.get_interface_config_from_dump(dump)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200470 self.test.assert_equal((if_state.flags & 1), admin_up_down, "admin state")
471 self.test.assert_equal((if_state.flags & 2), link_up_down, "link state")
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700472
473 def __str__(self):
474 return self.name
Neale Ranns2ac885c2019-03-20 18:24:43 +0000475
476 def get_rx_stats(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200477 return self.test.statistics["/if/rx"][:, self.sw_if_index].sum_packets()
Neale Ranns2ac885c2019-03-20 18:24:43 +0000478
479 def get_tx_stats(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200480 return self.test.statistics["/if/tx"][:, self.sw_if_index].sum_packets()
Ole Troaneb284a12019-10-09 13:33:19 +0200481
482 def set_l3_mtu(self, mtu):
483 self.test.vapi.sw_interface_set_mtu(self.sw_if_index, [mtu, 0, 0, 0])
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500484 return self
Ole Troaneb284a12019-10-09 13:33:19 +0200485
486 def set_ip4_mtu(self, mtu):
487 self.test.vapi.sw_interface_set_mtu(self.sw_if_index, [0, mtu, 0, 0])
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500488 return self
Ole Troaneb284a12019-10-09 13:33:19 +0200489
490 def set_ip6_mtu(self, mtu):
491 self.test.vapi.sw_interface_set_mtu(self.sw_if_index, [0, 0, mtu, 0])
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500492 return self
Ole Troaneb284a12019-10-09 13:33:19 +0200493
494 def set_mpls_mtu(self, mtu):
495 self.test.vapi.sw_interface_set_mtu(self.sw_if_index, [0, 0, 0, mtu])
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500496 return self