1 from vpp_object import VppObject
2 from ipaddress import ip_address
3 from vpp_papi import VppEnum
4 from vpp_interface import VppInterface
13 return {'packets': 0, 'bytes': 0}
16 class VppIpsecSpd(VppObject):
21 def __init__(self, test, id):
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)
29 def remove_vpp_config(self):
30 self.test.vapi.ipsec_spd_add_del(self.id, is_add=0)
33 return "ipsec-spd-%d" % self.id
35 def query_vpp_config(self):
36 spds = self.test.vapi.ipsec_spds_dump()
38 if spd.spd_id == self.id:
43 class VppIpsecSpdItfBinding(VppObject):
45 VPP SPD DB to interface binding
46 (i.e. this SPD is used on this interface)
49 def __init__(self, test, spd, itf):
54 def add_vpp_config(self):
55 self.test.vapi.ipsec_interface_add_del_spd(self.spd.id,
57 self.test.registry.register(self, self.test.logger)
59 def remove_vpp_config(self):
60 self.test.vapi.ipsec_interface_add_del_spd(self.spd.id,
65 return "bind-%s-to-%s" % (self.spd.id, self.itf)
67 def query_vpp_config(self):
68 bs = self.test.vapi.ipsec_spd_interface_dump()
70 if b.sw_if_index == self.itf.sw_if_index:
75 class VppIpsecSpdEntry(VppObject):
80 def __init__(self, test, spd, sa_id,
81 local_start, local_stop,
82 remote_start, remote_stop,
88 remote_port_stop=65535,
90 local_port_stop=65535):
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))
99 self.is_outbound = is_outbound
100 self.priority = priority
102 self.policy = (VppEnum.vl_api_ipsec_spd_action_t.
103 IPSEC_API_SPD_ACTION_BYPASS)
106 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
112 def add_vpp_config(self):
113 rv = self.test.vapi.ipsec_spd_entry_add_del(
121 is_ipv6=self.is_ipv6,
122 is_outbound=self.is_outbound,
123 priority=self.priority,
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)
129 self.stat_index = rv.stat_index
130 self.test.registry.register(self, self.test.logger)
133 def remove_vpp_config(self):
134 self.test.vapi.ipsec_spd_entry_add_del(
142 is_ipv6=self.is_ipv6,
143 is_outbound=self.is_outbound,
144 priority=self.priority,
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,
153 return "spd-entry-%d-%d-%d-%d-%d-%d" % (self.spd.id,
158 self.remote_port_start)
160 def query_vpp_config(self):
161 ss = self.test.vapi.ipsec_spd_dump(self.spd.id)
163 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:
172 def get_stats(self, worker=None):
173 c = self.test.statistics.get_counter("/net/ipsec/policy")
177 total['packets'] += t[self.stat_index]['packets']
180 # +1 to skip main thread
181 return c[worker+1][self.stat_index]
184 class VppIpsecSA(VppObject):
189 DEFAULT_UDP_PORT = 4500
191 def __init__(self, test, id, spi,
192 integ_alg, integ_key,
193 crypto_alg, crypto_key,
195 tun_src=None, tun_dst=None,
196 flags=None, salt=0, tun_flags=None,
198 udp_src=None, udp_dst=None, hop_limit=None):
199 e = VppEnum.vl_api_ipsec_sad_flags_t
203 self.integ_alg = integ_alg
204 self.integ_key = integ_key
205 self.crypto_alg = crypto_alg
206 self.crypto_key = crypto_key
211 self.tun_src = tun_src
212 self.tun_dst = tun_dst
214 self.flags = e.IPSEC_API_SAD_FLAG_NONE
218 self.tun_src = ip_address(text_type(tun_src))
219 self.flags = self.flags | e.IPSEC_API_SAD_FLAG_IS_TUNNEL
220 if (self.tun_src.version == 6):
221 self.flags = self.flags | e.IPSEC_API_SAD_FLAG_IS_TUNNEL_V6
223 self.tun_dst = ip_address(text_type(tun_dst))
224 self.udp_src = udp_src
225 self.udp_dst = udp_dst
226 self.tun_flags = (VppEnum.vl_api_tunnel_encap_decap_flags_t.
227 TUNNEL_API_ENCAP_DECAP_FLAG_NONE)
229 self.tun_flags = tun_flags
230 self.dscp = VppEnum.vl_api_ip_dscp_t.IP_API_DSCP_CS0
235 self.hop_limit = hop_limit
237 def tunnel_encode(self):
238 return {'src': (self.tun_src if self.tun_src else []),
239 'dst': (self.tun_dst if self.tun_dst else []),
240 'encap_decap_flags': self.tun_flags,
242 'hop_limit': self.hop_limit,
243 'table_id': self.table_id
246 def add_vpp_config(self):
250 'integrity_algorithm': self.integ_alg,
252 'length': len(self.integ_key),
253 'data': self.integ_key,
255 'crypto_algorithm': self.crypto_alg,
257 'data': self.crypto_key,
258 'length': len(self.crypto_key),
260 'protocol': self.proto,
261 'tunnel': self.tunnel_encode(),
265 # don't explicitly send the defaults, let papi fill them in
267 entry['udp_src_port'] = self.udp_src
269 entry['udp_dst_port'] = self.udp_dst
270 r = self.test.vapi.ipsec_sad_entry_add(entry=entry)
271 self.stat_index = r.stat_index
272 self.test.registry.register(self, self.test.logger)
275 def remove_vpp_config(self):
276 self.test.vapi.ipsec_sad_entry_del(id=self.id)
279 return "ipsec-sa-%d" % self.id
281 def query_vpp_config(self):
282 e = VppEnum.vl_api_ipsec_sad_flags_t
284 bs = self.test.vapi.ipsec_sa_v3_dump()
286 if b.entry.sad_id == self.id:
287 # if udp encap is configured then the ports should match
288 # those configured or the default
289 if (self.flags & e.IPSEC_API_SAD_FLAG_UDP_ENCAP):
290 if not b.entry.flags & e.IPSEC_API_SAD_FLAG_UDP_ENCAP:
293 if self.udp_src != b.entry.udp_src_port:
296 if self.DEFAULT_UDP_PORT != b.entry.udp_src_port:
299 if self.udp_dst != b.entry.udp_dst_port:
302 if self.DEFAULT_UDP_PORT != b.entry.udp_dst_port:
307 def get_stats(self, worker=None):
308 c = self.test.statistics.get_counter("/net/ipsec/sa")
312 total['packets'] += t[self.stat_index]['packets']
315 # +1 to skip main thread
316 return c[worker+1][self.stat_index]
319 class VppIpsecTunProtect(VppObject):
321 VPP IPSEC tunnel protection
324 def __init__(self, test, itf, sa_out, sas_in, nh=None):
329 self.sas_in.append(sa.id)
330 self.sa_out = sa_out.id
335 def update_vpp_config(self, sa_out, sas_in):
338 self.sas_in.append(sa.id)
339 self.sa_out = sa_out.id
340 self.test.vapi.ipsec_tunnel_protect_update(
342 'sw_if_index': self.itf._sw_if_index,
343 'n_sa_in': len(self.sas_in),
344 'sa_out': self.sa_out,
345 'sa_in': self.sas_in,
349 return "ipsec-tun-protect-%s-%s" % (self.itf, self.nh)
351 def add_vpp_config(self):
352 self.test.vapi.ipsec_tunnel_protect_update(
354 'sw_if_index': self.itf._sw_if_index,
355 'n_sa_in': len(self.sas_in),
356 'sa_out': self.sa_out,
357 'sa_in': self.sas_in,
359 self.test.registry.register(self, self.test.logger)
361 def remove_vpp_config(self):
362 self.test.vapi.ipsec_tunnel_protect_del(
363 sw_if_index=self.itf.sw_if_index,
366 def query_vpp_config(self):
367 bs = self.test.vapi.ipsec_tunnel_protect_dump(
368 sw_if_index=self.itf.sw_if_index)
370 if b.tun.sw_if_index == self.itf.sw_if_index and \
371 self.nh == str(b.tun.nh):
376 class VppIpsecInterface(VppInterface):
381 def __init__(self, test, mode=None, instance=0xffffffff):
382 super(VppIpsecInterface, self).__init__(test)
386 self.mode = (VppEnum.vl_api_tunnel_mode_t.
388 self.instance = instance
390 def add_vpp_config(self):
391 r = self.test.vapi.ipsec_itf_create(itf={
392 'user_instance': self.instance,
395 self.set_sw_if_index(r.sw_if_index)
396 self.test.registry.register(self, self.test.logger)
397 ts = self.test.vapi.ipsec_itf_dump(sw_if_index=self._sw_if_index)
398 self.instance = ts[0].itf.user_instance
401 def remove_vpp_config(self):
402 self.test.vapi.ipsec_itf_delete(sw_if_index=self._sw_if_index)
404 def query_vpp_config(self):
405 ts = self.test.vapi.ipsec_itf_dump(sw_if_index=0xffffffff)
407 if t.itf.sw_if_index == self._sw_if_index:
412 return self.object_id()
415 return "ipsec%d" % self.instance