Gabriel Oginski | 9587577 | 2022-07-08 07:46:32 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | DOCKER_1_NAME="vpp_sswan_docker1" |
| 4 | DOCKER_2_NAME="vpp_sswan_docker2" |
| 5 | |
| 6 | if [ "_$1" == "_prepare_containers" ]; |
| 7 | then |
| 8 | echo "### Building docker image for vpp sswan plugin" |
| 9 | ./init_containers.sh build_docker_image |
| 10 | echo "### Building the first container for vpp sswan plugin" |
| 11 | ./init_containers.sh create_docker1 $DOCKER_1_NAME |
| 12 | echo "### Building the second container for vpp sswan plugin" |
| 13 | ./init_containers.sh create_docker2 $DOCKER_2_NAME |
| 14 | elif [ "_$1" == "_config" ]; |
| 15 | then |
| 16 | echo "### Configuration $DOCKER_1_NAME and $DOCKER_2_NAME" |
| 17 | #ADD 1: set network namespace |
| 18 | echo "### Adding network namespace for $DOCKER_1_NAME and $DOCKER_2_NAME" |
| 19 | ip netns add vpp_sswan_temp |
| 20 | ./exposedockernetns.sh $DOCKER_1_NAME |
| 21 | ./exposedockernetns.sh $DOCKER_2_NAME |
| 22 | ip netns del vpp_sswan_temp |
| 23 | echo "### Adding network namespace for $DOCKER_1_NAME and $DOCKER_2_NAME finished" |
| 24 | |
| 25 | #ADD 2: settings network |
| 26 | echo "### Setting network for $DOCKER_1_NAME and $DOCKER_2_NAME" |
| 27 | |
| 28 | ip link add docker_1_eth2 type veth peer name docker_2_eth2 |
| 29 | ip link set netns $DOCKER_1_NAME dev docker_1_eth2 |
| 30 | ip link set netns $DOCKER_2_NAME dev docker_2_eth2 |
| 31 | #ADD 3: ip address |
| 32 | ip netns exec $DOCKER_2_NAME ip addr add 192.168.0.1/24 dev docker_2_eth2 |
| 33 | ip netns exec $DOCKER_2_NAME ip link set dev docker_2_eth2 up |
| 34 | |
| 35 | #LAN for Docker 1 |
| 36 | ip link add docker_1a_eth1 type veth peer name docker_1b_eth1 |
| 37 | ip link set netns $DOCKER_1_NAME dev docker_1a_eth1 |
| 38 | ip link set netns $DOCKER_1_NAME dev docker_1b_eth1 |
| 39 | ip netns exec $DOCKER_1_NAME ip addr add 192.168.200.10/24 dev docker_1b_eth1 |
| 40 | ip netns exec $DOCKER_1_NAME ip link set dev docker_1b_eth1 up |
| 41 | ip netns exec $DOCKER_1_NAME ip route add 192.168.100.0/24 via 192.168.200.1 dev docker_1b_eth1 |
| 42 | |
| 43 | #LAN for Docker 2 |
| 44 | ip link add docker_2a_eth1 type veth peer name docker_2b_eth1 |
| 45 | ip link set netns $DOCKER_2_NAME dev docker_2a_eth1 |
| 46 | ip link set netns $DOCKER_2_NAME dev docker_2b_eth1 |
| 47 | ip netns exec $DOCKER_2_NAME ip addr add 192.168.100.1/24 dev docker_2a_eth1 |
| 48 | ip netns exec $DOCKER_2_NAME ip addr add 192.168.100.10/24 dev docker_2b_eth1 |
| 49 | ip netns exec $DOCKER_2_NAME ip link set dev docker_2a_eth1 up |
| 50 | ip netns exec $DOCKER_2_NAME ip link set dev docker_2b_eth1 up |
| 51 | ip netns exec $DOCKER_2_NAME ip route add 192.168.200.0/24 via 192.168.100.1 dev docker_2b_eth1 |
| 52 | |
| 53 | echo "### Setting network for $DOCKER_1_NAME and $DOCKER_2_NAME finished" |
| 54 | |
| 55 | #ADD 4: run VPP on the first docker |
| 56 | echo "### Running VPP and sswan on: $DOCKER_1_NAME and $DOCKER_2_NAME" |
| 57 | docker exec -i "$DOCKER_1_NAME" "/root/run_vpp.sh" |
| 58 | docker exec -d $DOCKER_2_NAME systemctl restart strongswan.service |
| 59 | echo "### Running VPP and sswan on: $DOCKER_1_NAME and $DOCKER_2_NAME finished" |
| 60 | |
| 61 | #ADD 5: initiate sswan |
| 62 | echo "### initiate SSWAN between $DOCKER_1_NAME and $DOCKER_2_NAME" |
| 63 | docker exec -i $DOCKER_1_NAME swanctl --initiate --child net-net |
| 64 | echo "### initiate SSWAN between $DOCKER_1_NAME and $DOCKER_2_NAME finished" |
| 65 | |
| 66 | elif [ "_$1" == "_clean" ]; |
| 67 | then |
| 68 | #DELETE 5: initiate sswan |
| 69 | echo "### Terminate SSWAN between $DOCKER_1_NAME and $DOCKER_2_NAME" |
| 70 | docker exec -i $DOCKER_1_NAME swanctl --terminate --child net-net |
| 71 | echo "### Terminate SSWAN between $DOCKER_1_NAME and $DOCKER_2_NAME finished" |
| 72 | |
| 73 | #DELETE 4: run VPP on the first docker |
| 74 | echo "### Exit VPP on: $DOCKER_1_NAME" |
| 75 | docker exec -d $DOCKER_1_NAME pkill -9 -f vpp |
| 76 | echo "### Exit VPP on: $DOCKER_1_NAME finished" |
| 77 | |
| 78 | echo "### Deletting settings network for $DOCKER_1_NAME and $DOCKER_2_NAME" |
| 79 | #DELETE 3: ip address |
| 80 | ip netns exec $DOCKER_1_NAME ip link set dev docker_1_eth2 down |
| 81 | ip netns exec $DOCKER_2_NAME ip link set dev docker_2_eth2 down |
| 82 | #docker 1 |
| 83 | ip netns exec $DOCKER_1_NAME ip link set dev docker_1b_eth1 down |
| 84 | ip netns exec $DOCKER_1_NAME ip link set netns 1 dev docker_1a_eth1 |
| 85 | ip netns exec $DOCKER_1_NAME ip link set netns 1 dev docker_1b_eth1 |
| 86 | ip link del docker_1a_eth1 type veth peer name docker_1b_eth1 |
| 87 | |
| 88 | #docker 2 |
| 89 | ip netns exec $DOCKER_2_NAME ip link set dev docker_2a_eth1 down |
| 90 | ip netns exec $DOCKER_2_NAME ip link set dev docker_2b_eth1 down |
| 91 | ip netns exec $DOCKER_2_NAME ip link set netns 1 dev docker_2a_eth1 |
| 92 | ip netns exec $DOCKER_2_NAME ip link set netns 1 dev docker_2b_eth1 |
| 93 | ip link del docker_2a_eth1 type veth peer name docker_2b_eth1 |
| 94 | |
| 95 | #DELETE 2: settings network |
| 96 | ip netns exec $DOCKER_1_NAME ip link set netns 1 dev docker_1_eth2 |
| 97 | ip netns exec $DOCKER_2_NAME ip link set netns 1 dev docker_2_eth2 |
| 98 | ip link del docker_1_eth2 type veth peer name docker_2_eth2 |
| 99 | echo "### Deletting settings network for $DOCKER_1_NAME and $DOCKER_2_NAME finished" |
| 100 | |
| 101 | #DELETE 1: delete network namespace |
| 102 | echo "### Deleting network namespace for $DOCKER_1_NAME and $DOCKER_2_NAME" |
| 103 | ip netns del $DOCKER_1_NAME |
| 104 | ip netns del $DOCKER_2_NAME |
| 105 | echo "### Deleting network namespace for $DOCKER_1_NAME and $DOCKER_2_NAME finished" |
| 106 | |
| 107 | elif [ "_$1" == "_deleted" ]; |
| 108 | then |
| 109 | echo "### Exit VPP on: $DOCKER_1_NAME" |
| 110 | docker exec -d $DOCKER_1_NAME pkill -9 -f vpp |
| 111 | echo "### Exit VPP on: $DOCKER_1_NAME finished" |
| 112 | |
| 113 | echo "### Deleting container $DOCKER_1_NAME and $DOCKER_2_NAME" |
| 114 | ./init_containers.sh clean $DOCKER_1_NAME |
| 115 | ./init_containers.sh clean $DOCKER_2_NAME |
| 116 | echo "### Deleting image" |
| 117 | ./init_containers.sh clean_image |
| 118 | fi |