blob: 32d8166fc43beb987c308d509beb6440dc3e217a [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
Ole Troan7f991832018-12-06 17:35:12 +01005from util import Host, mk_ll_addr
Jakub Grajciar053204a2019-03-18 13:17:53 +01006from vpp_papi import mac_ntop, VppEnum
Neale Rannsefd7bc22019-11-11 08:32:34 +00007from ipaddress import IPv4Network, IPv6Network
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):
52 """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):
61 """Local IPv4 prefix """
Neale Rannsefd7bc22019-11-11 08:32:34 +000062 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):
80 """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):
Neale Rannsefd7bc22019-11-11 08:32:34 +000089 """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)."""
Neale Rannsefd7bc22019-11-11 08:32:34 +0000100 return self._local_ip6_ll
Juraj Slobodac0374232018-02-01 15:18:49 +0100101
102 @property
Juraj Slobodac0374232018-02-01 15:18:49 +0100103 def remote_ip6_ll(self):
104 """Link-local IPv6 address of remote peer
105 "connected" to this interface."""
106 return self._remote_ip6_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
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -0800184 @abc.abstractmethod
Matej Klottonc5bf07f2016-11-23 15:27:17 +0100185 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)
Neale Rannsefd7bc22019-11-11 08:32:34 +0000195 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)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500198 return self
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):
Paul Vinciguerra5ef9ca62019-12-01 22:16:54 -0500201 if sw_if_index > 255:
202 raise RuntimeError("Don't support sw_if_index values "
203 "greater than 255.")
Klement Sekera31da2e32018-06-24 22:49:55 +0200204 self._sw_if_index = sw_if_index
205
Matej Klotton0178d522016-11-04 11:11:44 +0100206 self.generate_remote_hosts()
Klement Sekeraf62ae122016-10-11 11:47:09 +0200207
Neale Rannsefd7bc22019-11-11 08:32:34 +0000208 self._local_ip4 = "172.16.%u.1" % self.sw_if_index
209 self._local_ip4_len = 24
Neale Ranns24b170a2017-08-15 05:33:11 -0700210 self._local_ip4_subnet = "172.16.%u.0" % self.sw_if_index
Neale Ranns24b170a2017-08-15 05:33:11 -0700211 self._local_ip4_bcast = "172.16.%u.255" % self.sw_if_index
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100212 self.has_ip4_config = False
213 self.ip4_table_id = 0
Klement Sekeraf62ae122016-10-11 11:47:09 +0200214
Neale Rannsefd7bc22019-11-11 08:32:34 +0000215 self._local_ip6 = "fd01:%x::1" % self.sw_if_index
216 self._local_ip6_len = 64
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100217 self.has_ip6_config = False
218 self.ip6_table_id = 0
Klement Sekeraf62ae122016-10-11 11:47:09 +0200219
Klement Sekera611864f2018-09-26 11:19:00 +0200220 self._local_addr = {socket.AF_INET: self.local_ip4,
221 socket.AF_INET6: self.local_ip6}
Klement Sekera611864f2018-09-26 11:19:00 +0200222 self._remote_addr = {socket.AF_INET: self.remote_ip4,
223 socket.AF_INET6: self.remote_ip6}
Klement Sekera611864f2018-09-26 11:19:00 +0200224
Paul Vinciguerra7a998232019-06-07 15:01:12 -0400225 r = self.test.vapi.sw_interface_dump(sw_if_index=self.sw_if_index)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200226 for intf in r:
227 if intf.sw_if_index == self.sw_if_index:
Ole Troan6ed154f2019-10-15 19:31:55 +0200228 self._name = intf.interface_name
229 self._local_mac = intf.l2_address
Klement Sekeraf62ae122016-10-11 11:47:09 +0200230 self._dump = intf
231 break
232 else:
233 raise Exception(
234 "Could not find interface with sw_if_index %d "
235 "in interface dump %s" %
Paul Vinciguerra6c746172018-11-26 09:57:21 -0800236 (self.sw_if_index, moves.reprlib.repr(r)))
Neale Rannsefd7bc22019-11-11 08:32:34 +0000237 self._local_ip6_ll = mk_ll_addr(self.local_mac)
Juraj Slobodac0374232018-02-01 15:18:49 +0100238 self._remote_ip6_ll = mk_ll_addr(self.remote_mac)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200239
Klement Sekeraf62ae122016-10-11 11:47:09 +0200240 def config_ip4(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100241 """Configure IPv4 address on the VPP interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200242 self.test.vapi.sw_interface_add_del_address(
Neale Rannsefd7bc22019-11-11 08:32:34 +0000243 sw_if_index=self.sw_if_index, prefix=self.local_ip4_prefix)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000244 self.has_ip4_config = True
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500245 return self
Neale Ranns177bbdc2016-11-15 09:46:51 +0000246
247 def unconfig_ip4(self):
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100248 """Remove IPv4 address on the VPP interface."""
Neale Ranns177bbdc2016-11-15 09:46:51 +0000249 try:
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100250 if self.has_ip4_config:
Neale Ranns177bbdc2016-11-15 09:46:51 +0000251 self.test.vapi.sw_interface_add_del_address(
Jakub Grajciar053204a2019-03-18 13:17:53 +0100252 sw_if_index=self.sw_if_index,
Neale Rannsefd7bc22019-11-11 08:32:34 +0000253 prefix=self.local_ip4_prefix, is_add=0)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000254 except AttributeError:
255 self.has_ip4_config = False
256 self.has_ip4_config = False
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500257 return self
Klement Sekeraf62ae122016-10-11 11:47:09 +0200258
Neale Rannsbaf2e902017-02-25 04:20:00 -0800259 def configure_ipv4_neighbors(self):
Jane546d3b2016-12-08 13:10:03 +0100260 """For every remote host assign neighbor's MAC to IPv4 addresses.
261
262 :param vrf_id: The FIB table / VRF ID. (Default value = 0)
263 """
Matej Klotton0178d522016-11-04 11:11:44 +0100264 for host in self._remote_hosts:
Neale Ranns37029302018-08-10 05:30:06 -0700265 self.test.vapi.ip_neighbor_add_del(self.sw_if_index,
266 host.mac,
267 host.ip4)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500268 return self
Matej Klotton0178d522016-11-04 11:11:44 +0100269
Klement Sekeraf62ae122016-10-11 11:47:09 +0200270 def config_ip6(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100271 """Configure IPv6 address on the VPP interface."""
Klement Sekeraf62ae122016-10-11 11:47:09 +0200272 self.test.vapi.sw_interface_add_del_address(
Neale Rannsefd7bc22019-11-11 08:32:34 +0000273 sw_if_index=self.sw_if_index, prefix=self.local_ip6_prefix)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000274 self.has_ip6_config = True
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500275 return self
Neale Ranns177bbdc2016-11-15 09:46:51 +0000276
277 def unconfig_ip6(self):
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100278 """Remove IPv6 address on the VPP interface."""
Neale Ranns177bbdc2016-11-15 09:46:51 +0000279 try:
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100280 if self.has_ip6_config:
Neale Ranns177bbdc2016-11-15 09:46:51 +0000281 self.test.vapi.sw_interface_add_del_address(
Jakub Grajciar053204a2019-03-18 13:17:53 +0100282 sw_if_index=self.sw_if_index,
Neale Rannsefd7bc22019-11-11 08:32:34 +0000283 prefix=self.local_ip6_prefix, is_add=0)
Neale Ranns177bbdc2016-11-15 09:46:51 +0000284 except AttributeError:
285 self.has_ip6_config = False
286 self.has_ip6_config = False
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500287 return self
Neale Ranns177bbdc2016-11-15 09:46:51 +0000288
Neale Rannsbaf2e902017-02-25 04:20:00 -0800289 def configure_ipv6_neighbors(self):
Jan Gelety057bb8c2016-12-20 17:32:45 +0100290 """For every remote host assign neighbor's MAC to IPv6 addresses.
291
292 :param vrf_id: The FIB table / VRF ID. (Default value = 0)
293 """
Klement Sekera46a87ad2017-01-02 08:22:23 +0100294 for host in self._remote_hosts:
Neale Ranns37029302018-08-10 05:30:06 -0700295 self.test.vapi.ip_neighbor_add_del(self.sw_if_index,
296 host.mac,
297 host.ip6)
Klement Sekera46a87ad2017-01-02 08:22:23 +0100298
Neale Ranns177bbdc2016-11-15 09:46:51 +0000299 def unconfig(self):
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100300 """Unconfigure IPv6 and IPv4 address on the VPP interface."""
Neale Ranns177bbdc2016-11-15 09:46:51 +0000301 self.unconfig_ip4()
302 self.unconfig_ip6()
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500303 return self
Klement Sekeraf62ae122016-10-11 11:47:09 +0200304
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000305 def set_table_ip4(self, table_id):
306 """Set the interface in a IPv4 Table.
Matej Klotton86d87c42016-11-11 11:38:55 +0100307
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100308 .. note:: Must be called before configuring IP4 addresses.
309 """
310 self.ip4_table_id = table_id
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000311 self.test.vapi.sw_interface_set_table(
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100312 self.sw_if_index, 0, self.ip4_table_id)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500313 return self
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000314
315 def set_table_ip6(self, table_id):
316 """Set the interface in a IPv6 Table.
Matej Klotton86d87c42016-11-11 11:38:55 +0100317
318 .. note:: Must be called before configuring IP6 addresses.
319 """
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100320 self.ip6_table_id = table_id
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000321 self.test.vapi.sw_interface_set_table(
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100322 self.sw_if_index, 1, self.ip6_table_id)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500323 return self
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000324
Klement Sekeraf62ae122016-10-11 11:47:09 +0200325 def disable_ipv6_ra(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100326 """Configure IPv6 RA suppress on the VPP interface."""
Ole Troane1ade682019-03-04 23:55:43 +0100327 self.test.vapi.sw_interface_ip6nd_ra_config(
328 sw_if_index=self.sw_if_index,
329 suppress=1)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500330 return self
Klement Sekeraf62ae122016-10-11 11:47:09 +0200331
Neale Ranns32e1c012016-11-22 17:07:28 +0000332 def ip6_ra_config(self, no=0, suppress=0, send_unicast=0):
Neale Ranns75152282017-01-09 01:00:45 -0800333 """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 is_no=no,
337 suppress=suppress,
338 send_unicast=send_unicast)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500339 return self
Neale Ranns75152282017-01-09 01:00:45 -0800340
Paul Vinciguerraab055082019-06-06 14:07:55 -0400341 def ip6_ra_prefix(self, prefix, is_no=0,
Neale Ranns87df12d2017-02-18 08:16:41 -0800342 off_link=0, no_autoconfig=0, use_default=0):
Paul Vinciguerraab055082019-06-06 14:07:55 -0400343 """Configure IPv6 RA suppress on the VPP interface.
344
345 prefix can be a string in the format of '<address>/<length_in_bits>'
346 or ipaddress.ipnetwork object (if strict.)"""
347
Ole Troane1ade682019-03-04 23:55:43 +0100348 self.test.vapi.sw_interface_ip6nd_ra_prefix(
Ole Troana5b2eec2019-03-11 19:23:25 +0100349 sw_if_index=self.sw_if_index,
Paul Vinciguerraab055082019-06-06 14:07:55 -0400350 prefix=prefix,
Ole Troana5b2eec2019-03-11 19:23:25 +0100351 use_default=use_default,
352 off_link=off_link, no_autoconfig=no_autoconfig,
353 is_no=is_no)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500354 return self
Neale Ranns87df12d2017-02-18 08:16:41 -0800355
Klement Sekeraf62ae122016-10-11 11:47:09 +0200356 def admin_up(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100357 """Put interface ADMIN-UP."""
Jakub Grajciar053204a2019-03-18 13:17:53 +0100358 self.test.vapi.sw_interface_set_flags(
359 self.sw_if_index,
360 flags=VppEnum.vl_api_if_status_flags_t.IF_STATUS_API_FLAG_ADMIN_UP)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500361 return self
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,
Jakub Grajciar053204a2019-03-18 13:17:53 +0100366 flags=0)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500367 return self
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100368
Neale Ranns63480742019-03-13 06:41:52 -0700369 def link_up(self):
370 """Put interface link-state-UP."""
371 self.test.vapi.cli("test interface link-state %s up" % self.name)
372
373 def link_down(self):
374 """Put interface link-state-down."""
375 self.test.vapi.cli("test interface link-state %s down" % self.name)
376
Neale Ranns75152282017-01-09 01:00:45 -0800377 def ip6_enable(self):
378 """IPv6 Enable interface"""
Ole Troane1ade682019-03-04 23:55:43 +0100379 self.test.vapi.sw_interface_ip6_enable_disable(self.sw_if_index,
Klement Sekerada505f62017-01-04 12:58:53 +0100380 enable=1)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500381 return self
Neale Ranns75152282017-01-09 01:00:45 -0800382
383 def ip6_disable(self):
384 """Put interface ADMIN-DOWN."""
Ole Troane1ade682019-03-04 23:55:43 +0100385 self.test.vapi.sw_interface_ip6_enable_disable(self.sw_if_index,
Klement Sekerada505f62017-01-04 12:58:53 +0100386 enable=0)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500387 return self
Neale Ranns75152282017-01-09 01:00:45 -0800388
Klement Sekeraf62ae122016-10-11 11:47:09 +0200389 def add_sub_if(self, sub_if):
Matej Klotton86d87c42016-11-11 11:38:55 +0100390 """Register a sub-interface with this interface.
Klement Sekeraf62ae122016-10-11 11:47:09 +0200391
392 :param sub_if: sub-interface
Klement Sekeraf62ae122016-10-11 11:47:09 +0200393 """
394 if not hasattr(self, 'sub_if'):
395 self.sub_if = sub_if
396 else:
397 if isinstance(self.sub_if, list):
398 self.sub_if.append(sub_if)
399 else:
400 self.sub_if = sub_if
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500401 return self
Neale Ranns8fe8cc22016-11-01 10:05:08 +0000402
403 def enable_mpls(self):
Matej Klotton86d87c42016-11-11 11:38:55 +0100404 """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)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500406 return self
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100407
Neale Ranns180279b2017-03-16 15:49:09 -0400408 def disable_mpls(self):
409 """Enable MPLS on the VPP interface."""
Ole Troan9a475372019-03-05 16:58:24 +0100410 self.test.vapi.sw_interface_set_mpls_enable(self.sw_if_index, 0)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500411 return self
Neale Ranns180279b2017-03-16 15:49:09 -0400412
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100413 def is_ip4_entry_in_fib_dump(self, dump):
414 for i in dump:
Neale Ranns097fa662018-05-01 05:17:55 -0700415 n = IPv4Network(text_type("%s/%d" % (self.local_ip4,
416 self.local_ip4_prefix_len)))
417 if i.route.prefix == n and \
418 i.route.table_id == self.ip4_table_id:
Matej Klotton8d8a1da2016-12-22 11:06:56 +0100419 return True
420 return False
Neale Ranns39f9d8b2017-02-16 21:57:05 -0800421
422 def set_unnumbered(self, ip_sw_if_index):
423 """ Set the interface to unnumbered via ip_sw_if_index """
Ole Troan9a475372019-03-05 16:58:24 +0100424 self.test.vapi.sw_interface_set_unnumbered(ip_sw_if_index,
425 self.sw_if_index)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500426 return self
Neale Ranns39f9d8b2017-02-16 21:57:05 -0800427
Neale Ranns37be7362017-02-21 17:30:26 -0800428 def unset_unnumbered(self, ip_sw_if_index):
Neale Ranns4b919a52017-03-11 05:55:21 -0800429 """ Unset the interface to unnumbered via ip_sw_if_index """
Ole Troan9a475372019-03-05 16:58:24 +0100430 self.test.vapi.sw_interface_set_unnumbered(ip_sw_if_index,
431 self.sw_if_index, is_add=0)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500432 return self
Neale Ranns37be7362017-02-21 17:30:26 -0800433
Neale Ranns39f9d8b2017-02-16 21:57:05 -0800434 def set_proxy_arp(self, enable=1):
435 """ Set the interface to enable/disable Proxy ARP """
436 self.test.vapi.proxy_arp_intfc_enable_disable(
Ole Troan5c2a2372020-11-19 16:01:23 +0100437 sw_if_index=self.sw_if_index,
438 enable=enable)
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500439 return self
Klement Sekera75e7d132017-09-20 08:26:30 +0200440
441 def query_vpp_config(self):
Paul Vinciguerra7a998232019-06-07 15:01:12 -0400442 dump = self.test.vapi.sw_interface_dump(sw_if_index=self.sw_if_index)
Klement Sekera75e7d132017-09-20 08:26:30 +0200443 return self.is_interface_config_in_dump(dump)
444
Juraj Slobodab3f90502018-10-04 15:15:16 +0200445 def get_interface_config_from_dump(self, dump):
Klement Sekera75e7d132017-09-20 08:26:30 +0200446 for i in dump:
447 if i.interface_name.rstrip(' \t\r\n\0') == self.name and \
Ole Troan9a475372019-03-05 16:58:24 +0100448 i.sw_if_index == self.sw_if_index:
Juraj Slobodab3f90502018-10-04 15:15:16 +0200449 return i
Klement Sekera75e7d132017-09-20 08:26:30 +0200450 else:
Juraj Slobodab3f90502018-10-04 15:15:16 +0200451 return None
452
453 def is_interface_config_in_dump(self, dump):
454 return self.get_interface_config_from_dump(dump) is not None
455
456 def assert_interface_state(self, admin_up_down, link_up_down,
457 expect_event=False):
458 if expect_event:
459 event = self.test.vapi.wait_for_event(timeout=1,
460 name='sw_interface_event')
461 self.test.assert_equal(event.sw_if_index, self.sw_if_index,
462 "sw_if_index")
Jakub Grajciar053204a2019-03-18 13:17:53 +0100463 self.test.assert_equal((event.flags & 1), admin_up_down,
Juraj Slobodab3f90502018-10-04 15:15:16 +0200464 "admin state")
Jakub Grajciar053204a2019-03-18 13:17:53 +0100465 self.test.assert_equal((event.flags & 2), link_up_down,
Juraj Slobodab3f90502018-10-04 15:15:16 +0200466 "link state")
467 dump = self.test.vapi.sw_interface_dump()
468 if_state = self.get_interface_config_from_dump(dump)
Jakub Grajciar053204a2019-03-18 13:17:53 +0100469 self.test.assert_equal((if_state.flags & 1), admin_up_down,
Juraj Slobodab3f90502018-10-04 15:15:16 +0200470 "admin state")
Jakub Grajciar053204a2019-03-18 13:17:53 +0100471 self.test.assert_equal((if_state.flags & 2), link_up_down,
Juraj Slobodab3f90502018-10-04 15:15:16 +0200472 "link state")
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700473
474 def __str__(self):
475 return self.name
Neale Ranns2ac885c2019-03-20 18:24:43 +0000476
477 def get_rx_stats(self):
478 c = self.test.statistics.get_counter("^/if/rx$")
479 return c[0][self.sw_if_index]
480
481 def get_tx_stats(self):
482 c = self.test.statistics.get_counter("^/if/tx$")
483 return c[0][self.sw_if_index]
Ole Troaneb284a12019-10-09 13:33:19 +0200484
485 def set_l3_mtu(self, mtu):
486 self.test.vapi.sw_interface_set_mtu(self.sw_if_index, [mtu, 0, 0, 0])
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500487 return self
Ole Troaneb284a12019-10-09 13:33:19 +0200488
489 def set_ip4_mtu(self, mtu):
490 self.test.vapi.sw_interface_set_mtu(self.sw_if_index, [0, mtu, 0, 0])
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500491 return self
Ole Troaneb284a12019-10-09 13:33:19 +0200492
493 def set_ip6_mtu(self, mtu):
494 self.test.vapi.sw_interface_set_mtu(self.sw_if_index, [0, 0, mtu, 0])
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500495 return self
Ole Troaneb284a12019-10-09 13:33:19 +0200496
497 def set_mpls_mtu(self, mtu):
498 self.test.vapi.sw_interface_set_mtu(self.sw_if_index, [0, 0, 0, mtu])
Paul Vinciguerra207083f2019-12-02 22:38:00 -0500499 return self