blob: d97ac84a443a252a957094348c3d14307f7620f6 [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
Pau Espin Pedrole1a58bd2020-04-10 20:46:07 +020022from ..core import log, config, util, template, process
Pau Espin Pedrole8bbcbf2020-04-10 19:51:31 +020023from . import 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 Pedrol4b475be2017-12-14 19:01:25 +010026
27##############
28# PROTECTED
29##############
Pau Espin Pedroldaed4472017-09-15 14:11:35 +020030
31 BIN_BTS_OCTPHY = 'osmo-bts-octphy'
Pau Espin Pedroldaed4472017-09-15 14:11:35 +020032 CONF_BTS_OCTPHY = 'osmo-bts-octphy.cfg'
33
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020034 def __init__(self, testenv, conf):
35 super().__init__(testenv, conf, OsmoBtsOctphy.BIN_BTS_OCTPHY, 'osmo_bts_octphy')
Pau Espin Pedrol58603672018-08-09 13:45:55 +020036 self.run_dir = None
37 self.inst = None
Pau Espin Pedroldaed4472017-09-15 14:11:35 +020038 self.env = {}
39 self.values = {}
Pau Espin Pedroldaed4472017-09-15 14:11:35 +020040
Pau Espin Pedroldaed4472017-09-15 14:11:35 +020041 def launch_process(self, binary_name, *args):
42 binary = os.path.abspath(self.inst.child('bin', binary_name))
43 run_dir = self.run_dir.new_dir(binary_name)
44 if not os.path.isfile(binary):
45 raise RuntimeError('Binary missing: %r' % binary)
46 proc = process.Process(binary_name, run_dir,
47 (binary,) + args,
48 env=self.env)
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020049 self.testenv.remember_to_stop(proc)
Pau Espin Pedroldaed4472017-09-15 14:11:35 +020050 proc.launch()
51 return proc
52
Pau Espin Pedroldaed4472017-09-15 14:11:35 +020053 def allocate_phy_instances(self, c):
54 '''
55 Generate match trx Z <-> phy X inst Y to use in vty config
56
57 We create a new phy for each trx found with a new hwaddr. If hwaddr is
58 already there, increase num_instances and give last instance index to
59 the current trx.
60 '''
61 phy_list = []
62 for trx in c.get('trx_list', []):
63 hwaddr = trx.get('hw_addr', None)
64 netdev = trx.get('net_device', None)
65 if hwaddr is None:
66 raise log.Error('Expected hw-addr value not found!')
67 found = False
68 phy_idx = 0
69 for phy in phy_list:
70 if phy['hw_addr'] == hwaddr:
71 phy['num_instances'] += 1
72 found = True
73 break
74 phy_idx += 1
75 if not found:
76 phy_list.append({'hw_addr': hwaddr, 'net_device': netdev, 'num_instances': 1})
77 trx['phy_idx'] = phy_idx
78 trx['instance_idx'] = phy_list[phy_idx]['num_instances'] - 1
79 c['phy_list'] = phy_list
80
81 def configure(self):
82 if self.bsc is None:
83 raise RuntimeError('BTS needs to be added to a BSC or NITB before it can be configured')
84 self.config_file = self.run_dir.new_file(OsmoBtsOctphy.CONF_BTS_OCTPHY)
85 self.dbg(config_file=self.config_file)
86
87 values = dict(osmo_bts_octphy=config.get_defaults('osmo_bts_octphy'))
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020088 config.overlay(values, self.testenv.suite().config())
Pau Espin Pedroldaed4472017-09-15 14:11:35 +020089 config.overlay(values, {
90 'osmo_bts_octphy': {
91 'oml_remote_ip': self.bsc.addr(),
92 'pcu_socket_path': self.pcu_socket_path(),
93 }
94 })
95 config.overlay(values, { 'osmo_bts_octphy': self.conf })
96
97 self.allocate_phy_instances(values['osmo_bts_octphy'])
98
99 self.dbg('OSMO-BTS-OCTPHY CONFIG:\n' + pprint.pformat(values))
100 self.values = values
101 with open(self.config_file, 'w') as f:
102 r = template.render(OsmoBtsOctphy.CONF_BTS_OCTPHY, values)
103 self.dbg(r)
104 f.write(r)
105
Pau Espin Pedrol4b475be2017-12-14 19:01:25 +0100106########################
107# PUBLIC - INTERNAL API
108########################
Pau Espin Pedroldaed4472017-09-15 14:11:35 +0200109 def conf_for_bsc(self):
Pau Espin Pedrole5194622018-05-07 13:36:58 +0200110 values = self.conf_for_bsc_prepare()
Pau Espin Pedroldaed4472017-09-15 14:11:35 +0200111 self.dbg(conf=values)
112 return values
113
Pau Espin Pedrol4b475be2017-12-14 19:01:25 +0100114###################
115# PUBLIC (test API included)
116###################
117 def start(self):
118 if self.bsc is None:
119 raise RuntimeError('BTS needs to be added to a BSC or NITB before it can be started')
Pau Espin Pedrola442cb82020-05-05 12:54:37 +0200120 self.testenv.poll()
Pau Espin Pedrol4b475be2017-12-14 19:01:25 +0100121
122 self.log('Starting to connect to', self.bsc)
Pau Espin Pedrol2a2d8462020-05-11 10:56:52 +0200123 self.run_dir = util.Dir(self.testenv.test().get_run_dir().new_dir(self.name()))
Pau Espin Pedrol4b475be2017-12-14 19:01:25 +0100124 self.configure()
125
Pau Espin Pedrola442cb82020-05-05 12:54:37 +0200126 self.inst = util.Dir(os.path.abspath(self.testenv.suite().trial().get_inst('osmo-bts')))
Pau Espin Pedrol4b475be2017-12-14 19:01:25 +0100127 btsoct_path = self.inst.child('bin', OsmoBtsOctphy.BIN_BTS_OCTPHY)
128 lib = self.inst.child('lib')
129 if not os.path.isdir(lib):
130 raise RuntimeError('No lib/ in %r' % self.inst)
131
132 # setting capabilities will later disable use of LD_LIBRARY_PATH from ELF loader -> modify RPATH instead.
133 self.log('Setting RPATH for', OsmoBtsOctphy.BIN_BTS_OCTPHY)
134 util.change_elf_rpath(btsoct_path, util.prepend_library_path(lib), self.run_dir.new_dir('patchelf'))
135 # osmo-bty-octphy requires CAP_NET_RAW to open AF_PACKET socket:
136 self.log('Applying CAP_NET_RAW capability to', OsmoBtsOctphy.BIN_BTS_OCTPHY)
137 util.setcap_net_raw(btsoct_path, self.run_dir.new_dir('setcap_net_raw'))
138
139 self.proc_bts = self.launch_process(OsmoBtsOctphy.BIN_BTS_OCTPHY, '-r', '1',
140 '-c', os.path.abspath(self.config_file),
141 '-i', self.bsc.addr(), '-t', str(self.num_trx()))
Pau Espin Pedrola442cb82020-05-05 12:54:37 +0200142 self.testenv.poll()
Pau Espin Pedrol4b475be2017-12-14 19:01:25 +0100143
Pau Espin Pedroldaed4472017-09-15 14:11:35 +0200144# vim: expandtab tabstop=4 shiftwidth=4