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