- 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')
-
- vat = VatExecutor()
- with open(tmp_fn1, 'w') as tmp_f1, open(tmp_fn2, 'w') as tmp_f2:
- for i in range(0, n_tunnels):
- integ = ''
- # if crypto_alg.alg_name != 'aes-gcm-128':
- 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)
-
- 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)
+ 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'
+ 'exec set ip arp {uifc} {raddr}/32 {rmac} static\n'
+ .format(
+ iaddr=if2_ip - 1,
+ raddr=if2_ip,
+ uifc=Topology.get_interface_name(
+ nodes['DUT1'], if1_key),
+ rmac=Topology.get_interface_mac(
+ nodes['DUT2'], if2_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,
+ flags=InterfaceStatusFlags.IF_STATUS_API_FLAG_ADMIN_UP.value)
+ 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=True,
+ del_all=False,
+ prefix=IPUtil.create_prefix_object(
+ if2_ip - 1, 96 if if2_ip.version == 6 else 24)
+ )
+ 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)
+ cmd4 = 'ip_neighbor_add_del'
+ args4 = dict(
+ is_add=1,
+ neighbor=dict(
+ sw_if_index=Topology.get_interface_sw_index(
+ nodes['DUT1'], if1_key),
+ flags=1,
+ mac_address=str(
+ Topology.get_interface_mac(nodes['DUT2'], if2_key)),
+ ip_address=str(ip_address(unicode(if2_ip_addr)))
+ )
+ )
+ err_msg = 'Failed to add IP neighbor on interface {ifc}'.format(
+ ifc=if1_key)
+ papi_exec.add(cmd4, **args4).get_reply(err_msg)
+ # Configure IPsec tunnel interfaces
+ args1 = dict(
+ sw_if_index=loop_sw_if_idx,
+ is_add=True,
+ del_all=False,
+ prefix=None
+ )
+ 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['prefix'] = IPUtil.create_prefix_object(
+ if1_ip + i * addr_incr, 128 if if1_ip.version == 6 else 32)
+ 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=True,
+ 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,
+ flags=InterfaceStatusFlags.IF_STATUS_API_FLAG_ADMIN_UP.value)
+ 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=True,
+ del_all=False,
+ prefix=IPUtil.create_prefix_object(
+ if2_ip, 96 if if2_ip.version == 6 else 24)
+ )
+ 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=True,
+ 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,
+ flags=InterfaceStatusFlags.IF_STATUS_API_FLAG_ADMIN_UP.value)
+ 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)