blob: 8144ea27c8f792535fe83729abb1342b31e0af8a [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
Neale Ranns4a56f4e2019-12-23 04:10:25 +000011def mk_counter():
12 return {'packets': 0, 'bytes': 0}
13
14
Neale Ranns311124e2019-01-24 04:52:25 -080015class VppIpsecSpd(VppObject):
16 """
17 VPP SPD DB
18 """
19
20 def __init__(self, test, id):
21 self.test = test
22 self.id = id
23
24 def add_vpp_config(self):
25 self.test.vapi.ipsec_spd_add_del(self.id)
26 self.test.registry.register(self, self.test.logger)
27
28 def remove_vpp_config(self):
29 self.test.vapi.ipsec_spd_add_del(self.id, is_add=0)
30
Neale Ranns311124e2019-01-24 04:52:25 -080031 def object_id(self):
32 return "ipsec-spd-%d" % self.id
33
34 def query_vpp_config(self):
35 spds = self.test.vapi.ipsec_spds_dump()
36 for spd in spds:
37 if spd.spd_id == self.id:
38 return True
39 return False
40
41
42class VppIpsecSpdItfBinding(VppObject):
43 """
44 VPP SPD DB to interface binding
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -070045 (i.e. this SPD is used on this interface)
Neale Ranns311124e2019-01-24 04:52:25 -080046 """
47
48 def __init__(self, test, spd, itf):
49 self.test = test
50 self.spd = spd
51 self.itf = itf
52
53 def add_vpp_config(self):
54 self.test.vapi.ipsec_interface_add_del_spd(self.spd.id,
55 self.itf.sw_if_index)
56 self.test.registry.register(self, self.test.logger)
57
58 def remove_vpp_config(self):
59 self.test.vapi.ipsec_interface_add_del_spd(self.spd.id,
60 self.itf.sw_if_index,
61 is_add=0)
62
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
79 def __init__(self, test, spd, sa_id,
80 local_start, local_stop,
81 remote_start, remote_stop,
82 proto,
83 priority=100,
Neale Ranns17dcec02019-01-09 21:22:20 -080084 policy=None,
Neale Ranns311124e2019-01-24 04:52:25 -080085 is_outbound=1,
86 remote_port_start=0,
87 remote_port_stop=65535,
88 local_port_start=0,
89 local_port_stop=65535):
90 self.test = test
91 self.spd = spd
92 self.sa_id = sa_id
93 self.local_start = ip_address(text_type(local_start))
94 self.local_stop = ip_address(text_type(local_stop))
95 self.remote_start = ip_address(text_type(remote_start))
96 self.remote_stop = ip_address(text_type(remote_stop))
97 self.proto = proto
98 self.is_outbound = is_outbound
99 self.priority = priority
Neale Ranns17dcec02019-01-09 21:22:20 -0800100 if not policy:
101 self.policy = (VppEnum.vl_api_ipsec_spd_action_t.
102 IPSEC_API_SPD_ACTION_BYPASS)
103 else:
104 self.policy = policy
Neale Ranns311124e2019-01-24 04:52:25 -0800105 self.is_ipv6 = (0 if self.local_start.version == 4 else 1)
106 self.local_port_start = local_port_start
107 self.local_port_stop = local_port_stop
108 self.remote_port_start = remote_port_start
109 self.remote_port_stop = remote_port_stop
110
111 def add_vpp_config(self):
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800112 rv = self.test.vapi.ipsec_spd_entry_add_del(
Neale Ranns311124e2019-01-24 04:52:25 -0800113 self.spd.id,
114 self.sa_id,
Neale Ranns17dcec02019-01-09 21:22:20 -0800115 self.local_start,
116 self.local_stop,
117 self.remote_start,
118 self.remote_stop,
Neale Ranns311124e2019-01-24 04:52:25 -0800119 protocol=self.proto,
120 is_ipv6=self.is_ipv6,
121 is_outbound=self.is_outbound,
122 priority=self.priority,
123 policy=self.policy,
124 local_port_start=self.local_port_start,
125 local_port_stop=self.local_port_stop,
126 remote_port_start=self.remote_port_start,
127 remote_port_stop=self.remote_port_stop)
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800128 self.stat_index = rv.stat_index
Neale Ranns311124e2019-01-24 04:52:25 -0800129 self.test.registry.register(self, self.test.logger)
130
131 def remove_vpp_config(self):
Neale Ranns17dcec02019-01-09 21:22:20 -0800132 self.test.vapi.ipsec_spd_entry_add_del(
Neale Ranns311124e2019-01-24 04:52:25 -0800133 self.spd.id,
134 self.sa_id,
Neale Ranns17dcec02019-01-09 21:22:20 -0800135 self.local_start,
136 self.local_stop,
137 self.remote_start,
138 self.remote_stop,
Neale Ranns311124e2019-01-24 04:52:25 -0800139 protocol=self.proto,
140 is_ipv6=self.is_ipv6,
141 is_outbound=self.is_outbound,
142 priority=self.priority,
143 policy=self.policy,
144 local_port_start=self.local_port_start,
145 local_port_stop=self.local_port_stop,
146 remote_port_start=self.remote_port_start,
147 remote_port_stop=self.remote_port_stop,
148 is_add=0)
149
Neale Ranns311124e2019-01-24 04:52:25 -0800150 def object_id(self):
151 return "spd-entry-%d-%d-%d-%d-%d-%d" % (self.spd.id,
152 self.priority,
153 self.policy,
154 self.is_outbound,
155 self.is_ipv6,
156 self.remote_port_start)
157
158 def query_vpp_config(self):
159 ss = self.test.vapi.ipsec_spd_dump(self.spd.id)
160 for s in ss:
Neale Ranns17dcec02019-01-09 21:22:20 -0800161 if s.entry.sa_id == self.sa_id and \
162 s.entry.is_outbound == self.is_outbound and \
163 s.entry.priority == self.priority and \
164 s.entry.policy == self.policy and \
165 s.entry.remote_address_start == self.remote_start and \
166 s.entry.remote_port_start == self.remote_port_start:
Neale Ranns311124e2019-01-24 04:52:25 -0800167 return True
168 return False
169
Neale Ranns4a56f4e2019-12-23 04:10:25 +0000170 def get_stats(self, worker=None):
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800171 c = self.test.statistics.get_counter("/net/ipsec/policy")
Neale Ranns4a56f4e2019-12-23 04:10:25 +0000172 if worker is None:
173 total = mk_counter()
174 for t in c:
175 total['packets'] += t[self.stat_index]['packets']
176 return total
177 else:
178 # +1 to skip main thread
179 return c[worker+1][self.stat_index]
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800180
Neale Ranns311124e2019-01-24 04:52:25 -0800181
182class VppIpsecSA(VppObject):
183 """
184 VPP SAD Entry
185 """
186
187 def __init__(self, test, id, spi,
188 integ_alg, integ_key,
189 crypto_alg, crypto_key,
190 proto,
191 tun_src=None, tun_dst=None,
Neale Ranns80f6fd52019-04-16 02:41:34 +0000192 flags=None, salt=0):
Neale Ranns17dcec02019-01-09 21:22:20 -0800193 e = VppEnum.vl_api_ipsec_sad_flags_t
Neale Ranns311124e2019-01-24 04:52:25 -0800194 self.test = test
195 self.id = id
196 self.spi = spi
197 self.integ_alg = integ_alg
198 self.integ_key = integ_key
199 self.crypto_alg = crypto_alg
200 self.crypto_key = crypto_key
201 self.proto = proto
Neale Ranns80f6fd52019-04-16 02:41:34 +0000202 self.salt = salt
Neale Ranns17dcec02019-01-09 21:22:20 -0800203
Neale Ranns311124e2019-01-24 04:52:25 -0800204 self.tun_src = tun_src
205 self.tun_dst = tun_dst
Neale Ranns17dcec02019-01-09 21:22:20 -0800206 if not flags:
207 self.flags = e.IPSEC_API_SAD_FLAG_NONE
208 else:
209 self.flags = flags
Neale Ranns311124e2019-01-24 04:52:25 -0800210 if (tun_src):
211 self.tun_src = ip_address(text_type(tun_src))
Neale Ranns17dcec02019-01-09 21:22:20 -0800212 self.flags = self.flags | e.IPSEC_API_SAD_FLAG_IS_TUNNEL
Neale Ranns311124e2019-01-24 04:52:25 -0800213 if (self.tun_src.version == 6):
Neale Ranns17dcec02019-01-09 21:22:20 -0800214 self.flags = self.flags | e.IPSEC_API_SAD_FLAG_IS_TUNNEL_V6
Neale Ranns311124e2019-01-24 04:52:25 -0800215 if (tun_dst):
216 self.tun_dst = ip_address(text_type(tun_dst))
Neale Ranns311124e2019-01-24 04:52:25 -0800217
218 def add_vpp_config(self):
Neale Rannseba31ec2019-02-17 18:04:27 +0000219 r = self.test.vapi.ipsec_sad_entry_add_del(
Neale Ranns311124e2019-01-24 04:52:25 -0800220 self.id,
221 self.spi,
222 self.integ_alg,
223 self.integ_key,
224 self.crypto_alg,
225 self.crypto_key,
226 self.proto,
Neale Ranns17dcec02019-01-09 21:22:20 -0800227 (self.tun_src if self.tun_src else []),
228 (self.tun_dst if self.tun_dst else []),
Neale Ranns80f6fd52019-04-16 02:41:34 +0000229 flags=self.flags,
230 salt=self.salt)
Neale Rannseba31ec2019-02-17 18:04:27 +0000231 self.stat_index = r.stat_index
Neale Ranns311124e2019-01-24 04:52:25 -0800232 self.test.registry.register(self, self.test.logger)
233
234 def remove_vpp_config(self):
Neale Ranns17dcec02019-01-09 21:22:20 -0800235 self.test.vapi.ipsec_sad_entry_add_del(
Neale Ranns311124e2019-01-24 04:52:25 -0800236 self.id,
237 self.spi,
238 self.integ_alg,
239 self.integ_key,
240 self.crypto_alg,
241 self.crypto_key,
242 self.proto,
Neale Ranns17dcec02019-01-09 21:22:20 -0800243 (self.tun_src if self.tun_src else []),
244 (self.tun_dst if self.tun_dst else []),
245 flags=self.flags,
Neale Ranns311124e2019-01-24 04:52:25 -0800246 is_add=0)
247
Neale Ranns311124e2019-01-24 04:52:25 -0800248 def object_id(self):
249 return "ipsec-sa-%d" % self.id
250
251 def query_vpp_config(self):
252 bs = self.test.vapi.ipsec_sa_dump()
253 for b in bs:
Neale Ranns8d7c5022019-02-06 01:41:05 -0800254 if b.entry.sad_id == self.id:
Neale Ranns311124e2019-01-24 04:52:25 -0800255 return True
256 return False
Neale Rannseba31ec2019-02-17 18:04:27 +0000257
Neale Ranns4a56f4e2019-12-23 04:10:25 +0000258 def get_stats(self, worker=None):
Neale Rannseba31ec2019-02-17 18:04:27 +0000259 c = self.test.statistics.get_counter("/net/ipsec/sa")
Neale Ranns4a56f4e2019-12-23 04:10:25 +0000260 if worker is None:
261 total = mk_counter()
262 for t in c:
263 total['packets'] += t[self.stat_index]['packets']
264 return total
265 else:
266 # +1 to skip main thread
267 return c[worker+1][self.stat_index]
Neale Rannsc87b66c2019-02-07 07:26:12 -0800268
269
270class VppIpsecTunProtect(VppObject):
271 """
272 VPP IPSEC tunnel protection
273 """
274
275 def __init__(self, test, itf, sa_out, sas_in):
276 self.test = test
277 self.itf = itf
278 self.sas_in = []
279 for sa in sas_in:
280 self.sas_in.append(sa.id)
281 self.sa_out = sa_out.id
282
283 def update_vpp_config(self, sa_out, sas_in):
284 self.sas_in = []
285 for sa in sas_in:
286 self.sas_in.append(sa.id)
287 self.sa_out = sa_out.id
288 self.test.vapi.ipsec_tunnel_protect_update(
289 tunnel={
290 'sw_if_index': self.itf._sw_if_index,
291 'n_sa_in': len(self.sas_in),
292 'sa_out': self.sa_out,
293 'sa_in': self.sas_in})
294
295 def object_id(self):
296 return "ipsec-tun-protect-%s" % self.itf
297
298 def add_vpp_config(self):
299 self.test.vapi.ipsec_tunnel_protect_update(
300 tunnel={
301 'sw_if_index': self.itf._sw_if_index,
302 'n_sa_in': len(self.sas_in),
303 'sa_out': self.sa_out,
304 'sa_in': self.sas_in})
305 self.test.registry.register(self, self.test.logger)
306
307 def remove_vpp_config(self):
308 self.test.vapi.ipsec_tunnel_protect_del(
309 sw_if_index=self.itf.sw_if_index)
310
311 def query_vpp_config(self):
312 bs = self.test.vapi.ipsec_tunnel_protect_dump(
313 sw_if_index=self.itf.sw_if_index)
314 for b in bs:
315 if b.tun.sw_if_index == self.itf.sw_if_index:
316 return True
317 return False