blob: d9ffd6e066debd7153219b4e5dfbb579d22b5576 [file] [log] [blame]
Pau Espin Pedroldaed4472017-09-15 14:11:35 +02001# osmo_gsm_tester: specifics for running an osmo-bts-octphy
2#
3# Copyright (C) 2016-2017 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
21import pprint
22import tempfile
23from . import log, config, util, template, process, event_loop
24
25class OsmoBtsOctphy(log.Origin):
26 suite_run = None
27 bsc = None
28 run_dir = None
29 inst = None
30 env = None
31 pcu_sk_tmp_dir = None
32 values = 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 Pedroldaed4472017-09-15 14:11:35 +020036
37 BIN_BTS_OCTPHY = 'osmo-bts-octphy'
38
39 CONF_BTS_OCTPHY = 'osmo-bts-octphy.cfg'
40
41 def __init__(self, suite_run, conf):
42 super().__init__(log.C_RUN, OsmoBtsOctphy.BIN_BTS_OCTPHY)
43 self.suite_run = suite_run
44 self.conf = conf
45 self.env = {}
46 self.values = {}
47 self.pcu_sk_tmp_dir = tempfile.mkdtemp('', 'ogtpcusk')
48 if len(self.pcu_socket_path().encode()) > 107:
49 raise log.Error('Path for pcu socket is longer than max allowed len for unix socket path (107):', self.pcu_socket_path())
50
51 def cleanup(self):
52 if self.pcu_sk_tmp_dir:
53 try:
54 os.remove(self.pcu_socket_path())
55 except OSError:
56 pass
57 os.rmdir(self.pcu_sk_tmp_dir)
58
59 def pcu_socket_path(self):
60 return os.path.join(self.pcu_sk_tmp_dir, 'pcu_bts')
61
62 def remote_addr(self):
63 return self.conf.get('addr')
64
65 def start(self):
66 if self.bsc is None:
67 raise RuntimeError('BTS needs to be added to a BSC or NITB before it can be started')
68 self.suite_run.poll()
69
70 self.log('Starting to connect to', self.bsc)
71 self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name()))
72 self.configure()
73
74 self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-bts')))
75 btsoct_path = self.inst.child('bin', OsmoBtsOctphy.BIN_BTS_OCTPHY)
76 lib = self.inst.child('lib')
77 if not os.path.isdir(lib):
78 raise RuntimeError('No lib/ in %r' % self.inst)
79
80 # setting capabilities will later disable use of LD_LIBRARY_PATH from ELF loader -> modify RPATH instead.
81 self.log('Setting RPATH for', OsmoBtsOctphy.BIN_BTS_OCTPHY)
82 util.change_elf_rpath(btsoct_path, util.prepend_library_path(lib), self.run_dir.new_dir('patchelf'))
83 # osmo-bty-octphy requires CAP_NET_RAW to open AF_PACKET socket:
84 self.log('Applying CAP_NET_RAW capability to', OsmoBtsOctphy.BIN_BTS_OCTPHY)
85 util.setcap_net_raw(btsoct_path, self.run_dir.new_dir('setcap_net_raw'))
86
Pau Espin Pedrold0a01112017-11-28 12:15:10 +010087 self.proc_bts = self.launch_process(OsmoBtsOctphy.BIN_BTS_OCTPHY, '-r', '1',
Pau Espin Pedroldaed4472017-09-15 14:11:35 +020088 '-c', os.path.abspath(self.config_file),
89 '-i', self.bsc.addr(), '-t', str(self.num_trx()))
Pau Espin Pedroldaed4472017-09-15 14:11:35 +020090 self.suite_run.poll()
91
92 def launch_process(self, binary_name, *args):
93 binary = os.path.abspath(self.inst.child('bin', binary_name))
94 run_dir = self.run_dir.new_dir(binary_name)
95 if not os.path.isfile(binary):
96 raise RuntimeError('Binary missing: %r' % binary)
97 proc = process.Process(binary_name, run_dir,
98 (binary,) + args,
99 env=self.env)
100 self.suite_run.remember_to_stop(proc)
101 proc.launch()
102 return proc
103
104 def num_trx(self):
105 return len(self.values['osmo_bts_octphy'].get('trx_list', []))
106
107 def allocate_phy_instances(self, c):
108 '''
109 Generate match trx Z <-> phy X inst Y to use in vty config
110
111 We create a new phy for each trx found with a new hwaddr. If hwaddr is
112 already there, increase num_instances and give last instance index to
113 the current trx.
114 '''
115 phy_list = []
116 for trx in c.get('trx_list', []):
117 hwaddr = trx.get('hw_addr', None)
118 netdev = trx.get('net_device', None)
119 if hwaddr is None:
120 raise log.Error('Expected hw-addr value not found!')
121 found = False
122 phy_idx = 0
123 for phy in phy_list:
124 if phy['hw_addr'] == hwaddr:
125 phy['num_instances'] += 1
126 found = True
127 break
128 phy_idx += 1
129 if not found:
130 phy_list.append({'hw_addr': hwaddr, 'net_device': netdev, 'num_instances': 1})
131 trx['phy_idx'] = phy_idx
132 trx['instance_idx'] = phy_list[phy_idx]['num_instances'] - 1
133 c['phy_list'] = phy_list
134
135 def configure(self):
136 if self.bsc is None:
137 raise RuntimeError('BTS needs to be added to a BSC or NITB before it can be configured')
138 self.config_file = self.run_dir.new_file(OsmoBtsOctphy.CONF_BTS_OCTPHY)
139 self.dbg(config_file=self.config_file)
140
141 values = dict(osmo_bts_octphy=config.get_defaults('osmo_bts_octphy'))
142 config.overlay(values, self.suite_run.config())
143 config.overlay(values, {
144 'osmo_bts_octphy': {
145 'oml_remote_ip': self.bsc.addr(),
146 'pcu_socket_path': self.pcu_socket_path(),
147 }
148 })
149 config.overlay(values, { 'osmo_bts_octphy': self.conf })
150
151 self.allocate_phy_instances(values['osmo_bts_octphy'])
152
153 self.dbg('OSMO-BTS-OCTPHY CONFIG:\n' + pprint.pformat(values))
154 self.values = values
155 with open(self.config_file, 'w') as f:
156 r = template.render(OsmoBtsOctphy.CONF_BTS_OCTPHY, values)
157 self.dbg(r)
158 f.write(r)
159
160 def conf_for_bsc(self):
161 values = config.get_defaults('bsc_bts')
162 config.overlay(values, config.get_defaults('osmo_bts_octphy'))
Pau Espin Pedrol5e0c2512017-11-06 18:40:23 +0100163 if self.lac is not None:
164 config.overlay(values, { 'location_area_code': self.lac })
Pau Espin Pedrol4ccce7c2017-11-07 11:13:20 +0100165 if self.cellid is not None:
166 config.overlay(values, { 'cell_identity': self.cellid })
Pau Espin Pedroldaed4472017-09-15 14:11:35 +0200167 config.overlay(values, self.conf)
168 self.dbg(conf=values)
169 return values
170
Pau Espin Pedrold0a01112017-11-28 12:15:10 +0100171 def ready_for_pcu(self):
172 if not self.proc_bts or not self.proc_bts.is_running:
173 return False
174 return 'BTS is up' in (self.proc_bts.get_stderr() or '')
175
Pau Espin Pedroldaed4472017-09-15 14:11:35 +0200176 def set_bsc(self, bsc):
177 self.bsc = bsc
178
Pau Espin Pedrol5e0c2512017-11-06 18:40:23 +0100179 def set_lac(self, lac):
180 self.lac = lac
181
Pau Espin Pedrol4ccce7c2017-11-07 11:13:20 +0100182 def set_cellid(self, cellid):
183 self.cellid = cellid
184
Pau Espin Pedroldaed4472017-09-15 14:11:35 +0200185# vim: expandtab tabstop=4 shiftwidth=4