Pau Espin Pedrol | fbb8611 | 2020-10-16 16:55:23 +0200 | [diff] [blame] | 1 | # osmo_gsm_tester: class defining a RF emulation object implemented using SRS ENB stdin interface |
| 2 | # |
| 3 | # Copyright (C) 2020 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 | |
| 20 | import json |
| 21 | import socket |
| 22 | |
| 23 | from ..core import log |
| 24 | from ..core import util |
| 25 | from ..core import process |
| 26 | from ..core import remote |
| 27 | from ..core.event_loop import MainLoop |
| 28 | |
| 29 | class GrBroker(log.Origin): |
| 30 | |
| 31 | # static fields: |
| 32 | refcount = 0 |
| 33 | instance = None |
| 34 | |
| 35 | def __init__(self): |
| 36 | super().__init__(log.C_RUN, 'gr_zmq_broker') |
| 37 | self.process = None |
| 38 | self.ctrl_port = 5005 |
| 39 | self.run_dir = None |
| 40 | self.rem_host = None |
| 41 | self.enb_li = [] |
| 42 | self.addr = None |
| 43 | self.ctrl_sk = None |
| 44 | self.num_enb_started = 0 |
| 45 | |
| 46 | @staticmethod |
| 47 | def ref(): |
| 48 | if GrBroker.refcount == 0: |
| 49 | GrBroker.instance = GrBroker() |
| 50 | GrBroker.refcount = GrBroker.refcount + 1 |
| 51 | return GrBroker.instance |
| 52 | |
| 53 | @staticmethod |
| 54 | def unref(): |
| 55 | GrBroker.refcount = GrBroker.refcount - 1 |
| 56 | if GrBroker.refcount == 0: |
| 57 | GrBroker.instance.cleanup() |
| 58 | GrBroker.instance = None |
| 59 | |
| 60 | |
| 61 | def cleanup(self): |
| 62 | if self.ctrl_sk is not None: |
| 63 | self.cmd_exit() |
| 64 | self.ctrl_sk.close() |
| 65 | self.ctrl_sk = None |
| 66 | self.enb_li = [] |
| 67 | self.testenv = None |
| 68 | |
| 69 | def register_enb(self, enb): |
| 70 | self.enb_li.append(enb) |
| 71 | |
| 72 | def unregister_enb(self, enb): |
| 73 | self.enb_li.remove(enb) |
| 74 | |
| 75 | def gen_json_enb(self, enb): |
| 76 | res = [] |
| 77 | cell_list = enb.gen_conf['enb']['cell_list'] |
| 78 | for cell in cell_list: |
| 79 | # TODO: probably add enb_id, cell_id to support several ENB |
| 80 | data = {'earfcn': int(cell['dl_earfcn']), |
| 81 | 'bind_port': int(cell['zmq_enb_peer_port']), |
| 82 | 'peer_addr': enb.addr(), |
| 83 | 'peer_port': int(cell['zmq_enb_bind_port']), |
| 84 | 'use_mimo': True if enb.num_ports() > 1 else False |
| 85 | } |
| 86 | res.append(data) |
| 87 | return res |
| 88 | |
| 89 | def gen_json_ue(self, enb): |
| 90 | res = {} |
| 91 | res = [] |
| 92 | earfcns_done = [] |
| 93 | cell_list = enb.gen_conf['enb']['cell_list'] |
| 94 | for cell in cell_list: |
| 95 | data = {} |
| 96 | if int(cell['dl_earfcn']) in earfcns_done: |
| 97 | continue |
| 98 | earfcns_done.append(int(cell['dl_earfcn'])) |
| 99 | data = {'earfcn': int(cell['dl_earfcn']), |
| 100 | 'bind_port': int(cell['zmq_ue_peer_port']), |
| 101 | 'peer_addr': enb.ue.addr(), |
| 102 | 'peer_port': int(cell['zmq_ue_bind_port']), |
| 103 | 'use_mimo': True if enb.num_ports() > 1 else False |
| 104 | } |
| 105 | res.append(data) |
| 106 | return res |
| 107 | |
| 108 | def gen_json(self): |
| 109 | res = {'enb': [self.gen_json_enb(enb) for enb in self.enb_li], |
| 110 | 'ue': [self.gen_json_ue(self.enb_li[0])]} |
| 111 | return res |
| 112 | |
| 113 | def configure(self): |
| 114 | self.addr = self.enb_li[0].addr() |
| 115 | self.testenv = self.enb_li[0].testenv |
| 116 | self.run_dir = util.Dir(self.testenv.test().get_run_dir().new_dir(self.name())) |
| 117 | if not self.enb_li[0]._run_node.is_local(): |
| 118 | self.rem_host = remote.RemoteHost(self.run_dir, self.enb_li[0]._run_node.ssh_user(), self.enb_li[0]._run_node.ssh_addr()) |
| 119 | |
| 120 | def start(self): |
| 121 | self.num_enb_started += 1 |
| 122 | self.dbg('start(%d/%d)' % (self.num_enb_started, len(self.enb_li))) |
| 123 | if self.num_enb_started == 1: |
| 124 | self.configure() |
| 125 | args = ('osmo-gsm-tester_zmq_broker.py', |
| 126 | '-c', str(self.ctrl_port), |
| 127 | '-b', self.addr) |
| 128 | if self.enb_li[0]._run_node.is_local(): |
| 129 | self.process = process.Process(self.name(), self.run_dir, args) |
| 130 | else: |
| 131 | self.process = self.rem_host.RemoteProcessSafeExit('zmq_gr_broker', util.Dir('/tmp/ogt_%s' % self.name()), args, wait_time_sec=7) |
| 132 | self.testenv.remember_to_stop(self.process) |
| 133 | self.process.launch() |
| 134 | # Wait until all ENBs are configured/started: |
| 135 | if self.num_enb_started == len(self.enb_li): |
| 136 | self.dbg('waiting for gr script to be available...') |
| 137 | MainLoop.sleep(5) |
| 138 | self.ctrl_sk = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 139 | self.cmd_setup() |
| 140 | |
| 141 | def send_cmd(self, str_buf): |
| 142 | self.dbg('sending cmd: "%s"' % str_buf) |
| 143 | self.ctrl_sk.sendto(str_buf.encode('utf-8'), (self.addr, self.ctrl_port)) |
| 144 | |
| 145 | def cmd_setup(self): |
| 146 | cfg = self.gen_json() |
| 147 | buf = json.dumps(cfg) |
| 148 | self.send_cmd(buf) |
| 149 | |
| 150 | def cmd_set_relative_gain_on_local_port(self, port, rel_gain): |
| 151 | d = { 'action': 'set_relative_gain', |
| 152 | 'port': port, |
| 153 | 'rel_gain': rel_gain |
| 154 | } |
| 155 | buf = json.dumps(d) |
| 156 | self.send_cmd(buf) |
| 157 | |
| 158 | def cmd_exit(self): |
| 159 | d = { 'action': 'exit' } |
| 160 | buf = json.dumps(d) |
| 161 | self.send_cmd(buf) |