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 | 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 | |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 37 | class VppIpAddressUnion(): |
| 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: |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 44 | return {'ip6': self.ip_addr} |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 45 | else: |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [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: |
Ole Troan | 8c8acc0 | 2018-11-27 10:05:23 +0100 | [diff] [blame] | 70 | return self.ip_addr.packed == other.ip4 |
Neale Ranns | c0a9314 | 2018-09-05 15:42:26 -0700 | [diff] [blame] | 71 | else: |
Ole Troan | 8c8acc0 | 2018-11-27 10:05:23 +0100 | [diff] [blame] | 72 | return self.ip_addr.packed == other.ip6 |
Neale Ranns | c0a9314 | 2018-09-05 15:42:26 -0700 | [diff] [blame] | 73 | else: |
Paul Vinciguerra | a3aaa61 | 2018-12-09 08:52:14 -0800 | [diff] [blame] | 74 | _log.error("Comparing VppIpAddressUnions:%s" |
| 75 | " with incomparable type: %s", |
| 76 | self, other) |
| 77 | return NotImplemented |
Neale Ranns | c0a9314 | 2018-09-05 15:42:26 -0700 | [diff] [blame] | 78 | |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 79 | def __str__(self): |
| 80 | return str(self.ip_addr) |
| 81 | |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 82 | |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 83 | class VppIpMPrefix(): |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 84 | def __init__(self, saddr, gaddr, glen): |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 85 | self.saddr = saddr |
| 86 | self.gaddr = gaddr |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 87 | self.glen = glen |
Neale Ranns | efd7bc2 | 2019-11-11 08:32:34 +0000 | [diff] [blame] | 88 | if ip_address(self.saddr).version != \ |
| 89 | ip_address(self.gaddr).version: |
Paul Vinciguerra | a3aaa61 | 2018-12-09 08:52:14 -0800 | [diff] [blame] | 90 | raise ValueError('Source and group addresses must be of the ' |
| 91 | 'same address family.') |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 92 | |
| 93 | def encode(self): |
Neale Ranns | efd7bc2 | 2019-11-11 08:32:34 +0000 | [diff] [blame] | 94 | if 6 == self.version: |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 95 | prefix = { |
Ole Troan | 0685da4 | 2018-10-16 14:42:50 +0200 | [diff] [blame] | 96 | 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP6, |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 97 | 'grp_address': { |
| 98 | 'ip6': self.gaddr |
| 99 | }, |
| 100 | 'src_address': { |
| 101 | 'ip6': self.saddr |
| 102 | }, |
| 103 | 'grp_address_length': self.glen, |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 104 | } |
| 105 | else: |
| 106 | prefix = { |
Ole Troan | 0685da4 | 2018-10-16 14:42:50 +0200 | [diff] [blame] | 107 | 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP4, |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 108 | 'grp_address': { |
| 109 | 'ip4': self.gaddr |
| 110 | }, |
| 111 | 'src_address': { |
| 112 | 'ip4': self.saddr |
| 113 | }, |
| 114 | 'grp_address_length': self.glen, |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 115 | } |
| 116 | return prefix |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 117 | |
| 118 | @property |
| 119 | def length(self): |
| 120 | return self.glen |
| 121 | |
| 122 | @property |
| 123 | def version(self): |
Neale Ranns | efd7bc2 | 2019-11-11 08:32:34 +0000 | [diff] [blame] | 124 | return ip_address(self.gaddr).version |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 125 | |
| 126 | def __str__(self): |
| 127 | return "(%s,%s)/%d" % (self.saddr, self.gaddr, self.glen) |
| 128 | |
| 129 | def __eq__(self, other): |
| 130 | if isinstance(other, self.__class__): |
| 131 | return (self.glen == other.glen and |
Neale Ranns | efd7bc2 | 2019-11-11 08:32:34 +0000 | [diff] [blame] | 132 | self.saddr == other.gaddr and |
| 133 | self.saddr == other.saddr) |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 134 | elif (hasattr(other, "grp_address_length") and |
| 135 | hasattr(other, "grp_address") and |
| 136 | hasattr(other, "src_address")): |
| 137 | # vl_api_mprefix_t |
Neale Ranns | efd7bc2 | 2019-11-11 08:32:34 +0000 | [diff] [blame] | 138 | if 4 == self.version: |
| 139 | return (self.glen == other.grp_address_length and |
| 140 | self.gaddr == str(other.grp_address.ip4) and |
| 141 | self.saddr == str(other.src_address.ip4)) |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 142 | else: |
| 143 | return (self.glen == other.grp_address_length and |
Neale Ranns | efd7bc2 | 2019-11-11 08:32:34 +0000 | [diff] [blame] | 144 | self.gaddr == str(other.grp_address.ip6) and |
| 145 | self.saddr == str(other.src_address.ip6)) |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 146 | else: |
| 147 | raise Exception("Comparing VppIpPrefix:%s with unknown type: %s" % |
| 148 | (self, other)) |
| 149 | return False |