blob: e1cf226ef6f89f398f915e7baf86228a790caf73 [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 Smith4186b952021-07-07 16:40:36 +020024if ! sudo iptables -t nat -C POSTROUTING -s ${GGSN_NET} -o $dev -j MASQUERADE 2>/dev/null; then
25 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
49logdir="current_log"
Oliver Smith669f1db2022-01-17 15:59:24 +010050piddir="run/pids"
Oliver Smith63273712022-01-18 11:21:37 +010051launcherdir="run/launchers"
52rm -rf "$launcherdir"
53mkdir -p "$logdir" "$piddir" "$launcherdir"
Neels Hofmeyr697a6172018-08-22 17:32:21 +020054
Oliver Smith4d7072c2018-09-28 11:30:46 +020055find_term() {
56 # Find a terminal program and write to the global "terminal" variable
Oliver Smithcd472bd2022-01-17 15:59:31 +010057 local programs="urxvt xterm tmux"
Oliver Smith4d7072c2018-09-28 11:30:46 +020058
Oliver Smithb28a9102022-01-17 15:59:02 +010059 if [ -z "${TERMINAL}" ]; then
60 echo "ERROR: TERMINAL is not defined in your osmo-dev net config file. Please add it."
61 exit 1
62 fi
63
64 case " $programs " in
65 *" ${TERMINAL} "*)
66 terminal="${TERMINAL}"
67
68 if command -v "$terminal" >/dev/null; then
69 echo "Terminal: ${TERMINAL}"
70 return
71 fi
72
73 echo "ERROR: Terminal '${TERMINAL}' is configured, but not installed"
74 exit 1
75 ;;
76 esac
77
78 echo "ERROR: Terminal '${TERMINAL}' is not in list of supported terminals ($programs)"
Oliver Smith4d7072c2018-09-28 11:30:46 +020079 exit 1
80}
81
Oliver Smith669f1db2022-01-17 15:59:24 +010082pidfiles_must_not_exist() {
83 local pidfile
84
85 for pidfile in "$@"; do
86 if [ -e "$pidfile" ]; then
87 echo
88 echo "ERROR: pidfile exists: $pidfile"
89 echo
90 kill_pids
91 exit 1
92 fi
93 done
94}
95
Neels Hofmeyr697a6172018-08-22 17:32:21 +020096term() {
97 title="$2"
98 if [ -z "$title" ]; then
99 title="$(basename $@)"
100 fi
Oliver Smith669f1db2022-01-17 15:59:24 +0100101
102 local pidfile="$piddir/$title.pid"
103 local pidfile_term="$piddir/$title.term.pid"
104 pidfiles_must_not_exist "$pidfile" "$pidfile_term"
105
Oliver Smith63273712022-01-18 11:21:37 +0100106 local launcher="$launcherdir/$title.sh"
107
108 cat << EOF > "$launcher"
109#!/bin/sh
110
111export LD_LIBRARY_PATH='/usr/local/lib'
112
113$1 &
114echo \$! > $pidfile
115wait
116
117echo
118
119while true; do
120 echo 'q Enter to close'
Oliver Smithe33f85f2022-02-21 11:25:37 +0100121 read q_to_close < /dev/tty
Oliver Smith63273712022-01-18 11:21:37 +0100122 if [ "x\$q_to_close" = xq ]; then
123 break
124 fi
125done
126EOF
127 chmod +x "$launcher"
128
Oliver Smithcd472bd2022-01-17 15:59:31 +0100129 case "$terminal" in
130 tmux)
Oliver Smithf34a9442022-02-23 12:29:06 +0100131 tmux new-window -d -n "$title" "$launcher &; echo \$! > $pidfile_term; wait"
Oliver Smithcd472bd2022-01-17 15:59:31 +0100132 ;;
133 *)
Oliver Smith76f16472022-02-22 16:23:51 +0100134 sleep .2
Oliver Smithcd472bd2022-01-17 15:59:31 +0100135 $terminal \
136 -title "CN:$title" \
137 -e sh -c "$launcher" \
138 &
Oliver Smith669f1db2022-01-17 15:59:24 +0100139
Oliver Smithcd472bd2022-01-17 15:59:31 +0100140 echo "$!" > "$pidfile_term"
141 ;;
142 esac
Oliver Smith669f1db2022-01-17 15:59:24 +0100143}
144
145kill_pids() {
146 local pidfile
147 local pid
148
149 for pidfile in "$piddir"/*.pid; do
150 if ! [ -e "$pidfile" ]; then
151 continue
152 fi
153
154 pid="$(cat "$pidfile")"
155
156 echo "killing $(basename "$pidfile") ($pid)"
157 sudo kill "$pid"
158 rm "$pidfile"
159 done
Neels Hofmeyr697a6172018-08-22 17:32:21 +0200160}
161
Oliver Smith4d7072c2018-09-28 11:30:46 +0200162find_term
Oliver Smith669f1db2022-01-17 15:59:24 +0100163kill_pids
Oliver Smith4d7072c2018-09-28 11:30:46 +0200164
Neels Hofmeyr697a6172018-08-22 17:32:21 +0200165hnbgw="osmo-hnbgw"
166msc="gdb -ex run --args $(which osmo-msc)"
Neels Hofmeyr8e494842019-08-07 01:35:06 +0200167# To enable udtrace on osmo-msc MNCC socket, use this with adjusted /path/to/udtrace:
168# - LD_LIBRARY_PATH allows linking to titan if udtrace was compiled with titan support.
169# - LD_PRELOAD of libasan allows building osmo-msc with the sanitize.opts.
170# - the tee saves the stderr logging as well as the udtrace output to new file current_log/osmo-msc.out, since udtrace
171# will not show in osmo-msc.log
172#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 +0200173gbproxy="osmo-gbproxy"
174sgsn="osmo-sgsn"
175ggsn="osmo-ggsn"
176mgw4msc="osmo-mgw -c osmo-mgw-for-msc.cfg"
Oliver Smithe7658192022-02-21 15:31:20 +0100177#mgw4bsc="gdb -ex run --args osmo-mgw"
178#mgw4bsc="strace osmo-mgw"
179mgw4bsc="osmo-mgw"
Oliver Smith5467ef92018-12-13 13:27:37 +0100180hlr="LD_LIBRARY_PATH=/usr/local/lib gdb -ex run --args osmo-hlr --db-upgrade"
Oliver Smitha3a1a0d2021-12-14 16:27:10 +0100181stp4cn="osmo-stp -c osmo-stp-cn.cfg"
182stp4ran="osmo-stp -c osmo-stp-ran.cfg"
Oliver Smithe7658192022-02-21 15:31:20 +0100183bsc="LD_LIBRARY_PATH=/usr/local/lib gdb -ex run --args osmo-bsc"
Oliver Smitha3a1a0d2021-12-14 16:27:10 +0100184bscnat="osmo-bsc-nat"
Oliver Smith321a47c2022-02-21 16:25:37 +0100185bts="osmo-bts-virtual"
Oliver Smithd264ec62022-02-23 10:36:23 +0100186virtphy="virtphy"
187ms="mobile -c mobile.cfg"
Neels Hofmeyr697a6172018-08-22 17:32:21 +0200188
Neels Hofmeyrcbdd7182019-03-04 00:39:32 +0100189if [ "x${MSC_MNCC}" != "xinternal" ]; then
Oliver Smithd9f3d342018-09-26 16:29:00 +0200190 sipcon="osmo-sip-connector -c osmo-sip-connector.cfg"
Oliver Smithd9f3d342018-09-26 16:29:00 +0200191
Neels Hofmeyrba0a8282019-12-04 03:57:56 +0100192 case "${PBX_SERVER}" in
Oliver Smith667f19b2019-11-08 18:16:30 +0100193 "kamailio")
194 # Require kamailio (PATH hack is needed for Debian)
195 kamailio="$(PATH="$PATH:/usr/sbin:/sbin" which kamailio)"
196 if [ -z "$kamailio" ]; then
197 echo "ERROR: kamailio is not installed."
198 echo "After installing it, make sure that it does *not* run as daemon."
199 exit 1
200 fi
201 kamailio="$kamailio -f kamailio.cfg -D -e -E"
202 ;;
203 "freeswitch")
204 if [ -z "$(which freeswitch)" ]; then
205 echo "ERROR: freeswitch is not installed."
206 echo "Guide: https://freeswitch.org/confluence/display/FREESWITCH/Debian+10+Buster"
207 echo "After installing it, make sure that it does *not* run as daemon."
208 exit 1
209 fi
210 ;;
211 "none")
212 ;;
213 *)
Neels Hofmeyrba0a8282019-12-04 03:57:56 +0100214 echo "ERROR: unknown value ${PBX_SERVER} for SIPCON_SERVER!"
Oliver Smith667f19b2019-11-08 18:16:30 +0100215 exit 1
216 ;;
217 esac
Oliver Smithd9f3d342018-09-26 16:29:00 +0200218fi
219
Oliver Smith669f1db2022-01-17 15:59:24 +0100220PIDFILE_TCPDUMP_DEV="$piddir/tcpdump.$dev.pid"
221PIDFILE_TCPDUMP_LO="$piddir/tcpdump.lo.pid"
222pidfiles_must_not_exist "$PIDFILE_TCPDUMP_DEV" "$PIDFILE_TCPDUMP_LO"
223
Oliver Smithd9f3d342018-09-26 16:29:00 +0200224sudo tcpdump -i $dev -n -w current_log/$dev.single.pcap -U not port 22 &
Oliver Smith669f1db2022-01-17 15:59:24 +0100225echo "$!" > "$PIDFILE_TCPDUMP_DEV"
Oliver Smithd9f3d342018-09-26 16:29:00 +0200226sudo tcpdump -i lo -n -w current_log/lo.single.pcap -U not port 22 &
Oliver Smith669f1db2022-01-17 15:59:24 +0100227echo "$!" > "$PIDFILE_TCPDUMP_LO"
Oliver Smithd9f3d342018-09-26 16:29:00 +0200228
Oliver Smith669f1db2022-01-17 15:59:24 +0100229term "$ggsn" GGSN
Oliver Smith8fd50982021-12-14 18:12:54 +0100230
Oliver Smitha3a1a0d2021-12-14 16:27:10 +0100231if [ "${STP_CN_IP}" = "${STP_RAN_IP}" ]; then
Oliver Smith669f1db2022-01-17 15:59:24 +0100232 term "$stp4cn" STP
Oliver Smitha3a1a0d2021-12-14 16:27:10 +0100233else
Oliver Smith669f1db2022-01-17 15:59:24 +0100234 term "$stp4cn" STP4CN
Oliver Smith669f1db2022-01-17 15:59:24 +0100235 term "$stp4ran" STP4RAN
Oliver Smith669f1db2022-01-17 15:59:24 +0100236 term "$bscnat" BSCNAT
Oliver Smitha3a1a0d2021-12-14 16:27:10 +0100237fi
Oliver Smith8fd50982021-12-14 18:12:54 +0100238
Oliver Smith669f1db2022-01-17 15:59:24 +0100239term "$hlr" HLR
Oliver Smith669f1db2022-01-17 15:59:24 +0100240term "$sgsn" SGSN
Oliver Smith669f1db2022-01-17 15:59:24 +0100241term "$gbproxy" GBPROXY
Oliver Smith669f1db2022-01-17 15:59:24 +0100242term "$mgw4msc" MGW4MSC
Oliver Smith669f1db2022-01-17 15:59:24 +0100243term "$msc" MSC
Oliver Smith669f1db2022-01-17 15:59:24 +0100244term "$hnbgw" HNBGW
Oliver Smith8fd50982021-12-14 18:12:54 +0100245
Oliver Smithe7658192022-02-21 15:31:20 +0100246
247if [ "$BSC_COUNT" = 1 ]; then
248 term "$mgw4bsc -c osmo-mgw-for-bsc-0.cfg" MGW4BSC
Oliver Smithe7658192022-02-21 15:31:20 +0100249 term "$bsc -c osmo-bsc-0.cfg" BSC
250else
251 term "$mgw4bsc -c osmo-mgw-for-bsc-0.cfg" MGW4BSC0
Oliver Smithe7658192022-02-21 15:31:20 +0100252 term "$mgw4bsc -c osmo-mgw-for-bsc-1.cfg" MGW4BSC1
Oliver Smithe7658192022-02-21 15:31:20 +0100253 term "$bsc -c osmo-bsc-0.cfg" BSC0
Oliver Smithe7658192022-02-21 15:31:20 +0100254 term "$bsc -c osmo-bsc-1.cfg" BSC1
255fi
Neels Hofmeyr697a6172018-08-22 17:32:21 +0200256
Oliver Smith321a47c2022-02-21 16:25:37 +0100257${foreach(BTS)}
258if [ "${BTSn_RUN_IN_OSMO_DEV}" = 1 ]; then
259 term "$bts -c osmo-bts-${BTSn}.cfg" BTS${BTSn}
260fi
261${foreach_end}
262
Oliver Smithd264ec62022-02-23 10:36:23 +0100263if [ "${MS_RUN_IN_OSMO_DEV}" = 1 ]; then
264 term "$virtphy" VIRTPHY
265 term "$ms" MS
266fi
Oliver Smith321a47c2022-02-21 16:25:37 +0100267
Neels Hofmeyrcbdd7182019-03-04 00:39:32 +0100268if [ "x${MSC_MNCC}" != "xinternal" ]; then
Oliver Smith669f1db2022-01-17 15:59:24 +0100269 term "$sipcon" SIPCON
Oliver Smith8fd50982021-12-14 18:12:54 +0100270
Neels Hofmeyrba0a8282019-12-04 03:57:56 +0100271 case "${PBX_SERVER}" in
Oliver Smith8fd50982021-12-14 18:12:54 +0100272 "kamailio")
Oliver Smith669f1db2022-01-17 15:59:24 +0100273 term "$kamailio" KAMAILIO
Oliver Smith8fd50982021-12-14 18:12:54 +0100274 ;;
275 "freeswitch")
Oliver Smith669f1db2022-01-17 15:59:24 +0100276 term "./freeswitch/freeswitch.sh" FREESWITCH
Oliver Smith8fd50982021-12-14 18:12:54 +0100277 ;;
Oliver Smith667f19b2019-11-08 18:16:30 +0100278 esac
Oliver Smithd9f3d342018-09-26 16:29:00 +0200279fi
280
Neels Hofmeyr697a6172018-08-22 17:32:21 +0200281#ssh bts rm /tmp/bts.log /tmp/pcu.log
282#ssh bts neels/run_remote.sh &
283
284echo enter to close
285read enter_to_close
286echo Closing...
287
Neels Hofmeyr697a6172018-08-22 17:32:21 +0200288#ssh bts neels/stop_remote.sh
289
Oliver Smith669f1db2022-01-17 15:59:24 +0100290kill_pids
Neels Hofmeyrfecf1562019-08-07 01:34:12 +0200291
Neels Hofmeyr697a6172018-08-22 17:32:21 +0200292set +e
293cp *.cfg "$logdir"/
294
295echo
296echo enter name to save log
297read log_name
298if [ -n "$log_name" ]; then
299 newlogdir="log/$log_name"
300 #scp "bts:/tmp/{bts,pcu}.log" "bts:neels/osmo-{bts,pcu}.cfg" "$logdir"
301else
Oliver Smith2cfd7922022-02-17 10:08:14 +0100302 log_name="log_$(date +%Y-%m-%d_%H-%M-%S)"
303 newlogdir="autolog/$log_name"
Neels Hofmeyr697a6172018-08-22 17:32:21 +0200304fi
305mkdir -p "$(dirname "$newlogdir")"
306
307mergecap -w "$logdir/trace.pcap" "$logdir/"*.single.pcap && rm -f "$logdir/"*.single.pcap
Oliver Smith2cfd7922022-02-17 10:08:14 +0100308mv "$logdir/trace.pcap" "$logdir/trace-$log_name.pcap"
Neels Hofmeyr697a6172018-08-22 17:32:21 +0200309
310if [ -x "$newlogdir" ]; then
311 echo "already exists, move it manually: $newlogdir"
312else
313 echo mv "$logdir" "$newlogdir"
314 mv "$logdir" "$newlogdir"
315 mkdir -p "$logdir"
316 logdir="$newlogdir"
317fi
318rm lastlog
319ln -s "$logdir" lastlog