blob: bd8aedf5cb78a47d2b9a5770e647fc6d0604630d [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
Pau Espin Pedrol9a4631c2018-03-28 19:17:34 +020023from . import log, config, util, template, process, pcu_osmo, bts_osmo
24from .event_loop import MainLoop
Neels Hofmeyr3531a192017-03-28 14:30:28 +020025
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +010026class OsmoBtsTrx(bts_osmo.OsmoBtsMainUnit):
Pau Espin Pedrol2e542952017-12-14 19:08:51 +010027##############
28# PROTECTED
29##############
Neels Hofmeyr3531a192017-03-28 14:30:28 +020030 run_dir = None
Neels Hofmeyr3531a192017-03-28 14:30:28 +020031 inst = None
32 env = None
Pau Espin Pedrol47738532017-08-15 16:20:07 +020033 trx = None
Neels Hofmeyr3531a192017-03-28 14:30:28 +020034
Neels Hofmeyr3531a192017-03-28 14:30:28 +020035 BIN_BTS_TRX = 'osmo-bts-trx'
36 BIN_PCU = 'osmo-pcu'
37
Neels Hofmeyr17c139e2017-04-12 02:42:02 +020038 CONF_BTS_TRX = 'osmo-bts-trx.cfg'
39
Neels Hofmeyr3531a192017-03-28 14:30:28 +020040 def __init__(self, suite_run, conf):
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +010041 super().__init__(suite_run, conf, OsmoBtsTrx.BIN_BTS_TRX)
Neels Hofmeyr3531a192017-03-28 14:30:28 +020042 self.env = {}
Pau Espin Pedrol404e1502017-08-22 11:17:43 +020043
44 def trx_remote_ip(self):
45 conf_ip = self.conf.get('trx_remote_ip', None)
46 if conf_ip is not None:
47 return conf_ip
48 # if 'trx_remote_ip' is not configured, use same IP as BTS
49 return self.remote_addr()
50
51 def launch_trx_enabled(self):
52 return util.str2bool(self.conf.get('launch_trx'))
Neels Hofmeyrd41dae92017-05-22 19:58:41 +020053
Neels Hofmeyr3531a192017-03-28 14:30:28 +020054 def launch_process(self, binary_name, *args):
Neels Hofmeyr3531a192017-03-28 14:30:28 +020055 binary = os.path.abspath(self.inst.child('bin', binary_name))
56 run_dir = self.run_dir.new_dir(binary_name)
57 if not os.path.isfile(binary):
58 raise RuntimeError('Binary missing: %r' % binary)
59 proc = process.Process(binary_name, run_dir,
60 (binary,) + args,
61 env=self.env)
Neels Hofmeyr3531a192017-03-28 14:30:28 +020062 self.suite_run.remember_to_stop(proc)
63 proc.launch()
Neels Hofmeyrfd39f3e2017-05-07 02:02:35 +020064 return proc
Neels Hofmeyr3531a192017-03-28 14:30:28 +020065
66 def configure(self):
Neels Hofmeyrc4ba4a62017-05-18 19:31:44 +020067 if self.bsc is None:
68 raise RuntimeError('BTS needs to be added to a BSC or NITB before it can be configured')
Neels Hofmeyr17c139e2017-04-12 02:42:02 +020069 self.config_file = self.run_dir.new_file(OsmoBtsTrx.CONF_BTS_TRX)
Neels Hofmeyr3531a192017-03-28 14:30:28 +020070 self.dbg(config_file=self.config_file)
71
72 values = dict(osmo_bts_trx=config.get_defaults('osmo_bts_trx'))
73 config.overlay(values, self.suite_run.config())
Pau Espin Pedrol329b6f42017-08-08 13:55:24 +020074 config.overlay(values, {
75 'osmo_bts_trx': {
76 'oml_remote_ip': self.bsc.addr(),
Pau Espin Pedrol404e1502017-08-22 11:17:43 +020077 'trx_local_ip': self.remote_addr(),
78 'trx_remote_ip': self.trx_remote_ip(),
Pau Espin Pedrol15aae982017-09-08 13:55:54 +020079 'pcu_socket_path': self.pcu_socket_path(),
Pau Espin Pedrol329b6f42017-08-08 13:55:24 +020080 }
81 })
82 config.overlay(values, { 'osmo_bts_trx': self.conf })
Neels Hofmeyr943c81d2017-05-22 20:16:03 +020083
84 self.dbg('OSMO-BTS-TRX CONFIG:\n' + pprint.pformat(values))
Neels Hofmeyr3531a192017-03-28 14:30:28 +020085
86 with open(self.config_file, 'w') as f:
Neels Hofmeyr17c139e2017-04-12 02:42:02 +020087 r = template.render(OsmoBtsTrx.CONF_BTS_TRX, values)
Neels Hofmeyr3531a192017-03-28 14:30:28 +020088 self.dbg(r)
89 f.write(r)
90
Pau Espin Pedrol2e542952017-12-14 19:08:51 +010091########################
92# PUBLIC - INTERNAL API
93########################
Neels Hofmeyrc4ba4a62017-05-18 19:31:44 +020094 def conf_for_bsc(self):
95 values = config.get_defaults('bsc_bts')
Neels Hofmeyr17c139e2017-04-12 02:42:02 +020096 config.overlay(values, config.get_defaults('osmo_bts_trx'))
Pau Espin Pedrol5e0c2512017-11-06 18:40:23 +010097 if self.lac is not None:
98 config.overlay(values, { 'location_area_code': self.lac })
Pau Espin Pedrol8a3a7b52017-11-28 15:50:02 +010099 if self.rac is not None:
100 config.overlay(values, { 'routing_area_code': self.rac })
Pau Espin Pedrol4ccce7c2017-11-07 11:13:20 +0100101 if self.cellid is not None:
102 config.overlay(values, { 'cell_identity': self.cellid })
Pau Espin Pedrol8a3a7b52017-11-28 15:50:02 +0100103 if self.bvci is not None:
104 config.overlay(values, { 'bvci': self.bvci })
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200105 config.overlay(values, self.conf)
Pau Espin Pedrol5936d8f2017-11-23 13:34:27 +0100106
107 sgsn_conf = {} if self.sgsn is None else self.sgsn.conf_for_client()
108 config.overlay(values, sgsn_conf)
109
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200110 self.dbg(conf=values)
111 return values
112
Pau Espin Pedrol2e542952017-12-14 19:08:51 +0100113###################
114# PUBLIC (test API included)
115###################
116 def start(self):
117 if self.bsc is None:
118 raise RuntimeError('BTS needs to be added to a BSC or NITB before it can be started')
119 self.suite_run.poll()
120
121 self.log('Starting to connect to', self.bsc)
122 self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name()))
123 self.configure()
124
125 if self.launch_trx_enabled():
Pau Espin Pedroledcc8d22018-03-04 19:58:23 +0100126 self.trx = OsmoTrx(self.suite_run, self.conf, self.trx_remote_ip(), self.remote_addr())
Pau Espin Pedrol2e542952017-12-14 19:08:51 +0100127 self.trx.start()
128 self.log('Waiting for osmo-trx to start up...')
Pau Espin Pedrol9a4631c2018-03-28 19:17:34 +0200129 MainLoop.wait(self, self.trx.trx_ready)
Pau Espin Pedrol2e542952017-12-14 19:08:51 +0100130
131 self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-bts')))
132 lib = self.inst.child('lib')
133 if not os.path.isdir(lib):
134 raise RuntimeError('No lib/ in %r' % self.inst)
135 self.env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
136
137 self.proc_bts = self.launch_process(OsmoBtsTrx.BIN_BTS_TRX, '-r', '1',
138 '-c', os.path.abspath(self.config_file),
139 '-i', self.bsc.addr())
140 self.suite_run.poll()
141
Pau Espin Pedrol47738532017-08-15 16:20:07 +0200142class OsmoTrx(log.Origin):
143 suite_run = None
144 run_dir = None
145 inst = None
146 env = None
147 proc_trx = None
148
Pau Espin Pedrol553eda62018-05-07 00:46:18 +0200149 BIN_TRX = 'osmo-trx-uhd'
Pau Espin Pedroledcc8d22018-03-04 19:58:23 +0100150 CONF_OSMO_TRX = 'osmo-trx.cfg'
Pau Espin Pedrol47738532017-08-15 16:20:07 +0200151
Pau Espin Pedroledcc8d22018-03-04 19:58:23 +0100152 def __init__(self, suite_run, conf, listen_ip, bts_ip):
Pau Espin Pedrol47738532017-08-15 16:20:07 +0200153 super().__init__(log.C_RUN, OsmoTrx.BIN_TRX)
154 self.suite_run = suite_run
Pau Espin Pedroledcc8d22018-03-04 19:58:23 +0100155 self.conf = conf
Pau Espin Pedrol47738532017-08-15 16:20:07 +0200156 self.env = {}
Pau Espin Pedrol404e1502017-08-22 11:17:43 +0200157 self.listen_ip = listen_ip
158 self.bts_ip = bts_ip
Pau Espin Pedrol47738532017-08-15 16:20:07 +0200159
Pau Espin Pedroledcc8d22018-03-04 19:58:23 +0100160 def configure(self):
161 self.config_file = self.run_dir.new_file(OsmoTrx.CONF_OSMO_TRX)
162 self.dbg(config_file=self.config_file)
163
164 values = dict(osmo_bts_trx=config.get_defaults('osmo_trx'))
165 config.overlay(values, self.suite_run.config())
166 config.overlay(values, {
167 'osmo_trx': {
168 'bind_ip' : self.listen_ip,
169 }
170 })
171 config.overlay(values, { 'osmo_trx': self.conf })
172
173 self.dbg('OSMO-TRX CONFIG:\n' + pprint.pformat(values))
174
175 with open(self.config_file, 'w') as f:
176 r = template.render(OsmoTrx.CONF_OSMO_TRX, values)
177 self.dbg(r)
178 f.write(r)
179
Pau Espin Pedrol47738532017-08-15 16:20:07 +0200180 def start(self):
181 self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name()))
Pau Espin Pedroledcc8d22018-03-04 19:58:23 +0100182 self.configure()
Pau Espin Pedrol7d4bdf22018-05-07 11:00:30 +0200183 self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-trx')))
Pau Espin Pedrol80a5c772017-08-21 19:03:08 +0200184 lib = self.inst.child('lib')
185 self.env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
Pau Espin Pedroledcc8d22018-03-04 19:58:23 +0100186 self.proc_trx = self.launch_process(OsmoTrx.BIN_TRX, '-x',
187 '-j', self.listen_ip, '-i', self.bts_ip,
188 '-C', os.path.abspath(self.config_file))
Pau Espin Pedrol47738532017-08-15 16:20:07 +0200189
190 def launch_process(self, binary_name, *args):
191 binary = os.path.abspath(self.inst.child('bin', binary_name))
192 run_dir = self.run_dir.new_dir(binary_name)
193 if not os.path.isfile(binary):
194 raise RuntimeError('Binary missing: %r' % binary)
195 proc = process.Process(binary_name, run_dir,
196 (binary,) + args,
197 env=self.env)
198 self.suite_run.remember_to_stop(proc)
199 proc.launch()
200 return proc
201
202 def trx_ready(self):
203 if not self.proc_trx or not self.proc_trx.is_running:
204 return False
205 return '-- Transceiver active with' in (self.proc_trx.get_stdout() or '')
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200206# vim: expandtab tabstop=4 shiftwidth=4