7 from ipaddress import ip_address
8 from socket import AF_INET, AF_INET6
9 from vpp_papi import VppEnum
11 _log = logging.getLogger(__name__)
18 DPO_PROTO_ETHERNET = 3
23 INVALID_INDEX = 0xffffffff
26 class VppIpAddressUnion():
27 def __init__(self, addr):
29 self.ip_addr = ip_address(unicode(self.addr))
33 return {'ip6': self.ip_addr.packed}
35 return {'ip4': self.ip_addr.packed}
39 return self.ip_addr.version
47 return self.ip_addr.max_prefixlen
51 return self.ip_addr.packed
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
59 return self.ip_addr.packed == other.ip4
61 return self.ip_addr.packed == other.ip6
63 _log.error("Comparing VppIpAddressUnions:%s"
64 " with incomparable type: %s",
70 def __init__(self, addr):
71 self.addr = VppIpAddressUnion(addr)
74 if self.addr.version == 6:
76 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP6,
77 'un': self.addr.encode()
81 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP4,
82 'un': self.addr.encode()
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"):
92 VppEnum.vl_api_address_family_t.ADDRESS_IP4 and \
96 VppEnum.vl_api_address_family_t.ADDRESS_IP6 and \
100 "Comparing VppIpAddress:<%s> %s with incomparable "
102 self.__class__.__name__, self,
103 other.__class__.__name__, other)
104 return NotImplemented
106 def __ne__(self, other):
107 return not (self == other)
114 return self.addr.bytes
118 return self.addr.address
122 return self.addr.length
126 return self.addr.version
130 return (self.version == 6)
134 if self.version == 6:
141 if self.version == 6:
142 return DpoProto.DPO_PROTO_IP6
144 return DpoProto.DPO_PROTO_IP4
148 def __init__(self, addr, len):
149 self.addr = VppIpAddress(addr)
153 return {'address': self.addr.encode(),
154 'address_length': self.len}
158 return self.addr.address
162 return self.addr.bytes
170 return self.addr.is_ip6
173 return "%s/%d" % (self.address, self.length)
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"):
180 return self.len == other.address_length and \
181 self.addr == other.address
184 "Comparing VppIpPrefix:%s with incomparable type: %s" %
186 return NotImplemented
189 class VppIpMPrefix():
190 def __init__(self, saddr, gaddr, len):
194 self.ip_saddr = ip_address(unicode(self.saddr))
195 self.ip_gaddr = ip_address(unicode(self.gaddr))
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.')
201 if 6 == self.ip_saddr.version:
203 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP6,
204 'grp_address': {'ip6': self.ip_gaddr.packed},
205 'src_address': {'ip6': self.ip_saddr.packed},
206 'grp_address_length': self.len,
210 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP4,
211 'grp_address': {'ip4': self.ip_gaddr.packed},
212 'src_address': {'ip4': self.ip_saddr.packed},
213 'grp_address_length': self.len,