blob: 9fcbda5af69964e7fdf9d731820df21a54247195 [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>
58
Philipp Maier7e07cf22017-03-15 18:09:35 +010059#include "convolve.h"
60#include "convert.h"
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +010061#include "trx_vty.h"
62#include "debug.h"
Pau Espin Pedroldb936b92018-09-03 16:50:49 +020063#include "osmo_signal.h"
Pau Espin Pedrol4456b6f2019-05-24 16:54:19 +020064#include "trx_rate_ctr.h"
Philipp Maier7e07cf22017-03-15 18:09:35 +010065}
66
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +010067#define DEFAULT_CONFIG_FILE "osmo-trx.cfg"
Thomas Tsou85b179d2013-11-15 21:14:33 -050068
Pau Espin Pedrol408f2502018-02-21 18:47:35 +010069#define charp2str(a) ((a) ? std::string(a) : std::string(""))
70
71static char* config_file = (char*)DEFAULT_CONFIG_FILE;
Thomas Tsou85b179d2013-11-15 21:14:33 -050072
Pau Espin Pedrol21032b72019-03-29 19:20:06 +010073struct osmo_fd signal_ofd;
Thomas Tsou85b179d2013-11-15 21:14:33 -050074volatile bool gshutdown = false;
75
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +010076static void *tall_trx_ctx;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +010077static struct trx_ctx *g_trx_ctx;
78static struct ctrl_handle *g_ctrlh;
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +010079
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +010080static RadioDevice *usrp;
81static RadioInterface *radio;
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +010082
Thomas Tsou85b179d2013-11-15 21:14:33 -050083/* Create radio interface
84 * The interface consists of sample rate changes, frequency shifts,
85 * channel multiplexing, and other conversions. The transceiver core
86 * accepts input vectors sampled at multiples of the GSM symbol rate.
87 * The radio interface connects the main transceiver with the device
88 * object, which may be operating some other rate.
89 */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +010090RadioInterface *makeRadioInterface(struct trx_ctx *trx,
Thomas Tsou85b179d2013-11-15 21:14:33 -050091 RadioDevice *usrp, int type)
92{
93 RadioInterface *radio = NULL;
94
95 switch (type) {
96 case RadioDevice::NORMAL:
Pau Espin Pedrol408f2502018-02-21 18:47:35 +010097 radio = new RadioInterface(usrp, trx->cfg.tx_sps,
98 trx->cfg.rx_sps, trx->cfg.num_chans);
Thomas Tsou85b179d2013-11-15 21:14:33 -050099 break;
100 case RadioDevice::RESAMP_64M:
101 case RadioDevice::RESAMP_100M:
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100102 radio = new RadioInterfaceResamp(usrp, trx->cfg.tx_sps,
103 trx->cfg.rx_sps);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500104 break;
Tom Tsou76764272016-06-24 14:25:39 -0700105 case RadioDevice::MULTI_ARFCN:
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100106 radio = new RadioInterfaceMulti(usrp, trx->cfg.tx_sps,
107 trx->cfg.rx_sps, trx->cfg.num_chans);
Tom Tsou76764272016-06-24 14:25:39 -0700108 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500109 default:
110 LOG(ALERT) << "Unsupported radio interface configuration";
111 return NULL;
112 }
113
114 if (!radio->init(type)) {
115 LOG(ALERT) << "Failed to initialize radio interface";
116 return NULL;
117 }
118
119 return radio;
120}
121
Pau Espin Pedroldb936b92018-09-03 16:50:49 +0200122/* Callback function to be called every time we receive a signal from TRANSC */
123static int transc_sig_cb(unsigned int subsys, unsigned int signal,
124 void *handler_data, void *signal_data)
125{
126 switch (signal) {
Pau Espin Pedrolb426e4a2019-06-04 12:39:28 +0200127 case S_MAIN_STOP_REQUIRED:
Pau Espin Pedroldb936b92018-09-03 16:50:49 +0200128 gshutdown = true;
129 break;
130 default:
131 break;
132 }
133 return 0;
134}
135
Thomas Tsou85b179d2013-11-15 21:14:33 -0500136/* Create transceiver core
137 * The multi-threaded modem core operates at multiples of the GSM rate of
138 * 270.8333 ksps and consists of GSM specific modulation, demodulation,
139 * and decoding schemes. Also included are the socket interfaces for
140 * connecting to the upper layer stack.
141 */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100142int makeTransceiver(struct trx_ctx *trx, RadioInterface *radio)
Thomas Tsou85b179d2013-11-15 21:14:33 -0500143{
Thomas Tsou85b179d2013-11-15 21:14:33 -0500144 VectorFIFO *fifo;
145
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100146 transceiver = new Transceiver(trx->cfg.base_port, trx->cfg.bind_addr,
147 trx->cfg.remote_addr, trx->cfg.tx_sps,
148 trx->cfg.rx_sps, trx->cfg.num_chans, GSM::Time(3,0),
Eric Wildac0487e2019-06-17 13:02:44 +0200149 radio, trx->cfg.rssi_offset, trx->cfg.stack_size);
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100150 if (!transceiver->init(trx->cfg.filler, trx->cfg.rtsc,
Vadim Yanitskiya8b35652018-10-22 02:52:18 +0200151 trx->cfg.rach_delay, trx->cfg.egprs, trx->cfg.ext_rach)) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500152 LOG(ALERT) << "Failed to initialize transceiver";
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100153 return -1;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500154 }
155
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100156 for (size_t i = 0; i < trx->cfg.num_chans; i++) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500157 fifo = radio->receiveFIFO(i);
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100158 if (fifo && transceiver->receiveFIFO(fifo, i))
Thomas Tsou85b179d2013-11-15 21:14:33 -0500159 continue;
160
161 LOG(ALERT) << "Could not attach FIFO to channel " << i;
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100162 return -1;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500163 }
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100164 return 0;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500165}
166
167static void sig_handler(int signo)
168{
Pau Espin Pedrold01c7b92019-03-29 18:36:30 +0100169
170 if (gshutdown)
171 /* We are in the middle of shutdown process, avoid any kind of extra
172 action like printing */
173 return;
174
Pau Espin Pedrol3eed8eb2019-08-14 16:26:45 +0200175 fprintf(stderr, "signal %d received\n", signo);
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100176 switch (signo) {
177 case SIGINT:
178 case SIGTERM:
Pau Espin Pedrol3eed8eb2019-08-14 16:26:45 +0200179 fprintf(stderr, "shutting down\n");
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100180 gshutdown = true;
181 break;
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +0100182 case SIGABRT:
183 case SIGUSR1:
184 talloc_report(tall_trx_ctx, stderr);
185 talloc_report_full(tall_trx_ctx, stderr);
186 break;
187 case SIGUSR2:
188 talloc_report_full(tall_trx_ctx, stderr);
189 break;
Pau Espin Pedrol21032b72019-03-29 19:20:06 +0100190 case SIGHUP:
191 log_targets_reopen();
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100192 default:
193 break;
194 }
Pau Espin Pedrol21032b72019-03-29 19:20:06 +0100195
196}
197
198static int signalfd_callback(struct osmo_fd *ofd, unsigned int what)
199{
200 struct signalfd_siginfo fdsi;
201 ssize_t s;
202
203 s = read(ofd->fd, &fdsi, sizeof(struct signalfd_siginfo));
204 if (s < 0) {
205 LOG(FATAL) << "Failed to read from signalfd ("<< ofd->fd << "): " << errno;
206 gshutdown = true;
207 return 0;
208 }
209 sig_handler(fdsi.ssi_signo);
210 return 0;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500211}
212
213static void setup_signal_handlers()
214{
Pau Espin Pedrol21032b72019-03-29 19:20:06 +0100215 sigset_t set;
216 int sfd;
217
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100218 signal(SIGABRT, &sig_handler);
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100219 osmo_init_ignore_signals();
Pau Espin Pedrol21032b72019-03-29 19:20:06 +0100220
221 /* Other threads created by this thread (main) will inherit a copy of the
222 signal mask. */
223 sigemptyset(&set);
224 sigaddset(&set, SIGINT);
225 sigaddset(&set, SIGTERM);
226 sigaddset(&set, SIGUSR1);
227 sigaddset(&set, SIGUSR2);
228 sigaddset(&set, SIGHUP);
229 if (pthread_sigmask(SIG_BLOCK, &set, NULL)) {
230 fprintf(stderr, "pthread_sigmask() failed.\n");
231 exit(EXIT_FAILURE);
232 }
233
234 if ((sfd = signalfd(-1, &set, 0)) == -1) {
235 fprintf(stderr, "signalfd() failed (%d).\n", errno);
236 exit(EXIT_FAILURE);
237 }
238
Pau Espin Pedrol4a575b02020-05-09 18:54:17 +0200239 osmo_fd_setup(&signal_ofd, sfd, OSMO_FD_READ, signalfd_callback, NULL, 0);
Pau Espin Pedrol5e6f3e02019-04-02 11:45:04 +0200240 if (osmo_fd_register(&signal_ofd) < 0) {
241 fprintf(stderr, "osmo_fd_register() failed.\n");
242 exit(EXIT_FAILURE);
243 }
Thomas Tsou85b179d2013-11-15 21:14:33 -0500244}
245
246static void print_help()
247{
248 fprintf(stdout, "Options:\n"
Oliver Smitha439fed2018-10-23 13:12:17 +0200249 " -h, --help This text\n"
250 " -C, --config Filename The config file to use\n"
251 " -V, --version Print the version of OsmoTRX\n"
Pau Espin Pedrol3da1f832018-02-20 20:01:10 +0100252 );
Thomas Tsou85b179d2013-11-15 21:14:33 -0500253}
254
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100255static void print_deprecated(char opt)
256{
257 LOG(WARNING) << "Cmd line option '" << opt << "' is deprecated and will be soon removed."
258 << " Please use VTY cfg option instead."
Ruben Undheim252564b2018-07-20 22:03:39 +0200259 << " All cmd line options are already being overridden by VTY options if set.";
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100260}
261
262static void handle_options(int argc, char **argv, struct trx_ctx* trx)
Thomas Tsou85b179d2013-11-15 21:14:33 -0500263{
264 int option;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100265 unsigned int i;
266 std::vector<std::string> rx_paths, tx_paths;
267 bool rx_paths_set = false, tx_paths_set = false;
Oliver Smitha439fed2018-10-23 13:12:17 +0200268 static struct option long_options[] = {
269 {"help", 0, 0, 'h'},
270 {"config", 1, 0, 'C'},
271 {"version", 0, 0, 'V'},
272 {NULL, 0, 0, 0}
273 };
Thomas Tsou85b179d2013-11-15 21:14:33 -0500274
Oliver Smitha439fed2018-10-23 13:12:17 +0200275 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,
276 NULL)) != -1) {
Thomas Tsou85b179d2013-11-15 21:14:33 -0500277 switch (option) {
278 case 'h':
279 print_help();
280 exit(0);
281 break;
282 case 'a':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100283 print_deprecated(option);
284 osmo_talloc_replace_string(trx, &trx->cfg.dev_args, optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500285 break;
Pau Espin Pedrol8dffadb2018-03-06 18:38:22 +0100286 case 'l':
287 print_deprecated(option);
288 log_set_log_level(osmo_stderr_target, atoi(optarg));
289 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500290 case 'i':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100291 print_deprecated(option);
292 osmo_talloc_replace_string(trx, &trx->cfg.remote_addr, optarg);
Pau Espin Pedrol8c800952017-08-16 16:53:23 +0200293 break;
294 case 'j':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100295 print_deprecated(option);
296 osmo_talloc_replace_string(trx, &trx->cfg.bind_addr, optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500297 break;
298 case 'p':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100299 print_deprecated(option);
300 trx->cfg.base_port = atoi(optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500301 break;
302 case 'c':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100303 print_deprecated(option);
304 trx->cfg.num_chans = atoi(optarg);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500305 break;
Tom Tsou76764272016-06-24 14:25:39 -0700306 case 'm':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100307 print_deprecated(option);
308 trx->cfg.multi_arfcn = true;
Tom Tsou76764272016-06-24 14:25:39 -0700309 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500310 case 'x':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100311 print_deprecated(option);
312 trx->cfg.clock_ref = REF_EXTERNAL;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500313 break;
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700314 case 'g':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100315 print_deprecated(option);
316 trx->cfg.clock_ref = REF_GPS;
Tom Tsou2f3e60b2016-07-17 19:29:08 -0700317 break;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500318 case 'f':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100319 print_deprecated(option);
320 trx->cfg.filler = FILLER_DUMMY;
Thomas Tsou15d743e2014-01-25 02:34:03 -0500321 break;
Thomas Tsou8e17df72014-03-06 14:16:11 -0500322 case 'o':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100323 print_deprecated(option);
324 trx->cfg.offset = atof(optarg);
Thomas Tsou8e17df72014-03-06 14:16:11 -0500325 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500326 case 's':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100327 print_deprecated(option);
328 trx->cfg.tx_sps = atoi(optarg);
Tom Tsou64ad7122015-05-19 18:26:31 -0700329 break;
Tom Tsou2e4ed102016-06-27 15:39:16 -0700330 case 'b':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100331 print_deprecated(option);
332 trx->cfg.rx_sps = atoi(optarg);
Tom Tsou2e4ed102016-06-27 15:39:16 -0700333 break;
Tom Tsou64ad7122015-05-19 18:26:31 -0700334 case 'r':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100335 print_deprecated(option);
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100336 trx->cfg.rtsc = atoi(optarg);
337 if (!trx->cfg.egprs) /* Don't override egprs which sets different filler */
338 trx->cfg.filler = FILLER_NORM_RAND;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500339 break;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300340 case 'A':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100341 print_deprecated(option);
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100342 trx->cfg.rach_delay = atoi(optarg);
343 trx->cfg.filler = FILLER_ACCESS_RAND;
Alexander Chemeris5efe0502016-03-23 17:06:32 +0300344 break;
Alexander Chemerise8905a02015-06-03 23:47:56 -0400345 case 'R':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100346 print_deprecated(option);
347 trx->cfg.rssi_offset = atof(optarg);
Alexander Chemerise8905a02015-06-03 23:47:56 -0400348 break;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400349 case 'S':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100350 print_deprecated(option);
351 trx->cfg.swap_channels = true;
Alexander Chemeris50747dc2015-06-07 01:07:45 -0400352 break;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800353 case 'e':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100354 print_deprecated(option);
355 trx->cfg.egprs = true;
Tom Tsoub0aefcb2016-03-06 03:44:34 -0800356 break;
Harald Welte81486e02017-06-29 15:35:22 +0200357 case 't':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100358 print_deprecated(option);
359 trx->cfg.sched_rr = atoi(optarg);
Harald Welte81486e02017-06-29 15:35:22 +0200360 break;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100361 case 'y':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100362 print_deprecated(option);
363 tx_paths = comma_delimited_to_vector(optarg);
364 tx_paths_set = true;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100365 break;
366 case 'z':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100367 print_deprecated(option);
368 rx_paths = comma_delimited_to_vector(optarg);
369 rx_paths_set = true;
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100370 break;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100371 case 'C':
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100372 config_file = optarg;
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100373 break;
Pau Espin Pedrol16e7e202018-06-15 15:19:21 +0200374 case 'V':
375 print_version(1);
376 exit(0);
377 break;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500378 default:
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100379 goto bad_config;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500380 }
381 }
Tom Tsou64ad7122015-05-19 18:26:31 -0700382
Harald Welte501d0532019-12-03 21:41:13 +0100383 if (argc > optind) {
384 LOG(ERROR) << "Unsupported positional arguments on command line";
385 goto bad_config;
386 }
387
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100388 /* Cmd line option specific validation & setup */
Tom Tsou2e4ed102016-06-27 15:39:16 -0700389
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100390 if (trx->cfg.num_chans > TRX_CHAN_MAX) {
391 LOG(ERROR) << "Too many channels requested, maximum is " << TRX_CHAN_MAX;
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700392 goto bad_config;
393 }
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100394 if ((tx_paths_set && tx_paths.size() != trx->cfg.num_chans) ||
395 (rx_paths_set && rx_paths.size() != trx->cfg.num_chans)) {
396 LOG(ERROR) << "Num of channels and num of Rx/Tx Antennas doesn't match";
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700397 goto bad_config;
Tom Tsou64ad7122015-05-19 18:26:31 -0700398 }
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100399 for (i = 0; i < trx->cfg.num_chans; i++) {
400 trx->cfg.chans[i].trx = trx;
401 trx->cfg.chans[i].idx = i;
402 if (tx_paths_set)
403 osmo_talloc_replace_string(trx, &trx->cfg.chans[i].tx_path, tx_paths[i].c_str());
404 if (rx_paths_set)
405 osmo_talloc_replace_string(trx, &trx->cfg.chans[i].rx_path, rx_paths[i].c_str());
Pau Espin Pedrol77ce99a2018-02-05 13:05:06 +0100406 }
407
Tom Tsou8f0ccf62016-07-20 16:35:03 -0700408 return;
409
410bad_config:
411 print_help();
412 exit(0);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500413}
414
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100415int trx_validate_config(struct trx_ctx *trx)
416{
Tom Tsoud2800452019-04-01 07:55:48 +0700417 if (trx->cfg.multi_arfcn && trx->cfg.num_chans > TRX_MCHAN_MAX) {
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100418 LOG(ERROR) << "Unsupported number of channels";
419 return -1;
420 }
421
422 /* Force 4 SPS for EDGE or multi-ARFCN configurations */
423 if ((trx->cfg.egprs || trx->cfg.multi_arfcn) &&
Harald Welte8fb0c3d2018-10-21 12:15:30 +0200424 (trx->cfg.tx_sps!=4 || trx->cfg.rx_sps!=4)) {
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100425 LOG(ERROR) << "EDGE and Multi-Carrier options require 4 tx and rx sps. Check you config.";
426 return -1;
427 }
428
429 return 0;
430}
431
432static int set_sched_rr(unsigned int prio)
Harald Welte81486e02017-06-29 15:35:22 +0200433{
434 struct sched_param param;
435 int rc;
436 memset(&param, 0, sizeof(param));
437 param.sched_priority = prio;
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100438 LOG(INFO) << "Setting SCHED_RR priority " << param.sched_priority;
Harald Welte81486e02017-06-29 15:35:22 +0200439 rc = sched_setscheduler(getpid(), SCHED_RR, &param);
440 if (rc != 0) {
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100441 LOG(ERROR) << "Config: Setting SCHED_RR failed";
Harald Welte81486e02017-06-29 15:35:22 +0200442 return -1;
443 }
444 return 0;
445}
446
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100447static void print_config(struct trx_ctx *trx)
448{
449 unsigned int i;
450 std::ostringstream ost("");
451
452 ost << "Config Settings" << std::endl;
Pau Espin Pedrol8dffadb2018-03-06 18:38:22 +0100453 ost << " Log Level............... " << (unsigned int) osmo_stderr_target->loglevel << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100454 ost << " Device args............. " << charp2str(trx->cfg.dev_args) << std::endl;
455 ost << " TRX Base Port........... " << trx->cfg.base_port << std::endl;
456 ost << " TRX Address............. " << charp2str(trx->cfg.bind_addr) << std::endl;
Harald Weltefad2e092018-04-28 21:25:09 +0200457 ost << " GSM BTS Address......... " << charp2str(trx->cfg.remote_addr) << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100458 ost << " Channels................ " << trx->cfg.num_chans << std::endl;
459 ost << " Tx Samples-per-Symbol... " << trx->cfg.tx_sps << std::endl;
460 ost << " Rx Samples-per-Symbol... " << trx->cfg.rx_sps << std::endl;
461 ost << " EDGE support............ " << trx->cfg.egprs << std::endl;
Vadim Yanitskiya8b35652018-10-22 02:52:18 +0200462 ost << " Extended RACH support... " << trx->cfg.ext_rach << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100463 ost << " Reference............... " << trx->cfg.clock_ref << std::endl;
Alexander Chemeris9a87d902019-10-15 00:33:07 +0300464 ost << " Filler Burst Type....... " << get_value_string(filler_names, trx->cfg.filler) << std::endl;
465 ost << " Filler Burst TSC........ " << trx->cfg.rtsc << std::endl;
466 ost << " Filler Burst RACH Delay. " << trx->cfg.rach_delay << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100467 ost << " Multi-Carrier........... " << trx->cfg.multi_arfcn << std::endl;
468 ost << " Tuning offset........... " << trx->cfg.offset << std::endl;
469 ost << " RSSI to dBm offset...... " << trx->cfg.rssi_offset << std::endl;
470 ost << " Swap channels........... " << trx->cfg.swap_channels << std::endl;
471 ost << " Tx Antennas.............";
472 for (i = 0; i < trx->cfg.num_chans; i++) {
473 std::string p = charp2str(trx->cfg.chans[i].tx_path);
474 ost << " '" << ((p != "") ? p : "<default>") << "'";
475 }
476 ost << std::endl;
477 ost << " Rx Antennas.............";
478 for (i = 0; i < trx->cfg.num_chans; i++) {
479 std::string p = charp2str(trx->cfg.chans[i].rx_path);
480 ost << " '" << ((p != "") ? p : "<default>") << "'";
481 }
482 ost << std::endl;
483
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100484 LOG(INFO) << ost << std::endl;
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100485}
486
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100487static void trx_stop()
488{
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100489 LOG(NOTICE) << "Shutting down transceiver..." << std::endl;
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100490
491 delete transceiver;
492 delete radio;
493 delete usrp;
494}
495
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100496static int trx_start(struct trx_ctx *trx)
Thomas Tsou85b179d2013-11-15 21:14:33 -0500497{
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100498 int type, chans;
499 unsigned int i;
500 std::vector<std::string> rx_paths, tx_paths;
Tom Tsou05c6feb2016-06-22 16:09:44 -0700501 RadioDevice::InterfaceType iface = RadioDevice::NORMAL;
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100502
503 /* Create the low level device object */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100504 if (trx->cfg.multi_arfcn)
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100505 iface = RadioDevice::MULTI_ARFCN;
506
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100507 /* Generate vector of rx/tx_path: */
508 for (i = 0; i < trx->cfg.num_chans; i++) {
509 rx_paths.push_back(charp2str(trx->cfg.chans[i].rx_path));
510 tx_paths.push_back(charp2str(trx->cfg.chans[i].tx_path));
511 }
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100512
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100513 usrp = RadioDevice::make(trx->cfg.tx_sps, trx->cfg.rx_sps, iface,
514 trx->cfg.num_chans, trx->cfg.offset,
515 tx_paths, rx_paths);
516 type = usrp->open(charp2str(trx->cfg.dev_args), trx->cfg.clock_ref, trx->cfg.swap_channels);
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100517 if (type < 0) {
518 LOG(ALERT) << "Failed to create radio device" << std::endl;
519 goto shutdown;
520 }
521
522 /* Setup the appropriate device interface */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100523 radio = makeRadioInterface(trx, usrp, type);
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100524 if (!radio)
525 goto shutdown;
526
527 /* Create the transceiver core */
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100528 if (makeTransceiver(trx, radio) < 0)
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100529 goto shutdown;
530
531 chans = transceiver->numChans();
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100532 LOG(NOTICE) << "-- Transceiver active with "
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100533 << chans << " channel(s)" << std::endl;
534
535 return 0;
536
537shutdown:
538 trx_stop();
539 return -1;
540}
541
542int main(int argc, char *argv[])
543{
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100544 int rc;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500545
Pau Espin Pedrol3a3b2202018-02-21 20:15:47 +0100546 tall_trx_ctx = talloc_named_const(NULL, 0, "OsmoTRX");
547 msgb_talloc_ctx_init(tall_trx_ctx, 0);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100548 g_vty_info.tall_ctx = tall_trx_ctx;
549
Pau Espin Pedrolab22f4c2018-02-21 20:15:18 +0100550 setup_signal_handlers();
551
Pau Espin Pedrola3ab8c22018-02-21 15:41:03 +0100552 g_trx_ctx = vty_trx_ctx_alloc(tall_trx_ctx);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100553
Philipp Maiere51a8f02017-03-16 12:09:34 +0100554#ifdef HAVE_SSE3
555 printf("Info: SSE3 support compiled in");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300556#ifdef HAVE___BUILTIN_CPU_SUPPORTS
Philipp Maiere51a8f02017-03-16 12:09:34 +0100557 if (__builtin_cpu_supports("sse3"))
558 printf(" and supported by CPU\n");
559 else
560 printf(", but not supported by CPU\n");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300561#else
562 printf(", but runtime SIMD detection disabled\n");
563#endif
Philipp Maiere51a8f02017-03-16 12:09:34 +0100564#endif
565
566#ifdef HAVE_SSE4_1
567 printf("Info: SSE4.1 support compiled in");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300568#ifdef HAVE___BUILTIN_CPU_SUPPORTS
Philipp Maiere51a8f02017-03-16 12:09:34 +0100569 if (__builtin_cpu_supports("sse4.1"))
570 printf(" and supported by CPU\n");
571 else
572 printf(", but not supported by CPU\n");
Vadim Yanitskiy3bd763d2017-05-20 01:46:51 +0300573#else
574 printf(", but runtime SIMD detection disabled\n");
575#endif
Philipp Maiere51a8f02017-03-16 12:09:34 +0100576#endif
577
Pau Espin Pedrole503c982019-09-13 18:56:08 +0200578#ifndef HAVE_ATOMIC_OPS
579#pragma message ("Built without atomic operation support. Using Mutex, it may affect performance!")
580 printf("Built without atomic operation support. Using Mutex, it may affect performance!\n");
581#endif
582
Philipp Maier7e07cf22017-03-15 18:09:35 +0100583 convolve_init();
584 convert_init();
585
Pau Espin Pedrole1977fc2018-04-16 14:50:11 +0200586 osmo_init_logging2(tall_trx_ctx, &log_info);
Pau Espin Pedrolb7e99272019-09-17 20:26:53 +0200587 log_enable_multithread();
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100588 osmo_stats_init(tall_trx_ctx);
589 vty_init(&g_vty_info);
Pau Espin Pedrolaebbfe02020-01-02 16:39:11 +0100590 logging_vty_add_cmds();
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100591 ctrl_vty_init(tall_trx_ctx);
592 trx_vty_init(g_trx_ctx);
593
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100594 osmo_talloc_vty_add_cmds();
595 osmo_stats_vty_add_cmds();
596
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100597 handle_options(argc, argv, g_trx_ctx);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500598
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100599 rate_ctr_init(tall_trx_ctx);
600
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100601 rc = vty_read_config_file(config_file, NULL);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100602 if (rc < 0) {
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100603 fprintf(stderr, "Failed to open config file: '%s'\n", config_file);
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100604 exit(2);
605 }
606
607 rc = telnet_init_dynif(tall_trx_ctx, NULL, vty_get_bind_addr(), OSMO_VTY_PORT_TRX);
608 if (rc < 0)
609 exit(1);
610
Pau Espin Pedrol88f86a12019-07-22 12:30:14 +0200611 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 +0100612 if (!g_ctrlh) {
Pau Espin Pedrol2d085e22018-12-03 18:21:13 +0100613 LOG(ERROR) << "Failed to create CTRL interface.\n";
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100614 exit(1);
615 }
616
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100617 /* Backward compatibility: Hack to have 1 channel allocated by default.
618 * Can be Dropped once we * get rid of "-c" cmdline param */
619 if (g_trx_ctx->cfg.num_chans == 0) {
620 g_trx_ctx->cfg.num_chans = 1;
621 g_trx_ctx->cfg.chans[0].trx = g_trx_ctx;
622 g_trx_ctx->cfg.chans[0].idx = 0;
623 LOG(ERROR) << "No explicit channel config found. Make sure you" \
624 " configure channels in VTY config. Using 1 channel as default," \
625 " but expect your config to break in the future.";
Harald Welte81486e02017-06-29 15:35:22 +0200626 }
627
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100628 print_config(g_trx_ctx);
629
630 if (trx_validate_config(g_trx_ctx) < 0) {
631 LOG(ERROR) << "Config failure - exiting";
Thomas Tsou85b179d2013-11-15 21:14:33 -0500632 return EXIT_FAILURE;
633 }
634
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100635 if (g_trx_ctx->cfg.sched_rr) {
636 if (set_sched_rr(g_trx_ctx->cfg.sched_rr) < 0)
637 return EXIT_FAILURE;
638 }
639
Pau Espin Pedrolb426e4a2019-06-04 12:39:28 +0200640 osmo_signal_register_handler(SS_MAIN, transc_sig_cb, NULL);
Pau Espin Pedrol4456b6f2019-05-24 16:54:19 +0200641 trx_rate_ctr_init(tall_trx_ctx, g_trx_ctx);
642
Thomas Tsou85b179d2013-11-15 21:14:33 -0500643 srandom(time(NULL));
644
Pau Espin Pedrol408f2502018-02-21 18:47:35 +0100645 if(trx_start(g_trx_ctx) < 0)
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100646 return EXIT_FAILURE;
Thomas Tsou85b179d2013-11-15 21:14:33 -0500647
648 while (!gshutdown)
Pau Espin Pedrol5ea18172018-02-20 16:48:15 +0100649 osmo_select_main(0);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500650
Pau Espin Pedrol0bbd8922018-02-21 11:59:26 +0100651 trx_stop();
Thomas Tsou85b179d2013-11-15 21:14:33 -0500652
Pau Espin Pedrol21032b72019-03-29 19:20:06 +0100653 osmo_fd_unregister(&signal_ofd);
654 osmo_fd_close(&signal_ofd);
Pau Espin Pedrolb426e4a2019-06-04 12:39:28 +0200655 osmo_signal_unregister_handler(SS_MAIN, transc_sig_cb, NULL);
Thomas Tsou85b179d2013-11-15 21:14:33 -0500656 return 0;
657}