blob: 055e6d73738123c0d52fd4443639ba55800f52dd [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:
Ole Troan8c8acc02018-11-27 10:05:23 +010030 return {'ip6': self.ip_addr.packed}
Neale Rannsd0df49f2018-08-08 01:06:40 -070031 else:
Ole Troan8c8acc02018-11-27 10:05:23 +010032 return {'ip4': self.ip_addr.packed}
Neale Rannsd0df49f2018-08-08 01:06:40 -070033
Neale Rannsc0a93142018-09-05 15:42:26 -070034 @property
35 def version(self):
36 return self.ip_addr.version
37
38 @property
39 def address(self):
40 return self.addr
41
42 @property
43 def length(self):
44 if self.version is 6:
45 return 128
46 else:
47 return 32
48
49 @property
50 def bytes(self):
51 return self.ip_addr.packed
52
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
58 if 4 is self.version:
Ole Troan8c8acc02018-11-27 10:05:23 +010059 return self.ip_addr.packed == other.ip4
Neale Rannsc0a93142018-09-05 15:42:26 -070060 else:
Ole Troan8c8acc02018-11-27 10:05:23 +010061 return self.ip_addr.packed == other.ip6
Neale Rannsc0a93142018-09-05 15:42:26 -070062 else:
63 raise Exception("Comparing VppIpAddresUnions:%s"
64 " with unknown type: %s" %
65 (self, other))
66
67 return False
68
Neale Rannsd0df49f2018-08-08 01:06:40 -070069
70class VppIpAddress():
71 def __init__(self, addr):
72 self.addr = VppIpAddressUnion(addr)
73
74 def encode(self):
75 if self.addr.version is 6:
76 return {
Ole Troan0685da42018-10-16 14:42:50 +020077 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP6,
Neale Rannsd0df49f2018-08-08 01:06:40 -070078 'un': self.addr.encode()
79 }
80 else:
81 return {
Ole Troan0685da42018-10-16 14:42:50 +020082 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP4,
Neale Rannsd0df49f2018-08-08 01:06:40 -070083 'un': self.addr.encode()
84 }
85
Neale Rannsc0a93142018-09-05 15:42:26 -070086 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"):
90 # a vp_api_address_t
91 if 4 is self.version:
Ole Troan0685da42018-10-16 14:42:50 +020092 return other.af == \
93 VppEnum.vl_api_address_family_t.ADDRESS_IP4 and \
Neale Rannsc0a93142018-09-05 15:42:26 -070094 other.un == self.addr
95 else:
Ole Troan0685da42018-10-16 14:42:50 +020096 return other.af == \
97 VppEnum.vl_api_address_family_t.ADDRESS_IP6 and \
Neale Rannsc0a93142018-09-05 15:42:26 -070098 other.un == self.addr
99 else:
100 raise Exception("Comparing VppIpAddress:%s with unknown type: %s" %
101 (self, other))
102 return False
103
104 def __ne__(self, other):
105 return not (self == other)
106
107 def __str__(self):
108 return self.address
109
110 @property
111 def bytes(self):
112 return self.addr.bytes
113
Neale Rannsd0df49f2018-08-08 01:06:40 -0700114 @property
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700115 def bytes(self):
116 return self.addr.bytes
117
118 @property
Neale Rannsd0df49f2018-08-08 01:06:40 -0700119 def address(self):
120 return self.addr.address
121
Neale Rannsc0a93142018-09-05 15:42:26 -0700122 @property
123 def length(self):
124 return self.addr.length
125
126 @property
127 def version(self):
128 return self.addr.version
129
130 @property
131 def is_ip6(self):
132 return (self.version == 6)
133
134 @property
135 def af(self):
136 if self.version == 6:
137 return AF_INET6
138 else:
139 return AF_INET
140
141 @property
142 def dpo_proto(self):
143 if self.version is 6:
144 return DpoProto.DPO_PROTO_IP6
145 else:
146 return DpoProto.DPO_PROTO_IP4
147
Neale Rannsd0df49f2018-08-08 01:06:40 -0700148
149class VppIpPrefix():
150 def __init__(self, addr, len):
151 self.addr = VppIpAddress(addr)
152 self.len = len
153
154 def __eq__(self, other):
155 if self.addr == other.addr and self.len == other.len:
156 return True
157 return False
158
159 def encode(self):
160 return {'address': self.addr.encode(),
161 'address_length': self.len}
162
163 @property
164 def address(self):
165 return self.addr.address
166
Neale Rannsc0a93142018-09-05 15:42:26 -0700167 @property
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700168 def bytes(self):
169 return self.addr.bytes
170
171 @property
Neale Rannsc0a93142018-09-05 15:42:26 -0700172 def length(self):
173 return self.len
174
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700175 @property
176 def is_ip6(self):
177 return self.addr.is_ip6
178
Neale Rannsc0a93142018-09-05 15:42:26 -0700179 def __str__(self):
180 return "%s/%d" % (self.address, self.length)
181
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"):
186 # vl_api_prefix_t
187 return self.len == other.address_length and \
188 self.addr == other.address
189 else:
190 raise Exception("Comparing VppIpPrefix:%s with unknown type: %s" %
191 (self, other))
192 return False
193
Neale Rannsd0df49f2018-08-08 01:06:40 -0700194
195class VppIpMPrefix():
196 def __init__(self, saddr, gaddr, len):
197 self.saddr = saddr
198 self.gaddr = gaddr
199 self.len = len
200 self.ip_saddr = ip_address(unicode(self.saddr))
201 self.ip_gaddr = ip_address(unicode(self.gaddr))
202
203 def encode(self):
204
205 if 6 is self.ip_saddr.version:
206 prefix = {
Ole Troan0685da42018-10-16 14:42:50 +0200207 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP6,
Ole Troan8c8acc02018-11-27 10:05:23 +0100208 'grp_address': {'ip6': self.ip_gaddr.packed},
209 'src_address': {'ip6': self.ip_saddr.packed},
Neale Rannsd0df49f2018-08-08 01:06:40 -0700210 'grp_address_length': self.len,
211 }
212 else:
213 prefix = {
Ole Troan0685da42018-10-16 14:42:50 +0200214 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP4,
Ole Troan8c8acc02018-11-27 10:05:23 +0100215 'grp_address': {'ip4': self.ip_gaddr.packed},
216 'src_address': {'ip4': self.ip_saddr.packed},
Neale Rannsd0df49f2018-08-08 01:06:40 -0700217 'grp_address_length': self.len,
218 }
219 return prefix