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