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