blob: 947eb7bd7c2eb9b41e0ab3ba345fc5bd2a56a968 [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
Pau Espin Pedrolb6937712020-02-27 18:02:20 +010025def rlc_drb_mode2qci(rlc_drb_mode):
26 if rlc_drb_mode.upper() == "UM":
27 return 7;
28 elif rlc_drb_mode.upper() == "AM":
29 return 9;
30 raise log.Error('Unexpected rlc_drb_mode', rlc_drb_mode=rlc_drb_mode)
31
Pau Espin Pedrolc8b0f932020-02-11 17:45:26 +010032class srsEPC(log.Origin):
33
34 REMOTE_DIR = '/osmo-gsm-tester-srsepc'
35 BINFILE = 'srsepc'
36 CFGFILE = 'srsepc.conf'
37 DBFILE = 'srsepc_user_db.csv'
38 PCAPFILE = 'srsepc.pcap'
39 LOGFILE = 'srsepc.log'
40
41 def __init__(self, suite_run, run_node):
42 super().__init__(log.C_RUN, 'srsepc')
43 self._addr = run_node.run_addr()
44 self.set_name('srsepc_%s' % self._addr)
45 self.run_dir = None
46 self.config_file = None
47 self.db_file = None
48 self.log_file = None
49 self.pcap_file = None
50 self.process = None
51 self.rem_host = None
52 self.remote_config_file = None
53 self.remote_db_file = None
54 self.remote_log_file = None
55 self.remote_pcap_file = None
Pau Espin Pedrol1e81b5a2020-03-16 12:42:17 +010056 self.enable_pcap = False
Pau Espin Pedrolc8b0f932020-02-11 17:45:26 +010057 self.subscriber_list = []
58 self.suite_run = suite_run
59 self._run_node = run_node
60
61 def cleanup(self):
62 if self.process is None:
63 return
64 if self._run_node.is_local():
65 return
66 # copy back files (may not exist, for instance if there was an early error of process):
67 try:
68 self.rem_host.scpfrom('scp-back-log', self.remote_log_file, self.log_file)
69 except Exception as e:
70 self.log(repr(e))
Pau Espin Pedrol1e81b5a2020-03-16 12:42:17 +010071 if self.enable_pcap:
72 try:
73 self.rem_host.scpfrom('scp-back-pcap', self.remote_pcap_file, self.pcap_file)
74 except Exception as e:
75 self.log(repr(e))
Pau Espin Pedrolc8b0f932020-02-11 17:45:26 +010076
77 def start(self):
78 self.log('Starting srsepc')
79 self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name()))
80 self.configure()
81 if self._run_node.is_local():
82 self.start_locally()
83 else:
84 self.start_remotely()
85
86 def start_remotely(self):
87 self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('srslte')))
88 lib = self.inst.child('lib')
89 if not os.path.isdir(lib):
90 raise log.Error('No lib/ in', self.inst)
91 if not self.inst.isfile('bin', srsEPC.BINFILE):
92 raise log.Error('No %s binary in' % srsEPC.BINFILE, self.inst)
93
94 self.rem_host = remote.RemoteHost(self.run_dir, self._run_node.ssh_user(), self._run_node.ssh_addr())
95 remote_prefix_dir = util.Dir(srsEPC.REMOTE_DIR)
96 remote_inst = util.Dir(remote_prefix_dir.child(os.path.basename(str(self.inst))))
97 remote_run_dir = util.Dir(remote_prefix_dir.child(srsEPC.BINFILE))
98 self.remote_config_file = remote_run_dir.child(srsEPC.CFGFILE)
99 self.remote_db_file = remote_run_dir.child(srsEPC.DBFILE)
100 self.remote_log_file = remote_run_dir.child(srsEPC.LOGFILE)
101 self.remote_pcap_file = remote_run_dir.child(srsEPC.PCAPFILE)
102
103 self.rem_host.recreate_remote_dir(remote_inst)
104 self.rem_host.scp('scp-inst-to-remote', str(self.inst), remote_prefix_dir)
Pau Espin Pedrol1e81b5a2020-03-16 12:42:17 +0100105 self.rem_host.recreate_remote_dir(remote_run_dir)
Pau Espin Pedrolc8b0f932020-02-11 17:45:26 +0100106 self.rem_host.scp('scp-cfg-to-remote', self.config_file, self.remote_config_file)
107 self.rem_host.scp('scp-db-to-remote', self.db_file, self.remote_db_file)
108
109 remote_lib = remote_inst.child('lib')
110 remote_binary = remote_inst.child('bin', srsEPC.BINFILE)
111 # setting capabilities will later disable use of LD_LIBRARY_PATH from ELF loader -> modify RPATH instead.
112 self.log('Setting RPATH for srsepc')
113 self.rem_host.change_elf_rpath(remote_binary, remote_lib)
114 # srsepc requires CAP_NET_ADMIN to create tunnel devices: ioctl(TUNSETIFF):
115 self.log('Applying CAP_NET_ADMIN capability to srsepc')
116 self.rem_host.setcap_net_admin(remote_binary)
117
118 args = (remote_binary, self.remote_config_file,
119 '--hss.db_file=' + self.remote_db_file,
120 '--log.filename=' + self.remote_log_file,
Pau Espin Pedrolc8b0f932020-02-11 17:45:26 +0100121 '--pcap.filename=' + self.remote_pcap_file)
122
123 self.process = self.rem_host.RemoteProcess(srsEPC.BINFILE, args)
124 #self.process = self.rem_host.RemoteProcessFixIgnoreSIGHUP(srsEPC.BINFILE, remote_run_dir, args)
125 self.suite_run.remember_to_stop(self.process)
126 self.process.launch()
127
128 def start_locally(self):
129 inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('srslte')))
130
131 binary = inst.child('bin', BINFILE)
132 if not os.path.isfile(binary):
133 raise log.Error('Binary missing:', binary)
134 lib = inst.child('lib')
135 if not os.path.isdir(lib):
136 raise log.Error('No lib/ in', inst)
137
138 env = {}
139 # setting capabilities will later disable use of LD_LIBRARY_PATH from ELF loader -> modify RPATH instead.
140 self.log('Setting RPATH for srsepc')
141 # srsepc binary needs patchelf <= 0.9 (0.10 and current master fail) to avoid failing during patch. OS#4389, patchelf-GH#192.
142 util.change_elf_rpath(binary, util.prepend_library_path(lib), self.run_dir.new_dir('patchelf'))
143 # srsepc requires CAP_NET_ADMIN to create tunnel devices: ioctl(TUNSETIFF):
144 self.log('Applying CAP_NET_ADMIN capability to srsepc')
145 util.setcap_net_admin(binary, self.run_dir.new_dir('setcap_net_admin'))
146
147 self.dbg(run_dir=self.run_dir, binary=binary, env=env)
148 args = (binary, os.path.abspath(self.config_file),
149 '--hss.db_file=' + self.db_file,
150 '--log.filename=' + self.log_file,
Pau Espin Pedrolc8b0f932020-02-11 17:45:26 +0100151 '--pcap.filename=' + self.pcap_file)
152
153 self.process = process.Process(self.name(), self.run_dir, args, env=env)
154 self.suite_run.remember_to_stop(self.process)
155 self.process.launch()
156
157 def configure(self):
Pau Espin Pedrol1e81b5a2020-03-16 12:42:17 +0100158 self.config_file = self.run_dir.child(srsEPC.CFGFILE)
159 self.db_file = self.run_dir.child(srsEPC.DBFILE)
160 self.log_file = self.run_dir.child(srsEPC.LOGFILE)
161 self.pcap_file = self.run_dir.child(srsEPC.PCAPFILE)
Pau Espin Pedrolc8b0f932020-02-11 17:45:26 +0100162 self.dbg(config_file=self.config_file, db_file=self.db_file)
163
164 values = dict(epc=config.get_defaults('srsepc'))
Pau Espin Pedrol1e81b5a2020-03-16 12:42:17 +0100165 config.overlay(values, dict(epc=self.suite_run.config().get('epc', {})))
Pau Espin Pedrolc8b0f932020-02-11 17:45:26 +0100166 config.overlay(values, dict(epc={'run_addr': self.addr()}))
167
Pau Espin Pedrol1e81b5a2020-03-16 12:42:17 +0100168 # Convert parsed boolean string to Python boolean:
169 self.enable_pcap = util.str2bool(values['epc'].get('enable_pcap', 'false'))
170 config.overlay(values, dict(epc={'enable_pcap': self.enable_pcap}))
171
Pau Espin Pedrolb6937712020-02-27 18:02:20 +0100172 # Set qci for each subscriber:
173 rlc_drb_mode = values['epc'].get('rlc_drb_mode', None)
174 assert rlc_drb_mode is not None
175 for i in range(len(self.subscriber_list)):
176 self.subscriber_list[i]['qci'] = rlc_drb_mode2qci(rlc_drb_mode)
177 config.overlay(values, dict(epc=dict(hss=dict(subscribers=self.subscriber_list))))
178
Pau Espin Pedrolc8b0f932020-02-11 17:45:26 +0100179 self.dbg('SRSEPC CONFIG:\n' + pprint.pformat(values))
180
181 with open(self.config_file, 'w') as f:
182 r = template.render(srsEPC.CFGFILE, values)
183 self.dbg(r)
184 f.write(r)
185 with open(self.db_file, 'w') as f:
186 r = template.render(srsEPC.DBFILE, values)
187 self.dbg(r)
188 f.write(r)
189
190 def subscriber_add(self, modem, msisdn=None, algo_str=None):
191 if msisdn is None:
192 msisdn = self.suite_run.resources_pool.next_msisdn(modem)
193 modem.set_msisdn(msisdn)
194
195 if algo_str is None:
196 algo_str = modem.auth_algo() or util.OSMO_AUTH_ALGO_NONE
197
198 if algo_str != util.OSMO_AUTH_ALGO_NONE and not modem.ki():
199 raise log.Error("Auth algo %r selected but no KI specified" % algo_str)
200
201 subscriber_id = len(self.subscriber_list) # list index
Andre Puschmann22ec00a2020-03-24 09:58:06 +0100202 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 +0100203
204 self.log('Add subscriber', msisdn=msisdn, imsi=modem.imsi(), subscriber_id=subscriber_id,
205 algo_str=algo_str)
206 return subscriber_id
207
208 def enb_is_connected(self, enb):
Pau Espin Pedrolb9aeb152020-03-09 15:06:45 +0100209 # TODO: match against srsENB config: "S1 Setup Request - eNB Name: srsenb01, eNB id: 0x19"
210 return 'S1 Setup Request - eNB' in (self.process.get_stdout() or '')
Pau Espin Pedrolc8b0f932020-02-11 17:45:26 +0100211
212 def running(self):
213 return not self.process.terminated()
214
215 def addr(self):
216 return self._addr
217
218 def tun_addr(self):
219 return '172.16.0.1'
220
221 def run_node(self):
222 return self._run_node
223
224# vim: expandtab tabstop=4 shiftwidth=4