Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 1 | """ |
| 2 | IP Types |
| 3 | |
| 4 | """ |
Paul Vinciguerra | a3aaa61 | 2018-12-09 08:52:14 -0800 | [diff] [blame] | 5 | import logging |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 6 | |
| 7 | from ipaddress import ip_address |
Jakub Grajciar | 2df2f75 | 2020-12-01 11:23:44 +0100 | [diff] [blame] | 8 | from vpp_object import VppObject |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 9 | |
Paul Vinciguerra | 9e31595 | 2019-01-29 11:51:44 -0800 | [diff] [blame] | 10 | try: |
| 11 | text_type = unicode |
| 12 | except NameError: |
| 13 | text_type = str |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 14 | |
Paul Vinciguerra | a3aaa61 | 2018-12-09 08:52:14 -0800 | [diff] [blame] | 15 | _log = logging.getLogger(__name__) |
| 16 | |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 17 | |
Neale Ranns | c0a9314 | 2018-09-05 15:42:26 -0700 | [diff] [blame] | 18 | class DpoProto: |
| 19 | DPO_PROTO_IP4 = 0 |
| 20 | DPO_PROTO_IP6 = 1 |
| 21 | DPO_PROTO_MPLS = 2 |
| 22 | DPO_PROTO_ETHERNET = 3 |
| 23 | DPO_PROTO_BIER = 4 |
| 24 | DPO_PROTO_NSH = 5 |
| 25 | |
| 26 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 27 | INVALID_INDEX = 0xFFFFFFFF |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 28 | |
| 29 | |
Neale Ranns | efd7bc2 | 2019-11-11 08:32:34 +0000 | [diff] [blame] | 30 | def get_dpo_proto(addr): |
| 31 | if ip_address(addr).version == 6: |
| 32 | return DpoProto.DPO_PROTO_IP6 |
| 33 | else: |
| 34 | return DpoProto.DPO_PROTO_IP4 |
| 35 | |
| 36 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 37 | class VppIpAddressUnion: |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 38 | def __init__(self, addr): |
| 39 | self.addr = addr |
Paul Vinciguerra | 9e31595 | 2019-01-29 11:51:44 -0800 | [diff] [blame] | 40 | self.ip_addr = ip_address(text_type(self.addr)) |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 41 | |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 42 | def encode(self): |
Paul Vinciguerra | a3aaa61 | 2018-12-09 08:52:14 -0800 | [diff] [blame] | 43 | if self.version == 6: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 44 | return {"ip6": self.ip_addr} |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 45 | else: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 46 | return {"ip4": self.ip_addr} |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 47 | |
Neale Ranns | c0a9314 | 2018-09-05 15:42:26 -0700 | [diff] [blame] | 48 | @property |
| 49 | def version(self): |
| 50 | return self.ip_addr.version |
| 51 | |
| 52 | @property |
| 53 | def address(self): |
| 54 | return self.addr |
| 55 | |
| 56 | @property |
| 57 | def length(self): |
Paul Vinciguerra | a3aaa61 | 2018-12-09 08:52:14 -0800 | [diff] [blame] | 58 | return self.ip_addr.max_prefixlen |
Neale Ranns | c0a9314 | 2018-09-05 15:42:26 -0700 | [diff] [blame] | 59 | |
| 60 | @property |
| 61 | def bytes(self): |
| 62 | return self.ip_addr.packed |
| 63 | |
| 64 | def __eq__(self, other): |
| 65 | if isinstance(other, self.__class__): |
| 66 | return self.ip_addr == other.ip_addr |
| 67 | elif hasattr(other, "ip4") and hasattr(other, "ip6"): |
| 68 | # vl_api_address_union_t |
Paul Vinciguerra | a3aaa61 | 2018-12-09 08:52:14 -0800 | [diff] [blame] | 69 | if 4 == self.version: |
Neale Ranns | 9db6ada | 2019-11-08 12:42:31 +0000 | [diff] [blame] | 70 | return self.ip_addr == other.ip4 |
Neale Ranns | c0a9314 | 2018-09-05 15:42:26 -0700 | [diff] [blame] | 71 | else: |
Neale Ranns | 9db6ada | 2019-11-08 12:42:31 +0000 | [diff] [blame] | 72 | return self.ip_addr == other.ip6 |
Neale Ranns | c0a9314 | 2018-09-05 15:42:26 -0700 | [diff] [blame] | 73 | else: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 74 | raise Exception( |
| 75 | "Comparing VppIpAddressUnions:%s with incomparable type: %s", |
| 76 | self, |
| 77 | other, |
| 78 | ) |
Neale Ranns | 9db6ada | 2019-11-08 12:42:31 +0000 | [diff] [blame] | 79 | |
| 80 | def __ne__(self, other): |
| 81 | return not (self == other) |
Neale Ranns | c0a9314 | 2018-09-05 15:42:26 -0700 | [diff] [blame] | 82 | |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 83 | def __str__(self): |
| 84 | return str(self.ip_addr) |
| 85 | |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 86 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 87 | class VppIpMPrefix: |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 88 | def __init__(self, saddr, gaddr, glen): |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 89 | self.saddr = saddr |
| 90 | self.gaddr = gaddr |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 91 | self.glen = glen |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 92 | if ip_address(self.saddr).version != ip_address(self.gaddr).version: |
| 93 | raise ValueError( |
| 94 | "Source and group addresses must be of the same address family." |
| 95 | ) |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 96 | |
| 97 | def encode(self): |
Paul Vinciguerra | e64e5ff | 2020-04-28 00:27:38 -0400 | [diff] [blame] | 98 | return { |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 99 | "af": ip_address(self.gaddr).vapi_af, |
| 100 | "grp_address": {ip_address(self.gaddr).vapi_af_name: self.gaddr}, |
| 101 | "src_address": {ip_address(self.saddr).vapi_af_name: self.saddr}, |
| 102 | "grp_address_length": self.glen, |
Paul Vinciguerra | e64e5ff | 2020-04-28 00:27:38 -0400 | [diff] [blame] | 103 | } |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 104 | |
| 105 | @property |
| 106 | def length(self): |
| 107 | return self.glen |
| 108 | |
| 109 | @property |
| 110 | def version(self): |
Neale Ranns | efd7bc2 | 2019-11-11 08:32:34 +0000 | [diff] [blame] | 111 | return ip_address(self.gaddr).version |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 112 | |
| 113 | def __str__(self): |
| 114 | return "(%s,%s)/%d" % (self.saddr, self.gaddr, self.glen) |
| 115 | |
| 116 | def __eq__(self, other): |
| 117 | if isinstance(other, self.__class__): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 118 | return ( |
| 119 | self.glen == other.glen |
| 120 | and self.saddr == other.gaddr |
| 121 | and self.saddr == other.saddr |
| 122 | ) |
| 123 | elif ( |
| 124 | hasattr(other, "grp_address_length") |
| 125 | and hasattr(other, "grp_address") |
| 126 | and hasattr(other, "src_address") |
| 127 | ): |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 128 | # vl_api_mprefix_t |
Neale Ranns | efd7bc2 | 2019-11-11 08:32:34 +0000 | [diff] [blame] | 129 | if 4 == self.version: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 130 | return ( |
| 131 | self.glen == other.grp_address_length |
| 132 | and self.gaddr == str(other.grp_address.ip4) |
| 133 | and self.saddr == str(other.src_address.ip4) |
| 134 | ) |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 135 | else: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 136 | return ( |
| 137 | self.glen == other.grp_address_length |
| 138 | and self.gaddr == str(other.grp_address.ip6) |
| 139 | and self.saddr == str(other.src_address.ip6) |
| 140 | ) |
Paul Vinciguerra | e64e5ff | 2020-04-28 00:27:38 -0400 | [diff] [blame] | 141 | return NotImplemented |
Jakub Grajciar | 2df2f75 | 2020-12-01 11:23:44 +0100 | [diff] [blame] | 142 | |
| 143 | |
| 144 | class VppIpPuntPolicer(VppObject): |
| 145 | def __init__(self, test, policer_index, is_ip6=False): |
| 146 | self._test = test |
| 147 | self._policer_index = policer_index |
| 148 | self._is_ip6 = is_ip6 |
| 149 | |
| 150 | def add_vpp_config(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 151 | self._test.vapi.ip_punt_police( |
| 152 | policer_index=self._policer_index, is_ip6=self._is_ip6, is_add=True |
| 153 | ) |
Jakub Grajciar | 2df2f75 | 2020-12-01 11:23:44 +0100 | [diff] [blame] | 154 | |
| 155 | def remove_vpp_config(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 156 | self._test.vapi.ip_punt_police( |
| 157 | policer_index=self._policer_index, is_ip6=self._is_ip6, is_add=False |
| 158 | ) |
Jakub Grajciar | 2df2f75 | 2020-12-01 11:23:44 +0100 | [diff] [blame] | 159 | |
| 160 | def query_vpp_config(self): |
| 161 | NotImplemented |
| 162 | |
| 163 | |
| 164 | class VppIpPuntRedirect(VppObject): |
| 165 | def __init__(self, test, rx_index, tx_index, nh_addr): |
| 166 | self._test = test |
| 167 | self._rx_index = rx_index |
| 168 | self._tx_index = tx_index |
| 169 | self._nh_addr = ip_address(nh_addr) |
| 170 | |
| 171 | def encode(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 172 | return { |
| 173 | "rx_sw_if_index": self._rx_index, |
| 174 | "tx_sw_if_index": self._tx_index, |
| 175 | "nh": self._nh_addr, |
| 176 | } |
Jakub Grajciar | 2df2f75 | 2020-12-01 11:23:44 +0100 | [diff] [blame] | 177 | |
| 178 | def add_vpp_config(self): |
| 179 | self._test.vapi.ip_punt_redirect(punt=self.encode(), is_add=True) |
| 180 | self._test.registry.register(self, self._test.logger) |
Neale Ranns | aa7cfd0 | 2022-03-24 12:28:42 +0000 | [diff] [blame] | 181 | return self |
Jakub Grajciar | 2df2f75 | 2020-12-01 11:23:44 +0100 | [diff] [blame] | 182 | |
| 183 | def remove_vpp_config(self): |
| 184 | self._test.vapi.ip_punt_redirect(punt=self.encode(), is_add=False) |
| 185 | |
| 186 | def get_vpp_config(self): |
| 187 | is_ipv6 = True if self._nh_addr.version == 6 else False |
| 188 | return self._test.vapi.ip_punt_redirect_dump( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 189 | sw_if_index=self._rx_index, is_ipv6=is_ipv6 |
| 190 | ) |
Jakub Grajciar | 2df2f75 | 2020-12-01 11:23:44 +0100 | [diff] [blame] | 191 | |
| 192 | def query_vpp_config(self): |
| 193 | if self.get_vpp_config(): |
| 194 | return True |
| 195 | return False |
Neale Ranns | 8f5fef2 | 2020-12-21 08:29:34 +0000 | [diff] [blame] | 196 | |
| 197 | |
| 198 | class VppIpPathMtu(VppObject): |
| 199 | def __init__(self, test, nh, pmtu, table_id=0): |
| 200 | self._test = test |
| 201 | self.nh = nh |
| 202 | self.pmtu = pmtu |
| 203 | self.table_id = table_id |
| 204 | |
| 205 | def add_vpp_config(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 206 | self._test.vapi.ip_path_mtu_update( |
| 207 | pmtu={"nh": self.nh, "table_id": self.table_id, "path_mtu": self.pmtu} |
| 208 | ) |
Neale Ranns | 8f5fef2 | 2020-12-21 08:29:34 +0000 | [diff] [blame] | 209 | self._test.registry.register(self, self._test.logger) |
| 210 | return self |
| 211 | |
| 212 | def modify(self, pmtu): |
| 213 | self.pmtu = pmtu |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 214 | self._test.vapi.ip_path_mtu_update( |
| 215 | pmtu={"nh": self.nh, "table_id": self.table_id, "path_mtu": self.pmtu} |
| 216 | ) |
Neale Ranns | 8f5fef2 | 2020-12-21 08:29:34 +0000 | [diff] [blame] | 217 | return self |
| 218 | |
| 219 | def remove_vpp_config(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 220 | self._test.vapi.ip_path_mtu_update( |
| 221 | pmtu={"nh": self.nh, "table_id": self.table_id, "path_mtu": 0} |
| 222 | ) |
Neale Ranns | 8f5fef2 | 2020-12-21 08:29:34 +0000 | [diff] [blame] | 223 | |
| 224 | def query_vpp_config(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 225 | ds = list(self._test.vapi.vpp.details_iter(self._test.vapi.ip_path_mtu_get)) |
Neale Ranns | 8f5fef2 | 2020-12-21 08:29:34 +0000 | [diff] [blame] | 226 | |
| 227 | for d in ds: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 228 | if ( |
| 229 | self.nh == str(d.pmtu.nh) |
| 230 | and self.table_id == d.pmtu.table_id |
| 231 | and self.pmtu == d.pmtu.path_mtu |
| 232 | ): |
Neale Ranns | 8f5fef2 | 2020-12-21 08:29:34 +0000 | [diff] [blame] | 233 | return True |
| 234 | return False |
| 235 | |
| 236 | def object_id(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 237 | return "ip-path-mtu-%d-%s-%d" % (self.table_id, self.nh, self.pmtu) |
Neale Ranns | 8f5fef2 | 2020-12-21 08:29:34 +0000 | [diff] [blame] | 238 | |
| 239 | def __str__(self): |
| 240 | return self.object_id() |