Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 1 | # osmo_gsm_tester: specifics for running an osmo-msc |
| 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 |
| 21 | import pprint |
| 22 | |
Pau Espin Pedrol | 2d16f6f | 2017-05-30 15:33:57 +0200 | [diff] [blame] | 23 | from . import log, util, config, template, process, osmo_ctrl, pcap_recorder, smsc |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 24 | |
| 25 | class OsmoMsc(log.Origin): |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 26 | |
Pau Espin Pedrol | 86ea02f | 2018-02-26 12:14:46 +0100 | [diff] [blame] | 27 | def __init__(self, suite_run, hlr, mgw, stp, ip_address): |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 28 | super().__init__(log.C_RUN, 'osmo-msc_%s' % ip_address.get('addr')) |
Pau Espin Pedrol | 5860367 | 2018-08-09 13:45:55 +0200 | [diff] [blame] | 29 | self.run_dir = None |
| 30 | self.config_file = None |
| 31 | self.process = None |
| 32 | self.config = None |
| 33 | self.encryption = None |
| 34 | self.authentication = None |
Pau Espin Pedrol | fed578e | 2019-06-19 17:18:43 +0200 | [diff] [blame] | 35 | self.use_osmux = "off" |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 36 | self.suite_run = suite_run |
| 37 | self.ip_address = ip_address |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 38 | self.hlr = hlr |
Pau Espin Pedrol | 86ea02f | 2018-02-26 12:14:46 +0100 | [diff] [blame] | 39 | self.mgw = mgw |
Pau Espin Pedrol | 1e1d381 | 2017-11-16 18:06:37 +0100 | [diff] [blame] | 40 | self.stp = stp |
Pau Espin Pedrol | 2d16f6f | 2017-05-30 15:33:57 +0200 | [diff] [blame] | 41 | self.smsc = smsc.Smsc((ip_address.get('addr'), 2775)) |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 42 | |
| 43 | def start(self): |
| 44 | self.log('Starting osmo-msc') |
Pau Espin Pedrol | d091233 | 2017-06-14 13:27:08 +0200 | [diff] [blame] | 45 | self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name())) |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 46 | self.configure() |
| 47 | inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-msc'))) |
| 48 | binary = inst.child('bin', 'osmo-msc') |
| 49 | if not os.path.isfile(binary): |
| 50 | raise RuntimeError('Binary missing: %r' % binary) |
| 51 | lib = inst.child('lib') |
| 52 | if not os.path.isdir(lib): |
| 53 | raise RuntimeError('No lib/ in %r' % inst) |
| 54 | |
Pau Espin Pedrol | 9cc1d08 | 2017-11-20 17:40:25 +0100 | [diff] [blame] | 55 | pcap_recorder.PcapRecorder(self.suite_run, self.run_dir.new_dir('pcap'), None, |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 56 | 'host %s and port not 22' % self.addr()) |
| 57 | |
| 58 | env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) } |
| 59 | |
| 60 | self.dbg(run_dir=self.run_dir, binary=binary, env=env) |
| 61 | self.process = process.Process(self.name(), self.run_dir, |
| 62 | (binary, '-c', |
| 63 | os.path.abspath(self.config_file)), |
| 64 | env=env) |
| 65 | self.suite_run.remember_to_stop(self.process) |
| 66 | self.process.launch() |
| 67 | |
| 68 | def configure(self): |
| 69 | self.config_file = self.run_dir.new_file('osmo-msc.cfg') |
| 70 | self.dbg(config_file=self.config_file) |
| 71 | |
| 72 | values = dict(msc=config.get_defaults('msc')) |
| 73 | config.overlay(values, self.suite_run.config()) |
| 74 | config.overlay(values, dict(msc=dict(ip_address=self.ip_address))) |
Pau Espin Pedrol | 86ea02f | 2018-02-26 12:14:46 +0100 | [diff] [blame] | 75 | config.overlay(values, self.mgw.conf_for_client()) |
Pau Espin Pedrol | 5bbb300 | 2017-11-23 11:07:02 +0100 | [diff] [blame] | 76 | config.overlay(values, self.hlr.conf_for_client()) |
Pau Espin Pedrol | 1e1d381 | 2017-11-16 18:06:37 +0100 | [diff] [blame] | 77 | config.overlay(values, self.stp.conf_for_client()) |
Pau Espin Pedrol | 2d16f6f | 2017-05-30 15:33:57 +0200 | [diff] [blame] | 78 | config.overlay(values, self.smsc.get_config()) |
Pau Espin Pedrol | ca126b1 | 2017-08-22 19:04:06 +0200 | [diff] [blame] | 79 | |
| 80 | # runtime parameters: |
| 81 | if self.encryption is not None: |
Pau Espin Pedrol | abd556a | 2017-09-04 16:26:08 +0200 | [diff] [blame] | 82 | encryption_vty = util.encryption2osmovty(self.encryption) |
| 83 | else: |
| 84 | encryption_vty = util.encryption2osmovty(values['msc']['net']['encryption']) |
| 85 | config.overlay(values, dict(msc=dict(net=dict(encryption=encryption_vty)))) |
Pau Espin Pedrol | b5a8614 | 2017-08-23 17:39:54 +0200 | [diff] [blame] | 86 | if self.authentication is not None: |
| 87 | config.overlay(values, dict(msc=dict(net=dict(authentication=self.authentication)))) |
Pau Espin Pedrol | fed578e | 2019-06-19 17:18:43 +0200 | [diff] [blame] | 88 | config.overlay(values, dict(msc=dict(use_osmux=self.use_osmux))) |
Pau Espin Pedrol | b5a8614 | 2017-08-23 17:39:54 +0200 | [diff] [blame] | 89 | |
Pau Espin Pedrol | ca126b1 | 2017-08-22 19:04:06 +0200 | [diff] [blame] | 90 | |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 91 | self.config = values |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 92 | |
| 93 | self.dbg('MSC CONFIG:\n' + pprint.pformat(values)) |
| 94 | |
| 95 | with open(self.config_file, 'w') as f: |
| 96 | r = template.render('osmo-msc.cfg', values) |
| 97 | self.dbg(r) |
| 98 | f.write(r) |
| 99 | |
| 100 | def addr(self): |
| 101 | return self.ip_address.get('addr') |
| 102 | |
Pau Espin Pedrol | ca126b1 | 2017-08-22 19:04:06 +0200 | [diff] [blame] | 103 | def set_encryption(self, val): |
| 104 | self.encryption = val |
| 105 | |
Pau Espin Pedrol | b5a8614 | 2017-08-23 17:39:54 +0200 | [diff] [blame] | 106 | def set_authentication(self, val): |
| 107 | if val is None: |
| 108 | self.authroziation = None |
| 109 | return |
| 110 | self.authentication = "required" if val else "optional" |
| 111 | |
Pau Espin Pedrol | fed578e | 2019-06-19 17:18:43 +0200 | [diff] [blame] | 112 | def set_use_osmux(self, use=False, force=False): |
| 113 | if not use: |
| 114 | self.use_osmux = "off" |
| 115 | else: |
| 116 | if not force: |
| 117 | self.use_osmux = "on" |
| 118 | else: |
| 119 | self.use_osmux = "only" |
| 120 | |
Pau Espin Pedrol | 0e57aad | 2017-05-29 14:25:22 +0200 | [diff] [blame] | 121 | def mcc(self): |
| 122 | return self.config['msc']['net']['mcc'] |
| 123 | |
| 124 | def mnc(self): |
| 125 | return self.config['msc']['net']['mnc'] |
| 126 | |
| 127 | def mcc_mnc(self): |
| 128 | return (self.mcc(), self.mnc()) |
| 129 | |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 130 | def subscriber_attached(self, *modems): |
| 131 | return self.imsi_attached(*[m.imsi() for m in modems]) |
| 132 | |
| 133 | def imsi_attached(self, *imsis): |
| 134 | attached = self.imsi_list_attached() |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 135 | log.dbg('attached:', attached) |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 136 | return all([(imsi in attached) for imsi in imsis]) |
| 137 | |
| 138 | def imsi_list_attached(self): |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 139 | return OsmoMscCtrl(self).subscriber_list_active() |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 140 | |
| 141 | def running(self): |
| 142 | return not self.process.terminated() |
| 143 | |
| 144 | |
| 145 | class OsmoMscCtrl(log.Origin): |
| 146 | PORT = 4255 |
| 147 | SUBSCR_LIST_ACTIVE_VAR = 'subscriber-list-active-v1' |
| 148 | |
| 149 | def __init__(self, msc): |
| 150 | self.msc = msc |
Neels Hofmeyr | 1a7a3f0 | 2017-06-10 01:18:27 +0200 | [diff] [blame] | 151 | super().__init__(log.C_BUS, 'CTRL(%s:%d)' % (self.msc.addr(), self.PORT)) |
Neels Hofmeyr | 798e592 | 2017-05-18 15:24:02 +0200 | [diff] [blame] | 152 | |
| 153 | def ctrl(self): |
| 154 | return osmo_ctrl.OsmoCtrl(self.msc.addr(), self.PORT) |
| 155 | |
| 156 | def subscriber_list_active(self): |
| 157 | aslist_str = "" |
| 158 | with self.ctrl() as ctrl: |
| 159 | ctrl.do_get(self.SUBSCR_LIST_ACTIVE_VAR) |
| 160 | # This is legacy code from the old osmo-gsm-tester. |
| 161 | # looks like this doesn't work for long data. |
| 162 | data = ctrl.receive() |
| 163 | while (len(data) > 0): |
| 164 | (answer, data) = ctrl.remove_ipa_ctrl_header(data) |
| 165 | answer_str = answer.decode('utf-8') |
| 166 | answer_str = answer_str.replace('\n', ' ') |
| 167 | aslist_str = answer_str |
| 168 | return aslist_str |
| 169 | |
| 170 | # vim: expandtab tabstop=4 shiftwidth=4 |