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