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