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