blob: fb13545ab82d465cbbbeffba66063a30a51d2178 [file] [log] [blame]
Neels Hofmeyr17c139e2017-04-12 02:42:02 +02001# osmo_gsm_tester: specifics for running an osmo-bts-trx
Neels Hofmeyr3531a192017-03-28 14:30:28 +02002#
3# Copyright (C) 2016-2017 by sysmocom - s.f.m.c. GmbH
4#
5# Author: Neels Hofmeyr <neels@hofmeyr.de>
6#
7# This program is free software: you can redistribute it and/or modify
Harald Welte27205342017-06-03 09:51:45 +02008# it under the terms of the GNU General Public License as
Neels Hofmeyr3531a192017-03-28 14:30:28 +02009# published by the Free Software Foundation, either version 3 of the
10# License, or (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte27205342017-06-03 09:51:45 +020015# GNU General Public License for more details.
Neels Hofmeyr3531a192017-03-28 14:30:28 +020016#
Harald Welte27205342017-06-03 09:51:45 +020017# You should have received a copy of the GNU General Public License
Neels Hofmeyr3531a192017-03-28 14:30:28 +020018# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20import os
Neels Hofmeyr943c81d2017-05-22 20:16:03 +020021import pprint
Pau Espin Pedrol15aae982017-09-08 13:55:54 +020022import tempfile
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +010023from . import log, config, util, template, process, event_loop, pcu_osmo, bts_osmo
Neels Hofmeyr3531a192017-03-28 14:30:28 +020024
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +010025class OsmoBtsTrx(bts_osmo.OsmoBtsMainUnit):
Neels Hofmeyr3531a192017-03-28 14:30:28 +020026 run_dir = None
Neels Hofmeyr3531a192017-03-28 14:30:28 +020027 inst = None
28 env = None
Pau Espin Pedrol47738532017-08-15 16:20:07 +020029 trx = None
Neels Hofmeyr3531a192017-03-28 14:30:28 +020030
Neels Hofmeyr3531a192017-03-28 14:30:28 +020031 BIN_BTS_TRX = 'osmo-bts-trx'
32 BIN_PCU = 'osmo-pcu'
33
Neels Hofmeyr17c139e2017-04-12 02:42:02 +020034 CONF_BTS_TRX = 'osmo-bts-trx.cfg'
35
Neels Hofmeyr3531a192017-03-28 14:30:28 +020036 def __init__(self, suite_run, conf):
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +010037 super().__init__(suite_run, conf, OsmoBtsTrx.BIN_BTS_TRX)
Neels Hofmeyr3531a192017-03-28 14:30:28 +020038 self.env = {}
Pau Espin Pedrol404e1502017-08-22 11:17:43 +020039
40 def trx_remote_ip(self):
41 conf_ip = self.conf.get('trx_remote_ip', None)
42 if conf_ip is not None:
43 return conf_ip
44 # if 'trx_remote_ip' is not configured, use same IP as BTS
45 return self.remote_addr()
46
47 def launch_trx_enabled(self):
48 return util.str2bool(self.conf.get('launch_trx'))
Neels Hofmeyrd41dae92017-05-22 19:58:41 +020049
Neels Hofmeyr3531a192017-03-28 14:30:28 +020050 def start(self):
Neels Hofmeyrc4ba4a62017-05-18 19:31:44 +020051 if self.bsc is None:
52 raise RuntimeError('BTS needs to be added to a BSC or NITB before it can be started')
Neels Hofmeyr3531a192017-03-28 14:30:28 +020053 self.suite_run.poll()
54
Neels Hofmeyrc4ba4a62017-05-18 19:31:44 +020055 self.log('Starting to connect to', self.bsc)
Pau Espin Pedrold0912332017-06-14 13:27:08 +020056 self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name()))
Neels Hofmeyr3531a192017-03-28 14:30:28 +020057 self.configure()
58
Pau Espin Pedrol404e1502017-08-22 11:17:43 +020059 if self.launch_trx_enabled():
60 self.trx = OsmoTrx(self.suite_run, self.trx_remote_ip(), self.remote_addr())
61 self.trx.start()
62 self.log('Waiting for osmo-trx to start up...')
63 event_loop.wait(self, self.trx.trx_ready)
Pau Espin Pedrol47738532017-08-15 16:20:07 +020064
Pau Espin Pedrolcfac9932017-09-13 18:05:13 +020065 self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-bts')))
Your Name3c6673a2017-04-08 18:52:39 +020066 lib = self.inst.child('lib')
67 if not os.path.isdir(lib):
68 raise RuntimeError('No lib/ in %r' % self.inst)
Pau Espin Pedrole39c6f12017-05-08 16:21:38 +020069 self.env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
Neels Hofmeyr3531a192017-03-28 14:30:28 +020070
Pau Espin Pedrold0a01112017-11-28 12:15:10 +010071 self.proc_bts = self.launch_process(OsmoBtsTrx.BIN_BTS_TRX, '-r', '1',
Pau Espin Pedrol6ab98982017-05-12 16:07:35 +020072 '-c', os.path.abspath(self.config_file),
Neels Hofmeyrc4ba4a62017-05-18 19:31:44 +020073 '-i', self.bsc.addr())
Neels Hofmeyr3531a192017-03-28 14:30:28 +020074 self.suite_run.poll()
75
76 def launch_process(self, binary_name, *args):
Neels Hofmeyr3531a192017-03-28 14:30:28 +020077 binary = os.path.abspath(self.inst.child('bin', binary_name))
78 run_dir = self.run_dir.new_dir(binary_name)
79 if not os.path.isfile(binary):
80 raise RuntimeError('Binary missing: %r' % binary)
81 proc = process.Process(binary_name, run_dir,
82 (binary,) + args,
83 env=self.env)
Neels Hofmeyr3531a192017-03-28 14:30:28 +020084 self.suite_run.remember_to_stop(proc)
85 proc.launch()
Neels Hofmeyrfd39f3e2017-05-07 02:02:35 +020086 return proc
Neels Hofmeyr3531a192017-03-28 14:30:28 +020087
88 def configure(self):
Neels Hofmeyrc4ba4a62017-05-18 19:31:44 +020089 if self.bsc is None:
90 raise RuntimeError('BTS needs to be added to a BSC or NITB before it can be configured')
Neels Hofmeyr17c139e2017-04-12 02:42:02 +020091 self.config_file = self.run_dir.new_file(OsmoBtsTrx.CONF_BTS_TRX)
Neels Hofmeyr3531a192017-03-28 14:30:28 +020092 self.dbg(config_file=self.config_file)
93
94 values = dict(osmo_bts_trx=config.get_defaults('osmo_bts_trx'))
95 config.overlay(values, self.suite_run.config())
Pau Espin Pedrol329b6f42017-08-08 13:55:24 +020096 config.overlay(values, {
97 'osmo_bts_trx': {
98 'oml_remote_ip': self.bsc.addr(),
Pau Espin Pedrol404e1502017-08-22 11:17:43 +020099 'trx_local_ip': self.remote_addr(),
100 'trx_remote_ip': self.trx_remote_ip(),
Pau Espin Pedrol15aae982017-09-08 13:55:54 +0200101 'pcu_socket_path': self.pcu_socket_path(),
Pau Espin Pedrol329b6f42017-08-08 13:55:24 +0200102 }
103 })
104 config.overlay(values, { 'osmo_bts_trx': self.conf })
Neels Hofmeyr943c81d2017-05-22 20:16:03 +0200105
106 self.dbg('OSMO-BTS-TRX CONFIG:\n' + pprint.pformat(values))
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200107
108 with open(self.config_file, 'w') as f:
Neels Hofmeyr17c139e2017-04-12 02:42:02 +0200109 r = template.render(OsmoBtsTrx.CONF_BTS_TRX, values)
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200110 self.dbg(r)
111 f.write(r)
112
Neels Hofmeyrc4ba4a62017-05-18 19:31:44 +0200113 def conf_for_bsc(self):
114 values = config.get_defaults('bsc_bts')
Neels Hofmeyr17c139e2017-04-12 02:42:02 +0200115 config.overlay(values, config.get_defaults('osmo_bts_trx'))
Pau Espin Pedrol5e0c2512017-11-06 18:40:23 +0100116 if self.lac is not None:
117 config.overlay(values, { 'location_area_code': self.lac })
Pau Espin Pedrol8a3a7b52017-11-28 15:50:02 +0100118 if self.rac is not None:
119 config.overlay(values, { 'routing_area_code': self.rac })
Pau Espin Pedrol4ccce7c2017-11-07 11:13:20 +0100120 if self.cellid is not None:
121 config.overlay(values, { 'cell_identity': self.cellid })
Pau Espin Pedrol8a3a7b52017-11-28 15:50:02 +0100122 if self.bvci is not None:
123 config.overlay(values, { 'bvci': self.bvci })
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200124 config.overlay(values, self.conf)
Pau Espin Pedrol5936d8f2017-11-23 13:34:27 +0100125
126 sgsn_conf = {} if self.sgsn is None else self.sgsn.conf_for_client()
127 config.overlay(values, sgsn_conf)
128
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200129 self.dbg(conf=values)
130 return values
131
Pau Espin Pedrol47738532017-08-15 16:20:07 +0200132class OsmoTrx(log.Origin):
133 suite_run = None
134 run_dir = None
135 inst = None
136 env = None
137 proc_trx = None
138
139 BIN_TRX = 'osmo-trx'
140
Pau Espin Pedrol404e1502017-08-22 11:17:43 +0200141 def __init__(self, suite_run, listen_ip, bts_ip):
Pau Espin Pedrol47738532017-08-15 16:20:07 +0200142 super().__init__(log.C_RUN, OsmoTrx.BIN_TRX)
143 self.suite_run = suite_run
144 self.env = {}
Pau Espin Pedrol404e1502017-08-22 11:17:43 +0200145 self.listen_ip = listen_ip
146 self.bts_ip = bts_ip
Pau Espin Pedrol47738532017-08-15 16:20:07 +0200147
148 def start(self):
149 self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name()))
150 self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst(OsmoTrx.BIN_TRX)))
Pau Espin Pedrol80a5c772017-08-21 19:03:08 +0200151 lib = self.inst.child('lib')
152 self.env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
Pau Espin Pedrol404e1502017-08-22 11:17:43 +0200153 self.proc_trx = self.launch_process(OsmoTrx.BIN_TRX, '-x', '-j', self.listen_ip, '-i', self.bts_ip)
Pau Espin Pedrol47738532017-08-15 16:20:07 +0200154
155 def launch_process(self, binary_name, *args):
156 binary = os.path.abspath(self.inst.child('bin', binary_name))
157 run_dir = self.run_dir.new_dir(binary_name)
158 if not os.path.isfile(binary):
159 raise RuntimeError('Binary missing: %r' % binary)
160 proc = process.Process(binary_name, run_dir,
161 (binary,) + args,
162 env=self.env)
163 self.suite_run.remember_to_stop(proc)
164 proc.launch()
165 return proc
166
167 def trx_ready(self):
168 if not self.proc_trx or not self.proc_trx.is_running:
169 return False
170 return '-- Transceiver active with' in (self.proc_trx.get_stdout() or '')
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200171# vim: expandtab tabstop=4 shiftwidth=4