blob: 5fe8bae4ae7ac83287217ccca3e3f2bcb17cea42 [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
Holger Hans Peter Freytherd03acdf2018-09-23 18:06:48 +010023from . import log, config, util, process, pcap_recorder, bts, pcu
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010024from . import powersupply
Pau Espin Pedrol9a4631c2018-03-28 19:17:34 +020025from .event_loop import MainLoop
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010026
27class NanoBts(bts.Bts):
28
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010029##############
30# PROTECTED
31##############
32 def __init__(self, suite_run, conf):
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +020033 super().__init__(suite_run, conf, 'nanobts_%s' % conf.get('label', 'nolabel'), 'nanobts')
Pau Espin Pedrol926a4b82018-08-09 12:57:03 +020034 self.pwsup_list = []
35 self._pcu = None
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010036
37 def _configure(self):
38 if self.bsc is None:
39 raise log.Error('BTS needs to be added to a BSC or NITB before it can be configured')
40
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +020041 for trx_i in range(self.num_trx()):
42 pwsup_opt = self.conf.get('trx_list')[trx_i].get('power_supply', {})
43 if not pwsup_opt:
44 raise log.Error('No power_supply attribute provided in conf for TRX %d!' % trx_i)
45 pwsup_type = pwsup_opt.get('type')
46 if not pwsup_type:
47 raise log.Error('No type attribute provided in power_supply conf for TRX %d!' % trx_i)
48 self.pwsup_list.append(powersupply.get_instance_by_type(pwsup_type, pwsup_opt))
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010049
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +020050
51 def get_pcap_filter_all_trx_ip(self):
52 ret = "("
53 for trx_i in range(self.num_trx()):
54 if trx_i != 0:
55 ret = ret + " or "
56 bts_trx_ip = self.conf.get('trx_list')[trx_i].get('addr')
57 ret = ret + "host " + bts_trx_ip
58 ret = ret + ")"
59 return ret
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010060
61########################
62# PUBLIC - INTERNAL API
63########################
64
65 def conf_for_bsc(self):
Pau Espin Pedrole5194622018-05-07 13:36:58 +020066 values = self.conf_for_bsc_prepare()
Pau Espin Pedrolfef9c1c2018-03-27 19:20:47 +020067 # Hack until we have proper ARFCN resource allocation support (OS#2230)
68 band = values.get('band')
69 trx_list = values.get('trx_list')
70 if band == 'GSM-1900':
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +020071 for trx_i in range(len(trx_list)):
72 config.overlay(trx_list[trx_i], { 'arfcn' : str(531 + trx_i * 2) })
Pau Espin Pedrolfef9c1c2018-03-27 19:20:47 +020073 elif band == 'GSM-900':
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +020074 for trx_i in range(len(trx_list)):
75 config.overlay(trx_list[trx_i], { 'arfcn' : str(50 + trx_i * 2) })
Pau Espin Pedrolfef9c1c2018-03-27 19:20:47 +020076
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010077 config.overlay(values, { 'osmobsc_bts_type': 'nanobts' })
78
79 self.dbg(conf=values)
80 return values
81
82
83 def cleanup(self):
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +020084 for pwsup in self.pwsup_list:
85 self.dbg('Powering off NanoBTS TRX')
86 pwsup.power_set(False)
87 self.pwsup_list = []
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010088
89###################
90# PUBLIC (test API included)
91###################
92
Pau Espin Pedrolb1526b92018-05-22 20:32:30 +020093 def start(self, keepalive=False):
Pau Espin Pedrola238ed92018-03-26 13:06:12 +020094 if self.conf.get('ipa_unit_id') is None:
95 raise log.Error('No attribute %s provided in conf!' % attr)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010096 self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name()))
97 self._configure()
98
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010099 unitid = int(self.conf.get('ipa_unit_id'))
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100100
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +0200101 # Make sure all nanoBTS TRX are powered and in a clean state:
102 for pwsup in self.pwsup_list:
103 self.dbg('Powering cycling NanoBTS TRX')
104 pwsup.power_cycle(1.0)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100105
Pau Espin Pedrola238ed92018-03-26 13:06:12 +0200106 pcap_recorder.PcapRecorder(self.suite_run, self.run_dir.new_dir('pcap'), None,
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +0200107 '%s and port not 22' % self.get_pcap_filter_all_trx_ip())
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100108
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100109
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +0200110 # TODO: If setting N TRX, we should set up them in parallel instead of waiting for each one.
111 for trx_i in range(self.num_trx()):
112 bts_trx_ip = self.conf.get('trx_list')[trx_i].get('addr')
113 # This fine for now, however concurrent tests using Nanobts may run into "address already in use" since dst is broadcast.
114 # 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.
115 local_bind_ip = util.dst_ip_get_local_bind(bts_trx_ip)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100116
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +0200117 self.log('Finding nanobts %s, binding on %s...' % (bts_trx_ip, local_bind_ip))
118 ipfind = AbisIpFind(self.suite_run, self.run_dir, local_bind_ip, 'preconf')
119 ipfind.start()
120 ipfind.wait_bts_ready(bts_trx_ip)
121 running_unitid, running_trx = ipfind.get_unitid_by_ip(bts_trx_ip)
122 self.log('Found nanobts %s with unit_id %d trx %d' % (bts_trx_ip, running_unitid, running_trx))
123 ipfind.stop()
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100124
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +0200125 ipconfig = IpAccessConfig(self.suite_run, self.run_dir, bts_trx_ip)
Pau Espin Pedrol14e3e502018-11-12 18:44:47 +0100126 running_oml_ip = ipconfig.get_oml_ip()
127
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +0200128 if running_unitid != unitid or running_trx != trx_i:
129 if not ipconfig.set_unit_id(unitid, trx_i, False):
130 raise log.Error('Failed configuring unit id %d trx %d' % (unitid, trx_i))
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +0200131
Pau Espin Pedrol14e3e502018-11-12 18:44:47 +0100132 if running_oml_ip != self.bsc.addr():
133 # Apply OML IP and restart nanoBTS as it is required to apply the changes.
134 self.dbg('Current OML IPaddr "%s" does not match BSC IPaddr "%s", reconfiguring and restarting it' % (running_oml_ip, self.bsc.addr()))
135 if not ipconfig.set_oml_ip(self.bsc.addr(), True):
136 raise log.Error('Failed configuring OML IP %s' % bts_trx_ip)
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +0200137
Pau Espin Pedrol14e3e502018-11-12 18:44:47 +0100138 # Let some time for BTS to restart. It takes much more than 20 secs, and
139 # this way we make sure we don't catch responses in abisip-find prior to
140 # BTS restarting.
141 MainLoop.sleep(self, 20)
142
143 self.dbg('Starting to connect id %d trx %d to' % (unitid, trx_i), self.bsc)
144 ipfind = AbisIpFind(self.suite_run, self.run_dir, local_bind_ip, 'postconf')
145 ipfind.start()
146 ipfind.wait_bts_ready(bts_trx_ip)
147 self.log('nanoBTS id %d trx %d configured and running' % (unitid, trx_i))
148 ipfind.stop()
149 else:
150 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 +0100151
Pau Espin Pedrol9a4631c2018-03-28 19:17:34 +0200152 MainLoop.wait(self, self.bsc.bts_is_connected, self, timeout=600)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100153 self.log('nanoBTS connected to BSC')
154
155 #According to roh, it can be configured to use a static IP in a permanent way:
156 # 1- use abisip-find to find the default address
157 # 2- use ./ipaccess-config --ip-address IP/MASK
158 # 3- use ./ipaccess-config --ip-gateway IP to set the IP of the main unit
159 # 4- use ./ipaccess-config --restart to restart and apply the changes
160
161 #Start must do the following:
162 # 1- use abisip-find to find the default address
163 # 2- use ./ipaccess-config --unit-id UNIT_ID
164 # 3- use ./ipaccess-config --oml-ip --restart to set the IP of the BSC and apply+restart.
165 # According to roh, using the 3 of them together was not reliable to work properly.
166
167 def ready_for_pcu(self):
168 """We don't really care as we use a Dummy PCU class."""
169 return True
170
171 def pcu(self):
172 if not self._pcu:
173 self._pcu = pcu.PcuDummy(self.suite_run, self, self.conf)
174 return self._pcu
175
176
177class AbisIpFind(log.Origin):
178 suite_run = None
179 parent_run_dir = None
180 run_dir = None
181 inst = None
182 env = None
Pau Espin Pedrola238ed92018-03-26 13:06:12 +0200183 bind_ip = None
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100184 proc = None
185
186 BIN_ABISIP_FIND = 'abisip-find'
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +0200187 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 +0100188
Pau Espin Pedrola238ed92018-03-26 13:06:12 +0200189 def __init__(self, suite_run, parent_run_dir, bind_ip, name_suffix):
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100190 super().__init__(log.C_RUN, AbisIpFind.BIN_ABISIP_FIND + '-' + name_suffix)
191 self.suite_run = suite_run
192 self.parent_run_dir = parent_run_dir
Pau Espin Pedrola238ed92018-03-26 13:06:12 +0200193 self.bind_ip = bind_ip
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100194 self.env = {}
195
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100196 def start(self):
197 self.run_dir = util.Dir(self.parent_run_dir.new_dir(self.name()))
198 self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-bsc')))
Pau Espin Pedrola238ed92018-03-26 13:06:12 +0200199
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100200 lib = self.inst.child('lib')
201 if not os.path.isdir(lib):
202 raise log.Error('No lib/ in %r' % self.inst)
203 ipfind_path = self.inst.child('bin', AbisIpFind.BIN_ABISIP_FIND)
Pau Espin Pedrola238ed92018-03-26 13:06:12 +0200204 if not os.path.isfile(ipfind_path):
205 raise RuntimeError('Binary missing: %r' % ipfind_path)
206
207 env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
208 self.proc = process.Process(self.name(), self.run_dir,
Pau Espin Pedrol48fce862018-04-04 12:44:24 +0200209 (ipfind_path, '-i', '1', '-b', self.bind_ip),
Pau Espin Pedrola238ed92018-03-26 13:06:12 +0200210 env=env)
211 self.suite_run.remember_to_stop(self.proc)
212 self.proc.launch()
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100213
214 def stop(self):
215 self.suite_run.stop_process(self.proc)
216
217 def get_line_by_ip(self, ipaddr):
218 """Get latest line (more up to date) from abisip-find based on ip address."""
219 token = "IP_Address='%s'" % ipaddr
220 myline = None
221 for line in (self.proc.get_stdout() or '').splitlines():
222 if token in line:
223 myline = line
224 return myline
225
226 def get_unitid_by_ip(self, ipaddr):
227 line = self.get_line_by_ip(ipaddr)
228 if line is None:
229 return None
230 res = AbisIpFind.BTS_UNIT_ID_RE.search(line)
231 if res:
232 unit_id = int(res.group('unit_id'))
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +0200233 trx_id = int(res.group('trx_id'))
234 return (unit_id, trx_id)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100235 raise log.Error('abisip-find unit_id field for nanobts %s not found in %s' %(ipaddr, line))
236
237 def bts_ready(self, ipaddr):
238 return self.get_line_by_ip(ipaddr) is not None
239
240 def wait_bts_ready(self, ipaddr):
Pau Espin Pedrol9a4631c2018-03-28 19:17:34 +0200241 MainLoop.wait(self, self.bts_ready, ipaddr)
Pau Espin Pedrol48fce862018-04-04 12:44:24 +0200242 # There's a period of time after boot in which nanobts answers to
243 # abisip-find but tcp RSTs ipacces-config conns. Let's wait in here a
244 # bit more time to avoid failing after stating the BTS is ready.
245 MainLoop.sleep(self, 2)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100246
247class IpAccessConfig(log.Origin):
248 suite_run = None
249 parent_run_dir = None
250 run_dir = None
251 inst = None
252 env = None
253 bts_ip = None
254
255 BIN_IPACCESS_CONFIG = 'ipaccess-config'
256
257 def __init__(self, suite_run, parent_run_dir, bts_ip):
258 super().__init__(log.C_RUN, IpAccessConfig.BIN_IPACCESS_CONFIG)
259 self.suite_run = suite_run
260 self.parent_run_dir = parent_run_dir
261 self.bts_ip = bts_ip
262 self.env = {}
263
Pau Espin Pedrol1444f552018-11-12 18:23:34 +0100264 def create_process(self, binary_name, *args):
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100265 binary = os.path.abspath(self.inst.child('bin', binary_name))
266 run_dir = self.run_dir.new_dir(binary_name)
267 if not os.path.isfile(binary):
268 raise RuntimeError('Binary missing: %r' % binary)
269 proc = process.Process(binary_name, run_dir,
270 (binary,) + args,
271 env=self.env)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100272 return proc
273
274 def run(self, name_suffix, *args):
275 self.run_dir = util.Dir(self.parent_run_dir.new_dir(self.name()+'-'+name_suffix))
276 self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-bsc')))
277 lib = self.inst.child('lib')
278 self.env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
Pau Espin Pedrol1444f552018-11-12 18:23:34 +0100279 self.proc = self.create_process(IpAccessConfig.BIN_IPACCESS_CONFIG, *args)
280 return self.proc.launch_sync(raise_nonsuccess=False)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100281
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +0200282 def set_unit_id(self, unitid, trx_num, restart=False):
283 uid_str = '%d/0/%d' % (unitid, trx_num)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100284 if restart:
Pau Espin Pedrol14e3e502018-11-12 18:44:47 +0100285 retcode = self.run('setunitid', '--restart', '--unit-id', '%s' % uid_str, self.bts_ip)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100286 else:
Pau Espin Pedrol14e3e502018-11-12 18:44:47 +0100287 retcode = self.run('setunitid', '--unit-id', '%s' % uid_str, self.bts_ip)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100288 if retcode != 0:
Pau Espin Pedrol14e3e502018-11-12 18:44:47 +0100289 self.err('ipaccess-config --unit-id %s returned error code %d' % (uid_str, retcode))
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100290 return retcode == 0
291
292 def set_oml_ip(self, omlip, restart=False):
293 if restart:
Pau Espin Pedrol14e3e502018-11-12 18:44:47 +0100294 retcode = self.run('setoml', '--restart', '--oml-ip', omlip, self.bts_ip)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100295 else:
Pau Espin Pedrol14e3e502018-11-12 18:44:47 +0100296 retcode = self.run('setoml', '--oml-ip', omlip, self.bts_ip)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100297 if retcode != 0:
298 self.error('ipaccess-config --oml-ip %s returned error code %d' % (omlip, retcode))
299 return retcode == 0
300
Pau Espin Pedrol14e3e502018-11-12 18:44:47 +0100301 def get_oml_ip(self):
302 retcode = self.run('getoml', '-q', '-G', self.bts_ip)
303 if retcode != 0:
304 raise log.Error('ipaccess-config -q -G %s returned error code %d' % (self.bts_ip, retcode))
305 output = self.proc.get_stdout()
306 # Our logging system adds "launched on" line at the start, so let's skip until the json code:
307 output_json = output[output.index('{'):]
308 json_data = json.loads(output_json)
309 return json_data['primary_oml_ip']
310
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100311# vim: expandtab tabstop=4 shiftwidth=4