Pau Espin Pedrol | c8b0f93 | 2020-02-11 17:45:26 +0100 | [diff] [blame] | 1 | # 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 | |
| 20 | import os |
| 21 | import pprint |
| 22 | |
| 23 | from . import log, util, config, template, process, remote |
| 24 | |
Pau Espin Pedrol | b693771 | 2020-02-27 18:02:20 +0100 | [diff] [blame] | 25 | def 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 Pedrol | c8b0f93 | 2020-02-11 17:45:26 +0100 | [diff] [blame] | 32 | class 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 |
| 56 | self.subscriber_list = [] |
| 57 | self.suite_run = suite_run |
| 58 | self._run_node = run_node |
| 59 | |
| 60 | def cleanup(self): |
| 61 | if self.process is None: |
| 62 | return |
| 63 | if self._run_node.is_local(): |
| 64 | return |
| 65 | # copy back files (may not exist, for instance if there was an early error of process): |
| 66 | try: |
| 67 | self.rem_host.scpfrom('scp-back-log', self.remote_log_file, self.log_file) |
| 68 | except Exception as e: |
| 69 | self.log(repr(e)) |
| 70 | try: |
| 71 | self.rem_host.scpfrom('scp-back-pcap', self.remote_pcap_file, self.pcap_file) |
| 72 | except Exception as e: |
| 73 | self.log(repr(e)) |
| 74 | |
| 75 | def start(self): |
| 76 | self.log('Starting srsepc') |
| 77 | self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name())) |
| 78 | self.configure() |
| 79 | if self._run_node.is_local(): |
| 80 | self.start_locally() |
| 81 | else: |
| 82 | self.start_remotely() |
| 83 | |
| 84 | def start_remotely(self): |
| 85 | self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('srslte'))) |
| 86 | lib = self.inst.child('lib') |
| 87 | if not os.path.isdir(lib): |
| 88 | raise log.Error('No lib/ in', self.inst) |
| 89 | if not self.inst.isfile('bin', srsEPC.BINFILE): |
| 90 | raise log.Error('No %s binary in' % srsEPC.BINFILE, self.inst) |
| 91 | |
| 92 | self.rem_host = remote.RemoteHost(self.run_dir, self._run_node.ssh_user(), self._run_node.ssh_addr()) |
| 93 | remote_prefix_dir = util.Dir(srsEPC.REMOTE_DIR) |
| 94 | remote_inst = util.Dir(remote_prefix_dir.child(os.path.basename(str(self.inst)))) |
| 95 | remote_run_dir = util.Dir(remote_prefix_dir.child(srsEPC.BINFILE)) |
| 96 | self.remote_config_file = remote_run_dir.child(srsEPC.CFGFILE) |
| 97 | self.remote_db_file = remote_run_dir.child(srsEPC.DBFILE) |
| 98 | self.remote_log_file = remote_run_dir.child(srsEPC.LOGFILE) |
| 99 | self.remote_pcap_file = remote_run_dir.child(srsEPC.PCAPFILE) |
| 100 | |
| 101 | self.rem_host.recreate_remote_dir(remote_inst) |
| 102 | self.rem_host.scp('scp-inst-to-remote', str(self.inst), remote_prefix_dir) |
| 103 | self.rem_host.create_remote_dir(remote_run_dir) |
| 104 | self.rem_host.scp('scp-cfg-to-remote', self.config_file, self.remote_config_file) |
| 105 | self.rem_host.scp('scp-db-to-remote', self.db_file, self.remote_db_file) |
| 106 | |
| 107 | remote_lib = remote_inst.child('lib') |
| 108 | remote_binary = remote_inst.child('bin', srsEPC.BINFILE) |
| 109 | # setting capabilities will later disable use of LD_LIBRARY_PATH from ELF loader -> modify RPATH instead. |
| 110 | self.log('Setting RPATH for srsepc') |
| 111 | self.rem_host.change_elf_rpath(remote_binary, remote_lib) |
| 112 | # srsepc requires CAP_NET_ADMIN to create tunnel devices: ioctl(TUNSETIFF): |
| 113 | self.log('Applying CAP_NET_ADMIN capability to srsepc') |
| 114 | self.rem_host.setcap_net_admin(remote_binary) |
| 115 | |
| 116 | args = (remote_binary, self.remote_config_file, |
| 117 | '--hss.db_file=' + self.remote_db_file, |
| 118 | '--log.filename=' + self.remote_log_file, |
| 119 | '--pcap.enable=true', |
| 120 | '--pcap.filename=' + self.remote_pcap_file) |
| 121 | |
| 122 | self.process = self.rem_host.RemoteProcess(srsEPC.BINFILE, args) |
| 123 | #self.process = self.rem_host.RemoteProcessFixIgnoreSIGHUP(srsEPC.BINFILE, remote_run_dir, args) |
| 124 | self.suite_run.remember_to_stop(self.process) |
| 125 | self.process.launch() |
| 126 | |
| 127 | def start_locally(self): |
| 128 | inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('srslte'))) |
| 129 | |
| 130 | binary = inst.child('bin', BINFILE) |
| 131 | if not os.path.isfile(binary): |
| 132 | raise log.Error('Binary missing:', binary) |
| 133 | lib = inst.child('lib') |
| 134 | if not os.path.isdir(lib): |
| 135 | raise log.Error('No lib/ in', inst) |
| 136 | |
| 137 | env = {} |
| 138 | # setting capabilities will later disable use of LD_LIBRARY_PATH from ELF loader -> modify RPATH instead. |
| 139 | self.log('Setting RPATH for srsepc') |
| 140 | # srsepc binary needs patchelf <= 0.9 (0.10 and current master fail) to avoid failing during patch. OS#4389, patchelf-GH#192. |
| 141 | util.change_elf_rpath(binary, util.prepend_library_path(lib), self.run_dir.new_dir('patchelf')) |
| 142 | # srsepc requires CAP_NET_ADMIN to create tunnel devices: ioctl(TUNSETIFF): |
| 143 | self.log('Applying CAP_NET_ADMIN capability to srsepc') |
| 144 | util.setcap_net_admin(binary, self.run_dir.new_dir('setcap_net_admin')) |
| 145 | |
| 146 | self.dbg(run_dir=self.run_dir, binary=binary, env=env) |
| 147 | args = (binary, os.path.abspath(self.config_file), |
| 148 | '--hss.db_file=' + self.db_file, |
| 149 | '--log.filename=' + self.log_file, |
| 150 | '--pcap.enable=true', |
| 151 | '--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): |
| 158 | self.config_file = self.run_dir.new_file(srsEPC.CFGFILE) |
| 159 | self.db_file = self.run_dir.new_file(srsEPC.DBFILE) |
| 160 | self.log_file = self.run_dir.new_file(srsEPC.LOGFILE) |
| 161 | self.pcap_file = self.run_dir.new_file(srsEPC.PCAPFILE) |
| 162 | self.dbg(config_file=self.config_file, db_file=self.db_file) |
| 163 | |
| 164 | values = dict(epc=config.get_defaults('srsepc')) |
Pau Espin Pedrol | c8b0f93 | 2020-02-11 17:45:26 +0100 | [diff] [blame] | 165 | config.overlay(values, self.suite_run.config()) |
| 166 | config.overlay(values, dict(epc={'run_addr': self.addr()})) |
| 167 | |
Pau Espin Pedrol | b693771 | 2020-02-27 18:02:20 +0100 | [diff] [blame] | 168 | # Set qci for each subscriber: |
| 169 | rlc_drb_mode = values['epc'].get('rlc_drb_mode', None) |
| 170 | assert rlc_drb_mode is not None |
| 171 | for i in range(len(self.subscriber_list)): |
| 172 | self.subscriber_list[i]['qci'] = rlc_drb_mode2qci(rlc_drb_mode) |
| 173 | config.overlay(values, dict(epc=dict(hss=dict(subscribers=self.subscriber_list)))) |
| 174 | |
Pau Espin Pedrol | c8b0f93 | 2020-02-11 17:45:26 +0100 | [diff] [blame] | 175 | self.dbg('SRSEPC CONFIG:\n' + pprint.pformat(values)) |
| 176 | |
| 177 | with open(self.config_file, 'w') as f: |
| 178 | r = template.render(srsEPC.CFGFILE, values) |
| 179 | self.dbg(r) |
| 180 | f.write(r) |
| 181 | with open(self.db_file, 'w') as f: |
| 182 | r = template.render(srsEPC.DBFILE, values) |
| 183 | self.dbg(r) |
| 184 | f.write(r) |
| 185 | |
| 186 | def subscriber_add(self, modem, msisdn=None, algo_str=None): |
| 187 | if msisdn is None: |
| 188 | msisdn = self.suite_run.resources_pool.next_msisdn(modem) |
| 189 | modem.set_msisdn(msisdn) |
| 190 | |
| 191 | if algo_str is None: |
| 192 | algo_str = modem.auth_algo() or util.OSMO_AUTH_ALGO_NONE |
| 193 | |
| 194 | if algo_str != util.OSMO_AUTH_ALGO_NONE and not modem.ki(): |
| 195 | raise log.Error("Auth algo %r selected but no KI specified" % algo_str) |
| 196 | |
| 197 | subscriber_id = len(self.subscriber_list) # list index |
| 198 | self.subscriber_list.append({'id': subscriber_id, 'imsi': modem.imsi(), 'msisdn': msisdn, 'auth_algo': algo_str, 'ki': modem.ki(), 'opc': None}) |
| 199 | |
| 200 | self.log('Add subscriber', msisdn=msisdn, imsi=modem.imsi(), subscriber_id=subscriber_id, |
| 201 | algo_str=algo_str) |
| 202 | return subscriber_id |
| 203 | |
| 204 | def enb_is_connected(self, enb): |
Pau Espin Pedrol | b9aeb15 | 2020-03-09 15:06:45 +0100 | [diff] [blame] | 205 | # TODO: match against srsENB config: "S1 Setup Request - eNB Name: srsenb01, eNB id: 0x19" |
| 206 | return 'S1 Setup Request - eNB' in (self.process.get_stdout() or '') |
Pau Espin Pedrol | c8b0f93 | 2020-02-11 17:45:26 +0100 | [diff] [blame] | 207 | |
| 208 | def running(self): |
| 209 | return not self.process.terminated() |
| 210 | |
| 211 | def addr(self): |
| 212 | return self._addr |
| 213 | |
| 214 | def tun_addr(self): |
| 215 | return '172.16.0.1' |
| 216 | |
| 217 | def run_node(self): |
| 218 | return self._run_node |
| 219 | |
| 220 | # vim: expandtab tabstop=4 shiftwidth=4 |