Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 1 | # osmo_gsm_tester: specifics for running an osmo-bsc |
| 2 | # |
| 3 | # Copyright (C) 2016-2017 by sysmocom - s.f.m.c. GmbH |
| 4 | # |
| 5 | # Author: Neels Hofmeyr <neels@hofmeyr.de> |
| 6 | # |
| 7 | # This program is free software: you can redistribute it and/or modify |
Harald Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 8 | # it under the terms of the GNU General Public License as |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 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 |
Harald Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 15 | # GNU General Public License for more details. |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 16 | # |
Harald Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 17 | # You should have received a copy of the GNU General Public License |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | import os |
Pau Espin Pedrol | 7bd7103 | 2017-12-11 15:02:26 +0100 | [diff] [blame] | 21 | import re |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 22 | import pprint |
Neels Hofmeyr | af4e231 | 2020-11-27 08:20:56 +0100 | [diff] [blame] | 23 | import re |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 24 | |
Pau Espin Pedrol | e1a58bd | 2020-04-10 20:46:07 +0200 | [diff] [blame] | 25 | from ..core import log, util, config, template, process |
Pau Espin Pedrol | ea8c3d4 | 2020-05-04 12:05:05 +0200 | [diff] [blame] | 26 | from ..core import schema |
Neels Hofmeyr | af4e231 | 2020-11-27 08:20:56 +0100 | [diff] [blame] | 27 | from . import osmo_ctrl, osmo_vty, pcap_recorder |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 28 | |
Pau Espin Pedrol | ea8c3d4 | 2020-05-04 12:05:05 +0200 | [diff] [blame] | 29 | def on_register_schemas(): |
| 30 | config_schema = { |
| 31 | 'net.codec_list[]': schema.CODEC, |
| 32 | } |
| 33 | schema.register_config_schema('bsc', config_schema) |
| 34 | |
| 35 | |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 36 | class OsmoBsc(log.Origin): |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 37 | |
Pau Espin Pedrol | a442cb8 | 2020-05-05 12:54:37 +0200 | [diff] [blame] | 38 | def __init__(self, testenv, msc, mgw, stp, ip_address): |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 39 | super().__init__(log.C_RUN, 'osmo-bsc_%s' % ip_address.get('addr')) |
Pau Espin Pedrol | 5860367 | 2018-08-09 13:45:55 +0200 | [diff] [blame] | 40 | self.run_dir = None |
| 41 | self.config_file = None |
| 42 | self.process = None |
| 43 | self.encryption = None |
| 44 | self.rsl_ip = None |
Pau Espin Pedrol | fed578e | 2019-06-19 17:18:43 +0200 | [diff] [blame] | 45 | self.use_osmux = "off" |
Pau Espin Pedrol | a442cb8 | 2020-05-05 12:54:37 +0200 | [diff] [blame] | 46 | self.testenv = testenv |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 47 | self.ip_address = ip_address |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 48 | self.bts = [] |
| 49 | self.msc = msc |
Pau Espin Pedrol | 386b78d | 2017-11-09 13:02:09 +0100 | [diff] [blame] | 50 | self.mgw = mgw |
Pau Espin Pedrol | 1e1d381 | 2017-11-16 18:06:37 +0100 | [diff] [blame] | 51 | self.stp = stp |
Neels Hofmeyr | af4e231 | 2020-11-27 08:20:56 +0100 | [diff] [blame] | 52 | self.vty = None |
Neels Hofmeyr | 012a17d | 2020-12-08 17:36:04 +0100 | [diff] [blame] | 53 | self.ctrl = None |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 54 | |
| 55 | def start(self): |
| 56 | self.log('Starting osmo-bsc') |
Pau Espin Pedrol | 2a2d846 | 2020-05-11 10:56:52 +0200 | [diff] [blame] | 57 | self.run_dir = util.Dir(self.testenv.test().get_run_dir().new_dir(self.name())) |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 58 | self.configure() |
| 59 | |
Pau Espin Pedrol | a442cb8 | 2020-05-05 12:54:37 +0200 | [diff] [blame] | 60 | inst = util.Dir(os.path.abspath(self.testenv.suite().trial().get_inst('osmo-bsc'))) |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 61 | |
| 62 | binary = inst.child('bin', 'osmo-bsc') |
| 63 | if not os.path.isfile(binary): |
| 64 | raise RuntimeError('Binary missing: %r' % binary) |
| 65 | lib = inst.child('lib') |
| 66 | if not os.path.isdir(lib): |
| 67 | raise RuntimeError('No lib/ in %r' % inst) |
| 68 | |
Pau Espin Pedrol | 16abefb | 2018-10-09 18:54:45 +0200 | [diff] [blame] | 69 | if self.rsl_ip and self.addr() != self.rsl_ip: |
| 70 | filter = 'host %s or host %s and port not 22' % (self.addr(), self.rsl_ip) |
| 71 | else: |
| 72 | filter = 'host %s and port not 22' % self.addr() |
Pau Espin Pedrol | a442cb8 | 2020-05-05 12:54:37 +0200 | [diff] [blame] | 73 | pcap_recorder.PcapRecorder(self.testenv, self.run_dir.new_dir('pcap'), None, filter) |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 74 | |
| 75 | env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) } |
| 76 | |
| 77 | self.dbg(run_dir=self.run_dir, binary=binary, env=env) |
| 78 | self.process = process.Process(self.name(), self.run_dir, |
| 79 | (binary, '-c', |
| 80 | os.path.abspath(self.config_file)), |
| 81 | env=env) |
Pau Espin Pedrol | a442cb8 | 2020-05-05 12:54:37 +0200 | [diff] [blame] | 82 | self.testenv.remember_to_stop(self.process) |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 83 | self.process.launch() |
| 84 | |
Neels Hofmeyr | af4e231 | 2020-11-27 08:20:56 +0100 | [diff] [blame] | 85 | self.vty = OsmoBscVty(self) |
| 86 | self.vty.connect() |
| 87 | |
Neels Hofmeyr | 012a17d | 2020-12-08 17:36:04 +0100 | [diff] [blame] | 88 | self.ctrl = OsmoBscCtrl(self) |
| 89 | self.ctrl.connect() |
| 90 | |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 91 | def configure(self): |
| 92 | self.config_file = self.run_dir.new_file('osmo-bsc.cfg') |
| 93 | self.dbg(config_file=self.config_file) |
| 94 | |
| 95 | values = dict(bsc=config.get_defaults('bsc')) |
Pau Espin Pedrol | a442cb8 | 2020-05-05 12:54:37 +0200 | [diff] [blame] | 96 | config.overlay(values, self.testenv.suite().config()) |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 97 | config.overlay(values, dict(bsc=dict(ip_address=self.ip_address))) |
Pau Espin Pedrol | 386b78d | 2017-11-09 13:02:09 +0100 | [diff] [blame] | 98 | config.overlay(values, self.mgw.conf_for_client()) |
Pau Espin Pedrol | 1e1d381 | 2017-11-16 18:06:37 +0100 | [diff] [blame] | 99 | config.overlay(values, self.stp.conf_for_client()) |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 100 | |
| 101 | bts_list = [] |
| 102 | for bts in self.bts: |
| 103 | bts_list.append(bts.conf_for_bsc()) |
| 104 | config.overlay(values, dict(bsc=dict(net=dict(bts_list=bts_list)))) |
| 105 | |
Pau Espin Pedrol | 387526a | 2017-08-24 15:12:59 +0200 | [diff] [blame] | 106 | # runtime parameters: |
| 107 | if self.encryption is not None: |
Pau Espin Pedrol | abd556a | 2017-09-04 16:26:08 +0200 | [diff] [blame] | 108 | encryption_vty = util.encryption2osmovty(self.encryption) |
| 109 | else: |
| 110 | encryption_vty = util.encryption2osmovty(values['bsc']['net']['encryption']) |
| 111 | config.overlay(values, dict(bsc=dict(net=dict(encryption=encryption_vty)))) |
Pau Espin Pedrol | fed578e | 2019-06-19 17:18:43 +0200 | [diff] [blame] | 112 | config.overlay(values, dict(bsc=dict(use_osmux=self.use_osmux))) |
Pau Espin Pedrol | 387526a | 2017-08-24 15:12:59 +0200 | [diff] [blame] | 113 | |
Pau Espin Pedrol | 63f2d47 | 2018-05-22 18:24:50 +0200 | [diff] [blame] | 114 | if self.rsl_ip is not None: |
| 115 | config.overlay(values, dict(bsc=dict(net=dict(rsl_ip=self.rsl_ip)))) |
| 116 | |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 117 | self.dbg('BSC CONFIG:\n' + pprint.pformat(values)) |
| 118 | |
| 119 | with open(self.config_file, 'w') as f: |
| 120 | r = template.render('osmo-bsc.cfg', values) |
| 121 | self.dbg(r) |
| 122 | f.write(r) |
| 123 | |
| 124 | def addr(self): |
| 125 | return self.ip_address.get('addr') |
| 126 | |
Pau Espin Pedrol | 387526a | 2017-08-24 15:12:59 +0200 | [diff] [blame] | 127 | def set_encryption(self, val): |
| 128 | self.encryption = val |
| 129 | |
Pau Espin Pedrol | 63f2d47 | 2018-05-22 18:24:50 +0200 | [diff] [blame] | 130 | def set_rsl_ip(self, ip_addr): |
| 131 | '''Overwrite RSL IPaddr option sent to all BTS during OML config. Useful |
| 132 | for tests only willing to use osmo-bsc to do the OML setup but using |
| 133 | other external entities to test the RSL path, such as TTCN3 tests.''' |
| 134 | self.rsl_ip = ip_addr |
| 135 | |
Pau Espin Pedrol | fed578e | 2019-06-19 17:18:43 +0200 | [diff] [blame] | 136 | def set_use_osmux(self, use=False, force=False): |
| 137 | if not use: |
| 138 | self.use_osmux = "off" |
| 139 | else: |
| 140 | if not force: |
| 141 | self.use_osmux = "on" |
| 142 | else: |
| 143 | self.use_osmux = "only" |
| 144 | |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 145 | def bts_add(self, bts): |
| 146 | self.bts.append(bts) |
| 147 | bts.set_bsc(self) |
| 148 | |
Pau Espin Pedrol | 7bd7103 | 2017-12-11 15:02:26 +0100 | [diff] [blame] | 149 | def bts_num(self, bts): |
| 150 | 'Provide number id used by OsmoNITB to identify configured BTS' |
| 151 | # We take advantage from the fact that VTY code assigns VTY in ascending |
| 152 | # order through the bts nodes found. As we populate the config iterating |
| 153 | # over this list, we have a 1:1 match in indexes. |
| 154 | return self.bts.index(bts) |
| 155 | |
Neels Hofmeyr | 012a17d | 2020-12-08 17:36:04 +0100 | [diff] [blame] | 156 | def bts_is_connected(self, bts): |
| 157 | return self.ctrl.bts_is_connected(self.bts_num(bts)) |
Pau Espin Pedrol | 7bd7103 | 2017-12-11 15:02:26 +0100 | [diff] [blame] | 158 | |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 159 | def running(self): |
| 160 | return not self.process.terminated() |
| 161 | |
Neels Hofmeyr | af4e231 | 2020-11-27 08:20:56 +0100 | [diff] [blame] | 162 | def cleanup(self): |
| 163 | if self.vty is not None: |
| 164 | self.vty.disconnect() |
| 165 | self.vty = None |
Neels Hofmeyr | 012a17d | 2020-12-08 17:36:04 +0100 | [diff] [blame] | 166 | if self.ctrl is not None: |
| 167 | self.ctrl.disconnect() |
| 168 | self.ctrl = None |
Neels Hofmeyr | 5b04ef2 | 2020-12-01 04:48:38 +0100 | [diff] [blame] | 169 | |
| 170 | class OsmoBscCtrl(osmo_ctrl.OsmoCtrl): |
| 171 | def __init__(self, bsc, port=4249): |
| 172 | self.bsc = bsc |
| 173 | super().__init__(bsc.addr(), port) |
Pau Espin Pedrol | 7bd7103 | 2017-12-11 15:02:26 +0100 | [diff] [blame] | 174 | |
| 175 | def bts_is_connected(self, bts_num): |
Neels Hofmeyr | 5b04ef2 | 2020-12-01 04:48:38 +0100 | [diff] [blame] | 176 | return self.get_var('bts.%d.oml-connection-state' % bts_num) == 'connected' |
Pau Espin Pedrol | 7bd7103 | 2017-12-11 15:02:26 +0100 | [diff] [blame] | 177 | |
Neels Hofmeyr | af4e231 | 2020-11-27 08:20:56 +0100 | [diff] [blame] | 178 | class OsmoBscVty(osmo_vty.OsmoVty): |
| 179 | def __init__(self, bsc, port=4242): |
| 180 | self.bsc = bsc |
| 181 | super().__init__(self.bsc.addr(), port) |
| 182 | |
Neels Hofmeyr | ca155b6 | 2020-11-27 08:20:56 +0100 | [diff] [blame] | 183 | def get_active_lchans(self): |
| 184 | lchan_summary = self.cmd('show lchan summary') |
| 185 | |
| 186 | re_lchan_summary = re.compile('BTS ([0-9]+), TRX ([0-9]+), Timeslot ([0-9]+) *([^,]*), Lchan ([0-9]+),.* State ([A-Za-z_]+).*') |
| 187 | active_lchans = set() |
| 188 | for line in lchan_summary: |
| 189 | m = re_lchan_summary.match(line) |
| 190 | if m: |
| 191 | bts, trx, ts, lchan_type, subslot, state = m.groups() |
| 192 | active_lchans.add('%s-%s-%s-%s %s %s' % (bts, trx, ts, subslot, lchan_type, state)) |
| 193 | if not active_lchans: |
| 194 | self.dbg('No active lchans') |
| 195 | else: |
| 196 | self.dbg('Active lchans:\n|', '\n| '.join(active_lchans), '\n'); |
| 197 | return active_lchans |
| 198 | |
| 199 | def active_lchans_match(self, expected=[], not_expected=[]): |
| 200 | active_lchans = self.get_active_lchans() |
| 201 | matches = [] |
| 202 | mismatches = [] |
| 203 | |
| 204 | for expected_lchan in expected: |
| 205 | found = False |
| 206 | for active_lchan in active_lchans: |
| 207 | if active_lchan.startswith(expected_lchan): |
| 208 | found = True |
| 209 | break |
| 210 | if found: |
| 211 | matches.append(expected_lchan) |
| 212 | else: |
| 213 | mismatches.append('missing: ' + expected_lchan) |
| 214 | |
| 215 | for not_expected_lchan in not_expected: |
| 216 | found = False |
| 217 | for active_lchan in active_lchans: |
| 218 | if active_lchan.startswith(not_expected_lchan): |
| 219 | found = True |
| 220 | break |
| 221 | if not found: |
| 222 | matches.append('not: ' + not_expected_lchan) |
| 223 | else: |
| 224 | mismatches.append('unexpected: ' + not_expected_lchan) |
| 225 | |
| 226 | if matches: |
| 227 | self.log('Found matching lchan activity (%d of %d requirements):' % (len(matches), len(expected) + len(not_expected)), matches) |
| 228 | if mismatches: |
| 229 | self.err('Found unexpected lchan activity (%d of %d requirements):' % (len(mismatches), len(expected) + len(not_expected)), mismatches) |
| 230 | return not mismatches |
| 231 | |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 232 | # vim: expandtab tabstop=4 shiftwidth=4 |