blob: 318c30d86b1de7c864c7a67320e30ad60e1b7fb2 [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 Pedrol9a4631c2018-03-28 19:17:34 +020023from . import log, config, util, template, process, 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 Pedrol4b475be2017-12-14 19:01:25 +010026
27##############
28# PROTECTED
29##############
Pau Espin Pedroldaed4472017-09-15 14:11:35 +020030 run_dir = None
31 inst = None
32 env = None
Pau Espin Pedroldaed4472017-09-15 14:11:35 +020033
34 BIN_BTS_OCTPHY = 'osmo-bts-octphy'
35
36 CONF_BTS_OCTPHY = 'osmo-bts-octphy.cfg'
37
38 def __init__(self, suite_run, conf):
Pau Espin Pedrole5194622018-05-07 13:36:58 +020039 super().__init__(suite_run, conf, OsmoBtsOctphy.BIN_BTS_OCTPHY, 'osmo_bts_octphy')
Pau Espin Pedroldaed4472017-09-15 14:11:35 +020040 self.env = {}
41 self.values = {}
Pau Espin Pedroldaed4472017-09-15 14:11:35 +020042
Pau Espin Pedroldaed4472017-09-15 14:11:35 +020043 def launch_process(self, binary_name, *args):
44 binary = os.path.abspath(self.inst.child('bin', binary_name))
45 run_dir = self.run_dir.new_dir(binary_name)
46 if not os.path.isfile(binary):
47 raise RuntimeError('Binary missing: %r' % binary)
48 proc = process.Process(binary_name, run_dir,
49 (binary,) + args,
50 env=self.env)
51 self.suite_run.remember_to_stop(proc)
52 proc.launch()
53 return proc
54
Pau Espin Pedroldaed4472017-09-15 14:11:35 +020055 def allocate_phy_instances(self, c):
56 '''
57 Generate match trx Z <-> phy X inst Y to use in vty config
58
59 We create a new phy for each trx found with a new hwaddr. If hwaddr is
60 already there, increase num_instances and give last instance index to
61 the current trx.
62 '''
63 phy_list = []
64 for trx in c.get('trx_list', []):
65 hwaddr = trx.get('hw_addr', None)
66 netdev = trx.get('net_device', None)
67 if hwaddr is None:
68 raise log.Error('Expected hw-addr value not found!')
69 found = False
70 phy_idx = 0
71 for phy in phy_list:
72 if phy['hw_addr'] == hwaddr:
73 phy['num_instances'] += 1
74 found = True
75 break
76 phy_idx += 1
77 if not found:
78 phy_list.append({'hw_addr': hwaddr, 'net_device': netdev, 'num_instances': 1})
79 trx['phy_idx'] = phy_idx
80 trx['instance_idx'] = phy_list[phy_idx]['num_instances'] - 1
81 c['phy_list'] = phy_list
82
83 def configure(self):
84 if self.bsc is None:
85 raise RuntimeError('BTS needs to be added to a BSC or NITB before it can be configured')
86 self.config_file = self.run_dir.new_file(OsmoBtsOctphy.CONF_BTS_OCTPHY)
87 self.dbg(config_file=self.config_file)
88
89 values = dict(osmo_bts_octphy=config.get_defaults('osmo_bts_octphy'))
90 config.overlay(values, self.suite_run.config())
91 config.overlay(values, {
92 'osmo_bts_octphy': {
93 'oml_remote_ip': self.bsc.addr(),
94 'pcu_socket_path': self.pcu_socket_path(),
95 }
96 })
97 config.overlay(values, { 'osmo_bts_octphy': self.conf })
98
99 self.allocate_phy_instances(values['osmo_bts_octphy'])
100
101 self.dbg('OSMO-BTS-OCTPHY CONFIG:\n' + pprint.pformat(values))
102 self.values = values
103 with open(self.config_file, 'w') as f:
104 r = template.render(OsmoBtsOctphy.CONF_BTS_OCTPHY, values)
105 self.dbg(r)
106 f.write(r)
107
Pau Espin Pedrol4b475be2017-12-14 19:01:25 +0100108########################
109# PUBLIC - INTERNAL API
110########################
Pau Espin Pedroldaed4472017-09-15 14:11:35 +0200111 def conf_for_bsc(self):
Pau Espin Pedrole5194622018-05-07 13:36:58 +0200112 values = self.conf_for_bsc_prepare()
Pau Espin Pedroldaed4472017-09-15 14:11:35 +0200113 self.dbg(conf=values)
114 return values
115
Pau Espin Pedrol4b475be2017-12-14 19:01:25 +0100116###################
117# PUBLIC (test API included)
118###################
119 def start(self):
120 if self.bsc is None:
121 raise RuntimeError('BTS needs to be added to a BSC or NITB before it can be started')
122 self.suite_run.poll()
123
124 self.log('Starting to connect to', self.bsc)
125 self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name()))
126 self.configure()
127
128 self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-bts')))
129 btsoct_path = self.inst.child('bin', OsmoBtsOctphy.BIN_BTS_OCTPHY)
130 lib = self.inst.child('lib')
131 if not os.path.isdir(lib):
132 raise RuntimeError('No lib/ in %r' % self.inst)
133
134 # setting capabilities will later disable use of LD_LIBRARY_PATH from ELF loader -> modify RPATH instead.
135 self.log('Setting RPATH for', OsmoBtsOctphy.BIN_BTS_OCTPHY)
136 util.change_elf_rpath(btsoct_path, util.prepend_library_path(lib), self.run_dir.new_dir('patchelf'))
137 # osmo-bty-octphy requires CAP_NET_RAW to open AF_PACKET socket:
138 self.log('Applying CAP_NET_RAW capability to', OsmoBtsOctphy.BIN_BTS_OCTPHY)
139 util.setcap_net_raw(btsoct_path, self.run_dir.new_dir('setcap_net_raw'))
140
141 self.proc_bts = self.launch_process(OsmoBtsOctphy.BIN_BTS_OCTPHY, '-r', '1',
142 '-c', os.path.abspath(self.config_file),
143 '-i', self.bsc.addr(), '-t', str(self.num_trx()))
144 self.suite_run.poll()
145
Pau Espin Pedroldaed4472017-09-15 14:11:35 +0200146# vim: expandtab tabstop=4 shiftwidth=4