7 from ipaddress import ip_address
8 from socket import AF_INET, AF_INET6
9 from vpp_papi import VppEnum
15 _log = logging.getLogger(__name__)
22 DPO_PROTO_ETHERNET = 3
27 INVALID_INDEX = 0xffffffff
30 class VppIpAddressUnion():
31 def __init__(self, addr):
33 self.ip_addr = ip_address(text_type(self.addr))
37 return {'ip6': self.ip_addr.packed}
39 return {'ip4': self.ip_addr.packed}
43 return self.ip_addr.version
51 return self.ip_addr.max_prefixlen
55 return self.ip_addr.packed
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
63 return self.ip_addr.packed == other.ip4
65 return self.ip_addr.packed == other.ip6
67 _log.error("Comparing VppIpAddressUnions:%s"
68 " with incomparable type: %s",
74 def __init__(self, addr):
75 self.addr = VppIpAddressUnion(addr)
78 if self.addr.version == 6:
80 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP6,
81 'un': self.addr.encode()
85 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP4,
86 'un': self.addr.encode()
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"):
96 VppEnum.vl_api_address_family_t.ADDRESS_IP4 and \
100 VppEnum.vl_api_address_family_t.ADDRESS_IP6 and \
101 other.un == self.addr
104 "Comparing VppIpAddress:<%s> %s with incomparable "
106 self.__class__.__name__, self,
107 other.__class__.__name__, other)
108 return NotImplemented
110 def __ne__(self, other):
111 return not (self == other)
118 return self.addr.bytes
122 return self.addr.address
126 return self.addr.length
130 return self.addr.version
134 return (self.version == 6)
138 if self.version == 6:
145 if self.version == 6:
146 return DpoProto.DPO_PROTO_IP6
148 return DpoProto.DPO_PROTO_IP4
152 def __init__(self, addr, len):
153 self.addr = VppIpAddress(addr)
157 return {'address': self.addr.encode(),
158 'address_length': self.len}
162 return self.addr.address
166 return self.addr.bytes
174 return self.addr.is_ip6
177 return "%s/%d" % (self.address, self.length)
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"):
184 return self.len == other.address_length and \
185 self.addr == other.address
188 "Comparing VppIpPrefix:%s with incomparable type: %s" %
190 return NotImplemented
193 class VppIpMPrefix():
194 def __init__(self, saddr, gaddr, len):
198 self.ip_saddr = ip_address(text_type(self.saddr))
199 self.ip_gaddr = ip_address(text_type(self.gaddr))
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.')
205 if 6 == self.ip_saddr.version:
207 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP6,
208 'grp_address': {'ip6': self.ip_gaddr.packed},
209 'src_address': {'ip6': self.ip_saddr.packed},
210 'grp_address_length': self.len,
214 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP4,
215 'grp_address': {'ip4': self.ip_gaddr.packed},
216 'src_address': {'ip4': self.ip_saddr.packed},
217 'grp_address_length': self.len,