blob: 003cabb4b4b6e4b08b5b78bc4893246323e64f09 [file] [log] [blame]
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +04001/* pcu_main.cpp
2 *
3 * Copyright (C) 2012 Ivan Klyuchnikov
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040020#include <pcu_l1_if.h>
Andreas Eversberg5dac2f02012-06-27 15:52:04 +020021#include <gprs_rlcmac.h>
Ivan Kluchnikova9f1ff22012-05-24 22:25:06 +040022#include <gprs_debug.h>
Andreas Eversberg81e895b2012-07-06 08:24:53 +020023#include <unistd.h>
Max1187a772018-01-26 13:31:42 +010024#include <stdbool.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <errno.h>
Andreas Eversberg81e895b2012-07-06 08:24:53 +020029#include <getopt.h>
Andreas Eversberge266bd42012-07-13 14:00:21 +020030#include <signal.h>
Andreas Eversberg783aa4b2013-05-05 09:15:11 +020031#include <sched.h>
Holger Hans Peter Freyther67ed34e2013-10-17 17:01:54 +020032#include <bts.h>
Max1187a772018-01-26 13:31:42 +010033#include <osmocom/pcu/pcuif_proto.h>
Alexander Couzens5c3783b2019-05-25 05:41:18 +020034#include "gprs_bssgp_pcu.h"
35
Andreas Eversbergebde64f2012-07-12 09:18:42 +020036extern "C" {
37#include "pcu_vty.h"
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +020038#include "coding_scheme.h"
Max1187a772018-01-26 13:31:42 +010039#include <osmocom/gprs/gprs_bssgp.h>
Alexander Couzens290d9032020-09-16 21:52:02 +020040#include <osmocom/gprs/gprs_ns2.h>
Andreas Eversbergebde64f2012-07-12 09:18:42 +020041#include <osmocom/vty/telnet_interface.h>
Max1187a772018-01-26 13:31:42 +010042#include <osmocom/vty/command.h>
43#include <osmocom/vty/vty.h>
Neels Hofmeyr01826c12016-08-23 16:31:02 +020044#include <osmocom/vty/ports.h>
Harald Welte398f60e2020-12-10 13:39:21 +010045#include <osmocom/vty/logging.h>
Pau Espin Pedrolb75c27f2020-08-18 13:43:43 +020046#include <osmocom/vty/cpu_sched_vty.h>
Max1187a772018-01-26 13:31:42 +010047#include <osmocom/core/rate_ctr.h>
48#include <osmocom/core/select.h>
49#include <osmocom/core/application.h>
50#include <osmocom/core/msgb.h>
Jacob Erlbeck42aba812015-11-10 15:32:00 +010051#include <osmocom/core/stats.h>
Max2efdf692016-02-22 11:39:30 +010052#include <osmocom/core/gsmtap.h>
53#include <osmocom/core/gsmtap_util.h>
Andreas Eversbergebde64f2012-07-12 09:18:42 +020054}
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040055
Andreas Eversberg3e372d52012-07-06 09:28:15 +020056extern struct gprs_nsvc *nsvc;
Andreas Eversberg81e895b2012-07-06 08:24:53 +020057uint16_t spoof_mcc = 0, spoof_mnc = 0;
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +010058bool spoof_mnc_3_digits = false;
Andreas Eversbergebde64f2012-07-12 09:18:42 +020059static int config_given = 0;
Holger Hans Peter Freyther4e8b50c2013-11-11 20:12:30 +010060static char *config_file = strdup("osmo-pcu.cfg");
Andreas Eversbergebde64f2012-07-12 09:18:42 +020061extern struct vty_app_info pcu_vty_info;
Neels Hofmeyr42f2d612018-04-01 16:54:40 +020062void *tall_pcu_ctx = NULL;
Andreas Eversberg7a5a67a2013-01-16 08:43:24 +010063extern void *bv_tall_ctx;
Andreas Eversberge266bd42012-07-13 14:00:21 +020064static int quit = 0;
Andreas Eversberg783aa4b2013-05-05 09:15:11 +020065static int rt_prio = -1;
Rafael Dinizf0af1b02019-01-28 22:37:46 -020066static bool daemonize = false;
Holger Hans Peter Freyther173ef902016-03-04 17:54:09 +010067static const char *gsmtap_addr = "localhost"; // FIXME: use gengetopt's default value instead
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040068
Andreas Eversberg81e895b2012-07-06 08:24:53 +020069static void print_help()
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040070{
Vadim Yanitskiy35668a22020-10-24 05:10:30 +070071 printf( "Some useful options:\n"
72 " -h --help This text\n"
73 " -c --config-file Specify the filename of the config file\n"
74 " -m --mcc MCC Use given MCC instead of value provided by BTS\n"
75 " -n --mnc MNC Use given MNC instead of value provided by BTS\n"
76 " -V --version Print version\n"
77 " -r --realtime PRIO Use SCHED_RR with the specified priority\n"
78 " -D --daemonize Fork the process into a background daemon\n"
79 " -i --gsmtap-ip The destination IP used for GSMTAP\n"
Vadim Yanitskiy1136fdb2020-10-24 05:14:52 +070080 "\nVTY reference generation:\n"
81 " --vty-ref-mode MODE VTY reference generation mode (e.g. 'expert').\n"
82 " --vty-ref-xml Generate the VTY reference XML output and exit.\n"
Andreas Eversberg81e895b2012-07-06 08:24:53 +020083 );
84}
85
Vadim Yanitskiy1136fdb2020-10-24 05:14:52 +070086static void handle_long_options(const char *prog_name, const int long_option)
87{
88 static int vty_ref_mode = VTY_REF_GEN_MODE_DEFAULT;
89
90 switch (long_option) {
91 case 1:
92 vty_ref_mode = get_string_value(vty_ref_gen_mode_names, optarg);
93 if (vty_ref_mode < 0) {
94 fprintf(stderr, "%s: Unknown VTY reference generation "
95 "mode '%s'\n", prog_name, optarg);
96 exit(2);
97 }
98 break;
99 case 2:
100 fprintf(stderr, "Generating the VTY reference in mode '%s' (%s)\n",
101 get_value_string(vty_ref_gen_mode_names, vty_ref_mode),
102 get_value_string(vty_ref_gen_mode_desc, vty_ref_mode));
103 vty_dump_xml_ref_mode(stdout, (enum vty_ref_gen_mode) vty_ref_mode);
104 exit(0);
105 default:
106 fprintf(stderr, "%s: error parsing cmdline options\n", prog_name);
107 exit(2);
108 }
109}
110
Andreas Eversberg81e895b2012-07-06 08:24:53 +0200111/* FIXME: finally get some option parsing code into libosmocore */
112static void handle_options(int argc, char **argv)
113{
114 while (1) {
115 int option_idx = 0, c;
Philipp Maier08c50372020-10-09 20:41:17 +0200116 static int long_option = 0;
Andreas Eversberg81e895b2012-07-06 08:24:53 +0200117 static const struct option long_options[] = {
118 { "help", 0, 0, 'h' },
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200119 { "config-file", 1, 0, 'c' },
Andreas Eversberg1944bd52012-07-06 09:32:39 +0200120 { "mcc", 1, 0, 'm' },
121 { "mnc", 1, 0, 'n' },
Harald Weltee5a09392013-01-17 12:28:16 +0100122 { "version", 0, 0, 'V' },
Andreas Eversberg783aa4b2013-05-05 09:15:11 +0200123 { "realtime", 1, 0, 'r' },
Rafael Dinizf0af1b02019-01-28 22:37:46 -0200124 { "daemonize", 0, 0, 'D' },
Holger Hans Peter Freythera30f4762013-07-11 16:13:38 +0200125 { "exit", 0, 0, 'e' },
Max2efdf692016-02-22 11:39:30 +0100126 { "gsmtap-ip", 1, 0, 'i' },
Vadim Yanitskiy1136fdb2020-10-24 05:14:52 +0700127 { "vty-ref-mode", 1, &long_option, 1 },
128 { "vty-ref-xml", 0, &long_option, 2 },
Andreas Eversberg81e895b2012-07-06 08:24:53 +0200129 { 0, 0, 0, 0 }
130 };
131
Rafael Dinizf0af1b02019-01-28 22:37:46 -0200132 c = getopt_long(argc, argv, "hc:m:n:Vr:De:i:",
Andreas Eversberg81e895b2012-07-06 08:24:53 +0200133 long_options, &option_idx);
134 if (c == -1)
135 break;
136
137 switch (c) {
138 case 'h':
139 print_help();
140 exit(0);
141 break;
Philipp Maier08c50372020-10-09 20:41:17 +0200142 case 0:
Vadim Yanitskiy1136fdb2020-10-24 05:14:52 +0700143 handle_long_options(argv[0], long_option);
144 break;
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200145 case 'c':
Holger Hans Peter Freyther4e8b50c2013-11-11 20:12:30 +0100146 free(config_file);
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200147 config_file = strdup(optarg);
148 config_given = 1;
149 break;
Andreas Eversberg81e895b2012-07-06 08:24:53 +0200150 case 'm':
151 spoof_mcc = atoi(optarg);
152 break;
153 case 'n':
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +0100154 if (osmo_mnc_from_str(optarg, &spoof_mnc, &spoof_mnc_3_digits)) {
155 fprintf(stderr, "Error decoding MNC '%s'\n", optarg);
156 exit(1);
157 }
Andreas Eversberg81e895b2012-07-06 08:24:53 +0200158 break;
Harald Weltee5a09392013-01-17 12:28:16 +0100159 case 'V':
160 print_version(1);
161 exit(0);
162 break;
Max2efdf692016-02-22 11:39:30 +0100163 case 'i':
164 gsmtap_addr = optarg;
165 break;
Andreas Eversberg783aa4b2013-05-05 09:15:11 +0200166 case 'r':
167 rt_prio = atoi(optarg);
168 break;
Rafael Dinizf0af1b02019-01-28 22:37:46 -0200169 case 'D':
170 daemonize = true;
171 break;
Holger Hans Peter Freythera30f4762013-07-11 16:13:38 +0200172 case 'e':
Daniel Willmann6d8884d2014-06-04 18:30:59 +0200173 fprintf(stderr, "Warning: Option '-e' is deprecated!\n");
Holger Hans Peter Freythera30f4762013-07-11 16:13:38 +0200174 break;
Andreas Eversberg81e895b2012-07-06 08:24:53 +0200175 default:
176 fprintf(stderr, "Unknown option '%c'\n", c);
177 exit(0);
178 break;
179 }
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400180 }
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400181}
182
Andreas Eversberge266bd42012-07-13 14:00:21 +0200183void sighandler(int sigset)
184{
Harald Welte45143d22018-05-15 15:24:30 +0200185 if (sigset == SIGPIPE)
Andreas Eversberge266bd42012-07-13 14:00:21 +0200186 return;
187
188 fprintf(stderr, "Signal %d received.\n", sigset);
189
190 switch (sigset) {
191 case SIGINT:
Holger Hans Peter Freyther72075f02013-03-19 18:33:06 +0400192 case SIGTERM:
Andreas Eversberge266bd42012-07-13 14:00:21 +0200193 /* If another signal is received afterwards, the program
194 * is terminated without finishing shutdown process.
195 */
196 signal(SIGINT, SIG_DFL);
Andreas Eversberge266bd42012-07-13 14:00:21 +0200197 signal(SIGTERM, SIG_DFL);
198 signal(SIGPIPE, SIG_DFL);
199 signal(SIGABRT, SIG_DFL);
200 signal(SIGUSR1, SIG_DFL);
201 signal(SIGUSR2, SIG_DFL);
202
203 quit = 1;
204 break;
205 case SIGABRT:
Pau Espin Pedrol58cd1d22020-11-25 17:43:32 +0100206 /* in case of abort, we want to obtain a talloc report and
207 * then run default SIGABRT handler, who will generate coredump
208 * and abort the process. abort() should do this for us after we
209 * return, but program wouldn't exit if an external SIGABRT is
210 * received.
Andreas Eversberge266bd42012-07-13 14:00:21 +0200211 */
Pau Espin Pedrol58cd1d22020-11-25 17:43:32 +0100212 talloc_report_full(tall_pcu_ctx, stderr);
213 signal(SIGABRT, SIG_DFL);
214 raise(SIGABRT);
215 break;
Andreas Eversberge266bd42012-07-13 14:00:21 +0200216 case SIGUSR1:
217 case SIGUSR2:
218 talloc_report_full(tall_pcu_ctx, stderr);
219 break;
220 }
221}
222
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400223int main(int argc, char *argv[])
224{
Andreas Eversberg783aa4b2013-05-05 09:15:11 +0200225 struct sched_param param;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100226 struct gprs_pcu *pcu;
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200227 int rc;
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200228
Neels Hofmeyr42f2d612018-04-01 16:54:40 +0200229 /* tall_pcu_ctx may already have been initialized in bts.cpp during early_init(). */
230 if (!tall_pcu_ctx) {
231 tall_pcu_ctx = talloc_named_const(NULL, 1, "Osmo-PCU context");
232 if (!tall_pcu_ctx)
233 return -ENOMEM;
234 osmo_init_logging2(tall_pcu_ctx, &gprs_log_info);
235 }
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200236
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100237 pcu = gprs_pcu_alloc(tall_pcu_ctx);
238 the_pcu = pcu; /* globally avaialable object */
Aravind Sirsikar50b09702016-08-22 17:21:10 +0530239
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100240 pcu->pcu_sock_path = talloc_strdup(tall_pcu_ctx, PCU_SOCK_DEFAULT);
Pau Espin Pedrolc4178e52017-08-08 15:03:50 +0200241
Neels Hofmeyr78ce5912017-02-08 17:07:31 +0100242 msgb_talloc_ctx_init(tall_pcu_ctx, 0);
Andreas Eversberge266bd42012-07-13 14:00:21 +0200243
Jacob Erlbeck42aba812015-11-10 15:32:00 +0100244 osmo_stats_init(tall_pcu_ctx);
sivasankariee78bf02016-12-15 17:39:14 +0530245 rate_ctr_init(tall_pcu_ctx);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400246
Vadim Yanitskiyd83c8ff2020-02-06 16:46:17 +0700247 pcu_vty_info.tall_ctx = tall_pcu_ctx;
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200248 vty_init(&pcu_vty_info);
Pau Espin Pedrolcd2ac562019-08-05 14:30:44 +0200249 pcu_vty_init();
Pau Espin Pedrolb75c27f2020-08-18 13:43:43 +0200250 osmo_cpu_sched_vty_init(tall_pcu_ctx);
Harald Welte398f60e2020-12-10 13:39:21 +0100251 logging_vty_add_deprecated_subsys(tall_pcu_ctx, "bssgp");
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200252
Andreas Eversberg81e895b2012-07-06 08:24:53 +0200253 handle_options(argc, argv);
254 if ((!!spoof_mcc) + (!!spoof_mnc) == 1) {
255 fprintf(stderr, "--mcc and --mnc must be specified "
256 "together.\n");
257 exit(0);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400258 }
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200259
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100260 pcu->gsmtap = gsmtap_source_init(gsmtap_addr, GSMTAP_UDP_PORT, 1);
Max2efdf692016-02-22 11:39:30 +0100261
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100262 if (pcu->gsmtap)
263 gsmtap_source_add_sink(pcu->gsmtap);
Max2efdf692016-02-22 11:39:30 +0100264 else
265 fprintf(stderr, "Failed to initialize GSMTAP for %s\n", gsmtap_addr);
266
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100267 pcu->nsi = gprs_ns2_instantiate(tall_pcu_ctx, gprs_ns_prim_cb, NULL);
268 if (!pcu->nsi) {
Alexander Couzens5c3783b2019-05-25 05:41:18 +0200269 LOGP(DBSSGP, LOGL_ERROR, "Failed to create NS instance\n");
270 exit(1);
271 }
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100272 bssgp_set_bssgp_callback(gprs_gp_send_cb, pcu->nsi);
Alexander Couzens54211b12021-01-25 20:16:08 +0100273 gprs_ns2_vty_init(pcu->nsi);
Alexander Couzens5c3783b2019-05-25 05:41:18 +0200274
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200275 rc = vty_read_config_file(config_file, NULL);
276 if (rc < 0 && config_given) {
277 fprintf(stderr, "Failed to parse the config file: '%s'\n",
278 config_file);
279 exit(1);
280 }
281 if (rc < 0)
282 fprintf(stderr, "No config file: '%s' Using default config.\n",
283 config_file);
284
Neels Hofmeyrfd9e16c2016-08-23 16:43:05 +0200285 rc = telnet_init_dynif(tall_pcu_ctx, NULL, vty_get_bind_addr(),
286 OSMO_VTY_PORT_PCU);
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200287 if (rc < 0) {
288 fprintf(stderr, "Error initializing telnet\n");
289 exit(1);
290 }
291
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100292 if (!pcu->alloc_algorithm)
293 pcu->alloc_algorithm = alloc_algorithm_dynamic;
Andreas Eversbergf298fa82012-07-13 14:50:57 +0200294
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200295 rc = pcu_l1if_open();
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400296
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200297 if (rc < 0)
298 return rc;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400299
Andreas Eversberge266bd42012-07-13 14:00:21 +0200300 signal(SIGINT, sighandler);
Andreas Eversberge266bd42012-07-13 14:00:21 +0200301 signal(SIGTERM, sighandler);
302 signal(SIGPIPE, sighandler);
303 signal(SIGABRT, sighandler);
304 signal(SIGUSR1, sighandler);
305 signal(SIGUSR2, sighandler);
Harald Welte45143d22018-05-15 15:24:30 +0200306 osmo_init_ignore_signals();
Andreas Eversberge266bd42012-07-13 14:00:21 +0200307
Andreas Eversberg783aa4b2013-05-05 09:15:11 +0200308 /* enable realtime priority for us */
309 if (rt_prio != -1) {
310 memset(&param, 0, sizeof(param));
311 param.sched_priority = rt_prio;
312 rc = sched_setscheduler(getpid(), SCHED_RR, &param);
313 if (rc != 0) {
314 fprintf(stderr, "Setting SCHED_RR priority(%d) failed: %s\n",
315 param.sched_priority, strerror(errno));
316 exit(1);
317 }
318 }
319
Rafael Dinizf0af1b02019-01-28 22:37:46 -0200320 if (daemonize) {
321 rc = osmo_daemonize();
322 if (rc < 0) {
323 perror("Error during daemonize");
324 exit(1);
325 }
326 }
327
Andreas Eversberge266bd42012-07-13 14:00:21 +0200328 while (!quit) {
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400329 osmo_select_main(0);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400330 }
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400331
Andreas Eversberge266bd42012-07-13 14:00:21 +0200332 telnet_exit();
333
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200334 pcu_l1if_close();
Andreas Eversberge266bd42012-07-13 14:00:21 +0200335
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100336 TALLOC_FREE(the_pcu);
Andreas Eversberge266bd42012-07-13 14:00:21 +0200337 talloc_report_full(tall_pcu_ctx, stderr);
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200338 talloc_free(tall_pcu_ctx);
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200339
340 return 0;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400341}