6 from ipaddress import ip_address
7 from socket import AF_INET, AF_INET6
8 from vpp_papi import VppEnum
15 DPO_PROTO_ETHERNET = 3
20 INVALID_INDEX = 0xffffffff
23 class VppIpAddressUnion():
24 def __init__(self, addr):
26 self.ip_addr = ip_address(unicode(self.addr))
32 'address': self.ip_addr.packed
38 'address': self.ip_addr.packed
44 return self.ip_addr.version
59 return self.ip_addr.packed
61 def __eq__(self, other):
62 if isinstance(other, self.__class__):
63 return self.ip_addr == other.ip_addr
64 elif hasattr(other, "ip4") and hasattr(other, "ip6"):
65 # vl_api_address_union_t
67 return self.ip_addr.packed == other.ip4.address
69 return self.ip_addr.packed == other.ip6.address
71 raise Exception("Comparing VppIpAddresUnions:%s"
72 " with unknown type: %s" %
79 def __init__(self, addr):
80 self.addr = VppIpAddressUnion(addr)
83 if self.addr.version is 6:
85 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP6,
86 'un': self.addr.encode()
90 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP4,
91 'un': self.addr.encode()
94 def __eq__(self, other):
95 if isinstance(other, self.__class__):
96 return self.addr == other.addr
97 elif hasattr(other, "af") and hasattr(other, "un"):
101 VppEnum.vl_api_address_family_t.ADDRESS_IP4 and \
102 other.un == self.addr
105 VppEnum.vl_api_address_family_t.ADDRESS_IP6 and \
106 other.un == self.addr
108 raise Exception("Comparing VppIpAddress:%s with unknown type: %s" %
112 def __ne__(self, other):
113 return not (self == other)
120 return self.addr.bytes
124 return self.addr.bytes
128 return self.addr.address
132 return self.addr.length
136 return self.addr.version
140 return (self.version == 6)
144 if self.version == 6:
151 if self.version is 6:
152 return DpoProto.DPO_PROTO_IP6
154 return DpoProto.DPO_PROTO_IP4
158 def __init__(self, addr, len):
159 self.addr = VppIpAddress(addr)
162 def __eq__(self, other):
163 if self.addr == other.addr and self.len == other.len:
168 return {'address': self.addr.encode(),
169 'address_length': self.len}
173 return self.addr.address
177 return self.addr.bytes
185 return self.addr.is_ip6
188 return "%s/%d" % (self.address, self.length)
190 def __eq__(self, other):
191 if isinstance(other, self.__class__):
192 return (self.len == other.len and self.addr == other.addr)
193 elif hasattr(other, "address") and hasattr(other, "address_length"):
195 return self.len == other.address_length and \
196 self.addr == other.address
198 raise Exception("Comparing VppIpPrefix:%s with unknown type: %s" %
203 class VppIp6Prefix():
204 def __init__(self, prefix, prefixlen):
205 self.ip_prefix = ip_address(unicode(prefix))
206 self.prefixlen = prefixlen
209 return {'prefix': {'address': self.ip_prefix.packed},
210 'len': self.prefixlen}
213 class VppIp4Prefix(VppIp6Prefix):
217 class VppIpMPrefix():
218 def __init__(self, saddr, gaddr, len):
222 self.ip_saddr = ip_address(unicode(self.saddr))
223 self.ip_gaddr = ip_address(unicode(self.gaddr))
227 if 6 is self.ip_saddr.version:
229 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP6,
232 'address': self.ip_gaddr.packed
237 'address': self.ip_saddr.packed
240 'grp_address_length': self.len,
244 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP4,
247 'address': self.ip_gaddr.packed
252 'address': self.ip_saddr.packed
255 'grp_address_length': self.len,