blob: eb9be94b3e80960749fb8957977e6c4f924ba1b7 [file] [log] [blame]
Pau Espin Pedrolda2e31f2020-03-31 13:45:01 +02001# osmo_gsm_tester: specifics for running an SRS EPC process
2#
3# Copyright (C) 2020 by sysmocom - s.f.m.c. GmbH
4#
5# Author: Pau Espin Pedrol <pespin@sysmocom.de>
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as
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
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20import os
21import pprint
22
23from . import log, util, config, template, process, remote
24from . import epc
25
26#def rlc_drb_mode2qci(rlc_drb_mode):
27# if rlc_drb_mode.upper() == "UM":
28# return 7;
29# elif rlc_drb_mode.upper() == "AM":
30# return 9;
31# raise log.Error('Unexpected rlc_drb_mode', rlc_drb_mode=rlc_drb_mode)
32
33class AmarisoftEPC(epc.EPC):
34
35 REMOTE_DIR = '/osmo-gsm-tester-amarisoftepc'
36 BINFILE = 'ltemme'
37 CFGFILE = 'amarisoft_ltemme.cfg'
38 LOGFILE = 'ltemme.log'
39 IFUPFILE = 'mme-ifup'
40
41 def __init__(self, suite_run, run_node):
42 super().__init__(suite_run, run_node, 'amarisoftepc')
43 self.run_dir = None
44 self.config_file = None
45 self.log_file = None
46 self.ifup_file = None
47 self.process = None
48 self.rem_host = None
49 self.remote_inst = None
50 self.remote_config_file = None
51 self.remote_log_file = None
52 self.remote_ifup_file =None
53 self._bin_prefix = None
54 self.inst = None
55 self.subscriber_list = []
56
57 def bin_prefix(self):
58 if self._bin_prefix is None:
59 self._bin_prefix = os.getenv('AMARISOFT_PATH_EPC', AmarisoftEPC.REMOTE_DIR)
60 return self._bin_prefix
61
62 def cleanup(self):
63 if self.process is None:
64 return
65 if self._run_node.is_local():
66 return
67 # copy back files (may not exist, for instance if there was an early error of process):
68 try:
69 self.rem_host.scpfrom('scp-back-log', self.remote_log_file, self.log_file)
70 except Exception as e:
71 self.log(repr(e))
72
73 def start(self):
74 self.log('Starting amarisoftepc')
75 self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name()))
76 self.configure()
77 if self._run_node.is_local():
78 self.start_locally()
79 else:
80 self.start_remotely()
81
82 def start_remotely(self):
83 remote_binary = self.remote_inst.child('', AmarisoftEPC.BINFILE)
84 # setting capabilities will later disable use of LD_LIBRARY_PATH from ELF loader -> modify RPATH instead.
85 self.log('Setting RPATH for amarisoftepc')
86 self.rem_host.change_elf_rpath(remote_binary, str(self.remote_inst))
87 # amarisoftepc requires CAP_NET_ADMIN to create tunnel devices: ioctl(TUNSETIFF):
88 self.log('Applying CAP_NET_ADMIN capability to amarisoftepc')
89 self.rem_host.setcap_net_admin(remote_binary)
90
91 args = (remote_binary, self.remote_config_file)
92
93 self.process = self.rem_host.RemoteProcess(AmarisoftEPC.BINFILE, args)
94 #self.process = self.rem_host.RemoteProcessFixIgnoreSIGHUP(AmarisoftEPC.BINFILE, remote_run_dir, args)
95 self.suite_run.remember_to_stop(self.process)
96 self.process.launch()
97
98 def start_locally(self):
99 binary = self.inst.child('', BINFILE)
100
101 env = {}
102 # setting capabilities will later disable use of LD_LIBRARY_PATH from ELF loader -> modify RPATH instead.
103 self.log('Setting RPATH for amarisoftepc')
104 util.change_elf_rpath(binary, util.prepend_library_path(str(self.inst)), self.run_dir.new_dir('patchelf'))
105 # amarisoftepc requires CAP_NET_ADMIN to create tunnel devices: ioctl(TUNSETIFF):
106 self.log('Applying CAP_NET_ADMIN capability to amarisoftepc')
107 util.setcap_net_admin(binary, self.run_dir.new_dir('setcap_net_admin'))
108
109 self.dbg(run_dir=self.run_dir, binary=binary, env=env)
110 args = (binary, os.path.abspath(self.config_file))
111
112 self.process = process.Process(self.name(), self.run_dir, args, env=env)
113 self.suite_run.remember_to_stop(self.process)
114 self.process.launch()
115
116 def configure(self):
117 self.inst = util.Dir(os.path.abspath(self.bin_prefix()))
118 if not self.inst.isfile('', AmarisoftEPC.BINFILE):
119 raise log.Error('No %s binary in' % AmarisoftEPC.BINFILE, self.inst)
120
121 self.config_file = self.run_dir.child(AmarisoftEPC.CFGFILE)
122 self.log_file = self.run_dir.child(AmarisoftEPC.LOGFILE)
123 self.ifup_file = self.run_dir.new_file(AmarisoftEPC.IFUPFILE)
124 os.chmod(self.ifup_file, 0o744) # add execution permission
125 self.dbg(config_file=self.config_file)
126 with open(self.ifup_file, 'w') as f:
127 r = '''#!/bin/sh
128 set -x -e
129 # script + sudoers file available in osmo-gsm-tester.git/utils/{bin,sudoers.d}
130 sudo /usr/local/bin/osmo-gsm-tester_amarisoft_ltemme_ifup.sh "$@"
131 '''
132 f.write(r)
133
134 if not self._run_node.is_local():
135 self.rem_host = remote.RemoteHost(self.run_dir, self._run_node.ssh_user(), self._run_node.ssh_addr())
136 remote_prefix_dir = util.Dir(AmarisoftEPC.REMOTE_DIR)
137 self.remote_inst = util.Dir(remote_prefix_dir.child(os.path.basename(str(self.inst))))
138 remote_run_dir = util.Dir(remote_prefix_dir.child(AmarisoftEPC.BINFILE))
139
140 self.remote_config_file = remote_run_dir.child(AmarisoftEPC.CFGFILE)
141 self.remote_log_file = remote_run_dir.child(AmarisoftEPC.LOGFILE)
142 self.remote_ifup_file = remote_run_dir.child(AmarisoftEPC.IFUPFILE)
143
Pau Espin Pedrolc04528c2020-04-01 13:55:51 +0200144 values = super().configure(['amarisoft', 'amarisoftepc'])
Pau Espin Pedrolda2e31f2020-03-31 13:45:01 +0200145
146 logfile = self.log_file if self._run_node.is_local() else self.remote_log_file
147 ifupfile = self.ifup_file if self._run_node.is_local() else self.remote_ifup_file
148 config.overlay(values, dict(epc=dict(log_filename=logfile,
149 ifup_filename=ifupfile)))
150
151 # Set qci for each subscriber:
152 #rlc_drb_mode = values['epc'].get('rlc_drb_mode', None)
153 #assert rlc_drb_mode is not None
154 #for i in range(len(self.subscriber_list)):
155 # self.subscriber_list[i]['qci'] = rlc_drb_mode2qci(rlc_drb_mode)
156 config.overlay(values, dict(epc=dict(hss=dict(subscribers=self.subscriber_list))))
157
158 self.dbg('SRSEPC CONFIG:\n' + pprint.pformat(values))
159
160 with open(self.config_file, 'w') as f:
161 r = template.render(AmarisoftEPC.CFGFILE, values)
162 self.dbg(r)
163 f.write(r)
164
165 if not self._run_node.is_local():
166 self.rem_host.recreate_remote_dir(self.remote_inst)
167 self.rem_host.scp('scp-inst-to-remote', str(self.inst), remote_prefix_dir)
168 self.rem_host.recreate_remote_dir(remote_run_dir)
169 self.rem_host.scp('scp-cfg-to-remote', self.config_file, self.remote_config_file)
170 self.rem_host.scp('scp-ifup-to-remote', self.ifup_file, self.remote_ifup_file)
171
172 def subscriber_add(self, modem, msisdn=None, algo_str=None):
173 if msisdn is None:
174 msisdn = self.suite_run.resources_pool.next_msisdn(modem)
175 modem.set_msisdn(msisdn)
176
177 if algo_str is None:
178 algo_str = modem.auth_algo() or util.OSMO_AUTH_ALGO_NONE
179
180 if algo_str != util.OSMO_AUTH_ALGO_NONE and not modem.ki():
181 raise log.Error("Auth algo %r selected but no KI specified" % algo_str)
182
183 subscriber_id = len(self.subscriber_list) # list index
184 self.subscriber_list.append({'id': subscriber_id, 'imsi': modem.imsi(), 'msisdn': msisdn, 'auth_algo': algo_str, 'ki': modem.ki(), 'opc': None, 'apn_ipaddr': modem.apn_ipaddr()})
185
186 self.log('Add subscriber', msisdn=msisdn, imsi=modem.imsi(), subscriber_id=subscriber_id,
187 algo_str=algo_str)
188 return subscriber_id
189
190 def enb_is_connected(self, enb):
191 # TODO: improve this a bit, like matching IP addr of enb. CTRL iface?
192 # The string is only available in log file, not in stdout:
193 #return 'S1 setup response' in (self.process.get_stdout() or '')
194 return True
195
196 def running(self):
197 return not self.process.terminated()
198
199 def tun_addr(self):
200 # TODO: set proper addr
201 return '192.168.4.1'
202
203# vim: expandtab tabstop=4 shiftwidth=4