blob: 4a924473f5697d915a92d1fcf7d9f8bfabb4f701 [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 Pedrol408f2502018-02-21 18:47:35 +0100147 transceiver = new Transceiver(trx->cfg.base_port, trx->cfg.bind_addr,
148 trx->cfg.remote_addr, trx->cfg.tx_sps,
149 trx->cfg.rx_sps, trx->cfg.num_chans, GSM::Time(3,0),
Eric Wildac0487e2019-06-17 13:02:44 +0200150 radio, trx->cfg.rssi_offset, trx->cfg.stack_size);
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100151 if (!transceiver->init(trx->cfg.filler, trx->cfg.rtsc,
Vadim Yanitskiya8b35652018-10-22 02:52:18 +0200152 trx->cfg.rach_delay, trx->cfg.egprs, trx->cfg.ext_rach)) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500153 LOG(ALERT) << "Failed to initialize transceiver";
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100154 return -1;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500155 }
156
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100157 for (size_t i = 0; i < trx->cfg.num_chans; i++) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500158 fifo = radio->receiveFIFO(i);
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100159 if (fifo && transceiver->receiveFIFO(fifo, i))
Thomas Tsou85b179d2013-11-15 21:14:33 -0500160 continue;
161
162 LOG(ALERT) << "Could not attach FIFO to channel " << i;
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100163 return -1;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500164 }
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100165 return 0;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500166}
167
168static void sig_handler(int signo)
169{
Pau Espin Pedrold01c7b92019-03-29 18:36:30 +0100170
171 if (gshutdown)
172 /* We are in the middle of shutdown process, avoid any kind of extra
173 action like printing */
174 return;
175
Pau Espin Pedrol3eed8eb2019-08-14 16:26:45 +0200176 fprintf(stderr, "signal %d received\n", signo);
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100177 switch (signo) {
178 case SIGINT:
179 case SIGTERM:
Pau Espin Pedrol3eed8eb2019-08-14 16:26:45 +0200180 fprintf(stderr, "shutting down\n");
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100181 gshutdown = true;
182 break;
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +0100183 case SIGABRT:
184 case SIGUSR1:
185 talloc_report(tall_trx_ctx, stderr);
186 talloc_report_full(tall_trx_ctx, stderr);
187 break;
188 case SIGUSR2:
189 talloc_report_full(tall_trx_ctx, stderr);
190 break;
Pau Espin Pedrol21032b72019-03-29 19:20:06 +0100191 case SIGHUP:
192 log_targets_reopen();
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100193 default:
194 break;
195 }
Pau Espin Pedrol21032b72019-03-29 19:20:06 +0100196
197}
198
199static int signalfd_callback(struct osmo_fd *ofd, unsigned int what)
200{
201 struct signalfd_siginfo fdsi;
202 ssize_t s;
203
204 s = read(ofd->fd, &fdsi, sizeof(struct signalfd_siginfo));
205 if (s < 0) {
206 LOG(FATAL) << "Failed to read from signalfd ("<< ofd->fd << "): " << errno;
207 gshutdown = true;
208 return 0;
209 }
210 sig_handler(fdsi.ssi_signo);
211 return 0;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500212}
213
214static void setup_signal_handlers()
215{
Pau Espin Pedrol21032b72019-03-29 19:20:06 +0100216 sigset_t set;
217 int sfd;
218
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100219 signal(SIGABRT, &sig_handler);
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100220 osmo_init_ignore_signals();
Pau Espin Pedrol21032b72019-03-29 19:20:06 +0100221
222 /* Other threads created by this thread (main) will inherit a copy of the
223 signal mask. */
224 sigemptyset(&set);
225 sigaddset(&set, SIGINT);
226 sigaddset(&set, SIGTERM);
227 sigaddset(&set, SIGUSR1);
228 sigaddset(&set, SIGUSR2);
229 sigaddset(&set, SIGHUP);
230 if (pthread_sigmask(SIG_BLOCK, &set, NULL)) {
231 fprintf(stderr, "pthread_sigmask() failed.\n");
232 exit(EXIT_FAILURE);
233 }
234
235 if ((sfd = signalfd(-1, &set, 0)) == -1) {
236 fprintf(stderr, "signalfd() failed (%d).\n", errno);
237 exit(EXIT_FAILURE);
238 }
239
Pau Espin Pedrol4a575b02020-05-09 18:54:17 +0200240 osmo_fd_setup(&signal_ofd, sfd, OSMO_FD_READ, signalfd_callback, NULL, 0);
Pau Espin Pedrol5e6f3e02019-04-02 11:45:04 +0200241 if (osmo_fd_register(&signal_ofd) < 0) {
242 fprintf(stderr, "osmo_fd_register() failed.\n");
243 exit(EXIT_FAILURE);
244 }
Thomas Tsou85b179d2013-11-15 21:14:33 -0500245}
246
247static void print_help()
248{
249 fprintf(stdout, "Options:\n"
Oliver Smitha439fed2018-10-23 13:12:17 +0200250 " -h, --help This text\n"
251 " -C, --config Filename The config file to use\n"
252 " -V, --version Print the version of OsmoTRX\n"
Pau Espin Pedrol3da1f832018-02-20 20:01:10 +0100253 );
Thomas Tsou85b179d2013-11-15 21:14:33 -0500254}
255
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100256static void print_deprecated(char opt)
257{
258 LOG(WARNING) << "Cmd line option '" << opt << "' is deprecated and will be soon removed."
259 << " Please use VTY cfg option instead."
Ruben Undheim252564b2018-07-20 22:03:39 +0200260 << " All cmd line options are already being overridden by VTY options if set.";
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100261}
262
263static void handle_options(int argc, char **argv, struct trx_ctx* trx)
Thomas Tsou85b179d2013-11-15 21:14:33 -0500264{
265 int option;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100266 unsigned int i;
267 std::vector<std::string> rx_paths, tx_paths;
268 bool rx_paths_set = false, tx_paths_set = false;
Oliver Smitha439fed2018-10-23 13:12:17 +0200269 static struct option long_options[] = {
270 {"help", 0, 0, 'h'},
271 {"config", 1, 0, 'C'},
272 {"version", 0, 0, 'V'},
273 {NULL, 0, 0, 0}
274 };
Thomas Tsou85b179d2013-11-15 21:14:33 -0500275
Oliver Smitha439fed2018-10-23 13:12:17 +0200276 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,
277 NULL)) != -1) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500278 switch (option) {
279 case 'h':
280 print_help();
281 exit(0);
282 break;
283 case 'a':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100284 print_deprecated(option);
285 osmo_talloc_replace_string(trx, &trx->cfg.dev_args, optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500286 break;
Pau Espin Pedrol8dffadb2018-03-06 18:38:22 +0100287 case 'l':
288 print_deprecated(option);
289 log_set_log_level(osmo_stderr_target, atoi(optarg));
290 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500291 case 'i':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100292 print_deprecated(option);
293 osmo_talloc_replace_string(trx, &trx->cfg.remote_addr, optarg);
Pau Espin Pedrol8c800952017-08-16 16:53:23 +0200294 break;
295 case 'j':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100296 print_deprecated(option);
297 osmo_talloc_replace_string(trx, &trx->cfg.bind_addr, optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500298 break;
299 case 'p':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100300 print_deprecated(option);
301 trx->cfg.base_port = atoi(optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500302 break;
303 case 'c':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100304 print_deprecated(option);
305 trx->cfg.num_chans = atoi(optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500306 break;
Tom Tsou76764272016-06-24 14:25:39 -0700307 case 'm':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100308 print_deprecated(option);
309 trx->cfg.multi_arfcn = true;
Tom Tsou76764272016-06-24 14:25:39 -0700310 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500311 case 'x':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100312 print_deprecated(option);
313 trx->cfg.clock_ref = REF_EXTERNAL;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500314 break;
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700315 case 'g':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100316 print_deprecated(option);
317 trx->cfg.clock_ref = REF_GPS;
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700318 break;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500319 case 'f':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100320 print_deprecated(option);
321 trx->cfg.filler = FILLER_DUMMY;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500322 break;
Thomas Tsou8e17df72014-03-06 14:16:11 -0500323 case 'o':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100324 print_deprecated(option);
325 trx->cfg.offset = atof(optarg);
Thomas Tsou8e17df72014-03-06 14:16:11 -0500326 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500327 case 's':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100328 print_deprecated(option);
329 trx->cfg.tx_sps = atoi(optarg);
Tom Tsou64ad7122015-05-19 18:26:31 -0700330 break;
Tom Tsou2e4ed102016-06-27 15:39:16 -0700331 case 'b':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100332 print_deprecated(option);
333 trx->cfg.rx_sps = atoi(optarg);
Tom Tsou2e4ed102016-06-27 15:39:16 -0700334 break;
Tom Tsou64ad7122015-05-19 18:26:31 -0700335 case 'r':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100336 print_deprecated(option);
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100337 trx->cfg.rtsc = atoi(optarg);
338 if (!trx->cfg.egprs) /* Don't override egprs which sets different filler */
339 trx->cfg.filler = FILLER_NORM_RAND;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500340 break;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300341 case 'A':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100342 print_deprecated(option);
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100343 trx->cfg.rach_delay = atoi(optarg);
344 trx->cfg.filler = FILLER_ACCESS_RAND;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300345 break;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400346 case 'R':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100347 print_deprecated(option);
348 trx->cfg.rssi_offset = atof(optarg);
Alexander Chemerise8905a02015-06-03 23:47:56 -0400349 break;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400350 case 'S':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100351 print_deprecated(option);
352 trx->cfg.swap_channels = true;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400353 break;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800354 case 'e':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100355 print_deprecated(option);
356 trx->cfg.egprs = true;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800357 break;
Harald Welte81486e02017-06-29 15:35:22 +0200358 case 't':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100359 print_deprecated(option);
360 trx->cfg.sched_rr = atoi(optarg);
Harald Welte81486e02017-06-29 15:35:22 +0200361 break;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100362 case 'y':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100363 print_deprecated(option);
364 tx_paths = comma_delimited_to_vector(optarg);
365 tx_paths_set = true;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100366 break;
367 case 'z':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100368 print_deprecated(option);
369 rx_paths = comma_delimited_to_vector(optarg);
370 rx_paths_set = true;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100371 break;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100372 case 'C':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100373 config_file = optarg;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100374 break;
Pau Espin Pedrol16e7e202018-06-15 15:19:21 +0200375 case 'V':
376 print_version(1);
377 exit(0);
378 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500379 default:
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100380 goto bad_config;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500381 }
382 }
Tom Tsou64ad7122015-05-19 18:26:31 -0700383
Harald Welte501d0532019-12-03 21:41:13 +0100384 if (argc > optind) {
385 LOG(ERROR) << "Unsupported positional arguments on command line";
386 goto bad_config;
387 }
388
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100389 /* Cmd line option specific validation & setup */
Tom Tsou2e4ed102016-06-27 15:39:16 -0700390
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100391 if (trx->cfg.num_chans > TRX_CHAN_MAX) {
392 LOG(ERROR) << "Too many channels requested, maximum is " << TRX_CHAN_MAX;
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700393 goto bad_config;
394 }
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100395 if ((tx_paths_set && tx_paths.size() != trx->cfg.num_chans) ||
396 (rx_paths_set && rx_paths.size() != trx->cfg.num_chans)) {
397 LOG(ERROR) << "Num of channels and num of Rx/Tx Antennas doesn't match";
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700398 goto bad_config;
Tom Tsou64ad7122015-05-19 18:26:31 -0700399 }
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100400 for (i = 0; i < trx->cfg.num_chans; i++) {
401 trx->cfg.chans[i].trx = trx;
402 trx->cfg.chans[i].idx = i;
403 if (tx_paths_set)
404 osmo_talloc_replace_string(trx, &trx->cfg.chans[i].tx_path, tx_paths[i].c_str());
405 if (rx_paths_set)
406 osmo_talloc_replace_string(trx, &trx->cfg.chans[i].rx_path, rx_paths[i].c_str());
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100407 }
408
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700409 return;
410
411bad_config:
412 print_help();
413 exit(0);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500414}
415
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100416int trx_validate_config(struct trx_ctx *trx)
417{
Tom Tsoud2800452019-04-01 07:55:48 +0700418 if (trx->cfg.multi_arfcn && trx->cfg.num_chans > TRX_MCHAN_MAX) {
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100419 LOG(ERROR) << "Unsupported number of channels";
420 return -1;
421 }
422
423 /* Force 4 SPS for EDGE or multi-ARFCN configurations */
424 if ((trx->cfg.egprs || trx->cfg.multi_arfcn) &&
Harald Welte8fb0c3d2018-10-21 12:15:30 +0200425 (trx->cfg.tx_sps!=4 || trx->cfg.rx_sps!=4)) {
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100426 LOG(ERROR) << "EDGE and Multi-Carrier options require 4 tx and rx sps. Check you config.";
427 return -1;
428 }
429
430 return 0;
431}
432
433static int set_sched_rr(unsigned int prio)
Harald Welte81486e02017-06-29 15:35:22 +0200434{
435 struct sched_param param;
436 int rc;
437 memset(&param, 0, sizeof(param));
438 param.sched_priority = prio;
Pau Espin Pedrol553a2502020-07-29 18:05:25 +0200439 LOG(INFO) << "Setting SCHED_RR priority " << param.sched_priority
440 << ". This setting is DEPRECATED, please use 'policy rr " << param.sched_priority
441 << "' under the 'sched' VTY node instead.";
Harald Welte81486e02017-06-29 15:35:22 +0200442 rc = sched_setscheduler(getpid(), SCHED_RR, &param);
443 if (rc != 0) {
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100444 LOG(ERROR) << "Config: Setting SCHED_RR failed";
Harald Welte81486e02017-06-29 15:35:22 +0200445 return -1;
446 }
447 return 0;
448}
449
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100450static void print_config(struct trx_ctx *trx)
451{
452 unsigned int i;
453 std::ostringstream ost("");
454
455 ost << "Config Settings" << std::endl;
Pau Espin Pedrol8dffadb2018-03-06 18:38:22 +0100456 ost << " Log Level............... " << (unsigned int) osmo_stderr_target->loglevel << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100457 ost << " Device args............. " << charp2str(trx->cfg.dev_args) << std::endl;
458 ost << " TRX Base Port........... " << trx->cfg.base_port << std::endl;
459 ost << " TRX Address............. " << charp2str(trx->cfg.bind_addr) << std::endl;
Harald Weltefad2e092018-04-28 21:25:09 +0200460 ost << " GSM BTS Address......... " << charp2str(trx->cfg.remote_addr) << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100461 ost << " Channels................ " << trx->cfg.num_chans << std::endl;
462 ost << " Tx Samples-per-Symbol... " << trx->cfg.tx_sps << std::endl;
463 ost << " Rx Samples-per-Symbol... " << trx->cfg.rx_sps << std::endl;
464 ost << " EDGE support............ " << trx->cfg.egprs << std::endl;
Vadim Yanitskiya8b35652018-10-22 02:52:18 +0200465 ost << " Extended RACH support... " << trx->cfg.ext_rach << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100466 ost << " Reference............... " << trx->cfg.clock_ref << std::endl;
Alexander Chemeris9a87d902019-10-15 00:33:07 +0300467 ost << " Filler Burst Type....... " << get_value_string(filler_names, trx->cfg.filler) << std::endl;
468 ost << " Filler Burst TSC........ " << trx->cfg.rtsc << std::endl;
469 ost << " Filler Burst RACH Delay. " << trx->cfg.rach_delay << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100470 ost << " Multi-Carrier........... " << trx->cfg.multi_arfcn << std::endl;
471 ost << " Tuning offset........... " << trx->cfg.offset << std::endl;
472 ost << " RSSI to dBm offset...... " << trx->cfg.rssi_offset << std::endl;
473 ost << " Swap channels........... " << trx->cfg.swap_channels << std::endl;
474 ost << " Tx Antennas.............";
475 for (i = 0; i < trx->cfg.num_chans; i++) {
476 std::string p = charp2str(trx->cfg.chans[i].tx_path);
477 ost << " '" << ((p != "") ? p : "<default>") << "'";
478 }
479 ost << std::endl;
480 ost << " Rx Antennas.............";
481 for (i = 0; i < trx->cfg.num_chans; i++) {
482 std::string p = charp2str(trx->cfg.chans[i].rx_path);
483 ost << " '" << ((p != "") ? p : "<default>") << "'";
484 }
485 ost << std::endl;
486
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100487 LOG(INFO) << ost << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100488}
489
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100490static void trx_stop()
491{
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100492 LOG(NOTICE) << "Shutting down transceiver..." << std::endl;
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100493
494 delete transceiver;
495 delete radio;
496 delete usrp;
497}
498
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100499static int trx_start(struct trx_ctx *trx)
Thomas Tsou85b179d2013-11-15 21:14:33 -0500500{
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100501 int type, chans;
502 unsigned int i;
503 std::vector<std::string> rx_paths, tx_paths;
Tom Tsou05c6feb2016-06-22 16:09:44 -0700504 RadioDevice::InterfaceType iface = RadioDevice::NORMAL;
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100505
506 /* Create the low level device object */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100507 if (trx->cfg.multi_arfcn)
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100508 iface = RadioDevice::MULTI_ARFCN;
509
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100510 /* Generate vector of rx/tx_path: */
511 for (i = 0; i < trx->cfg.num_chans; i++) {
512 rx_paths.push_back(charp2str(trx->cfg.chans[i].rx_path));
513 tx_paths.push_back(charp2str(trx->cfg.chans[i].tx_path));
514 }
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100515
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100516 usrp = RadioDevice::make(trx->cfg.tx_sps, trx->cfg.rx_sps, iface,
517 trx->cfg.num_chans, trx->cfg.offset,
518 tx_paths, rx_paths);
519 type = usrp->open(charp2str(trx->cfg.dev_args), trx->cfg.clock_ref, trx->cfg.swap_channels);
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100520 if (type < 0) {
521 LOG(ALERT) << "Failed to create radio device" << std::endl;
522 goto shutdown;
523 }
524
525 /* Setup the appropriate device interface */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100526 radio = makeRadioInterface(trx, usrp, type);
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100527 if (!radio)
528 goto shutdown;
529
530 /* Create the transceiver core */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100531 if (makeTransceiver(trx, radio) < 0)
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100532 goto shutdown;
533
534 chans = transceiver->numChans();
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100535 LOG(NOTICE) << "-- Transceiver active with "
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100536 << chans << " channel(s)" << std::endl;
537
538 return 0;
539
540shutdown:
541 trx_stop();
542 return -1;
543}
544
545int main(int argc, char *argv[])
546{
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100547 int rc;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500548
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +0100549 tall_trx_ctx = talloc_named_const(NULL, 0, "OsmoTRX");
550 msgb_talloc_ctx_init(tall_trx_ctx, 0);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100551 g_vty_info.tall_ctx = tall_trx_ctx;
552
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100553 setup_signal_handlers();
554
Pau Espin Pedrola3ab8c22018-02-21 15:41:03 +0100555 g_trx_ctx = vty_trx_ctx_alloc(tall_trx_ctx);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100556
Philipp Maiere51a8f02017-03-16 12:09:34 +0100557#ifdef HAVE_SSE3
558 printf("Info: SSE3 support compiled in");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300559#ifdef HAVE___BUILTIN_CPU_SUPPORTS
Philipp Maiere51a8f02017-03-16 12:09:34 +0100560 if (__builtin_cpu_supports("sse3"))
561 printf(" and supported by CPU\n");
562 else
563 printf(", but not supported by CPU\n");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300564#else
565 printf(", but runtime SIMD detection disabled\n");
566#endif
Philipp Maiere51a8f02017-03-16 12:09:34 +0100567#endif
568
569#ifdef HAVE_SSE4_1
570 printf("Info: SSE4.1 support compiled in");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300571#ifdef HAVE___BUILTIN_CPU_SUPPORTS
Philipp Maiere51a8f02017-03-16 12:09:34 +0100572 if (__builtin_cpu_supports("sse4.1"))
573 printf(" and supported by CPU\n");
574 else
575 printf(", but not supported by CPU\n");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300576#else
577 printf(", but runtime SIMD detection disabled\n");
578#endif
Philipp Maiere51a8f02017-03-16 12:09:34 +0100579#endif
580
Pau Espin Pedrole503c982019-09-13 18:56:08 +0200581#ifndef HAVE_ATOMIC_OPS
582#pragma message ("Built without atomic operation support. Using Mutex, it may affect performance!")
583 printf("Built without atomic operation support. Using Mutex, it may affect performance!\n");
584#endif
585
Philipp Maier7e07cf22017-03-15 18:09:35 +0100586 convolve_init();
587 convert_init();
588
Pau Espin Pedrole1977fc2018-04-16 14:50:11 +0200589 osmo_init_logging2(tall_trx_ctx, &log_info);
Pau Espin Pedrolb7e99272019-09-17 20:26:53 +0200590 log_enable_multithread();
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100591 osmo_stats_init(tall_trx_ctx);
592 vty_init(&g_vty_info);
Pau Espin Pedrolaebbfe02020-01-02 16:39:11 +0100593 logging_vty_add_cmds();
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100594 ctrl_vty_init(tall_trx_ctx);
Pau Espin Pedrol553a2502020-07-29 18:05:25 +0200595 osmo_cpu_sched_vty_init(tall_trx_ctx);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100596 trx_vty_init(g_trx_ctx);
597
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100598 osmo_talloc_vty_add_cmds();
599 osmo_stats_vty_add_cmds();
600
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100601 handle_options(argc, argv, g_trx_ctx);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500602
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100603 rate_ctr_init(tall_trx_ctx);
604
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100605 rc = vty_read_config_file(config_file, NULL);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100606 if (rc < 0) {
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100607 fprintf(stderr, "Failed to open config file: '%s'\n", config_file);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100608 exit(2);
609 }
610
611 rc = telnet_init_dynif(tall_trx_ctx, NULL, vty_get_bind_addr(), OSMO_VTY_PORT_TRX);
612 if (rc < 0)
613 exit(1);
614
Pau Espin Pedrol88f86a12019-07-22 12:30:14 +0200615 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 +0100616 if (!g_ctrlh) {
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100617 LOG(ERROR) << "Failed to create CTRL interface.\n";
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100618 exit(1);
619 }
620
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100621 /* Backward compatibility: Hack to have 1 channel allocated by default.
622 * Can be Dropped once we * get rid of "-c" cmdline param */
623 if (g_trx_ctx->cfg.num_chans == 0) {
624 g_trx_ctx->cfg.num_chans = 1;
625 g_trx_ctx->cfg.chans[0].trx = g_trx_ctx;
626 g_trx_ctx->cfg.chans[0].idx = 0;
627 LOG(ERROR) << "No explicit channel config found. Make sure you" \
628 " configure channels in VTY config. Using 1 channel as default," \
629 " but expect your config to break in the future.";
Harald Welte81486e02017-06-29 15:35:22 +0200630 }
631
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100632 print_config(g_trx_ctx);
633
634 if (trx_validate_config(g_trx_ctx) < 0) {
635 LOG(ERROR) << "Config failure - exiting";
Thomas Tsou85b179d2013-11-15 21:14:33 -0500636 return EXIT_FAILURE;
637 }
638
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100639 if (g_trx_ctx->cfg.sched_rr) {
640 if (set_sched_rr(g_trx_ctx->cfg.sched_rr) < 0)
641 return EXIT_FAILURE;
642 }
643
Pau Espin Pedrolb426e4a2019-06-04 12:39:28 +0200644 osmo_signal_register_handler(SS_MAIN, transc_sig_cb, NULL);
Pau Espin Pedrol4456b6f2019-05-24 16:54:19 +0200645 trx_rate_ctr_init(tall_trx_ctx, g_trx_ctx);
646
Thomas Tsou85b179d2013-11-15 21:14:33 -0500647 srandom(time(NULL));
648
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100649 if(trx_start(g_trx_ctx) < 0)
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100650 return EXIT_FAILURE;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500651
652 while (!gshutdown)
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100653 osmo_select_main(0);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500654
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100655 trx_stop();
Thomas Tsou85b179d2013-11-15 21:14:33 -0500656
Pau Espin Pedrol21032b72019-03-29 19:20:06 +0100657 osmo_fd_unregister(&signal_ofd);
658 osmo_fd_close(&signal_ofd);
Pau Espin Pedrolb426e4a2019-06-04 12:39:28 +0200659 osmo_signal_unregister_handler(SS_MAIN, transc_sig_cb, NULL);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500660 return 0;
661}