blob: c19c08138464b8d59961994855e49386a43d9c06 [file] [log] [blame]
Neels Hofmeyr697a6172018-08-22 17:32:21 +02001#!/usr/bin/env bash
Oliver Smith24ddf9c2019-01-30 16:48:18 +01002
Oliver Smithcd472bd2022-01-17 15:59:31 +01003# tmux: start this script inside a new session
4tmux_session="CN"
5if [ "${TERMINAL}" = "tmux" ] && [ "$1" != "inside-tmux" ]; then
6 echo "Starting tmux session '$tmux_session'"
7 unset TMUX
8 exec tmux new-session -s "$tmux_session" -n "RUN" "$0" "inside-tmux"
9fi
10
Oliver Smith24ddf9c2019-01-30 16:48:18 +010011if ! ../fill_config.py --check-stale; then
12 echo
13 echo "WARNING: STALE CONFIGS - your net configs are older than the templates they should be based on!"
14 echo " * Hit enter to continue, and use the stale config files"
15 echo " * Hit ^C and run 'make regen' to regenerate your configs"
16 read enter_to_continue
17fi
Neels Hofmeyr697a6172018-08-22 17:32:21 +020018
19dev="${ETH_DEV}"
Neels Hofmeyr697a6172018-08-22 17:32:21 +020020apn="${APN_DEV}"
21
22sudo true || exit 1
23
Oliver Smithcef58b32022-02-23 17:12:09 +010024if ! sudo iptables -t nat -C POSTROUTING -s ${GGSN_NET} -o $dev -j MASQUERADE >/dev/null 2>&1; then
Oliver Smith4186b952021-07-07 16:40:36 +020025 echo "Adding iptables rule for masquerade"
26 sudo iptables -t nat -I POSTROUTING -s ${GGSN_NET} -o $dev -j MASQUERADE
Neels Hofmeyr697a6172018-08-22 17:32:21 +020027fi
28
29if [ "$(sudo cat /proc/sys/net/ipv4/ip_forward)" = "0" ]; then
30 sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
31fi
32
33if [ -z "$(ip tuntap show | grep $apn)" ]; then
34 sudo ip tuntap add dev $apn mode tun user $USER group $USER
35 sudo ip addr add ${GGSN_NET} dev $apn
36 sudo ip link set $apn up
37fi
38
Oliver Smith2490f692019-03-13 15:16:16 +010039if [ -z "$(ip addr show | grep "${TO_RAN_IP}")" ]; then
40 echo "No interface has IP address ${TO_RAN_IP}! Hit enter to continue anyway."
Neels Hofmeyrede80162018-09-16 13:49:13 +020041 read enter_to_continue
42fi
Oliver Smith2490f692019-03-13 15:16:16 +010043if [ -z "$(ip addr show | grep "${TO_RAN_IU_IP}")" ]; then
44 echo "No interface has IP address ${TO_RAN_IU_IP}! Hit enter to 'ip addr add ${TO_RAN_IU_IP}/32 dev $dev'"
Neels Hofmeyrede80162018-09-16 13:49:13 +020045 read enter_to_continue
Oliver Smith2490f692019-03-13 15:16:16 +010046 sudo ip addr add ${TO_RAN_IU_IP}/32 dev $dev
Neels Hofmeyr697a6172018-08-22 17:32:21 +020047fi
48
Oliver Smith67cd47f2022-02-23 15:25:32 +010049# Enable multicast on lo for virtual MS
50if [ "${MS_RUN_IN_OSMO_DEV}" = 1 ]; then
51 if [ -z "$(ip link show lo | grep MULTICAST)" ]; then
52 echo "Loopback device doesn't have multicast enabled! Hit enter to enable it"
53 read enter_to_continue
54 sudo ip link set lo multicast on
55 fi
56 if [ -z "$(ip route show dev lo | grep '224\.0\.0\.0/4')" ]; then
57 sudo ip route add 224.0.0.0/4 dev lo
58 fi
59fi
60
Neels Hofmeyr697a6172018-08-22 17:32:21 +020061logdir="current_log"
Oliver Smith669f1db2022-01-17 15:59:24 +010062piddir="run/pids"
Oliver Smith63273712022-01-18 11:21:37 +010063launcherdir="run/launchers"
64rm -rf "$launcherdir"
65mkdir -p "$logdir" "$piddir" "$launcherdir"
Neels Hofmeyr697a6172018-08-22 17:32:21 +020066
Oliver Smith4d7072c2018-09-28 11:30:46 +020067find_term() {
68 # Find a terminal program and write to the global "terminal" variable
Oliver Smithcd472bd2022-01-17 15:59:31 +010069 local programs="urxvt xterm tmux"
Oliver Smith4d7072c2018-09-28 11:30:46 +020070
Oliver Smithb28a9102022-01-17 15:59:02 +010071 if [ -z "${TERMINAL}" ]; then
72 echo "ERROR: TERMINAL is not defined in your osmo-dev net config file. Please add it."
73 exit 1
74 fi
75
76 case " $programs " in
77 *" ${TERMINAL} "*)
78 terminal="${TERMINAL}"
79
80 if command -v "$terminal" >/dev/null; then
81 echo "Terminal: ${TERMINAL}"
82 return
83 fi
84
85 echo "ERROR: Terminal '${TERMINAL}' is configured, but not installed"
86 exit 1
87 ;;
88 esac
89
90 echo "ERROR: Terminal '${TERMINAL}' is not in list of supported terminals ($programs)"
Oliver Smith4d7072c2018-09-28 11:30:46 +020091 exit 1
92}
93
Oliver Smith669f1db2022-01-17 15:59:24 +010094pidfiles_must_not_exist() {
95 local pidfile
96
97 for pidfile in "$@"; do
98 if [ -e "$pidfile" ]; then
99 echo
100 echo "ERROR: pidfile exists: $pidfile"
101 echo
102 kill_pids
103 exit 1
104 fi
105 done
106}
107
Neels Hofmeyr697a6172018-08-22 17:32:21 +0200108term() {
109 title="$2"
110 if [ -z "$title" ]; then
111 title="$(basename $@)"
112 fi
Oliver Smith669f1db2022-01-17 15:59:24 +0100113
114 local pidfile="$piddir/$title.pid"
115 local pidfile_term="$piddir/$title.term.pid"
116 pidfiles_must_not_exist "$pidfile" "$pidfile_term"
117
Oliver Smith63273712022-01-18 11:21:37 +0100118 local launcher="$launcherdir/$title.sh"
119
120 cat << EOF > "$launcher"
121#!/bin/sh
122
123export LD_LIBRARY_PATH='/usr/local/lib'
124
125$1 &
126echo \$! > $pidfile
127wait
128
129echo
130
131while true; do
132 echo 'q Enter to close'
Oliver Smithe33f85f2022-02-21 11:25:37 +0100133 read q_to_close < /dev/tty
Oliver Smith63273712022-01-18 11:21:37 +0100134 if [ "x\$q_to_close" = xq ]; then
135 break
136 fi
137done
138EOF
139 chmod +x "$launcher"
140
Oliver Smithcd472bd2022-01-17 15:59:31 +0100141 case "$terminal" in
142 tmux)
Oliver Smithf34a9442022-02-23 12:29:06 +0100143 tmux new-window -d -n "$title" "$launcher &; echo \$! > $pidfile_term; wait"
Oliver Smithcd472bd2022-01-17 15:59:31 +0100144 ;;
145 *)
Oliver Smith76f16472022-02-22 16:23:51 +0100146 sleep .2
Oliver Smithcd472bd2022-01-17 15:59:31 +0100147 $terminal \
148 -title "CN:$title" \
149 -e sh -c "$launcher" \
150 &
Oliver Smith669f1db2022-01-17 15:59:24 +0100151
Oliver Smithcd472bd2022-01-17 15:59:31 +0100152 echo "$!" > "$pidfile_term"
153 ;;
154 esac
Oliver Smith669f1db2022-01-17 15:59:24 +0100155}
156
157kill_pids() {
158 local pidfile
159 local pid
160
161 for pidfile in "$piddir"/*.pid; do
162 if ! [ -e "$pidfile" ]; then
163 continue
164 fi
165
166 pid="$(cat "$pidfile")"
167
168 echo "killing $(basename "$pidfile") ($pid)"
169 sudo kill "$pid"
170 rm "$pidfile"
171 done
Neels Hofmeyr697a6172018-08-22 17:32:21 +0200172}
173
Oliver Smith4d7072c2018-09-28 11:30:46 +0200174find_term
Oliver Smith669f1db2022-01-17 15:59:24 +0100175kill_pids
Oliver Smith4d7072c2018-09-28 11:30:46 +0200176
Neels Hofmeyr697a6172018-08-22 17:32:21 +0200177hnbgw="osmo-hnbgw"
178msc="gdb -ex run --args $(which osmo-msc)"
Neels Hofmeyr8e494842019-08-07 01:35:06 +0200179# To enable udtrace on osmo-msc MNCC socket, use this with adjusted /path/to/udtrace:
180# - LD_LIBRARY_PATH allows linking to titan if udtrace was compiled with titan support.
181# - LD_PRELOAD of libasan allows building osmo-msc with the sanitize.opts.
182# - the tee saves the stderr logging as well as the udtrace output to new file current_log/osmo-msc.out, since udtrace
183# will not show in osmo-msc.log
184#msc="LD_LIBRARY_PATH=/usr/lib/titan LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libasan.so.5:/path/to/udtrace/libudtrace.so osmo-msc 2>&1 | tee -a current_log/osmo-msc.out"
Neels Hofmeyr697a6172018-08-22 17:32:21 +0200185gbproxy="osmo-gbproxy"
186sgsn="osmo-sgsn"
187ggsn="osmo-ggsn"
188mgw4msc="osmo-mgw -c osmo-mgw-for-msc.cfg"
Oliver Smithe7658192022-02-21 15:31:20 +0100189#mgw4bsc="gdb -ex run --args osmo-mgw"
190#mgw4bsc="strace osmo-mgw"
191mgw4bsc="osmo-mgw"
Oliver Smith5467ef92018-12-13 13:27:37 +0100192hlr="LD_LIBRARY_PATH=/usr/local/lib gdb -ex run --args osmo-hlr --db-upgrade"
Oliver Smitha3a1a0d2021-12-14 16:27:10 +0100193stp4cn="osmo-stp -c osmo-stp-cn.cfg"
194stp4ran="osmo-stp -c osmo-stp-ran.cfg"
Oliver Smithe7658192022-02-21 15:31:20 +0100195bsc="LD_LIBRARY_PATH=/usr/local/lib gdb -ex run --args osmo-bsc"
Oliver Smitha3a1a0d2021-12-14 16:27:10 +0100196bscnat="osmo-bsc-nat"
Oliver Smith321a47c2022-02-21 16:25:37 +0100197bts="osmo-bts-virtual"
Oliver Smith67cd47f2022-02-23 15:25:32 +0100198virtphy="virtphy -D lo"
Oliver Smithd264ec62022-02-23 10:36:23 +0100199ms="mobile -c mobile.cfg"
Neels Hofmeyr697a6172018-08-22 17:32:21 +0200200
Neels Hofmeyrcbdd7182019-03-04 00:39:32 +0100201if [ "x${MSC_MNCC}" != "xinternal" ]; then
Oliver Smithd9f3d342018-09-26 16:29:00 +0200202 sipcon="osmo-sip-connector -c osmo-sip-connector.cfg"
Oliver Smithd9f3d342018-09-26 16:29:00 +0200203
Neels Hofmeyrba0a8282019-12-04 03:57:56 +0100204 case "${PBX_SERVER}" in
Oliver Smith667f19b2019-11-08 18:16:30 +0100205 "kamailio")
206 # Require kamailio (PATH hack is needed for Debian)
207 kamailio="$(PATH="$PATH:/usr/sbin:/sbin" which kamailio)"
208 if [ -z "$kamailio" ]; then
209 echo "ERROR: kamailio is not installed."
210 echo "After installing it, make sure that it does *not* run as daemon."
211 exit 1
212 fi
213 kamailio="$kamailio -f kamailio.cfg -D -e -E"
214 ;;
215 "freeswitch")
216 if [ -z "$(which freeswitch)" ]; then
217 echo "ERROR: freeswitch is not installed."
218 echo "Guide: https://freeswitch.org/confluence/display/FREESWITCH/Debian+10+Buster"
219 echo "After installing it, make sure that it does *not* run as daemon."
220 exit 1
221 fi
222 ;;
223 "none")
224 ;;
225 *)
Neels Hofmeyrba0a8282019-12-04 03:57:56 +0100226 echo "ERROR: unknown value ${PBX_SERVER} for SIPCON_SERVER!"
Oliver Smith667f19b2019-11-08 18:16:30 +0100227 exit 1
228 ;;
229 esac
Oliver Smithd9f3d342018-09-26 16:29:00 +0200230fi
231
Oliver Smith669f1db2022-01-17 15:59:24 +0100232PIDFILE_TCPDUMP_DEV="$piddir/tcpdump.$dev.pid"
233PIDFILE_TCPDUMP_LO="$piddir/tcpdump.lo.pid"
234pidfiles_must_not_exist "$PIDFILE_TCPDUMP_DEV" "$PIDFILE_TCPDUMP_LO"
235
Oliver Smithd9f3d342018-09-26 16:29:00 +0200236sudo tcpdump -i $dev -n -w current_log/$dev.single.pcap -U not port 22 &
Oliver Smith669f1db2022-01-17 15:59:24 +0100237echo "$!" > "$PIDFILE_TCPDUMP_DEV"
Oliver Smithd9f3d342018-09-26 16:29:00 +0200238sudo tcpdump -i lo -n -w current_log/lo.single.pcap -U not port 22 &
Oliver Smith669f1db2022-01-17 15:59:24 +0100239echo "$!" > "$PIDFILE_TCPDUMP_LO"
Oliver Smithd9f3d342018-09-26 16:29:00 +0200240
Oliver Smith669f1db2022-01-17 15:59:24 +0100241term "$ggsn" GGSN
Oliver Smith8fd50982021-12-14 18:12:54 +0100242
Oliver Smitha3a1a0d2021-12-14 16:27:10 +0100243if [ "${STP_CN_IP}" = "${STP_RAN_IP}" ]; then
Oliver Smith669f1db2022-01-17 15:59:24 +0100244 term "$stp4cn" STP
Oliver Smitha3a1a0d2021-12-14 16:27:10 +0100245else
Oliver Smith669f1db2022-01-17 15:59:24 +0100246 term "$stp4cn" STP4CN
Oliver Smith669f1db2022-01-17 15:59:24 +0100247 term "$stp4ran" STP4RAN
Oliver Smith669f1db2022-01-17 15:59:24 +0100248 term "$bscnat" BSCNAT
Oliver Smitha3a1a0d2021-12-14 16:27:10 +0100249fi
Oliver Smith8fd50982021-12-14 18:12:54 +0100250
Oliver Smith669f1db2022-01-17 15:59:24 +0100251term "$hlr" HLR
Oliver Smith669f1db2022-01-17 15:59:24 +0100252term "$sgsn" SGSN
Oliver Smith0d9bacc2022-02-23 12:43:37 +0100253
254if [ "${GBPROXY_RUN_IN_OSMO_DEV}" = 1 ]; then
255 term "$gbproxy" GBPROXY
256fi
257
Oliver Smith669f1db2022-01-17 15:59:24 +0100258term "$mgw4msc" MGW4MSC
Oliver Smith669f1db2022-01-17 15:59:24 +0100259term "$msc" MSC
Oliver Smith669f1db2022-01-17 15:59:24 +0100260term "$hnbgw" HNBGW
Oliver Smith8fd50982021-12-14 18:12:54 +0100261
Oliver Smithe7658192022-02-21 15:31:20 +0100262
263if [ "$BSC_COUNT" = 1 ]; then
264 term "$mgw4bsc -c osmo-mgw-for-bsc-0.cfg" MGW4BSC
Oliver Smithe7658192022-02-21 15:31:20 +0100265 term "$bsc -c osmo-bsc-0.cfg" BSC
266else
267 term "$mgw4bsc -c osmo-mgw-for-bsc-0.cfg" MGW4BSC0
Oliver Smithe7658192022-02-21 15:31:20 +0100268 term "$mgw4bsc -c osmo-mgw-for-bsc-1.cfg" MGW4BSC1
Oliver Smithe7658192022-02-21 15:31:20 +0100269 term "$bsc -c osmo-bsc-0.cfg" BSC0
Oliver Smithe7658192022-02-21 15:31:20 +0100270 term "$bsc -c osmo-bsc-1.cfg" BSC1
271fi
Neels Hofmeyr697a6172018-08-22 17:32:21 +0200272
Oliver Smith321a47c2022-02-21 16:25:37 +0100273${foreach(BTS)}
274if [ "${BTSn_RUN_IN_OSMO_DEV}" = 1 ]; then
275 term "$bts -c osmo-bts-${BTSn}.cfg" BTS${BTSn}
276fi
277${foreach_end}
278
Oliver Smithd264ec62022-02-23 10:36:23 +0100279if [ "${MS_RUN_IN_OSMO_DEV}" = 1 ]; then
280 term "$virtphy" VIRTPHY
281 term "$ms" MS
282fi
Oliver Smith321a47c2022-02-21 16:25:37 +0100283
Neels Hofmeyrcbdd7182019-03-04 00:39:32 +0100284if [ "x${MSC_MNCC}" != "xinternal" ]; then
Oliver Smith669f1db2022-01-17 15:59:24 +0100285 term "$sipcon" SIPCON
Oliver Smith8fd50982021-12-14 18:12:54 +0100286
Neels Hofmeyrba0a8282019-12-04 03:57:56 +0100287 case "${PBX_SERVER}" in
Oliver Smith8fd50982021-12-14 18:12:54 +0100288 "kamailio")
Oliver Smith669f1db2022-01-17 15:59:24 +0100289 term "$kamailio" KAMAILIO
Oliver Smith8fd50982021-12-14 18:12:54 +0100290 ;;
291 "freeswitch")
Oliver Smith669f1db2022-01-17 15:59:24 +0100292 term "./freeswitch/freeswitch.sh" FREESWITCH
Oliver Smith8fd50982021-12-14 18:12:54 +0100293 ;;
Oliver Smith667f19b2019-11-08 18:16:30 +0100294 esac
Oliver Smithd9f3d342018-09-26 16:29:00 +0200295fi
296
Neels Hofmeyr697a6172018-08-22 17:32:21 +0200297#ssh bts rm /tmp/bts.log /tmp/pcu.log
298#ssh bts neels/run_remote.sh &
299
300echo enter to close
301read enter_to_close
302echo Closing...
303
Neels Hofmeyr697a6172018-08-22 17:32:21 +0200304#ssh bts neels/stop_remote.sh
305
Oliver Smith669f1db2022-01-17 15:59:24 +0100306kill_pids
Neels Hofmeyrfecf1562019-08-07 01:34:12 +0200307
Neels Hofmeyr697a6172018-08-22 17:32:21 +0200308set +e
309cp *.cfg "$logdir"/
310
311echo
312echo enter name to save log
313read log_name
314if [ -n "$log_name" ]; then
315 newlogdir="log/$log_name"
316 #scp "bts:/tmp/{bts,pcu}.log" "bts:neels/osmo-{bts,pcu}.cfg" "$logdir"
317else
Oliver Smith2cfd7922022-02-17 10:08:14 +0100318 log_name="log_$(date +%Y-%m-%d_%H-%M-%S)"
319 newlogdir="autolog/$log_name"
Neels Hofmeyr697a6172018-08-22 17:32:21 +0200320fi
321mkdir -p "$(dirname "$newlogdir")"
322
323mergecap -w "$logdir/trace.pcap" "$logdir/"*.single.pcap && rm -f "$logdir/"*.single.pcap
Oliver Smith2cfd7922022-02-17 10:08:14 +0100324mv "$logdir/trace.pcap" "$logdir/trace-$log_name.pcap"
Neels Hofmeyr697a6172018-08-22 17:32:21 +0200325
326if [ -x "$newlogdir" ]; then
327 echo "already exists, move it manually: $newlogdir"
328else
329 echo mv "$logdir" "$newlogdir"
330 mv "$logdir" "$newlogdir"
331 mkdir -p "$logdir"
332 logdir="$newlogdir"
333fi
334rm lastlog
335ln -s "$logdir" lastlog