blob: d0ceeae2e4d385858c9153d4a3e007c286aa442a [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 Rannsdd4ccf22020-06-30 07:47:14 +00004from vpp_interface import VppInterface
Neale Ranns311124e2019-01-24 04:52:25 -08005
6try:
7 text_type = unicode
8except NameError:
9 text_type = str
10
11
Neale Ranns4a56f4e2019-12-23 04:10:25 +000012def mk_counter():
13 return {'packets': 0, 'bytes': 0}
14
15
Neale Ranns311124e2019-01-24 04:52:25 -080016class VppIpsecSpd(VppObject):
17 """
18 VPP SPD DB
19 """
20
21 def __init__(self, test, id):
22 self.test = test
23 self.id = id
24
25 def add_vpp_config(self):
26 self.test.vapi.ipsec_spd_add_del(self.id)
27 self.test.registry.register(self, self.test.logger)
28
29 def remove_vpp_config(self):
30 self.test.vapi.ipsec_spd_add_del(self.id, is_add=0)
31
Neale Ranns311124e2019-01-24 04:52:25 -080032 def object_id(self):
33 return "ipsec-spd-%d" % self.id
34
35 def query_vpp_config(self):
36 spds = self.test.vapi.ipsec_spds_dump()
37 for spd in spds:
38 if spd.spd_id == self.id:
39 return True
40 return False
41
42
43class VppIpsecSpdItfBinding(VppObject):
44 """
45 VPP SPD DB to interface binding
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -070046 (i.e. this SPD is used on this interface)
Neale Ranns311124e2019-01-24 04:52:25 -080047 """
48
49 def __init__(self, test, spd, itf):
50 self.test = test
51 self.spd = spd
52 self.itf = itf
53
54 def add_vpp_config(self):
55 self.test.vapi.ipsec_interface_add_del_spd(self.spd.id,
56 self.itf.sw_if_index)
57 self.test.registry.register(self, self.test.logger)
58
59 def remove_vpp_config(self):
60 self.test.vapi.ipsec_interface_add_del_spd(self.spd.id,
61 self.itf.sw_if_index,
62 is_add=0)
63
Neale Ranns311124e2019-01-24 04:52:25 -080064 def object_id(self):
65 return "bind-%s-to-%s" % (self.spd.id, self.itf)
66
67 def query_vpp_config(self):
68 bs = self.test.vapi.ipsec_spd_interface_dump()
69 for b in bs:
70 if b.sw_if_index == self.itf.sw_if_index:
71 return True
72 return False
73
74
75class VppIpsecSpdEntry(VppObject):
76 """
77 VPP SPD DB Entry
78 """
79
80 def __init__(self, test, spd, sa_id,
81 local_start, local_stop,
82 remote_start, remote_stop,
83 proto,
84 priority=100,
Neale Ranns17dcec02019-01-09 21:22:20 -080085 policy=None,
Neale Ranns311124e2019-01-24 04:52:25 -080086 is_outbound=1,
87 remote_port_start=0,
88 remote_port_stop=65535,
89 local_port_start=0,
90 local_port_stop=65535):
91 self.test = test
92 self.spd = spd
93 self.sa_id = sa_id
94 self.local_start = ip_address(text_type(local_start))
95 self.local_stop = ip_address(text_type(local_stop))
96 self.remote_start = ip_address(text_type(remote_start))
97 self.remote_stop = ip_address(text_type(remote_stop))
98 self.proto = proto
99 self.is_outbound = is_outbound
100 self.priority = priority
Neale Ranns17dcec02019-01-09 21:22:20 -0800101 if not policy:
102 self.policy = (VppEnum.vl_api_ipsec_spd_action_t.
103 IPSEC_API_SPD_ACTION_BYPASS)
104 else:
105 self.policy = policy
Neale Ranns311124e2019-01-24 04:52:25 -0800106 self.is_ipv6 = (0 if self.local_start.version == 4 else 1)
107 self.local_port_start = local_port_start
108 self.local_port_stop = local_port_stop
109 self.remote_port_start = remote_port_start
110 self.remote_port_stop = remote_port_stop
111
112 def add_vpp_config(self):
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800113 rv = self.test.vapi.ipsec_spd_entry_add_del(
Neale Ranns311124e2019-01-24 04:52:25 -0800114 self.spd.id,
115 self.sa_id,
Neale Ranns17dcec02019-01-09 21:22:20 -0800116 self.local_start,
117 self.local_stop,
118 self.remote_start,
119 self.remote_stop,
Neale Ranns311124e2019-01-24 04:52:25 -0800120 protocol=self.proto,
121 is_ipv6=self.is_ipv6,
122 is_outbound=self.is_outbound,
123 priority=self.priority,
124 policy=self.policy,
125 local_port_start=self.local_port_start,
126 local_port_stop=self.local_port_stop,
127 remote_port_start=self.remote_port_start,
128 remote_port_stop=self.remote_port_stop)
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800129 self.stat_index = rv.stat_index
Neale Ranns311124e2019-01-24 04:52:25 -0800130 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
Neale Ranns311124e2019-01-24 04:52:25 -0800151 def object_id(self):
152 return "spd-entry-%d-%d-%d-%d-%d-%d" % (self.spd.id,
153 self.priority,
154 self.policy,
155 self.is_outbound,
156 self.is_ipv6,
157 self.remote_port_start)
158
159 def query_vpp_config(self):
160 ss = self.test.vapi.ipsec_spd_dump(self.spd.id)
161 for s in ss:
Neale Ranns17dcec02019-01-09 21:22:20 -0800162 if s.entry.sa_id == self.sa_id and \
163 s.entry.is_outbound == self.is_outbound and \
164 s.entry.priority == self.priority and \
165 s.entry.policy == self.policy and \
166 s.entry.remote_address_start == self.remote_start and \
167 s.entry.remote_port_start == self.remote_port_start:
Neale Ranns311124e2019-01-24 04:52:25 -0800168 return True
169 return False
170
Neale Ranns4a56f4e2019-12-23 04:10:25 +0000171 def get_stats(self, worker=None):
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800172 c = self.test.statistics.get_counter("/net/ipsec/policy")
Neale Ranns4a56f4e2019-12-23 04:10:25 +0000173 if worker is None:
174 total = mk_counter()
175 for t in c:
176 total['packets'] += t[self.stat_index]['packets']
177 return total
178 else:
179 # +1 to skip main thread
180 return c[worker+1][self.stat_index]
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800181
Neale Ranns311124e2019-01-24 04:52:25 -0800182
183class VppIpsecSA(VppObject):
184 """
185 VPP SAD Entry
186 """
187
Neale Rannsabc56602020-04-01 09:45:23 +0000188 DEFAULT_UDP_PORT = 4500
189
Neale Ranns311124e2019-01-24 04:52:25 -0800190 def __init__(self, test, id, spi,
191 integ_alg, integ_key,
192 crypto_alg, crypto_key,
193 proto,
194 tun_src=None, tun_dst=None,
Neale Ranns041add72020-01-02 04:06:10 +0000195 flags=None, salt=0, tun_flags=None,
196 dscp=None,
Neale Ranns9ec846c2021-02-09 14:04:02 +0000197 udp_src=None, udp_dst=None, hop_limit=None):
Neale Ranns17dcec02019-01-09 21:22:20 -0800198 e = VppEnum.vl_api_ipsec_sad_flags_t
Neale Ranns311124e2019-01-24 04:52:25 -0800199 self.test = test
200 self.id = id
201 self.spi = spi
202 self.integ_alg = integ_alg
203 self.integ_key = integ_key
204 self.crypto_alg = crypto_alg
205 self.crypto_key = crypto_key
206 self.proto = proto
Neale Ranns80f6fd52019-04-16 02:41:34 +0000207 self.salt = salt
Neale Ranns17dcec02019-01-09 21:22:20 -0800208
Neale Ranns9ec846c2021-02-09 14:04:02 +0000209 self.table_id = 0
Neale Ranns311124e2019-01-24 04:52:25 -0800210 self.tun_src = tun_src
211 self.tun_dst = tun_dst
Neale Ranns17dcec02019-01-09 21:22:20 -0800212 if not flags:
213 self.flags = e.IPSEC_API_SAD_FLAG_NONE
214 else:
215 self.flags = flags
Neale Ranns311124e2019-01-24 04:52:25 -0800216 if (tun_src):
217 self.tun_src = ip_address(text_type(tun_src))
Neale Ranns17dcec02019-01-09 21:22:20 -0800218 self.flags = self.flags | e.IPSEC_API_SAD_FLAG_IS_TUNNEL
Neale Ranns311124e2019-01-24 04:52:25 -0800219 if (self.tun_src.version == 6):
Neale Ranns17dcec02019-01-09 21:22:20 -0800220 self.flags = self.flags | e.IPSEC_API_SAD_FLAG_IS_TUNNEL_V6
Neale Ranns311124e2019-01-24 04:52:25 -0800221 if (tun_dst):
222 self.tun_dst = ip_address(text_type(tun_dst))
Neale Rannsabc56602020-04-01 09:45:23 +0000223 self.udp_src = udp_src
224 self.udp_dst = udp_dst
Neale Ranns041add72020-01-02 04:06:10 +0000225 self.tun_flags = (VppEnum.vl_api_tunnel_encap_decap_flags_t.
226 TUNNEL_API_ENCAP_DECAP_FLAG_NONE)
227 if tun_flags:
228 self.tun_flags = tun_flags
229 self.dscp = VppEnum.vl_api_ip_dscp_t.IP_API_DSCP_CS0
230 if dscp:
231 self.dscp = dscp
Neale Ranns9ec846c2021-02-09 14:04:02 +0000232 self.hop_limit = 255
233 if hop_limit:
234 self.hop_limit = hop_limit
235
236 def tunnel_encode(self):
237 return {'src': (self.tun_src if self.tun_src else []),
238 'dst': (self.tun_dst if self.tun_dst else []),
239 'encap_decap_flags': self.tun_flags,
240 'dscp': self.dscp,
241 'hop_limit': self.hop_limit,
242 'table_id': self.table_id
243 }
Neale Ranns311124e2019-01-24 04:52:25 -0800244
245 def add_vpp_config(self):
Neale Rannsabc56602020-04-01 09:45:23 +0000246 entry = {
247 'sad_id': self.id,
248 'spi': self.spi,
249 'integrity_algorithm': self.integ_alg,
250 'integrity_key': {
251 'length': len(self.integ_key),
252 'data': self.integ_key,
253 },
254 'crypto_algorithm': self.crypto_alg,
255 'crypto_key': {
256 'data': self.crypto_key,
257 'length': len(self.crypto_key),
258 },
259 'protocol': self.proto,
Neale Ranns9ec846c2021-02-09 14:04:02 +0000260 'tunnel': self.tunnel_encode(),
Neale Rannsabc56602020-04-01 09:45:23 +0000261 'flags': self.flags,
262 'salt': self.salt
263 }
264 # don't explicitly send the defaults, let papi fill them in
265 if self.udp_src:
266 entry['udp_src_port'] = self.udp_src
267 if self.udp_dst:
268 entry['udp_dst_port'] = self.udp_dst
Neale Ranns9ec846c2021-02-09 14:04:02 +0000269 r = self.test.vapi.ipsec_sad_entry_add_del_v3(is_add=1, entry=entry)
Neale Rannseba31ec2019-02-17 18:04:27 +0000270 self.stat_index = r.stat_index
Neale Ranns311124e2019-01-24 04:52:25 -0800271 self.test.registry.register(self, self.test.logger)
Neale Rannsa9e27742020-12-23 16:22:28 +0000272 return self
Neale Ranns311124e2019-01-24 04:52:25 -0800273
274 def remove_vpp_config(self):
Neale Ranns9ec846c2021-02-09 14:04:02 +0000275 r = self.test.vapi.ipsec_sad_entry_add_del_v3(
Neale Rannsabc56602020-04-01 09:45:23 +0000276 is_add=0,
277 entry={
278 'sad_id': self.id,
279 'spi': self.spi,
280 'integrity_algorithm': self.integ_alg,
281 'integrity_key': {
282 'length': len(self.integ_key),
283 'data': self.integ_key,
284 },
285 'crypto_algorithm': self.crypto_alg,
286 'crypto_key': {
287 'data': self.crypto_key,
288 'length': len(self.crypto_key),
289 },
290 'protocol': self.proto,
Neale Ranns9ec846c2021-02-09 14:04:02 +0000291 'tunnel': self.tunnel_encode(),
Neale Rannsabc56602020-04-01 09:45:23 +0000292 'salt': self.salt
293 })
Neale Ranns311124e2019-01-24 04:52:25 -0800294
Neale Ranns311124e2019-01-24 04:52:25 -0800295 def object_id(self):
296 return "ipsec-sa-%d" % self.id
297
298 def query_vpp_config(self):
Neale Rannsabc56602020-04-01 09:45:23 +0000299 e = VppEnum.vl_api_ipsec_sad_flags_t
300
Neale Ranns9ec846c2021-02-09 14:04:02 +0000301 bs = self.test.vapi.ipsec_sa_v3_dump()
Neale Ranns311124e2019-01-24 04:52:25 -0800302 for b in bs:
Neale Ranns8d7c5022019-02-06 01:41:05 -0800303 if b.entry.sad_id == self.id:
Neale Rannsabc56602020-04-01 09:45:23 +0000304 # if udp encap is configured then the ports should match
305 # those configured or the default
306 if (self.flags & e.IPSEC_API_SAD_FLAG_UDP_ENCAP):
307 if not b.entry.flags & e.IPSEC_API_SAD_FLAG_UDP_ENCAP:
308 return False
309 if self.udp_src:
310 if self.udp_src != b.entry.udp_src_port:
311 return False
312 else:
313 if self.DEFAULT_UDP_PORT != b.entry.udp_src_port:
314 return False
315 if self.udp_dst:
316 if self.udp_dst != b.entry.udp_dst_port:
317 return False
318 else:
319 if self.DEFAULT_UDP_PORT != b.entry.udp_dst_port:
320 return False
Neale Ranns311124e2019-01-24 04:52:25 -0800321 return True
322 return False
Neale Rannseba31ec2019-02-17 18:04:27 +0000323
Neale Ranns4a56f4e2019-12-23 04:10:25 +0000324 def get_stats(self, worker=None):
Neale Rannseba31ec2019-02-17 18:04:27 +0000325 c = self.test.statistics.get_counter("/net/ipsec/sa")
Neale Ranns4a56f4e2019-12-23 04:10:25 +0000326 if worker is None:
327 total = mk_counter()
328 for t in c:
329 total['packets'] += t[self.stat_index]['packets']
330 return total
331 else:
332 # +1 to skip main thread
333 return c[worker+1][self.stat_index]
Neale Rannsc87b66c2019-02-07 07:26:12 -0800334
335
336class VppIpsecTunProtect(VppObject):
337 """
338 VPP IPSEC tunnel protection
339 """
340
Neale Ranns28287212019-12-16 00:53:11 +0000341 def __init__(self, test, itf, sa_out, sas_in, nh=None):
Neale Rannsc87b66c2019-02-07 07:26:12 -0800342 self.test = test
343 self.itf = itf
344 self.sas_in = []
345 for sa in sas_in:
346 self.sas_in.append(sa.id)
347 self.sa_out = sa_out.id
Neale Ranns28287212019-12-16 00:53:11 +0000348 self.nh = nh
349 if not self.nh:
350 self.nh = "0.0.0.0"
Neale Rannsc87b66c2019-02-07 07:26:12 -0800351
352 def update_vpp_config(self, sa_out, sas_in):
353 self.sas_in = []
354 for sa in sas_in:
355 self.sas_in.append(sa.id)
356 self.sa_out = sa_out.id
357 self.test.vapi.ipsec_tunnel_protect_update(
358 tunnel={
359 'sw_if_index': self.itf._sw_if_index,
360 'n_sa_in': len(self.sas_in),
361 'sa_out': self.sa_out,
Neale Ranns28287212019-12-16 00:53:11 +0000362 'sa_in': self.sas_in,
363 'nh': self.nh})
Neale Rannsc87b66c2019-02-07 07:26:12 -0800364
365 def object_id(self):
Neale Ranns28287212019-12-16 00:53:11 +0000366 return "ipsec-tun-protect-%s-%s" % (self.itf, self.nh)
Neale Rannsc87b66c2019-02-07 07:26:12 -0800367
368 def add_vpp_config(self):
369 self.test.vapi.ipsec_tunnel_protect_update(
370 tunnel={
371 'sw_if_index': self.itf._sw_if_index,
372 'n_sa_in': len(self.sas_in),
373 'sa_out': self.sa_out,
Neale Ranns28287212019-12-16 00:53:11 +0000374 'sa_in': self.sas_in,
375 'nh': self.nh})
Neale Rannsc87b66c2019-02-07 07:26:12 -0800376 self.test.registry.register(self, self.test.logger)
377
378 def remove_vpp_config(self):
379 self.test.vapi.ipsec_tunnel_protect_del(
Neale Ranns28287212019-12-16 00:53:11 +0000380 sw_if_index=self.itf.sw_if_index,
381 nh=self.nh)
Neale Rannsc87b66c2019-02-07 07:26:12 -0800382
383 def query_vpp_config(self):
384 bs = self.test.vapi.ipsec_tunnel_protect_dump(
385 sw_if_index=self.itf.sw_if_index)
386 for b in bs:
Neale Ranns28287212019-12-16 00:53:11 +0000387 if b.tun.sw_if_index == self.itf.sw_if_index and \
388 self.nh == str(b.tun.nh):
Neale Rannsc87b66c2019-02-07 07:26:12 -0800389 return True
390 return False
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000391
392
393class VppIpsecInterface(VppInterface):
394 """
395 VPP IPSec interface
396 """
397
Eric Kinzie609d5792020-10-13 20:02:11 -0400398 def __init__(self, test, mode=None, instance=0xffffffff):
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000399 super(VppIpsecInterface, self).__init__(test)
400
Neale Ranns6ba4e412020-10-19 09:59:41 +0000401 self.mode = mode
402 if not self.mode:
403 self.mode = (VppEnum.vl_api_tunnel_mode_t.
404 TUNNEL_API_MODE_P2P)
Eric Kinzie609d5792020-10-13 20:02:11 -0400405 self.instance = instance
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000406
407 def add_vpp_config(self):
408 r = self.test.vapi.ipsec_itf_create(itf={
Eric Kinzie609d5792020-10-13 20:02:11 -0400409 'user_instance': self.instance,
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000410 'mode': self.mode,
411 })
412 self.set_sw_if_index(r.sw_if_index)
413 self.test.registry.register(self, self.test.logger)
414 return self
415
416 def remove_vpp_config(self):
417 self.test.vapi.ipsec_itf_delete(sw_if_index=self._sw_if_index)
418
419 def query_vpp_config(self):
420 ts = self.test.vapi.ipsec_itf_dump(sw_if_index=0xffffffff)
421 for t in ts:
422 if t.tunnel.sw_if_index == self._sw_if_index:
423 return True
424 return False
425
426 def __str__(self):
427 return self.object_id()
428
429 def object_id(self):
430 return "ipsec-%d" % self._sw_if_index