blob: fd8d078690dd564d2a1697b3eb428451089a0956 [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
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +010023from . import log, config, util, template, process, event_loop, pcu_osmo, bts_osmo
Pau Espin Pedroldaed4472017-09-15 14:11:35 +020024
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +010025class OsmoBtsOctphy(bts_osmo.OsmoBtsMainUnit):
Pau Espin Pedroldaed4472017-09-15 14:11:35 +020026 run_dir = None
27 inst = None
28 env = None
Pau Espin Pedroldaed4472017-09-15 14:11:35 +020029
30 BIN_BTS_OCTPHY = 'osmo-bts-octphy'
31
32 CONF_BTS_OCTPHY = 'osmo-bts-octphy.cfg'
33
34 def __init__(self, suite_run, conf):
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +010035 super().__init__(suite_run, conf, OsmoBtsOctphy.BIN_BTS_OCTPHY)
Pau Espin Pedroldaed4472017-09-15 14:11:35 +020036 self.env = {}
37 self.values = {}
Pau Espin Pedroldaed4472017-09-15 14:11:35 +020038
39 def start(self):
40 if self.bsc is None:
41 raise RuntimeError('BTS needs to be added to a BSC or NITB before it can be started')
42 self.suite_run.poll()
43
44 self.log('Starting to connect to', self.bsc)
45 self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name()))
46 self.configure()
47
48 self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-bts')))
49 btsoct_path = self.inst.child('bin', OsmoBtsOctphy.BIN_BTS_OCTPHY)
50 lib = self.inst.child('lib')
51 if not os.path.isdir(lib):
52 raise RuntimeError('No lib/ in %r' % self.inst)
53
54 # setting capabilities will later disable use of LD_LIBRARY_PATH from ELF loader -> modify RPATH instead.
55 self.log('Setting RPATH for', OsmoBtsOctphy.BIN_BTS_OCTPHY)
56 util.change_elf_rpath(btsoct_path, util.prepend_library_path(lib), self.run_dir.new_dir('patchelf'))
57 # osmo-bty-octphy requires CAP_NET_RAW to open AF_PACKET socket:
58 self.log('Applying CAP_NET_RAW capability to', OsmoBtsOctphy.BIN_BTS_OCTPHY)
59 util.setcap_net_raw(btsoct_path, self.run_dir.new_dir('setcap_net_raw'))
60
Pau Espin Pedrold0a01112017-11-28 12:15:10 +010061 self.proc_bts = self.launch_process(OsmoBtsOctphy.BIN_BTS_OCTPHY, '-r', '1',
Pau Espin Pedroldaed4472017-09-15 14:11:35 +020062 '-c', os.path.abspath(self.config_file),
63 '-i', self.bsc.addr(), '-t', str(self.num_trx()))
Pau Espin Pedroldaed4472017-09-15 14:11:35 +020064 self.suite_run.poll()
65
66 def launch_process(self, binary_name, *args):
67 binary = os.path.abspath(self.inst.child('bin', binary_name))
68 run_dir = self.run_dir.new_dir(binary_name)
69 if not os.path.isfile(binary):
70 raise RuntimeError('Binary missing: %r' % binary)
71 proc = process.Process(binary_name, run_dir,
72 (binary,) + args,
73 env=self.env)
74 self.suite_run.remember_to_stop(proc)
75 proc.launch()
76 return proc
77
78 def num_trx(self):
79 return len(self.values['osmo_bts_octphy'].get('trx_list', []))
80
81 def allocate_phy_instances(self, c):
82 '''
83 Generate match trx Z <-> phy X inst Y to use in vty config
84
85 We create a new phy for each trx found with a new hwaddr. If hwaddr is
86 already there, increase num_instances and give last instance index to
87 the current trx.
88 '''
89 phy_list = []
90 for trx in c.get('trx_list', []):
91 hwaddr = trx.get('hw_addr', None)
92 netdev = trx.get('net_device', None)
93 if hwaddr is None:
94 raise log.Error('Expected hw-addr value not found!')
95 found = False
96 phy_idx = 0
97 for phy in phy_list:
98 if phy['hw_addr'] == hwaddr:
99 phy['num_instances'] += 1
100 found = True
101 break
102 phy_idx += 1
103 if not found:
104 phy_list.append({'hw_addr': hwaddr, 'net_device': netdev, 'num_instances': 1})
105 trx['phy_idx'] = phy_idx
106 trx['instance_idx'] = phy_list[phy_idx]['num_instances'] - 1
107 c['phy_list'] = phy_list
108
109 def configure(self):
110 if self.bsc is None:
111 raise RuntimeError('BTS needs to be added to a BSC or NITB before it can be configured')
112 self.config_file = self.run_dir.new_file(OsmoBtsOctphy.CONF_BTS_OCTPHY)
113 self.dbg(config_file=self.config_file)
114
115 values = dict(osmo_bts_octphy=config.get_defaults('osmo_bts_octphy'))
116 config.overlay(values, self.suite_run.config())
117 config.overlay(values, {
118 'osmo_bts_octphy': {
119 'oml_remote_ip': self.bsc.addr(),
120 'pcu_socket_path': self.pcu_socket_path(),
121 }
122 })
123 config.overlay(values, { 'osmo_bts_octphy': self.conf })
124
125 self.allocate_phy_instances(values['osmo_bts_octphy'])
126
127 self.dbg('OSMO-BTS-OCTPHY CONFIG:\n' + pprint.pformat(values))
128 self.values = values
129 with open(self.config_file, 'w') as f:
130 r = template.render(OsmoBtsOctphy.CONF_BTS_OCTPHY, values)
131 self.dbg(r)
132 f.write(r)
133
134 def conf_for_bsc(self):
135 values = config.get_defaults('bsc_bts')
136 config.overlay(values, config.get_defaults('osmo_bts_octphy'))
Pau Espin Pedrol5e0c2512017-11-06 18:40:23 +0100137 if self.lac is not None:
138 config.overlay(values, { 'location_area_code': self.lac })
Pau Espin Pedrol8a3a7b52017-11-28 15:50:02 +0100139 if self.rac is not None:
140 config.overlay(values, { 'routing_area_code': self.rac })
Pau Espin Pedrol4ccce7c2017-11-07 11:13:20 +0100141 if self.cellid is not None:
142 config.overlay(values, { 'cell_identity': self.cellid })
Pau Espin Pedrol8a3a7b52017-11-28 15:50:02 +0100143 if self.bvci is not None:
144 config.overlay(values, { 'bvci': self.bvci })
Pau Espin Pedroldaed4472017-09-15 14:11:35 +0200145 config.overlay(values, self.conf)
Pau Espin Pedrol05314b42017-11-23 13:35:42 +0100146
147 sgsn_conf = {} if self.sgsn is None else self.sgsn.conf_for_client()
148 config.overlay(values, sgsn_conf)
149
Pau Espin Pedroldaed4472017-09-15 14:11:35 +0200150 self.dbg(conf=values)
151 return values
152
Pau Espin Pedroldaed4472017-09-15 14:11:35 +0200153# vim: expandtab tabstop=4 shiftwidth=4