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 | |
Jan | 65209ed | 2016-12-05 23:29:17 +0100 | [diff] [blame] | 4 | from util import Host |
| 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): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +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 |
| 67 | def name(self): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 68 | """Name of the interface.""" |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 69 | return self._name |
| 70 | |
| 71 | @property |
| 72 | def dump(self): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 73 | """RAW result of sw_interface_dump for this interface.""" |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 74 | return self._dump |
| 75 | |
| 76 | @property |
| 77 | def test(self): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 78 | """Test case creating this interface.""" |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 79 | return self._test |
| 80 | |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 81 | @property |
| 82 | def remote_hosts(self): |
| 83 | """Remote hosts list""" |
| 84 | return self._remote_hosts |
| 85 | |
| 86 | @remote_hosts.setter |
| 87 | def remote_hosts(self, value): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 88 | """ |
| 89 | :param list value: List of remote hosts. |
| 90 | """ |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 91 | self._remote_hosts = value |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 92 | self._hosts_by_mac = {} |
| 93 | self._hosts_by_ip4 = {} |
| 94 | self._hosts_by_ip6 = {} |
| 95 | for host in self._remote_hosts: |
| 96 | self._hosts_by_mac[host.mac] = host |
| 97 | self._hosts_by_ip4[host.ip4] = host |
| 98 | self._hosts_by_ip6[host.ip6] = host |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 99 | |
| 100 | def host_by_mac(self, mac): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 101 | """ |
Matej Klotton | c5bf07f | 2016-11-23 15:27:17 +0100 | [diff] [blame] | 102 | :param mac: MAC address to find host by. |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 103 | :return: Host object assigned to interface. |
| 104 | """ |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 105 | return self._hosts_by_mac[mac] |
| 106 | |
| 107 | def host_by_ip4(self, ip): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 108 | """ |
| 109 | :param ip: IPv4 address to find host by. |
| 110 | :return: Host object assigned to interface. |
| 111 | """ |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 112 | return self._hosts_by_ip4[ip] |
| 113 | |
| 114 | def host_by_ip6(self, ip): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 115 | """ |
| 116 | :param ip: IPv6 address to find host by. |
| 117 | :return: Host object assigned to interface. |
| 118 | """ |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 119 | return self._hosts_by_ip6[ip] |
| 120 | |
| 121 | def generate_remote_hosts(self, count=1): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 122 | """Generate and add remote hosts for the interface. |
| 123 | |
| 124 | :param int count: Number of generated remote hosts. |
| 125 | """ |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 126 | self._remote_hosts = [] |
| 127 | self._hosts_by_mac = {} |
| 128 | self._hosts_by_ip4 = {} |
| 129 | self._hosts_by_ip6 = {} |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 130 | for i in range( |
| 131 | 2, count + 2): # 0: network address, 1: local vpp address |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 132 | mac = "02:%02x:00:00:ff:%02x" % (self.sw_if_index, i) |
| 133 | ip4 = "172.16.%u.%u" % (self.sw_if_index, i) |
| 134 | ip6 = "fd01:%04x::%04x" % (self.sw_if_index, i) |
| 135 | host = Host(mac, ip4, ip6) |
| 136 | self._remote_hosts.append(host) |
| 137 | self._hosts_by_mac[mac] = host |
| 138 | self._hosts_by_ip4[ip4] = host |
| 139 | self._hosts_by_ip6[ip6] = host |
| 140 | |
Matej Klotton | c5bf07f | 2016-11-23 15:27:17 +0100 | [diff] [blame] | 141 | @abstractmethod |
| 142 | def __init__(self, test): |
| 143 | self._test = test |
| 144 | |
| 145 | self._remote_hosts = [] |
| 146 | self._hosts_by_mac = {} |
| 147 | self._hosts_by_ip4 = {} |
| 148 | self._hosts_by_ip6 = {} |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 149 | |
| 150 | self.generate_remote_hosts() |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 151 | |
| 152 | self._local_ip4 = "172.16.%u.1" % self.sw_if_index |
| 153 | self._local_ip4n = socket.inet_pton(socket.AF_INET, self.local_ip4) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 154 | |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 155 | self._local_ip6 = "fd01:%04x::1" % self.sw_if_index |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 156 | self._local_ip6n = socket.inet_pton(socket.AF_INET6, self.local_ip6) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 157 | |
| 158 | r = self.test.vapi.sw_interface_dump() |
| 159 | for intf in r: |
| 160 | if intf.sw_if_index == self.sw_if_index: |
| 161 | self._name = intf.interface_name.split(b'\0', 1)[0] |
Klement Sekera | 7bb873a | 2016-11-18 07:38:42 +0100 | [diff] [blame] | 162 | self._local_mac =\ |
| 163 | ':'.join(intf.l2_address.encode('hex')[i:i + 2] |
| 164 | for i in range(0, 12, 2)) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 165 | self._dump = intf |
| 166 | break |
| 167 | else: |
| 168 | raise Exception( |
| 169 | "Could not find interface with sw_if_index %d " |
| 170 | "in interface dump %s" % |
| 171 | (self.sw_if_index, repr(r))) |
| 172 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 173 | def config_ip4(self): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 174 | """Configure IPv4 address on the VPP interface.""" |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 175 | addr = self.local_ip4n |
| 176 | addr_len = 24 |
| 177 | self.test.vapi.sw_interface_add_del_address( |
| 178 | self.sw_if_index, addr, addr_len) |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 179 | self.has_ip4_config = True |
| 180 | |
| 181 | def unconfig_ip4(self): |
| 182 | """Remove IPv4 address on the VPP interface""" |
| 183 | try: |
| 184 | if (self.has_ip4_config): |
| 185 | self.test.vapi.sw_interface_add_del_address( |
| 186 | self.sw_if_index, |
| 187 | self.local_ip4n, |
| 188 | 24, is_add=0) |
| 189 | except AttributeError: |
| 190 | self.has_ip4_config = False |
| 191 | self.has_ip4_config = False |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 192 | |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 193 | def configure_ipv4_neighbors(self): |
| 194 | """For every remote host assign neighbor's MAC to IPv4 addresses.""" |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 195 | for host in self._remote_hosts: |
| 196 | macn = host.mac.replace(":", "").decode('hex') |
| 197 | ipn = host.ip4n |
| 198 | self.test.vapi.ip_neighbor_add_del(self.sw_if_index, macn, ipn) |
| 199 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 200 | def config_ip6(self): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 201 | """Configure IPv6 address on the VPP interface.""" |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 202 | addr = self._local_ip6n |
| 203 | addr_len = 64 |
| 204 | self.test.vapi.sw_interface_add_del_address( |
| 205 | self.sw_if_index, addr, addr_len, is_ipv6=1) |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 206 | self.has_ip6_config = True |
| 207 | |
| 208 | def unconfig_ip6(self): |
| 209 | """Remove IPv6 address on the VPP interface""" |
| 210 | try: |
| 211 | if (self.has_ip6_config): |
| 212 | self.test.vapi.sw_interface_add_del_address( |
| 213 | self.sw_if_index, |
| 214 | self.local_ip6n, |
| 215 | 64, is_ipv6=1, is_add=0) |
| 216 | except AttributeError: |
| 217 | self.has_ip6_config = False |
| 218 | self.has_ip6_config = False |
| 219 | |
| 220 | def unconfig(self): |
| 221 | """Unconfigure IPv6 and IPv4 address on the VPP interface""" |
| 222 | self.unconfig_ip4() |
| 223 | self.unconfig_ip6() |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 224 | |
Neale Ranns | 8fe8cc2 | 2016-11-01 10:05:08 +0000 | [diff] [blame] | 225 | def set_table_ip4(self, table_id): |
| 226 | """Set the interface in a IPv4 Table. |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 227 | |
| 228 | .. note:: Must be called before configuring IP4 addresses.""" |
Neale Ranns | 8fe8cc2 | 2016-11-01 10:05:08 +0000 | [diff] [blame] | 229 | self.test.vapi.sw_interface_set_table( |
| 230 | self.sw_if_index, 0, table_id) |
| 231 | |
| 232 | def set_table_ip6(self, table_id): |
| 233 | """Set the interface in a IPv6 Table. |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 234 | |
| 235 | .. note:: Must be called before configuring IP6 addresses. |
| 236 | """ |
Neale Ranns | 8fe8cc2 | 2016-11-01 10:05:08 +0000 | [diff] [blame] | 237 | self.test.vapi.sw_interface_set_table( |
| 238 | self.sw_if_index, 1, table_id) |
| 239 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 240 | def disable_ipv6_ra(self): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 241 | """Configure IPv6 RA suppress on the VPP interface.""" |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 242 | self.test.vapi.sw_interface_ra_suppress(self.sw_if_index) |
| 243 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 244 | def admin_up(self): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 245 | """Put interface ADMIN-UP.""" |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 246 | self.test.vapi.sw_interface_set_flags(self.sw_if_index, admin_up_down=1) |
| 247 | |
| 248 | def add_sub_if(self, sub_if): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 249 | """Register a sub-interface with this interface. |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 250 | |
| 251 | :param sub_if: sub-interface |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 252 | """ |
| 253 | if not hasattr(self, 'sub_if'): |
| 254 | self.sub_if = sub_if |
| 255 | else: |
| 256 | if isinstance(self.sub_if, list): |
| 257 | self.sub_if.append(sub_if) |
| 258 | else: |
| 259 | self.sub_if = sub_if |
Neale Ranns | 8fe8cc2 | 2016-11-01 10:05:08 +0000 | [diff] [blame] | 260 | |
| 261 | def enable_mpls(self): |
Matej Klotton | 86d87c4 | 2016-11-11 11:38:55 +0100 | [diff] [blame] | 262 | """Enable MPLS on the VPP interface.""" |
Neale Ranns | 8fe8cc2 | 2016-11-01 10:05:08 +0000 | [diff] [blame] | 263 | self.test.vapi.sw_interface_enable_disable_mpls( |
| 264 | self.sw_if_index) |