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 | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 23 | from . import log, config, util, template, process, event_loop, pcu_osmo, bts_osmo |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 24 | |
Pau Espin Pedrol | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 25 | class OsmoBtsTrx(bts_osmo.OsmoBtsMainUnit): |
Pau Espin Pedrol | 2e54295 | 2017-12-14 19:08:51 +0100 | [diff] [blame] | 26 | ############## |
| 27 | # PROTECTED |
| 28 | ############## |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 29 | run_dir = None |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 30 | inst = None |
| 31 | env = None |
Pau Espin Pedrol | 4773853 | 2017-08-15 16:20:07 +0200 | [diff] [blame] | 32 | trx = None |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 33 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 34 | BIN_BTS_TRX = 'osmo-bts-trx' |
| 35 | BIN_PCU = 'osmo-pcu' |
| 36 | |
Neels Hofmeyr | 17c139e | 2017-04-12 02:42:02 +0200 | [diff] [blame] | 37 | CONF_BTS_TRX = 'osmo-bts-trx.cfg' |
| 38 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 39 | def __init__(self, suite_run, conf): |
Pau Espin Pedrol | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 40 | super().__init__(suite_run, conf, OsmoBtsTrx.BIN_BTS_TRX) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 41 | self.env = {} |
Pau Espin Pedrol | 404e150 | 2017-08-22 11:17:43 +0200 | [diff] [blame] | 42 | |
| 43 | def trx_remote_ip(self): |
| 44 | conf_ip = self.conf.get('trx_remote_ip', None) |
| 45 | if conf_ip is not None: |
| 46 | return conf_ip |
| 47 | # if 'trx_remote_ip' is not configured, use same IP as BTS |
| 48 | return self.remote_addr() |
| 49 | |
| 50 | def launch_trx_enabled(self): |
| 51 | return util.str2bool(self.conf.get('launch_trx')) |
Neels Hofmeyr | d41dae9 | 2017-05-22 19:58:41 +0200 | [diff] [blame] | 52 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 53 | def launch_process(self, binary_name, *args): |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 54 | 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) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 61 | self.suite_run.remember_to_stop(proc) |
| 62 | proc.launch() |
Neels Hofmeyr | fd39f3e | 2017-05-07 02:02:35 +0200 | [diff] [blame] | 63 | return proc |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 64 | |
| 65 | def configure(self): |
Neels Hofmeyr | c4ba4a6 | 2017-05-18 19:31:44 +0200 | [diff] [blame] | 66 | if self.bsc is None: |
| 67 | 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] | 68 | self.config_file = self.run_dir.new_file(OsmoBtsTrx.CONF_BTS_TRX) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 69 | self.dbg(config_file=self.config_file) |
| 70 | |
| 71 | values = dict(osmo_bts_trx=config.get_defaults('osmo_bts_trx')) |
| 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 | 404e150 | 2017-08-22 11:17:43 +0200 | [diff] [blame] | 76 | 'trx_local_ip': self.remote_addr(), |
| 77 | 'trx_remote_ip': self.trx_remote_ip(), |
Pau Espin Pedrol | 15aae98 | 2017-09-08 13:55:54 +0200 | [diff] [blame] | 78 | 'pcu_socket_path': self.pcu_socket_path(), |
Pau Espin Pedrol | 329b6f4 | 2017-08-08 13:55:24 +0200 | [diff] [blame] | 79 | } |
| 80 | }) |
| 81 | config.overlay(values, { 'osmo_bts_trx': self.conf }) |
Neels Hofmeyr | 943c81d | 2017-05-22 20:16:03 +0200 | [diff] [blame] | 82 | |
| 83 | self.dbg('OSMO-BTS-TRX CONFIG:\n' + pprint.pformat(values)) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 84 | |
| 85 | with open(self.config_file, 'w') as f: |
Neels Hofmeyr | 17c139e | 2017-04-12 02:42:02 +0200 | [diff] [blame] | 86 | r = template.render(OsmoBtsTrx.CONF_BTS_TRX, values) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 87 | self.dbg(r) |
| 88 | f.write(r) |
| 89 | |
Pau Espin Pedrol | 2e54295 | 2017-12-14 19:08:51 +0100 | [diff] [blame] | 90 | ######################## |
| 91 | # PUBLIC - INTERNAL API |
| 92 | ######################## |
Neels Hofmeyr | c4ba4a6 | 2017-05-18 19:31:44 +0200 | [diff] [blame] | 93 | def conf_for_bsc(self): |
| 94 | values = config.get_defaults('bsc_bts') |
Neels Hofmeyr | 17c139e | 2017-04-12 02:42:02 +0200 | [diff] [blame] | 95 | config.overlay(values, config.get_defaults('osmo_bts_trx')) |
Pau Espin Pedrol | 5e0c251 | 2017-11-06 18:40:23 +0100 | [diff] [blame] | 96 | if self.lac is not None: |
| 97 | config.overlay(values, { 'location_area_code': self.lac }) |
Pau Espin Pedrol | 8a3a7b5 | 2017-11-28 15:50:02 +0100 | [diff] [blame] | 98 | if self.rac is not None: |
| 99 | config.overlay(values, { 'routing_area_code': self.rac }) |
Pau Espin Pedrol | 4ccce7c | 2017-11-07 11:13:20 +0100 | [diff] [blame] | 100 | if self.cellid is not None: |
| 101 | config.overlay(values, { 'cell_identity': self.cellid }) |
Pau Espin Pedrol | 8a3a7b5 | 2017-11-28 15:50:02 +0100 | [diff] [blame] | 102 | if self.bvci is not None: |
| 103 | config.overlay(values, { 'bvci': self.bvci }) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 104 | config.overlay(values, self.conf) |
Pau Espin Pedrol | 5936d8f | 2017-11-23 13:34:27 +0100 | [diff] [blame] | 105 | |
| 106 | sgsn_conf = {} if self.sgsn is None else self.sgsn.conf_for_client() |
| 107 | config.overlay(values, sgsn_conf) |
| 108 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 109 | self.dbg(conf=values) |
| 110 | return values |
| 111 | |
Pau Espin Pedrol | 2e54295 | 2017-12-14 19:08:51 +0100 | [diff] [blame] | 112 | ################### |
| 113 | # PUBLIC (test API included) |
| 114 | ################### |
| 115 | def start(self): |
| 116 | if self.bsc is None: |
| 117 | raise RuntimeError('BTS needs to be added to a BSC or NITB before it can be started') |
| 118 | self.suite_run.poll() |
| 119 | |
| 120 | self.log('Starting to connect to', self.bsc) |
| 121 | self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name())) |
| 122 | self.configure() |
| 123 | |
| 124 | if self.launch_trx_enabled(): |
Pau Espin Pedrol | edcc8d2 | 2018-03-04 19:58:23 +0100 | [diff] [blame] | 125 | self.trx = OsmoTrx(self.suite_run, self.conf, self.trx_remote_ip(), self.remote_addr()) |
Pau Espin Pedrol | 2e54295 | 2017-12-14 19:08:51 +0100 | [diff] [blame] | 126 | self.trx.start() |
| 127 | self.log('Waiting for osmo-trx to start up...') |
| 128 | event_loop.wait(self, self.trx.trx_ready) |
| 129 | |
| 130 | self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-bts'))) |
| 131 | lib = self.inst.child('lib') |
| 132 | if not os.path.isdir(lib): |
| 133 | raise RuntimeError('No lib/ in %r' % self.inst) |
| 134 | self.env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) } |
| 135 | |
| 136 | self.proc_bts = self.launch_process(OsmoBtsTrx.BIN_BTS_TRX, '-r', '1', |
| 137 | '-c', os.path.abspath(self.config_file), |
| 138 | '-i', self.bsc.addr()) |
| 139 | self.suite_run.poll() |
| 140 | |
Pau Espin Pedrol | 4773853 | 2017-08-15 16:20:07 +0200 | [diff] [blame] | 141 | class OsmoTrx(log.Origin): |
| 142 | suite_run = None |
| 143 | run_dir = None |
| 144 | inst = None |
| 145 | env = None |
| 146 | proc_trx = None |
| 147 | |
| 148 | BIN_TRX = 'osmo-trx' |
Pau Espin Pedrol | edcc8d2 | 2018-03-04 19:58:23 +0100 | [diff] [blame] | 149 | CONF_OSMO_TRX = 'osmo-trx.cfg' |
Pau Espin Pedrol | 4773853 | 2017-08-15 16:20:07 +0200 | [diff] [blame] | 150 | |
Pau Espin Pedrol | edcc8d2 | 2018-03-04 19:58:23 +0100 | [diff] [blame] | 151 | def __init__(self, suite_run, conf, listen_ip, bts_ip): |
Pau Espin Pedrol | 4773853 | 2017-08-15 16:20:07 +0200 | [diff] [blame] | 152 | super().__init__(log.C_RUN, OsmoTrx.BIN_TRX) |
| 153 | self.suite_run = suite_run |
Pau Espin Pedrol | edcc8d2 | 2018-03-04 19:58:23 +0100 | [diff] [blame] | 154 | self.conf = conf |
Pau Espin Pedrol | 4773853 | 2017-08-15 16:20:07 +0200 | [diff] [blame] | 155 | self.env = {} |
Pau Espin Pedrol | 404e150 | 2017-08-22 11:17:43 +0200 | [diff] [blame] | 156 | self.listen_ip = listen_ip |
| 157 | self.bts_ip = bts_ip |
Pau Espin Pedrol | 4773853 | 2017-08-15 16:20:07 +0200 | [diff] [blame] | 158 | |
Pau Espin Pedrol | edcc8d2 | 2018-03-04 19:58:23 +0100 | [diff] [blame] | 159 | def configure(self): |
| 160 | self.config_file = self.run_dir.new_file(OsmoTrx.CONF_OSMO_TRX) |
| 161 | self.dbg(config_file=self.config_file) |
| 162 | |
| 163 | values = dict(osmo_bts_trx=config.get_defaults('osmo_trx')) |
| 164 | config.overlay(values, self.suite_run.config()) |
| 165 | config.overlay(values, { |
| 166 | 'osmo_trx': { |
| 167 | 'bind_ip' : self.listen_ip, |
| 168 | } |
| 169 | }) |
| 170 | config.overlay(values, { 'osmo_trx': self.conf }) |
| 171 | |
| 172 | self.dbg('OSMO-TRX CONFIG:\n' + pprint.pformat(values)) |
| 173 | |
| 174 | with open(self.config_file, 'w') as f: |
| 175 | r = template.render(OsmoTrx.CONF_OSMO_TRX, values) |
| 176 | self.dbg(r) |
| 177 | f.write(r) |
| 178 | |
Pau Espin Pedrol | 4773853 | 2017-08-15 16:20:07 +0200 | [diff] [blame] | 179 | def start(self): |
| 180 | self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name())) |
Pau Espin Pedrol | edcc8d2 | 2018-03-04 19:58:23 +0100 | [diff] [blame] | 181 | self.configure() |
Pau Espin Pedrol | 4773853 | 2017-08-15 16:20:07 +0200 | [diff] [blame] | 182 | self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst(OsmoTrx.BIN_TRX))) |
Pau Espin Pedrol | 80a5c77 | 2017-08-21 19:03:08 +0200 | [diff] [blame] | 183 | lib = self.inst.child('lib') |
| 184 | self.env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) } |
Pau Espin Pedrol | edcc8d2 | 2018-03-04 19:58:23 +0100 | [diff] [blame] | 185 | self.proc_trx = self.launch_process(OsmoTrx.BIN_TRX, '-x', |
| 186 | '-j', self.listen_ip, '-i', self.bts_ip, |
| 187 | '-C', os.path.abspath(self.config_file)) |
Pau Espin Pedrol | 4773853 | 2017-08-15 16:20:07 +0200 | [diff] [blame] | 188 | |
| 189 | def launch_process(self, binary_name, *args): |
| 190 | binary = os.path.abspath(self.inst.child('bin', binary_name)) |
| 191 | run_dir = self.run_dir.new_dir(binary_name) |
| 192 | if not os.path.isfile(binary): |
| 193 | raise RuntimeError('Binary missing: %r' % binary) |
| 194 | proc = process.Process(binary_name, run_dir, |
| 195 | (binary,) + args, |
| 196 | env=self.env) |
| 197 | self.suite_run.remember_to_stop(proc) |
| 198 | proc.launch() |
| 199 | return proc |
| 200 | |
| 201 | def trx_ready(self): |
| 202 | if not self.proc_trx or not self.proc_trx.is_running: |
| 203 | return False |
| 204 | return '-- Transceiver active with' in (self.proc_trx.get_stdout() or '') |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 205 | # vim: expandtab tabstop=4 shiftwidth=4 |