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