blob: fe985fb901cee7a772bbc5222e1e2493713c1797 [file] [log] [blame]
Neale Rannsd0df49f2018-08-08 01:06:40 -07001"""
2 IP Types
3
4"""
Paul Vinciguerraa3aaa612018-12-09 08:52:14 -08005import logging
Neale Rannsd0df49f2018-08-08 01:06:40 -07006
7from ipaddress import ip_address
Neale Rannsc0a93142018-09-05 15:42:26 -07008from socket import AF_INET, AF_INET6
Ole Troan0685da42018-10-16 14:42:50 +02009from vpp_papi import VppEnum
Neale Rannsd0df49f2018-08-08 01:06:40 -070010
Paul Vinciguerraa3aaa612018-12-09 08:52:14 -080011_log = logging.getLogger(__name__)
12
Neale Rannsd0df49f2018-08-08 01:06:40 -070013
Neale Rannsc0a93142018-09-05 15:42:26 -070014class DpoProto:
15 DPO_PROTO_IP4 = 0
16 DPO_PROTO_IP6 = 1
17 DPO_PROTO_MPLS = 2
18 DPO_PROTO_ETHERNET = 3
19 DPO_PROTO_BIER = 4
20 DPO_PROTO_NSH = 5
21
22
Neale Rannsd0df49f2018-08-08 01:06:40 -070023INVALID_INDEX = 0xffffffff
24
25
Neale Rannsd0df49f2018-08-08 01:06:40 -070026class VppIpAddressUnion():
27 def __init__(self, addr):
28 self.addr = addr
29 self.ip_addr = ip_address(unicode(self.addr))
30
Neale Rannsd0df49f2018-08-08 01:06:40 -070031 def encode(self):
Paul Vinciguerraa3aaa612018-12-09 08:52:14 -080032 if self.version == 6:
Ole Troan8c8acc02018-11-27 10:05:23 +010033 return {'ip6': self.ip_addr.packed}
Neale Rannsd0df49f2018-08-08 01:06:40 -070034 else:
Ole Troan8c8acc02018-11-27 10:05:23 +010035 return {'ip4': self.ip_addr.packed}
Neale Rannsd0df49f2018-08-08 01:06:40 -070036
Neale Rannsc0a93142018-09-05 15:42:26 -070037 @property
38 def version(self):
39 return self.ip_addr.version
40
41 @property
42 def address(self):
43 return self.addr
44
45 @property
46 def length(self):
Paul Vinciguerraa3aaa612018-12-09 08:52:14 -080047 return self.ip_addr.max_prefixlen
Neale Rannsc0a93142018-09-05 15:42:26 -070048
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
Paul Vinciguerraa3aaa612018-12-09 08:52:14 -080058 if 4 == 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:
Paul Vinciguerraa3aaa612018-12-09 08:52:14 -080063 _log.error("Comparing VppIpAddressUnions:%s"
64 " with incomparable type: %s",
65 self, other)
66 return NotImplemented
Neale Rannsc0a93142018-09-05 15:42:26 -070067
Neale Rannsd0df49f2018-08-08 01:06:40 -070068
69class VppIpAddress():
70 def __init__(self, addr):
71 self.addr = VppIpAddressUnion(addr)
72
73 def encode(self):
Paul Vinciguerraa3aaa612018-12-09 08:52:14 -080074 if self.addr.version == 6:
Neale Rannsd0df49f2018-08-08 01:06:40 -070075 return {
Ole Troan0685da42018-10-16 14:42:50 +020076 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP6,
Neale Rannsd0df49f2018-08-08 01:06:40 -070077 'un': self.addr.encode()
78 }
79 else:
80 return {
Ole Troan0685da42018-10-16 14:42:50 +020081 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP4,
Neale Rannsd0df49f2018-08-08 01:06:40 -070082 'un': self.addr.encode()
83 }
84
Neale Rannsc0a93142018-09-05 15:42:26 -070085 def __eq__(self, other):
86 if isinstance(other, self.__class__):
87 return self.addr == other.addr
88 elif hasattr(other, "af") and hasattr(other, "un"):
89 # a vp_api_address_t
Paul Vinciguerraa3aaa612018-12-09 08:52:14 -080090 if 4 == self.version:
Ole Troan0685da42018-10-16 14:42:50 +020091 return other.af == \
92 VppEnum.vl_api_address_family_t.ADDRESS_IP4 and \
Neale Rannsc0a93142018-09-05 15:42:26 -070093 other.un == self.addr
94 else:
Ole Troan0685da42018-10-16 14:42:50 +020095 return other.af == \
96 VppEnum.vl_api_address_family_t.ADDRESS_IP6 and \
Neale Rannsc0a93142018-09-05 15:42:26 -070097 other.un == self.addr
98 else:
Paul Vinciguerraa3aaa612018-12-09 08:52:14 -080099 _log.error(
100 "Comparing VppIpAddress:<%s> %s with incomparable "
101 "type: <%s> %s",
102 self.__class__.__name__, self,
103 other.__class__.__name__, other)
104 return NotImplemented
Neale Rannsc0a93142018-09-05 15:42:26 -0700105
106 def __ne__(self, other):
107 return not (self == other)
108
109 def __str__(self):
110 return self.address
111
112 @property
113 def bytes(self):
114 return self.addr.bytes
115
Neale Rannsd0df49f2018-08-08 01:06:40 -0700116 @property
117 def address(self):
118 return self.addr.address
119
Neale Rannsc0a93142018-09-05 15:42:26 -0700120 @property
121 def length(self):
122 return self.addr.length
123
124 @property
125 def version(self):
126 return self.addr.version
127
128 @property
129 def is_ip6(self):
130 return (self.version == 6)
131
132 @property
133 def af(self):
134 if self.version == 6:
135 return AF_INET6
136 else:
137 return AF_INET
138
139 @property
140 def dpo_proto(self):
Paul Vinciguerraa3aaa612018-12-09 08:52:14 -0800141 if self.version == 6:
Neale Rannsc0a93142018-09-05 15:42:26 -0700142 return DpoProto.DPO_PROTO_IP6
143 else:
144 return DpoProto.DPO_PROTO_IP4
145
Neale Rannsd0df49f2018-08-08 01:06:40 -0700146
147class VppIpPrefix():
148 def __init__(self, addr, len):
149 self.addr = VppIpAddress(addr)
150 self.len = len
151
Neale Rannsd0df49f2018-08-08 01:06:40 -0700152 def encode(self):
153 return {'address': self.addr.encode(),
154 'address_length': self.len}
155
156 @property
157 def address(self):
158 return self.addr.address
159
Neale Rannsc0a93142018-09-05 15:42:26 -0700160 @property
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700161 def bytes(self):
162 return self.addr.bytes
163
164 @property
Neale Rannsc0a93142018-09-05 15:42:26 -0700165 def length(self):
166 return self.len
167
Neale Ranns93cc3ee2018-10-10 07:22:51 -0700168 @property
169 def is_ip6(self):
170 return self.addr.is_ip6
171
Neale Rannsc0a93142018-09-05 15:42:26 -0700172 def __str__(self):
173 return "%s/%d" % (self.address, self.length)
174
175 def __eq__(self, other):
176 if isinstance(other, self.__class__):
177 return (self.len == other.len and self.addr == other.addr)
178 elif hasattr(other, "address") and hasattr(other, "address_length"):
179 # vl_api_prefix_t
180 return self.len == other.address_length and \
Paul Vinciguerraa3aaa612018-12-09 08:52:14 -0800181 self.addr == other.address
Neale Rannsc0a93142018-09-05 15:42:26 -0700182 else:
Paul Vinciguerraa3aaa612018-12-09 08:52:14 -0800183 _log.error(
184 "Comparing VppIpPrefix:%s with incomparable type: %s" %
185 (self, other))
186 return NotImplemented
Neale Rannsc0a93142018-09-05 15:42:26 -0700187
Neale Rannsd0df49f2018-08-08 01:06:40 -0700188
189class VppIpMPrefix():
190 def __init__(self, saddr, gaddr, len):
191 self.saddr = saddr
192 self.gaddr = gaddr
193 self.len = len
194 self.ip_saddr = ip_address(unicode(self.saddr))
195 self.ip_gaddr = ip_address(unicode(self.gaddr))
Paul Vinciguerraa3aaa612018-12-09 08:52:14 -0800196 if self.ip_saddr.version != self.ip_gaddr.version:
197 raise ValueError('Source and group addresses must be of the '
198 'same address family.')
Neale Rannsd0df49f2018-08-08 01:06:40 -0700199
200 def encode(self):
Paul Vinciguerraa3aaa612018-12-09 08:52:14 -0800201 if 6 == self.ip_saddr.version:
Neale Rannsd0df49f2018-08-08 01:06:40 -0700202 prefix = {
Ole Troan0685da42018-10-16 14:42:50 +0200203 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP6,
Ole Troan8c8acc02018-11-27 10:05:23 +0100204 'grp_address': {'ip6': self.ip_gaddr.packed},
205 'src_address': {'ip6': self.ip_saddr.packed},
Neale Rannsd0df49f2018-08-08 01:06:40 -0700206 'grp_address_length': self.len,
207 }
208 else:
209 prefix = {
Ole Troan0685da42018-10-16 14:42:50 +0200210 'af': VppEnum.vl_api_address_family_t.ADDRESS_IP4,
Ole Troan8c8acc02018-11-27 10:05:23 +0100211 'grp_address': {'ip4': self.ip_gaddr.packed},
212 'src_address': {'ip4': self.ip_saddr.packed},
Neale Rannsd0df49f2018-08-08 01:06:40 -0700213 'grp_address_length': self.len,
214 }
215 return prefix