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