blob: 0a0036e90da920f26be3200fd9d9f115f588986f [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
24
25class srsEPC(log.Origin):
26
27 REMOTE_DIR = '/osmo-gsm-tester-srsepc'
28 BINFILE = 'srsepc'
29 CFGFILE = 'srsepc.conf'
30 DBFILE = 'srsepc_user_db.csv'
31 PCAPFILE = 'srsepc.pcap'
32 LOGFILE = 'srsepc.log'
33
34 def __init__(self, suite_run, run_node):
35 super().__init__(log.C_RUN, 'srsepc')
36 self._addr = run_node.run_addr()
37 self.set_name('srsepc_%s' % self._addr)
38 self.run_dir = None
39 self.config_file = None
40 self.db_file = None
41 self.log_file = None
42 self.pcap_file = None
43 self.process = None
44 self.rem_host = None
45 self.remote_config_file = None
46 self.remote_db_file = None
47 self.remote_log_file = None
48 self.remote_pcap_file = None
49 self.subscriber_list = []
50 self.suite_run = suite_run
51 self._run_node = run_node
52
53 def cleanup(self):
54 if self.process is None:
55 return
56 if self._run_node.is_local():
57 return
58 # copy back files (may not exist, for instance if there was an early error of process):
59 try:
60 self.rem_host.scpfrom('scp-back-log', self.remote_log_file, self.log_file)
61 except Exception as e:
62 self.log(repr(e))
63 try:
64 self.rem_host.scpfrom('scp-back-pcap', self.remote_pcap_file, self.pcap_file)
65 except Exception as e:
66 self.log(repr(e))
67
68 def start(self):
69 self.log('Starting srsepc')
70 self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name()))
71 self.configure()
72 if self._run_node.is_local():
73 self.start_locally()
74 else:
75 self.start_remotely()
76
77 def start_remotely(self):
78 self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('srslte')))
79 lib = self.inst.child('lib')
80 if not os.path.isdir(lib):
81 raise log.Error('No lib/ in', self.inst)
82 if not self.inst.isfile('bin', srsEPC.BINFILE):
83 raise log.Error('No %s binary in' % srsEPC.BINFILE, self.inst)
84
85 self.rem_host = remote.RemoteHost(self.run_dir, self._run_node.ssh_user(), self._run_node.ssh_addr())
86 remote_prefix_dir = util.Dir(srsEPC.REMOTE_DIR)
87 remote_inst = util.Dir(remote_prefix_dir.child(os.path.basename(str(self.inst))))
88 remote_run_dir = util.Dir(remote_prefix_dir.child(srsEPC.BINFILE))
89 self.remote_config_file = remote_run_dir.child(srsEPC.CFGFILE)
90 self.remote_db_file = remote_run_dir.child(srsEPC.DBFILE)
91 self.remote_log_file = remote_run_dir.child(srsEPC.LOGFILE)
92 self.remote_pcap_file = remote_run_dir.child(srsEPC.PCAPFILE)
93
94 self.rem_host.recreate_remote_dir(remote_inst)
95 self.rem_host.scp('scp-inst-to-remote', str(self.inst), remote_prefix_dir)
96 self.rem_host.create_remote_dir(remote_run_dir)
97 self.rem_host.scp('scp-cfg-to-remote', self.config_file, self.remote_config_file)
98 self.rem_host.scp('scp-db-to-remote', self.db_file, self.remote_db_file)
99
100 remote_lib = remote_inst.child('lib')
101 remote_binary = remote_inst.child('bin', srsEPC.BINFILE)
102 # setting capabilities will later disable use of LD_LIBRARY_PATH from ELF loader -> modify RPATH instead.
103 self.log('Setting RPATH for srsepc')
104 self.rem_host.change_elf_rpath(remote_binary, remote_lib)
105 # srsepc requires CAP_NET_ADMIN to create tunnel devices: ioctl(TUNSETIFF):
106 self.log('Applying CAP_NET_ADMIN capability to srsepc')
107 self.rem_host.setcap_net_admin(remote_binary)
108
109 args = (remote_binary, self.remote_config_file,
110 '--hss.db_file=' + self.remote_db_file,
111 '--log.filename=' + self.remote_log_file,
112 '--pcap.enable=true',
113 '--pcap.filename=' + self.remote_pcap_file)
114
115 self.process = self.rem_host.RemoteProcess(srsEPC.BINFILE, args)
116 #self.process = self.rem_host.RemoteProcessFixIgnoreSIGHUP(srsEPC.BINFILE, remote_run_dir, args)
117 self.suite_run.remember_to_stop(self.process)
118 self.process.launch()
119
120 def start_locally(self):
121 inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('srslte')))
122
123 binary = inst.child('bin', BINFILE)
124 if not os.path.isfile(binary):
125 raise log.Error('Binary missing:', binary)
126 lib = inst.child('lib')
127 if not os.path.isdir(lib):
128 raise log.Error('No lib/ in', inst)
129
130 env = {}
131 # setting capabilities will later disable use of LD_LIBRARY_PATH from ELF loader -> modify RPATH instead.
132 self.log('Setting RPATH for srsepc')
133 # srsepc binary needs patchelf <= 0.9 (0.10 and current master fail) to avoid failing during patch. OS#4389, patchelf-GH#192.
134 util.change_elf_rpath(binary, util.prepend_library_path(lib), self.run_dir.new_dir('patchelf'))
135 # srsepc requires CAP_NET_ADMIN to create tunnel devices: ioctl(TUNSETIFF):
136 self.log('Applying CAP_NET_ADMIN capability to srsepc')
137 util.setcap_net_admin(binary, self.run_dir.new_dir('setcap_net_admin'))
138
139 self.dbg(run_dir=self.run_dir, binary=binary, env=env)
140 args = (binary, os.path.abspath(self.config_file),
141 '--hss.db_file=' + self.db_file,
142 '--log.filename=' + self.log_file,
143 '--pcap.enable=true',
144 '--pcap.filename=' + self.pcap_file)
145
146 self.process = process.Process(self.name(), self.run_dir, args, env=env)
147 self.suite_run.remember_to_stop(self.process)
148 self.process.launch()
149
150 def configure(self):
151 self.config_file = self.run_dir.new_file(srsEPC.CFGFILE)
152 self.db_file = self.run_dir.new_file(srsEPC.DBFILE)
153 self.log_file = self.run_dir.new_file(srsEPC.LOGFILE)
154 self.pcap_file = self.run_dir.new_file(srsEPC.PCAPFILE)
155 self.dbg(config_file=self.config_file, db_file=self.db_file)
156
157 values = dict(epc=config.get_defaults('srsepc'))
158 config.overlay(values, dict(epc=dict(hss=dict(subscribers=self.subscriber_list))))
159 config.overlay(values, self.suite_run.config())
160 config.overlay(values, dict(epc={'run_addr': self.addr()}))
161
162 self.dbg('SRSEPC CONFIG:\n' + pprint.pformat(values))
163
164 with open(self.config_file, 'w') as f:
165 r = template.render(srsEPC.CFGFILE, values)
166 self.dbg(r)
167 f.write(r)
168 with open(self.db_file, 'w') as f:
169 r = template.render(srsEPC.DBFILE, values)
170 self.dbg(r)
171 f.write(r)
172
173 def subscriber_add(self, modem, msisdn=None, algo_str=None):
174 if msisdn is None:
175 msisdn = self.suite_run.resources_pool.next_msisdn(modem)
176 modem.set_msisdn(msisdn)
177
178 if algo_str is None:
179 algo_str = modem.auth_algo() or util.OSMO_AUTH_ALGO_NONE
180
181 if algo_str != util.OSMO_AUTH_ALGO_NONE and not modem.ki():
182 raise log.Error("Auth algo %r selected but no KI specified" % algo_str)
183
184 subscriber_id = len(self.subscriber_list) # list index
185 self.subscriber_list.append({'id': subscriber_id, 'imsi': modem.imsi(), 'msisdn': msisdn, 'auth_algo': algo_str, 'ki': modem.ki(), 'opc': None})
186
187 self.log('Add subscriber', msisdn=msisdn, imsi=modem.imsi(), subscriber_id=subscriber_id,
188 algo_str=algo_str)
189 return subscriber_id
190
191 def enb_is_connected(self, enb):
192 # FIXME: srspec's stdout: "S1 Setup Request - eNB Id 0x66c0", but srsenb.conf has "enb_id = 0x19B"
193 return 'S1 Setup Request - eNB Id' in (self.process.get_stdout() or '')
194
195 def running(self):
196 return not self.process.terminated()
197
198 def addr(self):
199 return self._addr
200
201 def tun_addr(self):
202 return '172.16.0.1'
203
204 def run_node(self):
205 return self._run_node
206
207# vim: expandtab tabstop=4 shiftwidth=4