blob: c460e98d9e5a2b5394939f9b9a107ba23f39bcc3 [file] [log] [blame]
Thomas Tsou85b179d2013-11-15 21:14:33 -05001/*
2 * Copyright (C) 2013 Thomas Tsou <tom@tsou.cc>
3 *
Pau Espin Pedrol21d03d32019-07-22 12:05:52 +02004 * SPDX-License-Identifier: LGPL-2.1+
5 *
Thomas Tsou85b179d2013-11-15 21:14:33 -05006 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
Thomas Tsou85b179d2013-11-15 21:14:33 -050015 */
16
17#ifdef HAVE_CONFIG_H
18#include "config.h"
19#endif
20
21#include "Transceiver.h"
22#include "radioDevice.h"
Oliver Smith8d9a05c2018-12-12 16:03:38 +010023#include "Utils.h"
Thomas Tsou85b179d2013-11-15 21:14:33 -050024
25#include <time.h>
26#include <signal.h>
27#include <stdlib.h>
28#include <unistd.h>
Oliver Smitha439fed2018-10-23 13:12:17 +020029#include <getopt.h>
Harald Welte81486e02017-06-29 15:35:22 +020030#include <sched.h>
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +010031#include <vector>
32#include <string>
33#include <sstream>
34#include <iostream>
Pau Espin Pedrol21032b72019-03-29 19:20:06 +010035#include <sys/signalfd.h>
Thomas Tsou85b179d2013-11-15 21:14:33 -050036
37#include <GSMCommon.h>
38#include <Logger.h>
Thomas Tsou85b179d2013-11-15 21:14:33 -050039
Philipp Maier7e07cf22017-03-15 18:09:35 +010040extern "C" {
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +010041#include <osmocom/core/talloc.h>
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +010042#include <osmocom/core/application.h>
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +010043#include <osmocom/core/msgb.h>
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +010044#include <osmocom/core/stats.h>
45#include <osmocom/vty/logging.h>
46#include <osmocom/vty/ports.h>
47#include <osmocom/vty/misc.h>
48#include <osmocom/vty/telnet_interface.h>
49#include <osmocom/ctrl/control_vty.h>
50#include <osmocom/ctrl/ports.h>
51#include <osmocom/ctrl/control_if.h>
52#include <osmocom/vty/stats.h>
Pau Espin Pedrol16e7e202018-06-15 15:19:21 +020053#include <osmocom/vty/command.h>
Pau Espin Pedrol553a2502020-07-29 18:05:25 +020054#include <osmocom/vty/cpu_sched_vty.h>
Pau Espin Pedrol16e7e202018-06-15 15:19:21 +020055
Philipp Maier7e07cf22017-03-15 18:09:35 +010056#include "convolve.h"
57#include "convert.h"
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +010058#include "trx_vty.h"
59#include "debug.h"
Pau Espin Pedroldb936b92018-09-03 16:50:49 +020060#include "osmo_signal.h"
Pau Espin Pedrol4456b6f2019-05-24 16:54:19 +020061#include "trx_rate_ctr.h"
Philipp Maier7e07cf22017-03-15 18:09:35 +010062}
63
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +010064#define DEFAULT_CONFIG_FILE "osmo-trx.cfg"
Thomas Tsou85b179d2013-11-15 21:14:33 -050065
Pau Espin Pedrol408f2502018-02-21 18:47:35 +010066#define charp2str(a) ((a) ? std::string(a) : std::string(""))
67
68static char* config_file = (char*)DEFAULT_CONFIG_FILE;
Thomas Tsou85b179d2013-11-15 21:14:33 -050069
Pau Espin Pedrol21032b72019-03-29 19:20:06 +010070struct osmo_fd signal_ofd;
Thomas Tsou85b179d2013-11-15 21:14:33 -050071volatile bool gshutdown = false;
72
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +010073static void *tall_trx_ctx;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +010074static struct trx_ctx *g_trx_ctx;
75static struct ctrl_handle *g_ctrlh;
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +010076
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +010077static RadioDevice *usrp;
78static RadioInterface *radio;
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +010079
Ericcca5d932023-07-25 18:44:03 +020080/* adjusts read timestamp offset to make the viterbi equalizer happy by including the start tail bits */
81template <typename B>
82class rif_va_wrapper : public B {
83 bool use_va;
84
85 public:
86 template <typename... Args>
87 rif_va_wrapper(bool use_va, Args &&...args) : B(std::forward<Args>(args)...), use_va(use_va)
88 {
89 }
90 bool start() override
91 {
92 auto rv = B::start();
93 B::readTimestamp -= use_va ? 20 : 0;
94 return rv;
95 };
96};
97
Thomas Tsou85b179d2013-11-15 21:14:33 -050098/* Create radio interface
99 * The interface consists of sample rate changes, frequency shifts,
100 * channel multiplexing, and other conversions. The transceiver core
101 * accepts input vectors sampled at multiples of the GSM symbol rate.
102 * The radio interface connects the main transceiver with the device
103 * object, which may be operating some other rate.
104 */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100105RadioInterface *makeRadioInterface(struct trx_ctx *trx,
Thomas Tsou85b179d2013-11-15 21:14:33 -0500106 RadioDevice *usrp, int type)
107{
108 RadioInterface *radio = NULL;
109
110 switch (type) {
111 case RadioDevice::NORMAL:
Ericcca5d932023-07-25 18:44:03 +0200112 radio = new rif_va_wrapper<RadioInterface>(trx->cfg.use_va, usrp, trx->cfg.tx_sps, trx->cfg.rx_sps,
113 trx->cfg.num_chans);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500114 break;
115 case RadioDevice::RESAMP_64M:
116 case RadioDevice::RESAMP_100M:
Ericcca5d932023-07-25 18:44:03 +0200117 radio = new rif_va_wrapper<RadioInterfaceResamp>(trx->cfg.use_va, usrp, trx->cfg.tx_sps,
118 trx->cfg.rx_sps);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500119 break;
Tom Tsou76764272016-06-24 14:25:39 -0700120 case RadioDevice::MULTI_ARFCN:
Ericcca5d932023-07-25 18:44:03 +0200121 radio = new rif_va_wrapper<RadioInterfaceMulti>(trx->cfg.use_va, usrp, trx->cfg.tx_sps, trx->cfg.rx_sps,
122 trx->cfg.num_chans);
Tom Tsou76764272016-06-24 14:25:39 -0700123 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500124 default:
125 LOG(ALERT) << "Unsupported radio interface configuration";
126 return NULL;
127 }
128
129 if (!radio->init(type)) {
130 LOG(ALERT) << "Failed to initialize radio interface";
131 return NULL;
132 }
133
134 return radio;
135}
136
Pau Espin Pedroldb936b92018-09-03 16:50:49 +0200137/* Callback function to be called every time we receive a signal from TRANSC */
138static int transc_sig_cb(unsigned int subsys, unsigned int signal,
139 void *handler_data, void *signal_data)
140{
141 switch (signal) {
Pau Espin Pedrolb426e4a2019-06-04 12:39:28 +0200142 case S_MAIN_STOP_REQUIRED:
Pau Espin Pedroldb936b92018-09-03 16:50:49 +0200143 gshutdown = true;
144 break;
145 default:
146 break;
147 }
148 return 0;
149}
150
Thomas Tsou85b179d2013-11-15 21:14:33 -0500151/* Create transceiver core
152 * The multi-threaded modem core operates at multiples of the GSM rate of
153 * 270.8333 ksps and consists of GSM specific modulation, demodulation,
154 * and decoding schemes. Also included are the socket interfaces for
155 * connecting to the upper layer stack.
156 */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100157int makeTransceiver(struct trx_ctx *trx, RadioInterface *radio)
Thomas Tsou85b179d2013-11-15 21:14:33 -0500158{
Thomas Tsou85b179d2013-11-15 21:14:33 -0500159 VectorFIFO *fifo;
160
Pau Espin Pedrol93fee1f2020-10-13 17:27:08 +0200161 transceiver = new Transceiver(&trx->cfg, GSM::Time(3,0), radio);
162 if (!transceiver->init()) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500163 LOG(ALERT) << "Failed to initialize transceiver";
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100164 return -1;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500165 }
166
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100167 for (size_t i = 0; i < trx->cfg.num_chans; i++) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500168 fifo = radio->receiveFIFO(i);
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100169 if (fifo && transceiver->receiveFIFO(fifo, i))
Thomas Tsou85b179d2013-11-15 21:14:33 -0500170 continue;
171
172 LOG(ALERT) << "Could not attach FIFO to channel " << i;
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100173 return -1;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500174 }
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100175 return 0;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500176}
177
178static void sig_handler(int signo)
179{
Pau Espin Pedrold01c7b92019-03-29 18:36:30 +0100180
181 if (gshutdown)
182 /* We are in the middle of shutdown process, avoid any kind of extra
183 action like printing */
184 return;
185
Pau Espin Pedrol3eed8eb2019-08-14 16:26:45 +0200186 fprintf(stderr, "signal %d received\n", signo);
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100187 switch (signo) {
188 case SIGINT:
189 case SIGTERM:
Pau Espin Pedrol3eed8eb2019-08-14 16:26:45 +0200190 fprintf(stderr, "shutting down\n");
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100191 gshutdown = true;
192 break;
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +0100193 case SIGABRT:
Pau Espin Pedrol57db77f2020-11-25 17:50:21 +0100194 /* in case of abort, we want to obtain a talloc report and
195 * then run default SIGABRT handler, who will generate coredump
196 * and abort the process. abort() should do this for us after we
197 * return, but program wouldn't exit if an external SIGABRT is
198 * received.
199 */
200 talloc_report(tall_trx_ctx, stderr);
201 talloc_report_full(tall_trx_ctx, stderr);
202 signal(SIGABRT, SIG_DFL);
203 raise(SIGABRT);
204 break;
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +0100205 case SIGUSR1:
206 talloc_report(tall_trx_ctx, stderr);
207 talloc_report_full(tall_trx_ctx, stderr);
208 break;
209 case SIGUSR2:
210 talloc_report_full(tall_trx_ctx, stderr);
211 break;
Pau Espin Pedrol21032b72019-03-29 19:20:06 +0100212 case SIGHUP:
213 log_targets_reopen();
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100214 default:
215 break;
216 }
Pau Espin Pedrol21032b72019-03-29 19:20:06 +0100217
218}
219
220static int signalfd_callback(struct osmo_fd *ofd, unsigned int what)
221{
222 struct signalfd_siginfo fdsi;
223 ssize_t s;
224
225 s = read(ofd->fd, &fdsi, sizeof(struct signalfd_siginfo));
226 if (s < 0) {
227 LOG(FATAL) << "Failed to read from signalfd ("<< ofd->fd << "): " << errno;
228 gshutdown = true;
229 return 0;
230 }
231 sig_handler(fdsi.ssi_signo);
232 return 0;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500233}
234
235static void setup_signal_handlers()
236{
Pau Espin Pedrol21032b72019-03-29 19:20:06 +0100237 sigset_t set;
238 int sfd;
239
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100240 signal(SIGABRT, &sig_handler);
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100241 osmo_init_ignore_signals();
Pau Espin Pedrol21032b72019-03-29 19:20:06 +0100242
243 /* Other threads created by this thread (main) will inherit a copy of the
244 signal mask. */
245 sigemptyset(&set);
246 sigaddset(&set, SIGINT);
247 sigaddset(&set, SIGTERM);
248 sigaddset(&set, SIGUSR1);
249 sigaddset(&set, SIGUSR2);
250 sigaddset(&set, SIGHUP);
251 if (pthread_sigmask(SIG_BLOCK, &set, NULL)) {
252 fprintf(stderr, "pthread_sigmask() failed.\n");
253 exit(EXIT_FAILURE);
254 }
255
256 if ((sfd = signalfd(-1, &set, 0)) == -1) {
257 fprintf(stderr, "signalfd() failed (%d).\n", errno);
258 exit(EXIT_FAILURE);
259 }
260
Pau Espin Pedrol4a575b02020-05-09 18:54:17 +0200261 osmo_fd_setup(&signal_ofd, sfd, OSMO_FD_READ, signalfd_callback, NULL, 0);
Pau Espin Pedrol5e6f3e02019-04-02 11:45:04 +0200262 if (osmo_fd_register(&signal_ofd) < 0) {
263 fprintf(stderr, "osmo_fd_register() failed.\n");
264 exit(EXIT_FAILURE);
265 }
Thomas Tsou85b179d2013-11-15 21:14:33 -0500266}
267
268static void print_help()
269{
Vadim Yanitskiy6be2d152020-10-24 05:32:26 +0700270 printf( "Some useful options:\n"
271 " -h, --help This text\n"
272 " -C, --config Filename The config file to use\n"
273 " -V, --version Print the version of OsmoTRX\n"
274 "\nVTY reference generation:\n"
275 " --vty-ref-mode MODE VTY reference generation mode (e.g. 'expert').\n"
276 " --vty-ref-xml Generate the VTY reference XML output and exit.\n"
Pau Espin Pedrol3da1f832018-02-20 20:01:10 +0100277 );
Thomas Tsou85b179d2013-11-15 21:14:33 -0500278}
279
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100280static void print_deprecated(char opt)
281{
282 LOG(WARNING) << "Cmd line option '" << opt << "' is deprecated and will be soon removed."
283 << " Please use VTY cfg option instead."
Ruben Undheim252564b2018-07-20 22:03:39 +0200284 << " All cmd line options are already being overridden by VTY options if set.";
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100285}
286
Vadim Yanitskiy6be2d152020-10-24 05:32:26 +0700287static void handle_long_options(const char *prog_name, const int long_option)
288{
289 static int vty_ref_mode = VTY_REF_GEN_MODE_DEFAULT;
290
291 switch (long_option) {
292 case 1:
293 vty_ref_mode = get_string_value(vty_ref_gen_mode_names, optarg);
294 if (vty_ref_mode < 0) {
295 fprintf(stderr, "%s: Unknown VTY reference generation "
296 "mode '%s'\n", prog_name, optarg);
297 exit(2);
298 }
299 break;
300 case 2:
301 fprintf(stderr, "Generating the VTY reference in mode '%s' (%s)\n",
302 get_value_string(vty_ref_gen_mode_names, vty_ref_mode),
303 get_value_string(vty_ref_gen_mode_desc, vty_ref_mode));
304 vty_dump_xml_ref_mode(stdout, (enum vty_ref_gen_mode) vty_ref_mode);
305 exit(0);
306 default:
307 fprintf(stderr, "%s: error parsing cmdline options\n", prog_name);
308 exit(2);
309 }
310}
311
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100312static void handle_options(int argc, char **argv, struct trx_ctx* trx)
Thomas Tsou85b179d2013-11-15 21:14:33 -0500313{
314 int option;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100315 unsigned int i;
316 std::vector<std::string> rx_paths, tx_paths;
317 bool rx_paths_set = false, tx_paths_set = false;
Philipp Maier76795402020-10-09 20:47:12 +0200318 static int long_option = 0;
Oliver Smitha439fed2018-10-23 13:12:17 +0200319 static struct option long_options[] = {
320 {"help", 0, 0, 'h'},
321 {"config", 1, 0, 'C'},
322 {"version", 0, 0, 'V'},
Vadim Yanitskiy6be2d152020-10-24 05:32:26 +0700323 {"vty-ref-mode", 1, &long_option, 1},
324 {"vty-ref-xml", 0, &long_option, 2},
Oliver Smitha439fed2018-10-23 13:12:17 +0200325 {NULL, 0, 0, 0}
326 };
Thomas Tsou85b179d2013-11-15 21:14:33 -0500327
Oliver Smitha439fed2018-10-23 13:12:17 +0200328 while ((option = getopt_long(argc, argv, "ha:l:i:j:p:c:dmxgfo:s:b:r:A:R:Set:y:z:C:V", long_options,
329 NULL)) != -1) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500330 switch (option) {
331 case 'h':
332 print_help();
333 exit(0);
334 break;
Philipp Maier76795402020-10-09 20:47:12 +0200335 case 0:
Vadim Yanitskiy6be2d152020-10-24 05:32:26 +0700336 handle_long_options(argv[0], long_option);
337 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500338 case 'a':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100339 print_deprecated(option);
340 osmo_talloc_replace_string(trx, &trx->cfg.dev_args, optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500341 break;
Pau Espin Pedrol8dffadb2018-03-06 18:38:22 +0100342 case 'l':
343 print_deprecated(option);
344 log_set_log_level(osmo_stderr_target, atoi(optarg));
345 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500346 case 'i':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100347 print_deprecated(option);
348 osmo_talloc_replace_string(trx, &trx->cfg.remote_addr, optarg);
Pau Espin Pedrol8c800952017-08-16 16:53:23 +0200349 break;
350 case 'j':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100351 print_deprecated(option);
352 osmo_talloc_replace_string(trx, &trx->cfg.bind_addr, optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500353 break;
354 case 'p':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100355 print_deprecated(option);
356 trx->cfg.base_port = atoi(optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500357 break;
358 case 'c':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100359 print_deprecated(option);
360 trx->cfg.num_chans = atoi(optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500361 break;
Tom Tsou76764272016-06-24 14:25:39 -0700362 case 'm':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100363 print_deprecated(option);
364 trx->cfg.multi_arfcn = true;
Tom Tsou76764272016-06-24 14:25:39 -0700365 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500366 case 'x':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100367 print_deprecated(option);
368 trx->cfg.clock_ref = REF_EXTERNAL;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500369 break;
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700370 case 'g':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100371 print_deprecated(option);
372 trx->cfg.clock_ref = REF_GPS;
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700373 break;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500374 case 'f':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100375 print_deprecated(option);
376 trx->cfg.filler = FILLER_DUMMY;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500377 break;
Thomas Tsou8e17df72014-03-06 14:16:11 -0500378 case 'o':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100379 print_deprecated(option);
380 trx->cfg.offset = atof(optarg);
Thomas Tsou8e17df72014-03-06 14:16:11 -0500381 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500382 case 's':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100383 print_deprecated(option);
384 trx->cfg.tx_sps = atoi(optarg);
Tom Tsou64ad7122015-05-19 18:26:31 -0700385 break;
Tom Tsou2e4ed102016-06-27 15:39:16 -0700386 case 'b':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100387 print_deprecated(option);
388 trx->cfg.rx_sps = atoi(optarg);
Tom Tsou2e4ed102016-06-27 15:39:16 -0700389 break;
Tom Tsou64ad7122015-05-19 18:26:31 -0700390 case 'r':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100391 print_deprecated(option);
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100392 trx->cfg.rtsc = atoi(optarg);
393 if (!trx->cfg.egprs) /* Don't override egprs which sets different filler */
394 trx->cfg.filler = FILLER_NORM_RAND;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500395 break;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300396 case 'A':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100397 print_deprecated(option);
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100398 trx->cfg.rach_delay = atoi(optarg);
399 trx->cfg.filler = FILLER_ACCESS_RAND;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300400 break;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400401 case 'R':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100402 print_deprecated(option);
403 trx->cfg.rssi_offset = atof(optarg);
Pau Espin Pedrole91544d2020-10-13 17:03:37 +0200404 trx->cfg.force_rssi_offset = true;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400405 break;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400406 case 'S':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100407 print_deprecated(option);
408 trx->cfg.swap_channels = true;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400409 break;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800410 case 'e':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100411 print_deprecated(option);
412 trx->cfg.egprs = true;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800413 break;
Harald Welte81486e02017-06-29 15:35:22 +0200414 case 't':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100415 print_deprecated(option);
416 trx->cfg.sched_rr = atoi(optarg);
Harald Welte81486e02017-06-29 15:35:22 +0200417 break;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100418 case 'y':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100419 print_deprecated(option);
420 tx_paths = comma_delimited_to_vector(optarg);
421 tx_paths_set = true;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100422 break;
423 case 'z':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100424 print_deprecated(option);
425 rx_paths = comma_delimited_to_vector(optarg);
426 rx_paths_set = true;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100427 break;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100428 case 'C':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100429 config_file = optarg;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100430 break;
Pau Espin Pedrol16e7e202018-06-15 15:19:21 +0200431 case 'V':
432 print_version(1);
433 exit(0);
434 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500435 default:
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100436 goto bad_config;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500437 }
438 }
Tom Tsou64ad7122015-05-19 18:26:31 -0700439
Harald Welte501d0532019-12-03 21:41:13 +0100440 if (argc > optind) {
441 LOG(ERROR) << "Unsupported positional arguments on command line";
442 goto bad_config;
443 }
444
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100445 /* Cmd line option specific validation & setup */
Tom Tsou2e4ed102016-06-27 15:39:16 -0700446
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100447 if (trx->cfg.num_chans > TRX_CHAN_MAX) {
448 LOG(ERROR) << "Too many channels requested, maximum is " << TRX_CHAN_MAX;
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700449 goto bad_config;
450 }
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100451 if ((tx_paths_set && tx_paths.size() != trx->cfg.num_chans) ||
452 (rx_paths_set && rx_paths.size() != trx->cfg.num_chans)) {
453 LOG(ERROR) << "Num of channels and num of Rx/Tx Antennas doesn't match";
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700454 goto bad_config;
Tom Tsou64ad7122015-05-19 18:26:31 -0700455 }
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100456 for (i = 0; i < trx->cfg.num_chans; i++) {
457 trx->cfg.chans[i].trx = trx;
458 trx->cfg.chans[i].idx = i;
459 if (tx_paths_set)
460 osmo_talloc_replace_string(trx, &trx->cfg.chans[i].tx_path, tx_paths[i].c_str());
461 if (rx_paths_set)
462 osmo_talloc_replace_string(trx, &trx->cfg.chans[i].rx_path, rx_paths[i].c_str());
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100463 }
464
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700465 return;
466
467bad_config:
468 print_help();
469 exit(0);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500470}
471
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100472int trx_validate_config(struct trx_ctx *trx)
473{
Tom Tsoud2800452019-04-01 07:55:48 +0700474 if (trx->cfg.multi_arfcn && trx->cfg.num_chans > TRX_MCHAN_MAX) {
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100475 LOG(ERROR) << "Unsupported number of channels";
476 return -1;
477 }
478
479 /* Force 4 SPS for EDGE or multi-ARFCN configurations */
480 if ((trx->cfg.egprs || trx->cfg.multi_arfcn) &&
Harald Welte8fb0c3d2018-10-21 12:15:30 +0200481 (trx->cfg.tx_sps!=4 || trx->cfg.rx_sps!=4)) {
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100482 LOG(ERROR) << "EDGE and Multi-Carrier options require 4 tx and rx sps. Check you config.";
483 return -1;
484 }
485
Ericcca5d932023-07-25 18:44:03 +0200486 if (trx->cfg.use_va &&
487 (trx->cfg.egprs || trx->cfg.multi_arfcn || trx->cfg.tx_sps != 4 || trx->cfg.rx_sps != 4)) {
488 LOG(ERROR) << "Viterbi equalizer only works for gmsk with 4 tx/rx samples per symbol!";
489 return -1;
490 }
491
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100492 return 0;
493}
494
495static int set_sched_rr(unsigned int prio)
Harald Welte81486e02017-06-29 15:35:22 +0200496{
497 struct sched_param param;
498 int rc;
499 memset(&param, 0, sizeof(param));
500 param.sched_priority = prio;
Pau Espin Pedrol553a2502020-07-29 18:05:25 +0200501 LOG(INFO) << "Setting SCHED_RR priority " << param.sched_priority
502 << ". This setting is DEPRECATED, please use 'policy rr " << param.sched_priority
503 << "' under the 'sched' VTY node instead.";
Harald Welte81486e02017-06-29 15:35:22 +0200504 rc = sched_setscheduler(getpid(), SCHED_RR, &param);
505 if (rc != 0) {
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100506 LOG(ERROR) << "Config: Setting SCHED_RR failed";
Harald Welte81486e02017-06-29 15:35:22 +0200507 return -1;
508 }
509 return 0;
510}
511
Vadim Yanitskiy744e44e2020-10-29 16:49:08 +0700512static void print_simd_info(void)
513{
514#ifdef HAVE_SSE3
515 LOGP(DMAIN, LOGL_INFO, "SSE3 support compiled in");
516#ifdef HAVE___BUILTIN_CPU_SUPPORTS
517 if (__builtin_cpu_supports("sse3"))
518 LOGPC(DMAIN, LOGL_INFO, " and supported by CPU\n");
519 else
520 LOGPC(DMAIN, LOGL_INFO, ", but not supported by CPU\n");
521#else
522 LOGPC(DMAIN, LOGL_INFO, ", but runtime SIMD detection disabled\n");
523#endif
524#endif
525
526#ifdef HAVE_SSE4_1
527 LOGP(DMAIN, LOGL_INFO, "SSE4.1 support compiled in");
528#ifdef HAVE___BUILTIN_CPU_SUPPORTS
529 if (__builtin_cpu_supports("sse4.1"))
530 LOGPC(DMAIN, LOGL_INFO, " and supported by CPU\n");
531 else
532 LOGPC(DMAIN, LOGL_INFO, ", but not supported by CPU\n");
533#else
534 LOGPC(DMAIN, LOGL_INFO, ", but runtime SIMD detection disabled\n");
535#endif
536#endif
537
538#ifndef HAVE_ATOMIC_OPS
539#pragma message ("Built without atomic operation support. Using Mutex, it may affect performance!")
540 LOG(NOTICE) << "Built without atomic operation support. Using Mutex, it may affect performance!";
541#endif
542}
543
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100544static void print_config(struct trx_ctx *trx)
545{
546 unsigned int i;
547 std::ostringstream ost("");
548
549 ost << "Config Settings" << std::endl;
Pau Espin Pedrol8dffadb2018-03-06 18:38:22 +0100550 ost << " Log Level............... " << (unsigned int) osmo_stderr_target->loglevel << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100551 ost << " Device args............. " << charp2str(trx->cfg.dev_args) << std::endl;
552 ost << " TRX Base Port........... " << trx->cfg.base_port << std::endl;
553 ost << " TRX Address............. " << charp2str(trx->cfg.bind_addr) << std::endl;
Harald Weltefad2e092018-04-28 21:25:09 +0200554 ost << " GSM BTS Address......... " << charp2str(trx->cfg.remote_addr) << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100555 ost << " Channels................ " << trx->cfg.num_chans << std::endl;
556 ost << " Tx Samples-per-Symbol... " << trx->cfg.tx_sps << std::endl;
557 ost << " Rx Samples-per-Symbol... " << trx->cfg.rx_sps << std::endl;
558 ost << " EDGE support............ " << trx->cfg.egprs << std::endl;
Vadim Yanitskiya8b35652018-10-22 02:52:18 +0200559 ost << " Extended RACH support... " << trx->cfg.ext_rach << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100560 ost << " Reference............... " << trx->cfg.clock_ref << std::endl;
Alexander Chemeris9a87d902019-10-15 00:33:07 +0300561 ost << " Filler Burst Type....... " << get_value_string(filler_names, trx->cfg.filler) << std::endl;
562 ost << " Filler Burst TSC........ " << trx->cfg.rtsc << std::endl;
563 ost << " Filler Burst RACH Delay. " << trx->cfg.rach_delay << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100564 ost << " Multi-Carrier........... " << trx->cfg.multi_arfcn << std::endl;
Vadim Yanitskiy3b8f7c42018-08-23 13:41:06 +0700565 ost << " LO freq. offset......... " << trx->cfg.offset << std::endl;
566 if (trx->cfg.freq_offset_khz != 0)
567 ost << " Tune freq. offset....... " << trx->cfg.freq_offset_khz << std::endl;
Pau Espin Pedrole91544d2020-10-13 17:03:37 +0200568 ost << " RSSI to dBm offset...... " << trx->cfg.rssi_offset << (trx->cfg.force_rssi_offset ? "" : " (relative)") << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100569 ost << " Swap channels........... " << trx->cfg.swap_channels << std::endl;
570 ost << " Tx Antennas.............";
571 for (i = 0; i < trx->cfg.num_chans; i++) {
572 std::string p = charp2str(trx->cfg.chans[i].tx_path);
573 ost << " '" << ((p != "") ? p : "<default>") << "'";
574 }
575 ost << std::endl;
576 ost << " Rx Antennas.............";
577 for (i = 0; i < trx->cfg.num_chans; i++) {
578 std::string p = charp2str(trx->cfg.chans[i].rx_path);
579 ost << " '" << ((p != "") ? p : "<default>") << "'";
580 }
581 ost << std::endl;
582
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100583 LOG(INFO) << ost << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100584}
585
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100586static void trx_stop()
587{
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100588 LOG(NOTICE) << "Shutting down transceiver..." << std::endl;
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100589
590 delete transceiver;
591 delete radio;
592 delete usrp;
593}
594
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100595static int trx_start(struct trx_ctx *trx)
Thomas Tsou85b179d2013-11-15 21:14:33 -0500596{
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100597 int type, chans;
Tom Tsou05c6feb2016-06-22 16:09:44 -0700598 RadioDevice::InterfaceType iface = RadioDevice::NORMAL;
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100599
600 /* Create the low level device object */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100601 if (trx->cfg.multi_arfcn)
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100602 iface = RadioDevice::MULTI_ARFCN;
603
Eric19e134a2023-05-10 23:50:38 +0200604 usrp = RadioDevice::make(iface, &trx->cfg);
605 type = usrp->open();
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100606 if (type < 0) {
607 LOG(ALERT) << "Failed to create radio device" << std::endl;
608 goto shutdown;
609 }
610
611 /* Setup the appropriate device interface */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100612 radio = makeRadioInterface(trx, usrp, type);
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100613 if (!radio)
614 goto shutdown;
615
616 /* Create the transceiver core */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100617 if (makeTransceiver(trx, radio) < 0)
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100618 goto shutdown;
619
620 chans = transceiver->numChans();
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100621 LOG(NOTICE) << "-- Transceiver active with "
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100622 << chans << " channel(s)" << std::endl;
623
624 return 0;
625
626shutdown:
627 trx_stop();
628 return -1;
629}
630
631int main(int argc, char *argv[])
632{
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100633 int rc;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500634
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +0100635 tall_trx_ctx = talloc_named_const(NULL, 0, "OsmoTRX");
636 msgb_talloc_ctx_init(tall_trx_ctx, 0);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100637 g_vty_info.tall_ctx = tall_trx_ctx;
638
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100639 setup_signal_handlers();
640
Pau Espin Pedrola3ab8c22018-02-21 15:41:03 +0100641 g_trx_ctx = vty_trx_ctx_alloc(tall_trx_ctx);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100642
Philipp Maier7e07cf22017-03-15 18:09:35 +0100643 convolve_init();
644 convert_init();
645
Pau Espin Pedrole1977fc2018-04-16 14:50:11 +0200646 osmo_init_logging2(tall_trx_ctx, &log_info);
Pau Espin Pedrolb7e99272019-09-17 20:26:53 +0200647 log_enable_multithread();
Eric Wild75cf9252024-03-22 11:20:54 +0100648 log_cache_enable();
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100649 osmo_stats_init(tall_trx_ctx);
650 vty_init(&g_vty_info);
Pau Espin Pedrolaebbfe02020-01-02 16:39:11 +0100651 logging_vty_add_cmds();
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100652 ctrl_vty_init(tall_trx_ctx);
Pau Espin Pedrol553a2502020-07-29 18:05:25 +0200653 osmo_cpu_sched_vty_init(tall_trx_ctx);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100654 trx_vty_init(g_trx_ctx);
655
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100656 osmo_talloc_vty_add_cmds();
657 osmo_stats_vty_add_cmds();
658
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100659 handle_options(argc, argv, g_trx_ctx);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500660
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100661 rate_ctr_init(tall_trx_ctx);
662
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100663 rc = vty_read_config_file(config_file, NULL);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100664 if (rc < 0) {
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100665 fprintf(stderr, "Failed to open config file: '%s'\n", config_file);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100666 exit(2);
667 }
668
arehbein20ecc4f2023-02-25 17:48:58 +0100669 rc = telnet_init_default(tall_trx_ctx, NULL, OSMO_VTY_PORT_TRX);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100670 if (rc < 0)
671 exit(1);
672
Max934e1012022-12-17 21:26:49 +0300673 g_ctrlh = ctrl_interface_setup(NULL, OSMO_CTRL_PORT_TRX, NULL);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100674 if (!g_ctrlh) {
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100675 LOG(ERROR) << "Failed to create CTRL interface.\n";
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100676 exit(1);
677 }
678
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100679 /* Backward compatibility: Hack to have 1 channel allocated by default.
680 * Can be Dropped once we * get rid of "-c" cmdline param */
681 if (g_trx_ctx->cfg.num_chans == 0) {
682 g_trx_ctx->cfg.num_chans = 1;
683 g_trx_ctx->cfg.chans[0].trx = g_trx_ctx;
684 g_trx_ctx->cfg.chans[0].idx = 0;
685 LOG(ERROR) << "No explicit channel config found. Make sure you" \
686 " configure channels in VTY config. Using 1 channel as default," \
687 " but expect your config to break in the future.";
Harald Welte81486e02017-06-29 15:35:22 +0200688 }
689
Vadim Yanitskiy744e44e2020-10-29 16:49:08 +0700690 print_simd_info();
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100691 print_config(g_trx_ctx);
692
693 if (trx_validate_config(g_trx_ctx) < 0) {
694 LOG(ERROR) << "Config failure - exiting";
Thomas Tsou85b179d2013-11-15 21:14:33 -0500695 return EXIT_FAILURE;
696 }
697
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100698 if (g_trx_ctx->cfg.sched_rr) {
699 if (set_sched_rr(g_trx_ctx->cfg.sched_rr) < 0)
700 return EXIT_FAILURE;
701 }
702
Pau Espin Pedrolb426e4a2019-06-04 12:39:28 +0200703 osmo_signal_register_handler(SS_MAIN, transc_sig_cb, NULL);
Pau Espin Pedrol4456b6f2019-05-24 16:54:19 +0200704 trx_rate_ctr_init(tall_trx_ctx, g_trx_ctx);
705
Thomas Tsou85b179d2013-11-15 21:14:33 -0500706 srandom(time(NULL));
707
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100708 if(trx_start(g_trx_ctx) < 0)
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100709 return EXIT_FAILURE;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500710
711 while (!gshutdown)
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100712 osmo_select_main(0);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500713
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100714 trx_stop();
Thomas Tsou85b179d2013-11-15 21:14:33 -0500715
Pau Espin Pedrol21032b72019-03-29 19:20:06 +0100716 osmo_fd_unregister(&signal_ofd);
717 osmo_fd_close(&signal_ofd);
Pau Espin Pedrolb426e4a2019-06-04 12:39:28 +0200718 osmo_signal_unregister_handler(SS_MAIN, transc_sig_cb, NULL);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500719 return 0;
720}