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