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