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))
30 return {'ip6': self.ip_addr.packed}
32 return {'ip4': self.ip_addr.packed}
36 return self.ip_addr.version
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 raise Exception("Comparing VppIpAddresUnions:%s"
64 " with unknown type: %s" %
71 def __init__(self, addr):
72 self.addr = VppIpAddressUnion(addr)
75 if self.addr.version is 6:
77 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP6,
78 'un': self.addr.encode()
82 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP4,
83 'un': self.addr.encode()
86 def __eq__(self, other):
87 if isinstance(other, self.__class__):
88 return self.addr == other.addr
89 elif hasattr(other, "af") and hasattr(other, "un"):
93 VppEnum.vl_api_address_family_t.ADDRESS_IP4 and \
97 VppEnum.vl_api_address_family_t.ADDRESS_IP6 and \
100 raise Exception("Comparing VppIpAddress:%s with unknown type: %s" %
104 def __ne__(self, other):
105 return not (self == other)
112 return self.addr.bytes
116 return self.addr.bytes
120 return self.addr.address
124 return self.addr.length
128 return self.addr.version
132 return (self.version == 6)
136 if self.version == 6:
143 if self.version is 6:
144 return DpoProto.DPO_PROTO_IP6
146 return DpoProto.DPO_PROTO_IP4
150 def __init__(self, addr, len):
151 self.addr = VppIpAddress(addr)
154 def __eq__(self, other):
155 if self.addr == other.addr and self.len == other.len:
160 return {'address': self.addr.encode(),
161 'address_length': self.len}
165 return self.addr.address
169 return self.addr.bytes
177 return self.addr.is_ip6
180 return "%s/%d" % (self.address, self.length)
182 def __eq__(self, other):
183 if isinstance(other, self.__class__):
184 return (self.len == other.len and self.addr == other.addr)
185 elif hasattr(other, "address") and hasattr(other, "address_length"):
187 return self.len == other.address_length and \
188 self.addr == other.address
190 raise Exception("Comparing VppIpPrefix:%s with unknown type: %s" %
195 class VppIpMPrefix():
196 def __init__(self, saddr, gaddr, len):
200 self.ip_saddr = ip_address(unicode(self.saddr))
201 self.ip_gaddr = ip_address(unicode(self.gaddr))
205 if 6 is 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,