blob: c12648fabd6b6d0d8870800fd694a1b6e2023dc6 [file] [log] [blame]
Neels Hofmeyr3531a192017-03-28 14:30:28 +02001# 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 Welte27205342017-06-03 09:51:45 +02008# it under the terms of the GNU General Public License as
Neels Hofmeyr3531a192017-03-28 14:30:28 +02009# 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 Welte27205342017-06-03 09:51:45 +020015# GNU General Public License for more details.
Neels Hofmeyr3531a192017-03-28 14:30:28 +020016#
Harald Welte27205342017-06-03 09:51:45 +020017# You should have received a copy of the GNU General Public License
Neels Hofmeyr3531a192017-03-28 14:30:28 +020018# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20import os
Neels Hofmeyr3531a192017-03-28 14:30:28 +020021import re
Neels Hofmeyr943c81d2017-05-22 20:16:03 +020022import pprint
Neels Hofmeyr3531a192017-03-28 14:30:28 +020023
Pau Espin Pedrole1a58bd2020-04-10 20:46:07 +020024from ..core import log, util, config, template, process
Pau Espin Pedrole8bbcbf2020-04-10 19:51:31 +020025from . import osmo_ctrl, pcap_recorder, smsc
Neels Hofmeyr3531a192017-03-28 14:30:28 +020026
27class OsmoNitb(log.Origin):
Neels Hofmeyr3531a192017-03-28 14:30:28 +020028
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020029 def __init__(self, testenv, ip_address):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020030 super().__init__(log.C_RUN, 'osmo-nitb_%s' % ip_address.get('addr'))
Pau Espin Pedrol58603672018-08-09 13:45:55 +020031 self.run_dir = None
32 self.config_file = None
33 self.process = None
34 self.encryption = None
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020035 self.testenv = testenv
Neels Hofmeyr76d81032017-05-18 18:35:32 +020036 self.ip_address = ip_address
Neels Hofmeyr3531a192017-03-28 14:30:28 +020037 self.bts = []
Pau Espin Pedrol2d16f6f2017-05-30 15:33:57 +020038 self.smsc = smsc.Smsc((ip_address.get('addr'), 2775))
Neels Hofmeyr3531a192017-03-28 14:30:28 +020039
40 def start(self):
41 self.log('Starting osmo-nitb')
Pau Espin Pedrol2a2d8462020-05-11 10:56:52 +020042 self.run_dir = util.Dir(self.testenv.test().get_run_dir().new_dir(self.name()))
Neels Hofmeyr3531a192017-03-28 14:30:28 +020043 self.configure()
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020044 inst = util.Dir(os.path.abspath(self.testenv.suite().trial().get_inst('osmo-nitb')))
Your Name3c6673a2017-04-08 18:52:39 +020045 binary = inst.child('bin', 'osmo-nitb')
Neels Hofmeyr3531a192017-03-28 14:30:28 +020046 if not os.path.isfile(binary):
47 raise RuntimeError('Binary missing: %r' % binary)
Your Name3c6673a2017-04-08 18:52:39 +020048 lib = inst.child('lib')
49 if not os.path.isdir(lib):
50 raise RuntimeError('No lib/ in %r' % inst)
Pau Espin Pedrole39c6f12017-05-08 16:21:38 +020051
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020052 pcap_recorder.PcapRecorder(self.testenv, self.run_dir.new_dir('pcap'), None,
Pau Espin Pedrol8cd347b2017-05-12 16:24:21 +020053 'host %s and port not 22' % self.addr())
Pau Espin Pedrol13143bc2017-05-08 17:07:28 +020054
Pau Espin Pedrole39c6f12017-05-08 16:21:38 +020055 env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
56
Neels Hofmeyr3531a192017-03-28 14:30:28 +020057 self.dbg(run_dir=self.run_dir, binary=binary, env=env)
58 self.process = process.Process(self.name(), self.run_dir,
59 (binary, '-c',
60 os.path.abspath(self.config_file)),
61 env=env)
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020062 self.testenv.remember_to_stop(self.process)
Neels Hofmeyr3531a192017-03-28 14:30:28 +020063 self.process.launch()
64
65 def configure(self):
66 self.config_file = self.run_dir.new_file('osmo-nitb.cfg')
67 self.dbg(config_file=self.config_file)
68
69 values = dict(nitb=config.get_defaults('nitb'))
Pau Espin Pedrola442cb82020-05-05 12:54:37 +020070 config.overlay(values, self.testenv.suite().config())
Neels Hofmeyr4f59ba62017-05-22 20:04:05 +020071 config.overlay(values, dict(nitb=dict(ip_address=self.ip_address)))
Neels Hofmeyr3531a192017-03-28 14:30:28 +020072
73 bts_list = []
74 for bts in self.bts:
Neels Hofmeyrc4ba4a62017-05-18 19:31:44 +020075 bts_list.append(bts.conf_for_bsc())
Neels Hofmeyr3531a192017-03-28 14:30:28 +020076 config.overlay(values, dict(nitb=dict(net=dict(bts_list=bts_list))))
Pau Espin Pedrol2d16f6f2017-05-30 15:33:57 +020077 config.overlay(values, self.smsc.get_config())
Pau Espin Pedrolca126b12017-08-22 19:04:06 +020078
79 # runtime parameters:
80 if self.encryption is not None:
Pau Espin Pedrolabd556a2017-09-04 16:26:08 +020081 encryption_vty = util.encryption2osmovty(self.encryption)
82 else:
83 encryption_vty = util.encryption2osmovty(values['nitb']['net']['encryption'])
84 config.overlay(values, dict(nitb=dict(net=dict(encryption=encryption_vty))))
Pau Espin Pedrolca126b12017-08-22 19:04:06 +020085
Pau Espin Pedrol0e57aad2017-05-29 14:25:22 +020086 self.config = values
Neels Hofmeyr3531a192017-03-28 14:30:28 +020087
Neels Hofmeyr943c81d2017-05-22 20:16:03 +020088 self.dbg('NITB CONFIG:\n' + pprint.pformat(values))
Neels Hofmeyr3531a192017-03-28 14:30:28 +020089
90 with open(self.config_file, 'w') as f:
91 r = template.render('osmo-nitb.cfg', values)
92 self.dbg(r)
93 f.write(r)
94
95 def addr(self):
Neels Hofmeyr76d81032017-05-18 18:35:32 +020096 return self.ip_address.get('addr')
Neels Hofmeyr3531a192017-03-28 14:30:28 +020097
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +020098 def bts_add(self, bts):
Neels Hofmeyr3531a192017-03-28 14:30:28 +020099 self.bts.append(bts)
Neels Hofmeyrc4ba4a62017-05-18 19:31:44 +0200100 bts.set_bsc(self)
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200101
Pau Espin Pedrolca126b12017-08-22 19:04:06 +0200102 def set_encryption(self, val):
103 self.encryption = val
104
Pau Espin Pedrol0e57aad2017-05-29 14:25:22 +0200105 def mcc(self):
106 return self.config['nitb']['net']['mcc']
107
108 def mnc(self):
109 return self.config['nitb']['net']['mnc']
110
111 def mcc_mnc(self):
112 return (self.mcc(), self.mnc())
113
Pau Espin Pedrolc859eb32017-12-11 15:01:13 +0100114 def bts_num(self, bts):
115 'Provide number id used by OsmoNITB to identify configured BTS'
116 # We take advantage from the fact that VTY code assigns VTY in ascending
117 # order through the bts nodes found. As we populate the config iterating
118 # over this list, we have a 1:1 match in indexes.
119 return self.bts.index(bts)
120
Pau Espin Pedrol713ce2c2017-08-24 16:57:17 +0200121 def subscriber_add(self, modem, msisdn=None, algo=None):
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200122 if msisdn is None:
Pau Espin Pedrola442cb82020-05-05 12:54:37 +0200123 msisdn = self.testenv.msisdn()
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200124 modem.set_msisdn(msisdn)
Pau Espin Pedrol713ce2c2017-08-24 16:57:17 +0200125
126 if not algo:
127 alg_str = modem.auth_algo()
128 if not alg_str or alg_str == 'none':
129 algo = None
130 elif alg_str == 'comp128v1':
131 algo = 'comp128v1'
132 elif alg_str == 'xor':
133 algo = 'xor'
134 if algo is not None and not modem.ki():
135 raise log.Error("Auth algo %r selected and no KI specified" % algo)
136
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200137 self.log('Add subscriber', msisdn=msisdn, imsi=modem.imsi())
Pau Espin Pedrol713ce2c2017-08-24 16:57:17 +0200138 OsmoNitbCtrl(self).subscriber_add(modem.imsi(), msisdn, modem.ki(), algo)
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200139
Pau Espin Pedrole1a26222017-08-23 15:52:14 +0200140 def subscriber_delete(self, modem):
141 self.log('Delete subscriber', imsi=modem.imsi())
142 OsmoNitbCtrl(self).subscriber_delete(modem.imsi())
143
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200144 def subscriber_attached(self, *modems):
Neels Hofmeyrb02c2112017-04-09 18:46:48 +0200145 return self.imsi_attached(*[m.imsi() for m in modems])
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200146
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200147 def imsi_attached(self, *imsis):
148 attached = self.imsi_list_attached()
Neels Hofmeyrb02c2112017-04-09 18:46:48 +0200149 self.dbg('attached:', attached)
150 return all([(imsi in attached) for imsi in imsis])
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200151
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200152 def imsi_list_attached(self):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200153 return OsmoNitbCtrl(self).subscriber_list_active()
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200154
Pau Espin Pedrolc859eb32017-12-11 15:01:13 +0100155 def bts_is_connected(self, bts):
156 return OsmoNitbCtrl(self).bts_is_connected(self.bts_num(bts))
157
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200158 def running(self):
159 return not self.process.terminated()
160
161
162class OsmoNitbCtrl(log.Origin):
163 PORT = 4249
164 SUBSCR_MODIFY_VAR = 'subscriber-modify-v1'
165 SUBSCR_MODIFY_REPLY_RE = re.compile("SET_REPLY (\d+) %s OK" % SUBSCR_MODIFY_VAR)
Pau Espin Pedrole1a26222017-08-23 15:52:14 +0200166 SUBSCR_DELETE_VAR = 'subscriber-delete-v1'
167 SUBSCR_DELETE_REPLY_RE = re.compile("SET_REPLY (\d+) %s Removed" % SUBSCR_DELETE_VAR)
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200168 SUBSCR_LIST_ACTIVE_VAR = 'subscriber-list-active-v1'
Pau Espin Pedrolc859eb32017-12-11 15:01:13 +0100169 BTS_OML_STATE_VAR = "bts.%d.oml-connection-state"
170 BTS_OML_STATE_RE = re.compile("GET_REPLY (\d+) bts.\d+.oml-connection-state (?P<oml_state>\w+)")
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200171
172 def __init__(self, nitb):
173 self.nitb = nitb
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200174 super().__init__(log.C_BUS, 'CTRL(%s:%d)' % (self.nitb.addr(), OsmoNitbCtrl.PORT))
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200175
176 def ctrl(self):
177 return osmo_ctrl.OsmoCtrl(self.nitb.addr(), OsmoNitbCtrl.PORT)
178
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200179 def subscriber_add(self, imsi, msisdn, ki=None, algo=None):
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200180 if algo:
181 value = '%s,%s,%s,%s' % (imsi,msisdn,algo,ki)
182 else:
183 value = '%s,%s' % (imsi, msisdn)
184
Neels Hofmeyrb02c2112017-04-09 18:46:48 +0200185 with self.ctrl() as ctrl:
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200186 ctrl.do_set(OsmoNitbCtrl.SUBSCR_MODIFY_VAR, value)
187 data = ctrl.receive()
188 (answer, data) = ctrl.remove_ipa_ctrl_header(data)
189 answer_str = answer.decode('utf-8')
190 res = OsmoNitbCtrl.SUBSCR_MODIFY_REPLY_RE.match(answer_str)
191 if not res:
192 raise RuntimeError('Cannot create subscriber %r (answer=%r)' % (imsi, answer_str))
193 self.dbg('Created subscriber', imsi=imsi, msisdn=msisdn)
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200194
Pau Espin Pedrole1a26222017-08-23 15:52:14 +0200195 def subscriber_delete(self, imsi):
196 with self.ctrl() as ctrl:
197 ctrl.do_set(OsmoNitbCtrl.SUBSCR_DELETE_VAR, imsi)
198 data = ctrl.receive()
199 (answer, data) = ctrl.remove_ipa_ctrl_header(data)
200 answer_str = answer.decode('utf-8')
201 res = OsmoNitbCtrl.SUBSCR_DELETE_REPLY_RE.match(answer_str)
202 if not res:
203 raise RuntimeError('Cannot delete subscriber %r (answer=%r)' % (imsi, answer_str))
204 self.dbg('Deleted subscriber', imsi=imsi)
205
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200206 def subscriber_list_active(self):
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200207 aslist_str = ""
Neels Hofmeyrb02c2112017-04-09 18:46:48 +0200208 with self.ctrl() as ctrl:
209 ctrl.do_get(OsmoNitbCtrl.SUBSCR_LIST_ACTIVE_VAR)
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200210 # This is legacy code from the old osmo-gsm-tester.
211 # looks like this doesn't work for long data.
Neels Hofmeyrb02c2112017-04-09 18:46:48 +0200212 data = ctrl.receive()
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200213 while (len(data) > 0):
Neels Hofmeyrb02c2112017-04-09 18:46:48 +0200214 (answer, data) = ctrl.remove_ipa_ctrl_header(data)
215 answer_str = answer.decode('utf-8')
216 answer_str = answer_str.replace('\n', ' ')
217 aslist_str = answer_str
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200218 return aslist_str
219
Pau Espin Pedrolc859eb32017-12-11 15:01:13 +0100220 def bts_is_connected(self, bts_num):
221 with self.ctrl() as ctrl:
222 ctrl.do_get(OsmoNitbCtrl.BTS_OML_STATE_VAR % bts_num)
223 data = ctrl.receive()
224 while (len(data) > 0):
225 (answer, data) = ctrl.remove_ipa_ctrl_header(data)
226 answer_str = answer.decode('utf-8')
227 answer_str = answer_str.replace('\n', ' ')
228 res = OsmoNitbCtrl.BTS_OML_STATE_RE.match(answer_str)
229 if res:
230 oml_state = str(res.group('oml_state'))
231 if oml_state == 'connected':
232 return True
233 return False
234
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200235# vim: expandtab tabstop=4 shiftwidth=4