blob: f5fa90b1eb4b132a6bff6cfa1f1c8d64e5a079a6 [file] [log] [blame]
Pau Espin Pedroldaed4472017-09-15 14:11:35 +02001# osmo_gsm_tester: specifics for running an osmo-bts-octphy
2#
3# Copyright (C) 2016-2017 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
22import tempfile
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +010023from . import log, config, util, template, process, event_loop, pcu_osmo, bts_osmo
Pau Espin Pedroldaed4472017-09-15 14:11:35 +020024
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +010025class OsmoBtsOctphy(bts_osmo.OsmoBtsMainUnit):
Pau Espin Pedrol4b475be2017-12-14 19:01:25 +010026
27##############
28# PROTECTED
29##############
Pau Espin Pedroldaed4472017-09-15 14:11:35 +020030 run_dir = None
31 inst = None
32 env = None
Pau Espin Pedroldaed4472017-09-15 14:11:35 +020033
34 BIN_BTS_OCTPHY = 'osmo-bts-octphy'
35
36 CONF_BTS_OCTPHY = 'osmo-bts-octphy.cfg'
37
38 def __init__(self, suite_run, conf):
Pau Espin Pedrolc9817a52017-12-13 19:52:27 +010039 super().__init__(suite_run, conf, OsmoBtsOctphy.BIN_BTS_OCTPHY)
Pau Espin Pedroldaed4472017-09-15 14:11:35 +020040 self.env = {}
41 self.values = {}
Pau Espin Pedroldaed4472017-09-15 14:11:35 +020042
Pau Espin Pedroldaed4472017-09-15 14:11:35 +020043 def launch_process(self, binary_name, *args):
44 binary = os.path.abspath(self.inst.child('bin', binary_name))
45 run_dir = self.run_dir.new_dir(binary_name)
46 if not os.path.isfile(binary):
47 raise RuntimeError('Binary missing: %r' % binary)
48 proc = process.Process(binary_name, run_dir,
49 (binary,) + args,
50 env=self.env)
51 self.suite_run.remember_to_stop(proc)
52 proc.launch()
53 return proc
54
55 def num_trx(self):
56 return len(self.values['osmo_bts_octphy'].get('trx_list', []))
57
58 def allocate_phy_instances(self, c):
59 '''
60 Generate match trx Z <-> phy X inst Y to use in vty config
61
62 We create a new phy for each trx found with a new hwaddr. If hwaddr is
63 already there, increase num_instances and give last instance index to
64 the current trx.
65 '''
66 phy_list = []
67 for trx in c.get('trx_list', []):
68 hwaddr = trx.get('hw_addr', None)
69 netdev = trx.get('net_device', None)
70 if hwaddr is None:
71 raise log.Error('Expected hw-addr value not found!')
72 found = False
73 phy_idx = 0
74 for phy in phy_list:
75 if phy['hw_addr'] == hwaddr:
76 phy['num_instances'] += 1
77 found = True
78 break
79 phy_idx += 1
80 if not found:
81 phy_list.append({'hw_addr': hwaddr, 'net_device': netdev, 'num_instances': 1})
82 trx['phy_idx'] = phy_idx
83 trx['instance_idx'] = phy_list[phy_idx]['num_instances'] - 1
84 c['phy_list'] = phy_list
85
86 def configure(self):
87 if self.bsc is None:
88 raise RuntimeError('BTS needs to be added to a BSC or NITB before it can be configured')
89 self.config_file = self.run_dir.new_file(OsmoBtsOctphy.CONF_BTS_OCTPHY)
90 self.dbg(config_file=self.config_file)
91
92 values = dict(osmo_bts_octphy=config.get_defaults('osmo_bts_octphy'))
93 config.overlay(values, self.suite_run.config())
94 config.overlay(values, {
95 'osmo_bts_octphy': {
96 'oml_remote_ip': self.bsc.addr(),
97 'pcu_socket_path': self.pcu_socket_path(),
98 }
99 })
100 config.overlay(values, { 'osmo_bts_octphy': self.conf })
101
102 self.allocate_phy_instances(values['osmo_bts_octphy'])
103
104 self.dbg('OSMO-BTS-OCTPHY CONFIG:\n' + pprint.pformat(values))
105 self.values = values
106 with open(self.config_file, 'w') as f:
107 r = template.render(OsmoBtsOctphy.CONF_BTS_OCTPHY, values)
108 self.dbg(r)
109 f.write(r)
110
Pau Espin Pedrol4b475be2017-12-14 19:01:25 +0100111########################
112# PUBLIC - INTERNAL API
113########################
Pau Espin Pedroldaed4472017-09-15 14:11:35 +0200114 def conf_for_bsc(self):
115 values = config.get_defaults('bsc_bts')
116 config.overlay(values, config.get_defaults('osmo_bts_octphy'))
Pau Espin Pedrol5e0c2512017-11-06 18:40:23 +0100117 if self.lac is not None:
118 config.overlay(values, { 'location_area_code': self.lac })
Pau Espin Pedrol8a3a7b52017-11-28 15:50:02 +0100119 if self.rac is not None:
120 config.overlay(values, { 'routing_area_code': self.rac })
Pau Espin Pedrol4ccce7c2017-11-07 11:13:20 +0100121 if self.cellid is not None:
122 config.overlay(values, { 'cell_identity': self.cellid })
Pau Espin Pedrol8a3a7b52017-11-28 15:50:02 +0100123 if self.bvci is not None:
124 config.overlay(values, { 'bvci': self.bvci })
Pau Espin Pedroldaed4472017-09-15 14:11:35 +0200125 config.overlay(values, self.conf)
Pau Espin Pedrol05314b42017-11-23 13:35:42 +0100126
127 sgsn_conf = {} if self.sgsn is None else self.sgsn.conf_for_client()
128 config.overlay(values, sgsn_conf)
129
Pau Espin Pedroldaed4472017-09-15 14:11:35 +0200130 self.dbg(conf=values)
131 return values
132
Pau Espin Pedrol4b475be2017-12-14 19:01:25 +0100133###################
134# PUBLIC (test API included)
135###################
136 def start(self):
137 if self.bsc is None:
138 raise RuntimeError('BTS needs to be added to a BSC or NITB before it can be started')
139 self.suite_run.poll()
140
141 self.log('Starting to connect to', self.bsc)
142 self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name()))
143 self.configure()
144
145 self.inst = util.Dir(os.path.abspath(self.suite_run.trial.get_inst('osmo-bts')))
146 btsoct_path = self.inst.child('bin', OsmoBtsOctphy.BIN_BTS_OCTPHY)
147 lib = self.inst.child('lib')
148 if not os.path.isdir(lib):
149 raise RuntimeError('No lib/ in %r' % self.inst)
150
151 # setting capabilities will later disable use of LD_LIBRARY_PATH from ELF loader -> modify RPATH instead.
152 self.log('Setting RPATH for', OsmoBtsOctphy.BIN_BTS_OCTPHY)
153 util.change_elf_rpath(btsoct_path, util.prepend_library_path(lib), self.run_dir.new_dir('patchelf'))
154 # osmo-bty-octphy requires CAP_NET_RAW to open AF_PACKET socket:
155 self.log('Applying CAP_NET_RAW capability to', OsmoBtsOctphy.BIN_BTS_OCTPHY)
156 util.setcap_net_raw(btsoct_path, self.run_dir.new_dir('setcap_net_raw'))
157
158 self.proc_bts = self.launch_process(OsmoBtsOctphy.BIN_BTS_OCTPHY, '-r', '1',
159 '-c', os.path.abspath(self.config_file),
160 '-i', self.bsc.addr(), '-t', str(self.num_trx()))
161 self.suite_run.poll()
162
Pau Espin Pedroldaed4472017-09-15 14:11:35 +0200163# vim: expandtab tabstop=4 shiftwidth=4