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 |
Neale Ranns | c0a9314 | 2018-09-05 15:42:26 -0700 | [diff] [blame] | 8 | from socket import AF_INET, AF_INET6 |
Ole Troan | 0685da4 | 2018-10-16 14:42:50 +0200 | [diff] [blame] | 9 | from vpp_papi import VppEnum |
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 | |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 27 | INVALID_INDEX = 0xffffffff |
| 28 | |
| 29 | |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 30 | class VppIpAddressUnion(): |
| 31 | def __init__(self, addr): |
| 32 | self.addr = addr |
Paul Vinciguerra | 9e31595 | 2019-01-29 11:51:44 -0800 | [diff] [blame] | 33 | self.ip_addr = ip_address(text_type(self.addr)) |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 34 | |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 35 | def encode(self): |
Paul Vinciguerra | a3aaa61 | 2018-12-09 08:52:14 -0800 | [diff] [blame] | 36 | if self.version == 6: |
Ole Troan | 8c8acc0 | 2018-11-27 10:05:23 +0100 | [diff] [blame] | 37 | return {'ip6': self.ip_addr.packed} |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 38 | else: |
Ole Troan | 8c8acc0 | 2018-11-27 10:05:23 +0100 | [diff] [blame] | 39 | return {'ip4': self.ip_addr.packed} |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 40 | |
Neale Ranns | c0a9314 | 2018-09-05 15:42:26 -0700 | [diff] [blame] | 41 | @property |
| 42 | def version(self): |
| 43 | return self.ip_addr.version |
| 44 | |
| 45 | @property |
| 46 | def address(self): |
| 47 | return self.addr |
| 48 | |
| 49 | @property |
| 50 | def length(self): |
Paul Vinciguerra | a3aaa61 | 2018-12-09 08:52:14 -0800 | [diff] [blame] | 51 | return self.ip_addr.max_prefixlen |
Neale Ranns | c0a9314 | 2018-09-05 15:42:26 -0700 | [diff] [blame] | 52 | |
| 53 | @property |
| 54 | def bytes(self): |
| 55 | return self.ip_addr.packed |
| 56 | |
| 57 | def __eq__(self, other): |
| 58 | if isinstance(other, self.__class__): |
| 59 | return self.ip_addr == other.ip_addr |
| 60 | elif hasattr(other, "ip4") and hasattr(other, "ip6"): |
| 61 | # vl_api_address_union_t |
Paul Vinciguerra | a3aaa61 | 2018-12-09 08:52:14 -0800 | [diff] [blame] | 62 | if 4 == self.version: |
Ole Troan | 8c8acc0 | 2018-11-27 10:05:23 +0100 | [diff] [blame] | 63 | return self.ip_addr.packed == other.ip4 |
Neale Ranns | c0a9314 | 2018-09-05 15:42:26 -0700 | [diff] [blame] | 64 | else: |
Ole Troan | 8c8acc0 | 2018-11-27 10:05:23 +0100 | [diff] [blame] | 65 | return self.ip_addr.packed == other.ip6 |
Neale Ranns | c0a9314 | 2018-09-05 15:42:26 -0700 | [diff] [blame] | 66 | else: |
Paul Vinciguerra | a3aaa61 | 2018-12-09 08:52:14 -0800 | [diff] [blame] | 67 | _log.error("Comparing VppIpAddressUnions:%s" |
| 68 | " with incomparable type: %s", |
| 69 | self, other) |
| 70 | return NotImplemented |
Neale Ranns | c0a9314 | 2018-09-05 15:42:26 -0700 | [diff] [blame] | 71 | |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 72 | |
| 73 | class VppIpAddress(): |
| 74 | def __init__(self, addr): |
| 75 | self.addr = VppIpAddressUnion(addr) |
| 76 | |
| 77 | def encode(self): |
Paul Vinciguerra | a3aaa61 | 2018-12-09 08:52:14 -0800 | [diff] [blame] | 78 | if self.addr.version == 6: |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 79 | return { |
Ole Troan | 0685da4 | 2018-10-16 14:42:50 +0200 | [diff] [blame] | 80 | 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP6, |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 81 | 'un': self.addr.encode() |
| 82 | } |
| 83 | else: |
| 84 | return { |
Ole Troan | 0685da4 | 2018-10-16 14:42:50 +0200 | [diff] [blame] | 85 | 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP4, |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 86 | 'un': self.addr.encode() |
| 87 | } |
| 88 | |
Neale Ranns | c0a9314 | 2018-09-05 15:42:26 -0700 | [diff] [blame] | 89 | def __eq__(self, other): |
| 90 | if isinstance(other, self.__class__): |
| 91 | return self.addr == other.addr |
| 92 | elif hasattr(other, "af") and hasattr(other, "un"): |
| 93 | # a vp_api_address_t |
Paul Vinciguerra | a3aaa61 | 2018-12-09 08:52:14 -0800 | [diff] [blame] | 94 | if 4 == self.version: |
Ole Troan | 0685da4 | 2018-10-16 14:42:50 +0200 | [diff] [blame] | 95 | return other.af == \ |
| 96 | VppEnum.vl_api_address_family_t.ADDRESS_IP4 and \ |
Neale Ranns | c0a9314 | 2018-09-05 15:42:26 -0700 | [diff] [blame] | 97 | other.un == self.addr |
| 98 | else: |
Ole Troan | 0685da4 | 2018-10-16 14:42:50 +0200 | [diff] [blame] | 99 | return other.af == \ |
| 100 | VppEnum.vl_api_address_family_t.ADDRESS_IP6 and \ |
Neale Ranns | c0a9314 | 2018-09-05 15:42:26 -0700 | [diff] [blame] | 101 | other.un == self.addr |
| 102 | else: |
Paul Vinciguerra | a3aaa61 | 2018-12-09 08:52:14 -0800 | [diff] [blame] | 103 | _log.error( |
| 104 | "Comparing VppIpAddress:<%s> %s with incomparable " |
| 105 | "type: <%s> %s", |
| 106 | self.__class__.__name__, self, |
| 107 | other.__class__.__name__, other) |
| 108 | return NotImplemented |
Neale Ranns | c0a9314 | 2018-09-05 15:42:26 -0700 | [diff] [blame] | 109 | |
| 110 | def __ne__(self, other): |
| 111 | return not (self == other) |
| 112 | |
| 113 | def __str__(self): |
| 114 | return self.address |
| 115 | |
| 116 | @property |
| 117 | def bytes(self): |
| 118 | return self.addr.bytes |
| 119 | |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 120 | @property |
| 121 | def address(self): |
| 122 | return self.addr.address |
| 123 | |
Neale Ranns | c0a9314 | 2018-09-05 15:42:26 -0700 | [diff] [blame] | 124 | @property |
| 125 | def length(self): |
| 126 | return self.addr.length |
| 127 | |
| 128 | @property |
| 129 | def version(self): |
| 130 | return self.addr.version |
| 131 | |
| 132 | @property |
| 133 | def is_ip6(self): |
| 134 | return (self.version == 6) |
| 135 | |
| 136 | @property |
| 137 | def af(self): |
| 138 | if self.version == 6: |
| 139 | return AF_INET6 |
| 140 | else: |
| 141 | return AF_INET |
| 142 | |
| 143 | @property |
| 144 | def dpo_proto(self): |
Paul Vinciguerra | a3aaa61 | 2018-12-09 08:52:14 -0800 | [diff] [blame] | 145 | if self.version == 6: |
Neale Ranns | c0a9314 | 2018-09-05 15:42:26 -0700 | [diff] [blame] | 146 | return DpoProto.DPO_PROTO_IP6 |
| 147 | else: |
| 148 | return DpoProto.DPO_PROTO_IP4 |
| 149 | |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 150 | |
| 151 | class VppIpPrefix(): |
| 152 | def __init__(self, addr, len): |
| 153 | self.addr = VppIpAddress(addr) |
| 154 | self.len = len |
| 155 | |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 156 | def encode(self): |
| 157 | return {'address': self.addr.encode(), |
| 158 | 'address_length': self.len} |
| 159 | |
| 160 | @property |
| 161 | def address(self): |
| 162 | return self.addr.address |
| 163 | |
Neale Ranns | c0a9314 | 2018-09-05 15:42:26 -0700 | [diff] [blame] | 164 | @property |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 165 | def bytes(self): |
| 166 | return self.addr.bytes |
| 167 | |
| 168 | @property |
Neale Ranns | c0a9314 | 2018-09-05 15:42:26 -0700 | [diff] [blame] | 169 | def length(self): |
| 170 | return self.len |
| 171 | |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 172 | @property |
| 173 | def is_ip6(self): |
| 174 | return self.addr.is_ip6 |
| 175 | |
Neale Ranns | c0a9314 | 2018-09-05 15:42:26 -0700 | [diff] [blame] | 176 | def __str__(self): |
| 177 | return "%s/%d" % (self.address, self.length) |
| 178 | |
| 179 | def __eq__(self, other): |
| 180 | if isinstance(other, self.__class__): |
| 181 | return (self.len == other.len and self.addr == other.addr) |
| 182 | elif hasattr(other, "address") and hasattr(other, "address_length"): |
| 183 | # vl_api_prefix_t |
| 184 | return self.len == other.address_length and \ |
Paul Vinciguerra | a3aaa61 | 2018-12-09 08:52:14 -0800 | [diff] [blame] | 185 | self.addr == other.address |
Neale Ranns | c0a9314 | 2018-09-05 15:42:26 -0700 | [diff] [blame] | 186 | else: |
Paul Vinciguerra | a3aaa61 | 2018-12-09 08:52:14 -0800 | [diff] [blame] | 187 | _log.error( |
| 188 | "Comparing VppIpPrefix:%s with incomparable type: %s" % |
| 189 | (self, other)) |
| 190 | return NotImplemented |
Neale Ranns | c0a9314 | 2018-09-05 15:42:26 -0700 | [diff] [blame] | 191 | |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 192 | |
| 193 | class VppIpMPrefix(): |
| 194 | def __init__(self, saddr, gaddr, len): |
| 195 | self.saddr = saddr |
| 196 | self.gaddr = gaddr |
| 197 | self.len = len |
Paul Vinciguerra | 9e31595 | 2019-01-29 11:51:44 -0800 | [diff] [blame] | 198 | self.ip_saddr = ip_address(text_type(self.saddr)) |
| 199 | self.ip_gaddr = ip_address(text_type(self.gaddr)) |
Paul Vinciguerra | a3aaa61 | 2018-12-09 08:52:14 -0800 | [diff] [blame] | 200 | if self.ip_saddr.version != self.ip_gaddr.version: |
| 201 | raise ValueError('Source and group addresses must be of the ' |
| 202 | 'same address family.') |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 203 | |
| 204 | def encode(self): |
Paul Vinciguerra | a3aaa61 | 2018-12-09 08:52:14 -0800 | [diff] [blame] | 205 | if 6 == self.ip_saddr.version: |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 206 | prefix = { |
Ole Troan | 0685da4 | 2018-10-16 14:42:50 +0200 | [diff] [blame] | 207 | 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP6, |
Ole Troan | 8c8acc0 | 2018-11-27 10:05:23 +0100 | [diff] [blame] | 208 | 'grp_address': {'ip6': self.ip_gaddr.packed}, |
| 209 | 'src_address': {'ip6': self.ip_saddr.packed}, |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 210 | 'grp_address_length': self.len, |
| 211 | } |
| 212 | else: |
| 213 | prefix = { |
Ole Troan | 0685da4 | 2018-10-16 14:42:50 +0200 | [diff] [blame] | 214 | 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP4, |
Ole Troan | 8c8acc0 | 2018-11-27 10:05:23 +0100 | [diff] [blame] | 215 | 'grp_address': {'ip4': self.ip_gaddr.packed}, |
| 216 | 'src_address': {'ip4': self.ip_saddr.packed}, |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 217 | 'grp_address_length': self.len, |
| 218 | } |
| 219 | return prefix |