VatHistory: Add ability to trace file executions
[csit.git] / resources / libraries / python / IPsecUtil.py
1 # Copyright (c) 2016 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 """IPsec utilities library."""
15
16 import os
17 from ipaddress import ip_network, ip_address
18
19 from enum import Enum
20
21 from resources.libraries.python.VatExecutor import VatExecutor
22 from resources.libraries.python.topology import Topology
23 from resources.libraries.python.VatJsonUtil import VatJsonUtil
24
25
26 class PolicyAction(Enum):
27     """Policy actions."""
28     BYPASS = 'bypass'
29     DISCARD = 'discard'
30     PROTECT = 'protect'
31
32     def __init__(self, string):
33         self.string = string
34
35
36 class CryptoAlg(Enum):
37     """Encryption algorithms."""
38     AES_CBC_128 = ('aes-cbc-128', 'AES-CBC', 16)
39     AES_CBC_192 = ('aes-cbc-192', 'AES-CBC', 24)
40     AES_CBC_256 = ('aes-cbc-256', 'AES-CBC', 32)
41     AES_GCM_128 = ('aes-gcm-128', 'AES-GCM', 20)
42
43     def __init__(self, alg_name, scapy_name, key_len):
44         self.alg_name = alg_name
45         self.scapy_name = scapy_name
46         self.key_len = key_len
47
48
49 class IntegAlg(Enum):
50     """Integrity algorithm."""
51     SHA1_96 = ('sha1-96', 'HMAC-SHA1-96', 20)
52     SHA_256_128 = ('sha-256-128', 'SHA2-256-128', 32)
53     SHA_384_192 = ('sha-384-192', 'SHA2-384-192', 48)
54     SHA_512_256 = ('sha-512-256', 'SHA2-512-256', 64)
55     AES_GCM_128 = ('aes-gcm-128', 'AES-GCM', 20)
56
57     def __init__(self, alg_name, scapy_name, key_len):
58         self.alg_name = alg_name
59         self.scapy_name = scapy_name
60         self.key_len = key_len
61
62
63 class IPsecUtil(object):
64     """IPsec utilities."""
65
66     @staticmethod
67     def policy_action_bypass():
68         """Return policy action bypass.
69
70         :returns: PolicyAction enum BYPASS object.
71         :rtype: PolicyAction
72         """
73         return PolicyAction.BYPASS
74
75     @staticmethod
76     def policy_action_discard():
77         """Return policy action discard.
78
79         :returns: PolicyAction enum DISCARD object.
80         :rtype: PolicyAction
81         """
82         return PolicyAction.DISCARD
83
84     @staticmethod
85     def policy_action_protect():
86         """Return policy action protect.
87
88         :returns: PolicyAction enum PROTECT object.
89         :rtype: PolicyAction
90         """
91         return PolicyAction.PROTECT
92
93     @staticmethod
94     def crypto_alg_aes_cbc_128():
95         """Return encryption algorithm aes-cbc-128.
96
97         :returns: CryptoAlg enum AES_CBC_128 object.
98         :rtype: CryptoAlg
99         """
100         return CryptoAlg.AES_CBC_128
101
102     @staticmethod
103     def crypto_alg_aes_cbc_192():
104         """Return encryption algorithm aes-cbc-192.
105
106         :returns: CryptoAlg enum AES_CBC_192 objec.
107         :rtype: CryptoAlg
108         """
109         return CryptoAlg.AES_CBC_192
110
111     @staticmethod
112     def crypto_alg_aes_cbc_256():
113         """Return encryption algorithm aes-cbc-256.
114
115         :returns: CryptoAlg enum AES_CBC_256 object.
116         :rtype: CryptoAlg
117         """
118         return CryptoAlg.AES_CBC_256
119
120     @staticmethod
121     def crypto_alg_aes_gcm_128():
122         """Return encryption algorithm aes-gcm-128.
123
124         :returns: CryptoAlg enum AES_GCM_128 object.
125         :rtype: CryptoAlg
126         """
127         return CryptoAlg.AES_GCM_128
128
129     @staticmethod
130     def get_crypto_alg_key_len(crypto_alg):
131         """Return encryption algorithm key length.
132
133         :param crypto_alg: Encryption algorithm.
134         :type crypto_alg: CryptoAlg
135         :returns: Key length.
136         :rtype: int
137         """
138         return crypto_alg.key_len
139
140     @staticmethod
141     def get_crypto_alg_scapy_name(crypto_alg):
142         """Return encryption algorithm scapy name.
143
144         :param crypto_alg: Encryption algorithm.
145         :type crypto_alg: CryptoAlg
146         :returns: Algorithm scapy name.
147         :rtype: str
148         """
149         return crypto_alg.scapy_name
150
151     @staticmethod
152     def integ_alg_sha1_96():
153         """Return integrity algorithm SHA1-96.
154
155         :returns: IntegAlg enum SHA1_96 object.
156         :rtype: IntegAlg
157         """
158         return IntegAlg.SHA1_96
159
160     @staticmethod
161     def integ_alg_sha_256_128():
162         """Return integrity algorithm SHA-256-128.
163
164         :returns: IntegAlg enum SHA_256_128 object.
165         :rtype: IntegAlg
166         """
167         return IntegAlg.SHA_256_128
168
169     @staticmethod
170     def integ_alg_sha_384_192():
171         """Return integrity algorithm SHA-384-192.
172
173         :returns: IntegAlg enum SHA_384_192 object.
174         :rtype: IntegAlg
175         """
176         return IntegAlg.SHA_384_192
177
178     @staticmethod
179     def integ_alg_sha_512_256():
180         """Return integrity algorithm SHA-512-256.
181
182         :returns: IntegAlg enum SHA_512_256 object.
183         :rtype: IntegAlg
184         """
185         return IntegAlg.SHA_512_256
186
187     @staticmethod
188     def integ_alg_aes_gcm_128():
189         """Return integrity algorithm AES-GCM-128.
190
191         :returns: IntegAlg enum AES_GCM_128 object.
192         :rtype: IntegAlg
193         """
194         return IntegAlg.AES_GCM_128
195
196     @staticmethod
197     def get_integ_alg_key_len(integ_alg):
198         """Return integrity algorithm key length.
199
200         :param integ_alg: Integrity algorithm.
201         :type integ_alg: IntegAlg
202         :returns: Key length.
203         :rtype: int
204         """
205         return integ_alg.key_len
206
207     @staticmethod
208     def get_integ_alg_scapy_name(integ_alg):
209         """Return integrity algorithm scapy name.
210
211         :param integ_alg: Integrity algorithm.
212         :type integ_alg: IntegAlg
213         :returns: Algorithm scapy name.
214         :rtype: str
215         """
216         return integ_alg.scapy_name
217
218     @staticmethod
219     def vpp_ipsec_add_sad_entry(node, sad_id, spi, crypto_alg, crypto_key,
220                                 integ_alg, integ_key, tunnel_src=None,
221                                 tunnel_dst=None):
222         """Create Security Association Database entry on the VPP node.
223
224         :param node: VPP node to add SAD entry on.
225         :param sad_id: SAD entry ID.
226         :param spi: Security Parameter Index of this SAD entry.
227         :param crypto_alg: The encryption algorithm name.
228         :param crypto_key: The encryption key string.
229         :param integ_alg: The integrity algorithm name.
230         :param integ_key: The integrity key string.
231         :param tunnel_src: Tunnel header source IPv4 or IPv6 address. If not
232         specified ESP transport mode is used.
233         :param tunnel_dst: Tunnel header destination IPv4 or IPv6 address. If
234         not specified ESP transport mode is used.
235         :type node: dict
236         :type sad_id: int
237         :type spi: int
238         :type crypto_alg: CryptoAlg
239         :type crypto_key: str
240         :type integ_alg: str
241         :type integ_key: str
242         :type tunnel_src: str
243         :type tunnel_dst: str
244         """
245         ckey = crypto_key.encode('hex')
246         ikey = integ_key.encode('hex')
247         tunnel = 'tunnel_src {0} tunnel_dst {1}'.format(tunnel_src, tunnel_dst)\
248             if tunnel_src is not None and tunnel_dst is not None else ''
249
250         out = VatExecutor.cmd_from_template(node,
251                                             "ipsec/ipsec_sad_add_entry.vat",
252                                             sad_id=sad_id, spi=spi,
253                                             calg=crypto_alg.alg_name, ckey=ckey,
254                                             ialg=integ_alg.alg_name, ikey=ikey,
255                                             tunnel=tunnel)
256         VatJsonUtil.verify_vat_retval(
257             out[0],
258             err_msg='Add SAD entry failed on {0}'.format(node['host']))
259
260     @staticmethod
261     def vpp_ipsec_add_sad_entries(node, n_entries, sad_id, spi, crypto_alg,
262                                   crypto_key, integ_alg, integ_key,
263                                   tunnel_src=None, tunnel_dst=None):
264         """Create multiple Security Association Database entries on VPP node.
265
266         :param node: VPP node to add SAD entry on.
267         :param n_entries: Number of SAD entries to be created.
268         :param sad_id: First SAD entry ID. All subsequent SAD entries will have
269         id incremented by 1.
270         :param spi: Security Parameter Index of first SAD entry. All subsequent
271         SAD entries will have spi incremented by 1.
272         :param crypto_alg: The encryption algorithm name.
273         :param crypto_key: The encryption key string.
274         :param integ_alg: The integrity algorithm name.
275         :param integ_key: The integrity key string.
276         :param tunnel_src: Tunnel header source IPv4 or IPv6 address. If not
277         specified ESP transport mode is used.
278         :param tunnel_dst: Tunnel header destination IPv4 or IPv6 address. If
279         not specified ESP transport mode is used.
280         :type node: dict
281         :type n_entries: int
282         :type sad_id: int
283         :type spi: int
284         :type crypto_alg: CryptoAlg
285         :type crypto_key: str
286         :type integ_alg: IntegAlg
287         :type integ_key: str
288         :type tunnel_src: str
289         :type tunnel_dst: str
290         """
291         tmp_filename = '/tmp/ipsec_sad_{}_add_del_entry.vat'.format(sad_id)
292         ckey = crypto_key.encode('hex')
293         ikey = integ_key.encode('hex')
294         tunnel = 'tunnel_src {0} tunnel_dst {1}'.format(tunnel_src, tunnel_dst)\
295             if tunnel_src is not None and tunnel_dst is not None else ''
296
297         integ = 'integ_alg {0} integ_key {1}'.format(integ_alg.alg_name, ikey)\
298             if crypto_alg.alg_name != 'aes-gcm-128' else ''
299
300         with open(tmp_filename, 'w') as tmp_file:
301             for i in range(0, n_entries):
302                 buf_str = 'ipsec_sad_add_del_entry esp sad_id {0} spi {1} ' \
303                           'crypto_alg {2} crypto_key {3} {4} {5}\n'.format(
304                               sad_id+i, spi+i, crypto_alg.alg_name, ckey, integ,
305                               tunnel)
306                 tmp_file.write(buf_str)
307         vat = VatExecutor()
308         vat.scp_and_execute_script(tmp_filename, node, 300)
309         os.remove(tmp_filename)
310
311     @staticmethod
312     def vpp_ipsec_sa_set_key(node, sa_id, crypto_key, integ_key):
313         """Update Security Association (SA) keys.
314
315         :param node: VPP node to update SA keys.
316         :param sa_id: SAD entry ID.
317         :param crypto_key: The encryption key string.
318         :param integ_key: The integrity key string.
319         :type node: dict
320         :type sa_id: int
321         :type crypto_key: str
322         :type integ_key: str
323         """
324         ckey = crypto_key.encode('hex')
325         ikey = integ_key.encode('hex')
326
327         out = VatExecutor.cmd_from_template(node,
328                                             "ipsec/ipsec_sa_set_key.vat",
329                                             sa_id=sa_id,
330                                             ckey=ckey, ikey=ikey)
331         VatJsonUtil.verify_vat_retval(
332             out[0],
333             err_msg='Update SA key failed on {0}'.format(node['host']))
334
335     @staticmethod
336     def vpp_ipsec_add_spd(node, spd_id):
337         """Create Security Policy Database on the VPP node.
338
339         :param node: VPP node to add SPD on.
340         :param spd_id: SPD ID.
341         :type node: dict
342         :type spd_id: int
343         """
344         out = VatExecutor.cmd_from_template(node, "ipsec/ipsec_spd_add.vat",
345                                             spd_id=spd_id)
346         VatJsonUtil.verify_vat_retval(
347             out[0],
348             err_msg='Add SPD {0} failed on {1}'.format(spd_id, node['host']))
349
350     @staticmethod
351     def vpp_ipsec_spd_add_if(node, spd_id, interface):
352         """Add interface to the Security Policy Database.
353
354         :param node: VPP node.
355         :param spd_id: SPD ID to add interface on.
356         :param interface: Interface name or sw_if_index.
357         :type node: dict
358         :type spd_id: int
359         :type interface: str or int
360         """
361         sw_if_index = Topology.get_interface_sw_index(node, interface)\
362             if isinstance(interface, basestring) else interface
363
364         out = VatExecutor.cmd_from_template(node,
365                                             "ipsec/ipsec_interface_add_spd.vat",
366                                             spd_id=spd_id, sw_if_id=sw_if_index)
367         VatJsonUtil.verify_vat_retval(
368             out[0],
369             err_msg='Add interface {0} to SPD {1} failed on {2}'.format(
370                 interface, spd_id, node['host']))
371
372     @staticmethod
373     def vpp_ipsec_spd_add_entry(node, spd_id, priority, action, inbound=True,
374                                 sa_id=None, laddr_range=None, raddr_range=None,
375                                 proto=None, lport_range=None, rport_range=None):
376         """Create Security Policy Database entry on the VPP node.
377
378         :param node: VPP node to add SPD entry on.
379         :param spd_id: SPD ID to add entry on.
380         :param priority: SPD entry priority, higher number = higher priority.
381         :param action: Policy action.
382         :param inbound: If True policy is for inbound traffic, otherwise
383         outbound.
384         :param sa_id: SAD entry ID for protect action.
385         :param laddr_range: Policy selector local IPv4 or IPv6 address range in
386         format IP/prefix or IP/mask. If no mask is provided, it's considered
387         to be /32.
388         :param raddr_range: Policy selector remote IPv4 or IPv6 address range in
389         format IP/prefix or IP/mask. If no mask is provided, it's considered
390         to be /32.
391         :param proto: Policy selector next layer protocol number.
392         :param lport_range: Policy selector local TCP/UDP port range in format
393         <port_start>-<port_end>.
394         :param rport_range: Policy selector remote TCP/UDP port range in format
395         <port_start>-<port_end>.
396         :type node: dict
397         :type spd_id: int
398         :type priority: int
399         :type action: PolicyAction
400         :type inbound: bool
401         :type sa_id: int
402         :type laddr_range: string
403         :type raddr_range: string
404         :type proto: int
405         :type lport_range: string
406         :type rport_range: string
407         """
408         direction = 'inbound' if inbound else 'outbound'
409
410         act_str = action.value
411         if PolicyAction.PROTECT == action and sa_id is not None:
412             act_str += 'sa_id {0}'.format(sa_id)
413
414         selector = ''
415         if laddr_range is not None:
416             net = ip_network(unicode(laddr_range), strict=False)
417             selector += 'laddr_start {0} laddr_stop {1} '.format(
418                 net.network_address, net.broadcast_address)
419         if raddr_range is not None:
420             net = ip_network(unicode(raddr_range), strict=False)
421             selector += 'raddr_start {0} raddr_stop {1} '.format(
422                 net.network_address, net.broadcast_address)
423         if proto is not None:
424             selector += 'protocol {0} '.format(proto)
425         if lport_range is not None:
426             selector += 'lport_start {p[0]} lport_stop {p[1]} '.format(
427                 p=lport_range.split('-'))
428         if rport_range is not None:
429             selector += 'rport_start {p[0]} rport_stop {p[1]} '.format(
430                 p=rport_range.split('-'))
431
432         out = VatExecutor.cmd_from_template(node,
433                                             "ipsec/ipsec_spd_add_entry.vat",
434                                             spd_id=spd_id, priority=priority,
435                                             action=act_str, direction=direction,
436                                             selector=selector)
437         VatJsonUtil.verify_vat_retval(
438             out[0],
439             err_msg='Add entry to SPD {0} failed on {1}'.format(spd_id,
440                                                                 node['host']))
441
442     @staticmethod
443     def vpp_ipsec_spd_add_entries(node, n_entries, spd_id, priority, inbound,
444                                   sa_id, raddr_ip, raddr_range):
445         """Create multiple Security Policy Database entries on the VPP node.
446
447         :param node: VPP node to add SPD entries on.
448         :param n_entries: Number of SPD entries to be added.
449         :param spd_id: SPD ID to add entries on.
450         :param priority: SPD entries priority, higher number = higher priority.
451         :param inbound: If True policy is for inbound traffic, otherwise
452         outbound.
453         :param sa_id: SAD entry ID for first entry. Each subsequent entry will
454         SAD entry ID incremented by 1.
455         :param raddr_ip: Policy selector remote IPv4 start address for the first
456         entry. Remote IPv4 end address will be calculated depending on
457         raddr_range parameter. Each subsequent entry will have start address
458         next after IPv4 end address of previous entry.
459         :param raddr_range: Mask specifying range of Policy selector Remote IPv4
460         addresses. Valid values are from 1 to 32.
461         :type node: dict
462         :type n_entries: int
463         :type spd_id: int
464         :type priority: int
465         :type inbound: bool
466         :type sa_id: int
467         :type raddr_ip: string
468         :type raddr_range: int
469         """
470         tmp_filename = '/tmp/ipsec_spd_{}_add_del_entry.vat'.format(sa_id)
471         direction = 'inbound' if inbound else 'outbound'
472         addr_incr = 1 << (32 - raddr_range)
473         addr_ip = int(ip_address(unicode(raddr_ip)))
474         start_str = 'ipsec_spd_add_del_entry spd_id {0} priority {1} {2} ' \
475                     'action protect sa_id'.format(spd_id, priority, direction)
476         with open(tmp_filename, 'w') as tmp_file:
477             for i in range(0, n_entries):
478                 r_ip_s = ip_address(addr_ip + addr_incr * i)
479                 r_ip_e = ip_address(addr_ip + addr_incr * (i+1) - 1)
480                 buf_str = '{0} {1} raddr_start {2} raddr_stop {3}\n'.format(
481                     start_str, sa_id+i, r_ip_s, r_ip_e)
482                 tmp_file.write(buf_str)
483         vat = VatExecutor()
484         vat.scp_and_execute_script(tmp_filename, node, 300)
485         os.remove(tmp_filename)
486
487     @staticmethod
488     def vpp_ipsec_create_tunnel_interfaces(node1, node2, if1_ip_addr,
489                                            if2_ip_addr, if1_key, if2_key,
490                                            n_tunnels, crypto_alg, crypto_key,
491                                            integ_alg, integ_key, raddr_ip1,
492                                            raddr_ip2, raddr_range):
493         """Create multiple IPsec tunnel interfaces between two VPP nodes.
494
495         :param node1: VPP node 1 to create tunnel interfaces.
496         :param node2: VPP node 2 to create tunnel interfaces.
497         :param if1_ip_addr: VPP node 1 interface IP4 address.
498         :param if2_ip_addr: VPP node 2 interface IP4 address.
499         :param if1_key: VPP node 1 interface key from topology file.
500         :param if2_key: VPP node 2 interface key from topology file.
501         :param n_tunnels: Number of tunnell interfaces to create.
502         :param crypto_alg: The encryption algorithm name.
503         :param crypto_key: The encryption key string.
504         :param integ_alg: The integrity algorithm name.
505         :param integ_key: The integrity key string.
506         :param raddr_ip1: Policy selector remote IPv4 start address for the
507         first tunnel in direction node1->node2.
508         :param raddr_ip2: Policy selector remote IPv4 start address for the
509         first tunnel in direction node2->node1.
510         :param raddr_range: Mask specifying range of Policy selector Remote IPv4
511         addresses. Valid values are from 1 to 32.
512         :type node1: dict
513         :type node2: dict
514         :type if1_ip_addr: str
515         :type if2_ip_addr: str
516         :type if1_key: str
517         :type if2_key: str
518         :type n_tunnels: int
519         :type crypto_alg: CryptoAlg
520         :type crypto_key: str
521         :type integ_alg: IntegAlg
522         :type integ_key: str
523         :type raddr_ip1: string
524         :type raddr_ip2: string
525         :type raddr_range: int
526         """
527
528         spi_1 = 10000
529         spi_2 = 20000
530
531         raddr_ip1_i = int(ip_address(unicode(raddr_ip1)))
532         raddr_ip2_i = int(ip_address(unicode(raddr_ip2)))
533         addr_incr = 1 << (32 - raddr_range)
534
535         tmp_fn1 = '/tmp/ipsec_create_tunnel_dut1.config'
536         tmp_fn2 = '/tmp/ipsec_create_tunnel_dut2.config'
537
538         ckey = crypto_key.encode('hex')
539         ikey = integ_key.encode('hex')
540
541         with open(tmp_fn1, 'w') as tmp_f1, open(tmp_fn2, 'w') as tmp_f2:
542             for i in range(0, n_tunnels):
543                 if_s = 'ipsec{}'.format(i)
544                 dut1_tunnel_s = 'create ipsec tunnel local-ip {0} local-spi ' \
545                                 '{1} remote-ip {2} remote-spi {3}\n'.format(
546                                     if1_ip_addr, spi_1+i, if2_ip_addr, spi_2+i)
547                 tmp_f1.write(dut1_tunnel_s)
548                 dut2_tunnel_s = 'create ipsec tunnel local-ip {0} local-spi ' \
549                                 '{1} remote-ip {2} remote-spi {3}\n'.format(
550                                     if2_ip_addr, spi_2+i, if1_ip_addr, spi_1+i)
551                 tmp_f2.write(dut2_tunnel_s)
552                 loc_c_key = 'set interface ipsec key {0} local crypto {1} ' \
553                             '{2}\n'.format(if_s, crypto_alg.alg_name, ckey)
554                 tmp_f1.write(loc_c_key)
555                 tmp_f2.write(loc_c_key)
556                 rem_c_key = 'set interface ipsec key {0} remote crypto {1} ' \
557                             '{2}\n'.format(if_s, crypto_alg.alg_name, ckey)
558                 tmp_f1.write(rem_c_key)
559                 tmp_f2.write(rem_c_key)
560                 if crypto_alg.alg_name != 'aes-gcm-128':
561                     loc_i_key = 'set interface ipsec key {0} local integ {1} ' \
562                                 '{2}\n'.format(if_s, integ_alg.alg_name, ikey)
563                     tmp_f1.write(loc_i_key)
564                     tmp_f2.write(loc_i_key)
565                     rem_i_key = 'set interface ipsec key {0} remote integ {1}' \
566                                 ' {2}\n'.format(if_s, integ_alg.alg_name, ikey)
567                     tmp_f1.write(rem_i_key)
568                     tmp_f2.write(rem_i_key)
569                 raddr_ip1_s = ip_address(raddr_ip1_i + addr_incr*i)
570                 raddr_ip2_s = ip_address(raddr_ip2_i + addr_incr*i)
571                 dut1_rte_s = 'ip route add {0}/{1} via {2} {3}\n'.format(
572                     raddr_ip2_s, raddr_range, if2_ip_addr, if_s)
573                 tmp_f1.write(dut1_rte_s)
574                 dut2_rte_s = 'ip route add {0}/{1} via {2} {3}\n'.format(
575                     raddr_ip1_s, raddr_range, if1_ip_addr, if_s)
576                 tmp_f2.write(dut2_rte_s)
577                 dut1_if = Topology.get_interface_name(node1, if1_key)
578                 dut1_unnum_s = 'set interface unnumbered {0} use {1}\n'.format(
579                     if_s, dut1_if)
580                 tmp_f1.write(dut1_unnum_s)
581                 dut2_if = Topology.get_interface_name(node2, if2_key)
582                 dut2_unnum_s = 'set interface unnumbered {0} use {1}\n'.format(
583                     if_s, dut2_if)
584                 tmp_f2.write(dut2_unnum_s)
585                 up_s = 'set int state {0} up\n'.format(if_s)
586                 tmp_f1.write(up_s)
587                 tmp_f2.write(up_s)
588
589         vat = VatExecutor()
590         vat.scp_and_execute_cli_script(tmp_fn1, node1, 300)
591         vat.scp_and_execute_cli_script(tmp_fn2, node2, 300)
592         os.remove(tmp_fn1)
593         os.remove(tmp_fn2)
594
595     @staticmethod
596     def vpp_ipsec_add_multiple_tunnels(node1, node2, interface1, interface2,
597                                        n_tunnels, crypto_alg, crypto_key,
598                                        integ_alg, integ_key, tunnel_ip1,
599                                        tunnel_ip2, raddr_ip1, raddr_ip2,
600                                        raddr_range):
601         """Create multiple IPsec tunnels between two VPP nodes.
602
603         :param node1: VPP node 1 to create tunnels.
604         :param node2: VPP node 2 to create tunnels.
605         :param interface1: Interface name or sw_if_index on node 1.
606         :param interface2: Interface name or sw_if_index on node 2.
607         :param n_tunnels: Number of tunnels to create.
608         :param crypto_alg: The encryption algorithm name.
609         :param crypto_key: The encryption key string.
610         :param integ_alg: The integrity algorithm name.
611         :param integ_key: The integrity key string.
612         :param tunnel_ip1: Tunnel node1 IPv4 address.
613         :param tunnel_ip2: Tunnel node2 IPv4 address.
614         :param raddr_ip1: Policy selector remote IPv4 start address for the
615         first tunnel in direction node1->node2.
616         :param raddr_ip2: Policy selector remote IPv4 start address for the
617         first tunnel in direction node2->node1.
618         :param raddr_range: Mask specifying range of Policy selector Remote IPv4
619         addresses. Valid values are from 1 to 32.
620         :type node1: dict
621         :type node2: dict
622         :type interface1: str or int
623         :type interface2: str or int
624         :type n_tunnels: int
625         :type crypto_alg: CryptoAlg
626         :type crypto_key: str
627         :type integ_alg: str
628         :type integ_key: str
629         :type tunnel_ip1: str
630         :type tunnel_ip2: str
631         :type raddr_ip1: string
632         :type raddr_ip2: string
633         :type raddr_range: int
634         """
635         spd_id = 1
636         p_hi = 100
637         p_lo = 10
638         sa_id_1 = 10000
639         sa_id_2 = 20000
640         spi_1 = 30000
641         spi_2 = 40000
642         proto = 50
643
644         IPsecUtil.vpp_ipsec_add_spd(node1, spd_id)
645         IPsecUtil.vpp_ipsec_spd_add_if(node1, spd_id, interface1)
646         IPsecUtil.vpp_ipsec_spd_add_entry(node1, spd_id, p_hi,
647                                           PolicyAction.BYPASS, inbound=False,
648                                           proto=proto)
649         IPsecUtil.vpp_ipsec_spd_add_entry(node1, spd_id, p_hi,
650                                           PolicyAction.BYPASS, inbound=True,
651                                           proto=proto)
652
653         IPsecUtil.vpp_ipsec_add_spd(node2, spd_id)
654         IPsecUtil.vpp_ipsec_spd_add_if(node2, spd_id, interface2)
655         IPsecUtil.vpp_ipsec_spd_add_entry(node2, spd_id, p_hi,
656                                           PolicyAction.BYPASS, inbound=False,
657                                           proto=proto)
658         IPsecUtil.vpp_ipsec_spd_add_entry(node2, spd_id, p_hi,
659                                           PolicyAction.BYPASS, inbound=True,
660                                           proto=proto)
661
662         IPsecUtil.vpp_ipsec_add_sad_entries(node1, n_tunnels, sa_id_1, spi_1,
663                                             crypto_alg, crypto_key, integ_alg,
664                                             integ_key, tunnel_ip1, tunnel_ip2)
665
666         IPsecUtil.vpp_ipsec_spd_add_entries(node1, n_tunnels, spd_id, p_lo,
667                                             False, sa_id_1, raddr_ip2,
668                                             raddr_range)
669
670         IPsecUtil.vpp_ipsec_add_sad_entries(node2, n_tunnels, sa_id_1, spi_1,
671                                             crypto_alg, crypto_key, integ_alg,
672                                             integ_key, tunnel_ip1, tunnel_ip2)
673
674         IPsecUtil.vpp_ipsec_spd_add_entries(node2, n_tunnels, spd_id, p_lo,
675                                             True, sa_id_1, raddr_ip2,
676                                             raddr_range)
677
678         IPsecUtil.vpp_ipsec_add_sad_entries(node2, n_tunnels, sa_id_2, spi_2,
679                                             crypto_alg, crypto_key, integ_alg,
680                                             integ_key, tunnel_ip2, tunnel_ip1)
681
682         IPsecUtil.vpp_ipsec_spd_add_entries(node2, n_tunnels, spd_id, p_lo,
683                                             False, sa_id_2, raddr_ip1,
684                                             raddr_range)
685
686         IPsecUtil.vpp_ipsec_add_sad_entries(node1, n_tunnels, sa_id_2, spi_2,
687                                             crypto_alg, crypto_key, integ_alg,
688                                             integ_key, tunnel_ip2, tunnel_ip1)
689
690         IPsecUtil.vpp_ipsec_spd_add_entries(node1, n_tunnels, spd_id, p_lo,
691                                             True, sa_id_2, raddr_ip1,
692                                             raddr_range)
693
694     @staticmethod
695     def vpp_ipsec_show(node):
696         """Run "show ipsec" debug CLI command.
697
698         :param node: Node to run command on.
699         :type node: dict
700         """
701         VatExecutor().execute_script("ipsec/ipsec_show.vat", node,
702                                      json_out=False)