Neels Hofmeyr | 17c139e | 2017-04-12 02:42:02 +0200 | [diff] [blame] | 1 | # osmo_gsm_tester: specifics for running an osmo-bts-trx |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 2 | # |
| 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 Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 8 | # it under the terms of the GNU General Public License as |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 9 | # 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 Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 15 | # GNU General Public License for more details. |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 16 | # |
Harald Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 17 | # You should have received a copy of the GNU General Public License |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | import os |
Neels Hofmeyr | 943c81d | 2017-05-22 20:16:03 +0200 | [diff] [blame] | 21 | import pprint |
Pau Espin Pedrol | 15aae98 | 2017-09-08 13:55:54 +0200 | [diff] [blame] | 22 | import tempfile |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 23 | from abc import ABCMeta, abstractmethod |
Pau Espin Pedrol | 9a4631c | 2018-03-28 19:17:34 +0200 | [diff] [blame] | 24 | from . import log, config, util, template, process, pcu_osmo, bts_osmo |
| 25 | from .event_loop import MainLoop |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 26 | |
Pau Espin Pedrol | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 27 | class OsmoBtsTrx(bts_osmo.OsmoBtsMainUnit): |
Pau Espin Pedrol | 2e54295 | 2017-12-14 19:08:51 +0100 | [diff] [blame] | 28 | ############## |
| 29 | # PROTECTED |
| 30 | ############## |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 31 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 32 | BIN_BTS_TRX = 'osmo-bts-trx' |
| 33 | BIN_PCU = 'osmo-pcu' |
| 34 | |
Neels Hofmeyr | 17c139e | 2017-04-12 02:42:02 +0200 | [diff] [blame] | 35 | CONF_BTS_TRX = 'osmo-bts-trx.cfg' |
| 36 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 37 | def __init__(self, suite_run, conf): |
Pau Espin Pedrol | e519462 | 2018-05-07 13:36:58 +0200 | [diff] [blame] | 38 | super().__init__(suite_run, conf, OsmoBtsTrx.BIN_BTS_TRX, 'osmo_bts_trx') |
Pau Espin Pedrol | 5860367 | 2018-08-09 13:45:55 +0200 | [diff] [blame] | 39 | self.run_dir = None |
| 40 | self.inst = None |
| 41 | self.trx = None |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 42 | self.env = {} |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 43 | self.gen_conf = {} |
Pau Espin Pedrol | 404e150 | 2017-08-22 11:17:43 +0200 | [diff] [blame] | 44 | |
| 45 | def trx_remote_ip(self): |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 46 | conf_ip = self.conf.get('osmo_trx', {}).get('trx_ip', None) |
Pau Espin Pedrol | 404e150 | 2017-08-22 11:17:43 +0200 | [diff] [blame] | 47 | 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 Pedrol | b1526b9 | 2018-05-22 20:32:30 +0200 | [diff] [blame] | 52 | def launch_process(self, keepalive, binary_name, *args): |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 53 | 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 Pedrol | b1526b9 | 2018-05-22 20:32:30 +0200 | [diff] [blame] | 60 | self.suite_run.remember_to_stop(proc, keepalive) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 61 | proc.launch() |
Neels Hofmeyr | fd39f3e | 2017-05-07 02:02:35 +0200 | [diff] [blame] | 62 | return proc |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 63 | |
| 64 | def configure(self): |
Neels Hofmeyr | c4ba4a6 | 2017-05-18 19:31:44 +0200 | [diff] [blame] | 65 | if self.bsc is None: |
| 66 | raise RuntimeError('BTS needs to be added to a BSC or NITB before it can be configured') |
Neels Hofmeyr | 17c139e | 2017-04-12 02:42:02 +0200 | [diff] [blame] | 67 | self.config_file = self.run_dir.new_file(OsmoBtsTrx.CONF_BTS_TRX) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 68 | self.dbg(config_file=self.config_file) |
| 69 | |
| 70 | values = dict(osmo_bts_trx=config.get_defaults('osmo_bts_trx')) |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 71 | config.overlay(values, dict(osmo_bts_trx=dict(osmo_trx=config.get_defaults('osmo_trx')))) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 72 | config.overlay(values, self.suite_run.config()) |
Pau Espin Pedrol | 329b6f4 | 2017-08-08 13:55:24 +0200 | [diff] [blame] | 73 | config.overlay(values, { |
| 74 | 'osmo_bts_trx': { |
| 75 | 'oml_remote_ip': self.bsc.addr(), |
Pau Espin Pedrol | 15aae98 | 2017-09-08 13:55:54 +0200 | [diff] [blame] | 76 | 'pcu_socket_path': self.pcu_socket_path(), |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 77 | 'osmo_trx': { |
| 78 | 'bts_ip': self.remote_addr(), |
| 79 | 'trx_ip': self.trx_remote_ip(), |
| 80 | 'channels': [{}] # TODO: implement channels for multiTRX |
| 81 | } |
Pau Espin Pedrol | 329b6f4 | 2017-08-08 13:55:24 +0200 | [diff] [blame] | 82 | } |
| 83 | }) |
| 84 | config.overlay(values, { 'osmo_bts_trx': self.conf }) |
Neels Hofmeyr | 943c81d | 2017-05-22 20:16:03 +0200 | [diff] [blame] | 85 | |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 86 | self.gen_conf = values |
Neels Hofmeyr | 943c81d | 2017-05-22 20:16:03 +0200 | [diff] [blame] | 87 | self.dbg('OSMO-BTS-TRX CONFIG:\n' + pprint.pformat(values)) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 88 | |
| 89 | with open(self.config_file, 'w') as f: |
Neels Hofmeyr | 17c139e | 2017-04-12 02:42:02 +0200 | [diff] [blame] | 90 | r = template.render(OsmoBtsTrx.CONF_BTS_TRX, values) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 91 | self.dbg(r) |
| 92 | f.write(r) |
| 93 | |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 94 | 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 Pedrol | 2e54295 | 2017-12-14 19:08:51 +0100 | [diff] [blame] | 100 | ######################## |
| 101 | # PUBLIC - INTERNAL API |
| 102 | ######################## |
Neels Hofmeyr | c4ba4a6 | 2017-05-18 19:31:44 +0200 | [diff] [blame] | 103 | def conf_for_bsc(self): |
Pau Espin Pedrol | e519462 | 2018-05-07 13:36:58 +0200 | [diff] [blame] | 104 | values = self.conf_for_bsc_prepare() |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 105 | self.dbg(conf=values) |
| 106 | return values |
| 107 | |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 108 | def conf_for_osmotrx(self): |
| 109 | return dict(osmo_trx=self.gen_conf['osmo_bts_trx'].get('osmo_trx', {})) |
| 110 | |
Pau Espin Pedrol | 2e54295 | 2017-12-14 19:08:51 +0100 | [diff] [blame] | 111 | ################### |
| 112 | # PUBLIC (test API included) |
| 113 | ################### |
Pau Espin Pedrol | b1526b9 | 2018-05-22 20:32:30 +0200 | [diff] [blame] | 114 | def start(self, keepalive=False): |
Pau Espin Pedrol | 2e54295 | 2017-12-14 19:08:51 +0100 | [diff] [blame] | 115 | 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 Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 124 | self.trx = OsmoTrx.get_instance_by_type(self.get_osmo_trx_type(), self.suite_run, self.conf_for_osmotrx()) |
Pau Espin Pedrol | b1526b9 | 2018-05-22 20:32:30 +0200 | [diff] [blame] | 125 | self.trx.start(keepalive) |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 126 | self.log('Waiting for %s to start up...' % self.trx.name()) |
Pau Espin Pedrol | 9a4631c | 2018-03-28 19:17:34 +0200 | [diff] [blame] | 127 | MainLoop.wait(self, self.trx.trx_ready) |
Pau Espin Pedrol | 2e54295 | 2017-12-14 19:08:51 +0100 | [diff] [blame] | 128 | |
| 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 Pedrol | b1526b9 | 2018-05-22 20:32:30 +0200 | [diff] [blame] | 135 | self.proc_bts = self.launch_process(keepalive, OsmoBtsTrx.BIN_BTS_TRX, '-r', '1', |
Pau Espin Pedrol | 2e54295 | 2017-12-14 19:08:51 +0100 | [diff] [blame] | 136 | '-c', os.path.abspath(self.config_file), |
| 137 | '-i', self.bsc.addr()) |
| 138 | self.suite_run.poll() |
| 139 | |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 140 | class OsmoTrx(log.Origin, metaclass=ABCMeta): |
Pau Espin Pedrol | 4773853 | 2017-08-15 16:20:07 +0200 | [diff] [blame] | 141 | |
Pau Espin Pedrol | edcc8d2 | 2018-03-04 19:58:23 +0100 | [diff] [blame] | 142 | CONF_OSMO_TRX = 'osmo-trx.cfg' |
Pau Espin Pedrol | 4773853 | 2017-08-15 16:20:07 +0200 | [diff] [blame] | 143 | |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 144 | ############## |
| 145 | # PROTECTED |
| 146 | ############## |
| 147 | def __init__(self, suite_run, conf): |
| 148 | super().__init__(log.C_RUN, self.binary_name()) |
Pau Espin Pedrol | 4773853 | 2017-08-15 16:20:07 +0200 | [diff] [blame] | 149 | self.suite_run = suite_run |
Pau Espin Pedrol | edcc8d2 | 2018-03-04 19:58:23 +0100 | [diff] [blame] | 150 | self.conf = conf |
Pau Espin Pedrol | 4773853 | 2017-08-15 16:20:07 +0200 | [diff] [blame] | 151 | self.env = {} |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 152 | self.listen_ip = conf.get('trx_ip') |
| 153 | self.bts_ip = conf.get('bts_ip') |
| 154 | self.run_dir = None |
| 155 | self.inst = None |
| 156 | self.proc_trx = None |
| 157 | |
| 158 | @classmethod |
| 159 | def get_instance_by_type(cls, type, suite_run, conf): |
| 160 | KNOWN_OSMOTRX_TYPES = { |
| 161 | 'uhd': OsmoTrxUHD, |
| 162 | } |
| 163 | osmo_trx_class = KNOWN_OSMOTRX_TYPES.get(type) |
| 164 | return osmo_trx_class(suite_run, conf) |
| 165 | |
| 166 | @abstractmethod |
| 167 | def binary_name(self): |
| 168 | 'Used by base class. Subclass can create different OsmoTRX implementations.' |
| 169 | pass |
Pau Espin Pedrol | 4773853 | 2017-08-15 16:20:07 +0200 | [diff] [blame] | 170 | |
Pau Espin Pedrol | edcc8d2 | 2018-03-04 19:58:23 +0100 | [diff] [blame] | 171 | def configure(self): |
| 172 | self.config_file = self.run_dir.new_file(OsmoTrx.CONF_OSMO_TRX) |
| 173 | self.dbg(config_file=self.config_file) |
| 174 | |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 175 | values = self.conf |
Pau Espin Pedrol | edcc8d2 | 2018-03-04 19:58:23 +0100 | [diff] [blame] | 176 | |
| 177 | self.dbg('OSMO-TRX CONFIG:\n' + pprint.pformat(values)) |
| 178 | |
| 179 | with open(self.config_file, 'w') as f: |
| 180 | r = template.render(OsmoTrx.CONF_OSMO_TRX, values) |
| 181 | self.dbg(r) |
| 182 | f.write(r) |
| 183 | |
Pau Espin Pedrol | b1526b9 | 2018-05-22 20:32:30 +0200 | [diff] [blame] | 184 | def launch_process(self, keepalive, binary_name, *args): |
Pau Espin Pedrol | 4773853 | 2017-08-15 16:20:07 +0200 | [diff] [blame] | 185 | binary = os.path.abspath(self.inst.child('bin', binary_name)) |
| 186 | run_dir = self.run_dir.new_dir(binary_name) |
| 187 | if not os.path.isfile(binary): |
| 188 | raise RuntimeError('Binary missing: %r' % binary) |
| 189 | proc = process.Process(binary_name, run_dir, |
| 190 | (binary,) + args, |
| 191 | env=self.env) |
Pau Espin Pedrol | b1526b9 | 2018-05-22 20:32:30 +0200 | [diff] [blame] | 192 | self.suite_run.remember_to_stop(proc, keepalive) |
Pau Espin Pedrol | 4773853 | 2017-08-15 16:20:07 +0200 | [diff] [blame] | 193 | proc.launch() |
| 194 | return proc |
| 195 | |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 196 | ############## |
| 197 | # PUBLIC (test API included) |
| 198 | ############## |
| 199 | def start(self, keepalive=False): |
| 200 | self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name())) |
| 201 | self.configure() |
| 202 | self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-trx'))) |
| 203 | lib = self.inst.child('lib') |
| 204 | self.env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) } |
| 205 | self.proc_trx = self.launch_process(keepalive, self.binary_name(), |
| 206 | '-C', os.path.abspath(self.config_file)) |
| 207 | |
Pau Espin Pedrol | 4773853 | 2017-08-15 16:20:07 +0200 | [diff] [blame] | 208 | def trx_ready(self): |
| 209 | if not self.proc_trx or not self.proc_trx.is_running: |
| 210 | return False |
| 211 | return '-- Transceiver active with' in (self.proc_trx.get_stdout() or '') |
Pau Espin Pedrol | 0d45504 | 2018-08-27 17:07:41 +0200 | [diff] [blame] | 212 | |
| 213 | class OsmoTrxUHD(OsmoTrx): |
| 214 | BIN_TRX = 'osmo-trx-uhd' |
| 215 | |
| 216 | def __init__(self, suite_run, conf): |
| 217 | super().__init__(suite_run, conf) |
| 218 | |
| 219 | def binary_name(self): |
| 220 | return OsmoTrxUHD.BIN_TRX |
| 221 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 222 | # vim: expandtab tabstop=4 shiftwidth=4 |