blob: 31e814e9ba15a31a035fa1e738904d6a8206aafb [file] [log] [blame]
Pau Espin Pedrol65beb8f2020-03-31 12:03:19 +02001# osmo_gsm_tester: specifics for running an SRS UE 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 .run_node import RunNode
25from .event_loop import MainLoop
26from .ms import MS
27
28def rf_type_valid(rf_type_str):
29 return rf_type_str in ('uhd', 'zmq')
30
31#reference: srsLTE.git srslte_symbol_sz()
32def num_prb2symbol_sz(num_prb):
33 if num_prb <= 6:
34 return 128
35 if num_prb <= 15:
36 return 256
37 if num_prb <= 25:
38 return 384
39 if num_prb <= 50:
40 return 768
41 if num_prb <= 75:
42 return 1024
43 if num_prb <= 110:
44 return 1536
45 raise log.Error('invalid num_prb %r', num_prb)
46
47def num_prb2base_srate(num_prb):
48 return num_prb2symbol_sz(num_prb) * 15 * 1000
49
50class AmarisoftUE(MS):
51
52 REMOTE_DIR = '/osmo-gsm-tester-amarisoftue'
53 BINFILE = 'lteue'
54 CFGFILE = 'amarisoft_lteue.cfg'
55 CFGFILE_RF = 'amarisoft_rf_driver.cfg'
56 LOGFILE = 'lteue.log'
57
58 def __init__(self, suite_run, conf):
59 self._addr = conf.get('addr', None)
60 if self._addr is None:
61 raise log.Error('addr not set')
62 super().__init__('amarisoftue_%s' % self._addr, conf)
63 self.enb = None
64 self.run_dir = None
65 self.inst = None
66 self._bin_prefix = None
67 self.config_file = None
68 self.config_rf_file = None
69 self.log_file = None
70 self.process = None
71 self.rem_host = None
72 self.remote_inst = None
73 self.remote_config_file = None
74 self.remote_config_rf_file = None
75 self.remote_log_file = None
76 self.suite_run = suite_run
77 self.remote_user = conf.get('remote_user', None)
78 if not rf_type_valid(conf.get('rf_dev_type', None)):
79 raise log.Error('Invalid rf_dev_type=%s' % conf.get('rf_dev_type', None))
80
81 def bin_prefix(self):
82 if self._bin_prefix is None:
83 self._bin_prefix = os.getenv('AMARISOFT_PATH_UE', None)
84 if self._bin_prefix == None:
85 self._bin_prefix = self.suite_run.trial.get_inst('amarisoftue')
86 return self._bin_prefix
87
88 def cleanup(self):
89 if self.process is None:
90 return
91 if self.setup_runs_locally():
92 return
93 # copy back files (may not exist, for instance if there was an early error of process):
94 try:
95 self.rem_host.scpfrom('scp-back-log', self.remote_log_file, self.log_file)
96 except Exception as e:
97 self.log(repr(e))
98
99 def setup_runs_locally(self):
100 return self.remote_user is None
101
102 def netns(self):
103 return "amarisoftue1"
104
105 def stop(self):
106 self.suite_run.stop_process(self.process)
107
108 def connect(self, enb):
109 self.log('Starting amarisoftue')
110 self.enb = enb
111 self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name()))
112 self.configure()
113 if self.setup_runs_locally():
114 self.start_locally()
115 else:
116 self.start_remotely()
117
118 # send t+Enter to enable console trace
119 self.dbg('Enabling console trace')
120 self.process.stdin_write('t\n')
121
122 def start_remotely(self):
123 remote_binary = self.remote_inst.child('', AmarisoftUE.BINFILE)
124 # setting capabilities will later disable use of LD_LIBRARY_PATH from ELF loader -> modify RPATH instead.
125 self.log('Setting RPATH for ltetue')
126 # amarisoftue binary needs patchelf >= 0.9+52 to avoid failing during patch. OS#4389, patchelf-GH#192.
127 self.rem_host.set_remote_env({'PATCHELF_BIN': '/opt/bin/patchelf-v0.10' })
128 self.rem_host.change_elf_rpath(remote_binary, str(self.remote_inst))
129
130 # amarisoftue requires CAP_SYS_ADMIN to cjump to net network namespace: netns(CLONE_NEWNET):
131 # amarisoftue requires CAP_NET_ADMIN to create tunnel devices: ioctl(TUNSETIFF):
132 self.log('Applying CAP_SYS_ADMIN+CAP_NET_ADMIN capability to ltetue')
133 self.rem_host.setcap_netsys_admin(remote_binary)
134
135 self.log('Creating netns %s' % self.netns())
136 self.rem_host.create_netns(self.netns())
137
138 args = (remote_binary, self.remote_config_file)
139 self.process = self.rem_host.RemoteProcess(AmarisoftUE.BINFILE, args)
140 self.suite_run.remember_to_stop(self.process)
141 self.process.launch()
142
143 def start_locally(self):
144 binary = self.inst.child('', AmarisoftUE.BINFILE)
145 env = {}
146
147 # setting capabilities will later disable use of LD_LIBRARY_PATH from ELF loader -> modify RPATH instead.
148 self.log('Setting RPATH for lteue')
149 util.change_elf_rpath(binary, util.prepend_library_path(self.inst), self.run_dir.new_dir('patchelf'))
150
151 # amarisoftue requires CAP_SYS_ADMIN to cjump to net network namespace: netns(CLONE_NEWNET):
152 # amarisoftue requires CAP_NET_ADMIN to create tunnel devices: ioctl(TUNSETIFF):
153 self.log('Applying CAP_SYS_ADMIN+CAP_NET_ADMIN capability to lteue')
154 util.setcap_netsys_admin(binary, self.run_dir.new_dir('setcap_netsys_admin'))
155
156 self.log('Creating netns %s' % self.netns())
157 util.create_netns(self.netns(), self.run_dir.new_dir('create_netns'))
158
159 args = (binary, os.path.abspath(self.config_file))
160 self.dbg(run_dir=self.run_dir, binary=binary, env=env)
161 self.process = process.Process(self.name(), self.run_dir, args, env=env)
162 self.suite_run.remember_to_stop(self.process)
163 self.process.launch()
164
165 def gen_conf_file(self, path, filename, values):
166 self.dbg('AmarisoftUE ' + filename + ':\n' + pprint.pformat(values))
167 with open(path, 'w') as f:
168 r = template.render(filename, values)
169 self.dbg(r)
170 f.write(r)
171
172 def configure(self):
173 self.inst = util.Dir(os.path.abspath(self.bin_prefix()))
174 if not self.inst.isfile('', AmarisoftUE.BINFILE):
175 raise log.Error('No %s binary in' % AmarisoftUE.BINFILE, self.inst)
176
177 self.config_file = self.run_dir.child(AmarisoftUE.CFGFILE)
178 self.config_rf_file = self.run_dir.child(AmarisoftUE.CFGFILE_RF)
179 self.log_file = self.run_dir.child(AmarisoftUE.LOGFILE)
180
181 if not self.setup_runs_locally():
182 self.rem_host = remote.RemoteHost(self.run_dir, self.remote_user, self._addr)
183 remote_prefix_dir = util.Dir(AmarisoftUE.REMOTE_DIR)
184 self.remote_inst = util.Dir(remote_prefix_dir.child(os.path.basename(str(self.inst))))
185 remote_run_dir = util.Dir(remote_prefix_dir.child(AmarisoftUE.BINFILE))
186
187 self.remote_config_file = remote_run_dir.child(AmarisoftUE.CFGFILE)
188 self.remote_config_rf_file = remote_run_dir.child(AmarisoftUE.CFGFILE_RF)
189 self.remote_log_file = remote_run_dir.child(AmarisoftUE.LOGFILE)
190
191 values = dict(ue=config.get_defaults('amarisoft'))
192 config.overlay(values, dict(ue=config.get_defaults('amarisoftue')))
193 config.overlay(values, dict(ue=self.suite_run.config().get('amarisoft', {})))
194 config.overlay(values, dict(ue=self.suite_run.config().get('modem', {})))
195 config.overlay(values, dict(ue=self._conf))
196 config.overlay(values, dict(ue=dict(num_antennas = self.enb.num_ports())))
197
198 logfile = self.log_file if self.setup_runs_locally() else self.remote_log_file
199 config.overlay(values, dict(ue=dict(log_filename=logfile)))
200
201 # We need to set some specific variables programatically here to match IP addresses:
202 if self._conf.get('rf_dev_type') == 'zmq':
203 base_srate = num_prb2base_srate(self.enb.num_prb())
204 rf_dev_args = 'tx_port0=tcp://' + self.addr() + ':2001' \
205 + ',tx_port1=tcp://' + self.addr() + ':2003' \
206 + ',rx_port0=tcp://' + self.enb.addr() + ':2000' \
207 + ',rx_port1=tcp://' + self.enb.addr() + ':2002' \
208 + ',tx_freq=2510e6,rx_freq=2630e6,tx_freq2=2530e6,rx_freq2=2650e6' \
209 + ',id=ue,base_srate='+ str(base_srate)
210 config.overlay(values, dict(ue=dict(sample_rate = base_srate / (1000*1000),
211 rf_dev_args = rf_dev_args)))
212
213 # Set UHD frame size as a function of the cell bandwidth on B2XX
214 if self._conf.get('rf_dev_type') == 'UHD' and values['ue'].get('rf_dev_args', None) is not None:
215 if 'b200' in values['ue'].get('rf_dev_args'):
216 rf_dev_args = values['ue'].get('rf_dev_args', '')
217 rf_dev_args += ',' if rf_dev_args != '' and not rf_dev_args.endswith(',') else ''
218
219 if self.enb.num_prb() < 25:
220 rf_dev_args += 'send_frame_size=512,recv_frame_size=512'
221 elif self.enb.num_prb() == 25:
222 rf_dev_args += 'send_frame_size=1024,recv_frame_size=1024'
223 elif self.enb.num_prb() > 50:
224 rf_dev_args += 'num_recv_frames=64,num_send_frames=64'
225
226 # For 15 and 20 MHz, further reduce over the wire format to sc12
227 if self.enb.num_prb() >= 75:
228 rf_dev_args += ',otw_format=sc12'
229
230 config.overlay(values, dict(ue=dict(rf_dev_args=rf_dev_args)))
231
232 # rf driver is shared between amarisoft enb and ue, so it has a
233 # different cfg namespace 'trx'. Copy needed values over there:
234 config.overlay(values, dict(trx=dict(rf_dev_type=values['ue'].get('rf_dev_type', None),
235 rf_dev_args=values['ue'].get('rf_dev_args', None))))
236
237 self.gen_conf_file(self.config_file, AmarisoftUE.CFGFILE, values)
238 self.gen_conf_file(self.config_rf_file, AmarisoftUE.CFGFILE_RF, values)
239
240 if not self.setup_runs_locally():
241 self.rem_host.recreate_remote_dir(self.remote_inst)
242 self.rem_host.scp('scp-inst-to-remote', str(self.inst), remote_prefix_dir)
243 self.rem_host.recreate_remote_dir(remote_run_dir)
244 self.rem_host.scp('scp-cfg-to-remote', self.config_file, self.remote_config_file)
245 self.rem_host.scp('scp-cfg-rf-to-remote', self.config_rf_file, self.remote_config_rf_file)
246
247 def is_connected(self, mcc_mnc=None):
248 return 'Network attach successful.' in (self.process.get_stdout() or '')
249
250 def is_attached(self):
251 return self.is_connected()
252
253 def running(self):
254 return not self.process.terminated()
255
256 def addr(self):
257 return self._addr
258
259 def run_node(self):
260 return RunNode(RunNode.T_REM_SSH, self._addr, self.remote_user, self._addr)
261
262 def run_netns_wait(self, name, popen_args):
263 if self.setup_runs_locally():
264 proc = process.NetNSProcess(name, self.run_dir.new_dir(name), self.netns(), popen_args, env={})
265 else:
266 proc = self.rem_host.RemoteNetNSProcess(name, self.netns(), popen_args, env={})
267 proc.launch_sync()
268 return proc
269
270 def verify_metric(self, value, operation='avg', metric='dl_brate', criterion='gt'):
271 return 'metrics not yet implemented with Amarisoft UE'
272
273# vim: expandtab tabstop=4 shiftwidth=4