blob: e0df0d6d3c8a1420fd5836e1cd2865e6d5f3e67f [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>
26
27#define _GNU_SOURCE
28#include <getopt.h>
29
30#include <osmocom/core/signal.h>
31#include <osmocom/core/application.h>
32#include <osmocom/core/logging.h>
33#include <osmocom/core/utils.h>
34#include <osmocom/core/rate_ctr.h>
35
36#include <osmocom/vty/logging.h>
37#include <osmocom/vty/telnet_interface.h>
38
39#include <openbsc/debug.h>
40#include <openbsc/gtphub.h>
41#include <openbsc/vty.h>
42
43#include "../../bscconfig.h"
44
45#define LOGERR(fmt, args...) \
46 LOGP(DGTPHUB, LOGL_ERROR, fmt, ##args)
47
48#define LOG(fmt, args...) \
49 LOGP(DGTPHUB, LOGL_NOTICE, fmt, ##args)
50
51#ifndef OSMO_VTY_PORT_GTPHUB
52/* should come from libosmocore */
53#define OSMO_VTY_PORT_GTPHUB 4253
54#endif
55
56extern void *osmo_gtphub_ctx;
57
58
59const char *gtphub_copyright =
60 "Copyright (C) 2015 sysmocom s.f.m.c GmbH <info@sysmocom.de>\r\n"
61 "License AGPLv3+: GNU AGPL version 2 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
62 "This is free software: you are free to change and redistribute it.\r\n"
63 "There is NO WARRANTY, to the extent permitted by law.\r\n";
64
65static struct log_info_cat gtphub_categories[] = {
66 [DGTPHUB] = {
67 .name = "DGTPHUB",
68 .description = "GTP Hub",
69 .color = "\033[1;33m",
70 .enabled = 1, .loglevel = LOGL_NOTICE,
71 },
72};
73
74int gtphub_log_filter_fn(const struct log_context *ctx,
75 struct log_target *tar)
76{
77 return 0;
78}
79
80static const struct log_info gtphub_log_info = {
81 .filter_fn = gtphub_log_filter_fn,
82 .cat = gtphub_categories,
83 .num_cat = ARRAY_SIZE(gtphub_categories),
84};
85
86void log_cfg(struct gtphub_cfg *cfg)
87{
88 struct gtphub_cfg_addr *a;
89 a = &cfg->to_sgsns[GTPH_PLANE_CTRL].bind;
90 LOG("to-SGSNs bind, Control: %s port %d\n",
91 a->addr_str, a->port);
92 a = &cfg->to_sgsns[GTPH_PLANE_USER].bind;
93 LOG("to-SGSNs bind, User: %s port %d\n",
94 a->addr_str, a->port);
95 a = &cfg->to_ggsns[GTPH_PLANE_CTRL].bind;
96 LOG("to-GGSNs bind, Control: %s port %d\n",
97 a->addr_str, a->port);
98 a = &cfg->to_ggsns[GTPH_PLANE_USER].bind;
99 LOG("to-GGSNs bind, User: %s port %d\n",
100 a->addr_str, a->port);
101}
102
103static void signal_handler(int signal)
104{
105 fprintf(stdout, "signal %u received\n", signal);
106
107 switch (signal) {
108 case SIGINT:
109 osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL);
110 sleep(1);
111 exit(0);
112 break;
113 case SIGABRT:
114 /* in case of abort, we want to obtain a talloc report
115 * and then return to the caller, who will abort the process */
116 case SIGUSR1:
117 case SIGUSR2:
118 talloc_report_full(osmo_gtphub_ctx, stderr);
119 break;
120 default:
121 break;
122 }
123}
124
125extern int bsc_vty_go_parent(struct vty *vty);
126
127static struct vty_app_info vty_info = {
128 .name = "OsmoGTPhub",
129 .version = PACKAGE_VERSION,
130 .go_parent_cb = bsc_vty_go_parent,
131 .is_config_node = bsc_vty_is_config_node,
132};
133
134struct cmdline_cfg {
135 const char *config_file;
136 int daemonize;
137};
138
139static void print_help(struct cmdline_cfg *ccfg)
140{
141 printf("gtphub commandline options\n");
142 printf(" -h --help This text.\n");
143 printf(" -D --daemonize Fork the process into a background daemon.\n");
144 printf(" -d,--debug <cat> Enable Debugging for this category.\n");
145 printf(" Pass '-d list' to get a category listing.\n");
146 printf(" -s --disable-color");
147 printf(" -c --config-file The config file to use [%s].\n", ccfg->config_file);
148 printf(" -e,--log-level <nr> Set a global log level.\n");
149}
150
151static void list_categories(void)
152{
153 printf("Avaliable debug categories:\n");
154 int i;
155 for (i = 0; i < gtphub_log_info.num_cat; ++i) {
156 if (!gtphub_log_info.cat[i].name)
157 continue;
158
159 printf("%s\n", gtphub_log_info.cat[i].name);
160 }
161}
162
163static void handle_options(struct cmdline_cfg *ccfg, int argc, char **argv)
164{
165 while (1) {
166 int option_index = 0, c;
167 static struct option long_options[] = {
168 {"help", 0, 0, 'h'},
169 {"debug", 1, 0, 'd'},
170 {"daemonize", 0, 0, 'D'},
171 {"config-file", 1, 0, 'c'},
172 {"disable-color", 0, 0, 's'},
173 {"timestamp", 0, 0, 'T'},
174 {"log-level", 1, 0, 'e'},
175 {NULL, 0, 0, 0}
176 };
177
178 c = getopt_long(argc, argv, "hd:Dc:sTe:",
179 long_options, &option_index);
180 if (c == -1)
181 break;
182
183 switch (c) {
184 case 'h':
185 //print_usage();
186 print_help(ccfg);
187 exit(0);
188 case 's':
189 log_set_use_color(osmo_stderr_target, 0);
190 break;
191 case 'd':
192 if (strcmp("list", optarg) == 0) {
193 list_categories();
194 exit(0);
195 } else
196 log_parse_category_mask(osmo_stderr_target, optarg);
197 break;
198 case 'D':
199 ccfg->daemonize = 1;
200 break;
201 case 'c':
202 ccfg->config_file = optarg;
203 break;
204 case 'T':
205 log_set_print_timestamp(osmo_stderr_target, 1);
206 break;
207 case 'e':
208 log_set_log_level(osmo_stderr_target, atoi(optarg));
209 break;
210 default:
211 /* ignore */
212 break;
213 }
214 }
215}
216
217int main(int argc, char **argv)
218{
219 int rc;
220
221 osmo_gtphub_ctx = talloc_named_const(NULL, 0, "osmo_gtphub");
222
223 signal(SIGINT, &signal_handler);
224 signal(SIGABRT, &signal_handler);
225 signal(SIGUSR1, &signal_handler);
226 signal(SIGUSR2, &signal_handler);
227 osmo_init_ignore_signals();
228
229 osmo_init_logging(&gtphub_log_info);
230
231 vty_info.copyright = gtphub_copyright;
232 vty_init(&vty_info);
233 logging_vty_add_cmds(&gtphub_log_info);
234 gtphub_vty_init();
235
236 rate_ctr_init(osmo_gtphub_ctx);
237 rc = telnet_init(osmo_gtphub_ctx, 0, OSMO_VTY_PORT_GTPHUB);
238 if (rc < 0)
239 exit(1);
240
241 struct cmdline_cfg _ccfg;
242 struct cmdline_cfg *ccfg = &_ccfg;
243 memset(ccfg, '\0', sizeof(*ccfg));
244 ccfg->config_file = "./gtphub.conf";
245
246 struct gtphub_cfg _cfg;
247 struct gtphub_cfg *cfg = &_cfg;
248 memset(cfg, '\0', sizeof(*cfg));
249
250 struct gtphub _hub;
251 struct gtphub *hub = &_hub;
252
253 handle_options(ccfg, argc, argv);
254
255 rc = gtphub_cfg_read(cfg, ccfg->config_file);
256 if (rc < 0) {
257 LOGP(DGTPHUB, LOGL_FATAL, "Cannot parse config file '%s'\n", ccfg->config_file);
258 exit(2);
259 }
260
261 if (gtphub_start(hub, cfg) != 0)
262 return -1;
263
264 log_cfg(cfg);
265
266 if (ccfg->daemonize) {
267 rc = osmo_daemonize();
268 if (rc < 0) {
269 LOGERR("Error during daemonize");
270 exit(1);
271 }
272 }
273
274 while (1) {
275 rc = osmo_select_main(0);
276 if (rc < 0)
277 exit(3);
278 }
279
280 /* not reached */
281 exit(0);
282}