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
221 self.tun_dst = ip_address(text_type(tun_dst))
222 self.udp_src = udp_src
223 self.udp_dst = udp_dst
224 self.tun_flags = (VppEnum.vl_api_tunnel_encap_decap_flags_t.
225 TUNNEL_API_ENCAP_DECAP_FLAG_NONE)
227 self.tun_flags = tun_flags
228 self.dscp = VppEnum.vl_api_ip_dscp_t.IP_API_DSCP_CS0
233 self.hop_limit = hop_limit
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,
240 'hop_limit': self.hop_limit,
241 'table_id': self.table_id
244 def add_vpp_config(self):
248 'integrity_algorithm': self.integ_alg,
250 'length': len(self.integ_key),
251 'data': self.integ_key,
253 'crypto_algorithm': self.crypto_alg,
255 'data': self.crypto_key,
256 'length': len(self.crypto_key),
258 'protocol': self.proto,
259 'tunnel': self.tunnel_encode(),
263 # don't explicitly send the defaults, let papi fill them in
265 entry['udp_src_port'] = self.udp_src
267 entry['udp_dst_port'] = self.udp_dst
268 r = self.test.vapi.ipsec_sad_entry_add(entry=entry)
269 self.stat_index = r.stat_index
270 self.test.registry.register(self, self.test.logger)
273 def remove_vpp_config(self):
274 self.test.vapi.ipsec_sad_entry_del(id=self.id)
277 return "ipsec-sa-%d" % self.id
279 def query_vpp_config(self):
280 e = VppEnum.vl_api_ipsec_sad_flags_t
282 bs = self.test.vapi.ipsec_sa_v3_dump()
284 if b.entry.sad_id == self.id:
285 # 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:
291 if self.udp_src != b.entry.udp_src_port:
294 if self.DEFAULT_UDP_PORT != b.entry.udp_src_port:
297 if self.udp_dst != b.entry.udp_dst_port:
300 if self.DEFAULT_UDP_PORT != b.entry.udp_dst_port:
305 def get_stats(self, worker=None):
306 c = self.test.statistics.get_counter("/net/ipsec/sa")
310 total['packets'] += t[self.stat_index]['packets']
313 # +1 to skip main thread
314 return c[worker+1][self.stat_index]
316 def get_lost(self, worker=None):
317 c = self.test.statistics.get_counter("/net/ipsec/sa/lost")
321 total += t[self.stat_index]
324 # +1 to skip main thread
325 return c[worker+1][self.stat_index]
328 class VppIpsecTunProtect(VppObject):
330 VPP IPSEC tunnel protection
333 def __init__(self, test, itf, sa_out, sas_in, nh=None):
338 self.sas_in.append(sa.id)
339 self.sa_out = sa_out.id
344 def update_vpp_config(self, sa_out, sas_in):
347 self.sas_in.append(sa.id)
348 self.sa_out = sa_out.id
349 self.test.vapi.ipsec_tunnel_protect_update(
351 'sw_if_index': self.itf._sw_if_index,
352 'n_sa_in': len(self.sas_in),
353 'sa_out': self.sa_out,
354 'sa_in': self.sas_in,
358 return "ipsec-tun-protect-%s-%s" % (self.itf, self.nh)
360 def add_vpp_config(self):
361 self.test.vapi.ipsec_tunnel_protect_update(
363 'sw_if_index': self.itf._sw_if_index,
364 'n_sa_in': len(self.sas_in),
365 'sa_out': self.sa_out,
366 'sa_in': self.sas_in,
368 self.test.registry.register(self, self.test.logger)
370 def remove_vpp_config(self):
371 self.test.vapi.ipsec_tunnel_protect_del(
372 sw_if_index=self.itf.sw_if_index,
375 def query_vpp_config(self):
376 bs = self.test.vapi.ipsec_tunnel_protect_dump(
377 sw_if_index=self.itf.sw_if_index)
379 if b.tun.sw_if_index == self.itf.sw_if_index and \
380 self.nh == str(b.tun.nh):
385 class VppIpsecInterface(VppInterface):
390 def __init__(self, test, mode=None, instance=0xffffffff):
391 super(VppIpsecInterface, self).__init__(test)
395 self.mode = (VppEnum.vl_api_tunnel_mode_t.
397 self.instance = instance
399 def add_vpp_config(self):
400 r = self.test.vapi.ipsec_itf_create(itf={
401 'user_instance': self.instance,
404 self.set_sw_if_index(r.sw_if_index)
405 self.test.registry.register(self, self.test.logger)
406 ts = self.test.vapi.ipsec_itf_dump(sw_if_index=self._sw_if_index)
407 self.instance = ts[0].itf.user_instance
410 def remove_vpp_config(self):
411 self.test.vapi.ipsec_itf_delete(sw_if_index=self._sw_if_index)
413 def query_vpp_config(self):
414 ts = self.test.vapi.ipsec_itf_dump(sw_if_index=0xffffffff)
416 if t.itf.sw_if_index == self._sw_if_index:
421 return self.object_id()
424 return "ipsec%d" % self.instance