blob: fe391b99e59ea459914b11f01c3ee5ac046c0037 [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;
Pau Espin Pedrol13866f02021-11-12 13:42:03 +010067static const char *gsmtap_addr;
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"
Vadim Yanitskiy35668a22020-10-24 05:10:30 +070077 " -D --daemonize Fork the process into a background daemon\n"
Vadim Yanitskiy1136fdb2020-10-24 05:14:52 +070078 "\nVTY reference generation:\n"
79 " --vty-ref-mode MODE VTY reference generation mode (e.g. 'expert').\n"
80 " --vty-ref-xml Generate the VTY reference XML output and exit.\n"
Andreas Eversberg81e895b2012-07-06 08:24:53 +020081 );
82}
83
Vadim Yanitskiy1136fdb2020-10-24 05:14:52 +070084static void handle_long_options(const char *prog_name, const int long_option)
85{
86 static int vty_ref_mode = VTY_REF_GEN_MODE_DEFAULT;
87
88 switch (long_option) {
89 case 1:
90 vty_ref_mode = get_string_value(vty_ref_gen_mode_names, optarg);
91 if (vty_ref_mode < 0) {
92 fprintf(stderr, "%s: Unknown VTY reference generation "
93 "mode '%s'\n", prog_name, optarg);
94 exit(2);
95 }
96 break;
97 case 2:
98 fprintf(stderr, "Generating the VTY reference in mode '%s' (%s)\n",
99 get_value_string(vty_ref_gen_mode_names, vty_ref_mode),
100 get_value_string(vty_ref_gen_mode_desc, vty_ref_mode));
101 vty_dump_xml_ref_mode(stdout, (enum vty_ref_gen_mode) vty_ref_mode);
102 exit(0);
103 default:
104 fprintf(stderr, "%s: error parsing cmdline options\n", prog_name);
105 exit(2);
106 }
107}
108
Andreas Eversberg81e895b2012-07-06 08:24:53 +0200109/* FIXME: finally get some option parsing code into libosmocore */
110static void handle_options(int argc, char **argv)
111{
112 while (1) {
113 int option_idx = 0, c;
Philipp Maier08c50372020-10-09 20:41:17 +0200114 static int long_option = 0;
Andreas Eversberg81e895b2012-07-06 08:24:53 +0200115 static const struct option long_options[] = {
116 { "help", 0, 0, 'h' },
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200117 { "config-file", 1, 0, 'c' },
Andreas Eversberg1944bd52012-07-06 09:32:39 +0200118 { "mcc", 1, 0, 'm' },
119 { "mnc", 1, 0, 'n' },
Harald Weltee5a09392013-01-17 12:28:16 +0100120 { "version", 0, 0, 'V' },
Andreas Eversberg783aa4b2013-05-05 09:15:11 +0200121 { "realtime", 1, 0, 'r' },
Rafael Dinizf0af1b02019-01-28 22:37:46 -0200122 { "daemonize", 0, 0, 'D' },
Holger Hans Peter Freythera30f4762013-07-11 16:13:38 +0200123 { "exit", 0, 0, 'e' },
Max2efdf692016-02-22 11:39:30 +0100124 { "gsmtap-ip", 1, 0, 'i' },
Vadim Yanitskiy1136fdb2020-10-24 05:14:52 +0700125 { "vty-ref-mode", 1, &long_option, 1 },
126 { "vty-ref-xml", 0, &long_option, 2 },
Andreas Eversberg81e895b2012-07-06 08:24:53 +0200127 { 0, 0, 0, 0 }
128 };
129
Rafael Dinizf0af1b02019-01-28 22:37:46 -0200130 c = getopt_long(argc, argv, "hc:m:n:Vr:De:i:",
Andreas Eversberg81e895b2012-07-06 08:24:53 +0200131 long_options, &option_idx);
132 if (c == -1)
133 break;
134
135 switch (c) {
136 case 'h':
137 print_help();
138 exit(0);
139 break;
Philipp Maier08c50372020-10-09 20:41:17 +0200140 case 0:
Vadim Yanitskiy1136fdb2020-10-24 05:14:52 +0700141 handle_long_options(argv[0], long_option);
142 break;
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200143 case 'c':
Holger Hans Peter Freyther4e8b50c2013-11-11 20:12:30 +0100144 free(config_file);
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200145 config_file = strdup(optarg);
146 config_given = 1;
147 break;
Andreas Eversberg81e895b2012-07-06 08:24:53 +0200148 case 'm':
149 spoof_mcc = atoi(optarg);
150 break;
151 case 'n':
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +0100152 if (osmo_mnc_from_str(optarg, &spoof_mnc, &spoof_mnc_3_digits)) {
153 fprintf(stderr, "Error decoding MNC '%s'\n", optarg);
154 exit(1);
155 }
Andreas Eversberg81e895b2012-07-06 08:24:53 +0200156 break;
Harald Weltee5a09392013-01-17 12:28:16 +0100157 case 'V':
158 print_version(1);
159 exit(0);
160 break;
Max2efdf692016-02-22 11:39:30 +0100161 case 'i':
162 gsmtap_addr = optarg;
Pau Espin Pedrol13866f02021-11-12 13:42:03 +0100163 fprintf(stderr, "Command line argument '-i' is deprecated, use VTY "
164 "parameter 'gsmtap-remote-host %s' instead.\n", gsmtap_addr);
Max2efdf692016-02-22 11:39:30 +0100165 break;
Andreas Eversberg783aa4b2013-05-05 09:15:11 +0200166 case 'r':
167 rt_prio = atoi(optarg);
Pau Espin Pedrolafd93932021-11-12 13:22:34 +0100168 fprintf(stderr, "Command line argument '-r' is deprecated, use VTY "
169 "cpu-sched node setting 'policy rr %d' instead.\n", rt_prio);
Andreas Eversberg783aa4b2013-05-05 09:15:11 +0200170 break;
Rafael Dinizf0af1b02019-01-28 22:37:46 -0200171 case 'D':
172 daemonize = true;
173 break;
Holger Hans Peter Freythera30f4762013-07-11 16:13:38 +0200174 case 'e':
Daniel Willmann6d8884d2014-06-04 18:30:59 +0200175 fprintf(stderr, "Warning: Option '-e' is deprecated!\n");
Holger Hans Peter Freythera30f4762013-07-11 16:13:38 +0200176 break;
Andreas Eversberg81e895b2012-07-06 08:24:53 +0200177 default:
178 fprintf(stderr, "Unknown option '%c'\n", c);
179 exit(0);
180 break;
181 }
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400182 }
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400183}
184
Andreas Eversberge266bd42012-07-13 14:00:21 +0200185void sighandler(int sigset)
186{
Harald Welte45143d22018-05-15 15:24:30 +0200187 if (sigset == SIGPIPE)
Andreas Eversberge266bd42012-07-13 14:00:21 +0200188 return;
189
190 fprintf(stderr, "Signal %d received.\n", sigset);
191
192 switch (sigset) {
193 case SIGINT:
Holger Hans Peter Freyther72075f02013-03-19 18:33:06 +0400194 case SIGTERM:
Andreas Eversberge266bd42012-07-13 14:00:21 +0200195 /* If another signal is received afterwards, the program
196 * is terminated without finishing shutdown process.
197 */
198 signal(SIGINT, SIG_DFL);
Andreas Eversberge266bd42012-07-13 14:00:21 +0200199 signal(SIGTERM, SIG_DFL);
200 signal(SIGPIPE, SIG_DFL);
201 signal(SIGABRT, SIG_DFL);
202 signal(SIGUSR1, SIG_DFL);
203 signal(SIGUSR2, SIG_DFL);
204
205 quit = 1;
206 break;
207 case SIGABRT:
Pau Espin Pedrol58cd1d22020-11-25 17:43:32 +0100208 /* in case of abort, we want to obtain a talloc report and
209 * then run default SIGABRT handler, who will generate coredump
210 * and abort the process. abort() should do this for us after we
211 * return, but program wouldn't exit if an external SIGABRT is
212 * received.
Andreas Eversberge266bd42012-07-13 14:00:21 +0200213 */
Pau Espin Pedrol58cd1d22020-11-25 17:43:32 +0100214 talloc_report_full(tall_pcu_ctx, stderr);
215 signal(SIGABRT, SIG_DFL);
216 raise(SIGABRT);
217 break;
Andreas Eversberge266bd42012-07-13 14:00:21 +0200218 case SIGUSR1:
219 case SIGUSR2:
220 talloc_report_full(tall_pcu_ctx, stderr);
221 break;
222 }
223}
224
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400225int main(int argc, char *argv[])
226{
Andreas Eversberg783aa4b2013-05-05 09:15:11 +0200227 struct sched_param param;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100228 struct gprs_pcu *pcu;
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200229 int rc;
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200230
Neels Hofmeyr42f2d612018-04-01 16:54:40 +0200231 /* tall_pcu_ctx may already have been initialized in bts.cpp during early_init(). */
232 if (!tall_pcu_ctx) {
233 tall_pcu_ctx = talloc_named_const(NULL, 1, "Osmo-PCU context");
234 if (!tall_pcu_ctx)
235 return -ENOMEM;
236 osmo_init_logging2(tall_pcu_ctx, &gprs_log_info);
237 }
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200238
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100239 pcu = gprs_pcu_alloc(tall_pcu_ctx);
240 the_pcu = pcu; /* globally avaialable object */
Aravind Sirsikar50b09702016-08-22 17:21:10 +0530241
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100242 pcu->pcu_sock_path = talloc_strdup(tall_pcu_ctx, PCU_SOCK_DEFAULT);
Pau Espin Pedrolc4178e52017-08-08 15:03:50 +0200243
Neels Hofmeyr78ce5912017-02-08 17:07:31 +0100244 msgb_talloc_ctx_init(tall_pcu_ctx, 0);
Andreas Eversberge266bd42012-07-13 14:00:21 +0200245
Jacob Erlbeck42aba812015-11-10 15:32:00 +0100246 osmo_stats_init(tall_pcu_ctx);
sivasankariee78bf02016-12-15 17:39:14 +0530247 rate_ctr_init(tall_pcu_ctx);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400248
Vadim Yanitskiyd83c8ff2020-02-06 16:46:17 +0700249 pcu_vty_info.tall_ctx = tall_pcu_ctx;
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200250 vty_init(&pcu_vty_info);
Pau Espin Pedrolcd2ac562019-08-05 14:30:44 +0200251 pcu_vty_init();
Pau Espin Pedrolb75c27f2020-08-18 13:43:43 +0200252 osmo_cpu_sched_vty_init(tall_pcu_ctx);
Harald Welte398f60e2020-12-10 13:39:21 +0100253 logging_vty_add_deprecated_subsys(tall_pcu_ctx, "bssgp");
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200254
Andreas Eversberg81e895b2012-07-06 08:24:53 +0200255 handle_options(argc, argv);
256 if ((!!spoof_mcc) + (!!spoof_mnc) == 1) {
257 fprintf(stderr, "--mcc and --mnc must be specified "
258 "together.\n");
259 exit(0);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400260 }
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200261
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100262 pcu->nsi = gprs_ns2_instantiate(tall_pcu_ctx, gprs_ns_prim_cb, NULL);
263 if (!pcu->nsi) {
Alexander Couzens5c3783b2019-05-25 05:41:18 +0200264 LOGP(DBSSGP, LOGL_ERROR, "Failed to create NS instance\n");
265 exit(1);
266 }
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100267 bssgp_set_bssgp_callback(gprs_gp_send_cb, pcu->nsi);
Alexander Couzens54211b12021-01-25 20:16:08 +0100268 gprs_ns2_vty_init(pcu->nsi);
Alexander Couzens5c3783b2019-05-25 05:41:18 +0200269
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200270 rc = vty_read_config_file(config_file, NULL);
271 if (rc < 0 && config_given) {
272 fprintf(stderr, "Failed to parse the config file: '%s'\n",
273 config_file);
274 exit(1);
275 }
276 if (rc < 0)
277 fprintf(stderr, "No config file: '%s' Using default config.\n",
278 config_file);
279
Pau Espin Pedrol13866f02021-11-12 13:42:03 +0100280 /* Accept a GSMTAP host from VTY config, but a commandline option overrides that. */
281 if (gsmtap_addr) {
282 if (pcu->gsmtap_remote_host != NULL) {
283 LOGP(DLGLOBAL, LOGL_NOTICE,
284 "Command line argument '-i %s' overrides "
285 "'gsmtap-remote-host %s' from the config file\n",
286 gsmtap_addr, pcu->gsmtap_remote_host);
287 talloc_free(pcu->gsmtap_remote_host);
288 }
289 pcu->gsmtap_remote_host = talloc_strdup(pcu, gsmtap_addr);
290 }
291
292 if (pcu->gsmtap_remote_host) {
293 LOGP(DLGLOBAL, LOGL_NOTICE,
294 "Setting up GSMTAP Um forwarding to '%s:%u'\n",
295 pcu->gsmtap_remote_host, GSMTAP_UDP_PORT);
296 pcu->gsmtap = gsmtap_source_init(pcu->gsmtap_remote_host,
297 GSMTAP_UDP_PORT, 1);
298 if (pcu->gsmtap == NULL) {
299 fprintf(stderr, "Failed during gsmtap_source_init()\n");
300 exit(1);
301 }
302 gsmtap_source_add_sink(pcu->gsmtap);
303 }
304
Neels Hofmeyrfd9e16c2016-08-23 16:43:05 +0200305 rc = telnet_init_dynif(tall_pcu_ctx, NULL, vty_get_bind_addr(),
306 OSMO_VTY_PORT_PCU);
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200307 if (rc < 0) {
308 fprintf(stderr, "Error initializing telnet\n");
309 exit(1);
310 }
311
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100312 if (!pcu->alloc_algorithm)
313 pcu->alloc_algorithm = alloc_algorithm_dynamic;
Andreas Eversbergf298fa82012-07-13 14:50:57 +0200314
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200315 rc = pcu_l1if_open();
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400316
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200317 if (rc < 0)
318 return rc;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400319
Andreas Eversberge266bd42012-07-13 14:00:21 +0200320 signal(SIGINT, sighandler);
Andreas Eversberge266bd42012-07-13 14:00:21 +0200321 signal(SIGTERM, sighandler);
322 signal(SIGPIPE, sighandler);
323 signal(SIGABRT, sighandler);
324 signal(SIGUSR1, sighandler);
325 signal(SIGUSR2, sighandler);
Harald Welte45143d22018-05-15 15:24:30 +0200326 osmo_init_ignore_signals();
Andreas Eversberge266bd42012-07-13 14:00:21 +0200327
Andreas Eversberg783aa4b2013-05-05 09:15:11 +0200328 /* enable realtime priority for us */
329 if (rt_prio != -1) {
330 memset(&param, 0, sizeof(param));
331 param.sched_priority = rt_prio;
332 rc = sched_setscheduler(getpid(), SCHED_RR, &param);
333 if (rc != 0) {
334 fprintf(stderr, "Setting SCHED_RR priority(%d) failed: %s\n",
335 param.sched_priority, strerror(errno));
336 exit(1);
337 }
338 }
339
Rafael Dinizf0af1b02019-01-28 22:37:46 -0200340 if (daemonize) {
341 rc = osmo_daemonize();
342 if (rc < 0) {
343 perror("Error during daemonize");
344 exit(1);
345 }
346 }
347
Andreas Eversberge266bd42012-07-13 14:00:21 +0200348 while (!quit) {
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400349 osmo_select_main(0);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400350 }
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400351
Andreas Eversberge266bd42012-07-13 14:00:21 +0200352 telnet_exit();
353
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200354 pcu_l1if_close();
Andreas Eversberge266bd42012-07-13 14:00:21 +0200355
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100356 TALLOC_FREE(the_pcu);
Andreas Eversberge266bd42012-07-13 14:00:21 +0200357 talloc_report_full(tall_pcu_ctx, stderr);
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200358 talloc_free(tall_pcu_ctx);
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200359
360 return 0;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400361}