blob: 8f91bbdf333e463f5697a6aa8534f0e4ff106b58 [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 Pedrol2d16f6f2017-05-30 15:33:57 +020024from . import log, util, config, template, process, osmo_ctrl, pcap_recorder, smsc
Neels Hofmeyr3531a192017-03-28 14:30:28 +020025
26class OsmoNitb(log.Origin):
27 suite_run = None
Neels Hofmeyr76d81032017-05-18 18:35:32 +020028 ip_address = None
Neels Hofmeyr3531a192017-03-28 14:30:28 +020029 run_dir = None
30 config_file = None
31 process = None
32 bts = None
Pau Espin Pedrol2d16f6f2017-05-30 15:33:57 +020033 smsc = None
Pau Espin Pedrolca126b12017-08-22 19:04:06 +020034 encryption = None
Neels Hofmeyr3531a192017-03-28 14:30:28 +020035
Neels Hofmeyr76d81032017-05-18 18:35:32 +020036 def __init__(self, suite_run, ip_address):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +020037 super().__init__(log.C_RUN, 'osmo-nitb_%s' % ip_address.get('addr'))
Neels Hofmeyr3531a192017-03-28 14:30:28 +020038 self.suite_run = suite_run
Neels Hofmeyr76d81032017-05-18 18:35:32 +020039 self.ip_address = ip_address
Neels Hofmeyr3531a192017-03-28 14:30:28 +020040 self.bts = []
Pau Espin Pedrol2d16f6f2017-05-30 15:33:57 +020041 self.smsc = smsc.Smsc((ip_address.get('addr'), 2775))
Neels Hofmeyr3531a192017-03-28 14:30:28 +020042
43 def start(self):
44 self.log('Starting osmo-nitb')
Pau Espin Pedrold0912332017-06-14 13:27:08 +020045 self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name()))
Neels Hofmeyr3531a192017-03-28 14:30:28 +020046 self.configure()
Your Name3c6673a2017-04-08 18:52:39 +020047 inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-nitb')))
48 binary = inst.child('bin', 'osmo-nitb')
Neels Hofmeyr3531a192017-03-28 14:30:28 +020049 if not os.path.isfile(binary):
50 raise RuntimeError('Binary missing: %r' % binary)
Your Name3c6673a2017-04-08 18:52:39 +020051 lib = inst.child('lib')
52 if not os.path.isdir(lib):
53 raise RuntimeError('No lib/ in %r' % inst)
Pau Espin Pedrole39c6f12017-05-08 16:21:38 +020054
Pau Espin Pedrol13143bc2017-05-08 17:07:28 +020055 iface = util.ip_to_iface(self.addr())
Neels Hofmeyr95fd6732017-05-15 14:03:07 +020056 pcap_recorder.PcapRecorder(self.suite_run, self.run_dir.new_dir('pcap'), iface,
Pau Espin Pedrol8cd347b2017-05-12 16:24:21 +020057 'host %s and port not 22' % self.addr())
Pau Espin Pedrol13143bc2017-05-08 17:07:28 +020058
Pau Espin Pedrole39c6f12017-05-08 16:21:38 +020059 env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
60
Neels Hofmeyr3531a192017-03-28 14:30:28 +020061 self.dbg(run_dir=self.run_dir, binary=binary, env=env)
62 self.process = process.Process(self.name(), self.run_dir,
63 (binary, '-c',
64 os.path.abspath(self.config_file)),
65 env=env)
66 self.suite_run.remember_to_stop(self.process)
67 self.process.launch()
68
69 def configure(self):
70 self.config_file = self.run_dir.new_file('osmo-nitb.cfg')
71 self.dbg(config_file=self.config_file)
72
73 values = dict(nitb=config.get_defaults('nitb'))
74 config.overlay(values, self.suite_run.config())
Neels Hofmeyr4f59ba62017-05-22 20:04:05 +020075 config.overlay(values, dict(nitb=dict(ip_address=self.ip_address)))
Neels Hofmeyr3531a192017-03-28 14:30:28 +020076
77 bts_list = []
78 for bts in self.bts:
Neels Hofmeyrc4ba4a62017-05-18 19:31:44 +020079 bts_list.append(bts.conf_for_bsc())
Neels Hofmeyr3531a192017-03-28 14:30:28 +020080 config.overlay(values, dict(nitb=dict(net=dict(bts_list=bts_list))))
Pau Espin Pedrol2d16f6f2017-05-30 15:33:57 +020081 config.overlay(values, self.smsc.get_config())
Pau Espin Pedrolca126b12017-08-22 19:04:06 +020082
83 # runtime parameters:
84 if self.encryption is not None:
85 config.overlay(values, dict(nitb=dict(net=dict(encryption=self.encryption))))
86
Pau Espin Pedrol0e57aad2017-05-29 14:25:22 +020087 self.config = values
Neels Hofmeyr3531a192017-03-28 14:30:28 +020088
Neels Hofmeyr943c81d2017-05-22 20:16:03 +020089 self.dbg('NITB CONFIG:\n' + pprint.pformat(values))
Neels Hofmeyr3531a192017-03-28 14:30:28 +020090
91 with open(self.config_file, 'w') as f:
92 r = template.render('osmo-nitb.cfg', values)
93 self.dbg(r)
94 f.write(r)
95
96 def addr(self):
Neels Hofmeyr76d81032017-05-18 18:35:32 +020097 return self.ip_address.get('addr')
Neels Hofmeyr3531a192017-03-28 14:30:28 +020098
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +020099 def bts_add(self, bts):
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200100 self.bts.append(bts)
Neels Hofmeyrc4ba4a62017-05-18 19:31:44 +0200101 bts.set_bsc(self)
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200102
Pau Espin Pedrolca126b12017-08-22 19:04:06 +0200103 def set_encryption(self, val):
104 self.encryption = val
105
Pau Espin Pedrol0e57aad2017-05-29 14:25:22 +0200106 def mcc(self):
107 return self.config['nitb']['net']['mcc']
108
109 def mnc(self):
110 return self.config['nitb']['net']['mnc']
111
112 def mcc_mnc(self):
113 return (self.mcc(), self.mnc())
114
Pau Espin Pedrol713ce2c2017-08-24 16:57:17 +0200115 def subscriber_add(self, modem, msisdn=None, algo=None):
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200116 if msisdn is None:
117 msisdn = self.suite_run.resources_pool.next_msisdn(modem)
118 modem.set_msisdn(msisdn)
Pau Espin Pedrol713ce2c2017-08-24 16:57:17 +0200119
120 if not algo:
121 alg_str = modem.auth_algo()
122 if not alg_str or alg_str == 'none':
123 algo = None
124 elif alg_str == 'comp128v1':
125 algo = 'comp128v1'
126 elif alg_str == 'xor':
127 algo = 'xor'
128 if algo is not None and not modem.ki():
129 raise log.Error("Auth algo %r selected and no KI specified" % algo)
130
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200131 self.log('Add subscriber', msisdn=msisdn, imsi=modem.imsi())
Pau Espin Pedrol713ce2c2017-08-24 16:57:17 +0200132 OsmoNitbCtrl(self).subscriber_add(modem.imsi(), msisdn, modem.ki(), algo)
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200133
Pau Espin Pedrole1a26222017-08-23 15:52:14 +0200134 def subscriber_delete(self, modem):
135 self.log('Delete subscriber', imsi=modem.imsi())
136 OsmoNitbCtrl(self).subscriber_delete(modem.imsi())
137
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200138 def subscriber_attached(self, *modems):
Neels Hofmeyrb02c2112017-04-09 18:46:48 +0200139 return self.imsi_attached(*[m.imsi() for m in modems])
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200140
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200141 def imsi_attached(self, *imsis):
142 attached = self.imsi_list_attached()
Neels Hofmeyrb02c2112017-04-09 18:46:48 +0200143 self.dbg('attached:', attached)
144 return all([(imsi in attached) for imsi in imsis])
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200145
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200146 def imsi_list_attached(self):
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200147 return OsmoNitbCtrl(self).subscriber_list_active()
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200148
149 def running(self):
150 return not self.process.terminated()
151
152
153class OsmoNitbCtrl(log.Origin):
154 PORT = 4249
155 SUBSCR_MODIFY_VAR = 'subscriber-modify-v1'
156 SUBSCR_MODIFY_REPLY_RE = re.compile("SET_REPLY (\d+) %s OK" % SUBSCR_MODIFY_VAR)
Pau Espin Pedrole1a26222017-08-23 15:52:14 +0200157 SUBSCR_DELETE_VAR = 'subscriber-delete-v1'
158 SUBSCR_DELETE_REPLY_RE = re.compile("SET_REPLY (\d+) %s Removed" % SUBSCR_DELETE_VAR)
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200159 SUBSCR_LIST_ACTIVE_VAR = 'subscriber-list-active-v1'
160
161 def __init__(self, nitb):
162 self.nitb = nitb
Neels Hofmeyr1a7a3f02017-06-10 01:18:27 +0200163 super().__init__(log.C_BUS, 'CTRL(%s:%d)' % (self.nitb.addr(), OsmoNitbCtrl.PORT))
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200164
165 def ctrl(self):
166 return osmo_ctrl.OsmoCtrl(self.nitb.addr(), OsmoNitbCtrl.PORT)
167
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200168 def subscriber_add(self, imsi, msisdn, ki=None, algo=None):
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200169 created = False
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200170
171 if algo:
172 value = '%s,%s,%s,%s' % (imsi,msisdn,algo,ki)
173 else:
174 value = '%s,%s' % (imsi, msisdn)
175
Neels Hofmeyrb02c2112017-04-09 18:46:48 +0200176 with self.ctrl() as ctrl:
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200177 ctrl.do_set(OsmoNitbCtrl.SUBSCR_MODIFY_VAR, value)
178 data = ctrl.receive()
179 (answer, data) = ctrl.remove_ipa_ctrl_header(data)
180 answer_str = answer.decode('utf-8')
181 res = OsmoNitbCtrl.SUBSCR_MODIFY_REPLY_RE.match(answer_str)
182 if not res:
183 raise RuntimeError('Cannot create subscriber %r (answer=%r)' % (imsi, answer_str))
184 self.dbg('Created subscriber', imsi=imsi, msisdn=msisdn)
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200185
Pau Espin Pedrole1a26222017-08-23 15:52:14 +0200186 def subscriber_delete(self, imsi):
187 with self.ctrl() as ctrl:
188 ctrl.do_set(OsmoNitbCtrl.SUBSCR_DELETE_VAR, imsi)
189 data = ctrl.receive()
190 (answer, data) = ctrl.remove_ipa_ctrl_header(data)
191 answer_str = answer.decode('utf-8')
192 res = OsmoNitbCtrl.SUBSCR_DELETE_REPLY_RE.match(answer_str)
193 if not res:
194 raise RuntimeError('Cannot delete subscriber %r (answer=%r)' % (imsi, answer_str))
195 self.dbg('Deleted subscriber', imsi=imsi)
196
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200197 def subscriber_list_active(self):
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200198 aslist_str = ""
Neels Hofmeyrb02c2112017-04-09 18:46:48 +0200199 with self.ctrl() as ctrl:
200 ctrl.do_get(OsmoNitbCtrl.SUBSCR_LIST_ACTIVE_VAR)
Neels Hofmeyrb3daaea2017-04-09 14:18:34 +0200201 # This is legacy code from the old osmo-gsm-tester.
202 # looks like this doesn't work for long data.
Neels Hofmeyrb02c2112017-04-09 18:46:48 +0200203 data = ctrl.receive()
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200204 while (len(data) > 0):
Neels Hofmeyrb02c2112017-04-09 18:46:48 +0200205 (answer, data) = ctrl.remove_ipa_ctrl_header(data)
206 answer_str = answer.decode('utf-8')
207 answer_str = answer_str.replace('\n', ' ')
208 aslist_str = answer_str
Neels Hofmeyr3531a192017-03-28 14:30:28 +0200209 return aslist_str
210
211# vim: expandtab tabstop=4 shiftwidth=4