1#!/bin/bash
2
3# check required parameters
4SGW_REQ_VARS="SGW_PATH SGW_PORT_CFG SGW_WAIT_DEV"
5for reqvar in ${SGW_REQ_VARS}
6do
7	if [[ -z "${!reqvar}" ]]; then
8		echo "Required parameter ${reqvar} is empty"
9		exit 127
10	fi
11done
12
13# check if SGW_PATH point to an executable
14if [[ ! -x ${SGW_PATH} ]]; then
15	echo "${SGW_PATH} is not executable"
16	exit 127
17fi
18
19# setup SGW_LCORE
20SGW_LCORE=${SGW_LCORE:-0}
21
22# setup config and output filenames
23SGW_OUT_FILE=./ipsec-secgw.out1
24SGW_CFG_FILE=$(mktemp)
25
26# setup secgw parameters
27SGW_CMD_EAL_PRM="--lcores=${SGW_LCORE} -n 4"
28SGW_CMD_CFG="(0,0,${SGW_LCORE}),(1,0,${SGW_LCORE})"
29SGW_CMD_PRM="-p 0x3 -u 1 -P --config=\"${SGW_CMD_CFG}\""
30
31# start ipsec-secgw
32secgw_start()
33{
34	SGW_EXEC_FILE=$(mktemp)
35	cat <<EOF > ${SGW_EXEC_FILE}
36stdbuf -o0 ${SGW_PATH} ${SGW_CMD_EAL_PRM} ${CRYPTO_DEV} \
37${SGW_PORT_CFG} ${SGW_EAL_XPRM} \
38-- ${SGW_CMD_PRM} ${SGW_CMD_XPRM} -f ${SGW_CFG_FILE} > \
39${SGW_OUT_FILE} 2>&1 &
40p=\$!
41echo \$p
42EOF
43
44	cat ${SGW_EXEC_FILE}
45	cat ${SGW_CFG_FILE}
46	SGW_PID=`/bin/bash -x ${SGW_EXEC_FILE}`
47
48	# wait till ipsec-secgw start properly
49	i=0
50	st=1
51	while [[ $i -ne 10 && $st -ne 0 ]]; do
52		sleep 1
53		ifconfig ${SGW_WAIT_DEV}
54		st=$?
55		let i++
56	done
57}
58
59# stop ipsec-secgw and cleanup
60secgw_stop()
61{
62	kill ${SGW_PID}
63	rm -f ${SGW_EXEC_FILE}
64	rm -f ${SGW_CFG_FILE}
65}
66