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 | |
Neels Hofmeyr | 1fe012e | 2017-05-14 15:45:05 +0200 | [diff] [blame] | 24 | from . import log, util, config, template, process, osmo_ctrl, pcap_recorder |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 25 | |
| 26 | class OsmoNitb(log.Origin): |
| 27 | suite_run = None |
Neels Hofmeyr | 76d8103 | 2017-05-18 18:35:32 +0200 | [diff] [blame] | 28 | ip_address = None |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 29 | run_dir = None |
| 30 | config_file = None |
| 31 | process = None |
| 32 | bts = None |
| 33 | |
Neels Hofmeyr | 76d8103 | 2017-05-18 18:35:32 +0200 | [diff] [blame] | 34 | def __init__(self, suite_run, ip_address): |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 35 | self.suite_run = suite_run |
Neels Hofmeyr | 76d8103 | 2017-05-18 18:35:32 +0200 | [diff] [blame] | 36 | self.ip_address = ip_address |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 37 | self.set_log_category(log.C_RUN) |
Neels Hofmeyr | 76d8103 | 2017-05-18 18:35:32 +0200 | [diff] [blame] | 38 | self.set_name('osmo-nitb_%s' % ip_address.get('addr')) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 39 | self.bts = [] |
| 40 | |
| 41 | def start(self): |
| 42 | self.log('Starting osmo-nitb') |
| 43 | self.run_dir = util.Dir(self.suite_run.trial.get_run_dir().new_dir(self.name())) |
| 44 | self.configure() |
Your Name | 3c6673a | 2017-04-08 18:52:39 +0200 | [diff] [blame] | 45 | inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-nitb'))) |
| 46 | binary = inst.child('bin', 'osmo-nitb') |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 47 | if not os.path.isfile(binary): |
| 48 | raise RuntimeError('Binary missing: %r' % binary) |
Your Name | 3c6673a | 2017-04-08 18:52:39 +0200 | [diff] [blame] | 49 | lib = inst.child('lib') |
| 50 | if not os.path.isdir(lib): |
| 51 | raise RuntimeError('No lib/ in %r' % inst) |
Pau Espin Pedrol | e39c6f1 | 2017-05-08 16:21:38 +0200 | [diff] [blame] | 52 | |
Pau Espin Pedrol | 13143bc | 2017-05-08 17:07:28 +0200 | [diff] [blame] | 53 | iface = util.ip_to_iface(self.addr()) |
Neels Hofmeyr | 95fd673 | 2017-05-15 14:03:07 +0200 | [diff] [blame] | 54 | pcap_recorder.PcapRecorder(self.suite_run, self.run_dir.new_dir('pcap'), iface, |
Pau Espin Pedrol | 8cd347b | 2017-05-12 16:24:21 +0200 | [diff] [blame] | 55 | 'host %s and port not 22' % self.addr()) |
Pau Espin Pedrol | 13143bc | 2017-05-08 17:07:28 +0200 | [diff] [blame] | 56 | |
Pau Espin Pedrol | e39c6f1 | 2017-05-08 16:21:38 +0200 | [diff] [blame] | 57 | env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) } |
| 58 | |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 59 | self.dbg(run_dir=self.run_dir, binary=binary, env=env) |
| 60 | self.process = process.Process(self.name(), self.run_dir, |
| 61 | (binary, '-c', |
| 62 | os.path.abspath(self.config_file)), |
| 63 | env=env) |
| 64 | self.suite_run.remember_to_stop(self.process) |
| 65 | self.process.launch() |
| 66 | |
| 67 | def configure(self): |
| 68 | self.config_file = self.run_dir.new_file('osmo-nitb.cfg') |
| 69 | self.dbg(config_file=self.config_file) |
| 70 | |
| 71 | values = dict(nitb=config.get_defaults('nitb')) |
| 72 | config.overlay(values, self.suite_run.config()) |
Neels Hofmeyr | 4f59ba6 | 2017-05-22 20:04:05 +0200 | [diff] [blame] | 73 | config.overlay(values, dict(nitb=dict(ip_address=self.ip_address))) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 74 | |
| 75 | bts_list = [] |
| 76 | for bts in self.bts: |
Neels Hofmeyr | c4ba4a6 | 2017-05-18 19:31:44 +0200 | [diff] [blame] | 77 | bts_list.append(bts.conf_for_bsc()) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 78 | config.overlay(values, dict(nitb=dict(net=dict(bts_list=bts_list)))) |
| 79 | |
Neels Hofmeyr | 943c81d | 2017-05-22 20:16:03 +0200 | [diff] [blame] | 80 | self.dbg('NITB CONFIG:\n' + pprint.pformat(values)) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 81 | |
| 82 | with open(self.config_file, 'w') as f: |
| 83 | r = template.render('osmo-nitb.cfg', values) |
| 84 | self.dbg(r) |
| 85 | f.write(r) |
| 86 | |
| 87 | def addr(self): |
Neels Hofmeyr | 76d8103 | 2017-05-18 18:35:32 +0200 | [diff] [blame] | 88 | return self.ip_address.get('addr') |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 89 | |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 90 | def bts_add(self, bts): |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 91 | self.bts.append(bts) |
Neels Hofmeyr | c4ba4a6 | 2017-05-18 19:31:44 +0200 | [diff] [blame] | 92 | bts.set_bsc(self) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 93 | |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 94 | def subscriber_add(self, modem, msisdn=None): |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 95 | if msisdn is None: |
| 96 | msisdn = self.suite_run.resources_pool.next_msisdn(modem) |
| 97 | modem.set_msisdn(msisdn) |
| 98 | self.log('Add subscriber', msisdn=msisdn, imsi=modem.imsi()) |
| 99 | with self: |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 100 | OsmoNitbCtrl(self).subscriber_add(modem.imsi(), msisdn, modem.ki()) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 101 | |
| 102 | def subscriber_attached(self, *modems): |
Neels Hofmeyr | b02c211 | 2017-04-09 18:46:48 +0200 | [diff] [blame] | 103 | return self.imsi_attached(*[m.imsi() for m in modems]) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 104 | |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 105 | def imsi_attached(self, *imsis): |
| 106 | attached = self.imsi_list_attached() |
Neels Hofmeyr | b02c211 | 2017-04-09 18:46:48 +0200 | [diff] [blame] | 107 | self.dbg('attached:', attached) |
| 108 | return all([(imsi in attached) for imsi in imsis]) |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 109 | |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 110 | def imsi_list_attached(self): |
| 111 | with self: |
Neels Hofmeyr | b02c211 | 2017-04-09 18:46:48 +0200 | [diff] [blame] | 112 | return OsmoNitbCtrl(self).subscriber_list_active() |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 113 | |
| 114 | def running(self): |
| 115 | return not self.process.terminated() |
| 116 | |
| 117 | |
| 118 | class OsmoNitbCtrl(log.Origin): |
| 119 | PORT = 4249 |
| 120 | SUBSCR_MODIFY_VAR = 'subscriber-modify-v1' |
| 121 | SUBSCR_MODIFY_REPLY_RE = re.compile("SET_REPLY (\d+) %s OK" % SUBSCR_MODIFY_VAR) |
| 122 | SUBSCR_LIST_ACTIVE_VAR = 'subscriber-list-active-v1' |
| 123 | |
| 124 | def __init__(self, nitb): |
| 125 | self.nitb = nitb |
| 126 | self.set_name('CTRL(%s:%d)' % (self.nitb.addr(), OsmoNitbCtrl.PORT)) |
| 127 | self.set_child_of(nitb) |
| 128 | |
| 129 | def ctrl(self): |
| 130 | return osmo_ctrl.OsmoCtrl(self.nitb.addr(), OsmoNitbCtrl.PORT) |
| 131 | |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 132 | def subscriber_add(self, imsi, msisdn, ki=None, algo=None): |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 133 | created = False |
| 134 | if ki and not algo: |
| 135 | algo = 'comp128v1' |
| 136 | |
| 137 | if algo: |
| 138 | value = '%s,%s,%s,%s' % (imsi,msisdn,algo,ki) |
| 139 | else: |
| 140 | value = '%s,%s' % (imsi, msisdn) |
| 141 | |
Neels Hofmeyr | b02c211 | 2017-04-09 18:46:48 +0200 | [diff] [blame] | 142 | with self.ctrl() as ctrl: |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 143 | ctrl.do_set(OsmoNitbCtrl.SUBSCR_MODIFY_VAR, value) |
| 144 | data = ctrl.receive() |
| 145 | (answer, data) = ctrl.remove_ipa_ctrl_header(data) |
| 146 | answer_str = answer.decode('utf-8') |
| 147 | res = OsmoNitbCtrl.SUBSCR_MODIFY_REPLY_RE.match(answer_str) |
| 148 | if not res: |
| 149 | raise RuntimeError('Cannot create subscriber %r (answer=%r)' % (imsi, answer_str)) |
| 150 | self.dbg('Created subscriber', imsi=imsi, msisdn=msisdn) |
| 151 | return True |
| 152 | |
| 153 | def subscriber_list_active(self): |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 154 | aslist_str = "" |
Neels Hofmeyr | b02c211 | 2017-04-09 18:46:48 +0200 | [diff] [blame] | 155 | with self.ctrl() as ctrl: |
| 156 | ctrl.do_get(OsmoNitbCtrl.SUBSCR_LIST_ACTIVE_VAR) |
Neels Hofmeyr | b3daaea | 2017-04-09 14:18:34 +0200 | [diff] [blame] | 157 | # This is legacy code from the old osmo-gsm-tester. |
| 158 | # looks like this doesn't work for long data. |
Neels Hofmeyr | b02c211 | 2017-04-09 18:46:48 +0200 | [diff] [blame] | 159 | data = ctrl.receive() |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 160 | while (len(data) > 0): |
Neels Hofmeyr | b02c211 | 2017-04-09 18:46:48 +0200 | [diff] [blame] | 161 | (answer, data) = ctrl.remove_ipa_ctrl_header(data) |
| 162 | answer_str = answer.decode('utf-8') |
| 163 | answer_str = answer_str.replace('\n', ' ') |
| 164 | aslist_str = answer_str |
Neels Hofmeyr | 3531a19 | 2017-03-28 14:30:28 +0200 | [diff] [blame] | 165 | return aslist_str |
| 166 | |
| 167 | # vim: expandtab tabstop=4 shiftwidth=4 |