blob: 1218c4bb8bbdeb932f987ccb1c92757533b4eab4 [file] [log] [blame]
Neale Ranns311124e2019-01-24 04:52:25 -08001from vpp_object import *
2from ipaddress import ip_address
Neale Ranns17dcec02019-01-09 21:22:20 -08003from vpp_papi import VppEnum
Neale Ranns311124e2019-01-24 04:52:25 -08004
5try:
6 text_type = unicode
7except NameError:
8 text_type = str
9
10
11class VppIpsecSpd(VppObject):
12 """
13 VPP SPD DB
14 """
15
16 def __init__(self, test, id):
17 self.test = test
18 self.id = id
19
20 def add_vpp_config(self):
21 self.test.vapi.ipsec_spd_add_del(self.id)
22 self.test.registry.register(self, self.test.logger)
23
24 def remove_vpp_config(self):
25 self.test.vapi.ipsec_spd_add_del(self.id, is_add=0)
26
27 def __str__(self):
28 return self.object_id()
29
30 def object_id(self):
31 return "ipsec-spd-%d" % self.id
32
33 def query_vpp_config(self):
34 spds = self.test.vapi.ipsec_spds_dump()
35 for spd in spds:
36 if spd.spd_id == self.id:
37 return True
38 return False
39
40
41class VppIpsecSpdItfBinding(VppObject):
42 """
43 VPP SPD DB to interface binding
44 (i.e. this SPD is used on this interfce)
45 """
46
47 def __init__(self, test, spd, itf):
48 self.test = test
49 self.spd = spd
50 self.itf = itf
51
52 def add_vpp_config(self):
53 self.test.vapi.ipsec_interface_add_del_spd(self.spd.id,
54 self.itf.sw_if_index)
55 self.test.registry.register(self, self.test.logger)
56
57 def remove_vpp_config(self):
58 self.test.vapi.ipsec_interface_add_del_spd(self.spd.id,
59 self.itf.sw_if_index,
60 is_add=0)
61
62 def __str__(self):
63 return self.object_id()
64
65 def object_id(self):
66 return "bind-%s-to-%s" % (self.spd.id, self.itf)
67
68 def query_vpp_config(self):
69 bs = self.test.vapi.ipsec_spd_interface_dump()
70 for b in bs:
71 if b.sw_if_index == self.itf.sw_if_index:
72 return True
73 return False
74
75
76class VppIpsecSpdEntry(VppObject):
77 """
78 VPP SPD DB Entry
79 """
80
81 def __init__(self, test, spd, sa_id,
82 local_start, local_stop,
83 remote_start, remote_stop,
84 proto,
85 priority=100,
Neale Ranns17dcec02019-01-09 21:22:20 -080086 policy=None,
Neale Ranns311124e2019-01-24 04:52:25 -080087 is_outbound=1,
88 remote_port_start=0,
89 remote_port_stop=65535,
90 local_port_start=0,
91 local_port_stop=65535):
92 self.test = test
93 self.spd = spd
94 self.sa_id = sa_id
95 self.local_start = ip_address(text_type(local_start))
96 self.local_stop = ip_address(text_type(local_stop))
97 self.remote_start = ip_address(text_type(remote_start))
98 self.remote_stop = ip_address(text_type(remote_stop))
99 self.proto = proto
100 self.is_outbound = is_outbound
101 self.priority = priority
Neale Ranns17dcec02019-01-09 21:22:20 -0800102 if not policy:
103 self.policy = (VppEnum.vl_api_ipsec_spd_action_t.
104 IPSEC_API_SPD_ACTION_BYPASS)
105 else:
106 self.policy = policy
Neale Ranns311124e2019-01-24 04:52:25 -0800107 self.is_ipv6 = (0 if self.local_start.version == 4 else 1)
108 self.local_port_start = local_port_start
109 self.local_port_stop = local_port_stop
110 self.remote_port_start = remote_port_start
111 self.remote_port_stop = remote_port_stop
112
113 def add_vpp_config(self):
Neale Ranns17dcec02019-01-09 21:22:20 -0800114 self.test.vapi.ipsec_spd_entry_add_del(
Neale Ranns311124e2019-01-24 04:52:25 -0800115 self.spd.id,
116 self.sa_id,
Neale Ranns17dcec02019-01-09 21:22:20 -0800117 self.local_start,
118 self.local_stop,
119 self.remote_start,
120 self.remote_stop,
Neale Ranns311124e2019-01-24 04:52:25 -0800121 protocol=self.proto,
122 is_ipv6=self.is_ipv6,
123 is_outbound=self.is_outbound,
124 priority=self.priority,
125 policy=self.policy,
126 local_port_start=self.local_port_start,
127 local_port_stop=self.local_port_stop,
128 remote_port_start=self.remote_port_start,
129 remote_port_stop=self.remote_port_stop)
130 self.test.registry.register(self, self.test.logger)
131
132 def remove_vpp_config(self):
Neale Ranns17dcec02019-01-09 21:22:20 -0800133 self.test.vapi.ipsec_spd_entry_add_del(
Neale Ranns311124e2019-01-24 04:52:25 -0800134 self.spd.id,
135 self.sa_id,
Neale Ranns17dcec02019-01-09 21:22:20 -0800136 self.local_start,
137 self.local_stop,
138 self.remote_start,
139 self.remote_stop,
Neale Ranns311124e2019-01-24 04:52:25 -0800140 protocol=self.proto,
141 is_ipv6=self.is_ipv6,
142 is_outbound=self.is_outbound,
143 priority=self.priority,
144 policy=self.policy,
145 local_port_start=self.local_port_start,
146 local_port_stop=self.local_port_stop,
147 remote_port_start=self.remote_port_start,
148 remote_port_stop=self.remote_port_stop,
149 is_add=0)
150
151 def __str__(self):
152 return self.object_id()
153
154 def object_id(self):
155 return "spd-entry-%d-%d-%d-%d-%d-%d" % (self.spd.id,
156 self.priority,
157 self.policy,
158 self.is_outbound,
159 self.is_ipv6,
160 self.remote_port_start)
161
162 def query_vpp_config(self):
163 ss = self.test.vapi.ipsec_spd_dump(self.spd.id)
164 for s in ss:
Neale Ranns17dcec02019-01-09 21:22:20 -0800165 if s.entry.sa_id == self.sa_id and \
166 s.entry.is_outbound == self.is_outbound and \
167 s.entry.priority == self.priority and \
168 s.entry.policy == self.policy and \
169 s.entry.remote_address_start == self.remote_start and \
170 s.entry.remote_port_start == self.remote_port_start:
Neale Ranns311124e2019-01-24 04:52:25 -0800171 return True
172 return False
173
174
175class VppIpsecSA(VppObject):
176 """
177 VPP SAD Entry
178 """
179
180 def __init__(self, test, id, spi,
181 integ_alg, integ_key,
182 crypto_alg, crypto_key,
183 proto,
184 tun_src=None, tun_dst=None,
Neale Ranns17dcec02019-01-09 21:22:20 -0800185 flags=None):
186 e = VppEnum.vl_api_ipsec_sad_flags_t
Neale Ranns311124e2019-01-24 04:52:25 -0800187 self.test = test
188 self.id = id
189 self.spi = spi
190 self.integ_alg = integ_alg
191 self.integ_key = integ_key
192 self.crypto_alg = crypto_alg
193 self.crypto_key = crypto_key
194 self.proto = proto
Neale Ranns17dcec02019-01-09 21:22:20 -0800195
Neale Ranns311124e2019-01-24 04:52:25 -0800196 self.tun_src = tun_src
197 self.tun_dst = tun_dst
Neale Ranns17dcec02019-01-09 21:22:20 -0800198 if not flags:
199 self.flags = e.IPSEC_API_SAD_FLAG_NONE
200 else:
201 self.flags = flags
Neale Ranns311124e2019-01-24 04:52:25 -0800202 if (tun_src):
203 self.tun_src = ip_address(text_type(tun_src))
Neale Ranns17dcec02019-01-09 21:22:20 -0800204 self.flags = self.flags | e.IPSEC_API_SAD_FLAG_IS_TUNNEL
Neale Ranns311124e2019-01-24 04:52:25 -0800205 if (self.tun_src.version == 6):
Neale Ranns17dcec02019-01-09 21:22:20 -0800206 self.flags = self.flags | e.IPSEC_API_SAD_FLAG_IS_TUNNEL_V6
Neale Ranns311124e2019-01-24 04:52:25 -0800207 if (tun_dst):
208 self.tun_dst = ip_address(text_type(tun_dst))
Neale Ranns311124e2019-01-24 04:52:25 -0800209
210 def add_vpp_config(self):
Neale Ranns17dcec02019-01-09 21:22:20 -0800211 self.test.vapi.ipsec_sad_entry_add_del(
Neale Ranns311124e2019-01-24 04:52:25 -0800212 self.id,
213 self.spi,
214 self.integ_alg,
215 self.integ_key,
216 self.crypto_alg,
217 self.crypto_key,
218 self.proto,
Neale Ranns17dcec02019-01-09 21:22:20 -0800219 (self.tun_src if self.tun_src else []),
220 (self.tun_dst if self.tun_dst else []),
221 flags=self.flags)
Neale Ranns311124e2019-01-24 04:52:25 -0800222 self.test.registry.register(self, self.test.logger)
223
224 def remove_vpp_config(self):
Neale Ranns17dcec02019-01-09 21:22:20 -0800225 self.test.vapi.ipsec_sad_entry_add_del(
Neale Ranns311124e2019-01-24 04:52:25 -0800226 self.id,
227 self.spi,
228 self.integ_alg,
229 self.integ_key,
230 self.crypto_alg,
231 self.crypto_key,
232 self.proto,
Neale Ranns17dcec02019-01-09 21:22:20 -0800233 (self.tun_src if self.tun_src else []),
234 (self.tun_dst if self.tun_dst else []),
235 flags=self.flags,
Neale Ranns311124e2019-01-24 04:52:25 -0800236 is_add=0)
237
238 def __str__(self):
239 return self.object_id()
240
241 def object_id(self):
242 return "ipsec-sa-%d" % self.id
243
244 def query_vpp_config(self):
245 bs = self.test.vapi.ipsec_sa_dump()
246 for b in bs:
247 if b.sa_id == self.id:
248 return True
249 return False