blob: eb0209fc57a6c36b8a623031f3df5c8d1310fa3f [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():
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020013 return {"packets": 0, "bytes": 0}
Neale Ranns4a56f4e2019-12-23 04:10:25 +000014
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):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020055 self.test.vapi.ipsec_interface_add_del_spd(self.spd.id, self.itf.sw_if_index)
Neale Ranns311124e2019-01-24 04:52:25 -080056 self.test.registry.register(self, self.test.logger)
57
58 def remove_vpp_config(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020059 self.test.vapi.ipsec_interface_add_del_spd(
60 self.spd.id, self.itf.sw_if_index, is_add=0
61 )
Neale Ranns311124e2019-01-24 04:52:25 -080062
Neale Ranns311124e2019-01-24 04:52:25 -080063 def object_id(self):
64 return "bind-%s-to-%s" % (self.spd.id, self.itf)
65
66 def query_vpp_config(self):
67 bs = self.test.vapi.ipsec_spd_interface_dump()
68 for b in bs:
69 if b.sw_if_index == self.itf.sw_if_index:
70 return True
71 return False
72
73
74class VppIpsecSpdEntry(VppObject):
75 """
76 VPP SPD DB Entry
77 """
78
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020079 def __init__(
80 self,
81 test,
82 spd,
83 sa_id,
84 local_start,
85 local_stop,
86 remote_start,
87 remote_stop,
Piotr Bronowski815c6a42022-06-09 09:09:28 +000088 proto=socket.IPPROTO_RAW,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020089 priority=100,
90 policy=None,
91 is_outbound=1,
92 remote_port_start=0,
93 remote_port_stop=65535,
94 local_port_start=0,
95 local_port_stop=65535,
96 ):
Neale Ranns311124e2019-01-24 04:52:25 -080097 self.test = test
98 self.spd = spd
99 self.sa_id = sa_id
100 self.local_start = ip_address(text_type(local_start))
101 self.local_stop = ip_address(text_type(local_stop))
102 self.remote_start = ip_address(text_type(remote_start))
103 self.remote_stop = ip_address(text_type(remote_stop))
104 self.proto = proto
105 self.is_outbound = is_outbound
106 self.priority = priority
Neale Ranns17dcec02019-01-09 21:22:20 -0800107 if not policy:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200108 self.policy = VppEnum.vl_api_ipsec_spd_action_t.IPSEC_API_SPD_ACTION_BYPASS
Neale Ranns17dcec02019-01-09 21:22:20 -0800109 else:
110 self.policy = policy
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200111 self.is_ipv6 = 0 if self.local_start.version == 4 else 1
Neale Ranns311124e2019-01-24 04:52:25 -0800112 self.local_port_start = local_port_start
113 self.local_port_stop = local_port_stop
114 self.remote_port_start = remote_port_start
115 self.remote_port_stop = remote_port_stop
116
117 def add_vpp_config(self):
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800118 rv = self.test.vapi.ipsec_spd_entry_add_del(
Neale Ranns311124e2019-01-24 04:52:25 -0800119 self.spd.id,
120 self.sa_id,
Neale Ranns17dcec02019-01-09 21:22:20 -0800121 self.local_start,
122 self.local_stop,
123 self.remote_start,
124 self.remote_stop,
Neale Ranns311124e2019-01-24 04:52:25 -0800125 protocol=self.proto,
126 is_ipv6=self.is_ipv6,
127 is_outbound=self.is_outbound,
128 priority=self.priority,
129 policy=self.policy,
130 local_port_start=self.local_port_start,
131 local_port_stop=self.local_port_stop,
132 remote_port_start=self.remote_port_start,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200133 remote_port_stop=self.remote_port_stop,
134 )
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800135 self.stat_index = rv.stat_index
Neale Ranns311124e2019-01-24 04:52:25 -0800136 self.test.registry.register(self, self.test.logger)
Neale Rannsfc811342021-02-26 10:35:33 +0000137 return self
Neale Ranns311124e2019-01-24 04:52:25 -0800138
139 def remove_vpp_config(self):
Neale Ranns17dcec02019-01-09 21:22:20 -0800140 self.test.vapi.ipsec_spd_entry_add_del(
Neale Ranns311124e2019-01-24 04:52:25 -0800141 self.spd.id,
142 self.sa_id,
Neale Ranns17dcec02019-01-09 21:22:20 -0800143 self.local_start,
144 self.local_stop,
145 self.remote_start,
146 self.remote_stop,
Neale Ranns311124e2019-01-24 04:52:25 -0800147 protocol=self.proto,
148 is_ipv6=self.is_ipv6,
149 is_outbound=self.is_outbound,
150 priority=self.priority,
151 policy=self.policy,
152 local_port_start=self.local_port_start,
153 local_port_stop=self.local_port_stop,
154 remote_port_start=self.remote_port_start,
155 remote_port_stop=self.remote_port_stop,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200156 is_add=0,
157 )
Neale Ranns311124e2019-01-24 04:52:25 -0800158
Neale Ranns311124e2019-01-24 04:52:25 -0800159 def object_id(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200160 return "spd-entry-%d-%d-%d-%d-%d-%d" % (
161 self.spd.id,
162 self.priority,
163 self.policy,
164 self.is_outbound,
165 self.is_ipv6,
166 self.remote_port_start,
167 )
Neale Ranns311124e2019-01-24 04:52:25 -0800168
169 def query_vpp_config(self):
170 ss = self.test.vapi.ipsec_spd_dump(self.spd.id)
171 for s in ss:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200172 if (
173 s.entry.sa_id == self.sa_id
174 and s.entry.is_outbound == self.is_outbound
175 and s.entry.priority == self.priority
176 and s.entry.policy == self.policy
177 and s.entry.remote_address_start == self.remote_start
178 and s.entry.remote_port_start == self.remote_port_start
179 ):
Neale Ranns311124e2019-01-24 04:52:25 -0800180 return True
181 return False
182
Neale Ranns4a56f4e2019-12-23 04:10:25 +0000183 def get_stats(self, worker=None):
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800184 c = self.test.statistics.get_counter("/net/ipsec/policy")
Neale Ranns4a56f4e2019-12-23 04:10:25 +0000185 if worker is None:
186 total = mk_counter()
187 for t in c:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200188 total["packets"] += t[self.stat_index]["packets"]
Neale Ranns4a56f4e2019-12-23 04:10:25 +0000189 return total
190 else:
191 # +1 to skip main thread
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200192 return c[worker + 1][self.stat_index]
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800193
Neale Ranns311124e2019-01-24 04:52:25 -0800194
195class VppIpsecSA(VppObject):
196 """
197 VPP SAD Entry
198 """
199
Neale Rannsabc56602020-04-01 09:45:23 +0000200 DEFAULT_UDP_PORT = 4500
201
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200202 def __init__(
203 self,
204 test,
205 id,
206 spi,
207 integ_alg,
208 integ_key,
209 crypto_alg,
210 crypto_key,
211 proto,
212 tun_src=None,
213 tun_dst=None,
214 flags=None,
215 salt=0,
216 tun_flags=None,
217 dscp=None,
218 udp_src=None,
219 udp_dst=None,
220 hop_limit=None,
221 ):
Neale Ranns17dcec02019-01-09 21:22:20 -0800222 e = VppEnum.vl_api_ipsec_sad_flags_t
Neale Ranns311124e2019-01-24 04:52:25 -0800223 self.test = test
224 self.id = id
225 self.spi = spi
226 self.integ_alg = integ_alg
227 self.integ_key = integ_key
228 self.crypto_alg = crypto_alg
229 self.crypto_key = crypto_key
230 self.proto = proto
Neale Ranns80f6fd52019-04-16 02:41:34 +0000231 self.salt = salt
Neale Ranns17dcec02019-01-09 21:22:20 -0800232
Neale Ranns9ec846c2021-02-09 14:04:02 +0000233 self.table_id = 0
Neale Ranns311124e2019-01-24 04:52:25 -0800234 self.tun_src = tun_src
235 self.tun_dst = tun_dst
Neale Ranns17dcec02019-01-09 21:22:20 -0800236 if not flags:
237 self.flags = e.IPSEC_API_SAD_FLAG_NONE
238 else:
239 self.flags = flags
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200240 if tun_src:
Neale Ranns311124e2019-01-24 04:52:25 -0800241 self.tun_src = ip_address(text_type(tun_src))
Neale Ranns17dcec02019-01-09 21:22:20 -0800242 self.flags = self.flags | e.IPSEC_API_SAD_FLAG_IS_TUNNEL
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200243 if tun_dst:
Neale Ranns311124e2019-01-24 04:52:25 -0800244 self.tun_dst = ip_address(text_type(tun_dst))
Neale Rannsabc56602020-04-01 09:45:23 +0000245 self.udp_src = udp_src
246 self.udp_dst = udp_dst
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200247 self.tun_flags = (
248 VppEnum.vl_api_tunnel_encap_decap_flags_t.TUNNEL_API_ENCAP_DECAP_FLAG_NONE
249 )
Neale Ranns041add72020-01-02 04:06:10 +0000250 if tun_flags:
251 self.tun_flags = tun_flags
252 self.dscp = VppEnum.vl_api_ip_dscp_t.IP_API_DSCP_CS0
253 if dscp:
254 self.dscp = dscp
Neale Ranns9ec846c2021-02-09 14:04:02 +0000255 self.hop_limit = 255
256 if hop_limit:
257 self.hop_limit = hop_limit
258
259 def tunnel_encode(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200260 return {
261 "src": (self.tun_src if self.tun_src else []),
262 "dst": (self.tun_dst if self.tun_dst else []),
263 "encap_decap_flags": self.tun_flags,
264 "dscp": self.dscp,
265 "hop_limit": self.hop_limit,
266 "table_id": self.table_id,
267 }
Neale Ranns311124e2019-01-24 04:52:25 -0800268
269 def add_vpp_config(self):
Neale Rannsabc56602020-04-01 09:45:23 +0000270 entry = {
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200271 "sad_id": self.id,
272 "spi": self.spi,
273 "integrity_algorithm": self.integ_alg,
274 "integrity_key": {
275 "length": len(self.integ_key),
276 "data": self.integ_key,
Neale Rannsabc56602020-04-01 09:45:23 +0000277 },
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200278 "crypto_algorithm": self.crypto_alg,
279 "crypto_key": {
280 "data": self.crypto_key,
281 "length": len(self.crypto_key),
Neale Rannsabc56602020-04-01 09:45:23 +0000282 },
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200283 "protocol": self.proto,
284 "tunnel": self.tunnel_encode(),
285 "flags": self.flags,
286 "salt": self.salt,
Neale Rannsabc56602020-04-01 09:45:23 +0000287 }
288 # don't explicitly send the defaults, let papi fill them in
289 if self.udp_src:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200290 entry["udp_src_port"] = self.udp_src
Neale Rannsabc56602020-04-01 09:45:23 +0000291 if self.udp_dst:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200292 entry["udp_dst_port"] = self.udp_dst
Neale Rannsff2e4132021-06-24 14:57:56 +0000293 r = self.test.vapi.ipsec_sad_entry_add(entry=entry)
Neale Rannseba31ec2019-02-17 18:04:27 +0000294 self.stat_index = r.stat_index
Neale Ranns311124e2019-01-24 04:52:25 -0800295 self.test.registry.register(self, self.test.logger)
Neale Rannsa9e27742020-12-23 16:22:28 +0000296 return self
Neale Ranns311124e2019-01-24 04:52:25 -0800297
298 def remove_vpp_config(self):
Neale Rannsff2e4132021-06-24 14:57:56 +0000299 self.test.vapi.ipsec_sad_entry_del(id=self.id)
Neale Ranns311124e2019-01-24 04:52:25 -0800300
Neale Ranns311124e2019-01-24 04:52:25 -0800301 def object_id(self):
302 return "ipsec-sa-%d" % self.id
303
304 def query_vpp_config(self):
Neale Rannsabc56602020-04-01 09:45:23 +0000305 e = VppEnum.vl_api_ipsec_sad_flags_t
306
Neale Ranns9ec846c2021-02-09 14:04:02 +0000307 bs = self.test.vapi.ipsec_sa_v3_dump()
Neale Ranns311124e2019-01-24 04:52:25 -0800308 for b in bs:
Neale Ranns8d7c5022019-02-06 01:41:05 -0800309 if b.entry.sad_id == self.id:
Neale Rannsabc56602020-04-01 09:45:23 +0000310 # if udp encap is configured then the ports should match
311 # those configured or the default
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200312 if self.flags & e.IPSEC_API_SAD_FLAG_UDP_ENCAP:
Neale Rannsabc56602020-04-01 09:45:23 +0000313 if not b.entry.flags & e.IPSEC_API_SAD_FLAG_UDP_ENCAP:
314 return False
315 if self.udp_src:
316 if self.udp_src != b.entry.udp_src_port:
317 return False
318 else:
319 if self.DEFAULT_UDP_PORT != b.entry.udp_src_port:
320 return False
321 if self.udp_dst:
322 if self.udp_dst != b.entry.udp_dst_port:
323 return False
324 else:
325 if self.DEFAULT_UDP_PORT != b.entry.udp_dst_port:
326 return False
Neale Ranns311124e2019-01-24 04:52:25 -0800327 return True
328 return False
Neale Rannseba31ec2019-02-17 18:04:27 +0000329
Neale Ranns4a56f4e2019-12-23 04:10:25 +0000330 def get_stats(self, worker=None):
Neale Rannseba31ec2019-02-17 18:04:27 +0000331 c = self.test.statistics.get_counter("/net/ipsec/sa")
Neale Ranns4a56f4e2019-12-23 04:10:25 +0000332 if worker is None:
333 total = mk_counter()
334 for t in c:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200335 total["packets"] += t[self.stat_index]["packets"]
Neale Ranns4a56f4e2019-12-23 04:10:25 +0000336 return total
337 else:
338 # +1 to skip main thread
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200339 return c[worker + 1][self.stat_index]
Neale Rannsc87b66c2019-02-07 07:26:12 -0800340
Neale Rannse11203e2021-09-21 12:34:19 +0000341 def get_lost(self, worker=None):
342 c = self.test.statistics.get_counter("/net/ipsec/sa/lost")
343 if worker is None:
344 total = 0
345 for t in c:
346 total += t[self.stat_index]
347 return total
348 else:
349 # +1 to skip main thread
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200350 return c[worker + 1][self.stat_index]
Neale Rannse11203e2021-09-21 12:34:19 +0000351
Neale Rannsc87b66c2019-02-07 07:26:12 -0800352
353class VppIpsecTunProtect(VppObject):
354 """
355 VPP IPSEC tunnel protection
356 """
357
Neale Ranns28287212019-12-16 00:53:11 +0000358 def __init__(self, test, itf, sa_out, sas_in, nh=None):
Neale Rannsc87b66c2019-02-07 07:26:12 -0800359 self.test = test
360 self.itf = itf
361 self.sas_in = []
362 for sa in sas_in:
363 self.sas_in.append(sa.id)
364 self.sa_out = sa_out.id
Neale Ranns28287212019-12-16 00:53:11 +0000365 self.nh = nh
366 if not self.nh:
367 self.nh = "0.0.0.0"
Neale Rannsc87b66c2019-02-07 07:26:12 -0800368
369 def update_vpp_config(self, sa_out, sas_in):
370 self.sas_in = []
371 for sa in sas_in:
372 self.sas_in.append(sa.id)
373 self.sa_out = sa_out.id
374 self.test.vapi.ipsec_tunnel_protect_update(
375 tunnel={
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200376 "sw_if_index": self.itf._sw_if_index,
377 "n_sa_in": len(self.sas_in),
378 "sa_out": self.sa_out,
379 "sa_in": self.sas_in,
380 "nh": self.nh,
381 }
382 )
Neale Rannsc87b66c2019-02-07 07:26:12 -0800383
384 def object_id(self):
Neale Ranns28287212019-12-16 00:53:11 +0000385 return "ipsec-tun-protect-%s-%s" % (self.itf, self.nh)
Neale Rannsc87b66c2019-02-07 07:26:12 -0800386
387 def add_vpp_config(self):
388 self.test.vapi.ipsec_tunnel_protect_update(
389 tunnel={
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200390 "sw_if_index": self.itf._sw_if_index,
391 "n_sa_in": len(self.sas_in),
392 "sa_out": self.sa_out,
393 "sa_in": self.sas_in,
394 "nh": self.nh,
395 }
396 )
Neale Rannsc87b66c2019-02-07 07:26:12 -0800397 self.test.registry.register(self, self.test.logger)
398
399 def remove_vpp_config(self):
400 self.test.vapi.ipsec_tunnel_protect_del(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200401 sw_if_index=self.itf.sw_if_index, nh=self.nh
402 )
Neale Rannsc87b66c2019-02-07 07:26:12 -0800403
404 def query_vpp_config(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200405 bs = self.test.vapi.ipsec_tunnel_protect_dump(sw_if_index=self.itf.sw_if_index)
Neale Rannsc87b66c2019-02-07 07:26:12 -0800406 for b in bs:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200407 if b.tun.sw_if_index == self.itf.sw_if_index and self.nh == str(b.tun.nh):
Neale Rannsc87b66c2019-02-07 07:26:12 -0800408 return True
409 return False
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000410
411
412class VppIpsecInterface(VppInterface):
413 """
414 VPP IPSec interface
415 """
416
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200417 def __init__(self, test, mode=None, instance=0xFFFFFFFF):
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000418 super(VppIpsecInterface, self).__init__(test)
419
Neale Ranns6ba4e412020-10-19 09:59:41 +0000420 self.mode = mode
421 if not self.mode:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200422 self.mode = VppEnum.vl_api_tunnel_mode_t.TUNNEL_API_MODE_P2P
Eric Kinzie609d5792020-10-13 20:02:11 -0400423 self.instance = instance
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000424
425 def add_vpp_config(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200426 r = self.test.vapi.ipsec_itf_create(
427 itf={
428 "user_instance": self.instance,
429 "mode": self.mode,
430 }
431 )
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000432 self.set_sw_if_index(r.sw_if_index)
433 self.test.registry.register(self, self.test.logger)
Neale Ranns89d939e2021-06-07 09:34:07 +0000434 ts = self.test.vapi.ipsec_itf_dump(sw_if_index=self._sw_if_index)
435 self.instance = ts[0].itf.user_instance
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000436 return self
437
438 def remove_vpp_config(self):
439 self.test.vapi.ipsec_itf_delete(sw_if_index=self._sw_if_index)
440
441 def query_vpp_config(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200442 ts = self.test.vapi.ipsec_itf_dump(sw_if_index=0xFFFFFFFF)
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000443 for t in ts:
Neale Ranns89d939e2021-06-07 09:34:07 +0000444 if t.itf.sw_if_index == self._sw_if_index:
Neale Rannsdd4ccf22020-06-30 07:47:14 +0000445 return True
446 return False
447
448 def __str__(self):
449 return self.object_id()
450
451 def object_id(self):
Neale Ranns89d939e2021-06-07 09:34:07 +0000452 return "ipsec%d" % self.instance