blob: 79928530df49b1f22a541230685761e9dd6e7f23 [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"
Vadim Yanitskiy35668a22020-10-24 05:10:30 +070077 " -D --daemonize Fork the process into a background daemon\n"
78 " -i --gsmtap-ip The destination IP used for GSMTAP\n"
Vadim Yanitskiy1136fdb2020-10-24 05:14:52 +070079 "\nVTY reference generation:\n"
80 " --vty-ref-mode MODE VTY reference generation mode (e.g. 'expert').\n"
81 " --vty-ref-xml Generate the VTY reference XML output and exit.\n"
Andreas Eversberg81e895b2012-07-06 08:24:53 +020082 );
83}
84
Vadim Yanitskiy1136fdb2020-10-24 05:14:52 +070085static void handle_long_options(const char *prog_name, const int long_option)
86{
87 static int vty_ref_mode = VTY_REF_GEN_MODE_DEFAULT;
88
89 switch (long_option) {
90 case 1:
91 vty_ref_mode = get_string_value(vty_ref_gen_mode_names, optarg);
92 if (vty_ref_mode < 0) {
93 fprintf(stderr, "%s: Unknown VTY reference generation "
94 "mode '%s'\n", prog_name, optarg);
95 exit(2);
96 }
97 break;
98 case 2:
99 fprintf(stderr, "Generating the VTY reference in mode '%s' (%s)\n",
100 get_value_string(vty_ref_gen_mode_names, vty_ref_mode),
101 get_value_string(vty_ref_gen_mode_desc, vty_ref_mode));
102 vty_dump_xml_ref_mode(stdout, (enum vty_ref_gen_mode) vty_ref_mode);
103 exit(0);
104 default:
105 fprintf(stderr, "%s: error parsing cmdline options\n", prog_name);
106 exit(2);
107 }
108}
109
Andreas Eversberg81e895b2012-07-06 08:24:53 +0200110/* FIXME: finally get some option parsing code into libosmocore */
111static void handle_options(int argc, char **argv)
112{
113 while (1) {
114 int option_idx = 0, c;
Philipp Maier08c50372020-10-09 20:41:17 +0200115 static int long_option = 0;
Andreas Eversberg81e895b2012-07-06 08:24:53 +0200116 static const struct option long_options[] = {
117 { "help", 0, 0, 'h' },
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200118 { "config-file", 1, 0, 'c' },
Andreas Eversberg1944bd52012-07-06 09:32:39 +0200119 { "mcc", 1, 0, 'm' },
120 { "mnc", 1, 0, 'n' },
Harald Weltee5a09392013-01-17 12:28:16 +0100121 { "version", 0, 0, 'V' },
Andreas Eversberg783aa4b2013-05-05 09:15:11 +0200122 { "realtime", 1, 0, 'r' },
Rafael Dinizf0af1b02019-01-28 22:37:46 -0200123 { "daemonize", 0, 0, 'D' },
Holger Hans Peter Freythera30f4762013-07-11 16:13:38 +0200124 { "exit", 0, 0, 'e' },
Max2efdf692016-02-22 11:39:30 +0100125 { "gsmtap-ip", 1, 0, 'i' },
Vadim Yanitskiy1136fdb2020-10-24 05:14:52 +0700126 { "vty-ref-mode", 1, &long_option, 1 },
127 { "vty-ref-xml", 0, &long_option, 2 },
Andreas Eversberg81e895b2012-07-06 08:24:53 +0200128 { 0, 0, 0, 0 }
129 };
130
Rafael Dinizf0af1b02019-01-28 22:37:46 -0200131 c = getopt_long(argc, argv, "hc:m:n:Vr:De:i:",
Andreas Eversberg81e895b2012-07-06 08:24:53 +0200132 long_options, &option_idx);
133 if (c == -1)
134 break;
135
136 switch (c) {
137 case 'h':
138 print_help();
139 exit(0);
140 break;
Philipp Maier08c50372020-10-09 20:41:17 +0200141 case 0:
Vadim Yanitskiy1136fdb2020-10-24 05:14:52 +0700142 handle_long_options(argv[0], long_option);
143 break;
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200144 case 'c':
Holger Hans Peter Freyther4e8b50c2013-11-11 20:12:30 +0100145 free(config_file);
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200146 config_file = strdup(optarg);
147 config_given = 1;
148 break;
Andreas Eversberg81e895b2012-07-06 08:24:53 +0200149 case 'm':
150 spoof_mcc = atoi(optarg);
151 break;
152 case 'n':
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +0100153 if (osmo_mnc_from_str(optarg, &spoof_mnc, &spoof_mnc_3_digits)) {
154 fprintf(stderr, "Error decoding MNC '%s'\n", optarg);
155 exit(1);
156 }
Andreas Eversberg81e895b2012-07-06 08:24:53 +0200157 break;
Harald Weltee5a09392013-01-17 12:28:16 +0100158 case 'V':
159 print_version(1);
160 exit(0);
161 break;
Max2efdf692016-02-22 11:39:30 +0100162 case 'i':
163 gsmtap_addr = optarg;
164 break;
Andreas Eversberg783aa4b2013-05-05 09:15:11 +0200165 case 'r':
166 rt_prio = atoi(optarg);
Pau Espin Pedrolafd93932021-11-12 13:22:34 +0100167 fprintf(stderr, "Command line argument '-r' is deprecated, use VTY "
168 "cpu-sched node setting 'policy rr %d' instead.\n", rt_prio);
Andreas Eversberg783aa4b2013-05-05 09:15:11 +0200169 break;
Rafael Dinizf0af1b02019-01-28 22:37:46 -0200170 case 'D':
171 daemonize = true;
172 break;
Holger Hans Peter Freythera30f4762013-07-11 16:13:38 +0200173 case 'e':
Daniel Willmann6d8884d2014-06-04 18:30:59 +0200174 fprintf(stderr, "Warning: Option '-e' is deprecated!\n");
Holger Hans Peter Freythera30f4762013-07-11 16:13:38 +0200175 break;
Andreas Eversberg81e895b2012-07-06 08:24:53 +0200176 default:
177 fprintf(stderr, "Unknown option '%c'\n", c);
178 exit(0);
179 break;
180 }
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400181 }
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400182}
183
Andreas Eversberge266bd42012-07-13 14:00:21 +0200184void sighandler(int sigset)
185{
Harald Welte45143d22018-05-15 15:24:30 +0200186 if (sigset == SIGPIPE)
Andreas Eversberge266bd42012-07-13 14:00:21 +0200187 return;
188
189 fprintf(stderr, "Signal %d received.\n", sigset);
190
191 switch (sigset) {
192 case SIGINT:
Holger Hans Peter Freyther72075f02013-03-19 18:33:06 +0400193 case SIGTERM:
Andreas Eversberge266bd42012-07-13 14:00:21 +0200194 /* If another signal is received afterwards, the program
195 * is terminated without finishing shutdown process.
196 */
197 signal(SIGINT, SIG_DFL);
Andreas Eversberge266bd42012-07-13 14:00:21 +0200198 signal(SIGTERM, SIG_DFL);
199 signal(SIGPIPE, SIG_DFL);
200 signal(SIGABRT, SIG_DFL);
201 signal(SIGUSR1, SIG_DFL);
202 signal(SIGUSR2, SIG_DFL);
203
204 quit = 1;
205 break;
206 case SIGABRT:
Pau Espin Pedrol58cd1d22020-11-25 17:43:32 +0100207 /* in case of abort, we want to obtain a talloc report and
208 * then run default SIGABRT handler, who will generate coredump
209 * and abort the process. abort() should do this for us after we
210 * return, but program wouldn't exit if an external SIGABRT is
211 * received.
Andreas Eversberge266bd42012-07-13 14:00:21 +0200212 */
Pau Espin Pedrol58cd1d22020-11-25 17:43:32 +0100213 talloc_report_full(tall_pcu_ctx, stderr);
214 signal(SIGABRT, SIG_DFL);
215 raise(SIGABRT);
216 break;
Andreas Eversberge266bd42012-07-13 14:00:21 +0200217 case SIGUSR1:
218 case SIGUSR2:
219 talloc_report_full(tall_pcu_ctx, stderr);
220 break;
221 }
222}
223
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400224int main(int argc, char *argv[])
225{
Andreas Eversberg783aa4b2013-05-05 09:15:11 +0200226 struct sched_param param;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100227 struct gprs_pcu *pcu;
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200228 int rc;
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200229
Neels Hofmeyr42f2d612018-04-01 16:54:40 +0200230 /* tall_pcu_ctx may already have been initialized in bts.cpp during early_init(). */
231 if (!tall_pcu_ctx) {
232 tall_pcu_ctx = talloc_named_const(NULL, 1, "Osmo-PCU context");
233 if (!tall_pcu_ctx)
234 return -ENOMEM;
235 osmo_init_logging2(tall_pcu_ctx, &gprs_log_info);
236 }
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200237
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100238 pcu = gprs_pcu_alloc(tall_pcu_ctx);
239 the_pcu = pcu; /* globally avaialable object */
Aravind Sirsikar50b09702016-08-22 17:21:10 +0530240
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100241 pcu->pcu_sock_path = talloc_strdup(tall_pcu_ctx, PCU_SOCK_DEFAULT);
Pau Espin Pedrolc4178e52017-08-08 15:03:50 +0200242
Neels Hofmeyr78ce5912017-02-08 17:07:31 +0100243 msgb_talloc_ctx_init(tall_pcu_ctx, 0);
Andreas Eversberge266bd42012-07-13 14:00:21 +0200244
Jacob Erlbeck42aba812015-11-10 15:32:00 +0100245 osmo_stats_init(tall_pcu_ctx);
sivasankariee78bf02016-12-15 17:39:14 +0530246 rate_ctr_init(tall_pcu_ctx);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400247
Vadim Yanitskiyd83c8ff2020-02-06 16:46:17 +0700248 pcu_vty_info.tall_ctx = tall_pcu_ctx;
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200249 vty_init(&pcu_vty_info);
Pau Espin Pedrolcd2ac562019-08-05 14:30:44 +0200250 pcu_vty_init();
Pau Espin Pedrolb75c27f2020-08-18 13:43:43 +0200251 osmo_cpu_sched_vty_init(tall_pcu_ctx);
Harald Welte398f60e2020-12-10 13:39:21 +0100252 logging_vty_add_deprecated_subsys(tall_pcu_ctx, "bssgp");
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200253
Andreas Eversberg81e895b2012-07-06 08:24:53 +0200254 handle_options(argc, argv);
255 if ((!!spoof_mcc) + (!!spoof_mnc) == 1) {
256 fprintf(stderr, "--mcc and --mnc must be specified "
257 "together.\n");
258 exit(0);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400259 }
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200260
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100261 pcu->gsmtap = gsmtap_source_init(gsmtap_addr, GSMTAP_UDP_PORT, 1);
Max2efdf692016-02-22 11:39:30 +0100262
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100263 if (pcu->gsmtap)
264 gsmtap_source_add_sink(pcu->gsmtap);
Max2efdf692016-02-22 11:39:30 +0100265 else
266 fprintf(stderr, "Failed to initialize GSMTAP for %s\n", gsmtap_addr);
267
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100268 pcu->nsi = gprs_ns2_instantiate(tall_pcu_ctx, gprs_ns_prim_cb, NULL);
269 if (!pcu->nsi) {
Alexander Couzens5c3783b2019-05-25 05:41:18 +0200270 LOGP(DBSSGP, LOGL_ERROR, "Failed to create NS instance\n");
271 exit(1);
272 }
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100273 bssgp_set_bssgp_callback(gprs_gp_send_cb, pcu->nsi);
Alexander Couzens54211b12021-01-25 20:16:08 +0100274 gprs_ns2_vty_init(pcu->nsi);
Alexander Couzens5c3783b2019-05-25 05:41:18 +0200275
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200276 rc = vty_read_config_file(config_file, NULL);
277 if (rc < 0 && config_given) {
278 fprintf(stderr, "Failed to parse the config file: '%s'\n",
279 config_file);
280 exit(1);
281 }
282 if (rc < 0)
283 fprintf(stderr, "No config file: '%s' Using default config.\n",
284 config_file);
285
Neels Hofmeyrfd9e16c2016-08-23 16:43:05 +0200286 rc = telnet_init_dynif(tall_pcu_ctx, NULL, vty_get_bind_addr(),
287 OSMO_VTY_PORT_PCU);
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200288 if (rc < 0) {
289 fprintf(stderr, "Error initializing telnet\n");
290 exit(1);
291 }
292
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100293 if (!pcu->alloc_algorithm)
294 pcu->alloc_algorithm = alloc_algorithm_dynamic;
Andreas Eversbergf298fa82012-07-13 14:50:57 +0200295
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200296 rc = pcu_l1if_open();
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400297
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200298 if (rc < 0)
299 return rc;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400300
Andreas Eversberge266bd42012-07-13 14:00:21 +0200301 signal(SIGINT, sighandler);
Andreas Eversberge266bd42012-07-13 14:00:21 +0200302 signal(SIGTERM, sighandler);
303 signal(SIGPIPE, sighandler);
304 signal(SIGABRT, sighandler);
305 signal(SIGUSR1, sighandler);
306 signal(SIGUSR2, sighandler);
Harald Welte45143d22018-05-15 15:24:30 +0200307 osmo_init_ignore_signals();
Andreas Eversberge266bd42012-07-13 14:00:21 +0200308
Andreas Eversberg783aa4b2013-05-05 09:15:11 +0200309 /* enable realtime priority for us */
310 if (rt_prio != -1) {
311 memset(&param, 0, sizeof(param));
312 param.sched_priority = rt_prio;
313 rc = sched_setscheduler(getpid(), SCHED_RR, &param);
314 if (rc != 0) {
315 fprintf(stderr, "Setting SCHED_RR priority(%d) failed: %s\n",
316 param.sched_priority, strerror(errno));
317 exit(1);
318 }
319 }
320
Rafael Dinizf0af1b02019-01-28 22:37:46 -0200321 if (daemonize) {
322 rc = osmo_daemonize();
323 if (rc < 0) {
324 perror("Error during daemonize");
325 exit(1);
326 }
327 }
328
Andreas Eversberge266bd42012-07-13 14:00:21 +0200329 while (!quit) {
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400330 osmo_select_main(0);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400331 }
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400332
Andreas Eversberge266bd42012-07-13 14:00:21 +0200333 telnet_exit();
334
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200335 pcu_l1if_close();
Andreas Eversberge266bd42012-07-13 14:00:21 +0200336
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100337 TALLOC_FREE(the_pcu);
Andreas Eversberge266bd42012-07-13 14:00:21 +0200338 talloc_report_full(tall_pcu_ctx, stderr);
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200339 talloc_free(tall_pcu_ctx);
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200340
341 return 0;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400342}