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