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