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