blob: 3e9b035ac551dad89d868be595f5bc4d0cbd445e [file] [log] [blame]
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +01001# osmo_gsm_tester: specifics for running an ip.access nanoBTS
2#
3# Copyright (C) 2018 by sysmocom - s.f.m.c. GmbH
4#
5# Author: Pau Espin Pedrol <pespin@sysmocom.de>
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as
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
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20import os
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010021import re
Pau Espin Pedrol14e3e502018-11-12 18:44:47 +010022import json
Pau Espin Pedrole1a58bd2020-04-10 20:46:07 +020023from ..core import log, config, util, process
24from ..core.event_loop import MainLoop
Pau Espin Pedrole8bbcbf2020-04-10 19:51:31 +020025from . import pcap_recorder, bts, pcu
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010026from . import powersupply
27
28class NanoBts(bts.Bts):
29
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010030##############
31# PROTECTED
32##############
33 def __init__(self, suite_run, conf):
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +020034 super().__init__(suite_run, conf, 'nanobts_%s' % conf.get('label', 'nolabel'), 'nanobts')
Pau Espin Pedrol926a4b82018-08-09 12:57:03 +020035 self.pwsup_list = []
36 self._pcu = None
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010037
38 def _configure(self):
39 if self.bsc is None:
40 raise log.Error('BTS needs to be added to a BSC or NITB before it can be configured')
41
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +020042 for trx_i in range(self.num_trx()):
43 pwsup_opt = self.conf.get('trx_list')[trx_i].get('power_supply', {})
44 if not pwsup_opt:
45 raise log.Error('No power_supply attribute provided in conf for TRX %d!' % trx_i)
46 pwsup_type = pwsup_opt.get('type')
47 if not pwsup_type:
48 raise log.Error('No type attribute provided in power_supply conf for TRX %d!' % trx_i)
49 self.pwsup_list.append(powersupply.get_instance_by_type(pwsup_type, pwsup_opt))
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010050
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +020051
52 def get_pcap_filter_all_trx_ip(self):
53 ret = "("
54 for trx_i in range(self.num_trx()):
55 if trx_i != 0:
56 ret = ret + " or "
57 bts_trx_ip = self.conf.get('trx_list')[trx_i].get('addr')
58 ret = ret + "host " + bts_trx_ip
59 ret = ret + ")"
60 return ret
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010061
62########################
63# PUBLIC - INTERNAL API
64########################
65
66 def conf_for_bsc(self):
Pau Espin Pedrole5194622018-05-07 13:36:58 +020067 values = self.conf_for_bsc_prepare()
Pau Espin Pedrolfef9c1c2018-03-27 19:20:47 +020068 # Hack until we have proper ARFCN resource allocation support (OS#2230)
69 band = values.get('band')
70 trx_list = values.get('trx_list')
71 if band == 'GSM-1900':
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +020072 for trx_i in range(len(trx_list)):
73 config.overlay(trx_list[trx_i], { 'arfcn' : str(531 + trx_i * 2) })
Pau Espin Pedrolfef9c1c2018-03-27 19:20:47 +020074 elif band == 'GSM-900':
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +020075 for trx_i in range(len(trx_list)):
76 config.overlay(trx_list[trx_i], { 'arfcn' : str(50 + trx_i * 2) })
Pau Espin Pedrolfef9c1c2018-03-27 19:20:47 +020077
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010078 config.overlay(values, { 'osmobsc_bts_type': 'nanobts' })
79
80 self.dbg(conf=values)
81 return values
82
83
84 def cleanup(self):
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +020085 for pwsup in self.pwsup_list:
86 self.dbg('Powering off NanoBTS TRX')
87 pwsup.power_set(False)
88 self.pwsup_list = []
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010089
90###################
91# PUBLIC (test API included)
92###################
93
Pau Espin Pedrolb1526b92018-05-22 20:32:30 +020094 def start(self, keepalive=False):
Pau Espin Pedrola238ed92018-03-26 13:06:12 +020095 if self.conf.get('ipa_unit_id') is None:
Holger Hans Peter Freyther1ea4e522019-02-27 04:34:28 +000096 raise log.Error('No attribute ipa_unit_id provided in conf!')
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010097 self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name()))
98 self._configure()
99
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100100 unitid = int(self.conf.get('ipa_unit_id'))
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100101
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +0200102 # Make sure all nanoBTS TRX are powered and in a clean state:
103 for pwsup in self.pwsup_list:
104 self.dbg('Powering cycling NanoBTS TRX')
105 pwsup.power_cycle(1.0)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100106
Pau Espin Pedrola238ed92018-03-26 13:06:12 +0200107 pcap_recorder.PcapRecorder(self.suite_run, self.run_dir.new_dir('pcap'), None,
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +0200108 '%s and port not 22' % self.get_pcap_filter_all_trx_ip())
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100109
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100110
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +0200111 # TODO: If setting N TRX, we should set up them in parallel instead of waiting for each one.
112 for trx_i in range(self.num_trx()):
113 bts_trx_ip = self.conf.get('trx_list')[trx_i].get('addr')
114 # This fine for now, however concurrent tests using Nanobts may run into "address already in use" since dst is broadcast.
115 # Once concurrency is needed, a new config attr should be added to have an extra static IP assigned on the main-unit to each Nanobts resource.
116 local_bind_ip = util.dst_ip_get_local_bind(bts_trx_ip)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100117
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +0200118 self.log('Finding nanobts %s, binding on %s...' % (bts_trx_ip, local_bind_ip))
119 ipfind = AbisIpFind(self.suite_run, self.run_dir, local_bind_ip, 'preconf')
120 ipfind.start()
121 ipfind.wait_bts_ready(bts_trx_ip)
122 running_unitid, running_trx = ipfind.get_unitid_by_ip(bts_trx_ip)
123 self.log('Found nanobts %s with unit_id %d trx %d' % (bts_trx_ip, running_unitid, running_trx))
124 ipfind.stop()
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100125
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +0200126 ipconfig = IpAccessConfig(self.suite_run, self.run_dir, bts_trx_ip)
Pau Espin Pedrol14e3e502018-11-12 18:44:47 +0100127 running_oml_ip = ipconfig.get_oml_ip()
128
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +0200129 if running_unitid != unitid or running_trx != trx_i:
130 if not ipconfig.set_unit_id(unitid, trx_i, False):
131 raise log.Error('Failed configuring unit id %d trx %d' % (unitid, trx_i))
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +0200132
Pau Espin Pedrol14e3e502018-11-12 18:44:47 +0100133 if running_oml_ip != self.bsc.addr():
134 # Apply OML IP and restart nanoBTS as it is required to apply the changes.
135 self.dbg('Current OML IPaddr "%s" does not match BSC IPaddr "%s", reconfiguring and restarting it' % (running_oml_ip, self.bsc.addr()))
136 if not ipconfig.set_oml_ip(self.bsc.addr(), True):
137 raise log.Error('Failed configuring OML IP %s' % bts_trx_ip)
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +0200138
Pau Espin Pedrol14e3e502018-11-12 18:44:47 +0100139 # Let some time for BTS to restart. It takes much more than 20 secs, and
140 # this way we make sure we don't catch responses in abisip-find prior to
141 # BTS restarting.
142 MainLoop.sleep(self, 20)
143
144 self.dbg('Starting to connect id %d trx %d to' % (unitid, trx_i), self.bsc)
145 ipfind = AbisIpFind(self.suite_run, self.run_dir, local_bind_ip, 'postconf')
146 ipfind.start()
147 ipfind.wait_bts_ready(bts_trx_ip)
148 self.log('nanoBTS id %d trx %d configured and running' % (unitid, trx_i))
149 ipfind.stop()
150 else:
151 self.dbg('nanoBTS id %d trx %d no need to change OML IP (%s) and restart' % (unitid, trx_i, running_oml_ip))
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100152
Pau Espin Pedrol9a4631c2018-03-28 19:17:34 +0200153 MainLoop.wait(self, self.bsc.bts_is_connected, self, timeout=600)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100154 self.log('nanoBTS connected to BSC')
155
156 #According to roh, it can be configured to use a static IP in a permanent way:
157 # 1- use abisip-find to find the default address
158 # 2- use ./ipaccess-config --ip-address IP/MASK
159 # 3- use ./ipaccess-config --ip-gateway IP to set the IP of the main unit
160 # 4- use ./ipaccess-config --restart to restart and apply the changes
161
162 #Start must do the following:
163 # 1- use abisip-find to find the default address
164 # 2- use ./ipaccess-config --unit-id UNIT_ID
165 # 3- use ./ipaccess-config --oml-ip --restart to set the IP of the BSC and apply+restart.
166 # According to roh, using the 3 of them together was not reliable to work properly.
167
168 def ready_for_pcu(self):
169 """We don't really care as we use a Dummy PCU class."""
170 return True
171
172 def pcu(self):
173 if not self._pcu:
174 self._pcu = pcu.PcuDummy(self.suite_run, self, self.conf)
175 return self._pcu
176
177
178class AbisIpFind(log.Origin):
179 suite_run = None
180 parent_run_dir = None
181 run_dir = None
182 inst = None
183 env = None
Pau Espin Pedrola238ed92018-03-26 13:06:12 +0200184 bind_ip = None
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100185 proc = None
186
187 BIN_ABISIP_FIND = 'abisip-find'
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +0200188 BTS_UNIT_ID_RE = re.compile("Unit_ID='(?P<unit_id>\d+)/\d+/(?P<trx_id>\d+)'")
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100189
Pau Espin Pedrola238ed92018-03-26 13:06:12 +0200190 def __init__(self, suite_run, parent_run_dir, bind_ip, name_suffix):
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100191 super().__init__(log.C_RUN, AbisIpFind.BIN_ABISIP_FIND + '-' + name_suffix)
192 self.suite_run = suite_run
193 self.parent_run_dir = parent_run_dir
Pau Espin Pedrola238ed92018-03-26 13:06:12 +0200194 self.bind_ip = bind_ip
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100195 self.env = {}
196
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100197 def start(self):
198 self.run_dir = util.Dir(self.parent_run_dir.new_dir(self.name()))
199 self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-bsc')))
Pau Espin Pedrola238ed92018-03-26 13:06:12 +0200200
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100201 lib = self.inst.child('lib')
202 if not os.path.isdir(lib):
203 raise log.Error('No lib/ in %r' % self.inst)
204 ipfind_path = self.inst.child('bin', AbisIpFind.BIN_ABISIP_FIND)
Pau Espin Pedrola238ed92018-03-26 13:06:12 +0200205 if not os.path.isfile(ipfind_path):
206 raise RuntimeError('Binary missing: %r' % ipfind_path)
207
208 env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
209 self.proc = process.Process(self.name(), self.run_dir,
Pau Espin Pedrol48fce862018-04-04 12:44:24 +0200210 (ipfind_path, '-i', '1', '-b', self.bind_ip),
Pau Espin Pedrola238ed92018-03-26 13:06:12 +0200211 env=env)
212 self.suite_run.remember_to_stop(self.proc)
213 self.proc.launch()
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100214
215 def stop(self):
216 self.suite_run.stop_process(self.proc)
217
218 def get_line_by_ip(self, ipaddr):
219 """Get latest line (more up to date) from abisip-find based on ip address."""
220 token = "IP_Address='%s'" % ipaddr
221 myline = None
222 for line in (self.proc.get_stdout() or '').splitlines():
223 if token in line:
224 myline = line
225 return myline
226
227 def get_unitid_by_ip(self, ipaddr):
228 line = self.get_line_by_ip(ipaddr)
229 if line is None:
230 return None
231 res = AbisIpFind.BTS_UNIT_ID_RE.search(line)
232 if res:
233 unit_id = int(res.group('unit_id'))
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +0200234 trx_id = int(res.group('trx_id'))
235 return (unit_id, trx_id)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100236 raise log.Error('abisip-find unit_id field for nanobts %s not found in %s' %(ipaddr, line))
237
238 def bts_ready(self, ipaddr):
239 return self.get_line_by_ip(ipaddr) is not None
240
241 def wait_bts_ready(self, ipaddr):
Pau Espin Pedrol9a4631c2018-03-28 19:17:34 +0200242 MainLoop.wait(self, self.bts_ready, ipaddr)
Pau Espin Pedrol48fce862018-04-04 12:44:24 +0200243 # There's a period of time after boot in which nanobts answers to
244 # abisip-find but tcp RSTs ipacces-config conns. Let's wait in here a
245 # bit more time to avoid failing after stating the BTS is ready.
246 MainLoop.sleep(self, 2)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100247
248class IpAccessConfig(log.Origin):
249 suite_run = None
250 parent_run_dir = None
251 run_dir = None
252 inst = None
253 env = None
254 bts_ip = None
255
256 BIN_IPACCESS_CONFIG = 'ipaccess-config'
257
258 def __init__(self, suite_run, parent_run_dir, bts_ip):
259 super().__init__(log.C_RUN, IpAccessConfig.BIN_IPACCESS_CONFIG)
260 self.suite_run = suite_run
261 self.parent_run_dir = parent_run_dir
262 self.bts_ip = bts_ip
263 self.env = {}
264
Pau Espin Pedrol1444f552018-11-12 18:23:34 +0100265 def create_process(self, binary_name, *args):
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100266 binary = os.path.abspath(self.inst.child('bin', binary_name))
267 run_dir = self.run_dir.new_dir(binary_name)
268 if not os.path.isfile(binary):
269 raise RuntimeError('Binary missing: %r' % binary)
270 proc = process.Process(binary_name, run_dir,
271 (binary,) + args,
272 env=self.env)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100273 return proc
274
275 def run(self, name_suffix, *args):
276 self.run_dir = util.Dir(self.parent_run_dir.new_dir(self.name()+'-'+name_suffix))
277 self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-bsc')))
278 lib = self.inst.child('lib')
279 self.env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
Pau Espin Pedrol1444f552018-11-12 18:23:34 +0100280 self.proc = self.create_process(IpAccessConfig.BIN_IPACCESS_CONFIG, *args)
281 return self.proc.launch_sync(raise_nonsuccess=False)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100282
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +0200283 def set_unit_id(self, unitid, trx_num, restart=False):
284 uid_str = '%d/0/%d' % (unitid, trx_num)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100285 if restart:
Pau Espin Pedrol14e3e502018-11-12 18:44:47 +0100286 retcode = self.run('setunitid', '--restart', '--unit-id', '%s' % uid_str, self.bts_ip)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100287 else:
Pau Espin Pedrol14e3e502018-11-12 18:44:47 +0100288 retcode = self.run('setunitid', '--unit-id', '%s' % uid_str, self.bts_ip)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100289 if retcode != 0:
Pau Espin Pedrol14e3e502018-11-12 18:44:47 +0100290 self.err('ipaccess-config --unit-id %s returned error code %d' % (uid_str, retcode))
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100291 return retcode == 0
292
293 def set_oml_ip(self, omlip, restart=False):
294 if restart:
Pau Espin Pedrol14e3e502018-11-12 18:44:47 +0100295 retcode = self.run('setoml', '--restart', '--oml-ip', omlip, self.bts_ip)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100296 else:
Pau Espin Pedrol14e3e502018-11-12 18:44:47 +0100297 retcode = self.run('setoml', '--oml-ip', omlip, self.bts_ip)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100298 if retcode != 0:
299 self.error('ipaccess-config --oml-ip %s returned error code %d' % (omlip, retcode))
300 return retcode == 0
301
Pau Espin Pedrol14e3e502018-11-12 18:44:47 +0100302 def get_oml_ip(self):
303 retcode = self.run('getoml', '-q', '-G', self.bts_ip)
304 if retcode != 0:
305 raise log.Error('ipaccess-config -q -G %s returned error code %d' % (self.bts_ip, retcode))
306 output = self.proc.get_stdout()
307 # Our logging system adds "launched on" line at the start, so let's skip until the json code:
308 output_json = output[output.index('{'):]
309 json_data = json.loads(output_json)
310 return json_data['primary_oml_ip']
311
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100312# vim: expandtab tabstop=4 shiftwidth=4