blob: b227863858e6a44249bc33073961b597e4a311f4 [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.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#ifdef HAVE_CONFIG_H
22#include "config.h"
23#endif
24
25#include "Transceiver.h"
26#include "radioDevice.h"
Oliver Smith8d9a05c2018-12-12 16:03:38 +010027#include "Utils.h"
Thomas Tsou85b179d2013-11-15 21:14:33 -050028
29#include <time.h>
30#include <signal.h>
31#include <stdlib.h>
32#include <unistd.h>
Oliver Smitha439fed2018-10-23 13:12:17 +020033#include <getopt.h>
Harald Welte81486e02017-06-29 15:35:22 +020034#include <sched.h>
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +010035#include <vector>
36#include <string>
37#include <sstream>
38#include <iostream>
Pau Espin Pedrol21032b72019-03-29 19:20:06 +010039#include <sys/signalfd.h>
Thomas Tsou85b179d2013-11-15 21:14:33 -050040
41#include <GSMCommon.h>
42#include <Logger.h>
Thomas Tsou85b179d2013-11-15 21:14:33 -050043
Philipp Maier7e07cf22017-03-15 18:09:35 +010044extern "C" {
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +010045#include <osmocom/core/talloc.h>
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +010046#include <osmocom/core/application.h>
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +010047#include <osmocom/core/msgb.h>
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +010048#include <osmocom/core/stats.h>
49#include <osmocom/vty/logging.h>
50#include <osmocom/vty/ports.h>
51#include <osmocom/vty/misc.h>
52#include <osmocom/vty/telnet_interface.h>
53#include <osmocom/ctrl/control_vty.h>
54#include <osmocom/ctrl/ports.h>
55#include <osmocom/ctrl/control_if.h>
56#include <osmocom/vty/stats.h>
Pau Espin Pedrol16e7e202018-06-15 15:19:21 +020057#include <osmocom/vty/command.h>
Pau Espin Pedrol553a2502020-07-29 18:05:25 +020058#include <osmocom/vty/cpu_sched_vty.h>
Pau Espin Pedrol16e7e202018-06-15 15:19:21 +020059
Philipp Maier7e07cf22017-03-15 18:09:35 +010060#include "convolve.h"
61#include "convert.h"
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +010062#include "trx_vty.h"
63#include "debug.h"
Pau Espin Pedroldb936b92018-09-03 16:50:49 +020064#include "osmo_signal.h"
Pau Espin Pedrol4456b6f2019-05-24 16:54:19 +020065#include "trx_rate_ctr.h"
Philipp Maier7e07cf22017-03-15 18:09:35 +010066}
67
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +010068#define DEFAULT_CONFIG_FILE "osmo-trx.cfg"
Thomas Tsou85b179d2013-11-15 21:14:33 -050069
Pau Espin Pedrol408f2502018-02-21 18:47:35 +010070#define charp2str(a) ((a) ? std::string(a) : std::string(""))
71
72static char* config_file = (char*)DEFAULT_CONFIG_FILE;
Thomas Tsou85b179d2013-11-15 21:14:33 -050073
Pau Espin Pedrol21032b72019-03-29 19:20:06 +010074struct osmo_fd signal_ofd;
Thomas Tsou85b179d2013-11-15 21:14:33 -050075volatile bool gshutdown = false;
76
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +010077static void *tall_trx_ctx;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +010078static struct trx_ctx *g_trx_ctx;
79static struct ctrl_handle *g_ctrlh;
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +010080
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +010081static RadioDevice *usrp;
82static RadioInterface *radio;
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +010083
Thomas Tsou85b179d2013-11-15 21:14:33 -050084/* Create radio interface
85 * The interface consists of sample rate changes, frequency shifts,
86 * channel multiplexing, and other conversions. The transceiver core
87 * accepts input vectors sampled at multiples of the GSM symbol rate.
88 * The radio interface connects the main transceiver with the device
89 * object, which may be operating some other rate.
90 */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +010091RadioInterface *makeRadioInterface(struct trx_ctx *trx,
Thomas Tsou85b179d2013-11-15 21:14:33 -050092 RadioDevice *usrp, int type)
93{
94 RadioInterface *radio = NULL;
95
96 switch (type) {
97 case RadioDevice::NORMAL:
Pau Espin Pedrol408f2502018-02-21 18:47:35 +010098 radio = new RadioInterface(usrp, trx->cfg.tx_sps,
99 trx->cfg.rx_sps, trx->cfg.num_chans);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500100 break;
101 case RadioDevice::RESAMP_64M:
102 case RadioDevice::RESAMP_100M:
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100103 radio = new RadioInterfaceResamp(usrp, trx->cfg.tx_sps,
104 trx->cfg.rx_sps);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500105 break;
Tom Tsou76764272016-06-24 14:25:39 -0700106 case RadioDevice::MULTI_ARFCN:
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100107 radio = new RadioInterfaceMulti(usrp, trx->cfg.tx_sps,
108 trx->cfg.rx_sps, trx->cfg.num_chans);
Tom Tsou76764272016-06-24 14:25:39 -0700109 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500110 default:
111 LOG(ALERT) << "Unsupported radio interface configuration";
112 return NULL;
113 }
114
115 if (!radio->init(type)) {
116 LOG(ALERT) << "Failed to initialize radio interface";
117 return NULL;
118 }
119
120 return radio;
121}
122
Pau Espin Pedroldb936b92018-09-03 16:50:49 +0200123/* Callback function to be called every time we receive a signal from TRANSC */
124static int transc_sig_cb(unsigned int subsys, unsigned int signal,
125 void *handler_data, void *signal_data)
126{
127 switch (signal) {
Pau Espin Pedrolb426e4a2019-06-04 12:39:28 +0200128 case S_MAIN_STOP_REQUIRED:
Pau Espin Pedroldb936b92018-09-03 16:50:49 +0200129 gshutdown = true;
130 break;
131 default:
132 break;
133 }
134 return 0;
135}
136
Thomas Tsou85b179d2013-11-15 21:14:33 -0500137/* Create transceiver core
138 * The multi-threaded modem core operates at multiples of the GSM rate of
139 * 270.8333 ksps and consists of GSM specific modulation, demodulation,
140 * and decoding schemes. Also included are the socket interfaces for
141 * connecting to the upper layer stack.
142 */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100143int makeTransceiver(struct trx_ctx *trx, RadioInterface *radio)
Thomas Tsou85b179d2013-11-15 21:14:33 -0500144{
Thomas Tsou85b179d2013-11-15 21:14:33 -0500145 VectorFIFO *fifo;
146
Pau Espin Pedrol93fee1f2020-10-13 17:27:08 +0200147 transceiver = new Transceiver(&trx->cfg, GSM::Time(3,0), radio);
148 if (!transceiver->init()) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500149 LOG(ALERT) << "Failed to initialize transceiver";
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100150 return -1;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500151 }
152
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100153 for (size_t i = 0; i < trx->cfg.num_chans; i++) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500154 fifo = radio->receiveFIFO(i);
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100155 if (fifo && transceiver->receiveFIFO(fifo, i))
Thomas Tsou85b179d2013-11-15 21:14:33 -0500156 continue;
157
158 LOG(ALERT) << "Could not attach FIFO to channel " << i;
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100159 return -1;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500160 }
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100161 return 0;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500162}
163
164static void sig_handler(int signo)
165{
Pau Espin Pedrold01c7b92019-03-29 18:36:30 +0100166
167 if (gshutdown)
168 /* We are in the middle of shutdown process, avoid any kind of extra
169 action like printing */
170 return;
171
Pau Espin Pedrol3eed8eb2019-08-14 16:26:45 +0200172 fprintf(stderr, "signal %d received\n", signo);
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100173 switch (signo) {
174 case SIGINT:
175 case SIGTERM:
Pau Espin Pedrol3eed8eb2019-08-14 16:26:45 +0200176 fprintf(stderr, "shutting down\n");
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100177 gshutdown = true;
178 break;
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +0100179 case SIGABRT:
Pau Espin Pedrol57db77f2020-11-25 17:50:21 +0100180 /* in case of abort, we want to obtain a talloc report and
181 * then run default SIGABRT handler, who will generate coredump
182 * and abort the process. abort() should do this for us after we
183 * return, but program wouldn't exit if an external SIGABRT is
184 * received.
185 */
186 talloc_report(tall_trx_ctx, stderr);
187 talloc_report_full(tall_trx_ctx, stderr);
188 signal(SIGABRT, SIG_DFL);
189 raise(SIGABRT);
190 break;
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +0100191 case SIGUSR1:
192 talloc_report(tall_trx_ctx, stderr);
193 talloc_report_full(tall_trx_ctx, stderr);
194 break;
195 case SIGUSR2:
196 talloc_report_full(tall_trx_ctx, stderr);
197 break;
Pau Espin Pedrol21032b72019-03-29 19:20:06 +0100198 case SIGHUP:
199 log_targets_reopen();
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100200 default:
201 break;
202 }
Pau Espin Pedrol21032b72019-03-29 19:20:06 +0100203
204}
205
206static int signalfd_callback(struct osmo_fd *ofd, unsigned int what)
207{
208 struct signalfd_siginfo fdsi;
209 ssize_t s;
210
211 s = read(ofd->fd, &fdsi, sizeof(struct signalfd_siginfo));
212 if (s < 0) {
213 LOG(FATAL) << "Failed to read from signalfd ("<< ofd->fd << "): " << errno;
214 gshutdown = true;
215 return 0;
216 }
217 sig_handler(fdsi.ssi_signo);
218 return 0;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500219}
220
221static void setup_signal_handlers()
222{
Pau Espin Pedrol21032b72019-03-29 19:20:06 +0100223 sigset_t set;
224 int sfd;
225
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100226 signal(SIGABRT, &sig_handler);
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100227 osmo_init_ignore_signals();
Pau Espin Pedrol21032b72019-03-29 19:20:06 +0100228
229 /* Other threads created by this thread (main) will inherit a copy of the
230 signal mask. */
231 sigemptyset(&set);
232 sigaddset(&set, SIGINT);
233 sigaddset(&set, SIGTERM);
234 sigaddset(&set, SIGUSR1);
235 sigaddset(&set, SIGUSR2);
236 sigaddset(&set, SIGHUP);
237 if (pthread_sigmask(SIG_BLOCK, &set, NULL)) {
238 fprintf(stderr, "pthread_sigmask() failed.\n");
239 exit(EXIT_FAILURE);
240 }
241
242 if ((sfd = signalfd(-1, &set, 0)) == -1) {
243 fprintf(stderr, "signalfd() failed (%d).\n", errno);
244 exit(EXIT_FAILURE);
245 }
246
Pau Espin Pedrol4a575b02020-05-09 18:54:17 +0200247 osmo_fd_setup(&signal_ofd, sfd, OSMO_FD_READ, signalfd_callback, NULL, 0);
Pau Espin Pedrol5e6f3e02019-04-02 11:45:04 +0200248 if (osmo_fd_register(&signal_ofd) < 0) {
249 fprintf(stderr, "osmo_fd_register() failed.\n");
250 exit(EXIT_FAILURE);
251 }
Thomas Tsou85b179d2013-11-15 21:14:33 -0500252}
253
254static void print_help()
255{
Vadim Yanitskiy6be2d152020-10-24 05:32:26 +0700256 printf( "Some useful options:\n"
257 " -h, --help This text\n"
258 " -C, --config Filename The config file to use\n"
259 " -V, --version Print the version of OsmoTRX\n"
260 "\nVTY reference generation:\n"
261 " --vty-ref-mode MODE VTY reference generation mode (e.g. 'expert').\n"
262 " --vty-ref-xml Generate the VTY reference XML output and exit.\n"
Pau Espin Pedrol3da1f832018-02-20 20:01:10 +0100263 );
Thomas Tsou85b179d2013-11-15 21:14:33 -0500264}
265
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100266static void print_deprecated(char opt)
267{
268 LOG(WARNING) << "Cmd line option '" << opt << "' is deprecated and will be soon removed."
269 << " Please use VTY cfg option instead."
Ruben Undheim252564b2018-07-20 22:03:39 +0200270 << " All cmd line options are already being overridden by VTY options if set.";
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100271}
272
Vadim Yanitskiy6be2d152020-10-24 05:32:26 +0700273static void handle_long_options(const char *prog_name, const int long_option)
274{
275 static int vty_ref_mode = VTY_REF_GEN_MODE_DEFAULT;
276
277 switch (long_option) {
278 case 1:
279 vty_ref_mode = get_string_value(vty_ref_gen_mode_names, optarg);
280 if (vty_ref_mode < 0) {
281 fprintf(stderr, "%s: Unknown VTY reference generation "
282 "mode '%s'\n", prog_name, optarg);
283 exit(2);
284 }
285 break;
286 case 2:
287 fprintf(stderr, "Generating the VTY reference in mode '%s' (%s)\n",
288 get_value_string(vty_ref_gen_mode_names, vty_ref_mode),
289 get_value_string(vty_ref_gen_mode_desc, vty_ref_mode));
290 vty_dump_xml_ref_mode(stdout, (enum vty_ref_gen_mode) vty_ref_mode);
291 exit(0);
292 default:
293 fprintf(stderr, "%s: error parsing cmdline options\n", prog_name);
294 exit(2);
295 }
296}
297
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100298static void handle_options(int argc, char **argv, struct trx_ctx* trx)
Thomas Tsou85b179d2013-11-15 21:14:33 -0500299{
300 int option;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100301 unsigned int i;
302 std::vector<std::string> rx_paths, tx_paths;
303 bool rx_paths_set = false, tx_paths_set = false;
Philipp Maier76795402020-10-09 20:47:12 +0200304 static int long_option = 0;
Oliver Smitha439fed2018-10-23 13:12:17 +0200305 static struct option long_options[] = {
306 {"help", 0, 0, 'h'},
307 {"config", 1, 0, 'C'},
308 {"version", 0, 0, 'V'},
Vadim Yanitskiy6be2d152020-10-24 05:32:26 +0700309 {"vty-ref-mode", 1, &long_option, 1},
310 {"vty-ref-xml", 0, &long_option, 2},
Oliver Smitha439fed2018-10-23 13:12:17 +0200311 {NULL, 0, 0, 0}
312 };
Thomas Tsou85b179d2013-11-15 21:14:33 -0500313
Oliver Smitha439fed2018-10-23 13:12:17 +0200314 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,
315 NULL)) != -1) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500316 switch (option) {
317 case 'h':
318 print_help();
319 exit(0);
320 break;
Philipp Maier76795402020-10-09 20:47:12 +0200321 case 0:
Vadim Yanitskiy6be2d152020-10-24 05:32:26 +0700322 handle_long_options(argv[0], long_option);
323 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500324 case 'a':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100325 print_deprecated(option);
326 osmo_talloc_replace_string(trx, &trx->cfg.dev_args, optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500327 break;
Pau Espin Pedrol8dffadb2018-03-06 18:38:22 +0100328 case 'l':
329 print_deprecated(option);
330 log_set_log_level(osmo_stderr_target, atoi(optarg));
331 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500332 case 'i':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100333 print_deprecated(option);
334 osmo_talloc_replace_string(trx, &trx->cfg.remote_addr, optarg);
Pau Espin Pedrol8c800952017-08-16 16:53:23 +0200335 break;
336 case 'j':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100337 print_deprecated(option);
338 osmo_talloc_replace_string(trx, &trx->cfg.bind_addr, optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500339 break;
340 case 'p':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100341 print_deprecated(option);
342 trx->cfg.base_port = atoi(optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500343 break;
344 case 'c':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100345 print_deprecated(option);
346 trx->cfg.num_chans = atoi(optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500347 break;
Tom Tsou76764272016-06-24 14:25:39 -0700348 case 'm':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100349 print_deprecated(option);
350 trx->cfg.multi_arfcn = true;
Tom Tsou76764272016-06-24 14:25:39 -0700351 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500352 case 'x':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100353 print_deprecated(option);
354 trx->cfg.clock_ref = REF_EXTERNAL;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500355 break;
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700356 case 'g':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100357 print_deprecated(option);
358 trx->cfg.clock_ref = REF_GPS;
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700359 break;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500360 case 'f':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100361 print_deprecated(option);
362 trx->cfg.filler = FILLER_DUMMY;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500363 break;
Thomas Tsou8e17df72014-03-06 14:16:11 -0500364 case 'o':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100365 print_deprecated(option);
366 trx->cfg.offset = atof(optarg);
Thomas Tsou8e17df72014-03-06 14:16:11 -0500367 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500368 case 's':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100369 print_deprecated(option);
370 trx->cfg.tx_sps = atoi(optarg);
Tom Tsou64ad7122015-05-19 18:26:31 -0700371 break;
Tom Tsou2e4ed102016-06-27 15:39:16 -0700372 case 'b':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100373 print_deprecated(option);
374 trx->cfg.rx_sps = atoi(optarg);
Tom Tsou2e4ed102016-06-27 15:39:16 -0700375 break;
Tom Tsou64ad7122015-05-19 18:26:31 -0700376 case 'r':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100377 print_deprecated(option);
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100378 trx->cfg.rtsc = atoi(optarg);
379 if (!trx->cfg.egprs) /* Don't override egprs which sets different filler */
380 trx->cfg.filler = FILLER_NORM_RAND;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500381 break;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300382 case 'A':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100383 print_deprecated(option);
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100384 trx->cfg.rach_delay = atoi(optarg);
385 trx->cfg.filler = FILLER_ACCESS_RAND;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300386 break;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400387 case 'R':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100388 print_deprecated(option);
389 trx->cfg.rssi_offset = atof(optarg);
Pau Espin Pedrole91544d2020-10-13 17:03:37 +0200390 trx->cfg.force_rssi_offset = true;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400391 break;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400392 case 'S':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100393 print_deprecated(option);
394 trx->cfg.swap_channels = true;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400395 break;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800396 case 'e':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100397 print_deprecated(option);
398 trx->cfg.egprs = true;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800399 break;
Harald Welte81486e02017-06-29 15:35:22 +0200400 case 't':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100401 print_deprecated(option);
402 trx->cfg.sched_rr = atoi(optarg);
Harald Welte81486e02017-06-29 15:35:22 +0200403 break;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100404 case 'y':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100405 print_deprecated(option);
406 tx_paths = comma_delimited_to_vector(optarg);
407 tx_paths_set = true;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100408 break;
409 case 'z':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100410 print_deprecated(option);
411 rx_paths = comma_delimited_to_vector(optarg);
412 rx_paths_set = true;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100413 break;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100414 case 'C':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100415 config_file = optarg;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100416 break;
Pau Espin Pedrol16e7e202018-06-15 15:19:21 +0200417 case 'V':
418 print_version(1);
419 exit(0);
420 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500421 default:
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100422 goto bad_config;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500423 }
424 }
Tom Tsou64ad7122015-05-19 18:26:31 -0700425
Harald Welte501d0532019-12-03 21:41:13 +0100426 if (argc > optind) {
427 LOG(ERROR) << "Unsupported positional arguments on command line";
428 goto bad_config;
429 }
430
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100431 /* Cmd line option specific validation & setup */
Tom Tsou2e4ed102016-06-27 15:39:16 -0700432
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100433 if (trx->cfg.num_chans > TRX_CHAN_MAX) {
434 LOG(ERROR) << "Too many channels requested, maximum is " << TRX_CHAN_MAX;
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700435 goto bad_config;
436 }
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100437 if ((tx_paths_set && tx_paths.size() != trx->cfg.num_chans) ||
438 (rx_paths_set && rx_paths.size() != trx->cfg.num_chans)) {
439 LOG(ERROR) << "Num of channels and num of Rx/Tx Antennas doesn't match";
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700440 goto bad_config;
Tom Tsou64ad7122015-05-19 18:26:31 -0700441 }
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100442 for (i = 0; i < trx->cfg.num_chans; i++) {
443 trx->cfg.chans[i].trx = trx;
444 trx->cfg.chans[i].idx = i;
445 if (tx_paths_set)
446 osmo_talloc_replace_string(trx, &trx->cfg.chans[i].tx_path, tx_paths[i].c_str());
447 if (rx_paths_set)
448 osmo_talloc_replace_string(trx, &trx->cfg.chans[i].rx_path, rx_paths[i].c_str());
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100449 }
450
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700451 return;
452
453bad_config:
454 print_help();
455 exit(0);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500456}
457
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100458int trx_validate_config(struct trx_ctx *trx)
459{
Tom Tsoud2800452019-04-01 07:55:48 +0700460 if (trx->cfg.multi_arfcn && trx->cfg.num_chans > TRX_MCHAN_MAX) {
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100461 LOG(ERROR) << "Unsupported number of channels";
462 return -1;
463 }
464
465 /* Force 4 SPS for EDGE or multi-ARFCN configurations */
466 if ((trx->cfg.egprs || trx->cfg.multi_arfcn) &&
Harald Welte8fb0c3d2018-10-21 12:15:30 +0200467 (trx->cfg.tx_sps!=4 || trx->cfg.rx_sps!=4)) {
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100468 LOG(ERROR) << "EDGE and Multi-Carrier options require 4 tx and rx sps. Check you config.";
469 return -1;
470 }
471
472 return 0;
473}
474
475static int set_sched_rr(unsigned int prio)
Harald Welte81486e02017-06-29 15:35:22 +0200476{
477 struct sched_param param;
478 int rc;
479 memset(&param, 0, sizeof(param));
480 param.sched_priority = prio;
Pau Espin Pedrol553a2502020-07-29 18:05:25 +0200481 LOG(INFO) << "Setting SCHED_RR priority " << param.sched_priority
482 << ". This setting is DEPRECATED, please use 'policy rr " << param.sched_priority
483 << "' under the 'sched' VTY node instead.";
Harald Welte81486e02017-06-29 15:35:22 +0200484 rc = sched_setscheduler(getpid(), SCHED_RR, &param);
485 if (rc != 0) {
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100486 LOG(ERROR) << "Config: Setting SCHED_RR failed";
Harald Welte81486e02017-06-29 15:35:22 +0200487 return -1;
488 }
489 return 0;
490}
491
Vadim Yanitskiy744e44e2020-10-29 16:49:08 +0700492static void print_simd_info(void)
493{
494#ifdef HAVE_SSE3
495 LOGP(DMAIN, LOGL_INFO, "SSE3 support compiled in");
496#ifdef HAVE___BUILTIN_CPU_SUPPORTS
497 if (__builtin_cpu_supports("sse3"))
498 LOGPC(DMAIN, LOGL_INFO, " and supported by CPU\n");
499 else
500 LOGPC(DMAIN, LOGL_INFO, ", but not supported by CPU\n");
501#else
502 LOGPC(DMAIN, LOGL_INFO, ", but runtime SIMD detection disabled\n");
503#endif
504#endif
505
506#ifdef HAVE_SSE4_1
507 LOGP(DMAIN, LOGL_INFO, "SSE4.1 support compiled in");
508#ifdef HAVE___BUILTIN_CPU_SUPPORTS
509 if (__builtin_cpu_supports("sse4.1"))
510 LOGPC(DMAIN, LOGL_INFO, " and supported by CPU\n");
511 else
512 LOGPC(DMAIN, LOGL_INFO, ", but not supported by CPU\n");
513#else
514 LOGPC(DMAIN, LOGL_INFO, ", but runtime SIMD detection disabled\n");
515#endif
516#endif
517
518#ifndef HAVE_ATOMIC_OPS
519#pragma message ("Built without atomic operation support. Using Mutex, it may affect performance!")
520 LOG(NOTICE) << "Built without atomic operation support. Using Mutex, it may affect performance!";
521#endif
522}
523
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100524static void print_config(struct trx_ctx *trx)
525{
526 unsigned int i;
527 std::ostringstream ost("");
528
529 ost << "Config Settings" << std::endl;
Pau Espin Pedrol8dffadb2018-03-06 18:38:22 +0100530 ost << " Log Level............... " << (unsigned int) osmo_stderr_target->loglevel << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100531 ost << " Device args............. " << charp2str(trx->cfg.dev_args) << std::endl;
532 ost << " TRX Base Port........... " << trx->cfg.base_port << std::endl;
533 ost << " TRX Address............. " << charp2str(trx->cfg.bind_addr) << std::endl;
Harald Weltefad2e092018-04-28 21:25:09 +0200534 ost << " GSM BTS Address......... " << charp2str(trx->cfg.remote_addr) << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100535 ost << " Channels................ " << trx->cfg.num_chans << std::endl;
536 ost << " Tx Samples-per-Symbol... " << trx->cfg.tx_sps << std::endl;
537 ost << " Rx Samples-per-Symbol... " << trx->cfg.rx_sps << std::endl;
538 ost << " EDGE support............ " << trx->cfg.egprs << std::endl;
Vadim Yanitskiya8b35652018-10-22 02:52:18 +0200539 ost << " Extended RACH support... " << trx->cfg.ext_rach << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100540 ost << " Reference............... " << trx->cfg.clock_ref << std::endl;
Alexander Chemeris9a87d902019-10-15 00:33:07 +0300541 ost << " Filler Burst Type....... " << get_value_string(filler_names, trx->cfg.filler) << std::endl;
542 ost << " Filler Burst TSC........ " << trx->cfg.rtsc << std::endl;
543 ost << " Filler Burst RACH Delay. " << trx->cfg.rach_delay << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100544 ost << " Multi-Carrier........... " << trx->cfg.multi_arfcn << std::endl;
Vadim Yanitskiy3b8f7c42018-08-23 13:41:06 +0700545 ost << " LO freq. offset......... " << trx->cfg.offset << std::endl;
546 if (trx->cfg.freq_offset_khz != 0)
547 ost << " Tune freq. offset....... " << trx->cfg.freq_offset_khz << std::endl;
Pau Espin Pedrole91544d2020-10-13 17:03:37 +0200548 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 +0100549 ost << " Swap channels........... " << trx->cfg.swap_channels << std::endl;
550 ost << " Tx Antennas.............";
551 for (i = 0; i < trx->cfg.num_chans; i++) {
552 std::string p = charp2str(trx->cfg.chans[i].tx_path);
553 ost << " '" << ((p != "") ? p : "<default>") << "'";
554 }
555 ost << std::endl;
556 ost << " Rx Antennas.............";
557 for (i = 0; i < trx->cfg.num_chans; i++) {
558 std::string p = charp2str(trx->cfg.chans[i].rx_path);
559 ost << " '" << ((p != "") ? p : "<default>") << "'";
560 }
561 ost << std::endl;
562
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100563 LOG(INFO) << ost << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100564}
565
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100566static void trx_stop()
567{
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100568 LOG(NOTICE) << "Shutting down transceiver..." << std::endl;
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100569
570 delete transceiver;
571 delete radio;
572 delete usrp;
573}
574
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100575static int trx_start(struct trx_ctx *trx)
Thomas Tsou85b179d2013-11-15 21:14:33 -0500576{
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100577 int type, chans;
578 unsigned int i;
579 std::vector<std::string> rx_paths, tx_paths;
Tom Tsou05c6feb2016-06-22 16:09:44 -0700580 RadioDevice::InterfaceType iface = RadioDevice::NORMAL;
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100581
582 /* Create the low level device object */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100583 if (trx->cfg.multi_arfcn)
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100584 iface = RadioDevice::MULTI_ARFCN;
585
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100586 /* Generate vector of rx/tx_path: */
587 for (i = 0; i < trx->cfg.num_chans; i++) {
588 rx_paths.push_back(charp2str(trx->cfg.chans[i].rx_path));
589 tx_paths.push_back(charp2str(trx->cfg.chans[i].tx_path));
590 }
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100591
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100592 usrp = RadioDevice::make(trx->cfg.tx_sps, trx->cfg.rx_sps, iface,
593 trx->cfg.num_chans, trx->cfg.offset,
594 tx_paths, rx_paths);
595 type = usrp->open(charp2str(trx->cfg.dev_args), trx->cfg.clock_ref, trx->cfg.swap_channels);
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100596 if (type < 0) {
597 LOG(ALERT) << "Failed to create radio device" << std::endl;
598 goto shutdown;
599 }
600
601 /* Setup the appropriate device interface */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100602 radio = makeRadioInterface(trx, usrp, type);
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100603 if (!radio)
604 goto shutdown;
605
606 /* Create the transceiver core */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100607 if (makeTransceiver(trx, radio) < 0)
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100608 goto shutdown;
609
610 chans = transceiver->numChans();
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100611 LOG(NOTICE) << "-- Transceiver active with "
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100612 << chans << " channel(s)" << std::endl;
613
614 return 0;
615
616shutdown:
617 trx_stop();
618 return -1;
619}
620
621int main(int argc, char *argv[])
622{
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100623 int rc;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500624
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +0100625 tall_trx_ctx = talloc_named_const(NULL, 0, "OsmoTRX");
626 msgb_talloc_ctx_init(tall_trx_ctx, 0);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100627 g_vty_info.tall_ctx = tall_trx_ctx;
628
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100629 setup_signal_handlers();
630
Pau Espin Pedrola3ab8c22018-02-21 15:41:03 +0100631 g_trx_ctx = vty_trx_ctx_alloc(tall_trx_ctx);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100632
Philipp Maier7e07cf22017-03-15 18:09:35 +0100633 convolve_init();
634 convert_init();
635
Pau Espin Pedrole1977fc2018-04-16 14:50:11 +0200636 osmo_init_logging2(tall_trx_ctx, &log_info);
Pau Espin Pedrolb7e99272019-09-17 20:26:53 +0200637 log_enable_multithread();
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100638 osmo_stats_init(tall_trx_ctx);
639 vty_init(&g_vty_info);
Pau Espin Pedrolaebbfe02020-01-02 16:39:11 +0100640 logging_vty_add_cmds();
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100641 ctrl_vty_init(tall_trx_ctx);
Pau Espin Pedrol553a2502020-07-29 18:05:25 +0200642 osmo_cpu_sched_vty_init(tall_trx_ctx);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100643 trx_vty_init(g_trx_ctx);
644
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100645 osmo_talloc_vty_add_cmds();
646 osmo_stats_vty_add_cmds();
647
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100648 handle_options(argc, argv, g_trx_ctx);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500649
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100650 rate_ctr_init(tall_trx_ctx);
651
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100652 rc = vty_read_config_file(config_file, NULL);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100653 if (rc < 0) {
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100654 fprintf(stderr, "Failed to open config file: '%s'\n", config_file);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100655 exit(2);
656 }
657
658 rc = telnet_init_dynif(tall_trx_ctx, NULL, vty_get_bind_addr(), OSMO_VTY_PORT_TRX);
659 if (rc < 0)
660 exit(1);
661
Pau Espin Pedrol88f86a12019-07-22 12:30:14 +0200662 g_ctrlh = ctrl_interface_setup_dynip(NULL, ctrl_vty_get_bind_addr(), OSMO_CTRL_PORT_TRX, NULL);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100663 if (!g_ctrlh) {
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100664 LOG(ERROR) << "Failed to create CTRL interface.\n";
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100665 exit(1);
666 }
667
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100668 /* Backward compatibility: Hack to have 1 channel allocated by default.
669 * Can be Dropped once we * get rid of "-c" cmdline param */
670 if (g_trx_ctx->cfg.num_chans == 0) {
671 g_trx_ctx->cfg.num_chans = 1;
672 g_trx_ctx->cfg.chans[0].trx = g_trx_ctx;
673 g_trx_ctx->cfg.chans[0].idx = 0;
674 LOG(ERROR) << "No explicit channel config found. Make sure you" \
675 " configure channels in VTY config. Using 1 channel as default," \
676 " but expect your config to break in the future.";
Harald Welte81486e02017-06-29 15:35:22 +0200677 }
678
Vadim Yanitskiy744e44e2020-10-29 16:49:08 +0700679 print_simd_info();
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100680 print_config(g_trx_ctx);
681
682 if (trx_validate_config(g_trx_ctx) < 0) {
683 LOG(ERROR) << "Config failure - exiting";
Thomas Tsou85b179d2013-11-15 21:14:33 -0500684 return EXIT_FAILURE;
685 }
686
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100687 if (g_trx_ctx->cfg.sched_rr) {
688 if (set_sched_rr(g_trx_ctx->cfg.sched_rr) < 0)
689 return EXIT_FAILURE;
690 }
691
Pau Espin Pedrolb426e4a2019-06-04 12:39:28 +0200692 osmo_signal_register_handler(SS_MAIN, transc_sig_cb, NULL);
Pau Espin Pedrol4456b6f2019-05-24 16:54:19 +0200693 trx_rate_ctr_init(tall_trx_ctx, g_trx_ctx);
694
Thomas Tsou85b179d2013-11-15 21:14:33 -0500695 srandom(time(NULL));
696
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100697 if(trx_start(g_trx_ctx) < 0)
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100698 return EXIT_FAILURE;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500699
700 while (!gshutdown)
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100701 osmo_select_main(0);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500702
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100703 trx_stop();
Thomas Tsou85b179d2013-11-15 21:14:33 -0500704
Pau Espin Pedrol21032b72019-03-29 19:20:06 +0100705 osmo_fd_unregister(&signal_ofd);
706 osmo_fd_close(&signal_ofd);
Pau Espin Pedrolb426e4a2019-06-04 12:39:28 +0200707 osmo_signal_unregister_handler(SS_MAIN, transc_sig_cb, NULL);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500708 return 0;
709}