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