Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1 | /* 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 Hofmeyr | ba9e9f6 | 2015-11-26 22:19:22 +0100 | [diff] [blame] | 26 | #include <inttypes.h> |
| 27 | #include <sys/stat.h> |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 28 | |
| 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 Hofmeyr | 03933a4 | 2016-02-23 13:26:02 +0100 | [diff] [blame] | 40 | #include <osmocom/vty/ports.h> |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 41 | |
| 42 | #include <openbsc/debug.h> |
| 43 | #include <openbsc/gtphub.h> |
| 44 | #include <openbsc/vty.h> |
| 45 | |
| 46 | #include "../../bscconfig.h" |
| 47 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 48 | extern void *osmo_gtphub_ctx; |
| 49 | |
| 50 | |
| 51 | const char *gtphub_copyright = |
| 52 | "Copyright (C) 2015 sysmocom s.f.m.c GmbH <info@sysmocom.de>\r\n" |
| 53 | "License AGPLv3+: GNU AGPL version 2 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n" |
| 54 | "This is free software: you are free to change and redistribute it.\r\n" |
| 55 | "There is NO WARRANTY, to the extent permitted by law.\r\n"; |
| 56 | |
| 57 | static struct log_info_cat gtphub_categories[] = { |
| 58 | [DGTPHUB] = { |
| 59 | .name = "DGTPHUB", |
| 60 | .description = "GTP Hub", |
| 61 | .color = "\033[1;33m", |
Neels Hofmeyr | d9b1d49 | 2015-11-18 18:11:09 +0100 | [diff] [blame] | 62 | .enabled = 1, |
Neels Hofmeyr | 69da1d4 | 2016-02-23 13:28:04 +0100 | [diff] [blame] | 63 | .loglevel = LOGL_INFO, |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 64 | }, |
| 65 | }; |
| 66 | |
| 67 | int gtphub_log_filter_fn(const struct log_context *ctx, |
| 68 | struct log_target *tar) |
| 69 | { |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | static const struct log_info gtphub_log_info = { |
| 74 | .filter_fn = gtphub_log_filter_fn, |
| 75 | .cat = gtphub_categories, |
| 76 | .num_cat = ARRAY_SIZE(gtphub_categories), |
| 77 | }; |
| 78 | |
| 79 | void log_cfg(struct gtphub_cfg *cfg) |
| 80 | { |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 81 | int side_idx, plane_idx; |
| 82 | for_each_side_and_plane(side_idx, plane_idx) { |
Neels Hofmeyr | 0855008 | 2015-11-30 12:19:50 +0100 | [diff] [blame] | 83 | struct gtphub_cfg_addr *a; |
Neels Hofmeyr | a9905a5 | 2015-11-29 23:49:48 +0100 | [diff] [blame] | 84 | a = &cfg->to_gsns[side_idx][plane_idx].bind; |
| 85 | LOGP(DGTPHUB, LOGL_NOTICE, |
| 86 | "to-%ss bind, %s: %s port %d\n", |
| 87 | gtphub_side_idx_names[side_idx], |
| 88 | gtphub_plane_idx_names[plane_idx], |
| 89 | a->addr_str, a->port); |
| 90 | } |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static void signal_handler(int signal) |
| 94 | { |
Neels Hofmeyr | 0855008 | 2015-11-30 12:19:50 +0100 | [diff] [blame] | 95 | fprintf(stdout, "signal %d received\n", signal); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 96 | |
| 97 | switch (signal) { |
| 98 | case SIGINT: |
Harald Welte | 6f750e6 | 2017-08-20 20:50:06 +0200 | [diff] [blame] | 99 | case SIGTERM: |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 100 | osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL); |
| 101 | sleep(1); |
| 102 | exit(0); |
| 103 | break; |
| 104 | case SIGABRT: |
| 105 | /* in case of abort, we want to obtain a talloc report |
| 106 | * and then return to the caller, who will abort the process */ |
| 107 | case SIGUSR1: |
| 108 | case SIGUSR2: |
| 109 | talloc_report_full(osmo_gtphub_ctx, stderr); |
| 110 | break; |
| 111 | default: |
| 112 | break; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | extern int bsc_vty_go_parent(struct vty *vty); |
| 117 | |
| 118 | static struct vty_app_info vty_info = { |
| 119 | .name = "OsmoGTPhub", |
| 120 | .version = PACKAGE_VERSION, |
| 121 | .go_parent_cb = bsc_vty_go_parent, |
| 122 | .is_config_node = bsc_vty_is_config_node, |
| 123 | }; |
| 124 | |
| 125 | struct cmdline_cfg { |
| 126 | const char *config_file; |
Neels Hofmeyr | ba9e9f6 | 2015-11-26 22:19:22 +0100 | [diff] [blame] | 127 | const char *restart_counter_file; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 128 | int daemonize; |
| 129 | }; |
| 130 | |
Neels Hofmeyr | ba9e9f6 | 2015-11-26 22:19:22 +0100 | [diff] [blame] | 131 | static uint8_t next_restart_count(const char *path) |
| 132 | { |
| 133 | int umask_was = umask(022); |
| 134 | |
| 135 | uint8_t counter = 0; |
| 136 | |
| 137 | FILE *f = fopen(path, "r"); |
| 138 | if (f) { |
| 139 | int rc = fscanf(f, "%hhu", &counter); |
| 140 | |
| 141 | if (rc != 1) |
| 142 | goto failed_to_read; |
| 143 | |
| 144 | char c; |
| 145 | while (fread(&c, 1, 1, f) > 0) { |
| 146 | switch (c) { |
| 147 | case ' ': |
| 148 | case '\t': |
| 149 | case '\n': |
| 150 | case '\r': |
| 151 | break; |
| 152 | default: |
| 153 | goto failed_to_read; |
| 154 | } |
| 155 | } |
| 156 | fclose(f); |
| 157 | } |
| 158 | |
| 159 | counter ++; |
| 160 | |
| 161 | f = fopen(path, "w"); |
| 162 | if (!f) |
| 163 | goto failed_to_write; |
| 164 | if (fprintf(f, "%" PRIu8 "\n", counter) < 2) |
| 165 | goto failed_to_write; |
Holger Hans Peter Freyther | de76661 | 2016-01-23 10:28:09 +0100 | [diff] [blame] | 166 | if (fclose(f)) { |
| 167 | f = NULL; |
Neels Hofmeyr | ba9e9f6 | 2015-11-26 22:19:22 +0100 | [diff] [blame] | 168 | goto failed_to_write; |
Holger Hans Peter Freyther | de76661 | 2016-01-23 10:28:09 +0100 | [diff] [blame] | 169 | } |
Neels Hofmeyr | ba9e9f6 | 2015-11-26 22:19:22 +0100 | [diff] [blame] | 170 | |
| 171 | umask(umask_was); |
| 172 | |
| 173 | LOGP(DGTPHUB, LOGL_NOTICE, "Restarted with counter %hhu\n", counter); |
| 174 | return counter; |
| 175 | |
| 176 | failed_to_read: |
| 177 | fclose(f); |
| 178 | umask(umask_was); |
| 179 | LOGP(DGTPHUB, LOGL_FATAL, "Restart counter file cannot be parsed:" |
| 180 | " %s\n", path); |
| 181 | exit(1); |
| 182 | |
| 183 | failed_to_write: |
| 184 | if (f) |
| 185 | fclose(f); |
| 186 | umask(umask_was); |
| 187 | LOGP(DGTPHUB, LOGL_FATAL, "Restart counter file cannot be written:" |
| 188 | " %s\n", path); |
| 189 | exit(1); |
| 190 | } |
| 191 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 192 | static void print_help(struct cmdline_cfg *ccfg) |
| 193 | { |
| 194 | printf("gtphub commandline options\n"); |
Neels Hofmeyr | ba9e9f6 | 2015-11-26 22:19:22 +0100 | [diff] [blame] | 195 | printf(" -h,--help This text.\n"); |
| 196 | printf(" -D,--daemonize Fork the process into a background daemon.\n"); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 197 | printf(" -d,--debug <cat> Enable Debugging for this category.\n"); |
| 198 | printf(" Pass '-d list' to get a category listing.\n"); |
Neels Hofmeyr | ba9e9f6 | 2015-11-26 22:19:22 +0100 | [diff] [blame] | 199 | printf(" -s,--disable-color\n"); |
| 200 | printf(" -c,--config-file <path> The config file to use [%s].\n", |
| 201 | ccfg->config_file); |
| 202 | printf(" -e,--log-level <nr> Set a global log level.\n"); |
| 203 | printf(" -r,--restart-file <path> File for counting restarts [%s].\n", |
| 204 | ccfg->restart_counter_file); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static void list_categories(void) |
| 208 | { |
| 209 | printf("Avaliable debug categories:\n"); |
| 210 | int i; |
| 211 | for (i = 0; i < gtphub_log_info.num_cat; ++i) { |
| 212 | if (!gtphub_log_info.cat[i].name) |
| 213 | continue; |
| 214 | |
| 215 | printf("%s\n", gtphub_log_info.cat[i].name); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | static void handle_options(struct cmdline_cfg *ccfg, int argc, char **argv) |
| 220 | { |
| 221 | while (1) { |
| 222 | int option_index = 0, c; |
| 223 | static struct option long_options[] = { |
| 224 | {"help", 0, 0, 'h'}, |
| 225 | {"debug", 1, 0, 'd'}, |
| 226 | {"daemonize", 0, 0, 'D'}, |
| 227 | {"config-file", 1, 0, 'c'}, |
| 228 | {"disable-color", 0, 0, 's'}, |
| 229 | {"timestamp", 0, 0, 'T'}, |
| 230 | {"log-level", 1, 0, 'e'}, |
Neels Hofmeyr | ba9e9f6 | 2015-11-26 22:19:22 +0100 | [diff] [blame] | 231 | {"restart-file", 1, 0, 'r'}, |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 232 | {NULL, 0, 0, 0} |
| 233 | }; |
| 234 | |
Neels Hofmeyr | ba9e9f6 | 2015-11-26 22:19:22 +0100 | [diff] [blame] | 235 | c = getopt_long(argc, argv, "hd:Dc:sTe:r:", |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 236 | long_options, &option_index); |
Neels Hofmeyr | 8d1ffbd | 2015-11-26 05:20:18 +0100 | [diff] [blame] | 237 | if (c == -1) { |
| 238 | if (optind < argc) { |
| 239 | LOGP(DGTPHUB, LOGL_FATAL, |
| 240 | "Excess commandline arguments ('%s').\n", |
| 241 | argv[optind]); |
| 242 | exit(2); |
| 243 | } |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 244 | break; |
Neels Hofmeyr | 8d1ffbd | 2015-11-26 05:20:18 +0100 | [diff] [blame] | 245 | } |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 246 | |
| 247 | switch (c) { |
| 248 | case 'h': |
| 249 | //print_usage(); |
| 250 | print_help(ccfg); |
| 251 | exit(0); |
| 252 | case 's': |
| 253 | log_set_use_color(osmo_stderr_target, 0); |
| 254 | break; |
| 255 | case 'd': |
| 256 | if (strcmp("list", optarg) == 0) { |
| 257 | list_categories(); |
| 258 | exit(0); |
| 259 | } else |
| 260 | log_parse_category_mask(osmo_stderr_target, optarg); |
| 261 | break; |
| 262 | case 'D': |
| 263 | ccfg->daemonize = 1; |
| 264 | break; |
| 265 | case 'c': |
| 266 | ccfg->config_file = optarg; |
| 267 | break; |
| 268 | case 'T': |
| 269 | log_set_print_timestamp(osmo_stderr_target, 1); |
| 270 | break; |
| 271 | case 'e': |
| 272 | log_set_log_level(osmo_stderr_target, atoi(optarg)); |
| 273 | break; |
Neels Hofmeyr | ba9e9f6 | 2015-11-26 22:19:22 +0100 | [diff] [blame] | 274 | case 'r': |
| 275 | ccfg->restart_counter_file = optarg; |
| 276 | break; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 277 | default: |
Neels Hofmeyr | 956d856 | 2015-12-06 16:40:24 +0100 | [diff] [blame] | 278 | LOGP(DGTPHUB, LOGL_FATAL, "Invalid command line argument, abort.\n"); |
| 279 | exit(1); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 280 | break; |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | int main(int argc, char **argv) |
| 286 | { |
| 287 | int rc; |
| 288 | |
Neels Hofmeyr | 4b2cbda | 2015-11-20 03:16:19 +0100 | [diff] [blame] | 289 | struct cmdline_cfg _ccfg; |
| 290 | struct cmdline_cfg *ccfg = &_ccfg; |
| 291 | memset(ccfg, '\0', sizeof(*ccfg)); |
| 292 | ccfg->config_file = "./gtphub.conf"; |
Neels Hofmeyr | ba9e9f6 | 2015-11-26 22:19:22 +0100 | [diff] [blame] | 293 | ccfg->restart_counter_file = "./gtphub_restart_count"; |
Neels Hofmeyr | 4b2cbda | 2015-11-20 03:16:19 +0100 | [diff] [blame] | 294 | |
| 295 | struct gtphub_cfg _cfg; |
| 296 | struct gtphub_cfg *cfg = &_cfg; |
| 297 | memset(cfg, '\0', sizeof(*cfg)); |
| 298 | |
| 299 | struct gtphub _hub; |
| 300 | struct gtphub *hub = &_hub; |
| 301 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 302 | osmo_gtphub_ctx = talloc_named_const(NULL, 0, "osmo_gtphub"); |
Neels Hofmeyr | 4c2d4ab | 2016-09-16 02:31:17 +0200 | [diff] [blame] | 303 | msgb_talloc_ctx_init(osmo_gtphub_ctx, 0); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 304 | |
| 305 | signal(SIGINT, &signal_handler); |
Harald Welte | 6f750e6 | 2017-08-20 20:50:06 +0200 | [diff] [blame] | 306 | signal(SIGTERM, &signal_handler); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 307 | signal(SIGABRT, &signal_handler); |
| 308 | signal(SIGUSR1, &signal_handler); |
| 309 | signal(SIGUSR2, &signal_handler); |
| 310 | osmo_init_ignore_signals(); |
| 311 | |
| 312 | osmo_init_logging(>phub_log_info); |
| 313 | |
| 314 | vty_info.copyright = gtphub_copyright; |
| 315 | vty_init(&vty_info); |
Max | db0e380 | 2017-01-12 19:35:11 +0100 | [diff] [blame] | 316 | logging_vty_add_cmds(NULL); |
Neels Hofmeyr | 4b2cbda | 2015-11-20 03:16:19 +0100 | [diff] [blame] | 317 | gtphub_vty_init(hub, cfg); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 318 | |
| 319 | rate_ctr_init(osmo_gtphub_ctx); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 320 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 321 | handle_options(ccfg, argc, argv); |
| 322 | |
| 323 | rc = gtphub_cfg_read(cfg, ccfg->config_file); |
| 324 | if (rc < 0) { |
Neels Hofmeyr | d9b1d49 | 2015-11-18 18:11:09 +0100 | [diff] [blame] | 325 | LOGP(DGTPHUB, LOGL_FATAL, "Cannot parse config file '%s'\n", |
| 326 | ccfg->config_file); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 327 | exit(2); |
| 328 | } |
| 329 | |
Neels Hofmeyr | fa0f715 | 2016-02-23 14:09:38 +0100 | [diff] [blame] | 330 | /* start telnet after reading config for vty_get_bind_addr() */ |
Neels Hofmeyr | fa0f715 | 2016-02-23 14:09:38 +0100 | [diff] [blame] | 331 | rc = telnet_init_dynif(osmo_gtphub_ctx, 0, vty_get_bind_addr(), |
| 332 | OSMO_VTY_PORT_GTPHUB); |
| 333 | if (rc < 0) |
| 334 | exit(1); |
| 335 | |
Neels Hofmeyr | ba9e9f6 | 2015-11-26 22:19:22 +0100 | [diff] [blame] | 336 | if (gtphub_start(hub, cfg, |
| 337 | next_restart_count(ccfg->restart_counter_file)) |
| 338 | != 0) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 339 | return -1; |
| 340 | |
| 341 | log_cfg(cfg); |
| 342 | |
| 343 | if (ccfg->daemonize) { |
| 344 | rc = osmo_daemonize(); |
| 345 | if (rc < 0) { |
Neels Hofmeyr | d9b1d49 | 2015-11-18 18:11:09 +0100 | [diff] [blame] | 346 | LOGP(DGTPHUB, LOGL_FATAL, "Error during daemonize"); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 347 | exit(1); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | while (1) { |
| 352 | rc = osmo_select_main(0); |
| 353 | if (rc < 0) |
| 354 | exit(3); |
| 355 | } |
| 356 | |
| 357 | /* not reached */ |
| 358 | exit(0); |
| 359 | } |