blob: 7c324862ede0774a846d4e69af1f79ba104288d0 [file] [log] [blame]
Pau Espin Pedrolc8b0f932020-02-11 17:45:26 +01001# 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
Pau Espin Pedrolda2e31f2020-03-31 13:45:01 +020024from . import epc
Pau Espin Pedrolc8b0f932020-02-11 17:45:26 +010025
Pau Espin Pedrolda2e31f2020-03-31 13:45:01 +020026class srsEPC(epc.EPC):
Pau Espin Pedrolc8b0f932020-02-11 17:45:26 +010027
28 REMOTE_DIR = '/osmo-gsm-tester-srsepc'
29 BINFILE = 'srsepc'
30 CFGFILE = 'srsepc.conf'
31 DBFILE = 'srsepc_user_db.csv'
32 PCAPFILE = 'srsepc.pcap'
33 LOGFILE = 'srsepc.log'
34
35 def __init__(self, suite_run, run_node):
Pau Espin Pedrolda2e31f2020-03-31 13:45:01 +020036 super().__init__(suite_run, run_node, 'srsepc')
Pau Espin Pedrolc8b0f932020-02-11 17:45:26 +010037 self.run_dir = None
38 self.config_file = None
39 self.db_file = None
40 self.log_file = None
41 self.pcap_file = None
42 self.process = None
43 self.rem_host = None
44 self.remote_config_file = None
45 self.remote_db_file = None
46 self.remote_log_file = None
47 self.remote_pcap_file = None
Pau Espin Pedrol1e81b5a2020-03-16 12:42:17 +010048 self.enable_pcap = False
Pau Espin Pedrolc8b0f932020-02-11 17:45:26 +010049 self.subscriber_list = []
Pau Espin Pedrolc8b0f932020-02-11 17:45:26 +010050
51 def cleanup(self):
52 if self.process is None:
53 return
54 if self._run_node.is_local():
55 return
56 # copy back files (may not exist, for instance if there was an early error of process):
57 try:
58 self.rem_host.scpfrom('scp-back-log', self.remote_log_file, self.log_file)
59 except Exception as e:
60 self.log(repr(e))
Pau Espin Pedrol1e81b5a2020-03-16 12:42:17 +010061 if self.enable_pcap:
62 try:
63 self.rem_host.scpfrom('scp-back-pcap', self.remote_pcap_file, self.pcap_file)
64 except Exception as e:
65 self.log(repr(e))
Pau Espin Pedrolc8b0f932020-02-11 17:45:26 +010066
67 def start(self):
68 self.log('Starting srsepc')
69 self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name()))
70 self.configure()
71 if self._run_node.is_local():
72 self.start_locally()
73 else:
74 self.start_remotely()
75
76 def start_remotely(self):
77 self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('srslte')))
78 lib = self.inst.child('lib')
79 if not os.path.isdir(lib):
80 raise log.Error('No lib/ in', self.inst)
81 if not self.inst.isfile('bin', srsEPC.BINFILE):
82 raise log.Error('No %s binary in' % srsEPC.BINFILE, self.inst)
83
84 self.rem_host = remote.RemoteHost(self.run_dir, self._run_node.ssh_user(), self._run_node.ssh_addr())
85 remote_prefix_dir = util.Dir(srsEPC.REMOTE_DIR)
86 remote_inst = util.Dir(remote_prefix_dir.child(os.path.basename(str(self.inst))))
87 remote_run_dir = util.Dir(remote_prefix_dir.child(srsEPC.BINFILE))
88 self.remote_config_file = remote_run_dir.child(srsEPC.CFGFILE)
89 self.remote_db_file = remote_run_dir.child(srsEPC.DBFILE)
90 self.remote_log_file = remote_run_dir.child(srsEPC.LOGFILE)
91 self.remote_pcap_file = remote_run_dir.child(srsEPC.PCAPFILE)
92
93 self.rem_host.recreate_remote_dir(remote_inst)
94 self.rem_host.scp('scp-inst-to-remote', str(self.inst), remote_prefix_dir)
Pau Espin Pedrol1e81b5a2020-03-16 12:42:17 +010095 self.rem_host.recreate_remote_dir(remote_run_dir)
Pau Espin Pedrolc8b0f932020-02-11 17:45:26 +010096 self.rem_host.scp('scp-cfg-to-remote', self.config_file, self.remote_config_file)
97 self.rem_host.scp('scp-db-to-remote', self.db_file, self.remote_db_file)
98
99 remote_lib = remote_inst.child('lib')
100 remote_binary = remote_inst.child('bin', srsEPC.BINFILE)
101 # setting capabilities will later disable use of LD_LIBRARY_PATH from ELF loader -> modify RPATH instead.
102 self.log('Setting RPATH for srsepc')
103 self.rem_host.change_elf_rpath(remote_binary, remote_lib)
104 # srsepc requires CAP_NET_ADMIN to create tunnel devices: ioctl(TUNSETIFF):
105 self.log('Applying CAP_NET_ADMIN capability to srsepc')
106 self.rem_host.setcap_net_admin(remote_binary)
107
108 args = (remote_binary, self.remote_config_file,
109 '--hss.db_file=' + self.remote_db_file,
110 '--log.filename=' + self.remote_log_file,
Pau Espin Pedrolc8b0f932020-02-11 17:45:26 +0100111 '--pcap.filename=' + self.remote_pcap_file)
112
113 self.process = self.rem_host.RemoteProcess(srsEPC.BINFILE, args)
114 #self.process = self.rem_host.RemoteProcessFixIgnoreSIGHUP(srsEPC.BINFILE, remote_run_dir, args)
115 self.suite_run.remember_to_stop(self.process)
116 self.process.launch()
117
118 def start_locally(self):
119 inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('srslte')))
120
121 binary = inst.child('bin', BINFILE)
122 if not os.path.isfile(binary):
123 raise log.Error('Binary missing:', binary)
124 lib = inst.child('lib')
125 if not os.path.isdir(lib):
126 raise log.Error('No lib/ in', inst)
127
128 env = {}
129 # setting capabilities will later disable use of LD_LIBRARY_PATH from ELF loader -> modify RPATH instead.
130 self.log('Setting RPATH for srsepc')
131 # srsepc binary needs patchelf <= 0.9 (0.10 and current master fail) to avoid failing during patch. OS#4389, patchelf-GH#192.
132 util.change_elf_rpath(binary, util.prepend_library_path(lib), self.run_dir.new_dir('patchelf'))
133 # srsepc requires CAP_NET_ADMIN to create tunnel devices: ioctl(TUNSETIFF):
134 self.log('Applying CAP_NET_ADMIN capability to srsepc')
135 util.setcap_net_admin(binary, self.run_dir.new_dir('setcap_net_admin'))
136
137 self.dbg(run_dir=self.run_dir, binary=binary, env=env)
138 args = (binary, os.path.abspath(self.config_file),
139 '--hss.db_file=' + self.db_file,
140 '--log.filename=' + self.log_file,
Pau Espin Pedrolc8b0f932020-02-11 17:45:26 +0100141 '--pcap.filename=' + self.pcap_file)
142
143 self.process = process.Process(self.name(), self.run_dir, args, env=env)
144 self.suite_run.remember_to_stop(self.process)
145 self.process.launch()
146
147 def configure(self):
Pau Espin Pedrol1e81b5a2020-03-16 12:42:17 +0100148 self.config_file = self.run_dir.child(srsEPC.CFGFILE)
149 self.db_file = self.run_dir.child(srsEPC.DBFILE)
150 self.log_file = self.run_dir.child(srsEPC.LOGFILE)
151 self.pcap_file = self.run_dir.child(srsEPC.PCAPFILE)
Pau Espin Pedrolc8b0f932020-02-11 17:45:26 +0100152 self.dbg(config_file=self.config_file, db_file=self.db_file)
153
Pau Espin Pedrolc04528c2020-04-01 13:55:51 +0200154 values = super().configure(['srsepc'])
Pau Espin Pedrolc8b0f932020-02-11 17:45:26 +0100155
Pau Espin Pedrol1e81b5a2020-03-16 12:42:17 +0100156 # Convert parsed boolean string to Python boolean:
157 self.enable_pcap = util.str2bool(values['epc'].get('enable_pcap', 'false'))
158 config.overlay(values, dict(epc={'enable_pcap': self.enable_pcap}))
159
Pau Espin Pedrolb6937712020-02-27 18:02:20 +0100160 # Set qci for each subscriber:
Pau Espin Pedrol04ad3b52020-04-06 12:25:22 +0200161 qci = values['epc'].get('qci', None)
162 assert qci is not None
Pau Espin Pedrolb6937712020-02-27 18:02:20 +0100163 for i in range(len(self.subscriber_list)):
Pau Espin Pedrol04ad3b52020-04-06 12:25:22 +0200164 self.subscriber_list[i]['qci'] = qci
Pau Espin Pedrolb6937712020-02-27 18:02:20 +0100165 config.overlay(values, dict(epc=dict(hss=dict(subscribers=self.subscriber_list))))
166
Pau Espin Pedrolc8b0f932020-02-11 17:45:26 +0100167 self.dbg('SRSEPC CONFIG:\n' + pprint.pformat(values))
168
169 with open(self.config_file, 'w') as f:
170 r = template.render(srsEPC.CFGFILE, values)
171 self.dbg(r)
172 f.write(r)
173 with open(self.db_file, 'w') as f:
174 r = template.render(srsEPC.DBFILE, values)
175 self.dbg(r)
176 f.write(r)
177
178 def subscriber_add(self, modem, msisdn=None, algo_str=None):
179 if msisdn is None:
180 msisdn = self.suite_run.resources_pool.next_msisdn(modem)
181 modem.set_msisdn(msisdn)
182
183 if algo_str is None:
184 algo_str = modem.auth_algo() or util.OSMO_AUTH_ALGO_NONE
185
186 if algo_str != util.OSMO_AUTH_ALGO_NONE and not modem.ki():
187 raise log.Error("Auth algo %r selected but no KI specified" % algo_str)
188
189 subscriber_id = len(self.subscriber_list) # list index
Andre Puschmann22ec00a2020-03-24 09:58:06 +0100190 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()})
Pau Espin Pedrolc8b0f932020-02-11 17:45:26 +0100191
192 self.log('Add subscriber', msisdn=msisdn, imsi=modem.imsi(), subscriber_id=subscriber_id,
193 algo_str=algo_str)
194 return subscriber_id
195
196 def enb_is_connected(self, enb):
Pau Espin Pedrolb9aeb152020-03-09 15:06:45 +0100197 # TODO: match against srsENB config: "S1 Setup Request - eNB Name: srsenb01, eNB id: 0x19"
198 return 'S1 Setup Request - eNB' in (self.process.get_stdout() or '')
Pau Espin Pedrolc8b0f932020-02-11 17:45:26 +0100199
200 def running(self):
201 return not self.process.terminated()
202
Pau Espin Pedrolc8b0f932020-02-11 17:45:26 +0100203 def tun_addr(self):
204 return '172.16.0.1'
205
Pau Espin Pedrolc8b0f932020-02-11 17:45:26 +0100206# vim: expandtab tabstop=4 shiftwidth=4