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 | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 53 | |
| 54 | def start(self): |
| 55 | self.log('Starting osmo-bsc') |
Pau Espin Pedrol | 2a2d846 | 2020-05-11 10:56:52 +0200 | [diff] [blame] | 56 | 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] | 57 | self.configure() |
| 58 | |
Pau Espin Pedrol | a442cb8 | 2020-05-05 12:54:37 +0200 | [diff] [blame] | 59 | 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] | 60 | |
| 61 | binary = inst.child('bin', 'osmo-bsc') |
| 62 | if not os.path.isfile(binary): |
| 63 | raise RuntimeError('Binary missing: %r' % binary) |
| 64 | lib = inst.child('lib') |
| 65 | if not os.path.isdir(lib): |
| 66 | raise RuntimeError('No lib/ in %r' % inst) |
| 67 | |
Pau Espin Pedrol | 16abefb | 2018-10-09 18:54:45 +0200 | [diff] [blame] | 68 | if self.rsl_ip and self.addr() != self.rsl_ip: |
| 69 | filter = 'host %s or host %s and port not 22' % (self.addr(), self.rsl_ip) |
| 70 | else: |
| 71 | filter = 'host %s and port not 22' % self.addr() |
Pau Espin Pedrol | a442cb8 | 2020-05-05 12:54:37 +0200 | [diff] [blame] | 72 | 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] | 73 | |
| 74 | env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) } |
| 75 | |
| 76 | self.dbg(run_dir=self.run_dir, binary=binary, env=env) |
| 77 | self.process = process.Process(self.name(), self.run_dir, |
| 78 | (binary, '-c', |
| 79 | os.path.abspath(self.config_file)), |
| 80 | env=env) |
Pau Espin Pedrol | a442cb8 | 2020-05-05 12:54:37 +0200 | [diff] [blame] | 81 | self.testenv.remember_to_stop(self.process) |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 82 | self.process.launch() |
| 83 | |
Neels Hofmeyr | af4e231 | 2020-11-27 08:20:56 +0100 | [diff] [blame] | 84 | self.vty = OsmoBscVty(self) |
| 85 | self.vty.connect() |
| 86 | |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 87 | def configure(self): |
| 88 | self.config_file = self.run_dir.new_file('osmo-bsc.cfg') |
| 89 | self.dbg(config_file=self.config_file) |
| 90 | |
| 91 | values = dict(bsc=config.get_defaults('bsc')) |
Pau Espin Pedrol | a442cb8 | 2020-05-05 12:54:37 +0200 | [diff] [blame] | 92 | config.overlay(values, self.testenv.suite().config()) |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 93 | config.overlay(values, dict(bsc=dict(ip_address=self.ip_address))) |
Pau Espin Pedrol | 386b78d | 2017-11-09 13:02:09 +0100 | [diff] [blame] | 94 | config.overlay(values, self.mgw.conf_for_client()) |
Pau Espin Pedrol | 1e1d381 | 2017-11-16 18:06:37 +0100 | [diff] [blame] | 95 | config.overlay(values, self.stp.conf_for_client()) |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 96 | |
| 97 | bts_list = [] |
| 98 | for bts in self.bts: |
| 99 | bts_list.append(bts.conf_for_bsc()) |
| 100 | config.overlay(values, dict(bsc=dict(net=dict(bts_list=bts_list)))) |
| 101 | |
Pau Espin Pedrol | 387526a | 2017-08-24 15:12:59 +0200 | [diff] [blame] | 102 | # runtime parameters: |
| 103 | if self.encryption is not None: |
Pau Espin Pedrol | abd556a | 2017-09-04 16:26:08 +0200 | [diff] [blame] | 104 | encryption_vty = util.encryption2osmovty(self.encryption) |
| 105 | else: |
| 106 | encryption_vty = util.encryption2osmovty(values['bsc']['net']['encryption']) |
| 107 | config.overlay(values, dict(bsc=dict(net=dict(encryption=encryption_vty)))) |
Pau Espin Pedrol | fed578e | 2019-06-19 17:18:43 +0200 | [diff] [blame] | 108 | config.overlay(values, dict(bsc=dict(use_osmux=self.use_osmux))) |
Pau Espin Pedrol | 387526a | 2017-08-24 15:12:59 +0200 | [diff] [blame] | 109 | |
Pau Espin Pedrol | 63f2d47 | 2018-05-22 18:24:50 +0200 | [diff] [blame] | 110 | if self.rsl_ip is not None: |
| 111 | config.overlay(values, dict(bsc=dict(net=dict(rsl_ip=self.rsl_ip)))) |
| 112 | |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 113 | self.dbg('BSC CONFIG:\n' + pprint.pformat(values)) |
| 114 | |
| 115 | with open(self.config_file, 'w') as f: |
| 116 | r = template.render('osmo-bsc.cfg', values) |
| 117 | self.dbg(r) |
| 118 | f.write(r) |
| 119 | |
| 120 | def addr(self): |
| 121 | return self.ip_address.get('addr') |
| 122 | |
Pau Espin Pedrol | 387526a | 2017-08-24 15:12:59 +0200 | [diff] [blame] | 123 | def set_encryption(self, val): |
| 124 | self.encryption = val |
| 125 | |
Pau Espin Pedrol | 63f2d47 | 2018-05-22 18:24:50 +0200 | [diff] [blame] | 126 | def set_rsl_ip(self, ip_addr): |
| 127 | '''Overwrite RSL IPaddr option sent to all BTS during OML config. Useful |
| 128 | for tests only willing to use osmo-bsc to do the OML setup but using |
| 129 | other external entities to test the RSL path, such as TTCN3 tests.''' |
| 130 | self.rsl_ip = ip_addr |
| 131 | |
Pau Espin Pedrol | fed578e | 2019-06-19 17:18:43 +0200 | [diff] [blame] | 132 | def set_use_osmux(self, use=False, force=False): |
| 133 | if not use: |
| 134 | self.use_osmux = "off" |
| 135 | else: |
| 136 | if not force: |
| 137 | self.use_osmux = "on" |
| 138 | else: |
| 139 | self.use_osmux = "only" |
| 140 | |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 141 | def bts_add(self, bts): |
| 142 | self.bts.append(bts) |
| 143 | bts.set_bsc(self) |
| 144 | |
Pau Espin Pedrol | 7bd7103 | 2017-12-11 15:02:26 +0100 | [diff] [blame] | 145 | def bts_num(self, bts): |
| 146 | 'Provide number id used by OsmoNITB to identify configured BTS' |
| 147 | # We take advantage from the fact that VTY code assigns VTY in ascending |
| 148 | # order through the bts nodes found. As we populate the config iterating |
| 149 | # over this list, we have a 1:1 match in indexes. |
| 150 | return self.bts.index(bts) |
| 151 | |
| 152 | def bts_is_connected(self, bts): |
| 153 | return OsmoBscCtrl(self).bts_is_connected(self.bts_num(bts)) |
| 154 | |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 155 | def running(self): |
| 156 | return not self.process.terminated() |
| 157 | |
Neels Hofmeyr | af4e231 | 2020-11-27 08:20:56 +0100 | [diff] [blame] | 158 | def cleanup(self): |
| 159 | if self.vty is not None: |
| 160 | self.vty.disconnect() |
| 161 | self.vty = None |
Pau Espin Pedrol | 7bd7103 | 2017-12-11 15:02:26 +0100 | [diff] [blame] | 162 | |
| 163 | class OsmoBscCtrl(log.Origin): |
| 164 | PORT = 4249 |
| 165 | BTS_OML_STATE_VAR = "bts.%d.oml-connection-state" |
| 166 | BTS_OML_STATE_RE = re.compile("GET_REPLY (\d+) bts.\d+.oml-connection-state (?P<oml_state>\w+)") |
| 167 | |
| 168 | def __init__(self, bsc): |
| 169 | self.bsc = bsc |
| 170 | super().__init__(log.C_BUS, 'CTRL(%s:%d)' % (self.bsc.addr(), OsmoBscCtrl.PORT)) |
| 171 | |
| 172 | def ctrl(self): |
| 173 | return osmo_ctrl.OsmoCtrl(self.bsc.addr(), OsmoBscCtrl.PORT) |
| 174 | |
| 175 | def bts_is_connected(self, bts_num): |
| 176 | with self.ctrl() as ctrl: |
| 177 | ctrl.do_get(OsmoBscCtrl.BTS_OML_STATE_VAR % bts_num) |
| 178 | data = ctrl.receive() |
| 179 | while (len(data) > 0): |
| 180 | (answer, data) = ctrl.remove_ipa_ctrl_header(data) |
| 181 | answer_str = answer.decode('utf-8') |
| 182 | answer_str = answer_str.replace('\n', ' ') |
| 183 | res = OsmoBscCtrl.BTS_OML_STATE_RE.match(answer_str) |
| 184 | if res: |
| 185 | oml_state = str(res.group('oml_state')) |
| 186 | if oml_state == 'connected': |
| 187 | return True |
| 188 | return False |
| 189 | |
Neels Hofmeyr | af4e231 | 2020-11-27 08:20:56 +0100 | [diff] [blame] | 190 | class OsmoBscVty(osmo_vty.OsmoVty): |
| 191 | def __init__(self, bsc, port=4242): |
| 192 | self.bsc = bsc |
| 193 | super().__init__(self.bsc.addr(), port) |
| 194 | |
Neels Hofmeyr | ca155b6 | 2020-11-27 08:20:56 +0100 | [diff] [blame] | 195 | def get_active_lchans(self): |
| 196 | lchan_summary = self.cmd('show lchan summary') |
| 197 | |
| 198 | re_lchan_summary = re.compile('BTS ([0-9]+), TRX ([0-9]+), Timeslot ([0-9]+) *([^,]*), Lchan ([0-9]+),.* State ([A-Za-z_]+).*') |
| 199 | active_lchans = set() |
| 200 | for line in lchan_summary: |
| 201 | m = re_lchan_summary.match(line) |
| 202 | if m: |
| 203 | bts, trx, ts, lchan_type, subslot, state = m.groups() |
| 204 | active_lchans.add('%s-%s-%s-%s %s %s' % (bts, trx, ts, subslot, lchan_type, state)) |
| 205 | if not active_lchans: |
| 206 | self.dbg('No active lchans') |
| 207 | else: |
| 208 | self.dbg('Active lchans:\n|', '\n| '.join(active_lchans), '\n'); |
| 209 | return active_lchans |
| 210 | |
| 211 | def active_lchans_match(self, expected=[], not_expected=[]): |
| 212 | active_lchans = self.get_active_lchans() |
| 213 | matches = [] |
| 214 | mismatches = [] |
| 215 | |
| 216 | for expected_lchan in expected: |
| 217 | found = False |
| 218 | for active_lchan in active_lchans: |
| 219 | if active_lchan.startswith(expected_lchan): |
| 220 | found = True |
| 221 | break |
| 222 | if found: |
| 223 | matches.append(expected_lchan) |
| 224 | else: |
| 225 | mismatches.append('missing: ' + expected_lchan) |
| 226 | |
| 227 | for not_expected_lchan in not_expected: |
| 228 | found = False |
| 229 | for active_lchan in active_lchans: |
| 230 | if active_lchan.startswith(not_expected_lchan): |
| 231 | found = True |
| 232 | break |
| 233 | if not found: |
| 234 | matches.append('not: ' + not_expected_lchan) |
| 235 | else: |
| 236 | mismatches.append('unexpected: ' + not_expected_lchan) |
| 237 | |
| 238 | if matches: |
| 239 | self.log('Found matching lchan activity (%d of %d requirements):' % (len(matches), len(expected) + len(not_expected)), matches) |
| 240 | if mismatches: |
| 241 | self.err('Found unexpected lchan activity (%d of %d requirements):' % (len(mismatches), len(expected) + len(not_expected)), mismatches) |
| 242 | return not mismatches |
| 243 | |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 244 | # vim: expandtab tabstop=4 shiftwidth=4 |