blob: a96416930552ae260aaa46b30d716e28d8ed509e [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 Pedrol15aae982017-09-08 13:55:54 +020022import tempfile
Neels Hofmeyr206803d2017-05-29 03:45:51 +020023from . import log, config, util, template, process, event_loop
Neels Hofmeyr3531a192017-03-28 14:30:28 +020024
25class OsmoBtsTrx(log.Origin):
26 suite_run = None
Neels Hofmeyrc4ba4a62017-05-18 19:31:44 +020027 bsc = None
Neels Hofmeyr3531a192017-03-28 14:30:28 +020028 run_dir = None
Neels Hofmeyr3531a192017-03-28 14:30:28 +020029 inst = None
30 env = None
Pau Espin Pedrol47738532017-08-15 16:20:07 +020031 trx = None
Pau Espin Pedrol15aae982017-09-08 13:55:54 +020032 pcu_sk_tmp_dir = None
Pau Espin Pedrol5e0c2512017-11-06 18:40:23 +010033 lac = None
Pau Espin Pedrol4ccce7c2017-11-07 11:13:20 +010034 cellid = None
Neels Hofmeyr3531a192017-03-28 14:30:28 +020035
Neels Hofmeyr3531a192017-03-28 14:30:28 +020036 BIN_BTS_TRX = 'osmo-bts-trx'
37 BIN_PCU = 'osmo-pcu'
38
Neels Hofmeyr17c139e2017-04-12 02:42:02 +020039 CONF_BTS_TRX = 'osmo-bts-trx.cfg'
40
Neels Hofmeyr3531a192017-03-28 14:30:28 +020041 def __init__(self, suite_run, conf):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020042 super().__init__(log.C_RUN, OsmoBtsTrx.BIN_BTS_TRX)
Neels Hofmeyr3531a192017-03-28 14:30:28 +020043 self.suite_run = suite_run
44 self.conf = conf
Neels Hofmeyr3531a192017-03-28 14:30:28 +020045 self.env = {}
Pau Espin Pedrol32cab3f2017-09-13 17:01:55 +020046 self.pcu_sk_tmp_dir = tempfile.mkdtemp('', 'ogtpcusk')
Pau Espin Pedrol15aae982017-09-08 13:55:54 +020047 if len(self.pcu_socket_path().encode()) > 107:
48 raise log.Error('Path for pcu socket is longer than max allowed len for unix socket path (107):', self.pcu_socket_path())
49
50 def cleanup(self):
51 if self.pcu_sk_tmp_dir:
52 try:
53 os.remove(self.pcu_socket_path())
54 except OSError:
55 pass
56 os.rmdir(self.pcu_sk_tmp_dir)
57
58 def pcu_socket_path(self):
59 return os.path.join(self.pcu_sk_tmp_dir, 'pcu_bts')
Neels Hofmeyr3531a192017-03-28 14:30:28 +020060
Neels Hofmeyrd41dae92017-05-22 19:58:41 +020061 def remote_addr(self):
Pau Espin Pedrol404e1502017-08-22 11:17:43 +020062 return self.conf.get('addr')
63
64 def trx_remote_ip(self):
65 conf_ip = self.conf.get('trx_remote_ip', None)
66 if conf_ip is not None:
67 return conf_ip
68 # if 'trx_remote_ip' is not configured, use same IP as BTS
69 return self.remote_addr()
70
71 def launch_trx_enabled(self):
72 return util.str2bool(self.conf.get('launch_trx'))
Neels Hofmeyrd41dae92017-05-22 19:58:41 +020073
Neels Hofmeyr3531a192017-03-28 14:30:28 +020074 def start(self):
Neels Hofmeyrc4ba4a62017-05-18 19:31:44 +020075 if self.bsc is None:
76 raise RuntimeError('BTS needs to be added to a BSC or NITB before it can be started')
Neels Hofmeyr3531a192017-03-28 14:30:28 +020077 self.suite_run.poll()
78
Neels Hofmeyrc4ba4a62017-05-18 19:31:44 +020079 self.log('Starting to connect to', self.bsc)
Pau Espin Pedrold0912332017-06-14 13:27:08 +020080 self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name()))
Neels Hofmeyr3531a192017-03-28 14:30:28 +020081 self.configure()
82
Pau Espin Pedrol404e1502017-08-22 11:17:43 +020083 if self.launch_trx_enabled():
84 self.trx = OsmoTrx(self.suite_run, self.trx_remote_ip(), self.remote_addr())
85 self.trx.start()
86 self.log('Waiting for osmo-trx to start up...')
87 event_loop.wait(self, self.trx.trx_ready)
Pau Espin Pedrol47738532017-08-15 16:20:07 +020088
Pau Espin Pedrolcfac9932017-09-13 18:05:13 +020089 self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-bts')))
Your Name3c6673a2017-04-08 18:52:39 +020090 lib = self.inst.child('lib')
91 if not os.path.isdir(lib):
92 raise RuntimeError('No lib/ in %r' % self.inst)
Pau Espin Pedrole39c6f12017-05-08 16:21:38 +020093 self.env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
Neels Hofmeyr3531a192017-03-28 14:30:28 +020094
Pau Espin Pedrol6ab98982017-05-12 16:07:35 +020095 self.launch_process(OsmoBtsTrx.BIN_BTS_TRX, '-r', '1',
96 '-c', os.path.abspath(self.config_file),
Neels Hofmeyrc4ba4a62017-05-18 19:31:44 +020097 '-i', self.bsc.addr())
Neels Hofmeyr3531a192017-03-28 14:30:28 +020098 #self.launch_process(OsmoBtsTrx.BIN_PCU, '-r', '1')
99 self.suite_run.poll()
100
101 def launch_process(self, binary_name, *args):
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200102 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 Hofmeyr3531a192017-03-28 14:30:28 +0200109 self.suite_run.remember_to_stop(proc)
110 proc.launch()
Neels Hofmeyrfd39f3e2017-05-07 02:02:35 +0200111 return proc
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200112
113 def configure(self):
Neels Hofmeyrc4ba4a62017-05-18 19:31:44 +0200114 if self.bsc is None:
115 raise RuntimeError('BTS needs to be added to a BSC or NITB before it can be configured')
Neels Hofmeyr17c139e2017-04-12 02:42:02 +0200116 self.config_file = self.run_dir.new_file(OsmoBtsTrx.CONF_BTS_TRX)
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200117 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 Pedrol329b6f42017-08-08 13:55:24 +0200121 config.overlay(values, {
122 'osmo_bts_trx': {
123 'oml_remote_ip': self.bsc.addr(),
Pau Espin Pedrol404e1502017-08-22 11:17:43 +0200124 'trx_local_ip': self.remote_addr(),
125 'trx_remote_ip': self.trx_remote_ip(),
Pau Espin Pedrol15aae982017-09-08 13:55:54 +0200126 'pcu_socket_path': self.pcu_socket_path(),
Pau Espin Pedrol329b6f42017-08-08 13:55:24 +0200127 }
128 })
129 config.overlay(values, { 'osmo_bts_trx': self.conf })
Neels Hofmeyr943c81d2017-05-22 20:16:03 +0200130
131 self.dbg('OSMO-BTS-TRX CONFIG:\n' + pprint.pformat(values))
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200132
133 with open(self.config_file, 'w') as f:
Neels Hofmeyr17c139e2017-04-12 02:42:02 +0200134 r = template.render(OsmoBtsTrx.CONF_BTS_TRX, values)
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200135 self.dbg(r)
136 f.write(r)
137
Neels Hofmeyrc4ba4a62017-05-18 19:31:44 +0200138 def conf_for_bsc(self):
139 values = config.get_defaults('bsc_bts')
Neels Hofmeyr17c139e2017-04-12 02:42:02 +0200140 config.overlay(values, config.get_defaults('osmo_bts_trx'))
Pau Espin Pedrol5e0c2512017-11-06 18:40:23 +0100141 if self.lac is not None:
142 config.overlay(values, { 'location_area_code': self.lac })
Pau Espin Pedrol4ccce7c2017-11-07 11:13:20 +0100143 if self.cellid is not None:
144 config.overlay(values, { 'cell_identity': self.cellid })
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200145 config.overlay(values, self.conf)
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200146 self.dbg(conf=values)
147 return values
148
Neels Hofmeyrc4ba4a62017-05-18 19:31:44 +0200149 def set_bsc(self, bsc):
150 self.bsc = bsc
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200151
Pau Espin Pedrol5e0c2512017-11-06 18:40:23 +0100152 def set_lac(self, lac):
153 self.lac = lac
154
Pau Espin Pedrol4ccce7c2017-11-07 11:13:20 +0100155 def set_cellid(self, cellid):
156 self.cellid = cellid
157
Pau Espin Pedrol47738532017-08-15 16:20:07 +0200158class OsmoTrx(log.Origin):
159 suite_run = None
160 run_dir = None
161 inst = None
162 env = None
163 proc_trx = None
164
165 BIN_TRX = 'osmo-trx'
166
Pau Espin Pedrol404e1502017-08-22 11:17:43 +0200167 def __init__(self, suite_run, listen_ip, bts_ip):
Pau Espin Pedrol47738532017-08-15 16:20:07 +0200168 super().__init__(log.C_RUN, OsmoTrx.BIN_TRX)
169 self.suite_run = suite_run
170 self.env = {}
Pau Espin Pedrol404e1502017-08-22 11:17:43 +0200171 self.listen_ip = listen_ip
172 self.bts_ip = bts_ip
Pau Espin Pedrol47738532017-08-15 16:20:07 +0200173
174 def start(self):
175 self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name()))
176 self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst(OsmoTrx.BIN_TRX)))
Pau Espin Pedrol80a5c772017-08-21 19:03:08 +0200177 lib = self.inst.child('lib')
178 self.env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
Pau Espin Pedrol404e1502017-08-22 11:17:43 +0200179 self.proc_trx = self.launch_process(OsmoTrx.BIN_TRX, '-x', '-j', self.listen_ip, '-i', self.bts_ip)
Pau Espin Pedrol47738532017-08-15 16:20:07 +0200180
181 def launch_process(self, binary_name, *args):
182 binary = os.path.abspath(self.inst.child('bin', binary_name))
183 run_dir = self.run_dir.new_dir(binary_name)
184 if not os.path.isfile(binary):
185 raise RuntimeError('Binary missing: %r' % binary)
186 proc = process.Process(binary_name, run_dir,
187 (binary,) + args,
188 env=self.env)
189 self.suite_run.remember_to_stop(proc)
190 proc.launch()
191 return proc
192
193 def trx_ready(self):
194 if not self.proc_trx or not self.proc_trx.is_running:
195 return False
196 return '-- Transceiver active with' in (self.proc_trx.get_stdout() or '')
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200197# vim: expandtab tabstop=4 shiftwidth=4