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