blob: 782afb32200d0458ac1e230b28cc4116657f687e [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 Hofmeyree6cfdc2017-07-13 02:03:50 +020042#include <osmocom/sigtran/osmo_ss7.h>
43
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020044#include <osmocom/sgsn/debug.h>
45#include <osmocom/sgsn/gtphub.h>
46#include <osmocom/sgsn/vty.h>
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020047
48#include "../../bscconfig.h"
49
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020050extern void *osmo_gtphub_ctx;
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +020051void *tall_bsc_ctx;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020052
53const char *gtphub_copyright =
54 "Copyright (C) 2015 sysmocom s.f.m.c GmbH <info@sysmocom.de>\r\n"
55 "License AGPLv3+: GNU AGPL version 2 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
56 "This is free software: you are free to change and redistribute it.\r\n"
57 "There is NO WARRANTY, to the extent permitted by law.\r\n";
58
59static struct log_info_cat gtphub_categories[] = {
60 [DGTPHUB] = {
61 .name = "DGTPHUB",
62 .description = "GTP Hub",
63 .color = "\033[1;33m",
Neels Hofmeyrd9b1d492015-11-18 18:11:09 +010064 .enabled = 1,
Neels Hofmeyr69da1d42016-02-23 13:28:04 +010065 .loglevel = LOGL_INFO,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020066 },
67};
68
69int gtphub_log_filter_fn(const struct log_context *ctx,
70 struct log_target *tar)
71{
72 return 0;
73}
74
75static const struct log_info gtphub_log_info = {
76 .filter_fn = gtphub_log_filter_fn,
77 .cat = gtphub_categories,
78 .num_cat = ARRAY_SIZE(gtphub_categories),
79};
80
81void log_cfg(struct gtphub_cfg *cfg)
82{
Neels Hofmeyra9905a52015-11-29 23:49:48 +010083 int side_idx, plane_idx;
84 for_each_side_and_plane(side_idx, plane_idx) {
Neels Hofmeyr08550082015-11-30 12:19:50 +010085 struct gtphub_cfg_addr *a;
Neels Hofmeyra9905a52015-11-29 23:49:48 +010086 a = &cfg->to_gsns[side_idx][plane_idx].bind;
87 LOGP(DGTPHUB, LOGL_NOTICE,
88 "to-%ss bind, %s: %s port %d\n",
89 gtphub_side_idx_names[side_idx],
90 gtphub_plane_idx_names[plane_idx],
91 a->addr_str, a->port);
92 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020093}
94
95static void signal_handler(int signal)
96{
Neels Hofmeyr08550082015-11-30 12:19:50 +010097 fprintf(stdout, "signal %d received\n", signal);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +020098
99 switch (signal) {
100 case SIGINT:
Harald Welte6f750e62017-08-20 20:50:06 +0200101 case SIGTERM:
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200102 osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL);
103 sleep(1);
104 exit(0);
105 break;
106 case SIGABRT:
107 /* in case of abort, we want to obtain a talloc report
108 * and then return to the caller, who will abort the process */
109 case SIGUSR1:
110 case SIGUSR2:
111 talloc_report_full(osmo_gtphub_ctx, stderr);
112 break;
113 default:
114 break;
115 }
116}
117
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +0200118int gtphub_vty_go_parent(struct vty *vty)
119{
120 switch (vty->node) {
121 default:
122 osmo_ss7_vty_go_parent(vty);
123 }
124
125 return vty->node;
126}
127
128int gtphub_vty_is_config_node(struct vty *vty, int node)
129{
130 /* Check if libosmo-sccp declares the node in
131 * question as config node */
132 if (osmo_ss7_is_config_node(vty, node))
133 return 1;
134
135 switch (node) {
136 /* add items that are not config */
137 case CONFIG_NODE:
138 return 0;
139
140 default:
141 return 1;
142 }
143}
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200144
145static struct vty_app_info vty_info = {
146 .name = "OsmoGTPhub",
147 .version = PACKAGE_VERSION,
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +0200148 .go_parent_cb = gtphub_vty_go_parent,
149 .is_config_node = gtphub_vty_is_config_node,
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200150};
151
152struct cmdline_cfg {
153 const char *config_file;
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +0100154 const char *restart_counter_file;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200155 int daemonize;
156};
157
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +0100158static uint8_t next_restart_count(const char *path)
159{
160 int umask_was = umask(022);
161
162 uint8_t counter = 0;
163
164 FILE *f = fopen(path, "r");
165 if (f) {
166 int rc = fscanf(f, "%hhu", &counter);
167
168 if (rc != 1)
169 goto failed_to_read;
170
171 char c;
172 while (fread(&c, 1, 1, f) > 0) {
173 switch (c) {
174 case ' ':
175 case '\t':
176 case '\n':
177 case '\r':
178 break;
179 default:
180 goto failed_to_read;
181 }
182 }
183 fclose(f);
184 }
185
186 counter ++;
187
188 f = fopen(path, "w");
189 if (!f)
190 goto failed_to_write;
191 if (fprintf(f, "%" PRIu8 "\n", counter) < 2)
192 goto failed_to_write;
Holger Hans Peter Freytherde766612016-01-23 10:28:09 +0100193 if (fclose(f)) {
194 f = NULL;
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +0100195 goto failed_to_write;
Holger Hans Peter Freytherde766612016-01-23 10:28:09 +0100196 }
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +0100197
198 umask(umask_was);
199
200 LOGP(DGTPHUB, LOGL_NOTICE, "Restarted with counter %hhu\n", counter);
201 return counter;
202
203failed_to_read:
204 fclose(f);
205 umask(umask_was);
206 LOGP(DGTPHUB, LOGL_FATAL, "Restart counter file cannot be parsed:"
207 " %s\n", path);
208 exit(1);
209
210failed_to_write:
211 if (f)
212 fclose(f);
213 umask(umask_was);
214 LOGP(DGTPHUB, LOGL_FATAL, "Restart counter file cannot be written:"
215 " %s\n", path);
216 exit(1);
217}
218
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200219static void print_help(struct cmdline_cfg *ccfg)
220{
221 printf("gtphub commandline options\n");
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +0100222 printf(" -h,--help This text.\n");
223 printf(" -D,--daemonize Fork the process into a background daemon.\n");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200224 printf(" -d,--debug <cat> Enable Debugging for this category.\n");
225 printf(" Pass '-d list' to get a category listing.\n");
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +0100226 printf(" -s,--disable-color\n");
227 printf(" -c,--config-file <path> The config file to use [%s].\n",
228 ccfg->config_file);
229 printf(" -e,--log-level <nr> Set a global log level.\n");
230 printf(" -r,--restart-file <path> File for counting restarts [%s].\n",
231 ccfg->restart_counter_file);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200232}
233
234static void list_categories(void)
235{
236 printf("Avaliable debug categories:\n");
237 int i;
238 for (i = 0; i < gtphub_log_info.num_cat; ++i) {
239 if (!gtphub_log_info.cat[i].name)
240 continue;
241
242 printf("%s\n", gtphub_log_info.cat[i].name);
243 }
244}
245
246static void handle_options(struct cmdline_cfg *ccfg, int argc, char **argv)
247{
248 while (1) {
249 int option_index = 0, c;
250 static struct option long_options[] = {
251 {"help", 0, 0, 'h'},
252 {"debug", 1, 0, 'd'},
253 {"daemonize", 0, 0, 'D'},
254 {"config-file", 1, 0, 'c'},
255 {"disable-color", 0, 0, 's'},
256 {"timestamp", 0, 0, 'T'},
257 {"log-level", 1, 0, 'e'},
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +0100258 {"restart-file", 1, 0, 'r'},
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200259 {NULL, 0, 0, 0}
260 };
261
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +0100262 c = getopt_long(argc, argv, "hd:Dc:sTe:r:",
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200263 long_options, &option_index);
Neels Hofmeyr8d1ffbd2015-11-26 05:20:18 +0100264 if (c == -1) {
265 if (optind < argc) {
266 LOGP(DGTPHUB, LOGL_FATAL,
267 "Excess commandline arguments ('%s').\n",
268 argv[optind]);
269 exit(2);
270 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200271 break;
Neels Hofmeyr8d1ffbd2015-11-26 05:20:18 +0100272 }
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200273
274 switch (c) {
275 case 'h':
276 //print_usage();
277 print_help(ccfg);
278 exit(0);
279 case 's':
280 log_set_use_color(osmo_stderr_target, 0);
281 break;
282 case 'd':
283 if (strcmp("list", optarg) == 0) {
284 list_categories();
285 exit(0);
286 } else
287 log_parse_category_mask(osmo_stderr_target, optarg);
288 break;
289 case 'D':
290 ccfg->daemonize = 1;
291 break;
292 case 'c':
293 ccfg->config_file = optarg;
294 break;
295 case 'T':
296 log_set_print_timestamp(osmo_stderr_target, 1);
297 break;
298 case 'e':
299 log_set_log_level(osmo_stderr_target, atoi(optarg));
300 break;
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +0100301 case 'r':
302 ccfg->restart_counter_file = optarg;
303 break;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200304 default:
Neels Hofmeyr956d8562015-12-06 16:40:24 +0100305 LOGP(DGTPHUB, LOGL_FATAL, "Invalid command line argument, abort.\n");
306 exit(1);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200307 break;
308 }
309 }
310}
311
312int main(int argc, char **argv)
313{
314 int rc;
315
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100316 struct cmdline_cfg _ccfg;
317 struct cmdline_cfg *ccfg = &_ccfg;
318 memset(ccfg, '\0', sizeof(*ccfg));
319 ccfg->config_file = "./gtphub.conf";
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +0100320 ccfg->restart_counter_file = "./gtphub_restart_count";
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100321
322 struct gtphub_cfg _cfg;
323 struct gtphub_cfg *cfg = &_cfg;
324 memset(cfg, '\0', sizeof(*cfg));
325
326 struct gtphub _hub;
327 struct gtphub *hub = &_hub;
328
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200329 osmo_gtphub_ctx = talloc_named_const(NULL, 0, "osmo_gtphub");
Neels Hofmeyr4c2d4ab2016-09-16 02:31:17 +0200330 msgb_talloc_ctx_init(osmo_gtphub_ctx, 0);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200331
332 signal(SIGINT, &signal_handler);
Harald Welte6f750e62017-08-20 20:50:06 +0200333 signal(SIGTERM, &signal_handler);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200334 signal(SIGABRT, &signal_handler);
335 signal(SIGUSR1, &signal_handler);
336 signal(SIGUSR2, &signal_handler);
337 osmo_init_ignore_signals();
338
339 osmo_init_logging(&gtphub_log_info);
340
341 vty_info.copyright = gtphub_copyright;
342 vty_init(&vty_info);
Maxdb0e3802017-01-12 19:35:11 +0100343 logging_vty_add_cmds(NULL);
Neels Hofmeyr4b2cbda2015-11-20 03:16:19 +0100344 gtphub_vty_init(hub, cfg);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200345
346 rate_ctr_init(osmo_gtphub_ctx);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200347
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200348 handle_options(ccfg, argc, argv);
349
350 rc = gtphub_cfg_read(cfg, ccfg->config_file);
351 if (rc < 0) {
Neels Hofmeyrd9b1d492015-11-18 18:11:09 +0100352 LOGP(DGTPHUB, LOGL_FATAL, "Cannot parse config file '%s'\n",
353 ccfg->config_file);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200354 exit(2);
355 }
356
Neels Hofmeyrfa0f7152016-02-23 14:09:38 +0100357 /* start telnet after reading config for vty_get_bind_addr() */
Neels Hofmeyrfa0f7152016-02-23 14:09:38 +0100358 rc = telnet_init_dynif(osmo_gtphub_ctx, 0, vty_get_bind_addr(),
359 OSMO_VTY_PORT_GTPHUB);
360 if (rc < 0)
361 exit(1);
362
Neels Hofmeyrba9e9f62015-11-26 22:19:22 +0100363 if (gtphub_start(hub, cfg,
364 next_restart_count(ccfg->restart_counter_file))
365 != 0)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200366 return -1;
367
368 log_cfg(cfg);
369
370 if (ccfg->daemonize) {
371 rc = osmo_daemonize();
372 if (rc < 0) {
Neels Hofmeyrd9b1d492015-11-18 18:11:09 +0100373 LOGP(DGTPHUB, LOGL_FATAL, "Error during daemonize");
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200374 exit(1);
375 }
376 }
377
378 while (1) {
379 rc = osmo_select_main(0);
380 if (rc < 0)
381 exit(3);
382 }
383
384 /* not reached */
385 exit(0);
386}