Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 1 | # osmo_gsm_tester: specifics for running an osmo-nitb |
| 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 | 3531a19 | 2017-03-28 14:30:28 +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 | 3531a19 | 2017-03-28 14:30:28 +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 | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | import os |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 21 | import re |
Neels Hofmeyr | 943c81d | 2017-05-22 20:16:03 +0200 | [diff] [blame] | 22 | import pprint |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 23 | |
Pau Espin Pedrol | 2d16f6f | 2017-05-30 15:33:57 +0200 | [diff] [blame] | 24 | from . import log, util, config, template, process, osmo_ctrl, pcap_recorder, smsc |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 25 | |
| 26 | class OsmoNitb(log.Origin): |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 27 | |
Neels Hofmeyr | 76d8103 | 2017-05-18 18:35:32 +0200 | [diff] [blame] | 28 | def __init__(self, suite_run, ip_address): |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 29 | super().__init__(log.C_RUN, 'osmo-nitb_%s' % ip_address.get('addr')) |
Pau Espin Pedrol | 5860367 | 2018-08-09 13:45:55 +0200 | [diff] [blame] | 30 | self.run_dir = None |
| 31 | self.config_file = None |
| 32 | self.process = None |
| 33 | self.encryption = None |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 34 | self.suite_run = suite_run |
Neels Hofmeyr | 76d8103 | 2017-05-18 18:35:32 +0200 | [diff] [blame] | 35 | self.ip_address = ip_address |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 36 | self.bts = [] |
Pau Espin Pedrol | 2d16f6f | 2017-05-30 15:33:57 +0200 | [diff] [blame] | 37 | self.smsc = smsc.Smsc((ip_address.get('addr'), 2775)) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 38 | |
| 39 | def start(self): |
| 40 | self.log('Starting osmo-nitb') |
Pau Espin Pedrol | d091233 | 2017-06-14 13:27:08 +0200 | [diff] [blame] | 41 | self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name())) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 42 | self.configure() |
Your Name | 3c6673a | 2017-04-08 18:52:39 +0200 | [diff] [blame] | 43 | inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-nitb'))) |
| 44 | binary = inst.child('bin', 'osmo-nitb') |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 45 | if not os.path.isfile(binary): |
| 46 | raise RuntimeError('Binary missing: %r' % binary) |
Your Name | 3c6673a | 2017-04-08 18:52:39 +0200 | [diff] [blame] | 47 | lib = inst.child('lib') |
| 48 | if not os.path.isdir(lib): |
| 49 | raise RuntimeError('No lib/ in %r' % inst) |
Pau Espin Pedrol | e39c6f1 | 2017-05-08 16:21:38 +0200 | [diff] [blame] | 50 | |
Pau Espin Pedrol | 9cc1d08 | 2017-11-20 17:40:25 +0100 | [diff] [blame] | 51 | pcap_recorder.PcapRecorder(self.suite_run, self.run_dir.new_dir('pcap'), None, |
Pau Espin Pedrol | 8cd347b | 2017-05-12 16:24:21 +0200 | [diff] [blame] | 52 | 'host %s and port not 22' % self.addr()) |
Pau Espin Pedrol | 13143bc | 2017-05-08 17:07:28 +0200 | [diff] [blame] | 53 | |
Pau Espin Pedrol | e39c6f1 | 2017-05-08 16:21:38 +0200 | [diff] [blame] | 54 | env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) } |
| 55 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 56 | self.dbg(run_dir=self.run_dir, binary=binary, env=env) |
| 57 | self.process = process.Process(self.name(), self.run_dir, |
| 58 | (binary, '-c', |
| 59 | os.path.abspath(self.config_file)), |
| 60 | env=env) |
| 61 | self.suite_run.remember_to_stop(self.process) |
| 62 | self.process.launch() |
| 63 | |
| 64 | def configure(self): |
| 65 | self.config_file = self.run_dir.new_file('osmo-nitb.cfg') |
| 66 | self.dbg(config_file=self.config_file) |
| 67 | |
| 68 | values = dict(nitb=config.get_defaults('nitb')) |
| 69 | config.overlay(values, self.suite_run.config()) |
Neels Hofmeyr | 4f59ba6 | 2017-05-22 20:04:05 +0200 | [diff] [blame] | 70 | config.overlay(values, dict(nitb=dict(ip_address=self.ip_address))) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 71 | |
| 72 | bts_list = [] |
| 73 | for bts in self.bts: |
Neels Hofmeyr | c4ba4a6 | 2017-05-18 19:31:44 +0200 | [diff] [blame] | 74 | bts_list.append(bts.conf_for_bsc()) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 75 | config.overlay(values, dict(nitb=dict(net=dict(bts_list=bts_list)))) |
Pau Espin Pedrol | 2d16f6f | 2017-05-30 15:33:57 +0200 | [diff] [blame] | 76 | config.overlay(values, self.smsc.get_config()) |
Pau Espin Pedrol | ca126b1 | 2017-08-22 19:04:06 +0200 | [diff] [blame] | 77 | |
| 78 | # runtime parameters: |
| 79 | if self.encryption is not None: |
Pau Espin Pedrol | abd556a | 2017-09-04 16:26:08 +0200 | [diff] [blame] | 80 | encryption_vty = util.encryption2osmovty(self.encryption) |
| 81 | else: |
| 82 | encryption_vty = util.encryption2osmovty(values['nitb']['net']['encryption']) |
| 83 | config.overlay(values, dict(nitb=dict(net=dict(encryption=encryption_vty)))) |
Pau Espin Pedrol | ca126b1 | 2017-08-22 19:04:06 +0200 | [diff] [blame] | 84 | |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 85 | self.config = values |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 86 | |
Neels Hofmeyr | 943c81d | 2017-05-22 20:16:03 +0200 | [diff] [blame] | 87 | self.dbg('NITB CONFIG:\n' + pprint.pformat(values)) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 88 | |
| 89 | with open(self.config_file, 'w') as f: |
| 90 | r = template.render('osmo-nitb.cfg', values) |
| 91 | self.dbg(r) |
| 92 | f.write(r) |
| 93 | |
| 94 | def addr(self): |
Neels Hofmeyr | 76d8103 | 2017-05-18 18:35:32 +0200 | [diff] [blame] | 95 | return self.ip_address.get('addr') |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 96 | |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 97 | def bts_add(self, bts): |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 98 | self.bts.append(bts) |
Neels Hofmeyr | c4ba4a6 | 2017-05-18 19:31:44 +0200 | [diff] [blame] | 99 | bts.set_bsc(self) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 100 | |
Pau Espin Pedrol | ca126b1 | 2017-08-22 19:04:06 +0200 | [diff] [blame] | 101 | def set_encryption(self, val): |
| 102 | self.encryption = val |
| 103 | |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 104 | def mcc(self): |
| 105 | return self.config['nitb']['net']['mcc'] |
| 106 | |
| 107 | def mnc(self): |
| 108 | return self.config['nitb']['net']['mnc'] |
| 109 | |
| 110 | def mcc_mnc(self): |
| 111 | return (self.mcc(), self.mnc()) |
| 112 | |
Pau Espin Pedrol | c859eb3 | 2017-12-11 15:01:13 +0100 | [diff] [blame] | 113 | def bts_num(self, bts): |
| 114 | 'Provide number id used by OsmoNITB to identify configured BTS' |
| 115 | # We take advantage from the fact that VTY code assigns VTY in ascending |
| 116 | # order through the bts nodes found. As we populate the config iterating |
| 117 | # over this list, we have a 1:1 match in indexes. |
| 118 | return self.bts.index(bts) |
| 119 | |
Pau Espin Pedrol | 713ce2c | 2017-08-24 16:57:17 +0200 | [diff] [blame] | 120 | def subscriber_add(self, modem, msisdn=None, algo=None): |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 121 | if msisdn is None: |
| 122 | msisdn = self.suite_run.resources_pool.next_msisdn(modem) |
| 123 | modem.set_msisdn(msisdn) |
Pau Espin Pedrol | 713ce2c | 2017-08-24 16:57:17 +0200 | [diff] [blame] | 124 | |
| 125 | if not algo: |
| 126 | alg_str = modem.auth_algo() |
| 127 | if not alg_str or alg_str == 'none': |
| 128 | algo = None |
| 129 | elif alg_str == 'comp128v1': |
| 130 | algo = 'comp128v1' |
| 131 | elif alg_str == 'xor': |
| 132 | algo = 'xor' |
| 133 | if algo is not None and not modem.ki(): |
| 134 | raise log.Error("Auth algo %r selected and no KI specified" % algo) |
| 135 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 136 | self.log('Add subscriber', msisdn=msisdn, imsi=modem.imsi()) |
Pau Espin Pedrol | 713ce2c | 2017-08-24 16:57:17 +0200 | [diff] [blame] | 137 | OsmoNitbCtrl(self).subscriber_add(modem.imsi(), msisdn, modem.ki(), algo) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 138 | |
Pau Espin Pedrol | e1a2622 | 2017-08-23 15:52:14 +0200 | [diff] [blame] | 139 | def subscriber_delete(self, modem): |
| 140 | self.log('Delete subscriber', imsi=modem.imsi()) |
| 141 | OsmoNitbCtrl(self).subscriber_delete(modem.imsi()) |
| 142 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 143 | def subscriber_attached(self, *modems): |
Neels Hofmeyr | b02c211 | 2017-04-09 18:46:48 +0200 | [diff] [blame] | 144 | return self.imsi_attached(*[m.imsi() for m in modems]) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 145 | |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 146 | def imsi_attached(self, *imsis): |
| 147 | attached = self.imsi_list_attached() |
Neels Hofmeyr | b02c211 | 2017-04-09 18:46:48 +0200 | [diff] [blame] | 148 | self.dbg('attached:', attached) |
| 149 | return all([(imsi in attached) for imsi in imsis]) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 150 | |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 151 | def imsi_list_attached(self): |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 152 | return OsmoNitbCtrl(self).subscriber_list_active() |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 153 | |
Pau Espin Pedrol | c859eb3 | 2017-12-11 15:01:13 +0100 | [diff] [blame] | 154 | def bts_is_connected(self, bts): |
| 155 | return OsmoNitbCtrl(self).bts_is_connected(self.bts_num(bts)) |
| 156 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 157 | def running(self): |
| 158 | return not self.process.terminated() |
| 159 | |
| 160 | |
| 161 | class OsmoNitbCtrl(log.Origin): |
| 162 | PORT = 4249 |
| 163 | SUBSCR_MODIFY_VAR = 'subscriber-modify-v1' |
| 164 | SUBSCR_MODIFY_REPLY_RE = re.compile("SET_REPLY (\d+) %s OK" % SUBSCR_MODIFY_VAR) |
Pau Espin Pedrol | e1a2622 | 2017-08-23 15:52:14 +0200 | [diff] [blame] | 165 | SUBSCR_DELETE_VAR = 'subscriber-delete-v1' |
| 166 | SUBSCR_DELETE_REPLY_RE = re.compile("SET_REPLY (\d+) %s Removed" % SUBSCR_DELETE_VAR) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 167 | SUBSCR_LIST_ACTIVE_VAR = 'subscriber-list-active-v1' |
Pau Espin Pedrol | c859eb3 | 2017-12-11 15:01:13 +0100 | [diff] [blame] | 168 | BTS_OML_STATE_VAR = "bts.%d.oml-connection-state" |
| 169 | BTS_OML_STATE_RE = re.compile("GET_REPLY (\d+) bts.\d+.oml-connection-state (?P<oml_state>\w+)") |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 170 | |
| 171 | def __init__(self, nitb): |
| 172 | self.nitb = nitb |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 173 | super().__init__(log.C_BUS, 'CTRL(%s:%d)' % (self.nitb.addr(), OsmoNitbCtrl.PORT)) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 174 | |
| 175 | def ctrl(self): |
| 176 | return osmo_ctrl.OsmoCtrl(self.nitb.addr(), OsmoNitbCtrl.PORT) |
| 177 | |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 178 | def subscriber_add(self, imsi, msisdn, ki=None, algo=None): |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 179 | if algo: |
| 180 | value = '%s,%s,%s,%s' % (imsi,msisdn,algo,ki) |
| 181 | else: |
| 182 | value = '%s,%s' % (imsi, msisdn) |
| 183 | |
Neels Hofmeyr | b02c211 | 2017-04-09 18:46:48 +0200 | [diff] [blame] | 184 | with self.ctrl() as ctrl: |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 185 | ctrl.do_set(OsmoNitbCtrl.SUBSCR_MODIFY_VAR, value) |
| 186 | data = ctrl.receive() |
| 187 | (answer, data) = ctrl.remove_ipa_ctrl_header(data) |
| 188 | answer_str = answer.decode('utf-8') |
| 189 | res = OsmoNitbCtrl.SUBSCR_MODIFY_REPLY_RE.match(answer_str) |
| 190 | if not res: |
| 191 | raise RuntimeError('Cannot create subscriber %r (answer=%r)' % (imsi, answer_str)) |
| 192 | self.dbg('Created subscriber', imsi=imsi, msisdn=msisdn) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 193 | |
Pau Espin Pedrol | e1a2622 | 2017-08-23 15:52:14 +0200 | [diff] [blame] | 194 | def subscriber_delete(self, imsi): |
| 195 | with self.ctrl() as ctrl: |
| 196 | ctrl.do_set(OsmoNitbCtrl.SUBSCR_DELETE_VAR, imsi) |
| 197 | data = ctrl.receive() |
| 198 | (answer, data) = ctrl.remove_ipa_ctrl_header(data) |
| 199 | answer_str = answer.decode('utf-8') |
| 200 | res = OsmoNitbCtrl.SUBSCR_DELETE_REPLY_RE.match(answer_str) |
| 201 | if not res: |
| 202 | raise RuntimeError('Cannot delete subscriber %r (answer=%r)' % (imsi, answer_str)) |
| 203 | self.dbg('Deleted subscriber', imsi=imsi) |
| 204 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 205 | def subscriber_list_active(self): |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 206 | aslist_str = "" |
Neels Hofmeyr | b02c211 | 2017-04-09 18:46:48 +0200 | [diff] [blame] | 207 | with self.ctrl() as ctrl: |
| 208 | ctrl.do_get(OsmoNitbCtrl.SUBSCR_LIST_ACTIVE_VAR) |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 209 | # This is legacy code from the old osmo-gsm-tester. |
| 210 | # looks like this doesn't work for long data. |
Neels Hofmeyr | b02c211 | 2017-04-09 18:46:48 +0200 | [diff] [blame] | 211 | data = ctrl.receive() |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 212 | while (len(data) > 0): |
Neels Hofmeyr | b02c211 | 2017-04-09 18:46:48 +0200 | [diff] [blame] | 213 | (answer, data) = ctrl.remove_ipa_ctrl_header(data) |
| 214 | answer_str = answer.decode('utf-8') |
| 215 | answer_str = answer_str.replace('\n', ' ') |
| 216 | aslist_str = answer_str |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 217 | return aslist_str |
| 218 | |
Pau Espin Pedrol | c859eb3 | 2017-12-11 15:01:13 +0100 | [diff] [blame] | 219 | def bts_is_connected(self, bts_num): |
| 220 | with self.ctrl() as ctrl: |
| 221 | ctrl.do_get(OsmoNitbCtrl.BTS_OML_STATE_VAR % bts_num) |
| 222 | data = ctrl.receive() |
| 223 | while (len(data) > 0): |
| 224 | (answer, data) = ctrl.remove_ipa_ctrl_header(data) |
| 225 | answer_str = answer.decode('utf-8') |
| 226 | answer_str = answer_str.replace('\n', ' ') |
| 227 | res = OsmoNitbCtrl.BTS_OML_STATE_RE.match(answer_str) |
| 228 | if res: |
| 229 | oml_state = str(res.group('oml_state')) |
| 230 | if oml_state == 'connected': |
| 231 | return True |
| 232 | return False |
| 233 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 234 | # vim: expandtab tabstop=4 shiftwidth=4 |