blob: 3f7de5071f6f85b76a433bfada1f61f66e084910 [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>
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020041
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020042#include <osmocom/sgsn/debug.h>
43#include <osmocom/sgsn/gtphub.h>
44#include <osmocom/sgsn/vty.h>
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020045
46#include "../../bscconfig.h"
47
Max095de012017-10-26 15:27:12 +020048#if BUILD_IU
49#include <osmocom/sigtran/osmo_ss7.h>
50#endif
51
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020052extern void *osmo_gtphub_ctx;
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +020053void *tall_bsc_ctx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020054
55const char *gtphub_copyright =
56 "Copyright (C) 2015 sysmocom s.f.m.c GmbH <info@sysmocom.de>\r\n"
57 "License AGPLv3+: GNU AGPL version 2 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
58 "This is free software: you are free to change and redistribute it.\r\n"
59 "There is NO WARRANTY, to the extent permitted by law.\r\n";
60
61static struct log_info_cat gtphub_categories[] = {
62 [DGTPHUB] = {
63 .name = "DGTPHUB",
64 .description = "GTP Hub",
65 .color = "\033[1;33m",
Neels Hofmeyrd9b1d492015-11-18 18:11:09 +010066 .enabled = 1,
Neels Hofmeyr69da1d42016-02-23 13:28:04 +010067 .loglevel = LOGL_INFO,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020068 },
69};
70
71int gtphub_log_filter_fn(const struct log_context *ctx,
72 struct log_target *tar)
73{
74 return 0;
75}
76
77static const struct log_info gtphub_log_info = {
78 .filter_fn = gtphub_log_filter_fn,
79 .cat = gtphub_categories,
80 .num_cat = ARRAY_SIZE(gtphub_categories),
81};
82
83void log_cfg(struct gtphub_cfg *cfg)
84{
Neels Hofmeyra9905a52015-11-29 23:49:48 +010085 int side_idx, plane_idx;
86 for_each_side_and_plane(side_idx, plane_idx) {
Neels Hofmeyr08550082015-11-30 12:19:50 +010087 struct gtphub_cfg_addr *a;
Neels Hofmeyra9905a52015-11-29 23:49:48 +010088 a = &cfg->to_gsns[side_idx][plane_idx].bind;
89 LOGP(DGTPHUB, LOGL_NOTICE,
90 "to-%ss bind, %s: %s port %d\n",
91 gtphub_side_idx_names[side_idx],
92 gtphub_plane_idx_names[plane_idx],
93 a->addr_str, a->port);
94 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020095}
96
97static void signal_handler(int signal)
98{
Neels Hofmeyr08550082015-11-30 12:19:50 +010099 fprintf(stdout, "signal %d received\n", signal);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200100
101 switch (signal) {
102 case SIGINT:
Harald Welte6f750e62017-08-20 20:50:06 +0200103 case SIGTERM:
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200104 osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL);
105 sleep(1);
106 exit(0);
107 break;
108 case SIGABRT:
109 /* in case of abort, we want to obtain a talloc report
110 * and then return to the caller, who will abort the process */
111 case SIGUSR1:
112 case SIGUSR2:
113 talloc_report_full(osmo_gtphub_ctx, stderr);
114 break;
115 default:
116 break;
117 }
118}
119
Philipp Maier4a547a12017-10-20 11:27:26 +0200120#if BUILD_IU
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +0200121int gtphub_vty_go_parent(struct vty *vty)
122{
123 switch (vty->node) {
124 default:
125 osmo_ss7_vty_go_parent(vty);
126 }
127
128 return vty->node;
129}
Philipp Maier4a547a12017-10-20 11:27:26 +0200130#endif
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +0200131
132int gtphub_vty_is_config_node(struct vty *vty, int node)
133{
134 /* Check if libosmo-sccp declares the node in
135 * question as config node */
Philipp Maier4a547a12017-10-20 11:27:26 +0200136#if BUILD_IU
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +0200137 if (osmo_ss7_is_config_node(vty, node))
138 return 1;
Philipp Maier4a547a12017-10-20 11:27:26 +0200139#endif
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +0200140
141 switch (node) {
142 /* add items that are not config */
143 case CONFIG_NODE:
144 return 0;
145
146 default:
147 return 1;
148 }
149}
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200150
151static struct vty_app_info vty_info = {
152 .name = "OsmoGTPhub",
153 .version = PACKAGE_VERSION,
Philipp Maier4a547a12017-10-20 11:27:26 +0200154#if BUILD_IU
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +0200155 .go_parent_cb = gtphub_vty_go_parent,
Philipp Maier4a547a12017-10-20 11:27:26 +0200156#endif
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +0200157 .is_config_node = gtphub_vty_is_config_node,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200158};
159
160struct cmdline_cfg {
161 const char *config_file;
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +0100162 const char *restart_counter_file;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200163 int daemonize;
164};
165
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +0100166static uint8_t next_restart_count(const char *path)
167{
168 int umask_was = umask(022);
169
170 uint8_t counter = 0;
171
172 FILE *f = fopen(path, "r");
173 if (f) {
174 int rc = fscanf(f, "%hhu", &counter);
175
176 if (rc != 1)
177 goto failed_to_read;
178
179 char c;
180 while (fread(&c, 1, 1, f) > 0) {
181 switch (c) {
182 case ' ':
183 case '\t':
184 case '\n':
185 case '\r':
186 break;
187 default:
188 goto failed_to_read;
189 }
190 }
191 fclose(f);
192 }
193
194 counter ++;
195
196 f = fopen(path, "w");
197 if (!f)
198 goto failed_to_write;
199 if (fprintf(f, "%" PRIu8 "\n", counter) < 2)
200 goto failed_to_write;
Holger Hans Peter Freytherde766612016-01-23 10:28:09 +0100201 if (fclose(f)) {
202 f = NULL;
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +0100203 goto failed_to_write;
Holger Hans Peter Freytherde766612016-01-23 10:28:09 +0100204 }
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +0100205
206 umask(umask_was);
207
208 LOGP(DGTPHUB, LOGL_NOTICE, "Restarted with counter %hhu\n", counter);
209 return counter;
210
211failed_to_read:
212 fclose(f);
213 umask(umask_was);
214 LOGP(DGTPHUB, LOGL_FATAL, "Restart counter file cannot be parsed:"
215 " %s\n", path);
216 exit(1);
217
218failed_to_write:
219 if (f)
220 fclose(f);
221 umask(umask_was);
222 LOGP(DGTPHUB, LOGL_FATAL, "Restart counter file cannot be written:"
223 " %s\n", path);
224 exit(1);
225}
226
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200227static void print_help(struct cmdline_cfg *ccfg)
228{
229 printf("gtphub commandline options\n");
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +0100230 printf(" -h,--help This text.\n");
231 printf(" -D,--daemonize Fork the process into a background daemon.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200232 printf(" -d,--debug <cat> Enable Debugging for this category.\n");
233 printf(" Pass '-d list' to get a category listing.\n");
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +0100234 printf(" -s,--disable-color\n");
235 printf(" -c,--config-file <path> The config file to use [%s].\n",
236 ccfg->config_file);
237 printf(" -e,--log-level <nr> Set a global log level.\n");
238 printf(" -r,--restart-file <path> File for counting restarts [%s].\n",
239 ccfg->restart_counter_file);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200240}
241
242static void list_categories(void)
243{
244 printf("Avaliable debug categories:\n");
245 int i;
246 for (i = 0; i < gtphub_log_info.num_cat; ++i) {
247 if (!gtphub_log_info.cat[i].name)
248 continue;
249
250 printf("%s\n", gtphub_log_info.cat[i].name);
251 }
252}
253
254static void handle_options(struct cmdline_cfg *ccfg, int argc, char **argv)
255{
256 while (1) {
257 int option_index = 0, c;
258 static struct option long_options[] = {
259 {"help", 0, 0, 'h'},
260 {"debug", 1, 0, 'd'},
261 {"daemonize", 0, 0, 'D'},
262 {"config-file", 1, 0, 'c'},
263 {"disable-color", 0, 0, 's'},
264 {"timestamp", 0, 0, 'T'},
265 {"log-level", 1, 0, 'e'},
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +0100266 {"restart-file", 1, 0, 'r'},
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200267 {NULL, 0, 0, 0}
268 };
269
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +0100270 c = getopt_long(argc, argv, "hd:Dc:sTe:r:",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200271 long_options, &option_index);
Neels Hofmeyr8d1ffbd2015-11-26 05:20:18 +0100272 if (c == -1) {
273 if (optind < argc) {
274 LOGP(DGTPHUB, LOGL_FATAL,
275 "Excess commandline arguments ('%s').\n",
276 argv[optind]);
277 exit(2);
278 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200279 break;
Neels Hofmeyr8d1ffbd2015-11-26 05:20:18 +0100280 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200281
282 switch (c) {
283 case 'h':
284 //print_usage();
285 print_help(ccfg);
286 exit(0);
287 case 's':
288 log_set_use_color(osmo_stderr_target, 0);
289 break;
290 case 'd':
291 if (strcmp("list", optarg) == 0) {
292 list_categories();
293 exit(0);
294 } else
295 log_parse_category_mask(osmo_stderr_target, optarg);
296 break;
297 case 'D':
298 ccfg->daemonize = 1;
299 break;
300 case 'c':
301 ccfg->config_file = optarg;
302 break;
303 case 'T':
304 log_set_print_timestamp(osmo_stderr_target, 1);
305 break;
306 case 'e':
307 log_set_log_level(osmo_stderr_target, atoi(optarg));
308 break;
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +0100309 case 'r':
310 ccfg->restart_counter_file = optarg;
311 break;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200312 default:
Neels Hofmeyr956d8562015-12-06 16:40:24 +0100313 LOGP(DGTPHUB, LOGL_FATAL, "Invalid command line argument, abort.\n");
314 exit(1);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200315 break;
316 }
317 }
318}
319
320int main(int argc, char **argv)
321{
322 int rc;
323
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100324 struct cmdline_cfg _ccfg;
325 struct cmdline_cfg *ccfg = &_ccfg;
326 memset(ccfg, '\0', sizeof(*ccfg));
327 ccfg->config_file = "./gtphub.conf";
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +0100328 ccfg->restart_counter_file = "./gtphub_restart_count";
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100329
330 struct gtphub_cfg _cfg;
331 struct gtphub_cfg *cfg = &_cfg;
332 memset(cfg, '\0', sizeof(*cfg));
333
334 struct gtphub _hub;
335 struct gtphub *hub = &_hub;
336
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200337 osmo_gtphub_ctx = talloc_named_const(NULL, 0, "osmo_gtphub");
Neels Hofmeyr4c2d4ab2016-09-16 02:31:17 +0200338 msgb_talloc_ctx_init(osmo_gtphub_ctx, 0);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200339
340 signal(SIGINT, &signal_handler);
Harald Welte6f750e62017-08-20 20:50:06 +0200341 signal(SIGTERM, &signal_handler);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200342 signal(SIGABRT, &signal_handler);
343 signal(SIGUSR1, &signal_handler);
344 signal(SIGUSR2, &signal_handler);
345 osmo_init_ignore_signals();
346
347 osmo_init_logging(&gtphub_log_info);
348
349 vty_info.copyright = gtphub_copyright;
350 vty_init(&vty_info);
Maxdb0e3802017-01-12 19:35:11 +0100351 logging_vty_add_cmds(NULL);
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100352 gtphub_vty_init(hub, cfg);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200353
354 rate_ctr_init(osmo_gtphub_ctx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200355
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200356 handle_options(ccfg, argc, argv);
357
358 rc = gtphub_cfg_read(cfg, ccfg->config_file);
359 if (rc < 0) {
Neels Hofmeyrd9b1d492015-11-18 18:11:09 +0100360 LOGP(DGTPHUB, LOGL_FATAL, "Cannot parse config file '%s'\n",
361 ccfg->config_file);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200362 exit(2);
363 }
364
Neels Hofmeyrfa0f7152016-02-23 14:09:38 +0100365 /* start telnet after reading config for vty_get_bind_addr() */
Neels Hofmeyrfa0f7152016-02-23 14:09:38 +0100366 rc = telnet_init_dynif(osmo_gtphub_ctx, 0, vty_get_bind_addr(),
367 OSMO_VTY_PORT_GTPHUB);
368 if (rc < 0)
369 exit(1);
370
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +0100371 if (gtphub_start(hub, cfg,
372 next_restart_count(ccfg->restart_counter_file))
373 != 0)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200374 return -1;
375
376 log_cfg(cfg);
377
378 if (ccfg->daemonize) {
379 rc = osmo_daemonize();
380 if (rc < 0) {
Neels Hofmeyrd9b1d492015-11-18 18:11:09 +0100381 LOGP(DGTPHUB, LOGL_FATAL, "Error during daemonize");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200382 exit(1);
383 }
384 }
385
386 while (1) {
387 rc = osmo_select_main(0);
388 if (rc < 0)
389 exit(3);
390 }
391
392 /* not reached */
393 exit(0);
394}