blob: f9b7bc43752984c16925fe0df715be93d3696b3c [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)
Neale Rannsfc811342021-02-26 10:35:33 +0000131 return self
Neale Ranns311124e2019-01-24 04:52:25 -0800132
133 def remove_vpp_config(self):
Neale Ranns17dcec02019-01-09 21:22:20 -0800134 self.test.vapi.ipsec_spd_entry_add_del(
Neale Ranns311124e2019-01-24 04:52:25 -0800135 self.spd.id,
136 self.sa_id,
Neale Ranns17dcec02019-01-09 21:22:20 -0800137 self.local_start,
138 self.local_stop,
139 self.remote_start,
140 self.remote_stop,
Neale Ranns311124e2019-01-24 04:52:25 -0800141 protocol=self.proto,
142 is_ipv6=self.is_ipv6,
143 is_outbound=self.is_outbound,
144 priority=self.priority,
145 policy=self.policy,
146 local_port_start=self.local_port_start,
147 local_port_stop=self.local_port_stop,
148 remote_port_start=self.remote_port_start,
149 remote_port_stop=self.remote_port_stop,
150 is_add=0)
151
Neale Ranns311124e2019-01-24 04:52:25 -0800152 def object_id(self):
153 return "spd-entry-%d-%d-%d-%d-%d-%d" % (self.spd.id,
154 self.priority,
155 self.policy,
156 self.is_outbound,
157 self.is_ipv6,
158 self.remote_port_start)
159
160 def query_vpp_config(self):
161 ss = self.test.vapi.ipsec_spd_dump(self.spd.id)
162 for s in ss:
Neale Ranns17dcec02019-01-09 21:22:20 -0800163 if s.entry.sa_id == self.sa_id and \
164 s.entry.is_outbound == self.is_outbound and \
165 s.entry.priority == self.priority and \
166 s.entry.policy == self.policy and \
167 s.entry.remote_address_start == self.remote_start and \
168 s.entry.remote_port_start == self.remote_port_start:
Neale Ranns311124e2019-01-24 04:52:25 -0800169 return True
170 return False
171
Neale Ranns4a56f4e2019-12-23 04:10:25 +0000172 def get_stats(self, worker=None):
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800173 c = self.test.statistics.get_counter("/net/ipsec/policy")
Neale Ranns4a56f4e2019-12-23 04:10:25 +0000174 if worker is None:
175 total = mk_counter()
176 for t in c:
177 total['packets'] += t[self.stat_index]['packets']
178 return total
179 else:
180 # +1 to skip main thread
181 return c[worker+1][self.stat_index]
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800182
Neale Ranns311124e2019-01-24 04:52:25 -0800183
184class VppIpsecSA(VppObject):
185 """
186 VPP SAD Entry
187 """
188
Neale Rannsabc56602020-04-01 09:45:23 +0000189 DEFAULT_UDP_PORT = 4500
190
Neale Ranns311124e2019-01-24 04:52:25 -0800191 def __init__(self, test, id, spi,
192 integ_alg, integ_key,
193 crypto_alg, crypto_key,
194 proto,
195 tun_src=None, tun_dst=None,
Neale Ranns041add72020-01-02 04:06:10 +0000196 flags=None, salt=0, tun_flags=None,
197 dscp=None,
Neale Ranns9ec846c2021-02-09 14:04:02 +0000198 udp_src=None, udp_dst=None, hop_limit=None):
Neale Ranns17dcec02019-01-09 21:22:20 -0800199 e = VppEnum.vl_api_ipsec_sad_flags_t
Neale Ranns311124e2019-01-24 04:52:25 -0800200 self.test = test
201 self.id = id
202 self.spi = spi
203 self.integ_alg = integ_alg
204 self.integ_key = integ_key
205 self.crypto_alg = crypto_alg
206 self.crypto_key = crypto_key
207 self.proto = proto
Neale Ranns80f6fd52019-04-16 02:41:34 +0000208 self.salt = salt
Neale Ranns17dcec02019-01-09 21:22:20 -0800209
Neale Ranns9ec846c2021-02-09 14:04:02 +0000210 self.table_id = 0
Neale Ranns311124e2019-01-24 04:52:25 -0800211 self.tun_src = tun_src
212 self.tun_dst = tun_dst
Neale Ranns17dcec02019-01-09 21:22:20 -0800213 if not flags:
214 self.flags = e.IPSEC_API_SAD_FLAG_NONE
215 else:
216 self.flags = flags
Neale Ranns311124e2019-01-24 04:52:25 -0800217 if (tun_src):
218 self.tun_src = ip_address(text_type(tun_src))
Neale Ranns17dcec02019-01-09 21:22:20 -0800219 self.flags = self.flags | e.IPSEC_API_SAD_FLAG_IS_TUNNEL
Neale Ranns311124e2019-01-24 04:52:25 -0800220 if (tun_dst):
221 self.tun_dst = ip_address(text_type(tun_dst))
Neale Rannsabc56602020-04-01 09:45:23 +0000222 self.udp_src = udp_src
223 self.udp_dst = udp_dst
Neale Ranns041add72020-01-02 04:06:10 +0000224 self.tun_flags = (VppEnum.vl_api_tunnel_encap_decap_flags_t.
225 TUNNEL_API_ENCAP_DECAP_FLAG_NONE)
226 if tun_flags:
227 self.tun_flags = tun_flags
228 self.dscp = VppEnum.vl_api_ip_dscp_t.IP_API_DSCP_CS0
229 if dscp:
230 self.dscp = dscp
Neale Ranns9ec846c2021-02-09 14:04:02 +0000231 self.hop_limit = 255
232 if hop_limit:
233 self.hop_limit = hop_limit
234
235 def tunnel_encode(self):
236 return {'src': (self.tun_src if self.tun_src else []),
237 'dst': (self.tun_dst if self.tun_dst else []),
238 'encap_decap_flags': self.tun_flags,
239 'dscp': self.dscp,
240 'hop_limit': self.hop_limit,
241 'table_id': self.table_id
242 }
Neale Ranns311124e2019-01-24 04:52:25 -0800243
244 def add_vpp_config(self):
Neale Rannsabc56602020-04-01 09:45:23 +0000245 entry = {
246 'sad_id': self.id,
247 'spi': self.spi,
248 'integrity_algorithm': self.integ_alg,
249 'integrity_key': {
250 'length': len(self.integ_key),
251 'data': self.integ_key,
252 },
253 'crypto_algorithm': self.crypto_alg,
254 'crypto_key': {
255 'data': self.crypto_key,
256 'length': len(self.crypto_key),
257 },
258 'protocol': self.proto,
Neale Ranns9ec846c2021-02-09 14:04:02 +0000259 'tunnel': self.tunnel_encode(),
Neale Rannsabc56602020-04-01 09:45:23 +0000260 'flags': self.flags,
261 'salt': self.salt
262 }
263 # don't explicitly send the defaults, let papi fill them in
264 if self.udp_src:
265 entry['udp_src_port'] = self.udp_src
266 if self.udp_dst:
267 entry['udp_dst_port'] = self.udp_dst
Neale Rannsff2e4132021-06-24 14:57:56 +0000268 r = self.test.vapi.ipsec_sad_entry_add(entry=entry)
Neale Rannseba31ec2019-02-17 18:04:27 +0000269 self.stat_index = r.stat_index
Neale Ranns311124e2019-01-24 04:52:25 -0800270 self.test.registry.register(self, self.test.logger)
Neale Rannsa9e27742020-12-23 16:22:28 +0000271 return self
Neale Ranns311124e2019-01-24 04:52:25 -0800272
273 def remove_vpp_config(self):
Neale Rannsff2e4132021-06-24 14:57:56 +0000274 self.test.vapi.ipsec_sad_entry_del(id=self.id)
Neale Ranns311124e2019-01-24 04:52:25 -0800275
Neale Ranns311124e2019-01-24 04:52:25 -0800276 def object_id(self):
277 return "ipsec-sa-%d" % self.id
278
279 def query_vpp_config(self):
Neale Rannsabc56602020-04-01 09:45:23 +0000280 e = VppEnum.vl_api_ipsec_sad_flags_t
281
Neale Ranns9ec846c2021-02-09 14:04:02 +0000282 bs = self.test.vapi.ipsec_sa_v3_dump()
Neale Ranns311124e2019-01-24 04:52:25 -0800283 for b in bs:
Neale Ranns8d7c5022019-02-06 01:41:05 -0800284 if b.entry.sad_id == self.id:
Neale Rannsabc56602020-04-01 09:45:23 +0000285 # if udp encap is configured then the ports should match
286 # those configured or the default
287 if (self.flags & e.IPSEC_API_SAD_FLAG_UDP_ENCAP):
288 if not b.entry.flags & e.IPSEC_API_SAD_FLAG_UDP_ENCAP:
289 return False
290 if self.udp_src:
291 if self.udp_src != b.entry.udp_src_port:
292 return False
293 else:
294 if self.DEFAULT_UDP_PORT != b.entry.udp_src_port:
295 return False
296 if self.udp_dst:
297 if self.udp_dst != b.entry.udp_dst_port:
298 return False
299 else:
300 if self.DEFAULT_UDP_PORT != b.entry.udp_dst_port:
301 return False
Neale Ranns311124e2019-01-24 04:52:25 -0800302 return True
303 return False
Neale Rannseba31ec2019-02-17 18:04:27 +0000304
Neale Ranns4a56f4e2019-12-23 04:10:25 +0000305 def get_stats(self, worker=None):
Neale Rannseba31ec2019-02-17 18:04:27 +0000306 c = self.test.statistics.get_counter("/net/ipsec/sa")
Neale Ranns4a56f4e2019-12-23 04:10:25 +0000307 if worker is None:
308 total = mk_counter()
309 for t in c:
310 total['packets'] += t[self.stat_index]['packets']
311 return total
312 else:
313 # +1 to skip main thread
314 return c[worker+1][self.stat_index]
Neale Rannsc87b66c2019-02-07 07:26:12 -0800315
316
317class VppIpsecTunProtect(VppObject):
318 """
319 VPP IPSEC tunnel protection
320 """
321
Neale Ranns28287212019-12-16 00:53:11 +0000322 def __init__(self, test, itf, sa_out, sas_in, nh=None):
Neale Rannsc87b66c2019-02-07 07:26:12 -0800323 self.test = test
324 self.itf = itf
325 self.sas_in = []
326 for sa in sas_in:
327 self.sas_in.append(sa.id)
328 self.sa_out = sa_out.id
Neale Ranns28287212019-12-16 00:53:11 +0000329 self.nh = nh
330 if not self.nh:
331 self.nh = "0.0.0.0"
Neale Rannsc87b66c2019-02-07 07:26:12 -0800332
333 def update_vpp_config(self, sa_out, sas_in):
334 self.sas_in = []
335 for sa in sas_in:
336 self.sas_in.append(sa.id)
337 self.sa_out = sa_out.id
338 self.test.vapi.ipsec_tunnel_protect_update(
339 tunnel={
340 'sw_if_index': self.itf._sw_if_index,
341 'n_sa_in': len(self.sas_in),
342 'sa_out': self.sa_out,
Neale Ranns28287212019-12-16 00:53:11 +0000343 'sa_in': self.sas_in,
344 'nh': self.nh})
Neale Rannsc87b66c2019-02-07 07:26:12 -0800345
346 def object_id(self):
Neale Ranns28287212019-12-16 00:53:11 +0000347 return "ipsec-tun-protect-%s-%s" % (self.itf, self.nh)
Neale Rannsc87b66c2019-02-07 07:26:12 -0800348
349 def add_vpp_config(self):
350 self.test.vapi.ipsec_tunnel_protect_update(
351 tunnel={
352 'sw_if_index': self.itf._sw_if_index,
353 'n_sa_in': len(self.sas_in),
354 'sa_out': self.sa_out,
Neale Ranns28287212019-12-16 00:53:11 +0000355 'sa_in': self.sas_in,
356 'nh': self.nh})
Neale Rannsc87b66c2019-02-07 07:26:12 -0800357 self.test.registry.register(self, self.test.logger)
358
359 def remove_vpp_config(self):
360 self.test.vapi.ipsec_tunnel_protect_del(
Neale Ranns28287212019-12-16 00:53:11 +0000361 sw_if_index=self.itf.sw_if_index,
362 nh=self.nh)
Neale Rannsc87b66c2019-02-07 07:26:12 -0800363
364 def query_vpp_config(self):
365 bs = self.test.vapi.ipsec_tunnel_protect_dump(
366 sw_if_index=self.itf.sw_if_index)
367 for b in bs:
Neale Ranns28287212019-12-16 00:53:11 +0000368 if b.tun.sw_if_index == self.itf.sw_if_index and \
369 self.nh == str(b.tun.nh):
Neale Rannsc87b66c2019-02-07 07:26:12 -0800370 return True
371 return False
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000372
373
374class VppIpsecInterface(VppInterface):
375 """
376 VPP IPSec interface
377 """
378
Eric Kinzie609d5792020-10-13 20:02:11 -0400379 def __init__(self, test, mode=None, instance=0xffffffff):
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000380 super(VppIpsecInterface, self).__init__(test)
381
Neale Ranns6ba4e412020-10-19 09:59:41 +0000382 self.mode = mode
383 if not self.mode:
384 self.mode = (VppEnum.vl_api_tunnel_mode_t.
385 TUNNEL_API_MODE_P2P)
Eric Kinzie609d5792020-10-13 20:02:11 -0400386 self.instance = instance
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000387
388 def add_vpp_config(self):
389 r = self.test.vapi.ipsec_itf_create(itf={
Eric Kinzie609d5792020-10-13 20:02:11 -0400390 'user_instance': self.instance,
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000391 'mode': self.mode,
392 })
393 self.set_sw_if_index(r.sw_if_index)
394 self.test.registry.register(self, self.test.logger)
Neale Ranns89d939e2021-06-07 09:34:07 +0000395 ts = self.test.vapi.ipsec_itf_dump(sw_if_index=self._sw_if_index)
396 self.instance = ts[0].itf.user_instance
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000397 return self
398
399 def remove_vpp_config(self):
400 self.test.vapi.ipsec_itf_delete(sw_if_index=self._sw_if_index)
401
402 def query_vpp_config(self):
403 ts = self.test.vapi.ipsec_itf_dump(sw_if_index=0xffffffff)
404 for t in ts:
Neale Ranns89d939e2021-06-07 09:34:07 +0000405 if t.itf.sw_if_index == self._sw_if_index:
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000406 return True
407 return False
408
409 def __str__(self):
410 return self.object_id()
411
412 def object_id(self):
Neale Ranns89d939e2021-06-07 09:34:07 +0000413 return "ipsec%d" % self.instance