blob: 812a24743cadec5075930fbefaf46c4b8c423670 [file] [log] [blame]
Neels Hofmeyr3531a192017-03-28 14:30:28 +02001# osmo_gsm_tester: specifics for running a sysmoBTS
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 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
Neels Hofmeyr5356d0a2017-04-10 03:45:30 +020020import os
Neels Hofmeyr943c81d2017-05-22 20:16:03 +020021import pprint
Pau Espin Pedrolce35d912017-11-23 11:01:24 +010022from . import log, config, util, template, process, pcu_sysmo
Neels Hofmeyr3531a192017-03-28 14:30:28 +020023
24class SysmoBts(log.Origin):
25 suite_run = None
Neels Hofmeyrc4ba4a62017-05-18 19:31:44 +020026 bsc = None
Pau Espin Pedrolce35d912017-11-23 11:01:24 +010027 sgsn = None
Neels Hofmeyr3531a192017-03-28 14:30:28 +020028 run_dir = None
Neels Hofmeyr5356d0a2017-04-10 03:45:30 +020029 inst = None
Neels Hofmeyr5356d0a2017-04-10 03:45:30 +020030 remote_inst = None
31 remote_env = None
32 remote_dir = None
Pau Espin Pedrol5e0c2512017-11-06 18:40:23 +010033 lac = None
Pau Espin Pedrol4ccce7c2017-11-07 11:13:20 +010034 cellid = None
Pau Espin Pedrold0a01112017-11-28 12:15:10 +010035 proc_bts = None
Pau Espin Pedrolce35d912017-11-23 11:01:24 +010036 _pcu = None
Neels Hofmeyr5356d0a2017-04-10 03:45:30 +020037
Pau Espin Pedrolce35d912017-11-23 11:01:24 +010038 REMOTE_DIR = '/osmo-gsm-tester-bts'
Neels Hofmeyr5356d0a2017-04-10 03:45:30 +020039 BTS_SYSMO_BIN = 'osmo-bts-sysmo'
40 BTS_SYSMO_CFG = 'osmo-bts-sysmo.cfg'
Neels Hofmeyr3531a192017-03-28 14:30:28 +020041
42 def __init__(self, suite_run, conf):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020043 super().__init__(log.C_RUN, self.BTS_SYSMO_BIN)
Neels Hofmeyr3531a192017-03-28 14:30:28 +020044 self.suite_run = suite_run
45 self.conf = conf
Neels Hofmeyr5356d0a2017-04-10 03:45:30 +020046 self.remote_env = {}
Pau Espin Pedrol3895fec2017-04-28 16:13:03 +020047 self.remote_user = 'root'
Neels Hofmeyr3531a192017-03-28 14:30:28 +020048
49 def start(self):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020050 if self.bsc is None:
51 raise RuntimeError('BTS needs to be added to a BSC or NITB before it can be started')
52 log.log('Starting sysmoBTS to connect to', self.bsc)
Pau Espin Pedrold0912332017-06-14 13:27:08 +020053 self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name()))
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020054 self.configure()
Neels Hofmeyr5356d0a2017-04-10 03:45:30 +020055
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020056 self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst(SysmoBts.BTS_SYSMO_BIN)))
57 lib = self.inst.child('lib')
58 if not os.path.isdir(lib):
59 raise log.Error('No lib/ in', self.inst)
60 if not self.inst.isfile('bin', SysmoBts.BTS_SYSMO_BIN):
61 raise log.Error('No osmo-bts-sysmo binary in', self.inst)
Neels Hofmeyr5356d0a2017-04-10 03:45:30 +020062
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020063 self.remote_dir = util.Dir(SysmoBts.REMOTE_DIR)
64 self.remote_inst = util.Dir(self.remote_dir.child(os.path.basename(str(self.inst))))
Neels Hofmeyr5356d0a2017-04-10 03:45:30 +020065
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020066 self.run_remote('rm-remote-dir', ('test', '!', '-d', SysmoBts.REMOTE_DIR, '||', 'rm', '-rf', SysmoBts.REMOTE_DIR))
67 self.run_remote('mk-remote-dir', ('mkdir', '-p', SysmoBts.REMOTE_DIR))
68 self.run_local('scp-inst-to-sysmobts',
69 ('scp', '-r', str(self.inst), '%s@%s:%s' % (self.remote_user, self.remote_addr(), str(self.remote_inst))))
Neels Hofmeyr5356d0a2017-04-10 03:45:30 +020070
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020071 remote_run_dir = self.remote_dir.child(SysmoBts.BTS_SYSMO_BIN)
72 self.run_remote('mk-remote-run-dir', ('mkdir', '-p', remote_run_dir))
Neels Hofmeyr5356d0a2017-04-10 03:45:30 +020073
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020074 remote_config_file = self.remote_dir.child(SysmoBts.BTS_SYSMO_CFG)
75 self.run_local('scp-cfg-to-sysmobts',
76 ('scp', '-r', self.config_file, '%s@%s:%s' % (self.remote_user, self.remote_addr(), remote_config_file)))
Neels Hofmeyr5356d0a2017-04-10 03:45:30 +020077
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020078 self.run_remote('reload-dsp-firmware', ('/bin/sh', '-c', '"cat /lib/firmware/sysmobts-v?.bit > /dev/fpgadl_par0 ; cat /lib/firmware/sysmobts-v?.out > /dev/dspdl_dm644x_0"'))
Neels Hofmeyr96ed9ab2017-05-04 14:30:39 +020079
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020080 remote_lib = self.remote_inst.child('lib')
81 remote_binary = self.remote_inst.child('bin', 'osmo-bts-sysmo')
Pau Espin Pedrolce35d912017-11-23 11:01:24 +010082
83 args = ('LD_LIBRARY_PATH=%s' % remote_lib,
84 remote_binary, '-c', remote_config_file, '-r', '1',
85 '-i', self.bsc.addr())
86
87 if self._direct_pcu_enabled():
88 args += ('-M',)
89
90 self.proc_bts = self.launch_remote('osmo-bts-sysmo', args, remote_cwd=remote_run_dir)
Neels Hofmeyr5356d0a2017-04-10 03:45:30 +020091
Pau Espin Pedrol15aae982017-09-08 13:55:54 +020092 def cleanup(self):
93 pass
94
Pau Espin Pedrolce35d912017-11-23 11:01:24 +010095 def _direct_pcu_enabled(self):
96 return util.str2bool(self.conf.get('direct_pcu'))
97
Pau Espin Pedrol15aae982017-09-08 13:55:54 +020098 def pcu_socket_path(self):
99 return os.path.join(SysmoBts.REMOTE_DIR, 'pcu_bts')
100
Neels Hofmeyr5356d0a2017-04-10 03:45:30 +0200101 def _process_remote(self, name, popen_args, remote_cwd=None):
102 run_dir = self.run_dir.new_dir(name)
Neels Hofmeyrd41dae92017-05-22 19:58:41 +0200103 return process.RemoteProcess(name, run_dir, self.remote_user, self.remote_addr(), remote_cwd,
Neels Hofmeyr5356d0a2017-04-10 03:45:30 +0200104 popen_args)
105
106 def run_remote(self, name, popen_args, remote_cwd=None):
107 proc = self._process_remote(name, popen_args, remote_cwd)
108 proc.launch()
109 proc.wait()
110 if proc.result != 0:
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200111 log.ctx(proc)
112 raise log.Error('Exited in error')
Neels Hofmeyr5356d0a2017-04-10 03:45:30 +0200113
114 def launch_remote(self, name, popen_args, remote_cwd=None):
115 proc = self._process_remote(name, popen_args, remote_cwd)
116 self.suite_run.remember_to_stop(proc)
117 proc.launch()
Pau Espin Pedrold0a01112017-11-28 12:15:10 +0100118 return proc
Neels Hofmeyr5356d0a2017-04-10 03:45:30 +0200119
120 def run_local(self, name, popen_args):
121 run_dir = self.run_dir.new_dir(name)
122 proc = process.Process(name, run_dir, popen_args)
123 proc.launch()
124 proc.wait()
125 if proc.result != 0:
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200126 log.ctx(proc)
127 raise log.Error('Exited in error')
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200128
Pau Espin Pedrolce35d912017-11-23 11:01:24 +0100129 def pcu(self):
130 if self._pcu is None:
131 self._pcu = pcu_sysmo.OsmoPcuSysmo(self.suite_run, self, self.conf)
132 return self._pcu
133
Neels Hofmeyrd41dae92017-05-22 19:58:41 +0200134 def remote_addr(self):
135 return self.conf.get('addr')
136
Pau Espin Pedrolce35d912017-11-23 11:01:24 +0100137 def pcu_socket_path(self):
138 return os.path.join(SysmoBts.REMOTE_DIR, 'pcu_bts')
139
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200140 def configure(self):
Neels Hofmeyrc4ba4a62017-05-18 19:31:44 +0200141 if self.bsc is None:
142 raise RuntimeError('BTS needs to be added to a BSC or NITB before it can be configured')
Neels Hofmeyr5356d0a2017-04-10 03:45:30 +0200143
Neels Hofmeyr5356d0a2017-04-10 03:45:30 +0200144 self.config_file = self.run_dir.new_file(SysmoBts.BTS_SYSMO_CFG)
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200145 self.dbg(config_file=self.config_file)
146
147 values = { 'osmo_bts_sysmo': config.get_defaults('osmo_bts_sysmo') }
148 config.overlay(values, self.suite_run.config())
Pau Espin Pedrol329b6f42017-08-08 13:55:24 +0200149 config.overlay(values, {
150 'osmo_bts_sysmo': {
151 'oml_remote_ip': self.bsc.addr(),
Pau Espin Pedrol15aae982017-09-08 13:55:54 +0200152 'pcu_socket_path': self.pcu_socket_path(),
Pau Espin Pedrol329b6f42017-08-08 13:55:24 +0200153 }
154 })
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200155 config.overlay(values, { 'osmo_bts_sysmo': self.conf })
Neels Hofmeyr943c81d2017-05-22 20:16:03 +0200156
157 self.dbg('SYSMOBTS CONFIG:\n' + pprint.pformat(values))
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200158
159 with open(self.config_file, 'w') as f:
Neels Hofmeyr5356d0a2017-04-10 03:45:30 +0200160 r = template.render(SysmoBts.BTS_SYSMO_CFG, values)
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200161 self.dbg(r)
162 f.write(r)
163
Neels Hofmeyrc4ba4a62017-05-18 19:31:44 +0200164 def conf_for_bsc(self):
165 values = config.get_defaults('bsc_bts')
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200166 config.overlay(values, config.get_defaults('osmo_bts_sysmo'))
Pau Espin Pedrol5e0c2512017-11-06 18:40:23 +0100167 if self.lac is not None:
168 config.overlay(values, { 'location_area_code': self.lac })
Pau Espin Pedrol4ccce7c2017-11-07 11:13:20 +0100169 if self.cellid is not None:
170 config.overlay(values, { 'cell_identity': self.cellid })
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200171 config.overlay(values, self.conf)
Pau Espin Pedrolce35d912017-11-23 11:01:24 +0100172
173 sgsn_conf = {} if self.sgsn is None else self.sgsn.conf_for_client()
174 config.overlay(values, sgsn_conf)
175
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200176 self.dbg(conf=values)
177 return values
178
Pau Espin Pedrold0a01112017-11-28 12:15:10 +0100179 def ready_for_pcu(self):
180 if not self.proc_bts or not self.proc_bts.is_running:
181 return False
182 return 'BTS is up' in (self.proc_bts.get_stderr() or '')
183
Neels Hofmeyrc4ba4a62017-05-18 19:31:44 +0200184 def set_bsc(self, bsc):
185 self.bsc = bsc
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200186
Pau Espin Pedrolce35d912017-11-23 11:01:24 +0100187 def set_sgsn(self, sgsn):
188 self.sgsn = sgsn
189
Pau Espin Pedrol5e0c2512017-11-06 18:40:23 +0100190 def set_lac(self, lac):
191 self.lac = lac
192
Pau Espin Pedrol4ccce7c2017-11-07 11:13:20 +0100193 def set_cellid(self, cellid):
194 self.cellid = cellid
195
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200196# vim: expandtab tabstop=4 shiftwidth=4