Pau Espin Pedrol | c8b0f93 | 2020-02-11 17:45:26 +0100 | [diff] [blame] | 1 | # 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 | |
| 20 | import os |
| 21 | import pprint |
| 22 | |
| 23 | from . import log, util, config, template, process, remote |
| 24 | from .run_node import RunNode |
| 25 | from .ms import MS |
| 26 | |
| 27 | class srsUE(MS): |
| 28 | |
| 29 | REMOTE_DIR = '/osmo-gsm-tester-srsue' |
| 30 | BINFILE = 'srsue' |
| 31 | CFGFILE = 'srsue.conf' |
| 32 | PCAPFILE = 'srsue.pcap' |
| 33 | LOGFILE = 'srsue.log' |
| 34 | |
| 35 | def __init__(self, suite_run, conf): |
| 36 | self._addr = conf.get('addr', None) |
| 37 | if self._addr is None: |
| 38 | raise log.Error('addr not set') |
| 39 | super().__init__('srsue_%s' % self._addr, conf) |
| 40 | self.enb = None |
| 41 | self.run_dir = None |
| 42 | self.config_file = None |
| 43 | self.log_file = None |
| 44 | self.pcap_file = None |
| 45 | self.process = None |
| 46 | self.rem_host = None |
| 47 | self.remote_config_file = None |
| 48 | self.remote_log_file = None |
| 49 | self.remote_pcap_file = None |
| 50 | self.suite_run = suite_run |
| 51 | self.nof_prb=50 |
| 52 | if self.nof_prb == 75: |
| 53 | self.base_srate=15.36e6 |
| 54 | else: |
| 55 | self.base_srate=23.04e6 |
| 56 | self.remote_user = conf.get('remote_user', None) |
| 57 | |
| 58 | def cleanup(self): |
| 59 | if self.process is None: |
| 60 | return |
| 61 | if self.setup_runs_locally(): |
| 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)) |
| 68 | try: |
| 69 | self.rem_host.scpfrom('scp-back-pcap', self.remote_pcap_file, self.pcap_file) |
| 70 | except Exception as e: |
| 71 | self.log(repr(e)) |
| 72 | |
| 73 | def setup_runs_locally(self): |
| 74 | return self.remote_user is None |
| 75 | |
| 76 | def netns(self): |
| 77 | return "srsue1" |
| 78 | |
| 79 | def connect(self, enb): |
| 80 | self.log('Starting srsue') |
| 81 | self.enb = enb |
| 82 | self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name())) |
| 83 | self.configure() |
| 84 | if self.setup_runs_locally(): |
| 85 | self.start_locally() |
| 86 | else: |
| 87 | self.start_remotely() |
| 88 | |
| 89 | def start_remotely(self): |
| 90 | self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('srslte'))) |
| 91 | lib = self.inst.child('lib') |
| 92 | if not os.path.isdir(lib): |
| 93 | raise log.Error('No lib/ in', self.inst) |
| 94 | if not self.inst.isfile('bin', srsUE.BINFILE): |
| 95 | raise log.Error('No %s binary in' % srsUE.BINFILE, self.inst) |
| 96 | |
| 97 | self.rem_host = remote.RemoteHost(self.run_dir, self.remote_user, self._addr) |
| 98 | remote_prefix_dir = util.Dir(srsUE.REMOTE_DIR) |
| 99 | remote_inst = util.Dir(remote_prefix_dir.child(os.path.basename(str(self.inst)))) |
| 100 | remote_run_dir = util.Dir(remote_prefix_dir.child(srsUE.BINFILE)) |
| 101 | self.remote_config_file = remote_run_dir.child(srsUE.CFGFILE) |
| 102 | self.remote_log_file = remote_run_dir.child(srsUE.LOGFILE) |
| 103 | self.remote_pcap_file = remote_run_dir.child(srsUE.PCAPFILE) |
| 104 | |
| 105 | self.rem_host.recreate_remote_dir(remote_inst) |
| 106 | self.rem_host.scp('scp-inst-to-remote', str(self.inst), remote_prefix_dir) |
| 107 | self.rem_host.create_remote_dir(remote_run_dir) |
| 108 | self.rem_host.scp('scp-cfg-to-remote', self.config_file, self.remote_config_file) |
| 109 | |
| 110 | remote_lib = remote_inst.child('lib') |
| 111 | remote_binary = remote_inst.child('bin', srsUE.BINFILE) |
| 112 | # setting capabilities will later disable use of LD_LIBRARY_PATH from ELF loader -> modify RPATH instead. |
| 113 | self.log('Setting RPATH for srsue') |
| 114 | # srsue binary needs patchelf >= 0.9+52 to avoid failing during patch. OS#4389, patchelf-GH#192. |
| 115 | self.rem_host.set_remote_env({'PATCHELF_BIN': '/opt/bin/patchelf-v0.10' }) |
| 116 | self.rem_host.change_elf_rpath(remote_binary, remote_lib) |
| 117 | |
| 118 | # srsue requires CAP_SYS_ADMIN to cjump to net network namespace: netns(CLONE_NEWNET): |
| 119 | # srsue requires CAP_NET_ADMIN to create tunnel devices: ioctl(TUNSETIFF): |
| 120 | self.log('Applying CAP_SYS_ADMIN+CAP_NET_ADMIN capability to srsue') |
| 121 | self.rem_host.setcap_netsys_admin(remote_binary) |
| 122 | |
| 123 | #'strace', '-ff', |
| 124 | args = (remote_binary, self.remote_config_file, |
| 125 | '--rf.device_name=zmq', |
| 126 | '--rf.device_args="tx_port=tcp://'+ self.addr() +':2001,rx_port=tcp://'+ self.enb.addr() +':2000,id=ue,base_srate='+ str(self.base_srate) + '"', |
| 127 | '--phy.nof_phy_threads=1', |
| 128 | '--gw.netns=' + self.netns(), |
| 129 | '--log.filename=' + 'stdout', #self.remote_log_file, |
| 130 | '--pcap.enable=true', |
| 131 | '--pcap.filename=' + self.remote_pcap_file) |
| 132 | |
| 133 | self.process = self.rem_host.RemoteProcessFixIgnoreSIGHUP(srsUE.BINFILE, util.Dir(srsUE.REMOTE_DIR), args) |
| 134 | #self.process = self.rem_host.RemoteProcessFixIgnoreSIGHUP(srsUE.BINFILE, remote_run_dir, args, remote_lib) |
| 135 | self.suite_run.remember_to_stop(self.process) |
| 136 | self.process.launch() |
| 137 | |
| 138 | def start_locally(self): |
| 139 | inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('srslte'))) |
| 140 | |
| 141 | binary = inst.child('bin', BINFILE) |
| 142 | if not os.path.isfile(binary): |
| 143 | raise log.Error('Binary missing:', binary) |
| 144 | lib = inst.child('lib') |
| 145 | if not os.path.isdir(lib): |
| 146 | raise log.Error('No lib/ in', inst) |
| 147 | |
| 148 | env = {} |
| 149 | |
| 150 | # setting capabilities will later disable use of LD_LIBRARY_PATH from ELF loader -> modify RPATH instead. |
| 151 | self.log('Setting RPATH for srsue') |
| 152 | util.change_elf_rpath(binary, util.prepend_library_path(lib), self.run_dir.new_dir('patchelf')) |
| 153 | |
| 154 | # srsue requires CAP_SYS_ADMIN to cjump to net network namespace: netns(CLONE_NEWNET): |
| 155 | # srsue requires CAP_NET_ADMIN to create tunnel devices: ioctl(TUNSETIFF): |
| 156 | self.log('Applying CAP_SYS_ADMIN+CAP_NET_ADMIN capability to srsue') |
| 157 | util.setcap_netsys_admin(binary, self.run_dir.new_dir('setcap_netsys_admin')) |
| 158 | |
| 159 | args = (binary, os.path.abspath(self.config_file), |
| 160 | '--rf.device_name=zmq', |
| 161 | '--rf.device_args="tx_port=tcp://'+ self.addr() +':2001,rx_port=tcp://'+ self.enb.addr() +':2000,id=ue,base_srate='+ str(self.base_srate) + '"', |
| 162 | '--phy.nof_phy_threads=1', |
| 163 | '--gw.netns=' + self.netns(), |
| 164 | '--log.filename=' + self.log_file, |
| 165 | '--pcap.enable=true', |
| 166 | '--pcap.filename=' + self.pcap_file) |
| 167 | |
| 168 | self.dbg(run_dir=self.run_dir, binary=binary, env=env) |
| 169 | self.process = process.Process(self.name(), self.run_dir, args, env=env) |
| 170 | self.suite_run.remember_to_stop(self.process) |
| 171 | self.process.launch() |
| 172 | |
| 173 | def configure(self): |
| 174 | self.config_file = self.run_dir.new_file(srsUE.CFGFILE) |
| 175 | self.log_file = self.run_dir.child(srsUE.LOGFILE) |
| 176 | self.pcap_file = self.run_dir.new_file(srsUE.PCAPFILE) |
| 177 | self.dbg(config_file=self.config_file) |
| 178 | |
| 179 | values = dict(ue=config.get_defaults('srsue')) |
| 180 | config.overlay(values, self.suite_run.config()) |
| 181 | config.overlay(values, dict(ue=self._conf)) |
| 182 | |
| 183 | self.dbg('SRSUE CONFIG:\n' + pprint.pformat(values)) |
| 184 | |
| 185 | with open(self.config_file, 'w') as f: |
| 186 | r = template.render(srsUE.CFGFILE, values) |
| 187 | self.dbg(r) |
| 188 | f.write(r) |
| 189 | |
| 190 | def is_connected(self, mcc_mnc=None): |
| 191 | return 'Network attach successful.' in (self.process.get_stdout() or '') |
| 192 | |
| 193 | def is_attached(self): |
| 194 | return self.is_connected() |
| 195 | |
| 196 | def running(self): |
| 197 | return not self.process.terminated() |
| 198 | |
| 199 | def addr(self): |
| 200 | return self._addr |
| 201 | |
| 202 | def run_node(self): |
| 203 | # TODO: move to an object |
| 204 | return RunNode(RunNode.T_REM_SSH, self._addr, self.remote_user, self._addr) |
| 205 | |
| 206 | def run_netns_wait(self, name, popen_args): |
| 207 | if self.setup_runs_locally(): |
| 208 | proc = process.NetNSProcess(name, self.run_dir.new_dir(name), self.netns(), popen_args, env={}) |
| 209 | else: |
| 210 | proc = self.rem_host.RemoteNetNSProcess(name, self.netns(), popen_args, env={}) |
| 211 | proc.launch_sync() |
| 212 | |
| 213 | # vim: expandtab tabstop=4 shiftwidth=4 |