blob: 79fab8d7fe6b8c6787289c20e7333cc92d29940d [file] [log] [blame]
Pau Espin Pedrol0696c602021-03-16 14:25:37 +01001# osmo_gsm_tester: specifics for running an Open5GS sgwud process
2#
3# Copyright (C) 2021 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 ..core import log, util, config, template, process, remote
24from ..core import schema
25from . import epc
26
27def on_register_schemas():
28 pass
29
30class Open5gsSGWU(log.Origin):
31
32 REMOTE_DIR = '/osmo-gsm-tester-open5gs'
33 BINFILE = 'open5gs-sgwud'
34 CFGFILE = 'open5gs-sgwud.yaml'
35 LOGFILE = 'open5gs-sgwud.log'
36
37 def __init__(self, testenv, o5gs_epc):
38 super().__init__(log.C_RUN, 'open5gs-sgwud')
39 self.testenv = testenv
40 self.o5gs_epc = o5gs_epc
41 self._run_node = o5gs_epc.run_node()
42 self.run_dir = None
43 self.config_file = None
44 self.log_file = None
45 self.process = None
46 self.rem_host = None
47 self.remote_inst = None
48 self.remote_config_file = None
49 self.remote_log_file = None
50
51 def cleanup(self):
52 if self.process is None:
53 return
54 if self._run_node.is_local():
55 return
56 # copy back files (may not exist, for instance if there was an early error of process):
57 try:
58 self.rem_host.scpfrom('scp-back-log', self.remote_log_file, self.log_file)
59 except Exception as e:
60 self.log(repr(e))
61
62 def start(self):
63 self.log('Starting %s' % Open5gsSGWU.BINFILE)
64 if self._run_node.is_local():
65 self.start_locally()
66 else:
67 self.start_remotely()
68
69 def start_remotely(self):
70 remote_env = { 'LD_LIBRARY_PATH': self.remote_inst.child('lib') }
71 remote_lib = self.remote_inst.child('lib')
72 remote_binary = self.remote_inst.child('bin', Open5gsSGWU.BINFILE)
73
74 args = (remote_binary, '-c', self.remote_config_file)
Andre Puschmann9d75ccb2021-05-10 16:41:48 +020075 remote_run_dir = util.Dir(util.Dir(Open5gsSGWU.REMOTE_DIR).child(Open5gsSGWU.BINFILE))
Pau Espin Pedrol0696c602021-03-16 14:25:37 +010076
Andre Puschmann9d75ccb2021-05-10 16:41:48 +020077 self.process = self.rem_host.RemoteProcessSafeExit(Open5gsSGWU.BINFILE, remote_run_dir, args, remote_env=remote_env)
Pau Espin Pedrol0696c602021-03-16 14:25:37 +010078 self.testenv.remember_to_stop(self.process)
79 self.process.launch()
80
81 def start_locally(self):
82 binary = self.inst.child('bin', Open5gsSGWU.BINFILE)
83 lib = self.inst.child('lib')
84 env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
85
86 # setting capabilities will later disable use of LD_LIBRARY_PATH from ELF loader -> modify RPATH instead.
87 self.log('Setting RPATH for open5gs-sgwud')
88 # open5gs-sgwud binary needs patchelf <= 0.9 (0.10 and current master fail) to avoid failing during patch. OS#4389, patchelf-GH#192.
89 util.change_elf_rpath(binary, util.prepend_library_path(lib), self.run_dir.new_dir('patchelf'))
90 # open5gs-sgwud requires CAP_NET_ADMIN to create tunnel devices: ioctl(TUNSETIFF):
91 self.log('Applying CAP_NET_ADMIN capability to open5gs-sgwud')
92 util.setcap_net_admin(binary, self.run_dir.new_dir('setcap_net_admin'))
93
94 args = (binary, '-c', os.path.abspath(self.config_file))
95
96 self.process = process.Process(self.name(), self.run_dir, args, env=env)
97 self.testenv.remember_to_stop(self.process)
98 self.process.launch()
99
100 def configure(self, values):
101 self.run_dir = util.Dir(self.o5gs_epc.run_dir.new_dir(self.name()))
102 self.inst = util.Dir(os.path.abspath(self.testenv.suite().trial().get_inst('open5gs', self._run_node.run_label())))
103 if not os.path.isdir(self.inst.child('lib')):
104 raise log.Error('No lib/ in', self.inst)
105 if not self.inst.isfile('bin', Open5gsSGWU.BINFILE):
106 raise log.Error('No %s binary in' % Open5gsSGWU.BINFILE, self.inst)
107
108 self.config_file = self.run_dir.child(Open5gsSGWU.CFGFILE)
109 self.log_file = self.run_dir.child(Open5gsSGWU.LOGFILE)
110
111 if not self._run_node.is_local():
112 self.rem_host = remote.RemoteHost(self.run_dir, self._run_node.ssh_user(), self._run_node.ssh_addr())
113 remote_prefix_dir = util.Dir(Open5gsSGWU.REMOTE_DIR)
114 self.remote_inst = util.Dir(remote_prefix_dir.child(os.path.basename(str(self.inst))))
115 remote_run_dir = util.Dir(remote_prefix_dir.child(Open5gsSGWU.BINFILE))
116
117 self.remote_config_file = remote_run_dir.child(Open5gsSGWU.CFGFILE)
118 self.remote_log_file = remote_run_dir.child(Open5gsSGWU.LOGFILE)
119
120 logfile = self.log_file if self._run_node.is_local() else self.remote_log_file
121 inst_prefix = str(self.inst) if self._run_node.is_local() else str(self.remote_inst)
122 config.overlay(values, dict(sgwu=dict(log_filename=logfile,
123 inst_prefix=inst_prefix)))
124
125 self.dbg('OPEN5GS-SGWU CONFIG:\n' + pprint.pformat(values))
126
127 with open(self.config_file, 'w') as f:
128 r = template.render(Open5gsSGWU.CFGFILE, values)
129 self.dbg(r)
130 f.write(r)
131
132 if not self._run_node.is_local():
133 self.rem_host.recreate_remote_dir(self.remote_inst)
134 self.rem_host.scp('scp-inst-to-remote', str(self.inst), remote_prefix_dir)
135 self.rem_host.recreate_remote_dir(remote_run_dir)
136 self.rem_host.scp('scp-cfg-to-remote', self.config_file, self.remote_config_file)
137
138 def running(self):
139 return not self.process.terminated()
140
141# vim: expandtab tabstop=4 shiftwidth=4