Paul Vinciguerra | 6e4c6ad | 2018-11-25 10:35:29 -0800 | [diff] [blame] | 1 | import binascii |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 2 | import socket |
Paul Vinciguerra | 3bce8eb | 2018-11-24 21:46:05 -0800 | [diff] [blame] | 3 | import abc |
Paul Vinciguerra | 00671cf | 2018-11-25 12:47:04 -0800 | [diff] [blame] | 4 | |
Paul Vinciguerra | 3bce8eb | 2018-11-24 21:46:05 -0800 | [diff] [blame] | 5 | import six |
Paul Vinciguerra | 6c74617 | 2018-11-26 09:57:21 -0800 | [diff] [blame] | 6 | from six import moves |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 7 | |
Ole Troan | 7f99183 | 2018-12-06 17:35:12 +0100 | [diff] [blame] | 8 | from util import Host, mk_ll_addr |
Jakub Grajciar | 053204a | 2019-03-18 13:17:53 +0100 | [diff] [blame] | 9 | from vpp_papi import mac_ntop, VppEnum |
| 10 | from vpp_ip import VppIpAddress, VppIpPrefix |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 11 | from ipaddress import IPv4Network |
| 12 | |
| 13 | try: |
| 14 | text_type = unicode |
| 15 | except NameError: |
| 16 | text_type = str |
Jan | 65209ed | 2016-12-05 23:29:17 +0100 | [diff] [blame] | 17 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 18 | |
Paul Vinciguerra | 3bce8eb | 2018-11-24 21:46:05 -0800 | [diff] [blame] | 19 | @six.add_metaclass(abc.ABCMeta) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 20 | class VppInterface(object): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 21 | """Generic VPP interface.""" |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 22 | |
| 23 | @property |
| 24 | def sw_if_index(self): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 25 | """Interface index assigned by VPP.""" |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 26 | return self._sw_if_index |
| 27 | |
| 28 | @property |
| 29 | def remote_mac(self): |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 30 | """MAC-address of the remote interface "connected" to this interface""" |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 31 | return self._remote_hosts[0].mac |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 32 | |
| 33 | @property |
| 34 | def local_mac(self): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 35 | """MAC-address of the VPP interface.""" |
Ole Troan | 6ed154f | 2019-10-15 19:31:55 +0200 | [diff] [blame] | 36 | return str(self._local_mac) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 37 | |
| 38 | @property |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 39 | def local_addr(self): |
| 40 | return self._local_addr |
| 41 | |
| 42 | @property |
| 43 | def remote_addr(self): |
| 44 | return self._remote_addr |
| 45 | |
| 46 | @property |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 47 | def local_ip4(self): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 48 | """Local IPv4 address on VPP interface (string).""" |
Jakub Grajciar | 053204a | 2019-03-18 13:17:53 +0100 | [diff] [blame] | 49 | return self._local_ip4.address |
| 50 | |
| 51 | @local_ip4.setter |
| 52 | def local_ip4(self, value): |
| 53 | self._local_ip4.address = value |
| 54 | |
| 55 | @property |
| 56 | def local_ip4_prefix_len(self): |
| 57 | """Local IPv4 prefix length """ |
| 58 | return self._local_ip4.len |
| 59 | |
| 60 | @local_ip4_prefix_len.setter |
| 61 | def local_ip4_prefix_len(self, value): |
| 62 | self._local_ip4.len = value |
| 63 | |
| 64 | @property |
| 65 | def local_ip4_prefix(self): |
| 66 | """Local IPv4 prefix """ |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 67 | return self._local_ip4 |
| 68 | |
| 69 | @property |
| 70 | def local_ip4n(self): |
Jakub Grajciar | 053204a | 2019-03-18 13:17:53 +0100 | [diff] [blame] | 71 | """DEPRECATED """ |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 72 | """Local IPv4 address - raw, suitable as API parameter.""" |
Jakub Grajciar | 053204a | 2019-03-18 13:17:53 +0100 | [diff] [blame] | 73 | return socket.inet_pton(socket.AF_INET, self._local_ip4.address) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 74 | |
| 75 | @property |
| 76 | def remote_ip4(self): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 77 | """IPv4 address of remote peer "connected" to this interface.""" |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 78 | return self._remote_hosts[0].ip4 |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 79 | |
| 80 | @property |
| 81 | def remote_ip4n(self): |
Jakub Grajciar | 053204a | 2019-03-18 13:17:53 +0100 | [diff] [blame] | 82 | """DEPRECATED """ |
| 83 | """Local IPv6 address - raw, suitable as API parameter.""" |
| 84 | return socket.inet_pton(socket.AF_INET, self._remote_hosts[0].ip4) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 85 | |
| 86 | @property |
| 87 | def local_ip6(self): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 88 | """Local IPv6 address on VPP interface (string).""" |
Jakub Grajciar | 053204a | 2019-03-18 13:17:53 +0100 | [diff] [blame] | 89 | return self._local_ip6.address |
| 90 | |
| 91 | @local_ip6.setter |
| 92 | def local_ip6(self, value): |
| 93 | self._local_ip6.address = value |
| 94 | |
| 95 | @property |
| 96 | def local_ip6_prefix_len(self): |
| 97 | """Local IPv6 prefix length """ |
| 98 | return self._local_ip6.len |
| 99 | |
| 100 | @local_ip6_prefix_len.setter |
| 101 | def local_ip6_prefix_len(self, value): |
| 102 | self._local_ip6.len = value |
| 103 | |
| 104 | @property |
| 105 | def local_ip6_prefix(self): |
| 106 | """Local IPv6 prefix """ |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 107 | return self._local_ip6 |
| 108 | |
| 109 | @property |
| 110 | def local_ip6n(self): |
Jakub Grajciar | 053204a | 2019-03-18 13:17:53 +0100 | [diff] [blame] | 111 | """DEPRECATED """ |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 112 | """Local IPv6 address - raw, suitable as API parameter.""" |
Jakub Grajciar | 053204a | 2019-03-18 13:17:53 +0100 | [diff] [blame] | 113 | return socket.inet_pton(socket.AF_INET6, self._local_ip6.address) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 114 | |
| 115 | @property |
| 116 | def remote_ip6(self): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 117 | """IPv6 address of remote peer "connected" to this interface.""" |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 118 | return self._remote_hosts[0].ip6 |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 119 | |
| 120 | @property |
| 121 | def remote_ip6n(self): |
Jakub Grajciar | 053204a | 2019-03-18 13:17:53 +0100 | [diff] [blame] | 122 | """DEPRECATED """ |
| 123 | """Local IPv6 address - raw, suitable as API parameter.""" |
| 124 | return socket.inet_pton(socket.AF_INET6, self._remote_hosts[0].ip6) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 125 | |
| 126 | @property |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 127 | def local_ip6_ll(self): |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 128 | """Local IPv6 link-local address on VPP interface (string).""" |
Jakub Grajciar | 053204a | 2019-03-18 13:17:53 +0100 | [diff] [blame] | 129 | return self._local_ip6_ll.address |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 130 | |
| 131 | @property |
| 132 | def local_ip6n_ll(self): |
Jakub Grajciar | 053204a | 2019-03-18 13:17:53 +0100 | [diff] [blame] | 133 | """DEPRECATED """ |
| 134 | """Local IPv6 link-local address on VPP interface (string).""" |
| 135 | return socket.inet_pton(socket.AF_INET6, self._local_ip6_ll.address) |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 136 | |
| 137 | @property |
| 138 | def remote_ip6_ll(self): |
| 139 | """Link-local IPv6 address of remote peer |
| 140 | "connected" to this interface.""" |
| 141 | return self._remote_ip6_ll |
| 142 | |
| 143 | @property |
| 144 | def remote_ip6n_ll(self): |
Jakub Grajciar | 053204a | 2019-03-18 13:17:53 +0100 | [diff] [blame] | 145 | """DEPRECATED """ |
| 146 | """Local IPv6 link-local address on VPP interface (string).""" |
| 147 | return socket.inet_pton(socket.AF_INET6, self._remote_ip6_ll) |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 148 | |
| 149 | @property |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 150 | def name(self): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 151 | """Name of the interface.""" |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 152 | return self._name |
| 153 | |
| 154 | @property |
| 155 | def dump(self): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 156 | """RAW result of sw_interface_dump for this interface.""" |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 157 | return self._dump |
| 158 | |
| 159 | @property |
| 160 | def test(self): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 161 | """Test case creating this interface.""" |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 162 | return self._test |
| 163 | |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 164 | @property |
| 165 | def remote_hosts(self): |
| 166 | """Remote hosts list""" |
| 167 | return self._remote_hosts |
| 168 | |
| 169 | @remote_hosts.setter |
| 170 | def remote_hosts(self, value): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 171 | """ |
| 172 | :param list value: List of remote hosts. |
| 173 | """ |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 174 | self._remote_hosts = value |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 175 | self._hosts_by_mac = {} |
| 176 | self._hosts_by_ip4 = {} |
| 177 | self._hosts_by_ip6 = {} |
| 178 | for host in self._remote_hosts: |
| 179 | self._hosts_by_mac[host.mac] = host |
| 180 | self._hosts_by_ip4[host.ip4] = host |
| 181 | self._hosts_by_ip6[host.ip6] = host |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 182 | |
| 183 | def host_by_mac(self, mac): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 184 | """ |
Matej Klotton | c5bf07f | 2016-11-23 15:27:17 +0100 | [diff] [blame] | 185 | :param mac: MAC address to find host by. |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 186 | :return: Host object assigned to interface. |
| 187 | """ |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 188 | return self._hosts_by_mac[mac] |
| 189 | |
| 190 | def host_by_ip4(self, ip): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 191 | """ |
| 192 | :param ip: IPv4 address to find host by. |
| 193 | :return: Host object assigned to interface. |
| 194 | """ |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 195 | return self._hosts_by_ip4[ip] |
| 196 | |
| 197 | def host_by_ip6(self, ip): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 198 | """ |
| 199 | :param ip: IPv6 address to find host by. |
| 200 | :return: Host object assigned to interface. |
| 201 | """ |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 202 | return self._hosts_by_ip6[ip] |
| 203 | |
| 204 | def generate_remote_hosts(self, count=1): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 205 | """Generate and add remote hosts for the interface. |
| 206 | |
| 207 | :param int count: Number of generated remote hosts. |
| 208 | """ |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 209 | self._remote_hosts = [] |
| 210 | self._hosts_by_mac = {} |
| 211 | self._hosts_by_ip4 = {} |
| 212 | self._hosts_by_ip6 = {} |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 213 | for i in range( |
| 214 | 2, count + 2): # 0: network address, 1: local vpp address |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 215 | mac = "02:%02x:00:00:ff:%02x" % (self.sw_if_index, i) |
| 216 | ip4 = "172.16.%u.%u" % (self.sw_if_index, i) |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 217 | ip6 = "fd01:%x::%x" % (self.sw_if_index, i) |
Neale Ranns | 2a3ea49 | 2017-04-19 05:24:40 -0700 | [diff] [blame] | 218 | ip6_ll = mk_ll_addr(mac) |
| 219 | host = Host(mac, ip4, ip6, ip6_ll) |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 220 | self._remote_hosts.append(host) |
| 221 | self._hosts_by_mac[mac] = host |
| 222 | self._hosts_by_ip4[ip4] = host |
| 223 | self._hosts_by_ip6[ip6] = host |
| 224 | |
Paul Vinciguerra | 3bce8eb | 2018-11-24 21:46:05 -0800 | [diff] [blame] | 225 | @abc.abstractmethod |
Matej Klotton | c5bf07f | 2016-11-23 15:27:17 +0100 | [diff] [blame] | 226 | def __init__(self, test): |
| 227 | self._test = test |
| 228 | |
| 229 | self._remote_hosts = [] |
| 230 | self._hosts_by_mac = {} |
| 231 | self._hosts_by_ip4 = {} |
| 232 | self._hosts_by_ip6 = {} |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 233 | |
Neale Ranns | 3b81a1e | 2018-09-06 09:50:26 -0700 | [diff] [blame] | 234 | def set_mac(self, mac): |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 235 | self._local_mac = str(mac) |
Jakub Grajciar | 053204a | 2019-03-18 13:17:53 +0100 | [diff] [blame] | 236 | self._local_ip6_ll = VppIpAddress(mk_ll_addr(self._local_mac)) |
Neale Ranns | 3b81a1e | 2018-09-06 09:50:26 -0700 | [diff] [blame] | 237 | self.test.vapi.sw_interface_set_mac_address( |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 238 | self.sw_if_index, mac.packed) |
Neale Ranns | 3b81a1e | 2018-09-06 09:50:26 -0700 | [diff] [blame] | 239 | |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 240 | def set_sw_if_index(self, sw_if_index): |
| 241 | self._sw_if_index = sw_if_index |
| 242 | |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 243 | self.generate_remote_hosts() |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 244 | |
Jakub Grajciar | 053204a | 2019-03-18 13:17:53 +0100 | [diff] [blame] | 245 | self._local_ip4 = VppIpPrefix("172.16.%u.1" % self.sw_if_index, 24) |
Neale Ranns | 24b170a | 2017-08-15 05:33:11 -0700 | [diff] [blame] | 246 | self._local_ip4_subnet = "172.16.%u.0" % self.sw_if_index |
Neale Ranns | 24b170a | 2017-08-15 05:33:11 -0700 | [diff] [blame] | 247 | self._local_ip4_bcast = "172.16.%u.255" % self.sw_if_index |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 248 | self.has_ip4_config = False |
| 249 | self.ip4_table_id = 0 |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 250 | |
Jakub Grajciar | 053204a | 2019-03-18 13:17:53 +0100 | [diff] [blame] | 251 | self._local_ip6 = VppIpPrefix("fd01:%x::1" % self.sw_if_index, 64) |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 252 | self.has_ip6_config = False |
| 253 | self.ip6_table_id = 0 |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 254 | |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 255 | self._local_addr = {socket.AF_INET: self.local_ip4, |
| 256 | socket.AF_INET6: self.local_ip6} |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 257 | self._remote_addr = {socket.AF_INET: self.remote_ip4, |
| 258 | socket.AF_INET6: self.remote_ip6} |
Klement Sekera | 611864f | 2018-09-26 11:19:00 +0200 | [diff] [blame] | 259 | |
Paul Vinciguerra | 7a99823 | 2019-06-07 15:01:12 -0400 | [diff] [blame] | 260 | r = self.test.vapi.sw_interface_dump(sw_if_index=self.sw_if_index) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 261 | for intf in r: |
| 262 | if intf.sw_if_index == self.sw_if_index: |
Ole Troan | 6ed154f | 2019-10-15 19:31:55 +0200 | [diff] [blame] | 263 | self._name = intf.interface_name |
| 264 | self._local_mac = intf.l2_address |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 265 | self._dump = intf |
| 266 | break |
| 267 | else: |
| 268 | raise Exception( |
| 269 | "Could not find interface with sw_if_index %d " |
| 270 | "in interface dump %s" % |
Paul Vinciguerra | 6c74617 | 2018-11-26 09:57:21 -0800 | [diff] [blame] | 271 | (self.sw_if_index, moves.reprlib.repr(r))) |
Jakub Grajciar | 053204a | 2019-03-18 13:17:53 +0100 | [diff] [blame] | 272 | self._local_ip6_ll = VppIpAddress(mk_ll_addr(self.local_mac)) |
Juraj Sloboda | c037423 | 2018-02-01 15:18:49 +0100 | [diff] [blame] | 273 | self._remote_ip6_ll = mk_ll_addr(self.remote_mac) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 274 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 275 | def config_ip4(self): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 276 | """Configure IPv4 address on the VPP interface.""" |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 277 | self.test.vapi.sw_interface_add_del_address( |
Jakub Grajciar | 053204a | 2019-03-18 13:17:53 +0100 | [diff] [blame] | 278 | sw_if_index=self.sw_if_index, prefix=self._local_ip4.encode()) |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 279 | self.has_ip4_config = True |
| 280 | |
| 281 | def unconfig_ip4(self): |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 282 | """Remove IPv4 address on the VPP interface.""" |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 283 | try: |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 284 | if self.has_ip4_config: |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 285 | self.test.vapi.sw_interface_add_del_address( |
Jakub Grajciar | 053204a | 2019-03-18 13:17:53 +0100 | [diff] [blame] | 286 | sw_if_index=self.sw_if_index, |
| 287 | prefix=self._local_ip4.encode(), is_add=0) |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 288 | except AttributeError: |
| 289 | self.has_ip4_config = False |
| 290 | self.has_ip4_config = False |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 291 | |
Neale Ranns | baf2e90 | 2017-02-25 04:20:00 -0800 | [diff] [blame] | 292 | def configure_ipv4_neighbors(self): |
Jan | e546d3b | 2016-12-08 13:10:03 +0100 | [diff] [blame] | 293 | """For every remote host assign neighbor's MAC to IPv4 addresses. |
| 294 | |
| 295 | :param vrf_id: The FIB table / VRF ID. (Default value = 0) |
| 296 | """ |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 297 | for host in self._remote_hosts: |
Neale Ranns | 3702930 | 2018-08-10 05:30:06 -0700 | [diff] [blame] | 298 | self.test.vapi.ip_neighbor_add_del(self.sw_if_index, |
| 299 | host.mac, |
| 300 | host.ip4) |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 301 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 302 | def config_ip6(self): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 303 | """Configure IPv6 address on the VPP interface.""" |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 304 | self.test.vapi.sw_interface_add_del_address( |
Jakub Grajciar | 053204a | 2019-03-18 13:17:53 +0100 | [diff] [blame] | 305 | sw_if_index=self.sw_if_index, prefix=self._local_ip6.encode()) |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 306 | self.has_ip6_config = True |
| 307 | |
| 308 | def unconfig_ip6(self): |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 309 | """Remove IPv6 address on the VPP interface.""" |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 310 | try: |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 311 | if self.has_ip6_config: |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 312 | self.test.vapi.sw_interface_add_del_address( |
Jakub Grajciar | 053204a | 2019-03-18 13:17:53 +0100 | [diff] [blame] | 313 | sw_if_index=self.sw_if_index, |
| 314 | prefix=self._local_ip6.encode(), is_add=0) |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 315 | except AttributeError: |
| 316 | self.has_ip6_config = False |
| 317 | self.has_ip6_config = False |
| 318 | |
Neale Ranns | baf2e90 | 2017-02-25 04:20:00 -0800 | [diff] [blame] | 319 | def configure_ipv6_neighbors(self): |
Jan Gelety | 057bb8c | 2016-12-20 17:32:45 +0100 | [diff] [blame] | 320 | """For every remote host assign neighbor's MAC to IPv6 addresses. |
| 321 | |
| 322 | :param vrf_id: The FIB table / VRF ID. (Default value = 0) |
| 323 | """ |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 324 | for host in self._remote_hosts: |
Neale Ranns | 3702930 | 2018-08-10 05:30:06 -0700 | [diff] [blame] | 325 | self.test.vapi.ip_neighbor_add_del(self.sw_if_index, |
| 326 | host.mac, |
| 327 | host.ip6) |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 328 | |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 329 | def unconfig(self): |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 330 | """Unconfigure IPv6 and IPv4 address on the VPP interface.""" |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 331 | self.unconfig_ip4() |
| 332 | self.unconfig_ip6() |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 333 | |
Neale Ranns | 8fe8cc2 | 2016-11-01 10:05:08 +0000 | [diff] [blame] | 334 | def set_table_ip4(self, table_id): |
| 335 | """Set the interface in a IPv4 Table. |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 336 | |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 337 | .. note:: Must be called before configuring IP4 addresses. |
| 338 | """ |
| 339 | self.ip4_table_id = table_id |
Neale Ranns | 8fe8cc2 | 2016-11-01 10:05:08 +0000 | [diff] [blame] | 340 | self.test.vapi.sw_interface_set_table( |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 341 | self.sw_if_index, 0, self.ip4_table_id) |
Neale Ranns | 8fe8cc2 | 2016-11-01 10:05:08 +0000 | [diff] [blame] | 342 | |
| 343 | def set_table_ip6(self, table_id): |
| 344 | """Set the interface in a IPv6 Table. |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 345 | |
| 346 | .. note:: Must be called before configuring IP6 addresses. |
| 347 | """ |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 348 | self.ip6_table_id = table_id |
Neale Ranns | 8fe8cc2 | 2016-11-01 10:05:08 +0000 | [diff] [blame] | 349 | self.test.vapi.sw_interface_set_table( |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 350 | self.sw_if_index, 1, self.ip6_table_id) |
Neale Ranns | 8fe8cc2 | 2016-11-01 10:05:08 +0000 | [diff] [blame] | 351 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 352 | def disable_ipv6_ra(self): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 353 | """Configure IPv6 RA suppress on the VPP interface.""" |
Ole Troan | e1ade68 | 2019-03-04 23:55:43 +0100 | [diff] [blame] | 354 | self.test.vapi.sw_interface_ip6nd_ra_config( |
| 355 | sw_if_index=self.sw_if_index, |
| 356 | suppress=1) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 357 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 358 | def ip6_ra_config(self, no=0, suppress=0, send_unicast=0): |
Neale Ranns | 7515228 | 2017-01-09 01:00:45 -0800 | [diff] [blame] | 359 | """Configure IPv6 RA suppress on the VPP interface.""" |
Ole Troan | e1ade68 | 2019-03-04 23:55:43 +0100 | [diff] [blame] | 360 | self.test.vapi.sw_interface_ip6nd_ra_config( |
| 361 | sw_if_index=self.sw_if_index, |
| 362 | is_no=no, |
| 363 | suppress=suppress, |
| 364 | send_unicast=send_unicast) |
Neale Ranns | 7515228 | 2017-01-09 01:00:45 -0800 | [diff] [blame] | 365 | |
Paul Vinciguerra | ab05508 | 2019-06-06 14:07:55 -0400 | [diff] [blame] | 366 | def ip6_ra_prefix(self, prefix, is_no=0, |
Neale Ranns | 87df12d | 2017-02-18 08:16:41 -0800 | [diff] [blame] | 367 | off_link=0, no_autoconfig=0, use_default=0): |
Paul Vinciguerra | ab05508 | 2019-06-06 14:07:55 -0400 | [diff] [blame] | 368 | """Configure IPv6 RA suppress on the VPP interface. |
| 369 | |
| 370 | prefix can be a string in the format of '<address>/<length_in_bits>' |
| 371 | or ipaddress.ipnetwork object (if strict.)""" |
| 372 | |
Ole Troan | e1ade68 | 2019-03-04 23:55:43 +0100 | [diff] [blame] | 373 | self.test.vapi.sw_interface_ip6nd_ra_prefix( |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 374 | sw_if_index=self.sw_if_index, |
Paul Vinciguerra | ab05508 | 2019-06-06 14:07:55 -0400 | [diff] [blame] | 375 | prefix=prefix, |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 376 | use_default=use_default, |
| 377 | off_link=off_link, no_autoconfig=no_autoconfig, |
| 378 | is_no=is_no) |
Neale Ranns | 87df12d | 2017-02-18 08:16:41 -0800 | [diff] [blame] | 379 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 380 | def admin_up(self): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 381 | """Put interface ADMIN-UP.""" |
Jakub Grajciar | 053204a | 2019-03-18 13:17:53 +0100 | [diff] [blame] | 382 | self.test.vapi.sw_interface_set_flags( |
| 383 | self.sw_if_index, |
| 384 | flags=VppEnum.vl_api_if_status_flags_t.IF_STATUS_API_FLAG_ADMIN_UP) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 385 | |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 386 | def admin_down(self): |
| 387 | """Put interface ADMIN-down.""" |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 388 | self.test.vapi.sw_interface_set_flags(self.sw_if_index, |
Jakub Grajciar | 053204a | 2019-03-18 13:17:53 +0100 | [diff] [blame] | 389 | flags=0) |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 390 | |
Neale Ranns | 6348074 | 2019-03-13 06:41:52 -0700 | [diff] [blame] | 391 | def link_up(self): |
| 392 | """Put interface link-state-UP.""" |
| 393 | self.test.vapi.cli("test interface link-state %s up" % self.name) |
| 394 | |
| 395 | def link_down(self): |
| 396 | """Put interface link-state-down.""" |
| 397 | self.test.vapi.cli("test interface link-state %s down" % self.name) |
| 398 | |
Neale Ranns | 7515228 | 2017-01-09 01:00:45 -0800 | [diff] [blame] | 399 | def ip6_enable(self): |
| 400 | """IPv6 Enable interface""" |
Ole Troan | e1ade68 | 2019-03-04 23:55:43 +0100 | [diff] [blame] | 401 | self.test.vapi.sw_interface_ip6_enable_disable(self.sw_if_index, |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 402 | enable=1) |
Neale Ranns | 7515228 | 2017-01-09 01:00:45 -0800 | [diff] [blame] | 403 | |
| 404 | def ip6_disable(self): |
| 405 | """Put interface ADMIN-DOWN.""" |
Ole Troan | e1ade68 | 2019-03-04 23:55:43 +0100 | [diff] [blame] | 406 | self.test.vapi.sw_interface_ip6_enable_disable(self.sw_if_index, |
Klement Sekera | da505f6 | 2017-01-04 12:58:53 +0100 | [diff] [blame] | 407 | enable=0) |
Neale Ranns | 7515228 | 2017-01-09 01:00:45 -0800 | [diff] [blame] | 408 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 409 | def add_sub_if(self, sub_if): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 410 | """Register a sub-interface with this interface. |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 411 | |
| 412 | :param sub_if: sub-interface |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 413 | """ |
| 414 | if not hasattr(self, 'sub_if'): |
| 415 | self.sub_if = sub_if |
| 416 | else: |
| 417 | if isinstance(self.sub_if, list): |
| 418 | self.sub_if.append(sub_if) |
| 419 | else: |
| 420 | self.sub_if = sub_if |
Neale Ranns | 8fe8cc2 | 2016-11-01 10:05:08 +0000 | [diff] [blame] | 421 | |
| 422 | def enable_mpls(self): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 423 | """Enable MPLS on the VPP interface.""" |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 424 | self.test.vapi.sw_interface_set_mpls_enable(self.sw_if_index) |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 425 | |
Neale Ranns | 180279b | 2017-03-16 15:49:09 -0400 | [diff] [blame] | 426 | def disable_mpls(self): |
| 427 | """Enable MPLS on the VPP interface.""" |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 428 | self.test.vapi.sw_interface_set_mpls_enable(self.sw_if_index, 0) |
Neale Ranns | 180279b | 2017-03-16 15:49:09 -0400 | [diff] [blame] | 429 | |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 430 | def is_ip4_entry_in_fib_dump(self, dump): |
| 431 | for i in dump: |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 432 | n = IPv4Network(text_type("%s/%d" % (self.local_ip4, |
| 433 | self.local_ip4_prefix_len))) |
| 434 | if i.route.prefix == n and \ |
| 435 | i.route.table_id == self.ip4_table_id: |
Matej Klotton | 8d8a1da | 2016-12-22 11:06:56 +0100 | [diff] [blame] | 436 | return True |
| 437 | return False |
Neale Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 438 | |
| 439 | def set_unnumbered(self, ip_sw_if_index): |
| 440 | """ Set the interface to unnumbered via ip_sw_if_index """ |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 441 | self.test.vapi.sw_interface_set_unnumbered(ip_sw_if_index, |
| 442 | self.sw_if_index) |
Neale Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 443 | |
Neale Ranns | 37be736 | 2017-02-21 17:30:26 -0800 | [diff] [blame] | 444 | def unset_unnumbered(self, ip_sw_if_index): |
Neale Ranns | 4b919a5 | 2017-03-11 05:55:21 -0800 | [diff] [blame] | 445 | """ Unset the interface to unnumbered via ip_sw_if_index """ |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 446 | self.test.vapi.sw_interface_set_unnumbered(ip_sw_if_index, |
| 447 | self.sw_if_index, is_add=0) |
Neale Ranns | 37be736 | 2017-02-21 17:30:26 -0800 | [diff] [blame] | 448 | |
Neale Ranns | 39f9d8b | 2017-02-16 21:57:05 -0800 | [diff] [blame] | 449 | def set_proxy_arp(self, enable=1): |
| 450 | """ Set the interface to enable/disable Proxy ARP """ |
| 451 | self.test.vapi.proxy_arp_intfc_enable_disable( |
| 452 | self.sw_if_index, |
| 453 | enable) |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 454 | |
| 455 | def query_vpp_config(self): |
Paul Vinciguerra | 7a99823 | 2019-06-07 15:01:12 -0400 | [diff] [blame] | 456 | dump = self.test.vapi.sw_interface_dump(sw_if_index=self.sw_if_index) |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 457 | return self.is_interface_config_in_dump(dump) |
| 458 | |
Juraj Sloboda | b3f9050 | 2018-10-04 15:15:16 +0200 | [diff] [blame] | 459 | def get_interface_config_from_dump(self, dump): |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 460 | for i in dump: |
| 461 | if i.interface_name.rstrip(' \t\r\n\0') == self.name and \ |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 462 | i.sw_if_index == self.sw_if_index: |
Juraj Sloboda | b3f9050 | 2018-10-04 15:15:16 +0200 | [diff] [blame] | 463 | return i |
Klement Sekera | 75e7d13 | 2017-09-20 08:26:30 +0200 | [diff] [blame] | 464 | else: |
Juraj Sloboda | b3f9050 | 2018-10-04 15:15:16 +0200 | [diff] [blame] | 465 | return None |
| 466 | |
| 467 | def is_interface_config_in_dump(self, dump): |
| 468 | return self.get_interface_config_from_dump(dump) is not None |
| 469 | |
| 470 | def assert_interface_state(self, admin_up_down, link_up_down, |
| 471 | expect_event=False): |
| 472 | if expect_event: |
| 473 | event = self.test.vapi.wait_for_event(timeout=1, |
| 474 | name='sw_interface_event') |
| 475 | self.test.assert_equal(event.sw_if_index, self.sw_if_index, |
| 476 | "sw_if_index") |
Jakub Grajciar | 053204a | 2019-03-18 13:17:53 +0100 | [diff] [blame] | 477 | self.test.assert_equal((event.flags & 1), admin_up_down, |
Juraj Sloboda | b3f9050 | 2018-10-04 15:15:16 +0200 | [diff] [blame] | 478 | "admin state") |
Jakub Grajciar | 053204a | 2019-03-18 13:17:53 +0100 | [diff] [blame] | 479 | self.test.assert_equal((event.flags & 2), link_up_down, |
Juraj Sloboda | b3f9050 | 2018-10-04 15:15:16 +0200 | [diff] [blame] | 480 | "link state") |
| 481 | dump = self.test.vapi.sw_interface_dump() |
| 482 | if_state = self.get_interface_config_from_dump(dump) |
Jakub Grajciar | 053204a | 2019-03-18 13:17:53 +0100 | [diff] [blame] | 483 | self.test.assert_equal((if_state.flags & 1), admin_up_down, |
Juraj Sloboda | b3f9050 | 2018-10-04 15:15:16 +0200 | [diff] [blame] | 484 | "admin state") |
Jakub Grajciar | 053204a | 2019-03-18 13:17:53 +0100 | [diff] [blame] | 485 | self.test.assert_equal((if_state.flags & 2), link_up_down, |
Juraj Sloboda | b3f9050 | 2018-10-04 15:15:16 +0200 | [diff] [blame] | 486 | "link state") |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 487 | |
| 488 | def __str__(self): |
| 489 | return self.name |
Neale Ranns | 2ac885c | 2019-03-20 18:24:43 +0000 | [diff] [blame] | 490 | |
| 491 | def get_rx_stats(self): |
| 492 | c = self.test.statistics.get_counter("^/if/rx$") |
| 493 | return c[0][self.sw_if_index] |
| 494 | |
| 495 | def get_tx_stats(self): |
| 496 | c = self.test.statistics.get_counter("^/if/tx$") |
| 497 | return c[0][self.sw_if_index] |
Ole Troan | eb284a1 | 2019-10-09 13:33:19 +0200 | [diff] [blame] | 498 | |
| 499 | def set_l3_mtu(self, mtu): |
| 500 | self.test.vapi.sw_interface_set_mtu(self.sw_if_index, [mtu, 0, 0, 0]) |
| 501 | |
| 502 | def set_ip4_mtu(self, mtu): |
| 503 | self.test.vapi.sw_interface_set_mtu(self.sw_if_index, [0, mtu, 0, 0]) |
| 504 | |
| 505 | def set_ip6_mtu(self, mtu): |
| 506 | self.test.vapi.sw_interface_set_mtu(self.sw_if_index, [0, 0, mtu, 0]) |
| 507 | |
| 508 | def set_mpls_mtu(self, mtu): |
| 509 | self.test.vapi.sw_interface_set_mtu(self.sw_if_index, [0, 0, 0, mtu]) |