blob: b97dd9251f6fc6efef949a45fcc9d55d6fa9d46e [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)
75
76 self.process = self.rem_host.RemoteProcess(Open5gsSGWU.BINFILE, args, remote_env=remote_env)
77 self.testenv.remember_to_stop(self.process)
78 self.process.launch()
79
80 def start_locally(self):
81 binary = self.inst.child('bin', Open5gsSGWU.BINFILE)
82 lib = self.inst.child('lib')
83 env = { 'LD_LIBRARY_PATH': util.prepend_library_path(lib) }
84
85 # setting capabilities will later disable use of LD_LIBRARY_PATH from ELF loader -> modify RPATH instead.
86 self.log('Setting RPATH for open5gs-sgwud')
87 # open5gs-sgwud binary needs patchelf <= 0.9 (0.10 and current master fail) to avoid failing during patch. OS#4389, patchelf-GH#192.
88 util.change_elf_rpath(binary, util.prepend_library_path(lib), self.run_dir.new_dir('patchelf'))
89 # open5gs-sgwud requires CAP_NET_ADMIN to create tunnel devices: ioctl(TUNSETIFF):
90 self.log('Applying CAP_NET_ADMIN capability to open5gs-sgwud')
91 util.setcap_net_admin(binary, self.run_dir.new_dir('setcap_net_admin'))
92
93 args = (binary, '-c', os.path.abspath(self.config_file))
94
95 self.process = process.Process(self.name(), self.run_dir, args, env=env)
96 self.testenv.remember_to_stop(self.process)
97 self.process.launch()
98
99 def configure(self, values):
100 self.run_dir = util.Dir(self.o5gs_epc.run_dir.new_dir(self.name()))
101 self.inst = util.Dir(os.path.abspath(self.testenv.suite().trial().get_inst('open5gs', self._run_node.run_label())))
102 if not os.path.isdir(self.inst.child('lib')):
103 raise log.Error('No lib/ in', self.inst)
104 if not self.inst.isfile('bin', Open5gsSGWU.BINFILE):
105 raise log.Error('No %s binary in' % Open5gsSGWU.BINFILE, self.inst)
106
107 self.config_file = self.run_dir.child(Open5gsSGWU.CFGFILE)
108 self.log_file = self.run_dir.child(Open5gsSGWU.LOGFILE)
109
110 if not self._run_node.is_local():
111 self.rem_host = remote.RemoteHost(self.run_dir, self._run_node.ssh_user(), self._run_node.ssh_addr())
112 remote_prefix_dir = util.Dir(Open5gsSGWU.REMOTE_DIR)
113 self.remote_inst = util.Dir(remote_prefix_dir.child(os.path.basename(str(self.inst))))
114 remote_run_dir = util.Dir(remote_prefix_dir.child(Open5gsSGWU.BINFILE))
115
116 self.remote_config_file = remote_run_dir.child(Open5gsSGWU.CFGFILE)
117 self.remote_log_file = remote_run_dir.child(Open5gsSGWU.LOGFILE)
118
119 logfile = self.log_file if self._run_node.is_local() else self.remote_log_file
120 inst_prefix = str(self.inst) if self._run_node.is_local() else str(self.remote_inst)
121 config.overlay(values, dict(sgwu=dict(log_filename=logfile,
122 inst_prefix=inst_prefix)))
123
124 self.dbg('OPEN5GS-SGWU CONFIG:\n' + pprint.pformat(values))
125
126 with open(self.config_file, 'w') as f:
127 r = template.render(Open5gsSGWU.CFGFILE, values)
128 self.dbg(r)
129 f.write(r)
130
131 if not self._run_node.is_local():
132 self.rem_host.recreate_remote_dir(self.remote_inst)
133 self.rem_host.scp('scp-inst-to-remote', str(self.inst), remote_prefix_dir)
134 self.rem_host.recreate_remote_dir(remote_run_dir)
135 self.rem_host.scp('scp-cfg-to-remote', self.config_file, self.remote_config_file)
136
137 def running(self):
138 return not self.process.terminated()
139
140# vim: expandtab tabstop=4 shiftwidth=4