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.address
128 return self.addr.length
132 return self.addr.version
136 return (self.version == 6)
140 if self.version == 6:
147 if self.version is 6:
148 return DpoProto.DPO_PROTO_IP6
150 return DpoProto.DPO_PROTO_IP4
154 def __init__(self, addr, len):
155 self.addr = VppIpAddress(addr)
158 def __eq__(self, other):
159 if self.addr == other.addr and self.len == other.len:
164 return {'address': self.addr.encode(),
165 'address_length': self.len}
169 return self.addr.address
176 return "%s/%d" % (self.address, self.length)
178 def __eq__(self, other):
179 if isinstance(other, self.__class__):
180 return (self.len == other.len and self.addr == other.addr)
181 elif hasattr(other, "address") and hasattr(other, "address_length"):
183 return self.len == other.address_length and \
184 self.addr == other.address
186 raise Exception("Comparing VppIpPrefix:%s with unknown type: %s" %
191 class VppIpMPrefix():
192 def __init__(self, saddr, gaddr, len):
196 self.ip_saddr = ip_address(unicode(self.saddr))
197 self.ip_gaddr = ip_address(unicode(self.gaddr))
201 if 6 is self.ip_saddr.version:
203 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP6,
206 'address': self.ip_gaddr.packed
211 'address': self.ip_saddr.packed
214 'grp_address_length': self.len,
218 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP4,
221 'address': self.ip_gaddr.packed
226 'address': self.ip_saddr.packed
229 'grp_address_length': self.len,