Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 1 | /* pcu_main.cpp |
| 2 | * |
| 3 | * Copyright (C) 2012 Ivan Klyuchnikov |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version 2 |
| 8 | * of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 | */ |
| 19 | |
| 20 | #include <gprs_bssgp_pcu.h> |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 21 | #include <arpa/inet.h> |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 22 | #include <pcu_l1_if.h> |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 23 | #include <gprs_rlcmac.h> |
Ivan Kluchnikov | 61a33f7 | 2012-04-12 15:22:06 +0400 | [diff] [blame] | 24 | #include <gsm_timer.h> |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 25 | #include <gprs_debug.h> |
Andreas Eversberg | 81e895b | 2012-07-06 08:24:53 +0200 | [diff] [blame] | 26 | #include <unistd.h> |
| 27 | #include <getopt.h> |
Andreas Eversberg | e266bd4 | 2012-07-13 14:00:21 +0200 | [diff] [blame] | 28 | #include <signal.h> |
Andreas Eversberg | 783aa4b | 2013-05-05 09:15:11 +0200 | [diff] [blame] | 29 | #include <sched.h> |
Andreas Eversberg | ebde64f | 2012-07-12 09:18:42 +0200 | [diff] [blame] | 30 | extern "C" { |
| 31 | #include "pcu_vty.h" |
| 32 | #include <osmocom/vty/telnet_interface.h> |
| 33 | #include <osmocom/vty/logging.h> |
| 34 | } |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 35 | |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 36 | struct gprs_rlcmac_bts *gprs_rlcmac_bts; |
Andreas Eversberg | 3e372d5 | 2012-07-06 09:28:15 +0200 | [diff] [blame] | 37 | extern struct gprs_nsvc *nsvc; |
Andreas Eversberg | 81e895b | 2012-07-06 08:24:53 +0200 | [diff] [blame] | 38 | uint16_t spoof_mcc = 0, spoof_mnc = 0; |
Andreas Eversberg | ebde64f | 2012-07-12 09:18:42 +0200 | [diff] [blame] | 39 | static int config_given = 0; |
| 40 | static const char *config_file = "osmo-pcu.cfg"; |
| 41 | extern struct vty_app_info pcu_vty_info; |
| 42 | void *tall_pcu_ctx; |
Andreas Eversberg | 7a5a67a | 2013-01-16 08:43:24 +0100 | [diff] [blame] | 43 | extern void *bv_tall_ctx; |
Andreas Eversberg | e266bd4 | 2012-07-13 14:00:21 +0200 | [diff] [blame] | 44 | static int quit = 0; |
Andreas Eversberg | 783aa4b | 2013-05-05 09:15:11 +0200 | [diff] [blame] | 45 | static int rt_prio = -1; |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 46 | |
Andreas Eversberg | b83e2a7 | 2012-10-07 15:26:00 +0200 | [diff] [blame] | 47 | #ifdef DEBUG_DIAGRAM |
| 48 | extern struct timeval diagram_time; |
| 49 | #endif |
| 50 | |
Andreas Eversberg | 81e895b | 2012-07-06 08:24:53 +0200 | [diff] [blame] | 51 | static void print_help() |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 52 | { |
Andreas Eversberg | 81e895b | 2012-07-06 08:24:53 +0200 | [diff] [blame] | 53 | printf( "Some useful options:\n" |
| 54 | " -h --help this text\n" |
Andreas Eversberg | ebde64f | 2012-07-12 09:18:42 +0200 | [diff] [blame] | 55 | " -c --config-file Specify the filename of the config " |
| 56 | "file\n" |
Andreas Eversberg | 81e895b | 2012-07-06 08:24:53 +0200 | [diff] [blame] | 57 | " -m --mcc MCC use given MCC instead of value " |
| 58 | "provided by BTS\n" |
| 59 | " -n --mnc MNC use given MNC instead of value " |
| 60 | "provided by BTS\n" |
Andreas Eversberg | 783aa4b | 2013-05-05 09:15:11 +0200 | [diff] [blame] | 61 | " -r --realtime PRIO Use SCHED_RR with the specified " |
| 62 | "priority\n" |
Holger Hans Peter Freyther | a30f476 | 2013-07-11 16:13:38 +0200 | [diff] [blame] | 63 | " -e --exit Exit the application on disconnect\n" |
Andreas Eversberg | 81e895b | 2012-07-06 08:24:53 +0200 | [diff] [blame] | 64 | ); |
| 65 | } |
| 66 | |
| 67 | /* FIXME: finally get some option parsing code into libosmocore */ |
| 68 | static void handle_options(int argc, char **argv) |
| 69 | { |
| 70 | while (1) { |
| 71 | int option_idx = 0, c; |
| 72 | static const struct option long_options[] = { |
| 73 | { "help", 0, 0, 'h' }, |
Andreas Eversberg | ebde64f | 2012-07-12 09:18:42 +0200 | [diff] [blame] | 74 | { "config-file", 1, 0, 'c' }, |
Andreas Eversberg | 1944bd5 | 2012-07-06 09:32:39 +0200 | [diff] [blame] | 75 | { "mcc", 1, 0, 'm' }, |
| 76 | { "mnc", 1, 0, 'n' }, |
Harald Welte | e5a0939 | 2013-01-17 12:28:16 +0100 | [diff] [blame] | 77 | { "version", 0, 0, 'V' }, |
Andreas Eversberg | 783aa4b | 2013-05-05 09:15:11 +0200 | [diff] [blame] | 78 | { "realtime", 1, 0, 'r' }, |
Holger Hans Peter Freyther | a30f476 | 2013-07-11 16:13:38 +0200 | [diff] [blame] | 79 | { "exit", 0, 0, 'e' }, |
Andreas Eversberg | 81e895b | 2012-07-06 08:24:53 +0200 | [diff] [blame] | 80 | { 0, 0, 0, 0 } |
| 81 | }; |
| 82 | |
Holger Hans Peter Freyther | a30f476 | 2013-07-11 16:13:38 +0200 | [diff] [blame] | 83 | c = getopt_long(argc, argv, "hc:m:n:Vr:e", |
Andreas Eversberg | 81e895b | 2012-07-06 08:24:53 +0200 | [diff] [blame] | 84 | long_options, &option_idx); |
| 85 | if (c == -1) |
| 86 | break; |
| 87 | |
| 88 | switch (c) { |
| 89 | case 'h': |
| 90 | print_help(); |
| 91 | exit(0); |
| 92 | break; |
Andreas Eversberg | ebde64f | 2012-07-12 09:18:42 +0200 | [diff] [blame] | 93 | case 'c': |
| 94 | config_file = strdup(optarg); |
| 95 | config_given = 1; |
| 96 | break; |
Andreas Eversberg | 81e895b | 2012-07-06 08:24:53 +0200 | [diff] [blame] | 97 | case 'm': |
| 98 | spoof_mcc = atoi(optarg); |
| 99 | break; |
| 100 | case 'n': |
| 101 | spoof_mnc = atoi(optarg); |
| 102 | break; |
Harald Welte | e5a0939 | 2013-01-17 12:28:16 +0100 | [diff] [blame] | 103 | case 'V': |
| 104 | print_version(1); |
| 105 | exit(0); |
| 106 | break; |
Andreas Eversberg | 783aa4b | 2013-05-05 09:15:11 +0200 | [diff] [blame] | 107 | case 'r': |
| 108 | rt_prio = atoi(optarg); |
| 109 | break; |
Holger Hans Peter Freyther | a30f476 | 2013-07-11 16:13:38 +0200 | [diff] [blame] | 110 | case 'e': |
| 111 | gprs_bssgp_exit_on_destroy(); |
| 112 | break; |
Andreas Eversberg | 81e895b | 2012-07-06 08:24:53 +0200 | [diff] [blame] | 113 | default: |
| 114 | fprintf(stderr, "Unknown option '%c'\n", c); |
| 115 | exit(0); |
| 116 | break; |
| 117 | } |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 118 | } |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 119 | } |
| 120 | |
Andreas Eversberg | e266bd4 | 2012-07-13 14:00:21 +0200 | [diff] [blame] | 121 | void sighandler(int sigset) |
| 122 | { |
| 123 | if (sigset == SIGHUP || sigset == SIGPIPE) |
| 124 | return; |
| 125 | |
| 126 | fprintf(stderr, "Signal %d received.\n", sigset); |
| 127 | |
| 128 | switch (sigset) { |
| 129 | case SIGINT: |
Holger Hans Peter Freyther | 72075f0 | 2013-03-19 18:33:06 +0400 | [diff] [blame] | 130 | case SIGTERM: |
Andreas Eversberg | e266bd4 | 2012-07-13 14:00:21 +0200 | [diff] [blame] | 131 | /* If another signal is received afterwards, the program |
| 132 | * is terminated without finishing shutdown process. |
| 133 | */ |
| 134 | signal(SIGINT, SIG_DFL); |
| 135 | signal(SIGHUP, SIG_DFL); |
| 136 | signal(SIGTERM, SIG_DFL); |
| 137 | signal(SIGPIPE, SIG_DFL); |
| 138 | signal(SIGABRT, SIG_DFL); |
| 139 | signal(SIGUSR1, SIG_DFL); |
| 140 | signal(SIGUSR2, SIG_DFL); |
| 141 | |
| 142 | quit = 1; |
| 143 | break; |
| 144 | case SIGABRT: |
| 145 | /* in case of abort, we want to obtain a talloc report |
| 146 | * and then return to the caller, who will abort the process |
| 147 | */ |
| 148 | case SIGUSR1: |
| 149 | case SIGUSR2: |
| 150 | talloc_report_full(tall_pcu_ctx, stderr); |
| 151 | break; |
| 152 | } |
| 153 | } |
| 154 | |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 155 | int main(int argc, char *argv[]) |
| 156 | { |
Andreas Eversberg | 783aa4b | 2013-05-05 09:15:11 +0200 | [diff] [blame] | 157 | struct sched_param param; |
Andreas Eversberg | dfa563c | 2012-07-06 08:13:59 +0200 | [diff] [blame] | 158 | struct gprs_rlcmac_bts *bts; |
Andreas Eversberg | 3e372d5 | 2012-07-06 09:28:15 +0200 | [diff] [blame] | 159 | int rc; |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 160 | |
Andreas Eversberg | ebde64f | 2012-07-12 09:18:42 +0200 | [diff] [blame] | 161 | tall_pcu_ctx = talloc_named_const(NULL, 1, "Osmo-PCU context"); |
| 162 | if (!tall_pcu_ctx) |
| 163 | return -ENOMEM; |
Andreas Eversberg | 7a5a67a | 2013-01-16 08:43:24 +0100 | [diff] [blame] | 164 | bv_tall_ctx = tall_pcu_ctx; |
Andreas Eversberg | ebde64f | 2012-07-12 09:18:42 +0200 | [diff] [blame] | 165 | |
| 166 | bts = gprs_rlcmac_bts = talloc_zero(tall_pcu_ctx, |
| 167 | struct gprs_rlcmac_bts); |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 168 | if (!gprs_rlcmac_bts) |
| 169 | return -ENOMEM; |
Andreas Eversberg | cd8a83a | 2012-09-23 06:41:21 +0200 | [diff] [blame] | 170 | bts->fc_interval = 1; |
Andreas Eversberg | 499ff41 | 2012-10-03 14:21:36 +0200 | [diff] [blame] | 171 | bts->initial_cs_dl = bts->initial_cs_ul = 1; |
Andreas Eversberg | dfa563c | 2012-07-06 08:13:59 +0200 | [diff] [blame] | 172 | bts->cs1 = 1; |
| 173 | bts->t3142 = 20; |
| 174 | bts->t3169 = 5; |
| 175 | bts->t3191 = 5; |
| 176 | bts->t3193_msec = 100; |
| 177 | bts->t3195 = 5; |
| 178 | bts->n3101 = 10; |
| 179 | bts->n3103 = 4; |
| 180 | bts->n3105 = 8; |
Andreas Eversberg | 0b874b6 | 2013-03-16 15:56:01 +0100 | [diff] [blame] | 181 | bts->alpha = 0; /* a = 0.0 */ |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 182 | |
Andreas Eversberg | e266bd4 | 2012-07-13 14:00:21 +0200 | [diff] [blame] | 183 | msgb_set_talloc_ctx(tall_pcu_ctx); |
| 184 | |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 185 | osmo_init_logging(&gprs_log_info); |
Ivan Kluchnikov | e3a0596 | 2012-03-18 15:48:51 +0400 | [diff] [blame] | 186 | |
Andreas Eversberg | ebde64f | 2012-07-12 09:18:42 +0200 | [diff] [blame] | 187 | vty_init(&pcu_vty_info); |
| 188 | pcu_vty_init(&gprs_log_info); |
| 189 | |
Andreas Eversberg | 81e895b | 2012-07-06 08:24:53 +0200 | [diff] [blame] | 190 | handle_options(argc, argv); |
| 191 | if ((!!spoof_mcc) + (!!spoof_mnc) == 1) { |
| 192 | fprintf(stderr, "--mcc and --mnc must be specified " |
| 193 | "together.\n"); |
| 194 | exit(0); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 195 | } |
Andreas Eversberg | ebde64f | 2012-07-12 09:18:42 +0200 | [diff] [blame] | 196 | |
| 197 | rc = vty_read_config_file(config_file, NULL); |
| 198 | if (rc < 0 && config_given) { |
| 199 | fprintf(stderr, "Failed to parse the config file: '%s'\n", |
| 200 | config_file); |
| 201 | exit(1); |
| 202 | } |
| 203 | if (rc < 0) |
| 204 | fprintf(stderr, "No config file: '%s' Using default config.\n", |
| 205 | config_file); |
| 206 | |
| 207 | rc = telnet_init(tall_pcu_ctx, NULL, 4240); |
| 208 | if (rc < 0) { |
| 209 | fprintf(stderr, "Error initializing telnet\n"); |
| 210 | exit(1); |
| 211 | } |
| 212 | |
Andreas Eversberg | f298fa8 | 2012-07-13 14:50:57 +0200 | [diff] [blame] | 213 | if (!bts->alloc_algorithm) |
Andreas Eversberg | 53f4725 | 2012-07-15 07:10:10 +0200 | [diff] [blame] | 214 | bts->alloc_algorithm = alloc_algorithm_b; |
Andreas Eversberg | f298fa8 | 2012-07-13 14:50:57 +0200 | [diff] [blame] | 215 | |
Andreas Eversberg | 3e372d5 | 2012-07-06 09:28:15 +0200 | [diff] [blame] | 216 | rc = pcu_l1if_open(); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 217 | |
Andreas Eversberg | 3e372d5 | 2012-07-06 09:28:15 +0200 | [diff] [blame] | 218 | if (rc < 0) |
| 219 | return rc; |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 220 | |
Andreas Eversberg | e266bd4 | 2012-07-13 14:00:21 +0200 | [diff] [blame] | 221 | signal(SIGINT, sighandler); |
| 222 | signal(SIGHUP, sighandler); |
| 223 | signal(SIGTERM, sighandler); |
| 224 | signal(SIGPIPE, sighandler); |
| 225 | signal(SIGABRT, sighandler); |
| 226 | signal(SIGUSR1, sighandler); |
| 227 | signal(SIGUSR2, sighandler); |
| 228 | |
Andreas Eversberg | 783aa4b | 2013-05-05 09:15:11 +0200 | [diff] [blame] | 229 | /* enable realtime priority for us */ |
| 230 | if (rt_prio != -1) { |
| 231 | memset(¶m, 0, sizeof(param)); |
| 232 | param.sched_priority = rt_prio; |
| 233 | rc = sched_setscheduler(getpid(), SCHED_RR, ¶m); |
| 234 | if (rc != 0) { |
| 235 | fprintf(stderr, "Setting SCHED_RR priority(%d) failed: %s\n", |
| 236 | param.sched_priority, strerror(errno)); |
| 237 | exit(1); |
| 238 | } |
| 239 | } |
| 240 | |
Andreas Eversberg | e266bd4 | 2012-07-13 14:00:21 +0200 | [diff] [blame] | 241 | while (!quit) { |
Ivan Kluchnikov | 61a33f7 | 2012-04-12 15:22:06 +0400 | [diff] [blame] | 242 | osmo_gsm_timers_check(); |
| 243 | osmo_gsm_timers_prepare(); |
| 244 | osmo_gsm_timers_update(); |
| 245 | |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 246 | osmo_select_main(0); |
Andreas Eversberg | b83e2a7 | 2012-10-07 15:26:00 +0200 | [diff] [blame] | 247 | #ifdef DEBUG_DIAGRAM |
| 248 | gettimeofday(&diagram_time, NULL); |
| 249 | #endif |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 250 | } |
Ivan Kluchnikov | e3a0596 | 2012-03-18 15:48:51 +0400 | [diff] [blame] | 251 | |
Andreas Eversberg | e266bd4 | 2012-07-13 14:00:21 +0200 | [diff] [blame] | 252 | telnet_exit(); |
| 253 | |
Andreas Eversberg | 3e372d5 | 2012-07-06 09:28:15 +0200 | [diff] [blame] | 254 | pcu_l1if_close(); |
Andreas Eversberg | e266bd4 | 2012-07-13 14:00:21 +0200 | [diff] [blame] | 255 | |
Andreas Eversberg | a004e6a | 2013-05-13 16:45:21 +0200 | [diff] [blame] | 256 | flush_timing_advance(); |
| 257 | |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 258 | talloc_free(gprs_rlcmac_bts); |
Andreas Eversberg | e266bd4 | 2012-07-13 14:00:21 +0200 | [diff] [blame] | 259 | |
| 260 | talloc_report_full(tall_pcu_ctx, stderr); |
Andreas Eversberg | ebde64f | 2012-07-12 09:18:42 +0200 | [diff] [blame] | 261 | talloc_free(tall_pcu_ctx); |
Andreas Eversberg | 3e372d5 | 2012-07-06 09:28:15 +0200 | [diff] [blame] | 262 | |
| 263 | return 0; |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 264 | } |