blob: a4bc0827942c722001c4bba38014a60d63159adc [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 Pedrol0d455042018-08-27 17:07:41 +020023from abc import ABCMeta, abstractmethod
Pau Espin Pedrol9a4631c2018-03-28 19:17:34 +020024from . import log, config, util, template, process, pcu_osmo, bts_osmo
25from .event_loop import MainLoop
Neels Hofmeyr3531a192017-03-28 14:30:28 +020026
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +010027class OsmoBtsTrx(bts_osmo.OsmoBtsMainUnit):
Pau Espin Pedrol2e542952017-12-14 19:08:51 +010028##############
29# PROTECTED
30##############
Neels Hofmeyr3531a192017-03-28 14:30:28 +020031
Neels Hofmeyr3531a192017-03-28 14:30:28 +020032 BIN_BTS_TRX = 'osmo-bts-trx'
33 BIN_PCU = 'osmo-pcu'
34
Neels Hofmeyr17c139e2017-04-12 02:42:02 +020035 CONF_BTS_TRX = 'osmo-bts-trx.cfg'
36
Neels Hofmeyr3531a192017-03-28 14:30:28 +020037 def __init__(self, suite_run, conf):
Pau Espin Pedrole5194622018-05-07 13:36:58 +020038 super().__init__(suite_run, conf, OsmoBtsTrx.BIN_BTS_TRX, 'osmo_bts_trx')
Pau Espin Pedrol58603672018-08-09 13:45:55 +020039 self.run_dir = None
40 self.inst = None
41 self.trx = None
Neels Hofmeyr3531a192017-03-28 14:30:28 +020042 self.env = {}
Pau Espin Pedrol0d455042018-08-27 17:07:41 +020043 self.gen_conf = {}
Pau Espin Pedrol404e1502017-08-22 11:17:43 +020044
45 def trx_remote_ip(self):
Pau Espin Pedrol0d455042018-08-27 17:07:41 +020046 conf_ip = self.conf.get('osmo_trx', {}).get('trx_ip', None)
Pau Espin Pedrol404e1502017-08-22 11:17:43 +020047 if conf_ip is not None:
48 return conf_ip
49 # if 'trx_remote_ip' is not configured, use same IP as BTS
50 return self.remote_addr()
51
Pau Espin Pedrolb1526b92018-05-22 20:32:30 +020052 def launch_process(self, keepalive, binary_name, *args):
Neels Hofmeyr3531a192017-03-28 14:30:28 +020053 binary = os.path.abspath(self.inst.child('bin', binary_name))
54 run_dir = self.run_dir.new_dir(binary_name)
55 if not os.path.isfile(binary):
56 raise RuntimeError('Binary missing: %r' % binary)
57 proc = process.Process(binary_name, run_dir,
58 (binary,) + args,
59 env=self.env)
Pau Espin Pedrolb1526b92018-05-22 20:32:30 +020060 self.suite_run.remember_to_stop(proc, keepalive)
Neels Hofmeyr3531a192017-03-28 14:30:28 +020061 proc.launch()
Neels Hofmeyrfd39f3e2017-05-07 02:02:35 +020062 return proc
Neels Hofmeyr3531a192017-03-28 14:30:28 +020063
64 def configure(self):
Neels Hofmeyrc4ba4a62017-05-18 19:31:44 +020065 if self.bsc is None:
66 raise RuntimeError('BTS needs to be added to a BSC or NITB before it can be configured')
Neels Hofmeyr17c139e2017-04-12 02:42:02 +020067 self.config_file = self.run_dir.new_file(OsmoBtsTrx.CONF_BTS_TRX)
Neels Hofmeyr3531a192017-03-28 14:30:28 +020068 self.dbg(config_file=self.config_file)
69
70 values = dict(osmo_bts_trx=config.get_defaults('osmo_bts_trx'))
Pau Espin Pedrol0d455042018-08-27 17:07:41 +020071 config.overlay(values, dict(osmo_bts_trx=dict(osmo_trx=config.get_defaults('osmo_trx'))))
Neels Hofmeyr3531a192017-03-28 14:30:28 +020072 config.overlay(values, self.suite_run.config())
Pau Espin Pedrol329b6f42017-08-08 13:55:24 +020073 config.overlay(values, {
74 'osmo_bts_trx': {
75 'oml_remote_ip': self.bsc.addr(),
Pau Espin Pedrol15aae982017-09-08 13:55:54 +020076 'pcu_socket_path': self.pcu_socket_path(),
Pau Espin Pedrol0d455042018-08-27 17:07:41 +020077 'osmo_trx': {
78 'bts_ip': self.remote_addr(),
79 'trx_ip': self.trx_remote_ip(),
Pau Espin Pedrol44b7ea12018-09-17 20:24:13 +020080 'channels': [{} for trx_i in range(self.num_trx())]
Pau Espin Pedrol0d455042018-08-27 17:07:41 +020081 }
Pau Espin Pedrol329b6f42017-08-08 13:55:24 +020082 }
83 })
84 config.overlay(values, { 'osmo_bts_trx': self.conf })
Neels Hofmeyr943c81d2017-05-22 20:16:03 +020085
Pau Espin Pedrol0d455042018-08-27 17:07:41 +020086 self.gen_conf = values
Neels Hofmeyr943c81d2017-05-22 20:16:03 +020087 self.dbg('OSMO-BTS-TRX CONFIG:\n' + pprint.pformat(values))
Neels Hofmeyr3531a192017-03-28 14:30:28 +020088
89 with open(self.config_file, 'w') as f:
Neels Hofmeyr17c139e2017-04-12 02:42:02 +020090 r = template.render(OsmoBtsTrx.CONF_BTS_TRX, values)
Neels Hofmeyr3531a192017-03-28 14:30:28 +020091 self.dbg(r)
92 f.write(r)
93
Pau Espin Pedrol0d455042018-08-27 17:07:41 +020094 def launch_trx_enabled(self):
95 return util.str2bool(self.gen_conf['osmo_bts_trx'].get('osmo_trx', {}).get('launch_trx'))
96
97 def get_osmo_trx_type(self):
98 return self.gen_conf['osmo_bts_trx'].get('osmo_trx', {}).get('type')
99
Pau Espin Pedrol2e542952017-12-14 19:08:51 +0100100########################
101# PUBLIC - INTERNAL API
102########################
Neels Hofmeyrc4ba4a62017-05-18 19:31:44 +0200103 def conf_for_bsc(self):
Pau Espin Pedrole5194622018-05-07 13:36:58 +0200104 values = self.conf_for_bsc_prepare()
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200105 self.dbg(conf=values)
106 return values
107
Pau Espin Pedrol0d455042018-08-27 17:07:41 +0200108 def conf_for_osmotrx(self):
109 return dict(osmo_trx=self.gen_conf['osmo_bts_trx'].get('osmo_trx', {}))
110
Pau Espin Pedrol2e542952017-12-14 19:08:51 +0100111###################
112# PUBLIC (test API included)
113###################
Pau Espin Pedrolb1526b92018-05-22 20:32:30 +0200114 def start(self, keepalive=False):
Pau Espin Pedrol2e542952017-12-14 19:08:51 +0100115 if self.bsc is None:
116 raise RuntimeError('BTS needs to be added to a BSC or NITB before it can be started')
117 self.suite_run.poll()
118
119 self.log('Starting to connect to', self.bsc)
120 self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name()))
121 self.configure()
122
123 if self.launch_trx_enabled():
Pau Espin Pedrol0d455042018-08-27 17:07:41 +0200124 self.trx = OsmoTrx.get_instance_by_type(self.get_osmo_trx_type(), self.suite_run, self.conf_for_osmotrx())
Pau Espin Pedrolb1526b92018-05-22 20:32:30 +0200125 self.trx.start(keepalive)
Pau Espin Pedrol0d455042018-08-27 17:07:41 +0200126 self.log('Waiting for %s to start up...' % self.trx.name())
Pau Espin Pedrol9a4631c2018-03-28 19:17:34 +0200127 MainLoop.wait(self, self.trx.trx_ready)
Pau Espin Pedrol2e542952017-12-14 19:08:51 +0100128
129 self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-bts')))
130 lib = self.inst.child('lib')
131 if not os.path.isdir(lib):
132 raise RuntimeError('No lib/ in %r' % self.inst)
133 self.env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
134
Pau Espin Pedrolb1526b92018-05-22 20:32:30 +0200135 self.proc_bts = self.launch_process(keepalive, OsmoBtsTrx.BIN_BTS_TRX, '-r', '1',
Pau Espin Pedrol2e542952017-12-14 19:08:51 +0100136 '-c', os.path.abspath(self.config_file),
Pau Espin Pedrol44b7ea12018-09-17 20:24:13 +0200137 '-i', self.bsc.addr(),
138 '-t', str(self.num_trx()))
Pau Espin Pedrol2e542952017-12-14 19:08:51 +0100139 self.suite_run.poll()
140
Pau Espin Pedrol0d455042018-08-27 17:07:41 +0200141class OsmoTrx(log.Origin, metaclass=ABCMeta):
Pau Espin Pedrol47738532017-08-15 16:20:07 +0200142
Pau Espin Pedroledcc8d22018-03-04 19:58:23 +0100143 CONF_OSMO_TRX = 'osmo-trx.cfg'
Pau Espin Pedrol47738532017-08-15 16:20:07 +0200144
Pau Espin Pedrol0d455042018-08-27 17:07:41 +0200145##############
146# PROTECTED
147##############
148 def __init__(self, suite_run, conf):
149 super().__init__(log.C_RUN, self.binary_name())
Pau Espin Pedrol47738532017-08-15 16:20:07 +0200150 self.suite_run = suite_run
Pau Espin Pedroledcc8d22018-03-04 19:58:23 +0100151 self.conf = conf
Pau Espin Pedrol47738532017-08-15 16:20:07 +0200152 self.env = {}
Pau Espin Pedrol0d455042018-08-27 17:07:41 +0200153 self.listen_ip = conf.get('trx_ip')
154 self.bts_ip = conf.get('bts_ip')
155 self.run_dir = None
156 self.inst = None
157 self.proc_trx = None
158
159 @classmethod
160 def get_instance_by_type(cls, type, suite_run, conf):
161 KNOWN_OSMOTRX_TYPES = {
162 'uhd': OsmoTrxUHD,
163 }
164 osmo_trx_class = KNOWN_OSMOTRX_TYPES.get(type)
165 return osmo_trx_class(suite_run, conf)
166
167 @abstractmethod
168 def binary_name(self):
169 'Used by base class. Subclass can create different OsmoTRX implementations.'
170 pass
Pau Espin Pedrol47738532017-08-15 16:20:07 +0200171
Pau Espin Pedroledcc8d22018-03-04 19:58:23 +0100172 def configure(self):
173 self.config_file = self.run_dir.new_file(OsmoTrx.CONF_OSMO_TRX)
174 self.dbg(config_file=self.config_file)
175
Pau Espin Pedrol0d455042018-08-27 17:07:41 +0200176 values = self.conf
Pau Espin Pedroledcc8d22018-03-04 19:58:23 +0100177
178 self.dbg('OSMO-TRX CONFIG:\n' + pprint.pformat(values))
179
180 with open(self.config_file, 'w') as f:
181 r = template.render(OsmoTrx.CONF_OSMO_TRX, values)
182 self.dbg(r)
183 f.write(r)
184
Pau Espin Pedrolb1526b92018-05-22 20:32:30 +0200185 def launch_process(self, keepalive, binary_name, *args):
Pau Espin Pedrol47738532017-08-15 16:20:07 +0200186 binary = os.path.abspath(self.inst.child('bin', binary_name))
187 run_dir = self.run_dir.new_dir(binary_name)
188 if not os.path.isfile(binary):
189 raise RuntimeError('Binary missing: %r' % binary)
190 proc = process.Process(binary_name, run_dir,
191 (binary,) + args,
192 env=self.env)
Pau Espin Pedrolb1526b92018-05-22 20:32:30 +0200193 self.suite_run.remember_to_stop(proc, keepalive)
Pau Espin Pedrol47738532017-08-15 16:20:07 +0200194 proc.launch()
195 return proc
196
Pau Espin Pedrol0d455042018-08-27 17:07:41 +0200197##############
198# PUBLIC (test API included)
199##############
200 def start(self, keepalive=False):
201 self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name()))
202 self.configure()
203 self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-trx')))
204 lib = self.inst.child('lib')
205 self.env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
206 self.proc_trx = self.launch_process(keepalive, self.binary_name(),
207 '-C', os.path.abspath(self.config_file))
208
Pau Espin Pedrol47738532017-08-15 16:20:07 +0200209 def trx_ready(self):
210 if not self.proc_trx or not self.proc_trx.is_running:
211 return False
212 return '-- Transceiver active with' in (self.proc_trx.get_stdout() or '')
Pau Espin Pedrol0d455042018-08-27 17:07:41 +0200213
214class OsmoTrxUHD(OsmoTrx):
215 BIN_TRX = 'osmo-trx-uhd'
216
217 def __init__(self, suite_run, conf):
218 super().__init__(suite_run, conf)
219
220 def binary_name(self):
221 return OsmoTrxUHD.BIN_TRX
222
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200223# vim: expandtab tabstop=4 shiftwidth=4