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 | 05314b4 | 2017-11-23 13:35:42 +0100 | [diff] [blame] | 23 | from . import log, config, util, template, process, event_loop, pcu_osmo |
Pau Espin Pedrol | daed447 | 2017-09-15 14:11:35 +0200 | [diff] [blame] | 24 | |
| 25 | class OsmoBtsOctphy(log.Origin): |
| 26 | suite_run = None |
| 27 | bsc = None |
Pau Espin Pedrol | 05314b4 | 2017-11-23 13:35:42 +0100 | [diff] [blame] | 28 | sgsn = None |
Pau Espin Pedrol | daed447 | 2017-09-15 14:11:35 +0200 | [diff] [blame] | 29 | run_dir = None |
| 30 | inst = None |
| 31 | env = None |
| 32 | pcu_sk_tmp_dir = None |
| 33 | values = None |
Pau Espin Pedrol | 5e0c251 | 2017-11-06 18:40:23 +0100 | [diff] [blame] | 34 | lac = None |
Pau Espin Pedrol | 8a3a7b5 | 2017-11-28 15:50:02 +0100 | [diff] [blame] | 35 | rac = None |
Pau Espin Pedrol | 4ccce7c | 2017-11-07 11:13:20 +0100 | [diff] [blame] | 36 | cellid = None |
Pau Espin Pedrol | 8a3a7b5 | 2017-11-28 15:50:02 +0100 | [diff] [blame] | 37 | bvci = None |
Pau Espin Pedrol | d0a0111 | 2017-11-28 12:15:10 +0100 | [diff] [blame] | 38 | proc_bts = None |
Pau Espin Pedrol | 05314b4 | 2017-11-23 13:35:42 +0100 | [diff] [blame] | 39 | _pcu = None |
Pau Espin Pedrol | daed447 | 2017-09-15 14:11:35 +0200 | [diff] [blame] | 40 | |
| 41 | BIN_BTS_OCTPHY = 'osmo-bts-octphy' |
| 42 | |
| 43 | CONF_BTS_OCTPHY = 'osmo-bts-octphy.cfg' |
| 44 | |
| 45 | def __init__(self, suite_run, conf): |
| 46 | super().__init__(log.C_RUN, OsmoBtsOctphy.BIN_BTS_OCTPHY) |
| 47 | self.suite_run = suite_run |
| 48 | self.conf = conf |
| 49 | self.env = {} |
| 50 | self.values = {} |
| 51 | self.pcu_sk_tmp_dir = tempfile.mkdtemp('', 'ogtpcusk') |
| 52 | if len(self.pcu_socket_path().encode()) > 107: |
| 53 | raise log.Error('Path for pcu socket is longer than max allowed len for unix socket path (107):', self.pcu_socket_path()) |
| 54 | |
| 55 | def cleanup(self): |
| 56 | if self.pcu_sk_tmp_dir: |
| 57 | try: |
| 58 | os.remove(self.pcu_socket_path()) |
| 59 | except OSError: |
| 60 | pass |
| 61 | os.rmdir(self.pcu_sk_tmp_dir) |
| 62 | |
Pau Espin Pedrol | 05314b4 | 2017-11-23 13:35:42 +0100 | [diff] [blame] | 63 | def pcu(self): |
| 64 | if self._pcu is None: |
Pau Espin Pedrol | 7a4c17b | 2017-12-05 17:05:27 +0100 | [diff] [blame] | 65 | self._pcu = pcu_osmo.OsmoPcu(self.suite_run, self, self.conf) |
Pau Espin Pedrol | 05314b4 | 2017-11-23 13:35:42 +0100 | [diff] [blame] | 66 | return self._pcu |
| 67 | |
Pau Espin Pedrol | daed447 | 2017-09-15 14:11:35 +0200 | [diff] [blame] | 68 | def pcu_socket_path(self): |
| 69 | return os.path.join(self.pcu_sk_tmp_dir, 'pcu_bts') |
| 70 | |
| 71 | def remote_addr(self): |
| 72 | return self.conf.get('addr') |
| 73 | |
| 74 | def start(self): |
| 75 | if self.bsc is None: |
| 76 | raise RuntimeError('BTS needs to be added to a BSC or NITB before it can be started') |
| 77 | self.suite_run.poll() |
| 78 | |
| 79 | self.log('Starting to connect to', self.bsc) |
| 80 | self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name())) |
| 81 | self.configure() |
| 82 | |
| 83 | self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-bts'))) |
| 84 | btsoct_path = self.inst.child('bin', OsmoBtsOctphy.BIN_BTS_OCTPHY) |
| 85 | lib = self.inst.child('lib') |
| 86 | if not os.path.isdir(lib): |
| 87 | raise RuntimeError('No lib/ in %r' % self.inst) |
| 88 | |
| 89 | # setting capabilities will later disable use of LD_LIBRARY_PATH from ELF loader -> modify RPATH instead. |
| 90 | self.log('Setting RPATH for', OsmoBtsOctphy.BIN_BTS_OCTPHY) |
| 91 | util.change_elf_rpath(btsoct_path, util.prepend_library_path(lib), self.run_dir.new_dir('patchelf')) |
| 92 | # osmo-bty-octphy requires CAP_NET_RAW to open AF_PACKET socket: |
| 93 | self.log('Applying CAP_NET_RAW capability to', OsmoBtsOctphy.BIN_BTS_OCTPHY) |
| 94 | util.setcap_net_raw(btsoct_path, self.run_dir.new_dir('setcap_net_raw')) |
| 95 | |
Pau Espin Pedrol | d0a0111 | 2017-11-28 12:15:10 +0100 | [diff] [blame] | 96 | self.proc_bts = self.launch_process(OsmoBtsOctphy.BIN_BTS_OCTPHY, '-r', '1', |
Pau Espin Pedrol | daed447 | 2017-09-15 14:11:35 +0200 | [diff] [blame] | 97 | '-c', os.path.abspath(self.config_file), |
| 98 | '-i', self.bsc.addr(), '-t', str(self.num_trx())) |
Pau Espin Pedrol | daed447 | 2017-09-15 14:11:35 +0200 | [diff] [blame] | 99 | self.suite_run.poll() |
| 100 | |
| 101 | def launch_process(self, binary_name, *args): |
| 102 | binary = os.path.abspath(self.inst.child('bin', binary_name)) |
| 103 | run_dir = self.run_dir.new_dir(binary_name) |
| 104 | if not os.path.isfile(binary): |
| 105 | raise RuntimeError('Binary missing: %r' % binary) |
| 106 | proc = process.Process(binary_name, run_dir, |
| 107 | (binary,) + args, |
| 108 | env=self.env) |
| 109 | self.suite_run.remember_to_stop(proc) |
| 110 | proc.launch() |
| 111 | return proc |
| 112 | |
| 113 | def num_trx(self): |
| 114 | return len(self.values['osmo_bts_octphy'].get('trx_list', [])) |
| 115 | |
| 116 | def allocate_phy_instances(self, c): |
| 117 | ''' |
| 118 | Generate match trx Z <-> phy X inst Y to use in vty config |
| 119 | |
| 120 | We create a new phy for each trx found with a new hwaddr. If hwaddr is |
| 121 | already there, increase num_instances and give last instance index to |
| 122 | the current trx. |
| 123 | ''' |
| 124 | phy_list = [] |
| 125 | for trx in c.get('trx_list', []): |
| 126 | hwaddr = trx.get('hw_addr', None) |
| 127 | netdev = trx.get('net_device', None) |
| 128 | if hwaddr is None: |
| 129 | raise log.Error('Expected hw-addr value not found!') |
| 130 | found = False |
| 131 | phy_idx = 0 |
| 132 | for phy in phy_list: |
| 133 | if phy['hw_addr'] == hwaddr: |
| 134 | phy['num_instances'] += 1 |
| 135 | found = True |
| 136 | break |
| 137 | phy_idx += 1 |
| 138 | if not found: |
| 139 | phy_list.append({'hw_addr': hwaddr, 'net_device': netdev, 'num_instances': 1}) |
| 140 | trx['phy_idx'] = phy_idx |
| 141 | trx['instance_idx'] = phy_list[phy_idx]['num_instances'] - 1 |
| 142 | c['phy_list'] = phy_list |
| 143 | |
| 144 | def configure(self): |
| 145 | if self.bsc is None: |
| 146 | raise RuntimeError('BTS needs to be added to a BSC or NITB before it can be configured') |
| 147 | self.config_file = self.run_dir.new_file(OsmoBtsOctphy.CONF_BTS_OCTPHY) |
| 148 | self.dbg(config_file=self.config_file) |
| 149 | |
| 150 | values = dict(osmo_bts_octphy=config.get_defaults('osmo_bts_octphy')) |
| 151 | config.overlay(values, self.suite_run.config()) |
| 152 | config.overlay(values, { |
| 153 | 'osmo_bts_octphy': { |
| 154 | 'oml_remote_ip': self.bsc.addr(), |
| 155 | 'pcu_socket_path': self.pcu_socket_path(), |
| 156 | } |
| 157 | }) |
| 158 | config.overlay(values, { 'osmo_bts_octphy': self.conf }) |
| 159 | |
| 160 | self.allocate_phy_instances(values['osmo_bts_octphy']) |
| 161 | |
| 162 | self.dbg('OSMO-BTS-OCTPHY CONFIG:\n' + pprint.pformat(values)) |
| 163 | self.values = values |
| 164 | with open(self.config_file, 'w') as f: |
| 165 | r = template.render(OsmoBtsOctphy.CONF_BTS_OCTPHY, values) |
| 166 | self.dbg(r) |
| 167 | f.write(r) |
| 168 | |
| 169 | def conf_for_bsc(self): |
| 170 | values = config.get_defaults('bsc_bts') |
| 171 | config.overlay(values, config.get_defaults('osmo_bts_octphy')) |
Pau Espin Pedrol | 5e0c251 | 2017-11-06 18:40:23 +0100 | [diff] [blame] | 172 | if self.lac is not None: |
| 173 | config.overlay(values, { 'location_area_code': self.lac }) |
Pau Espin Pedrol | 8a3a7b5 | 2017-11-28 15:50:02 +0100 | [diff] [blame] | 174 | if self.rac is not None: |
| 175 | config.overlay(values, { 'routing_area_code': self.rac }) |
Pau Espin Pedrol | 4ccce7c | 2017-11-07 11:13:20 +0100 | [diff] [blame] | 176 | if self.cellid is not None: |
| 177 | config.overlay(values, { 'cell_identity': self.cellid }) |
Pau Espin Pedrol | 8a3a7b5 | 2017-11-28 15:50:02 +0100 | [diff] [blame] | 178 | if self.bvci is not None: |
| 179 | config.overlay(values, { 'bvci': self.bvci }) |
Pau Espin Pedrol | daed447 | 2017-09-15 14:11:35 +0200 | [diff] [blame] | 180 | config.overlay(values, self.conf) |
Pau Espin Pedrol | 05314b4 | 2017-11-23 13:35:42 +0100 | [diff] [blame] | 181 | |
| 182 | sgsn_conf = {} if self.sgsn is None else self.sgsn.conf_for_client() |
| 183 | config.overlay(values, sgsn_conf) |
| 184 | |
Pau Espin Pedrol | daed447 | 2017-09-15 14:11:35 +0200 | [diff] [blame] | 185 | self.dbg(conf=values) |
| 186 | return values |
| 187 | |
Pau Espin Pedrol | d0a0111 | 2017-11-28 12:15:10 +0100 | [diff] [blame] | 188 | def ready_for_pcu(self): |
| 189 | if not self.proc_bts or not self.proc_bts.is_running: |
| 190 | return False |
| 191 | return 'BTS is up' in (self.proc_bts.get_stderr() or '') |
| 192 | |
Pau Espin Pedrol | daed447 | 2017-09-15 14:11:35 +0200 | [diff] [blame] | 193 | def set_bsc(self, bsc): |
| 194 | self.bsc = bsc |
| 195 | |
Pau Espin Pedrol | 05314b4 | 2017-11-23 13:35:42 +0100 | [diff] [blame] | 196 | def set_sgsn(self, sgsn): |
| 197 | self.sgsn = sgsn |
| 198 | |
Pau Espin Pedrol | 5e0c251 | 2017-11-06 18:40:23 +0100 | [diff] [blame] | 199 | def set_lac(self, lac): |
| 200 | self.lac = lac |
| 201 | |
Pau Espin Pedrol | 8a3a7b5 | 2017-11-28 15:50:02 +0100 | [diff] [blame] | 202 | def set_rac(self, rac): |
| 203 | self.rac = rac |
| 204 | |
Pau Espin Pedrol | 4ccce7c | 2017-11-07 11:13:20 +0100 | [diff] [blame] | 205 | def set_cellid(self, cellid): |
| 206 | self.cellid = cellid |
| 207 | |
Pau Espin Pedrol | 8a3a7b5 | 2017-11-28 15:50:02 +0100 | [diff] [blame] | 208 | def set_bvci(self, bvci): |
| 209 | self.bvci = bvci |
| 210 | |
Pau Espin Pedrol | daed447 | 2017-09-15 14:11:35 +0200 | [diff] [blame] | 211 | # vim: expandtab tabstop=4 shiftwidth=4 |