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