blob: e92c91943db8d97c24b960ee4465fdc7d6392f9b [file] [log] [blame]
Neale Rannsd0df49f2018-08-08 01:06:40 -07001"""
2 IP Types
3
4"""
5
6from ipaddress import ip_address
Neale Rannsc0a93142018-09-05 15:42:26 -07007from socket import AF_INET, AF_INET6
Ole Troan0685da42018-10-16 14:42:50 +02008from vpp_papi import VppEnum
Neale Rannsd0df49f2018-08-08 01:06:40 -07009
10
Neale Rannsc0a93142018-09-05 15:42:26 -070011class DpoProto:
12 DPO_PROTO_IP4 = 0
13 DPO_PROTO_IP6 = 1
14 DPO_PROTO_MPLS = 2
15 DPO_PROTO_ETHERNET = 3
16 DPO_PROTO_BIER = 4
17 DPO_PROTO_NSH = 5
18
19
Neale Rannsd0df49f2018-08-08 01:06:40 -070020INVALID_INDEX = 0xffffffff
21
22
Neale Rannsd0df49f2018-08-08 01:06:40 -070023class VppIpAddressUnion():
24 def __init__(self, addr):
25 self.addr = addr
26 self.ip_addr = ip_address(unicode(self.addr))
27
Neale Rannsd0df49f2018-08-08 01:06:40 -070028 def encode(self):
Neale Rannsc0a93142018-09-05 15:42:26 -070029 if self.version is 6:
Neale Rannsd0df49f2018-08-08 01:06:40 -070030 return {
31 'ip6': {
32 'address': self.ip_addr.packed
33 },
34 }
35 else:
36 return {
37 'ip4': {
38 'address': self.ip_addr.packed
39 },
40 }
41
Neale Rannsc0a93142018-09-05 15:42:26 -070042 @property
43 def version(self):
44 return self.ip_addr.version
45
46 @property
47 def address(self):
48 return self.addr
49
50 @property
51 def length(self):
52 if self.version is 6:
53 return 128
54 else:
55 return 32
56
57 @property
58 def bytes(self):
59 return self.ip_addr.packed
60
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
66 if 4 is self.version:
67 return self.ip_addr.packed == other.ip4.address
68 else:
69 return self.ip_addr.packed == other.ip6.address
70 else:
71 raise Exception("Comparing VppIpAddresUnions:%s"
72 " with unknown type: %s" %
73 (self, other))
74
75 return False
76
Neale Rannsd0df49f2018-08-08 01:06:40 -070077
78class VppIpAddress():
79 def __init__(self, addr):
80 self.addr = VppIpAddressUnion(addr)
81
82 def encode(self):
83 if self.addr.version is 6:
84 return {
Ole Troan0685da42018-10-16 14:42:50 +020085 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP6,
Neale Rannsd0df49f2018-08-08 01:06:40 -070086 'un': self.addr.encode()
87 }
88 else:
89 return {
Ole Troan0685da42018-10-16 14:42:50 +020090 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP4,
Neale Rannsd0df49f2018-08-08 01:06:40 -070091 'un': self.addr.encode()
92 }
93
Neale Rannsc0a93142018-09-05 15:42:26 -070094 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"):
98 # a vp_api_address_t
99 if 4 is self.version:
Ole Troan0685da42018-10-16 14:42:50 +0200100 return other.af == \
101 VppEnum.vl_api_address_family_t.ADDRESS_IP4 and \
Neale Rannsc0a93142018-09-05 15:42:26 -0700102 other.un == self.addr
103 else:
Ole Troan0685da42018-10-16 14:42:50 +0200104 return other.af == \
105 VppEnum.vl_api_address_family_t.ADDRESS_IP6 and \
Neale Rannsc0a93142018-09-05 15:42:26 -0700106 other.un == self.addr
107 else:
108 raise Exception("Comparing VppIpAddress:%s with unknown type: %s" %
109 (self, other))
110 return False
111
112 def __ne__(self, other):
113 return not (self == other)
114
115 def __str__(self):
116 return self.address
117
118 @property
119 def bytes(self):
120 return self.addr.bytes
121
Neale Rannsd0df49f2018-08-08 01:06:40 -0700122 @property
123 def address(self):
124 return self.addr.address
125
Neale Rannsc0a93142018-09-05 15:42:26 -0700126 @property
127 def length(self):
128 return self.addr.length
129
130 @property
131 def version(self):
132 return self.addr.version
133
134 @property
135 def is_ip6(self):
136 return (self.version == 6)
137
138 @property
139 def af(self):
140 if self.version == 6:
141 return AF_INET6
142 else:
143 return AF_INET
144
145 @property
146 def dpo_proto(self):
147 if self.version is 6:
148 return DpoProto.DPO_PROTO_IP6
149 else:
150 return DpoProto.DPO_PROTO_IP4
151
Neale Rannsd0df49f2018-08-08 01:06:40 -0700152
153class VppIpPrefix():
154 def __init__(self, addr, len):
155 self.addr = VppIpAddress(addr)
156 self.len = len
157
158 def __eq__(self, other):
159 if self.addr == other.addr and self.len == other.len:
160 return True
161 return False
162
163 def encode(self):
164 return {'address': self.addr.encode(),
165 'address_length': self.len}
166
167 @property
168 def address(self):
169 return self.addr.address
170
Neale Rannsc0a93142018-09-05 15:42:26 -0700171 @property
172 def length(self):
173 return self.len
174
175 def __str__(self):
176 return "%s/%d" % (self.address, self.length)
177
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"):
182 # vl_api_prefix_t
183 return self.len == other.address_length and \
184 self.addr == other.address
185 else:
186 raise Exception("Comparing VppIpPrefix:%s with unknown type: %s" %
187 (self, other))
188 return False
189
Neale Rannsd0df49f2018-08-08 01:06:40 -0700190
191class VppIpMPrefix():
192 def __init__(self, saddr, gaddr, len):
193 self.saddr = saddr
194 self.gaddr = gaddr
195 self.len = len
196 self.ip_saddr = ip_address(unicode(self.saddr))
197 self.ip_gaddr = ip_address(unicode(self.gaddr))
198
199 def encode(self):
200
201 if 6 is self.ip_saddr.version:
202 prefix = {
Ole Troan0685da42018-10-16 14:42:50 +0200203 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP6,
Neale Rannsd0df49f2018-08-08 01:06:40 -0700204 'grp_address': {
205 'ip6': {
206 'address': self.ip_gaddr.packed
207 },
208 },
209 'src_address': {
210 'ip6': {
211 'address': self.ip_saddr.packed
212 },
213 },
214 'grp_address_length': self.len,
215 }
216 else:
217 prefix = {
Ole Troan0685da42018-10-16 14:42:50 +0200218 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP4,
Neale Rannsd0df49f2018-08-08 01:06:40 -0700219 'grp_address': {
220 'ip4': {
221 'address': self.ip_gaddr.packed
222 },
223 },
224 'src_address': {
225 'ip4': {
226 'address': self.ip_saddr.packed
227 },
228 },
229 'grp_address_length': self.len,
230 }
231 return prefix