blob: bec24334953840c60885839ea88bb056a79cfb51 [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
Holger Hans Peter Freytherd03acdf2018-09-23 18:06:48 +010022from . import log, config, util, process, pcap_recorder, bts, pcu
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010023from . import powersupply
Pau Espin Pedrol9a4631c2018-03-28 19:17:34 +020024from .event_loop import MainLoop
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010025
26class NanoBts(bts.Bts):
27
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010028##############
29# PROTECTED
30##############
31 def __init__(self, suite_run, conf):
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +020032 super().__init__(suite_run, conf, 'nanobts_%s' % conf.get('label', 'nolabel'), 'nanobts')
Pau Espin Pedrol926a4b82018-08-09 12:57:03 +020033 self.pwsup_list = []
34 self._pcu = None
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010035
36 def _configure(self):
37 if self.bsc is None:
38 raise log.Error('BTS needs to be added to a BSC or NITB before it can be configured')
39
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +020040 for trx_i in range(self.num_trx()):
41 pwsup_opt = self.conf.get('trx_list')[trx_i].get('power_supply', {})
42 if not pwsup_opt:
43 raise log.Error('No power_supply attribute provided in conf for TRX %d!' % trx_i)
44 pwsup_type = pwsup_opt.get('type')
45 if not pwsup_type:
46 raise log.Error('No type attribute provided in power_supply conf for TRX %d!' % trx_i)
47 self.pwsup_list.append(powersupply.get_instance_by_type(pwsup_type, pwsup_opt))
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010048
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +020049
50 def get_pcap_filter_all_trx_ip(self):
51 ret = "("
52 for trx_i in range(self.num_trx()):
53 if trx_i != 0:
54 ret = ret + " or "
55 bts_trx_ip = self.conf.get('trx_list')[trx_i].get('addr')
56 ret = ret + "host " + bts_trx_ip
57 ret = ret + ")"
58 return ret
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010059
60########################
61# PUBLIC - INTERNAL API
62########################
63
64 def conf_for_bsc(self):
Pau Espin Pedrole5194622018-05-07 13:36:58 +020065 values = self.conf_for_bsc_prepare()
Pau Espin Pedrolfef9c1c2018-03-27 19:20:47 +020066 # Hack until we have proper ARFCN resource allocation support (OS#2230)
67 band = values.get('band')
68 trx_list = values.get('trx_list')
69 if band == 'GSM-1900':
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +020070 for trx_i in range(len(trx_list)):
71 config.overlay(trx_list[trx_i], { 'arfcn' : str(531 + trx_i * 2) })
Pau Espin Pedrolfef9c1c2018-03-27 19:20:47 +020072 elif band == 'GSM-900':
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +020073 for trx_i in range(len(trx_list)):
74 config.overlay(trx_list[trx_i], { 'arfcn' : str(50 + trx_i * 2) })
Pau Espin Pedrolfef9c1c2018-03-27 19:20:47 +020075
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010076 config.overlay(values, { 'osmobsc_bts_type': 'nanobts' })
77
78 self.dbg(conf=values)
79 return values
80
81
82 def cleanup(self):
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +020083 for pwsup in self.pwsup_list:
84 self.dbg('Powering off NanoBTS TRX')
85 pwsup.power_set(False)
86 self.pwsup_list = []
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010087
88###################
89# PUBLIC (test API included)
90###################
91
Pau Espin Pedrolb1526b92018-05-22 20:32:30 +020092 def start(self, keepalive=False):
Pau Espin Pedrola238ed92018-03-26 13:06:12 +020093 if self.conf.get('ipa_unit_id') is None:
94 raise log.Error('No attribute %s provided in conf!' % attr)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010095 self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name()))
96 self._configure()
97
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010098 unitid = int(self.conf.get('ipa_unit_id'))
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +010099
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +0200100 # Make sure all nanoBTS TRX are powered and in a clean state:
101 for pwsup in self.pwsup_list:
102 self.dbg('Powering cycling NanoBTS TRX')
103 pwsup.power_cycle(1.0)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100104
Pau Espin Pedrola238ed92018-03-26 13:06:12 +0200105 pcap_recorder.PcapRecorder(self.suite_run, self.run_dir.new_dir('pcap'), None,
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +0200106 '%s and port not 22' % self.get_pcap_filter_all_trx_ip())
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100107
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100108
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +0200109 # TODO: If setting N TRX, we should set up them in parallel instead of waiting for each one.
110 for trx_i in range(self.num_trx()):
111 bts_trx_ip = self.conf.get('trx_list')[trx_i].get('addr')
112 # This fine for now, however concurrent tests using Nanobts may run into "address already in use" since dst is broadcast.
113 # 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.
114 local_bind_ip = util.dst_ip_get_local_bind(bts_trx_ip)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100115
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +0200116 self.log('Finding nanobts %s, binding on %s...' % (bts_trx_ip, local_bind_ip))
117 ipfind = AbisIpFind(self.suite_run, self.run_dir, local_bind_ip, 'preconf')
118 ipfind.start()
119 ipfind.wait_bts_ready(bts_trx_ip)
120 running_unitid, running_trx = ipfind.get_unitid_by_ip(bts_trx_ip)
121 self.log('Found nanobts %s with unit_id %d trx %d' % (bts_trx_ip, running_unitid, running_trx))
122 ipfind.stop()
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100123
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +0200124 ipconfig = IpAccessConfig(self.suite_run, self.run_dir, bts_trx_ip)
125 if running_unitid != unitid or running_trx != trx_i:
126 if not ipconfig.set_unit_id(unitid, trx_i, False):
127 raise log.Error('Failed configuring unit id %d trx %d' % (unitid, trx_i))
128 # Apply OML IP and restart nanoBTS as it is required to apply the changes.
129 if not ipconfig.set_oml_ip(self.bsc.addr(), True):
130 raise log.Error('Failed configuring OML IP %s' % bts_trx_ip)
131
132 # Let some time for BTS to restart. It takes much more than 20 secs, and
133 # this way we make sure we don't catch responses in abisip-find prior to
134 # BTS restarting.
135 MainLoop.sleep(self, 20)
136
137 self.log('Starting to connect id %d trx %d to' % (unitid, trx_i), self.bsc)
138 ipfind = AbisIpFind(self.suite_run, self.run_dir, local_bind_ip, 'postconf')
139 ipfind.start()
140 ipfind.wait_bts_ready(bts_trx_ip)
141 self.log('nanoBTS id %d trx %d configured and running' % (unitid, trx_i))
142 ipfind.stop()
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100143
Pau Espin Pedrol9a4631c2018-03-28 19:17:34 +0200144 MainLoop.wait(self, self.bsc.bts_is_connected, self, timeout=600)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100145 self.log('nanoBTS connected to BSC')
146
147 #According to roh, it can be configured to use a static IP in a permanent way:
148 # 1- use abisip-find to find the default address
149 # 2- use ./ipaccess-config --ip-address IP/MASK
150 # 3- use ./ipaccess-config --ip-gateway IP to set the IP of the main unit
151 # 4- use ./ipaccess-config --restart to restart and apply the changes
152
153 #Start must do the following:
154 # 1- use abisip-find to find the default address
155 # 2- use ./ipaccess-config --unit-id UNIT_ID
156 # 3- use ./ipaccess-config --oml-ip --restart to set the IP of the BSC and apply+restart.
157 # According to roh, using the 3 of them together was not reliable to work properly.
158
159 def ready_for_pcu(self):
160 """We don't really care as we use a Dummy PCU class."""
161 return True
162
163 def pcu(self):
164 if not self._pcu:
165 self._pcu = pcu.PcuDummy(self.suite_run, self, self.conf)
166 return self._pcu
167
168
169class AbisIpFind(log.Origin):
170 suite_run = None
171 parent_run_dir = None
172 run_dir = None
173 inst = None
174 env = None
Pau Espin Pedrola238ed92018-03-26 13:06:12 +0200175 bind_ip = None
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100176 proc = None
177
178 BIN_ABISIP_FIND = 'abisip-find'
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +0200179 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 +0100180
Pau Espin Pedrola238ed92018-03-26 13:06:12 +0200181 def __init__(self, suite_run, parent_run_dir, bind_ip, name_suffix):
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100182 super().__init__(log.C_RUN, AbisIpFind.BIN_ABISIP_FIND + '-' + name_suffix)
183 self.suite_run = suite_run
184 self.parent_run_dir = parent_run_dir
Pau Espin Pedrola238ed92018-03-26 13:06:12 +0200185 self.bind_ip = bind_ip
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100186 self.env = {}
187
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100188 def start(self):
189 self.run_dir = util.Dir(self.parent_run_dir.new_dir(self.name()))
190 self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-bsc')))
Pau Espin Pedrola238ed92018-03-26 13:06:12 +0200191
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100192 lib = self.inst.child('lib')
193 if not os.path.isdir(lib):
194 raise log.Error('No lib/ in %r' % self.inst)
195 ipfind_path = self.inst.child('bin', AbisIpFind.BIN_ABISIP_FIND)
Pau Espin Pedrola238ed92018-03-26 13:06:12 +0200196 if not os.path.isfile(ipfind_path):
197 raise RuntimeError('Binary missing: %r' % ipfind_path)
198
199 env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
200 self.proc = process.Process(self.name(), self.run_dir,
Pau Espin Pedrol48fce862018-04-04 12:44:24 +0200201 (ipfind_path, '-i', '1', '-b', self.bind_ip),
Pau Espin Pedrola238ed92018-03-26 13:06:12 +0200202 env=env)
203 self.suite_run.remember_to_stop(self.proc)
204 self.proc.launch()
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100205
206 def stop(self):
207 self.suite_run.stop_process(self.proc)
208
209 def get_line_by_ip(self, ipaddr):
210 """Get latest line (more up to date) from abisip-find based on ip address."""
211 token = "IP_Address='%s'" % ipaddr
212 myline = None
213 for line in (self.proc.get_stdout() or '').splitlines():
214 if token in line:
215 myline = line
216 return myline
217
218 def get_unitid_by_ip(self, ipaddr):
219 line = self.get_line_by_ip(ipaddr)
220 if line is None:
221 return None
222 res = AbisIpFind.BTS_UNIT_ID_RE.search(line)
223 if res:
224 unit_id = int(res.group('unit_id'))
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +0200225 trx_id = int(res.group('trx_id'))
226 return (unit_id, trx_id)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100227 raise log.Error('abisip-find unit_id field for nanobts %s not found in %s' %(ipaddr, line))
228
229 def bts_ready(self, ipaddr):
230 return self.get_line_by_ip(ipaddr) is not None
231
232 def wait_bts_ready(self, ipaddr):
Pau Espin Pedrol9a4631c2018-03-28 19:17:34 +0200233 MainLoop.wait(self, self.bts_ready, ipaddr)
Pau Espin Pedrol48fce862018-04-04 12:44:24 +0200234 # There's a period of time after boot in which nanobts answers to
235 # abisip-find but tcp RSTs ipacces-config conns. Let's wait in here a
236 # bit more time to avoid failing after stating the BTS is ready.
237 MainLoop.sleep(self, 2)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100238
239class IpAccessConfig(log.Origin):
240 suite_run = None
241 parent_run_dir = None
242 run_dir = None
243 inst = None
244 env = None
245 bts_ip = None
246
247 BIN_IPACCESS_CONFIG = 'ipaccess-config'
248
249 def __init__(self, suite_run, parent_run_dir, bts_ip):
250 super().__init__(log.C_RUN, IpAccessConfig.BIN_IPACCESS_CONFIG)
251 self.suite_run = suite_run
252 self.parent_run_dir = parent_run_dir
253 self.bts_ip = bts_ip
254 self.env = {}
255
256 def launch_process(self, binary_name, *args):
257 binary = os.path.abspath(self.inst.child('bin', binary_name))
258 run_dir = self.run_dir.new_dir(binary_name)
259 if not os.path.isfile(binary):
260 raise RuntimeError('Binary missing: %r' % binary)
261 proc = process.Process(binary_name, run_dir,
262 (binary,) + args,
263 env=self.env)
264 proc.launch()
265 return proc
266
267 def run(self, name_suffix, *args):
268 self.run_dir = util.Dir(self.parent_run_dir.new_dir(self.name()+'-'+name_suffix))
269 self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-bsc')))
270 lib = self.inst.child('lib')
271 self.env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
272 self.proc = self.launch_process(IpAccessConfig.BIN_IPACCESS_CONFIG, *args)
273 try:
Pau Espin Pedrol9a4631c2018-03-28 19:17:34 +0200274 MainLoop.wait(self, self.proc.terminated)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100275 except Exception as e:
276 self.proc.terminate()
277 raise e
278 return self.proc.result
279
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +0200280 def set_unit_id(self, unitid, trx_num, restart=False):
281 uid_str = '%d/0/%d' % (unitid, trx_num)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100282 if restart:
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +0200283 retcode = self.run('unitid', '--restart', '--unit-id', '%s' % uid_str, self.bts_ip)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100284 else:
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +0200285 retcode = self.run('unitid', '--unit-id', '%s' % uid_str, self.bts_ip)
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100286 if retcode != 0:
Pau Espin Pedrolf6a07122018-07-27 16:24:29 +0200287 log.err('ipaccess-config --unit-id %s returned error code %d' % (uid_str, retcode))
Pau Espin Pedrol1b28a582018-03-08 21:01:26 +0100288 return retcode == 0
289
290 def set_oml_ip(self, omlip, restart=False):
291 if restart:
292 retcode = self.run('oml', '--restart', '--oml-ip', omlip, self.bts_ip)
293 else:
294 retcode = self.run('oml', '--oml-ip', omlip, self.bts_ip)
295 if retcode != 0:
296 self.error('ipaccess-config --oml-ip %s returned error code %d' % (omlip, retcode))
297 return retcode == 0
298
299# vim: expandtab tabstop=4 shiftwidth=4