CSIT-1450: PAPI executor
[csit.git] / resources / libraries / python / IPsecUtil.py
index a554a54..5c9a08f 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (c) 2016 Cisco and/or its affiliates.
+# Copyright (c) 2019 Cisco and/or its affiliates.
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # You may obtain a copy of the License at:
 import os
 from ipaddress import ip_network, ip_address
 
-from enum import Enum
+from enum import Enum, IntEnum
+from robot.api import logger
 
-from resources.libraries.python.VatExecutor import VatExecutor
+from resources.libraries.python.PapiExecutor import PapiExecutor
 from resources.libraries.python.topology import Topology
+from resources.libraries.python.VatExecutor import VatExecutor
 from resources.libraries.python.VatJsonUtil import VatJsonUtil
 
 
@@ -60,6 +62,12 @@ class IntegAlg(Enum):
         self.key_len = key_len
 
 
+class IPsecProto(IntEnum):
+    """IPsec protocol."""
+    ESP = 1
+    SEC_AH = 0
+
+
 class IPsecUtil(object):
     """IPsec utilities."""
 
@@ -215,6 +223,72 @@ class IPsecUtil(object):
         """
         return integ_alg.scapy_name
 
+    @staticmethod
+    def ipsec_proto_esp():
+        """Return IPSec protocol ESP.
+
+        :returns: IPsecProto enum ESP object.
+        :rtype: IPsecProto
+        """
+        return int(IPsecProto.ESP)
+
+    @staticmethod
+    def ipsec_proto_ah():
+        """Return IPSec protocol AH.
+
+        :returns: IPsecProto enum AH object.
+        :rtype: IPsecProto
+        """
+        return int(IPsecProto.SEC_AH)
+
+    @staticmethod
+    def vpp_ipsec_select_backend(node, protocol, index=1):
+        """Select IPsec backend.
+
+        :param node: VPP node to select IPsec backend on.
+        :param protocol: IPsec protocol.
+        :param index: Backend index.
+        :type node: dict
+        :type protocol: IPsecProto
+        :type index: int
+        :raises RuntimeError: If failed to select IPsec backend or if no API
+            reply received.
+        """
+
+        cmd = 'ipsec_select_backend'
+        cmd_reply = 'ipsec_select_backend_reply'
+        err_msg = 'Failed to select IPsec backend on host {host}'.format(
+            host=node['host'])
+        args = dict(protocol=protocol, index=index)
+        with PapiExecutor(node) as papi_exec:
+            papi_resp = papi_exec.add(cmd, **args).execute_should_pass(err_msg)
+        data = papi_resp.reply[0]['api_reply'][cmd_reply]
+        if data['retval'] != 0:
+            raise RuntimeError('Failed to select IPsec backend on host {host}'.
+                               format(host=node['host']))
+
+    @staticmethod
+    def vpp_ipsec_backend_dump(node):
+        """Dump IPsec backends.
+
+        :param node: VPP node to dump IPsec backend on.
+        :type node: dict
+        """
+
+        err_msg = 'Failed to dump IPsec backends on host {host}'.format(
+            host=node['host'])
+        with PapiExecutor(node) as papi_exec:
+            papi_resp = papi_exec.add('ipsec_backend_dump').execute_should_pass(
+                err_msg, process_reply=False)
+        # After API change there is returned VPP internal enum object
+        # representing VPP IPSEC protocol instead of integer representation
+        # so JSON fails to decode it - we need to check if it is Python API
+        # bug or we need to adapt vpp_papi_provider to correctly encode
+        # such object into JSON
+        # logger.trace('IPsec backend dump\n{dump}'.
+        # format(dump=papi_resp.reply))
+        logger.trace('IPsec backend dump\n{dump}'.format(dump=papi_resp.stdout))
+
     @staticmethod
     def vpp_ipsec_add_sad_entry(node, sad_id, spi, crypto_alg, crypto_key,
                                 integ_alg, integ_key, tunnel_src=None,
@@ -229,26 +303,26 @@ class IPsecUtil(object):
         :param integ_alg: The integrity algorithm name.
         :param integ_key: The integrity key string.
         :param tunnel_src: Tunnel header source IPv4 or IPv6 address. If not
-        specified ESP transport mode is used.
+            specified ESP transport mode is used.
         :param tunnel_dst: Tunnel header destination IPv4 or IPv6 address. If
-        not specified ESP transport mode is used.
+            not specified ESP transport mode is used.
         :type node: dict
         :type sad_id: int
         :type spi: int
         :type crypto_alg: CryptoAlg
         :type crypto_key: str
-        :type integ_alg: str
+        :type integ_alg: IntegAlg
         :type integ_key: str
         :type tunnel_src: str
         :type tunnel_dst: str
         """
         ckey = crypto_key.encode('hex')
         ikey = integ_key.encode('hex')
-        tunnel = 'tunnel_src {0} tunnel_dst {1}'.format(tunnel_src, tunnel_dst)\
+        tunnel = 'tunnel-src {0} tunnel-dst {1}'.format(tunnel_src, tunnel_dst)\
             if tunnel_src is not None and tunnel_dst is not None else ''
 
         out = VatExecutor.cmd_from_template(node,
-                                            "ipsec/ipsec_sad_add_entry.vat",
+                                            'ipsec/ipsec_sad_add_entry.vat',
                                             sad_id=sad_id, spi=spi,
                                             calg=crypto_alg.alg_name, ckey=ckey,
                                             ialg=integ_alg.alg_name, ikey=ikey,
@@ -266,17 +340,17 @@ class IPsecUtil(object):
         :param node: VPP node to add SAD entry on.
         :param n_entries: Number of SAD entries to be created.
         :param sad_id: First SAD entry ID. All subsequent SAD entries will have
-        id incremented by 1.
+            id incremented by 1.
         :param spi: Security Parameter Index of first SAD entry. All subsequent
-        SAD entries will have spi incremented by 1.
+            SAD entries will have spi incremented by 1.
         :param crypto_alg: The encryption algorithm name.
         :param crypto_key: The encryption key string.
         :param integ_alg: The integrity algorithm name.
         :param integ_key: The integrity key string.
         :param tunnel_src: Tunnel header source IPv4 or IPv6 address. If not
-        specified ESP transport mode is used.
+            specified ESP transport mode is used.
         :param tunnel_dst: Tunnel header destination IPv4 or IPv6 address. If
-        not specified ESP transport mode is used.
+            not specified ESP transport mode is used.
         :type node: dict
         :type n_entries: int
         :type sad_id: int
@@ -288,24 +362,25 @@ class IPsecUtil(object):
         :type tunnel_src: str
         :type tunnel_dst: str
         """
-        tmp_filename = '/tmp/ipsec_sad_{}_add_del_entry.vat'.format(sad_id)
+        tmp_filename = '/tmp/ipsec_sad_{0}_add_del_entry.script'.format(sad_id)
         ckey = crypto_key.encode('hex')
         ikey = integ_key.encode('hex')
-        tunnel = 'tunnel_src {0} tunnel_dst {1}'.format(tunnel_src, tunnel_dst)\
+        tunnel = 'tunnel-src {0} tunnel-dst {1}'.format(tunnel_src, tunnel_dst)\
             if tunnel_src is not None and tunnel_dst is not None else ''
 
-        integ = 'integ_alg {0} integ_key {1}'.format(integ_alg.alg_name, ikey)\
+        integ = 'integ-alg {0} integ-key {1}'.format(integ_alg.alg_name, ikey)\
             if crypto_alg.alg_name != 'aes-gcm-128' else ''
 
         with open(tmp_filename, 'w') as tmp_file:
             for i in range(0, n_entries):
-                buf_str = 'ipsec_sad_add_del_entry esp sad_id {0} spi {1} ' \
-                          'crypto_alg {2} crypto_key {3} {4} {5}\n'.format(
+                buf_str = 'exec ipsec sa add {0} esp spi {1} ' \
+                          'crypto-alg {2} crypto-key {3} {4} {5}\n'.format(
                               sad_id+i, spi+i, crypto_alg.alg_name, ckey, integ,
                               tunnel)
                 tmp_file.write(buf_str)
         vat = VatExecutor()
-        vat.scp_and_execute_script(tmp_filename, node, 300)
+        vat.execute_script(tmp_filename, node, timeout=300, json_out=False,
+                           copy_on_execute=True)
         os.remove(tmp_filename)
 
     @staticmethod
@@ -324,10 +399,9 @@ class IPsecUtil(object):
         ckey = crypto_key.encode('hex')
         ikey = integ_key.encode('hex')
 
-        out = VatExecutor.cmd_from_template(node,
-                                            "ipsec/ipsec_sa_set_key.vat",
-                                            sa_id=sa_id,
-                                            ckey=ckey, ikey=ikey)
+        out = VatExecutor.cmd_from_template(
+            node, 'ipsec/ipsec_sa_set_key.vat', json_param=False, sa_id=sa_id,
+            ckey=ckey, ikey=ikey)
         VatJsonUtil.verify_vat_retval(
             out[0],
             err_msg='Update SA key failed on {0}'.format(node['host']))
@@ -341,7 +415,7 @@ class IPsecUtil(object):
         :type node: dict
         :type spd_id: int
         """
-        out = VatExecutor.cmd_from_template(node, "ipsec/ipsec_spd_add.vat",
+        out = VatExecutor.cmd_from_template(node, 'ipsec/ipsec_spd_add.vat',
                                             spd_id=spd_id)
         VatJsonUtil.verify_vat_retval(
             out[0],
@@ -362,7 +436,7 @@ class IPsecUtil(object):
             if isinstance(interface, basestring) else interface
 
         out = VatExecutor.cmd_from_template(node,
-                                            "ipsec/ipsec_interface_add_spd.vat",
+                                            'ipsec/ipsec_interface_add_spd.vat',
                                             spd_id=spd_id, sw_if_id=sw_if_index)
         VatJsonUtil.verify_vat_retval(
             out[0],
@@ -370,9 +444,10 @@ class IPsecUtil(object):
                 interface, spd_id, node['host']))
 
     @staticmethod
-    def vpp_ipsec_spd_add_entry(node, spd_id, priority, action, inbound=True,
-                                sa_id=None, laddr_range=None, raddr_range=None,
-                                proto=None, lport_range=None, rport_range=None):
+    def vpp_ipsec_policy_add(node, spd_id, priority, action, inbound=True,
+                             sa_id=None, laddr_range=None, raddr_range=None,
+                             proto=None, lport_range=None, rport_range=None,
+                             is_ipv6=False):
         """Create Security Policy Database entry on the VPP node.
 
         :param node: VPP node to add SPD entry on.
@@ -380,19 +455,21 @@ class IPsecUtil(object):
         :param priority: SPD entry priority, higher number = higher priority.
         :param action: Policy action.
         :param inbound: If True policy is for inbound traffic, otherwise
-        outbound.
+            outbound.
         :param sa_id: SAD entry ID for protect action.
         :param laddr_range: Policy selector local IPv4 or IPv6 address range in
-        format IP/prefix or IP/mask. If no mask is provided, it's considered
-        to be /32.
+            format IP/prefix or IP/mask. If no mask is provided,
+            it's considered to be /32.
         :param raddr_range: Policy selector remote IPv4 or IPv6 address range in
-        format IP/prefix or IP/mask. If no mask is provided, it's considered
-        to be /32.
+            format IP/prefix or IP/mask. If no mask is provided,
+            it's considered to be /32.
         :param proto: Policy selector next layer protocol number.
         :param lport_range: Policy selector local TCP/UDP port range in format
-        <port_start>-<port_end>.
+            <port_start>-<port_end>.
         :param rport_range: Policy selector remote TCP/UDP port range in format
-        <port_start>-<port_end>.
+            <port_start>-<port_end>.
+        :param is_ipv6: True in case of IPv6 policy when IPv6 address range is
+            not defined so it will default to address ::/0, otherwise False.
         :type node: dict
         :type spd_id: int
         :type priority: int
@@ -404,40 +481,44 @@ class IPsecUtil(object):
         :type proto: int
         :type lport_range: string
         :type rport_range: string
+        :type is_ipv6: bool
         """
         direction = 'inbound' if inbound else 'outbound'
 
+        if laddr_range is None and is_ipv6:
+            laddr_range = '::/0'
+
+        if raddr_range is None and is_ipv6:
+            raddr_range = '::/0'
+
         act_str = action.value
         if PolicyAction.PROTECT == action and sa_id is not None:
-            act_str += 'sa_id {0}'.format(sa_id)
+            act_str += ' sa {0}'.format(sa_id)
 
         selector = ''
         if laddr_range is not None:
             net = ip_network(unicode(laddr_range), strict=False)
-            selector += 'laddr_start {0} laddr_stop {1} '.format(
+            selector += 'local-ip-range {0} - {1} '.format(
                 net.network_address, net.broadcast_address)
         if raddr_range is not None:
             net = ip_network(unicode(raddr_range), strict=False)
-            selector += 'raddr_start {0} raddr_stop {1} '.format(
+            selector += 'remote-ip-range {0} - {1} '.format(
                 net.network_address, net.broadcast_address)
         if proto is not None:
             selector += 'protocol {0} '.format(proto)
         if lport_range is not None:
-            selector += 'lport_start {p[0]} lport_stop {p[1]} '.format(
-                p=lport_range.split('-'))
+            selector += 'local-port-range {0} '.format(lport_range)
         if rport_range is not None:
-            selector += 'rport_start {p[0]} rport_stop {p[1]} '.format(
-                p=rport_range.split('-'))
+            selector += 'remote-port-range {0} '.format(rport_range)
 
-        out = VatExecutor.cmd_from_template(node,
-                                            "ipsec/ipsec_spd_add_entry.vat",
-                                            spd_id=spd_id, priority=priority,
-                                            action=act_str, direction=direction,
-                                            selector=selector)
+        out = VatExecutor.cmd_from_template(
+            node, 'ipsec/ipsec_policy_add.vat', json_param=False, spd_id=spd_id,
+            priority=priority, action=act_str, direction=direction,
+            selector=selector)
         VatJsonUtil.verify_vat_retval(
             out[0],
-            err_msg='Add entry to SPD {0} failed on {1}'.format(spd_id,
-                                                                node['host']))
+            err_msg='Add IPsec policy ID {0} failed on {1}'.format(
+                spd_id, node['host']))
 
     @staticmethod
     def vpp_ipsec_spd_add_entries(node, n_entries, spd_id, priority, inbound,
@@ -449,15 +530,15 @@ class IPsecUtil(object):
         :param spd_id: SPD ID to add entries on.
         :param priority: SPD entries priority, higher number = higher priority.
         :param inbound: If True policy is for inbound traffic, otherwise
-        outbound.
+            outbound.
         :param sa_id: SAD entry ID for first entry. Each subsequent entry will
-        SAD entry ID incremented by 1.
+            SAD entry ID incremented by 1.
         :param raddr_ip: Policy selector remote IPv4 start address for the first
-        entry. Remote IPv4 end address will be calculated depending on
-        raddr_range parameter. Each subsequent entry will have start address
-        next after IPv4 end address of previous entry.
+            entry. Remote IPv4 end address will be calculated depending on
+            raddr_range parameter. Each subsequent entry will have start address
+            next after IPv4 end address of previous entry.
         :param raddr_range: Mask specifying range of Policy selector Remote IPv4
-        addresses. Valid values are from 1 to 32.
+            addresses. Valid values are from 1 to 32.
         :type node: dict
         :type n_entries: int
         :type spd_id: int
@@ -467,49 +548,56 @@ class IPsecUtil(object):
         :type raddr_ip: string
         :type raddr_range: int
         """
-        tmp_filename = '/tmp/ipsec_spd_{}_add_del_entry.vat'.format(sa_id)
+        tmp_filename = '/tmp/ipsec_spd_{0}_add_del_entry.script'.format(sa_id)
+
         direction = 'inbound' if inbound else 'outbound'
         addr_incr = 1 << (32 - raddr_range)
         addr_ip = int(ip_address(unicode(raddr_ip)))
-        start_str = 'ipsec_spd_add_del_entry spd_id {0} priority {1} {2} ' \
-                    'action protect sa_id'.format(spd_id, priority, direction)
+        start_str = 'exec ipsec policy add spd {0} priority {1} {2} ' \
+                    'action protect sa'.format(spd_id, priority, direction)
         with open(tmp_filename, 'w') as tmp_file:
             for i in range(0, n_entries):
                 r_ip_s = ip_address(addr_ip + addr_incr * i)
                 r_ip_e = ip_address(addr_ip + addr_incr * (i+1) - 1)
-                buf_str = '{0} {1} raddr_start {2} raddr_stop {3}\n'.format(
+                buf_str = '{0} {1} remote-ip-range {2} - {3}\n'.format(
                     start_str, sa_id+i, r_ip_s, r_ip_e)
                 tmp_file.write(buf_str)
         vat = VatExecutor()
-        vat.scp_and_execute_script(tmp_filename, node, 300)
+        vat.execute_script(tmp_filename, node, timeout=300, json_out=False,
+                           copy_on_execute=True)
         os.remove(tmp_filename)
 
     @staticmethod
     def vpp_ipsec_create_tunnel_interfaces(node1, node2, if1_ip_addr,
-                                           if2_ip_addr, n_tunnels, crypto_alg,
-                                           crypto_key, integ_alg, integ_key,
-                                           raddr_ip1, raddr_ip2, raddr_range):
+                                           if2_ip_addr, if1_key, if2_key,
+                                           n_tunnels, crypto_alg, crypto_key,
+                                           integ_alg, integ_key, raddr_ip1,
+                                           raddr_ip2, raddr_range):
         """Create multiple IPsec tunnel interfaces between two VPP nodes.
 
         :param node1: VPP node 1 to create tunnel interfaces.
         :param node2: VPP node 2 to create tunnel interfaces.
         :param if1_ip_addr: VPP node 1 interface IP4 address.
         :param if2_ip_addr: VPP node 2 interface IP4 address.
+        :param if1_key: VPP node 1 interface key from topology file.
+        :param if2_key: VPP node 2 interface key from topology file.
         :param n_tunnels: Number of tunnell interfaces to create.
         :param crypto_alg: The encryption algorithm name.
         :param crypto_key: The encryption key string.
         :param integ_alg: The integrity algorithm name.
         :param integ_key: The integrity key string.
         :param raddr_ip1: Policy selector remote IPv4 start address for the
-        first tunnel in direction node1->node2.
+            first tunnel in direction node1->node2.
         :param raddr_ip2: Policy selector remote IPv4 start address for the
-        first tunnel in direction node2->node1.
+            first tunnel in direction node2->node1.
         :param raddr_range: Mask specifying range of Policy selector Remote IPv4
-        addresses. Valid values are from 1 to 32.
+            addresses. Valid values are from 1 to 32.
         :type node1: dict
         :type node2: dict
         :type if1_ip_addr: str
         :type if2_ip_addr: str
+        :type if1_key: str
+        :type if2_key: str
         :type n_tunnels: int
         :type crypto_alg: CryptoAlg
         :type crypto_key: str
@@ -519,7 +607,6 @@ class IPsecUtil(object):
         :type raddr_ip2: string
         :type raddr_range: int
         """
-
         spi_1 = 10000
         spi_2 = 20000
 
@@ -533,49 +620,83 @@ class IPsecUtil(object):
         ckey = crypto_key.encode('hex')
         ikey = integ_key.encode('hex')
 
+        vat = VatExecutor()
         with open(tmp_fn1, 'w') as tmp_f1, open(tmp_fn2, 'w') as tmp_f2:
             for i in range(0, n_tunnels):
-                if_s = 'ipsec{}'.format(i)
-                dut1_tunnel_s = 'create ipsec tunnel local-ip {0} local-spi ' \
-                                '{1} remote-ip {2} remote-spi {3}\n'.format(
-                                    if1_ip_addr, spi_1+i, if2_ip_addr, spi_2+i)
-                tmp_f1.write(dut1_tunnel_s)
-                dut2_tunnel_s = 'create ipsec tunnel local-ip {0} local-spi ' \
-                                '{1} remote-ip {2} remote-spi {3}\n'.format(
-                                    if2_ip_addr, spi_2+i, if1_ip_addr, spi_1+i)
-                tmp_f2.write(dut2_tunnel_s)
-                loc_c_key = 'set interface ipsec key {0} local crypto {1} ' \
-                            '{2}\n'.format(if_s, crypto_alg.alg_name, ckey)
-                tmp_f1.write(loc_c_key)
-                tmp_f2.write(loc_c_key)
-                rem_c_key = 'set interface ipsec key {0} remote crypto {1} ' \
-                            '{2}\n'.format(if_s, crypto_alg.alg_name, ckey)
-                tmp_f1.write(rem_c_key)
-                tmp_f2.write(rem_c_key)
-                if crypto_alg.alg_name != 'aes-gcm-128':
-                    loc_i_key = 'set interface ipsec key {0} local integ {1} ' \
-                                '{2}\n'.format(if_s, integ_alg.alg_name, ikey)
-                    tmp_f1.write(loc_i_key)
-                    tmp_f2.write(loc_i_key)
-                    rem_i_key = 'set interface ipsec key {0} remote integ {1}' \
-                                ' {2}\n'.format(if_s, integ_alg.alg_name, ikey)
-                    tmp_f1.write(rem_i_key)
-                    tmp_f2.write(rem_i_key)
-                raddr_ip1_s = ip_address(raddr_ip1_i + addr_incr*i)
-                raddr_ip2_s = ip_address(raddr_ip2_i + addr_incr*i)
-                dut1_rte_s = 'ip route add {0}/{1} via {2} {3}\n'.format(
-                    raddr_ip2_s, raddr_range, if2_ip_addr, if_s)
-                tmp_f1.write(dut1_rte_s)
-                dut2_rte_s = 'ip route add {0}/{1} via {2} {3}\n'.format(
-                    raddr_ip1_s, raddr_range, if1_ip_addr, if_s)
-                tmp_f2.write(dut2_rte_s)
-                up_s = 'set int state {0} up\n'.format(if_s)
-                tmp_f1.write(up_s)
-                tmp_f2.write(up_s)
+                integ = ''
+                if not crypto_alg.alg_name.startswith('aes-gcm-'):
+                    integ = 'integ_alg {integ_alg} '\
+                            'local_integ_key {local_integ_key} '\
+                            'remote_integ_key {remote_integ_key} '\
+                            .format(integ_alg=integ_alg.alg_name,
+                                    local_integ_key=ikey,
+                                    remote_integ_key=ikey)
+                dut1_tunnel = 'ipsec_tunnel_if_add_del '\
+                              'local_spi {local_spi} '\
+                              'remote_spi {remote_spi} '\
+                              'crypto_alg {crypto_alg} '\
+                              'local_crypto_key {local_crypto_key} '\
+                              'remote_crypto_key {remote_crypto_key} '\
+                              '{integ} '\
+                              'local_ip {local_ip} '\
+                              'remote_ip {remote_ip}\n'\
+                              .format(local_spi=spi_1+i,
+                                      remote_spi=spi_2+i,
+                                      crypto_alg=crypto_alg.alg_name,
+                                      local_crypto_key=ckey,
+                                      remote_crypto_key=ckey,
+                                      integ=integ,
+                                      local_ip=if1_ip_addr,
+                                      remote_ip=if2_ip_addr)
+                dut2_tunnel = 'ipsec_tunnel_if_add_del '\
+                              'local_spi {local_spi} '\
+                              'remote_spi {remote_spi} '\
+                              'crypto_alg {crypto_alg} '\
+                              'local_crypto_key {local_crypto_key} '\
+                              'remote_crypto_key {remote_crypto_key} '\
+                              '{integ} '\
+                              'local_ip {local_ip} '\
+                              'remote_ip {remote_ip}\n'\
+                              .format(local_spi=spi_2+i,
+                                      remote_spi=spi_1+i,
+                                      crypto_alg=crypto_alg.alg_name,
+                                      local_crypto_key=ckey,
+                                      remote_crypto_key=ckey,
+                                      integ=integ,
+                                      local_ip=if2_ip_addr,
+                                      remote_ip=if1_ip_addr)
+                tmp_f1.write(dut1_tunnel)
+                tmp_f2.write(dut2_tunnel)
+        vat.execute_script(tmp_fn1, node1, timeout=300, json_out=False,
+                           copy_on_execute=True)
+        vat.execute_script(tmp_fn2, node2, timeout=300, json_out=False,
+                           copy_on_execute=True)
+        os.remove(tmp_fn1)
+        os.remove(tmp_fn2)
 
-        vat = VatExecutor()
-        vat.scp_and_execute_cli_script(tmp_fn1, node1, 300)
-        vat.scp_and_execute_cli_script(tmp_fn2, node2, 300)
+        with open(tmp_fn1, 'w') as tmp_f1, open(tmp_fn2, 'w') as tmp_f2:
+            for i in range(0, n_tunnels):
+                raddr_ip1 = ip_address(raddr_ip1_i + addr_incr*i)
+                raddr_ip2 = ip_address(raddr_ip2_i + addr_incr*i)
+                dut1_if = Topology.get_interface_name(node1, if1_key)
+                dut1 = 'exec ip route add {raddr}/{mask} via {addr} ipsec{i}\n'\
+                       'exec set interface unnumbered ipsec{i} use {uifc}\n'\
+                       'exec set interface state ipsec{i} up\n'\
+                       .format(raddr=raddr_ip2, mask=raddr_range,
+                               addr=if2_ip_addr, i=i, uifc=dut1_if)
+                dut2_if = Topology.get_interface_name(node2, if2_key)
+                dut2 = 'exec ip route add {raddr}/{mask} via {addr} ipsec{i}\n'\
+                       'exec set interface unnumbered ipsec{i} use {uifc}\n'\
+                       'exec set interface state ipsec{i} up\n'\
+                       .format(raddr=raddr_ip1, mask=raddr_range,
+                               addr=if1_ip_addr, i=i, uifc=dut2_if)
+                tmp_f1.write(dut1)
+                tmp_f2.write(dut2)
+
+        vat.execute_script(tmp_fn1, node1, timeout=300, json_out=False,
+                           copy_on_execute=True)
+        vat.execute_script(tmp_fn2, node2, timeout=300, json_out=False,
+                           copy_on_execute=True)
         os.remove(tmp_fn1)
         os.remove(tmp_fn2)
 
@@ -599,11 +720,11 @@ class IPsecUtil(object):
         :param tunnel_ip1: Tunnel node1 IPv4 address.
         :param tunnel_ip2: Tunnel node2 IPv4 address.
         :param raddr_ip1: Policy selector remote IPv4 start address for the
-        first tunnel in direction node1->node2.
+            first tunnel in direction node1->node2.
         :param raddr_ip2: Policy selector remote IPv4 start address for the
-        first tunnel in direction node2->node1.
+            first tunnel in direction node2->node1.
         :param raddr_range: Mask specifying range of Policy selector Remote IPv4
-        addresses. Valid values are from 1 to 32.
+            addresses. Valid values are from 1 to 32.
         :type node1: dict
         :type node2: dict
         :type interface1: str or int
@@ -630,21 +751,17 @@ class IPsecUtil(object):
 
         IPsecUtil.vpp_ipsec_add_spd(node1, spd_id)
         IPsecUtil.vpp_ipsec_spd_add_if(node1, spd_id, interface1)
-        IPsecUtil.vpp_ipsec_spd_add_entry(node1, spd_id, p_hi,
-                                          PolicyAction.BYPASS, inbound=False,
-                                          proto=proto)
-        IPsecUtil.vpp_ipsec_spd_add_entry(node1, spd_id, p_hi,
-                                          PolicyAction.BYPASS, inbound=True,
-                                          proto=proto)
+        IPsecUtil.vpp_ipsec_policy_add(node1, spd_id, p_hi, PolicyAction.BYPASS,
+                                       inbound=False, proto=proto)
+        IPsecUtil.vpp_ipsec_policy_add(node1, spd_id, p_hi, PolicyAction.BYPASS,
+                                       inbound=True, proto=proto)
 
         IPsecUtil.vpp_ipsec_add_spd(node2, spd_id)
         IPsecUtil.vpp_ipsec_spd_add_if(node2, spd_id, interface2)
-        IPsecUtil.vpp_ipsec_spd_add_entry(node2, spd_id, p_hi,
-                                          PolicyAction.BYPASS, inbound=False,
-                                          proto=proto)
-        IPsecUtil.vpp_ipsec_spd_add_entry(node2, spd_id, p_hi,
-                                          PolicyAction.BYPASS, inbound=True,
-                                          proto=proto)
+        IPsecUtil.vpp_ipsec_policy_add(node2, spd_id, p_hi, PolicyAction.BYPASS,
+                                       inbound=False, proto=proto)
+        IPsecUtil.vpp_ipsec_policy_add(node2, spd_id, p_hi, PolicyAction.BYPASS,
+                                       inbound=True, proto=proto)
 
         IPsecUtil.vpp_ipsec_add_sad_entries(node1, n_tunnels, sa_id_1, spi_1,
                                             crypto_alg, crypto_key, integ_alg,
@@ -685,5 +802,5 @@ class IPsecUtil(object):
         :param node: Node to run command on.
         :type node: dict
         """
-        VatExecutor().execute_script("ipsec/ipsec_show.vat", node,
+        VatExecutor().execute_script('ipsec/ipsec_show.vat', node,
                                      json_out=False)