blob: a9a7529cc6677bbfbbd9d135c60dc8227bfab823 [file] [log] [blame]
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +02001/* GTP Hub main program */
2
3/* (C) 2015 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
4 * All Rights Reserved
5 *
6 * Author: Neels Hofmeyr
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include <unistd.h>
23#include <signal.h>
24#include <string.h>
25#include <errno.h>
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +010026#include <inttypes.h>
27#include <sys/stat.h>
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020028
29#define _GNU_SOURCE
30#include <getopt.h>
31
32#include <osmocom/core/signal.h>
33#include <osmocom/core/application.h>
34#include <osmocom/core/logging.h>
35#include <osmocom/core/utils.h>
36#include <osmocom/core/rate_ctr.h>
37
38#include <osmocom/vty/logging.h>
39#include <osmocom/vty/telnet_interface.h>
Neels Hofmeyr03933a42016-02-23 13:26:02 +010040#include <osmocom/vty/ports.h>
Harald Welte731930b2018-02-14 01:02:22 +010041#include <osmocom/vty/misc.h>
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020042
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020043#include <osmocom/sgsn/debug.h>
44#include <osmocom/sgsn/gtphub.h>
45#include <osmocom/sgsn/vty.h>
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020046
47#include "../../bscconfig.h"
48
Max095de012017-10-26 15:27:12 +020049#if BUILD_IU
50#include <osmocom/sigtran/osmo_ss7.h>
51#endif
52
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020053extern void *osmo_gtphub_ctx;
Pau Espin Pedrolb1d1c242018-10-30 17:27:59 +010054void *tall_sgsn_ctx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020055
56const char *gtphub_copyright =
57 "Copyright (C) 2015 sysmocom s.f.m.c GmbH <info@sysmocom.de>\r\n"
58 "License AGPLv3+: GNU AGPL version 2 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
59 "This is free software: you are free to change and redistribute it.\r\n"
60 "There is NO WARRANTY, to the extent permitted by law.\r\n";
61
62static struct log_info_cat gtphub_categories[] = {
63 [DGTPHUB] = {
64 .name = "DGTPHUB",
65 .description = "GTP Hub",
66 .color = "\033[1;33m",
Neels Hofmeyrd9b1d492015-11-18 18:11:09 +010067 .enabled = 1,
Neels Hofmeyr69da1d42016-02-23 13:28:04 +010068 .loglevel = LOGL_INFO,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020069 },
70};
71
72int gtphub_log_filter_fn(const struct log_context *ctx,
73 struct log_target *tar)
74{
75 return 0;
76}
77
78static const struct log_info gtphub_log_info = {
79 .filter_fn = gtphub_log_filter_fn,
80 .cat = gtphub_categories,
81 .num_cat = ARRAY_SIZE(gtphub_categories),
82};
83
84void log_cfg(struct gtphub_cfg *cfg)
85{
Neels Hofmeyra9905a52015-11-29 23:49:48 +010086 int side_idx, plane_idx;
87 for_each_side_and_plane(side_idx, plane_idx) {
Neels Hofmeyr08550082015-11-30 12:19:50 +010088 struct gtphub_cfg_addr *a;
Neels Hofmeyra9905a52015-11-29 23:49:48 +010089 a = &cfg->to_gsns[side_idx][plane_idx].bind;
90 LOGP(DGTPHUB, LOGL_NOTICE,
91 "to-%ss bind, %s: %s port %d\n",
92 gtphub_side_idx_names[side_idx],
93 gtphub_plane_idx_names[plane_idx],
94 a->addr_str, a->port);
95 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020096}
97
Pau Espin Pedrol7ffc6602020-11-25 18:03:21 +010098static void signal_handler(int signum)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020099{
Pau Espin Pedrol7ffc6602020-11-25 18:03:21 +0100100 fprintf(stdout, "signal %d received\n", signum);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200101
Pau Espin Pedrol7ffc6602020-11-25 18:03:21 +0100102 switch (signum) {
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200103 case SIGINT:
Harald Welte6f750e62017-08-20 20:50:06 +0200104 case SIGTERM:
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200105 osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL);
106 sleep(1);
107 exit(0);
108 break;
109 case SIGABRT:
Pau Espin Pedrol7ffc6602020-11-25 18:03:21 +0100110 /* in case of abort, we want to obtain a talloc report and
111 * then run default SIGABRT handler, who will generate coredump
112 * and abort the process. abort() should do this for us after we
113 * return, but program wouldn't exit if an external SIGABRT is
114 * received.
115 */
116 talloc_report_full(osmo_gtphub_ctx, stderr);
117 signal(SIGABRT, SIG_DFL);
118 raise(SIGABRT);
119 break;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200120 case SIGUSR1:
121 case SIGUSR2:
122 talloc_report_full(osmo_gtphub_ctx, stderr);
123 break;
124 default:
125 break;
126 }
127}
128
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200129static struct vty_app_info vty_info = {
130 .name = "OsmoGTPhub",
131 .version = PACKAGE_VERSION,
Philipp Maier4a547a12017-10-20 11:27:26 +0200132#if BUILD_IU
Daniel Willmann62fa6192020-11-03 09:07:45 +0100133 .go_parent_cb = osmo_ss7_vty_go_parent,
Philipp Maier4a547a12017-10-20 11:27:26 +0200134#endif
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200135};
136
137struct cmdline_cfg {
138 const char *config_file;
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +0100139 const char *restart_counter_file;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200140 int daemonize;
141};
142
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +0100143static uint8_t next_restart_count(const char *path)
144{
145 int umask_was = umask(022);
146
147 uint8_t counter = 0;
148
149 FILE *f = fopen(path, "r");
150 if (f) {
151 int rc = fscanf(f, "%hhu", &counter);
152
153 if (rc != 1)
154 goto failed_to_read;
155
156 char c;
157 while (fread(&c, 1, 1, f) > 0) {
158 switch (c) {
159 case ' ':
160 case '\t':
161 case '\n':
162 case '\r':
163 break;
164 default:
165 goto failed_to_read;
166 }
167 }
168 fclose(f);
169 }
170
171 counter ++;
172
173 f = fopen(path, "w");
174 if (!f)
175 goto failed_to_write;
176 if (fprintf(f, "%" PRIu8 "\n", counter) < 2)
177 goto failed_to_write;
Holger Hans Peter Freytherde766612016-01-23 10:28:09 +0100178 if (fclose(f)) {
179 f = NULL;
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +0100180 goto failed_to_write;
Holger Hans Peter Freytherde766612016-01-23 10:28:09 +0100181 }
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +0100182
183 umask(umask_was);
184
185 LOGP(DGTPHUB, LOGL_NOTICE, "Restarted with counter %hhu\n", counter);
186 return counter;
187
188failed_to_read:
189 fclose(f);
190 umask(umask_was);
191 LOGP(DGTPHUB, LOGL_FATAL, "Restart counter file cannot be parsed:"
192 " %s\n", path);
193 exit(1);
194
195failed_to_write:
196 if (f)
197 fclose(f);
198 umask(umask_was);
199 LOGP(DGTPHUB, LOGL_FATAL, "Restart counter file cannot be written:"
200 " %s\n", path);
201 exit(1);
202}
203
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200204static void print_help(struct cmdline_cfg *ccfg)
205{
206 printf("gtphub commandline options\n");
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +0100207 printf(" -h,--help This text.\n");
208 printf(" -D,--daemonize Fork the process into a background daemon.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200209 printf(" -d,--debug <cat> Enable Debugging for this category.\n");
210 printf(" Pass '-d list' to get a category listing.\n");
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +0100211 printf(" -s,--disable-color\n");
212 printf(" -c,--config-file <path> The config file to use [%s].\n",
213 ccfg->config_file);
214 printf(" -e,--log-level <nr> Set a global log level.\n");
215 printf(" -r,--restart-file <path> File for counting restarts [%s].\n",
216 ccfg->restart_counter_file);
Daniel Willmann03a2ed52019-07-26 11:56:31 +0200217 printf(" -V,--version Print the version.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200218}
219
220static void list_categories(void)
221{
Ruben Undheim55fcf112018-09-25 22:59:34 +0200222 printf("Available debug categories:\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200223 int i;
224 for (i = 0; i < gtphub_log_info.num_cat; ++i) {
225 if (!gtphub_log_info.cat[i].name)
226 continue;
227
228 printf("%s\n", gtphub_log_info.cat[i].name);
229 }
230}
231
232static void handle_options(struct cmdline_cfg *ccfg, int argc, char **argv)
233{
234 while (1) {
235 int option_index = 0, c;
236 static struct option long_options[] = {
237 {"help", 0, 0, 'h'},
238 {"debug", 1, 0, 'd'},
239 {"daemonize", 0, 0, 'D'},
240 {"config-file", 1, 0, 'c'},
241 {"disable-color", 0, 0, 's'},
242 {"timestamp", 0, 0, 'T'},
243 {"log-level", 1, 0, 'e'},
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +0100244 {"restart-file", 1, 0, 'r'},
Oliver Smithe64f0ef2018-10-16 17:09:18 +0200245 { "version", 0, 0, 'V' },
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200246 {NULL, 0, 0, 0}
247 };
248
Oliver Smithe64f0ef2018-10-16 17:09:18 +0200249 c = getopt_long(argc, argv, "hd:Dc:sTe:r:V",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200250 long_options, &option_index);
Neels Hofmeyr8d1ffbd2015-11-26 05:20:18 +0100251 if (c == -1) {
252 if (optind < argc) {
253 LOGP(DGTPHUB, LOGL_FATAL,
254 "Excess commandline arguments ('%s').\n",
255 argv[optind]);
256 exit(2);
257 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200258 break;
Neels Hofmeyr8d1ffbd2015-11-26 05:20:18 +0100259 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200260
261 switch (c) {
262 case 'h':
263 //print_usage();
264 print_help(ccfg);
265 exit(0);
266 case 's':
267 log_set_use_color(osmo_stderr_target, 0);
268 break;
269 case 'd':
270 if (strcmp("list", optarg) == 0) {
271 list_categories();
272 exit(0);
273 } else
274 log_parse_category_mask(osmo_stderr_target, optarg);
275 break;
276 case 'D':
277 ccfg->daemonize = 1;
278 break;
279 case 'c':
280 ccfg->config_file = optarg;
281 break;
282 case 'T':
283 log_set_print_timestamp(osmo_stderr_target, 1);
284 break;
285 case 'e':
286 log_set_log_level(osmo_stderr_target, atoi(optarg));
287 break;
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +0100288 case 'r':
289 ccfg->restart_counter_file = optarg;
290 break;
Oliver Smithe64f0ef2018-10-16 17:09:18 +0200291 case 'V':
292 print_version(1);
293 exit(EXIT_SUCCESS);
294 break;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200295 default:
Neels Hofmeyr956d8562015-12-06 16:40:24 +0100296 LOGP(DGTPHUB, LOGL_FATAL, "Invalid command line argument, abort.\n");
297 exit(1);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200298 break;
299 }
300 }
Harald Weltede67c002019-12-03 22:28:19 +0100301
302 if (argc > optind) {
303 fprintf(stderr, "Unsupported positional arguments on command line\n");
304 exit(2);
305 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200306}
307
308int main(int argc, char **argv)
309{
310 int rc;
311
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100312 struct cmdline_cfg _ccfg;
313 struct cmdline_cfg *ccfg = &_ccfg;
314 memset(ccfg, '\0', sizeof(*ccfg));
315 ccfg->config_file = "./gtphub.conf";
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +0100316 ccfg->restart_counter_file = "./gtphub_restart_count";
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100317
318 struct gtphub_cfg _cfg;
319 struct gtphub_cfg *cfg = &_cfg;
320 memset(cfg, '\0', sizeof(*cfg));
321
322 struct gtphub _hub;
323 struct gtphub *hub = &_hub;
324
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200325 osmo_gtphub_ctx = talloc_named_const(NULL, 0, "osmo_gtphub");
Neels Hofmeyr4c2d4ab2016-09-16 02:31:17 +0200326 msgb_talloc_ctx_init(osmo_gtphub_ctx, 0);
Harald Welte731930b2018-02-14 01:02:22 +0100327 vty_info.tall_ctx = osmo_gtphub_ctx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200328
329 signal(SIGINT, &signal_handler);
Harald Welte6f750e62017-08-20 20:50:06 +0200330 signal(SIGTERM, &signal_handler);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200331 signal(SIGABRT, &signal_handler);
332 signal(SIGUSR1, &signal_handler);
333 signal(SIGUSR2, &signal_handler);
334 osmo_init_ignore_signals();
335
Neels Hofmeyr5bd340e2018-04-16 00:57:10 +0200336 osmo_init_logging2(osmo_gtphub_ctx, &gtphub_log_info);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200337
338 vty_info.copyright = gtphub_copyright;
339 vty_init(&vty_info);
Pau Espin Pedrole456e322019-08-05 16:03:39 +0200340 logging_vty_add_cmds();
Harald Welte731930b2018-02-14 01:02:22 +0100341 osmo_talloc_vty_add_cmds();
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100342 gtphub_vty_init(hub, cfg);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200343
344 rate_ctr_init(osmo_gtphub_ctx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200345
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200346 handle_options(ccfg, argc, argv);
347
348 rc = gtphub_cfg_read(cfg, ccfg->config_file);
349 if (rc < 0) {
Neels Hofmeyrd9b1d492015-11-18 18:11:09 +0100350 LOGP(DGTPHUB, LOGL_FATAL, "Cannot parse config file '%s'\n",
351 ccfg->config_file);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200352 exit(2);
353 }
354
Neels Hofmeyrfa0f7152016-02-23 14:09:38 +0100355 /* start telnet after reading config for vty_get_bind_addr() */
Neels Hofmeyrfa0f7152016-02-23 14:09:38 +0100356 rc = telnet_init_dynif(osmo_gtphub_ctx, 0, vty_get_bind_addr(),
357 OSMO_VTY_PORT_GTPHUB);
358 if (rc < 0)
359 exit(1);
360
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +0100361 if (gtphub_start(hub, cfg,
362 next_restart_count(ccfg->restart_counter_file))
363 != 0)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200364 return -1;
365
366 log_cfg(cfg);
367
368 if (ccfg->daemonize) {
369 rc = osmo_daemonize();
370 if (rc < 0) {
Neels Hofmeyrd9b1d492015-11-18 18:11:09 +0100371 LOGP(DGTPHUB, LOGL_FATAL, "Error during daemonize");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200372 exit(1);
373 }
374 }
375
376 while (1) {
377 rc = osmo_select_main(0);
378 if (rc < 0)
379 exit(3);
380 }
381
382 /* not reached */
383 exit(0);
384}