-
- spi_1 = 10000
- spi_2 = 20000
-
- raddr_ip1_i = int(ip_address(unicode(raddr_ip1)))
- raddr_ip2_i = int(ip_address(unicode(raddr_ip2)))
- addr_incr = 1 << (32 - raddr_range)
-
- tmp_fn1 = '/tmp/ipsec_create_tunnel_dut1.config'
- tmp_fn2 = '/tmp/ipsec_create_tunnel_dut2.config'
-
- ckey = crypto_key.encode('hex')
- ikey = integ_key.encode('hex')
-
- 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)
-
- vat = VatExecutor()
- vat.scp_and_execute_cli_script(tmp_fn1, node1, 300)
- vat.scp_and_execute_cli_script(tmp_fn2, node2, 300)
- os.remove(tmp_fn1)
- os.remove(tmp_fn2)
+ n_tunnels = int(n_tunnels)
+ spi_1 = 100000
+ spi_2 = 200000
+ if1_ip = ip_address(unicode(if1_ip_addr))
+ if2_ip = ip_address(unicode(if2_ip_addr))
+ raddr_ip1 = ip_address(unicode(raddr_ip1))
+ raddr_ip2 = ip_address(unicode(raddr_ip2))
+ addr_incr = 1 << (128 - raddr_range) if if1_ip.version == 6 \
+ else 1 << (32 - raddr_range)
+
+ if n_tunnels > 10:
+ tmp_fn1 = '/tmp/ipsec_create_tunnel_dut1.config'
+ tmp_fn2 = '/tmp/ipsec_create_tunnel_dut2.config'
+ vat = VatExecutor()
+ with open(tmp_fn1, 'w') as tmp_f1, open(tmp_fn2, 'w') as tmp_f2:
+ tmp_f1.write(
+ 'exec create loopback interface\n'
+ 'exec set interface state loop0 up\n'
+ 'exec set interface ip address {uifc} {iaddr}/{mask}\n'
+ .format(
+ iaddr=if2_ip - 1,
+ uifc=Topology.get_interface_name(
+ nodes['DUT1'], if1_key),
+ mask=96 if if2_ip.version == 6 else 24))
+ tmp_f2.write(
+ 'exec set interface ip address {uifc} {iaddr}/{mask}\n'
+ .format(
+ iaddr=if2_ip,
+ uifc=Topology.get_interface_name(
+ nodes['DUT2'], if2_key),
+ mask=96 if if2_ip.version == 6 else 24))
+ for i in xrange(n_tunnels):
+ ckey = gen_key(IPsecUtil.get_crypto_alg_key_len(
+ crypto_alg)).encode('hex')
+ if integ_alg:
+ ikey = gen_key(IPsecUtil.get_integ_alg_key_len(
+ integ_alg)).encode('hex')
+ 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))
+ else:
+ integ = ''
+ tmp_f1.write(
+ 'exec set interface ip address loop0 {laddr}/32\n'
+ '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 {laddr} '
+ 'remote_ip {raddr}\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,
+ laddr=if1_ip + i * addr_incr,
+ raddr=if2_ip))
+ tmp_f2.write(
+ '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 {laddr} '
+ 'remote_ip {raddr}\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,
+ laddr=if2_ip,
+ raddr=if1_ip + i * addr_incr))
+ vat.execute_script(
+ tmp_fn1, nodes['DUT1'], timeout=1800, json_out=False,
+ copy_on_execute=True,
+ history=False if n_tunnels > 100 else True)
+ vat.execute_script(
+ tmp_fn2, nodes['DUT2'], timeout=1800, json_out=False,
+ copy_on_execute=True,
+ history=False if n_tunnels > 100 else True)
+ os.remove(tmp_fn1)
+ os.remove(tmp_fn2)
+
+ with open(tmp_fn1, 'w') as tmp_f1, open(tmp_fn2, 'w') as tmp_f2:
+ tmp_f2.write(
+ 'exec ip route add {raddr} via {uifc} {iaddr}\n'
+ .format(
+ raddr=ip_network(unicode(if1_ip_addr+'/8'), False),
+ iaddr=if2_ip - 1,
+ uifc=Topology.get_interface_name(
+ nodes['DUT2'], if2_key)))
+ for i in xrange(n_tunnels):
+ tmp_f1.write(
+ 'exec set interface unnumbered ipsec{i} use {uifc}\n'
+ 'exec set interface state ipsec{i} up\n'
+ 'exec ip route add {taddr}/{mask} via ipsec{i}\n'
+ .format(
+ taddr=raddr_ip2 + i,
+ i=i,
+ uifc=Topology.get_interface_name(nodes['DUT1'],
+ if1_key),
+ mask=128 if if2_ip.version == 6 else 32))
+ tmp_f2.write(
+ 'exec set interface unnumbered ipsec{i} use {uifc}\n'
+ 'exec set interface state ipsec{i} up\n'
+ 'exec ip route add {taddr}/{mask} via ipsec{i}\n'
+ .format(
+ taddr=raddr_ip1 + i,
+ i=i,
+ uifc=Topology.get_interface_name(nodes['DUT2'],
+ if2_key),
+ mask=128 if if2_ip.version == 6 else 32))
+ vat.execute_script(
+ tmp_fn1, nodes['DUT1'], timeout=1800, json_out=False,
+ copy_on_execute=True,
+ history=False if n_tunnels > 100 else True)
+ vat.execute_script(
+ tmp_fn2, nodes['DUT2'], timeout=1800, json_out=False,
+ copy_on_execute=True,
+ history=False if n_tunnels > 100 else True)
+ os.remove(tmp_fn1)
+ os.remove(tmp_fn2)
+ return
+
+ with PapiSocketExecutor(nodes['DUT1']) as papi_exec:
+ # Create loopback interface on DUT1, set it to up state
+ cmd1 = 'create_loopback'
+ args1 = dict(mac_address=0)
+ err_msg = 'Failed to create loopback interface on host {host}'.\
+ format(host=nodes['DUT1']['host'])
+ loop_sw_if_idx = papi_exec.add(cmd1, **args1).\
+ get_sw_if_index(err_msg)
+ cmd1 = 'sw_interface_set_flags'
+ args1 = dict(
+ sw_if_index=loop_sw_if_idx,
+ admin_up_down=1)
+ err_msg = 'Failed to set loopback interface state up on host ' \
+ '{host}'.format(host=nodes['DUT1']['host'])
+ papi_exec.add(cmd1, **args1).get_reply(err_msg)
+ # Set IP address on VPP node 1 interface
+ cmd1 = 'sw_interface_add_del_address'
+ args1 = dict(
+ sw_if_index=InterfaceUtil.get_interface_index(
+ nodes['DUT1'], if1_key),
+ is_add=1,
+ is_ipv6=1 if if2_ip.version == 6 else 0,
+ del_all=0,
+ address_length=96 if if2_ip.version == 6 else 24,
+ address=getattr(if2_ip - 1, 'packed'))
+ err_msg = 'Failed to set IP address on interface {ifc} on host ' \
+ '{host}'.format(ifc=if1_key, host=nodes['DUT1']['host'])
+ papi_exec.add(cmd1, **args1).get_reply(err_msg)
+ # Configure IPsec tunnel interfaces
+ args1 = dict(
+ sw_if_index=loop_sw_if_idx,
+ is_add=1,
+ is_ipv6=1 if if1_ip.version == 6 else 0,
+ del_all=0,
+ address_length=128 if if1_ip.version == 6 else 32,
+ address='')
+ cmd2 = 'ipsec_tunnel_if_add_del'
+ args2 = dict(
+ is_add=1,
+ local_ip=None,
+ remote_ip=None,
+ local_spi=0,
+ remote_spi=0,
+ crypto_alg=crypto_alg.alg_int_repr,
+ local_crypto_key_len=0,
+ local_crypto_key=None,
+ remote_crypto_key_len=0,
+ remote_crypto_key=None,
+ integ_alg=integ_alg.alg_int_repr if integ_alg else 0,
+ local_integ_key_len=0,
+ local_integ_key=None,
+ remote_integ_key_len=0,
+ remote_integ_key=None,
+ tx_table_id=0
+ )
+ err_msg = 'Failed to add IPsec tunnel interfaces on host {host}'.\
+ format(host=nodes['DUT1']['host'])
+ ipsec_tunnels = list()
+ ckeys = list()
+ ikeys = list()
+ for i in xrange(n_tunnels):
+ ckeys.append(
+ gen_key(IPsecUtil.get_crypto_alg_key_len(crypto_alg)))
+ if integ_alg:
+ ikeys.append(
+ gen_key(IPsecUtil.get_integ_alg_key_len(integ_alg)))
+ args1['address'] = getattr(if1_ip + i * addr_incr, 'packed')
+ args2['local_spi'] = spi_1 + i
+ args2['remote_spi'] = spi_2 + i
+ args2['local_ip'] = IPUtil.create_ip_address_object(
+ if1_ip + i * addr_incr)
+ args2['remote_ip'] = IPUtil.create_ip_address_object(if2_ip)
+ args2['local_crypto_key_len'] = len(ckeys[i])
+ args2['local_crypto_key'] = ckeys[i]
+ args2['remote_crypto_key_len'] = len(ckeys[i])
+ args2['remote_crypto_key'] = ckeys[i]
+ if integ_alg:
+ args2['local_integ_key_len'] = len(ikeys[i])
+ args2['local_integ_key'] = ikeys[i]
+ args2['remote_integ_key_len'] = len(ikeys[i])
+ args2['remote_integ_key'] = ikeys[i]
+ history = False if 1 < i < n_tunnels - 1 else True
+ papi_exec.add(cmd1, history=history, **args1).\
+ add(cmd2, history=history, **args2)
+ replies = papi_exec.get_replies(err_msg)
+ for reply in replies:
+ if 'sw_if_index' in reply:
+ ipsec_tunnels.append(reply["sw_if_index"])
+ # Configure IP routes
+ cmd1 = 'sw_interface_set_unnumbered'
+ args1 = dict(
+ is_add=1,
+ sw_if_index=InterfaceUtil.get_interface_index(
+ nodes['DUT1'], if1_key),
+ unnumbered_sw_if_index=0
+ )
+ cmd2 = 'sw_interface_set_flags'
+ args2 = dict(
+ sw_if_index=0,
+ admin_up_down=1)
+ cmd3 = 'ip_route_add_del'
+ args3 = dict(
+ is_add=1,
+ is_multipath=0,
+ route=None
+ )
+ err_msg = 'Failed to add IP routes on host {host}'.format(
+ host=nodes['DUT1']['host'])
+ for i in xrange(n_tunnels):
+ args1['unnumbered_sw_if_index'] = ipsec_tunnels[i]
+ args2['sw_if_index'] = ipsec_tunnels[i]
+ args3['route'] = IPUtil.compose_vpp_route_structure(
+ nodes['DUT1'],
+ (raddr_ip2 + i).compressed,
+ prefix_len=128 if raddr_ip2.version == 6 else 32,
+ interface=ipsec_tunnels[i]
+ )
+ history = False if 1 < i < n_tunnels - 1 else True
+ papi_exec.add(cmd1, history=history, **args1).\
+ add(cmd2, history=history, **args2).\
+ add(cmd3, history=history, **args3)
+ papi_exec.get_replies(err_msg)
+
+ with PapiSocketExecutor(nodes['DUT2']) as papi_exec:
+ # Set IP address on VPP node 2 interface
+ cmd1 = 'sw_interface_add_del_address'
+ args1 = dict(
+ sw_if_index=InterfaceUtil.get_interface_index(
+ nodes['DUT2'], if2_key),
+ is_add=1,
+ is_ipv6=1 if if2_ip.version == 6 else 0,
+ del_all=0,
+ address_length=96 if if2_ip.version == 6 else 24,
+ address=if2_ip.packed)
+ err_msg = 'Failed to set IP address on interface {ifc} on host ' \
+ '{host}'.format(ifc=if2_key, host=nodes['DUT2']['host'])
+ papi_exec.add(cmd1, **args1).get_reply(err_msg)
+ # Configure IPsec tunnel interfaces
+ cmd2 = 'ipsec_tunnel_if_add_del'
+ args2 = dict(
+ is_add=1,
+ local_ip=IPUtil.create_ip_address_object(if2_ip),
+ remote_ip=None,
+ local_spi=0,
+ remote_spi=0,
+ crypto_alg=crypto_alg.alg_int_repr,
+ local_crypto_key_len=0,
+ local_crypto_key=None,
+ remote_crypto_key_len=0,
+ remote_crypto_key=None,
+ integ_alg=integ_alg.alg_int_repr if integ_alg else 0,
+ local_integ_key_len=0,
+ local_integ_key=None,
+ remote_integ_key_len=0,
+ remote_integ_key=None,
+ tx_table_id=0
+ )
+ err_msg = 'Failed to add IPsec tunnel interfaces on host {host}'. \
+ format(host=nodes['DUT2']['host'])
+ ipsec_tunnels = list()
+ for i in xrange(n_tunnels):
+ args2['local_spi'] = spi_2 + i
+ args2['remote_spi'] = spi_1 + i
+ args2['local_ip'] = IPUtil.create_ip_address_object(if2_ip)
+ args2['remote_ip'] = IPUtil.create_ip_address_object(
+ if1_ip + i * addr_incr)
+ args2['local_crypto_key_len'] = len(ckeys[i])
+ args2['local_crypto_key'] = ckeys[i]
+ args2['remote_crypto_key_len'] = len(ckeys[i])
+ args2['remote_crypto_key'] = ckeys[i]
+ if integ_alg:
+ args2['local_integ_key_len'] = len(ikeys[i])
+ args2['local_integ_key'] = ikeys[i]
+ args2['remote_integ_key_len'] = len(ikeys[i])
+ args2['remote_integ_key'] = ikeys[i]
+ history = False if 1 < i < n_tunnels - 1 else True
+ papi_exec.add(cmd2, history=history, **args2)
+ replies = papi_exec.get_replies(err_msg)
+ for reply in replies:
+ if 'sw_if_index' in reply:
+ ipsec_tunnels.append(reply["sw_if_index"])
+ # Configure IP routes
+ cmd1 = 'ip_route_add_del'
+ route = IPUtil.compose_vpp_route_structure(
+ nodes['DUT2'], if1_ip.compressed,
+ prefix_len=32 if if1_ip.version == 6 else 8,
+ interface=if2_key,
+ gateway=(if2_ip - 1).compressed
+ )
+ args1 = dict(
+ is_add=1,
+ is_multipath=0,
+ route=route
+ )
+ papi_exec.add(cmd1, **args1)
+ cmd1 = 'sw_interface_set_unnumbered'
+ args1 = dict(
+ is_add=1,
+ sw_if_index=InterfaceUtil.get_interface_index(
+ nodes['DUT2'], if2_key),
+ unnumbered_sw_if_index=0
+ )
+ cmd2 = 'sw_interface_set_flags'
+ args2 = dict(
+ sw_if_index=0,
+ admin_up_down=1)
+ cmd3 = 'ip_route_add_del'
+ args3 = dict(
+ is_add=1,
+ is_multipath=0,
+ route=None
+ )
+ err_msg = 'Failed to add IP routes on host {host}'.format(
+ host=nodes['DUT2']['host'])
+ for i in xrange(n_tunnels):
+ args1['unnumbered_sw_if_index'] = ipsec_tunnels[i]
+ args2['sw_if_index'] = ipsec_tunnels[i]
+ args3['route'] = IPUtil.compose_vpp_route_structure(
+ nodes['DUT1'],
+ (raddr_ip1 + i).compressed,
+ prefix_len=128 if raddr_ip1.version == 6 else 32,
+ interface=ipsec_tunnels[i]
+ )
+ history = False if 1 < i < n_tunnels - 1 else True
+ papi_exec.add(cmd1, history=history, **args1). \
+ add(cmd2, history=history, **args2). \
+ add(cmd3, history=history, **args3)
+ papi_exec.get_replies(err_msg)