blob: 0df8cb2a88acbe1133b8be26288647dc4d0bc249 [file] [log] [blame]
Paul Vinciguerraa279d9c2019-02-28 09:00:09 -08001from vpp_object import VppObject
Neale Ranns311124e2019-01-24 04:52:25 -08002from 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
Neale Ranns311124e2019-01-24 04:52:25 -080027 def object_id(self):
28 return "ipsec-spd-%d" % self.id
29
30 def query_vpp_config(self):
31 spds = self.test.vapi.ipsec_spds_dump()
32 for spd in spds:
33 if spd.spd_id == self.id:
34 return True
35 return False
36
37
38class VppIpsecSpdItfBinding(VppObject):
39 """
40 VPP SPD DB to interface binding
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -070041 (i.e. this SPD is used on this interface)
Neale Ranns311124e2019-01-24 04:52:25 -080042 """
43
44 def __init__(self, test, spd, itf):
45 self.test = test
46 self.spd = spd
47 self.itf = itf
48
49 def add_vpp_config(self):
50 self.test.vapi.ipsec_interface_add_del_spd(self.spd.id,
51 self.itf.sw_if_index)
52 self.test.registry.register(self, self.test.logger)
53
54 def remove_vpp_config(self):
55 self.test.vapi.ipsec_interface_add_del_spd(self.spd.id,
56 self.itf.sw_if_index,
57 is_add=0)
58
Neale Ranns311124e2019-01-24 04:52:25 -080059 def object_id(self):
60 return "bind-%s-to-%s" % (self.spd.id, self.itf)
61
62 def query_vpp_config(self):
63 bs = self.test.vapi.ipsec_spd_interface_dump()
64 for b in bs:
65 if b.sw_if_index == self.itf.sw_if_index:
66 return True
67 return False
68
69
70class VppIpsecSpdEntry(VppObject):
71 """
72 VPP SPD DB Entry
73 """
74
75 def __init__(self, test, spd, sa_id,
76 local_start, local_stop,
77 remote_start, remote_stop,
78 proto,
79 priority=100,
Neale Ranns17dcec02019-01-09 21:22:20 -080080 policy=None,
Neale Ranns311124e2019-01-24 04:52:25 -080081 is_outbound=1,
82 remote_port_start=0,
83 remote_port_stop=65535,
84 local_port_start=0,
85 local_port_stop=65535):
86 self.test = test
87 self.spd = spd
88 self.sa_id = sa_id
89 self.local_start = ip_address(text_type(local_start))
90 self.local_stop = ip_address(text_type(local_stop))
91 self.remote_start = ip_address(text_type(remote_start))
92 self.remote_stop = ip_address(text_type(remote_stop))
93 self.proto = proto
94 self.is_outbound = is_outbound
95 self.priority = priority
Neale Ranns17dcec02019-01-09 21:22:20 -080096 if not policy:
97 self.policy = (VppEnum.vl_api_ipsec_spd_action_t.
98 IPSEC_API_SPD_ACTION_BYPASS)
99 else:
100 self.policy = policy
Neale Ranns311124e2019-01-24 04:52:25 -0800101 self.is_ipv6 = (0 if self.local_start.version == 4 else 1)
102 self.local_port_start = local_port_start
103 self.local_port_stop = local_port_stop
104 self.remote_port_start = remote_port_start
105 self.remote_port_stop = remote_port_stop
106
107 def add_vpp_config(self):
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800108 rv = self.test.vapi.ipsec_spd_entry_add_del(
Neale Ranns311124e2019-01-24 04:52:25 -0800109 self.spd.id,
110 self.sa_id,
Neale Ranns17dcec02019-01-09 21:22:20 -0800111 self.local_start,
112 self.local_stop,
113 self.remote_start,
114 self.remote_stop,
Neale Ranns311124e2019-01-24 04:52:25 -0800115 protocol=self.proto,
116 is_ipv6=self.is_ipv6,
117 is_outbound=self.is_outbound,
118 priority=self.priority,
119 policy=self.policy,
120 local_port_start=self.local_port_start,
121 local_port_stop=self.local_port_stop,
122 remote_port_start=self.remote_port_start,
123 remote_port_stop=self.remote_port_stop)
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800124 self.stat_index = rv.stat_index
Neale Ranns311124e2019-01-24 04:52:25 -0800125 self.test.registry.register(self, self.test.logger)
126
127 def remove_vpp_config(self):
Neale Ranns17dcec02019-01-09 21:22:20 -0800128 self.test.vapi.ipsec_spd_entry_add_del(
Neale Ranns311124e2019-01-24 04:52:25 -0800129 self.spd.id,
130 self.sa_id,
Neale Ranns17dcec02019-01-09 21:22:20 -0800131 self.local_start,
132 self.local_stop,
133 self.remote_start,
134 self.remote_stop,
Neale Ranns311124e2019-01-24 04:52:25 -0800135 protocol=self.proto,
136 is_ipv6=self.is_ipv6,
137 is_outbound=self.is_outbound,
138 priority=self.priority,
139 policy=self.policy,
140 local_port_start=self.local_port_start,
141 local_port_stop=self.local_port_stop,
142 remote_port_start=self.remote_port_start,
143 remote_port_stop=self.remote_port_stop,
144 is_add=0)
145
Neale Ranns311124e2019-01-24 04:52:25 -0800146 def object_id(self):
147 return "spd-entry-%d-%d-%d-%d-%d-%d" % (self.spd.id,
148 self.priority,
149 self.policy,
150 self.is_outbound,
151 self.is_ipv6,
152 self.remote_port_start)
153
154 def query_vpp_config(self):
155 ss = self.test.vapi.ipsec_spd_dump(self.spd.id)
156 for s in ss:
Neale Ranns17dcec02019-01-09 21:22:20 -0800157 if s.entry.sa_id == self.sa_id and \
158 s.entry.is_outbound == self.is_outbound and \
159 s.entry.priority == self.priority and \
160 s.entry.policy == self.policy and \
161 s.entry.remote_address_start == self.remote_start and \
162 s.entry.remote_port_start == self.remote_port_start:
Neale Ranns311124e2019-01-24 04:52:25 -0800163 return True
164 return False
165
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800166 def get_stats(self):
167 c = self.test.statistics.get_counter("/net/ipsec/policy")
168 return c[0][self.stat_index]
169
Neale Ranns311124e2019-01-24 04:52:25 -0800170
171class VppIpsecSA(VppObject):
172 """
173 VPP SAD Entry
174 """
175
176 def __init__(self, test, id, spi,
177 integ_alg, integ_key,
178 crypto_alg, crypto_key,
179 proto,
180 tun_src=None, tun_dst=None,
Neale Ranns80f6fd52019-04-16 02:41:34 +0000181 flags=None, salt=0):
Neale Ranns17dcec02019-01-09 21:22:20 -0800182 e = VppEnum.vl_api_ipsec_sad_flags_t
Neale Ranns311124e2019-01-24 04:52:25 -0800183 self.test = test
184 self.id = id
185 self.spi = spi
186 self.integ_alg = integ_alg
187 self.integ_key = integ_key
188 self.crypto_alg = crypto_alg
189 self.crypto_key = crypto_key
190 self.proto = proto
Neale Ranns80f6fd52019-04-16 02:41:34 +0000191 self.salt = salt
Neale Ranns17dcec02019-01-09 21:22:20 -0800192
Neale Ranns311124e2019-01-24 04:52:25 -0800193 self.tun_src = tun_src
194 self.tun_dst = tun_dst
Neale Ranns17dcec02019-01-09 21:22:20 -0800195 if not flags:
196 self.flags = e.IPSEC_API_SAD_FLAG_NONE
197 else:
198 self.flags = flags
Neale Ranns311124e2019-01-24 04:52:25 -0800199 if (tun_src):
200 self.tun_src = ip_address(text_type(tun_src))
Neale Ranns17dcec02019-01-09 21:22:20 -0800201 self.flags = self.flags | e.IPSEC_API_SAD_FLAG_IS_TUNNEL
Neale Ranns311124e2019-01-24 04:52:25 -0800202 if (self.tun_src.version == 6):
Neale Ranns17dcec02019-01-09 21:22:20 -0800203 self.flags = self.flags | e.IPSEC_API_SAD_FLAG_IS_TUNNEL_V6
Neale Ranns311124e2019-01-24 04:52:25 -0800204 if (tun_dst):
205 self.tun_dst = ip_address(text_type(tun_dst))
Neale Ranns311124e2019-01-24 04:52:25 -0800206
207 def add_vpp_config(self):
Neale Rannseba31ec2019-02-17 18:04:27 +0000208 r = self.test.vapi.ipsec_sad_entry_add_del(
Neale Ranns311124e2019-01-24 04:52:25 -0800209 self.id,
210 self.spi,
211 self.integ_alg,
212 self.integ_key,
213 self.crypto_alg,
214 self.crypto_key,
215 self.proto,
Neale Ranns17dcec02019-01-09 21:22:20 -0800216 (self.tun_src if self.tun_src else []),
217 (self.tun_dst if self.tun_dst else []),
Neale Ranns80f6fd52019-04-16 02:41:34 +0000218 flags=self.flags,
219 salt=self.salt)
Neale Rannseba31ec2019-02-17 18:04:27 +0000220 self.stat_index = r.stat_index
Neale Ranns311124e2019-01-24 04:52:25 -0800221 self.test.registry.register(self, self.test.logger)
222
223 def remove_vpp_config(self):
Neale Ranns17dcec02019-01-09 21:22:20 -0800224 self.test.vapi.ipsec_sad_entry_add_del(
Neale Ranns311124e2019-01-24 04:52:25 -0800225 self.id,
226 self.spi,
227 self.integ_alg,
228 self.integ_key,
229 self.crypto_alg,
230 self.crypto_key,
231 self.proto,
Neale Ranns17dcec02019-01-09 21:22:20 -0800232 (self.tun_src if self.tun_src else []),
233 (self.tun_dst if self.tun_dst else []),
234 flags=self.flags,
Neale Ranns311124e2019-01-24 04:52:25 -0800235 is_add=0)
236
Neale Ranns311124e2019-01-24 04:52:25 -0800237 def object_id(self):
238 return "ipsec-sa-%d" % self.id
239
240 def query_vpp_config(self):
241 bs = self.test.vapi.ipsec_sa_dump()
242 for b in bs:
Neale Ranns8d7c5022019-02-06 01:41:05 -0800243 if b.entry.sad_id == self.id:
Neale Ranns311124e2019-01-24 04:52:25 -0800244 return True
245 return False
Neale Rannseba31ec2019-02-17 18:04:27 +0000246
247 def get_stats(self):
248 c = self.test.statistics.get_counter("/net/ipsec/sa")
249 return c[0][self.stat_index]
Neale Rannsc87b66c2019-02-07 07:26:12 -0800250
251
252class VppIpsecTunProtect(VppObject):
253 """
254 VPP IPSEC tunnel protection
255 """
256
257 def __init__(self, test, itf, sa_out, sas_in):
258 self.test = test
259 self.itf = itf
260 self.sas_in = []
261 for sa in sas_in:
262 self.sas_in.append(sa.id)
263 self.sa_out = sa_out.id
264
265 def update_vpp_config(self, sa_out, sas_in):
266 self.sas_in = []
267 for sa in sas_in:
268 self.sas_in.append(sa.id)
269 self.sa_out = sa_out.id
270 self.test.vapi.ipsec_tunnel_protect_update(
271 tunnel={
272 'sw_if_index': self.itf._sw_if_index,
273 'n_sa_in': len(self.sas_in),
274 'sa_out': self.sa_out,
275 'sa_in': self.sas_in})
276
277 def object_id(self):
278 return "ipsec-tun-protect-%s" % self.itf
279
280 def add_vpp_config(self):
281 self.test.vapi.ipsec_tunnel_protect_update(
282 tunnel={
283 'sw_if_index': self.itf._sw_if_index,
284 'n_sa_in': len(self.sas_in),
285 'sa_out': self.sa_out,
286 'sa_in': self.sas_in})
287 self.test.registry.register(self, self.test.logger)
288
289 def remove_vpp_config(self):
290 self.test.vapi.ipsec_tunnel_protect_del(
291 sw_if_index=self.itf.sw_if_index)
292
293 def query_vpp_config(self):
294 bs = self.test.vapi.ipsec_tunnel_protect_dump(
295 sw_if_index=self.itf.sw_if_index)
296 for b in bs:
297 if b.tun.sw_if_index == self.itf.sw_if_index:
298 return True
299 return False