Philipp Maier | 39f62bb | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 1 | /* (C) 2017 by sysmocom s.f.m.c. GmbH |
| 2 | * All Rights Reserved |
| 3 | * |
| 4 | * Author: Philipp Maier |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU Affero General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU Affero General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Affero General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <osmocom/core/logging.h> |
| 22 | #include <osmocom/core/utils.h> |
| 23 | #include <osmocom/core/timer.h> |
| 24 | #include <osmocom/core/fsm.h> |
| 25 | #include <unistd.h> |
| 26 | #include <errno.h> |
| 27 | #include <string.h> |
Neels Hofmeyr | c016479 | 2017-09-04 15:15:32 +0200 | [diff] [blame] | 28 | #include <osmocom/bsc/debug.h> |
| 29 | #include <osmocom/bsc/bsc_msc_data.h> |
| 30 | #include <osmocom/bsc/osmo_bsc_sigtran.h> |
Philipp Maier | 39f62bb | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 31 | |
| 32 | #define RESET_RESEND_INTERVAL 2 /* sec */ |
| 33 | #define RESET_RESEND_TIMER_NO 1234 /* FIXME: dig out the real timer number */ |
| 34 | #define BAD_CONNECTION_THRESOLD 3 /* connection failures */ |
| 35 | |
| 36 | enum fsm_states { |
| 37 | ST_DISC, /* Disconnected from remote end */ |
| 38 | ST_CONN, /* We have a confirmed connection */ |
| 39 | }; |
| 40 | |
Philipp Maier | 39f62bb | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 41 | enum fsm_evt { |
| 42 | EV_RESET_ACK, /* got reset acknowlegement from remote end */ |
| 43 | EV_N_DISCONNECT, /* lost a connection */ |
| 44 | EV_N_CONNECT, /* made a successful connection */ |
| 45 | }; |
| 46 | |
Philipp Maier | 3d5818a | 2018-02-26 21:47:06 +0100 | [diff] [blame^] | 47 | static const struct value_string fsm_event_names[] = { |
| 48 | OSMO_VALUE_STRING(EV_RESET_ACK), |
| 49 | OSMO_VALUE_STRING(EV_N_DISCONNECT), |
| 50 | OSMO_VALUE_STRING(EV_N_CONNECT), |
| 51 | {0, NULL} |
| 52 | }; |
| 53 | |
Philipp Maier | 39f62bb | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 54 | /* Disconnected state */ |
| 55 | static void fsm_disc_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 56 | { |
| 57 | struct a_reset_ctx *reset = (struct a_reset_ctx *)data; |
| 58 | OSMO_ASSERT(reset); |
Philipp Maier | 7e0c269 | 2017-11-07 12:47:59 +0100 | [diff] [blame] | 59 | OSMO_ASSERT(reset->fsm); |
Philipp Maier | e4cfa74 | 2017-11-24 11:41:36 +0100 | [diff] [blame] | 60 | LOGPFSML(reset->fsm, LOGL_NOTICE, "SIGTRAN connection succeded.\n"); |
Philipp Maier | 39f62bb | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 61 | |
| 62 | reset->conn_loss_counter = 0; |
| 63 | osmo_fsm_inst_state_chg(fi, ST_CONN, 0, 0); |
| 64 | } |
| 65 | |
| 66 | /* Connected state */ |
| 67 | static void fsm_conn_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 68 | { |
| 69 | struct a_reset_ctx *reset = (struct a_reset_ctx *)data; |
| 70 | OSMO_ASSERT(reset); |
| 71 | |
Philipp Maier | 39f62bb | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 72 | switch (event) { |
| 73 | case EV_N_DISCONNECT: |
| 74 | if (reset->conn_loss_counter >= BAD_CONNECTION_THRESOLD) { |
Philipp Maier | 7e0c269 | 2017-11-07 12:47:59 +0100 | [diff] [blame] | 75 | LOGPFSML(reset->fsm, LOGL_NOTICE, "SIGTRAN connection down, reconnecting...\n"); |
Philipp Maier | 39f62bb | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 76 | osmo_fsm_inst_state_chg(fi, ST_DISC, RESET_RESEND_INTERVAL, RESET_RESEND_TIMER_NO); |
| 77 | } else |
| 78 | reset->conn_loss_counter++; |
| 79 | break; |
| 80 | case EV_N_CONNECT: |
| 81 | reset->conn_loss_counter = 0; |
| 82 | break; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /* Timer callback to retransmit the reset signal */ |
| 87 | static int fsm_reset_ack_timeout_cb(struct osmo_fsm_inst *fi) |
| 88 | { |
| 89 | struct a_reset_ctx *reset = (struct a_reset_ctx *)fi->priv; |
Philipp Maier | 7e0c269 | 2017-11-07 12:47:59 +0100 | [diff] [blame] | 90 | OSMO_ASSERT(reset->fsm); |
Philipp Maier | 39f62bb | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 91 | |
Philipp Maier | e4cfa74 | 2017-11-24 11:41:36 +0100 | [diff] [blame] | 92 | LOGPFSML(reset->fsm, LOGL_NOTICE, "(re)sending BSSMAP RESET message...\n"); |
Philipp Maier | 39f62bb | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 93 | |
| 94 | reset->cb(reset->priv); |
| 95 | |
| 96 | osmo_fsm_inst_state_chg(fi, ST_DISC, RESET_RESEND_INTERVAL, RESET_RESEND_TIMER_NO); |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | static struct osmo_fsm_state fsm_states[] = { |
| 101 | [ST_DISC] = { |
| 102 | .in_event_mask = (1 << EV_RESET_ACK), |
| 103 | .out_state_mask = (1 << ST_DISC) | (1 << ST_CONN), |
| 104 | .name = "DISC", |
| 105 | .action = fsm_disc_cb, |
| 106 | }, |
| 107 | [ST_CONN] = { |
| 108 | .in_event_mask = (1 << EV_N_DISCONNECT) | (1 << EV_N_CONNECT), |
| 109 | .out_state_mask = (1 << ST_DISC) | (1 << ST_CONN), |
| 110 | .name = "CONN", |
| 111 | .action = fsm_conn_cb, |
| 112 | }, |
| 113 | }; |
| 114 | |
| 115 | /* State machine definition */ |
| 116 | static struct osmo_fsm fsm = { |
Harald Welte | 5de7285 | 2017-10-24 18:18:53 +0200 | [diff] [blame] | 117 | .name = "A-RESET", |
Philipp Maier | 39f62bb | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 118 | .states = fsm_states, |
| 119 | .num_states = ARRAY_SIZE(fsm_states), |
| 120 | .log_subsys = DMSC, |
| 121 | .timer_cb = fsm_reset_ack_timeout_cb, |
Philipp Maier | 3d5818a | 2018-02-26 21:47:06 +0100 | [diff] [blame^] | 122 | .event_names = fsm_event_names, |
Philipp Maier | 39f62bb | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | /* Create and start state machine which handles the reset/reset-ack procedure */ |
| 126 | struct a_reset_ctx *a_reset_alloc(const void *ctx, const char *name, void *cb, void *priv) |
| 127 | { |
| 128 | OSMO_ASSERT(name); |
| 129 | |
| 130 | struct a_reset_ctx *reset; |
| 131 | |
| 132 | /* Register the fsm description (if not already done) */ |
| 133 | if (osmo_fsm_find_by_name(fsm.name) != &fsm) |
| 134 | osmo_fsm_register(&fsm); |
| 135 | |
| 136 | /* Allocate and configure a new fsm instance */ |
| 137 | reset = talloc_zero(ctx, struct a_reset_ctx); |
| 138 | OSMO_ASSERT(reset); |
| 139 | reset->priv = priv; |
| 140 | reset->cb = cb; |
Philipp Maier | 39f62bb | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 141 | reset->conn_loss_counter = 0; |
Philipp Maier | 7e0c269 | 2017-11-07 12:47:59 +0100 | [diff] [blame] | 142 | reset->fsm = osmo_fsm_inst_alloc(&fsm, NULL, NULL, LOGL_DEBUG, name); |
Philipp Maier | 39f62bb | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 143 | OSMO_ASSERT(reset->fsm); |
| 144 | reset->fsm->priv = reset; |
Philipp Maier | 39f62bb | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 145 | |
| 146 | /* kick off reset-ack sending mechanism */ |
| 147 | osmo_fsm_inst_state_chg(reset->fsm, ST_DISC, RESET_RESEND_INTERVAL, RESET_RESEND_TIMER_NO); |
| 148 | |
| 149 | return reset; |
| 150 | } |
| 151 | |
| 152 | /* Tear down state machine */ |
| 153 | void a_reset_free(struct a_reset_ctx *reset) |
| 154 | { |
| 155 | OSMO_ASSERT(reset); |
| 156 | OSMO_ASSERT(reset->fsm); |
| 157 | |
| 158 | osmo_fsm_inst_free(reset->fsm); |
| 159 | reset->fsm = NULL; |
| 160 | |
| 161 | memset(reset, 0, sizeof(*reset)); |
| 162 | talloc_free(reset); |
Philipp Maier | 39f62bb | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /* Confirm that we sucessfully received a reset acknowlege message */ |
| 166 | void a_reset_ack_confirm(struct a_reset_ctx *reset) |
| 167 | { |
| 168 | OSMO_ASSERT(reset); |
| 169 | OSMO_ASSERT(reset->fsm); |
| 170 | |
| 171 | osmo_fsm_inst_dispatch(reset->fsm, EV_RESET_ACK, reset); |
| 172 | } |
| 173 | |
| 174 | /* Report a failed connection */ |
| 175 | void a_reset_conn_fail(struct a_reset_ctx *reset) |
| 176 | { |
| 177 | /* If no reset context is supplied, just drop the info */ |
| 178 | if (!reset) |
| 179 | return; |
| 180 | |
| 181 | OSMO_ASSERT(reset->fsm); |
| 182 | |
| 183 | osmo_fsm_inst_dispatch(reset->fsm, EV_N_DISCONNECT, reset); |
| 184 | } |
| 185 | |
| 186 | /* Report a successful connection */ |
| 187 | void a_reset_conn_success(struct a_reset_ctx *reset) |
| 188 | { |
| 189 | /* If no reset context is supplied, just drop the info */ |
| 190 | if (!reset) |
| 191 | return; |
| 192 | |
| 193 | OSMO_ASSERT(reset->fsm); |
| 194 | |
| 195 | osmo_fsm_inst_dispatch(reset->fsm, EV_N_CONNECT, reset); |
| 196 | } |
| 197 | |
| 198 | /* Check if we have a connection to a specified msc */ |
| 199 | bool a_reset_conn_ready(struct a_reset_ctx *reset) |
| 200 | { |
| 201 | /* If no reset context is supplied, we assume that |
| 202 | * the connection can't be ready! */ |
| 203 | if (!reset) |
| 204 | return false; |
| 205 | |
| 206 | OSMO_ASSERT(reset->fsm); |
| 207 | if (reset->fsm->state == ST_CONN) |
| 208 | return true; |
| 209 | |
| 210 | return false; |
| 211 | } |