Pau Espin Pedrol | daed447 | 2017-09-15 14:11:35 +0200 | [diff] [blame] | 1 | # 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 | |
| 20 | import os |
| 21 | import pprint |
| 22 | import tempfile |
Pau Espin Pedrol | 9a4631c | 2018-03-28 19:17:34 +0200 | [diff] [blame] | 23 | from . import log, config, util, template, process, pcu_osmo, bts_osmo |
Pau Espin Pedrol | daed447 | 2017-09-15 14:11:35 +0200 | [diff] [blame] | 24 | |
Pau Espin Pedrol | c9817a5 | 2017-12-13 19:52:27 +0100 | [diff] [blame] | 25 | class OsmoBtsOctphy(bts_osmo.OsmoBtsMainUnit): |
Pau Espin Pedrol | 4b475be | 2017-12-14 19:01:25 +0100 | [diff] [blame] | 26 | |
| 27 | ############## |
| 28 | # PROTECTED |
| 29 | ############## |
Pau Espin Pedrol | daed447 | 2017-09-15 14:11:35 +0200 | [diff] [blame] | 30 | run_dir = None |
| 31 | inst = None |
| 32 | env = None |
Pau Espin Pedrol | daed447 | 2017-09-15 14:11:35 +0200 | [diff] [blame] | 33 | |
| 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 Pedrol | e519462 | 2018-05-07 13:36:58 +0200 | [diff] [blame] | 39 | super().__init__(suite_run, conf, OsmoBtsOctphy.BIN_BTS_OCTPHY, 'osmo_bts_octphy') |
Pau Espin Pedrol | daed447 | 2017-09-15 14:11:35 +0200 | [diff] [blame] | 40 | self.env = {} |
| 41 | self.values = {} |
Pau Espin Pedrol | daed447 | 2017-09-15 14:11:35 +0200 | [diff] [blame] | 42 | |
Pau Espin Pedrol | daed447 | 2017-09-15 14:11:35 +0200 | [diff] [blame] | 43 | 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 Pedrol | daed447 | 2017-09-15 14:11:35 +0200 | [diff] [blame] | 55 | 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 Pedrol | 4b475be | 2017-12-14 19:01:25 +0100 | [diff] [blame] | 108 | ######################## |
| 109 | # PUBLIC - INTERNAL API |
| 110 | ######################## |
Pau Espin Pedrol | daed447 | 2017-09-15 14:11:35 +0200 | [diff] [blame] | 111 | def conf_for_bsc(self): |
Pau Espin Pedrol | e519462 | 2018-05-07 13:36:58 +0200 | [diff] [blame] | 112 | values = self.conf_for_bsc_prepare() |
Pau Espin Pedrol | daed447 | 2017-09-15 14:11:35 +0200 | [diff] [blame] | 113 | self.dbg(conf=values) |
| 114 | return values |
| 115 | |
Pau Espin Pedrol | 4b475be | 2017-12-14 19:01:25 +0100 | [diff] [blame] | 116 | ################### |
| 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 Pedrol | daed447 | 2017-09-15 14:11:35 +0200 | [diff] [blame] | 146 | # vim: expandtab tabstop=4 shiftwidth=4 |