blob: 22ff4b631eec332b2dc895e03fb461945bceefe6 [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 Pedrol0d455042018-08-27 17:07:41 +020022from abc import ABCMeta, abstractmethod
Pau Espin Pedrol2eafb032020-01-29 17:56:14 +010023from . import log, config, util, template, process, remote, bts_osmo
Pau Espin Pedrol55cee162019-09-10 13:47:04 +020024from . import powersupply
Pau Espin Pedrol9a4631c2018-03-28 19:17:34 +020025from .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
Pau Espin Pedrol55cee162019-09-10 13:47:04 +020042 self.pwsup_list = []
Neels Hofmeyr3531a192017-03-28 14:30:28 +020043 self.env = {}
Pau Espin Pedrol0d455042018-08-27 17:07:41 +020044 self.gen_conf = {}
Pau Espin Pedrol404e1502017-08-22 11:17:43 +020045
46 def trx_remote_ip(self):
Pau Espin Pedrol0d455042018-08-27 17:07:41 +020047 conf_ip = self.conf.get('osmo_trx', {}).get('trx_ip', None)
Pau Espin Pedrol404e1502017-08-22 11:17:43 +020048 if conf_ip is not None:
49 return conf_ip
50 # if 'trx_remote_ip' is not configured, use same IP as BTS
51 return self.remote_addr()
52
Pau Espin Pedrolb1526b92018-05-22 20:32:30 +020053 def launch_process(self, keepalive, binary_name, *args):
Neels Hofmeyr3531a192017-03-28 14:30:28 +020054 binary = os.path.abspath(self.inst.child('bin', binary_name))
55 run_dir = self.run_dir.new_dir(binary_name)
56 if not os.path.isfile(binary):
57 raise RuntimeError('Binary missing: %r' % binary)
58 proc = process.Process(binary_name, run_dir,
59 (binary,) + args,
60 env=self.env)
Pau Espin Pedrolb1526b92018-05-22 20:32:30 +020061 self.suite_run.remember_to_stop(proc, keepalive)
Neels Hofmeyr3531a192017-03-28 14:30:28 +020062 proc.launch()
Neels Hofmeyrfd39f3e2017-05-07 02:02:35 +020063 return proc
Neels Hofmeyr3531a192017-03-28 14:30:28 +020064
65 def configure(self):
Neels Hofmeyrc4ba4a62017-05-18 19:31:44 +020066 if self.bsc is None:
67 raise RuntimeError('BTS needs to be added to a BSC or NITB before it can be configured')
Neels Hofmeyr17c139e2017-04-12 02:42:02 +020068 self.config_file = self.run_dir.new_file(OsmoBtsTrx.CONF_BTS_TRX)
Neels Hofmeyr3531a192017-03-28 14:30:28 +020069 self.dbg(config_file=self.config_file)
70
71 values = dict(osmo_bts_trx=config.get_defaults('osmo_bts_trx'))
Pau Espin Pedrol0d455042018-08-27 17:07:41 +020072 config.overlay(values, dict(osmo_bts_trx=dict(osmo_trx=config.get_defaults('osmo_trx'))))
Neels Hofmeyr3531a192017-03-28 14:30:28 +020073 config.overlay(values, self.suite_run.config())
Pau Espin Pedrol329b6f42017-08-08 13:55:24 +020074 config.overlay(values, {
75 'osmo_bts_trx': {
76 'oml_remote_ip': self.bsc.addr(),
Pau Espin Pedrol15aae982017-09-08 13:55:54 +020077 'pcu_socket_path': self.pcu_socket_path(),
Pau Espin Pedrol0d455042018-08-27 17:07:41 +020078 'osmo_trx': {
79 'bts_ip': self.remote_addr(),
80 'trx_ip': self.trx_remote_ip(),
Pau Espin Pedrol29b71322020-04-06 18:14:29 +020081 'egprs': 'enable' if self.egprs_enabled() else 'disable',
Pau Espin Pedrol44b7ea12018-09-17 20:24:13 +020082 'channels': [{} for trx_i in range(self.num_trx())]
Pau Espin Pedrol0d455042018-08-27 17:07:41 +020083 }
Pau Espin Pedrol329b6f42017-08-08 13:55:24 +020084 }
85 })
86 config.overlay(values, { 'osmo_bts_trx': self.conf })
Neels Hofmeyr943c81d2017-05-22 20:16:03 +020087
Pau Espin Pedrol0d455042018-08-27 17:07:41 +020088 self.gen_conf = values
Neels Hofmeyr943c81d2017-05-22 20:16:03 +020089 self.dbg('OSMO-BTS-TRX CONFIG:\n' + pprint.pformat(values))
Neels Hofmeyr3531a192017-03-28 14:30:28 +020090
91 with open(self.config_file, 'w') as f:
Neels Hofmeyr17c139e2017-04-12 02:42:02 +020092 r = template.render(OsmoBtsTrx.CONF_BTS_TRX, values)
Neels Hofmeyr3531a192017-03-28 14:30:28 +020093 self.dbg(r)
94 f.write(r)
95
Pau Espin Pedrol55cee162019-09-10 13:47:04 +020096 self.pwsup_list = [None] * self.num_trx()
97 # Construct trx_list appending with empty dicts if needed:
98 conf_trx_list = self.conf.get('trx_list', [])
99 conf_trx_list = conf_trx_list + [{}] * (self.num_trx() - len(conf_trx_list))
100 for trx_i in range(self.num_trx()):
101 pwsup_opt = conf_trx_list[trx_i].get('power_supply', {})
102 if not pwsup_opt:
103 self.dbg('no power_supply configured for TRX %d' % trx_i)
104 continue
105 pwsup_type = pwsup_opt.get('type')
106 if not pwsup_type:
107 raise log.Error('No type attribute provided in power_supply conf for TRX %d!' % trx_i)
108 self.pwsup_list[trx_i] = powersupply.get_instance_by_type(pwsup_type, pwsup_opt)
109
Pau Espin Pedrol0d455042018-08-27 17:07:41 +0200110 def launch_trx_enabled(self):
111 return util.str2bool(self.gen_conf['osmo_bts_trx'].get('osmo_trx', {}).get('launch_trx'))
112
113 def get_osmo_trx_type(self):
114 return self.gen_conf['osmo_bts_trx'].get('osmo_trx', {}).get('type')
115
Pau Espin Pedrol2e542952017-12-14 19:08:51 +0100116########################
117# PUBLIC - INTERNAL API
118########################
Neels Hofmeyrc4ba4a62017-05-18 19:31:44 +0200119 def conf_for_bsc(self):
Pau Espin Pedrole5194622018-05-07 13:36:58 +0200120 values = self.conf_for_bsc_prepare()
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200121 self.dbg(conf=values)
122 return values
123
Pau Espin Pedrol0d455042018-08-27 17:07:41 +0200124 def conf_for_osmotrx(self):
125 return dict(osmo_trx=self.gen_conf['osmo_bts_trx'].get('osmo_trx', {}))
126
Pau Espin Pedrol55cee162019-09-10 13:47:04 +0200127 def cleanup(self):
128 i = 0
129 for pwsup in self.pwsup_list:
130 if pwsup:
131 self.dbg('Powering off TRX %d' % i)
132 pwsup.power_set(False)
133 i = i + 1
134 self.pwsup_list = []
135
Pau Espin Pedrol2e542952017-12-14 19:08:51 +0100136###################
137# PUBLIC (test API included)
138###################
Pau Espin Pedrolb1526b92018-05-22 20:32:30 +0200139 def start(self, keepalive=False):
Pau Espin Pedrol2e542952017-12-14 19:08:51 +0100140 if self.bsc is None:
141 raise RuntimeError('BTS needs to be added to a BSC or NITB before it can be started')
142 self.suite_run.poll()
143
144 self.log('Starting to connect to', self.bsc)
145 self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name()))
146 self.configure()
147
Pau Espin Pedrol55cee162019-09-10 13:47:04 +0200148 # Power cycle all TRX if needed (right now only TRX0 for SC5):
149 i = 0
150 for pwsup in self.pwsup_list:
151 if pwsup:
152 self.dbg('Powering cycling TRX %d' % i)
153 pwsup.power_cycle(1.0)
154 i = i + 1
155
Pau Espin Pedrol2e542952017-12-14 19:08:51 +0100156 if self.launch_trx_enabled():
Pau Espin Pedrol0d455042018-08-27 17:07:41 +0200157 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 +0200158 self.trx.start(keepalive)
Pau Espin Pedrol0d455042018-08-27 17:07:41 +0200159 self.log('Waiting for %s to start up...' % self.trx.name())
Pau Espin Pedrol9a4631c2018-03-28 19:17:34 +0200160 MainLoop.wait(self, self.trx.trx_ready)
Pau Espin Pedrol2e542952017-12-14 19:08:51 +0100161
162 self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-bts')))
163 lib = self.inst.child('lib')
164 if not os.path.isdir(lib):
165 raise RuntimeError('No lib/ in %r' % self.inst)
166 self.env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
167
Pau Espin Pedrolb1526b92018-05-22 20:32:30 +0200168 self.proc_bts = self.launch_process(keepalive, OsmoBtsTrx.BIN_BTS_TRX, '-r', '1',
Pau Espin Pedrol2e542952017-12-14 19:08:51 +0100169 '-c', os.path.abspath(self.config_file),
Pau Espin Pedrolc93707f2019-08-05 18:19:45 +0200170 '-i', self.bsc.addr())
Pau Espin Pedrol2e542952017-12-14 19:08:51 +0100171 self.suite_run.poll()
172
Pau Espin Pedrol55cee162019-09-10 13:47:04 +0200173
174################################################################################
175# TRX
176################################################################################
177
178class Trx(log.Origin, metaclass=ABCMeta):
179##############
180# PROTECTED
181##############
182 def __init__(self, suite_run, conf, name):
183 super().__init__(log.C_RUN, name)
184 self.suite_run = suite_run
185 self.conf = conf
186 self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name()))
187 self.listen_ip = conf.get('osmo_trx', {}).get('trx_ip')
188 self.remote_user = conf.get('osmo_trx', {}).get('remote_user', None)
189
190 @classmethod
191 def get_instance_by_type(cls, type, suite_run, conf):
192 KNOWN_OSMOTRX_TYPES = {
193 'uhd': OsmoTrxUHD,
194 'lms': OsmoTrxLMS,
195 'sc5': TrxSC5
196 }
197 osmo_trx_class = KNOWN_OSMOTRX_TYPES.get(type)
198 return osmo_trx_class(suite_run, conf)
199
200##############
201# PUBLIC (test API included)
202##############
203 @abstractmethod
204 def start(self, keepalive=False):
205 pass
206
207 @abstractmethod
208 def trx_ready(self):
209 pass
210
211class OsmoTrx(Trx, metaclass=ABCMeta):
Pau Espin Pedrol47738532017-08-15 16:20:07 +0200212
Pau Espin Pedroledcc8d22018-03-04 19:58:23 +0100213 CONF_OSMO_TRX = 'osmo-trx.cfg'
Pau Espin Pedrola9006df2018-10-01 12:26:39 +0200214 REMOTE_DIR = '/osmo-gsm-tester-trx/last_run'
Pau Espin Pedrol302c7562018-10-02 13:08:02 +0200215 WRAPPER_SCRIPT = 'ssh_sigkiller.sh'
Pau Espin Pedrol47738532017-08-15 16:20:07 +0200216
Pau Espin Pedrol0d455042018-08-27 17:07:41 +0200217##############
218# PROTECTED
219##############
220 def __init__(self, suite_run, conf):
Pau Espin Pedrol55cee162019-09-10 13:47:04 +0200221 super().__init__(suite_run, conf, self.binary_name())
Pau Espin Pedrol47738532017-08-15 16:20:07 +0200222 self.env = {}
Pau Espin Pedrola9006df2018-10-01 12:26:39 +0200223 self.log("OSMOTRX CONF: %r" % conf)
Pau Espin Pedrola9006df2018-10-01 12:26:39 +0200224 self.bts_ip = conf.get('osmo_trx', {}).get('bts_ip')
Pau Espin Pedrol0d455042018-08-27 17:07:41 +0200225 self.inst = None
226 self.proc_trx = None
227
Pau Espin Pedrol0d455042018-08-27 17:07:41 +0200228 @abstractmethod
229 def binary_name(self):
230 'Used by base class. Subclass can create different OsmoTRX implementations.'
231 pass
Pau Espin Pedrol47738532017-08-15 16:20:07 +0200232
Pau Espin Pedroledcc8d22018-03-04 19:58:23 +0100233 def configure(self):
234 self.config_file = self.run_dir.new_file(OsmoTrx.CONF_OSMO_TRX)
235 self.dbg(config_file=self.config_file)
236
Pau Espin Pedrol0d455042018-08-27 17:07:41 +0200237 values = self.conf
Pau Espin Pedrol1ff03762018-10-02 15:50:48 +0200238
239 # we don't need to enable multi-arfcn for single channel
240 if len(values.get('osmo_trx', {}).get('channels', [])) > 1:
241 multi_arfcn_bool = util.str2bool(values.get('osmo_trx', {}).get('multi_arfcn', False))
242 else:
243 multi_arfcn_bool = False
Pau Espin Pedrol94eab262018-09-17 20:25:55 +0200244 config.overlay(values, { 'osmo_trx': { 'multi_arfcn': multi_arfcn_bool } })
Pau Espin Pedroledcc8d22018-03-04 19:58:23 +0100245
246 self.dbg('OSMO-TRX CONFIG:\n' + pprint.pformat(values))
247
248 with open(self.config_file, 'w') as f:
249 r = template.render(OsmoTrx.CONF_OSMO_TRX, values)
250 self.dbg(r)
251 f.write(r)
252
Pau Espin Pedrola9006df2018-10-01 12:26:39 +0200253 def launch_process_local(self, keepalive, binary_name, *args):
Pau Espin Pedrol47738532017-08-15 16:20:07 +0200254 binary = os.path.abspath(self.inst.child('bin', binary_name))
255 run_dir = self.run_dir.new_dir(binary_name)
256 if not os.path.isfile(binary):
257 raise RuntimeError('Binary missing: %r' % binary)
258 proc = process.Process(binary_name, run_dir,
259 (binary,) + args,
260 env=self.env)
Pau Espin Pedrolb1526b92018-05-22 20:32:30 +0200261 self.suite_run.remember_to_stop(proc, keepalive)
Pau Espin Pedrol47738532017-08-15 16:20:07 +0200262 proc.launch()
263 return proc
264
Pau Espin Pedroldd710222019-11-26 14:33:09 +0100265 def start_remotely(self, keepalive):
266 # Run remotely through ssh. We need to run osmo-trx under a wrapper
267 # script since osmo-trx ignores SIGHUP and will keep running after
268 # we close local ssh session. The wrapper script catches SIGHUP and
269 # sends SIGINT to it.
Pau Espin Pedrol2eafb032020-01-29 17:56:14 +0100270
271 rem_host = remote.RemoteHost(self.run_dir, self.remote_user, self.listen_ip)
272
273 remote_prefix_dir = util.Dir(OsmoTrx.REMOTE_DIR)
274 remote_run_dir = util.Dir(remote_prefix_dir.child(self.binary_name()))
Pau Espin Pedroldd710222019-11-26 14:33:09 +0100275 remote_config_file = remote_run_dir.child(OsmoTrx.CONF_OSMO_TRX)
276
Pau Espin Pedrol2eafb032020-01-29 17:56:14 +0100277 have_inst = rem_host.inst_compatible_for_remote()
Pau Espin Pedroldd710222019-11-26 14:33:09 +0100278 if have_inst:
279 self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-trx')))
280
Pau Espin Pedrol2eafb032020-01-29 17:56:14 +0100281 rem_host.recreate_remote_dir(remote_prefix_dir)
282 if have_inst:
283 self.remote_inst = util.Dir(remote_prefix_dir.child(os.path.basename(str(self.inst))))
284 rem_host.create_remote_dir(self.remote_inst)
285 rem_host.scp('scp-inst-to-remote', str(self.inst), remote_prefix_dir)
286 rem_host.create_remote_dir(remote_run_dir)
287 rem_host.scp('scp-cfg-to-remote', self.config_file, remote_config_file)
Pau Espin Pedroldd710222019-11-26 14:33:09 +0100288
289 if have_inst:
Pau Espin Pedrolf6d45ad2020-02-11 14:39:15 +0100290 remote_env = { 'LD_LIBRARY_PATH': self.remote_inst.child('lib') }
Pau Espin Pedroldd710222019-11-26 14:33:09 +0100291 remote_binary = self.remote_inst.child('bin', self.binary_name())
Pau Espin Pedrol2eafb032020-01-29 17:56:14 +0100292 args = (remote_binary, '-C', remote_config_file)
Pau Espin Pedroldd710222019-11-26 14:33:09 +0100293 else: # Use whatever is available i nremote system PATH:
Pau Espin Pedrolf6d45ad2020-02-11 14:39:15 +0100294 remote_env = {}
Pau Espin Pedrol2eafb032020-01-29 17:56:14 +0100295 remote_binary = self.binary_name()
296 args = (remote_binary, '-C', remote_config_file)
Pau Espin Pedrolf6d45ad2020-02-11 14:39:15 +0100297 self.proc_trx = rem_host.RemoteProcessFixIgnoreSIGHUP(self.binary_name(), remote_run_dir, args, remote_env=remote_env)
Pau Espin Pedrol2eafb032020-01-29 17:56:14 +0100298 self.suite_run.remember_to_stop(self.proc_trx, keepalive)
299 self.proc_trx.launch()
Pau Espin Pedroldd710222019-11-26 14:33:09 +0100300
Pau Espin Pedrol0d455042018-08-27 17:07:41 +0200301##############
302# PUBLIC (test API included)
303##############
304 def start(self, keepalive=False):
Pau Espin Pedrol0d455042018-08-27 17:07:41 +0200305 self.configure()
Pau Espin Pedroldd710222019-11-26 14:33:09 +0100306 if self.remote_user:
307 self.start_remotely(keepalive)
308 return
309 # Run locally if ssh user is not set
Pau Espin Pedrol0d455042018-08-27 17:07:41 +0200310 self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-trx')))
Pau Espin Pedroldd710222019-11-26 14:33:09 +0100311 lib = self.inst.child('lib')
312 self.env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
313 self.proc_trx = self.launch_process_local(keepalive, self.binary_name(),
314 '-C', os.path.abspath(self.config_file))
Pau Espin Pedrol0d455042018-08-27 17:07:41 +0200315
Pau Espin Pedrol47738532017-08-15 16:20:07 +0200316 def trx_ready(self):
317 if not self.proc_trx or not self.proc_trx.is_running:
318 return False
319 return '-- Transceiver active with' in (self.proc_trx.get_stdout() or '')
Pau Espin Pedrol0d455042018-08-27 17:07:41 +0200320
321class OsmoTrxUHD(OsmoTrx):
322 BIN_TRX = 'osmo-trx-uhd'
323
324 def __init__(self, suite_run, conf):
325 super().__init__(suite_run, conf)
326
327 def binary_name(self):
328 return OsmoTrxUHD.BIN_TRX
329
Pau Espin Pedrole789f0c2018-08-27 18:45:01 +0200330class OsmoTrxLMS(OsmoTrx):
331 BIN_TRX = 'osmo-trx-lms'
332
333 def __init__(self, suite_run, conf):
334 super().__init__(suite_run, conf)
Pau Espin Pedrole789f0c2018-08-27 18:45:01 +0200335
336 def binary_name(self):
337 return OsmoTrxLMS.BIN_TRX
338
Pau Espin Pedrol55cee162019-09-10 13:47:04 +0200339class TrxSC5(Trx):
340
341 def __init__(self, suite_run, conf):
342 super().__init__(suite_run, conf, "sc5-trx")
343 self.ready = False
344
345 def start(self, keepalive=False):
346 name = "ssh_sc5_ccli"
347 run_dir = self.run_dir.new_dir(name)
348 popen_args = ('/cx/bin/ccli', '-c', 'gsm.unlock')
349 proc = process.RemoteProcess(name, run_dir, self.remote_user, self.listen_ip, None,
350 popen_args)
351 keep_trying = 10
352 while keep_trying > 0:
353 if proc.respawn_sync(raise_nonsuccess=False) == 0 and 'OK' in (proc.get_stdout() or ''):
354 break
355 keep_trying = keep_trying - 1
356 self.log('Configuring SC5 TRX failed, retrying %d more times' % keep_trying)
357 MainLoop.sleep(self, 5)
358 if keep_trying == 0:
359 raise log.Error('Failed configuring SC5!')
360 self.ready = True
361
362 def trx_ready(self):
363 return self.ready
364
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200365# vim: expandtab tabstop=4 shiftwidth=4