blob: cda7fc0b4b80ba608093af52b7e69d74278a84c5 [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"
Neels Hofmeyr697a6172018-08-22 17:32:21 +0200186
Neels Hofmeyrcbdd7182019-03-04 00:39:32 +0100187if [ "x${MSC_MNCC}" != "xinternal" ]; then
Oliver Smithd9f3d342018-09-26 16:29:00 +0200188 sipcon="osmo-sip-connector -c osmo-sip-connector.cfg"
Oliver Smithd9f3d342018-09-26 16:29:00 +0200189
Neels Hofmeyrba0a8282019-12-04 03:57:56 +0100190 case "${PBX_SERVER}" in
Oliver Smith667f19b2019-11-08 18:16:30 +0100191 "kamailio")
192 # Require kamailio (PATH hack is needed for Debian)
193 kamailio="$(PATH="$PATH:/usr/sbin:/sbin" which kamailio)"
194 if [ -z "$kamailio" ]; then
195 echo "ERROR: kamailio is not installed."
196 echo "After installing it, make sure that it does *not* run as daemon."
197 exit 1
198 fi
199 kamailio="$kamailio -f kamailio.cfg -D -e -E"
200 ;;
201 "freeswitch")
202 if [ -z "$(which freeswitch)" ]; then
203 echo "ERROR: freeswitch is not installed."
204 echo "Guide: https://freeswitch.org/confluence/display/FREESWITCH/Debian+10+Buster"
205 echo "After installing it, make sure that it does *not* run as daemon."
206 exit 1
207 fi
208 ;;
209 "none")
210 ;;
211 *)
Neels Hofmeyrba0a8282019-12-04 03:57:56 +0100212 echo "ERROR: unknown value ${PBX_SERVER} for SIPCON_SERVER!"
Oliver Smith667f19b2019-11-08 18:16:30 +0100213 exit 1
214 ;;
215 esac
Oliver Smithd9f3d342018-09-26 16:29:00 +0200216fi
217
Oliver Smith669f1db2022-01-17 15:59:24 +0100218PIDFILE_TCPDUMP_DEV="$piddir/tcpdump.$dev.pid"
219PIDFILE_TCPDUMP_LO="$piddir/tcpdump.lo.pid"
220pidfiles_must_not_exist "$PIDFILE_TCPDUMP_DEV" "$PIDFILE_TCPDUMP_LO"
221
Oliver Smithd9f3d342018-09-26 16:29:00 +0200222sudo tcpdump -i $dev -n -w current_log/$dev.single.pcap -U not port 22 &
Oliver Smith669f1db2022-01-17 15:59:24 +0100223echo "$!" > "$PIDFILE_TCPDUMP_DEV"
Oliver Smithd9f3d342018-09-26 16:29:00 +0200224sudo tcpdump -i lo -n -w current_log/lo.single.pcap -U not port 22 &
Oliver Smith669f1db2022-01-17 15:59:24 +0100225echo "$!" > "$PIDFILE_TCPDUMP_LO"
Oliver Smithd9f3d342018-09-26 16:29:00 +0200226
Oliver Smith669f1db2022-01-17 15:59:24 +0100227term "$ggsn" GGSN
Oliver Smith8fd50982021-12-14 18:12:54 +0100228
Oliver Smitha3a1a0d2021-12-14 16:27:10 +0100229if [ "${STP_CN_IP}" = "${STP_RAN_IP}" ]; then
Oliver Smith669f1db2022-01-17 15:59:24 +0100230 term "$stp4cn" STP
Oliver Smitha3a1a0d2021-12-14 16:27:10 +0100231else
Oliver Smith669f1db2022-01-17 15:59:24 +0100232 term "$stp4cn" STP4CN
Oliver Smith669f1db2022-01-17 15:59:24 +0100233 term "$stp4ran" STP4RAN
Oliver Smith669f1db2022-01-17 15:59:24 +0100234 term "$bscnat" BSCNAT
Oliver Smitha3a1a0d2021-12-14 16:27:10 +0100235fi
Oliver Smith8fd50982021-12-14 18:12:54 +0100236
Oliver Smith669f1db2022-01-17 15:59:24 +0100237term "$hlr" HLR
Oliver Smith669f1db2022-01-17 15:59:24 +0100238term "$sgsn" SGSN
Oliver Smith669f1db2022-01-17 15:59:24 +0100239term "$gbproxy" GBPROXY
Oliver Smith669f1db2022-01-17 15:59:24 +0100240term "$mgw4msc" MGW4MSC
Oliver Smith669f1db2022-01-17 15:59:24 +0100241term "$msc" MSC
Oliver Smith669f1db2022-01-17 15:59:24 +0100242term "$hnbgw" HNBGW
Oliver Smith8fd50982021-12-14 18:12:54 +0100243
Oliver Smithe7658192022-02-21 15:31:20 +0100244
245if [ "$BSC_COUNT" = 1 ]; then
246 term "$mgw4bsc -c osmo-mgw-for-bsc-0.cfg" MGW4BSC
Oliver Smithe7658192022-02-21 15:31:20 +0100247 term "$bsc -c osmo-bsc-0.cfg" BSC
248else
249 term "$mgw4bsc -c osmo-mgw-for-bsc-0.cfg" MGW4BSC0
Oliver Smithe7658192022-02-21 15:31:20 +0100250 term "$mgw4bsc -c osmo-mgw-for-bsc-1.cfg" MGW4BSC1
Oliver Smithe7658192022-02-21 15:31:20 +0100251 term "$bsc -c osmo-bsc-0.cfg" BSC0
Oliver Smithe7658192022-02-21 15:31:20 +0100252 term "$bsc -c osmo-bsc-1.cfg" BSC1
253fi
Neels Hofmeyr697a6172018-08-22 17:32:21 +0200254
Oliver Smith321a47c2022-02-21 16:25:37 +0100255${foreach(BTS)}
256if [ "${BTSn_RUN_IN_OSMO_DEV}" = 1 ]; then
257 term "$bts -c osmo-bts-${BTSn}.cfg" BTS${BTSn}
258fi
259${foreach_end}
260
261
Neels Hofmeyrcbdd7182019-03-04 00:39:32 +0100262if [ "x${MSC_MNCC}" != "xinternal" ]; then
Oliver Smith669f1db2022-01-17 15:59:24 +0100263 term "$sipcon" SIPCON
Oliver Smith8fd50982021-12-14 18:12:54 +0100264
Neels Hofmeyrba0a8282019-12-04 03:57:56 +0100265 case "${PBX_SERVER}" in
Oliver Smith8fd50982021-12-14 18:12:54 +0100266 "kamailio")
Oliver Smith669f1db2022-01-17 15:59:24 +0100267 term "$kamailio" KAMAILIO
Oliver Smith8fd50982021-12-14 18:12:54 +0100268 ;;
269 "freeswitch")
Oliver Smith669f1db2022-01-17 15:59:24 +0100270 term "./freeswitch/freeswitch.sh" FREESWITCH
Oliver Smith8fd50982021-12-14 18:12:54 +0100271 ;;
Oliver Smith667f19b2019-11-08 18:16:30 +0100272 esac
Oliver Smithd9f3d342018-09-26 16:29:00 +0200273fi
274
Neels Hofmeyr697a6172018-08-22 17:32:21 +0200275#ssh bts rm /tmp/bts.log /tmp/pcu.log
276#ssh bts neels/run_remote.sh &
277
278echo enter to close
279read enter_to_close
280echo Closing...
281
Neels Hofmeyr697a6172018-08-22 17:32:21 +0200282#ssh bts neels/stop_remote.sh
283
Oliver Smith669f1db2022-01-17 15:59:24 +0100284kill_pids
Neels Hofmeyrfecf1562019-08-07 01:34:12 +0200285
Neels Hofmeyr697a6172018-08-22 17:32:21 +0200286set +e
287cp *.cfg "$logdir"/
288
289echo
290echo enter name to save log
291read log_name
292if [ -n "$log_name" ]; then
293 newlogdir="log/$log_name"
294 #scp "bts:/tmp/{bts,pcu}.log" "bts:neels/osmo-{bts,pcu}.cfg" "$logdir"
295else
Oliver Smith2cfd7922022-02-17 10:08:14 +0100296 log_name="log_$(date +%Y-%m-%d_%H-%M-%S)"
297 newlogdir="autolog/$log_name"
Neels Hofmeyr697a6172018-08-22 17:32:21 +0200298fi
299mkdir -p "$(dirname "$newlogdir")"
300
301mergecap -w "$logdir/trace.pcap" "$logdir/"*.single.pcap && rm -f "$logdir/"*.single.pcap
Oliver Smith2cfd7922022-02-17 10:08:14 +0100302mv "$logdir/trace.pcap" "$logdir/trace-$log_name.pcap"
Neels Hofmeyr697a6172018-08-22 17:32:21 +0200303
304if [ -x "$newlogdir" ]; then
305 echo "already exists, move it manually: $newlogdir"
306else
307 echo mv "$logdir" "$newlogdir"
308 mv "$logdir" "$newlogdir"
309 mkdir -p "$logdir"
310 logdir="$newlogdir"
311fi
312rm lastlog
313ln -s "$logdir" lastlog