blob: bc93a3a50f47d8edb1cc21e1bb59b50efa7d88e1 [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 Pedroldaed4472017-09-15 14:11:35 +020035
36 BIN_BTS_OCTPHY = 'osmo-bts-octphy'
37
38 CONF_BTS_OCTPHY = 'osmo-bts-octphy.cfg'
39
40 def __init__(self, suite_run, conf):
41 super().__init__(log.C_RUN, OsmoBtsOctphy.BIN_BTS_OCTPHY)
42 self.suite_run = suite_run
43 self.conf = conf
44 self.env = {}
45 self.values = {}
46 self.pcu_sk_tmp_dir = tempfile.mkdtemp('', 'ogtpcusk')
47 if len(self.pcu_socket_path().encode()) > 107:
48 raise log.Error('Path for pcu socket is longer than max allowed len for unix socket path (107):', self.pcu_socket_path())
49
50 def cleanup(self):
51 if self.pcu_sk_tmp_dir:
52 try:
53 os.remove(self.pcu_socket_path())
54 except OSError:
55 pass
56 os.rmdir(self.pcu_sk_tmp_dir)
57
58 def pcu_socket_path(self):
59 return os.path.join(self.pcu_sk_tmp_dir, 'pcu_bts')
60
61 def remote_addr(self):
62 return self.conf.get('addr')
63
64 def start(self):
65 if self.bsc is None:
66 raise RuntimeError('BTS needs to be added to a BSC or NITB before it can be started')
67 self.suite_run.poll()
68
69 self.log('Starting to connect to', self.bsc)
70 self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name()))
71 self.configure()
72
73 self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-bts')))
74 btsoct_path = self.inst.child('bin', OsmoBtsOctphy.BIN_BTS_OCTPHY)
75 lib = self.inst.child('lib')
76 if not os.path.isdir(lib):
77 raise RuntimeError('No lib/ in %r' % self.inst)
78
79 # setting capabilities will later disable use of LD_LIBRARY_PATH from ELF loader -> modify RPATH instead.
80 self.log('Setting RPATH for', OsmoBtsOctphy.BIN_BTS_OCTPHY)
81 util.change_elf_rpath(btsoct_path, util.prepend_library_path(lib), self.run_dir.new_dir('patchelf'))
82 # osmo-bty-octphy requires CAP_NET_RAW to open AF_PACKET socket:
83 self.log('Applying CAP_NET_RAW capability to', OsmoBtsOctphy.BIN_BTS_OCTPHY)
84 util.setcap_net_raw(btsoct_path, self.run_dir.new_dir('setcap_net_raw'))
85
86 self.launch_process(OsmoBtsOctphy.BIN_BTS_OCTPHY, '-r', '1',
87 '-c', os.path.abspath(self.config_file),
88 '-i', self.bsc.addr(), '-t', str(self.num_trx()))
89 #self.launch_process(OsmoBtsOctphy.BIN_PCU, '-r', '1')
90 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
171 def set_bsc(self, bsc):
172 self.bsc = bsc
173
Pau Espin Pedrol5e0c2512017-11-06 18:40:23 +0100174 def set_lac(self, lac):
175 self.lac = lac
176
Pau Espin Pedrol4ccce7c2017-11-07 11:13:20 +0100177 def set_cellid(self, cellid):
178 self.cellid = cellid
179
Pau Espin Pedroldaed4472017-09-15 14:11:35 +0200180# vim: expandtab tabstop=4 shiftwidth=4