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