Philipp Maier | fbf6610 | 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 | 9084396 | 2017-09-04 15:04:35 +0200 | [diff] [blame] | 28 | #include <osmocom/msc/debug.h> |
Harald Welte | 4bd35d4 | 2018-01-25 00:26:59 +0100 | [diff] [blame] | 29 | #include <osmocom/msc/a_reset.h> |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 30 | |
| 31 | #define RESET_RESEND_INTERVAL 2 /* sec */ |
| 32 | #define RESET_RESEND_TIMER_NO 1234 /* FIXME: dig out the real timer number */ |
| 33 | #define BAD_CONNECTION_THRESOLD 3 /* connection failures */ |
| 34 | |
| 35 | enum fsm_states { |
| 36 | ST_DISC, /* Disconnected from remote end */ |
| 37 | ST_CONN, /* We have a confirmed connection */ |
| 38 | }; |
| 39 | |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 40 | enum fsm_evt { |
| 41 | EV_RESET_ACK, /* got reset acknowlegement from remote end */ |
| 42 | EV_N_DISCONNECT, /* lost a connection */ |
| 43 | EV_N_CONNECT, /* made a successful connection */ |
| 44 | }; |
| 45 | |
Philipp Maier | 5861678 | 2018-02-26 12:40:27 +0100 | [diff] [blame] | 46 | static const struct value_string fsm_event_names[] = { |
| 47 | OSMO_VALUE_STRING(EV_RESET_ACK), |
| 48 | OSMO_VALUE_STRING(EV_N_DISCONNECT), |
| 49 | OSMO_VALUE_STRING(EV_N_CONNECT), |
| 50 | {0, NULL} |
| 51 | }; |
| 52 | |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 53 | /* Disconnected state */ |
| 54 | static void fsm_disc_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 55 | { |
| 56 | struct a_reset_ctx *reset = (struct a_reset_ctx *)data; |
| 57 | OSMO_ASSERT(reset); |
Philipp Maier | 8ae3c92 | 2017-11-09 11:17:24 +0100 | [diff] [blame] | 58 | OSMO_ASSERT(reset->fsm); |
Philipp Maier | b8acdcd | 2017-11-24 11:39:10 +0100 | [diff] [blame] | 59 | LOGPFSML(reset->fsm, LOGL_NOTICE, "SIGTRAN connection succeded.\n"); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 60 | |
| 61 | reset->conn_loss_counter = 0; |
| 62 | osmo_fsm_inst_state_chg(fi, ST_CONN, 0, 0); |
| 63 | } |
| 64 | |
| 65 | /* Connected state */ |
| 66 | static void fsm_conn_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 67 | { |
| 68 | struct a_reset_ctx *reset = (struct a_reset_ctx *)data; |
| 69 | OSMO_ASSERT(reset); |
| 70 | |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 71 | switch (event) { |
| 72 | case EV_N_DISCONNECT: |
| 73 | if (reset->conn_loss_counter >= BAD_CONNECTION_THRESOLD) { |
Philipp Maier | 8ae3c92 | 2017-11-09 11:17:24 +0100 | [diff] [blame] | 74 | LOGPFSML(reset->fsm, LOGL_NOTICE, "SIGTRAN connection down, reconnecting...\n"); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 75 | osmo_fsm_inst_state_chg(fi, ST_DISC, RESET_RESEND_INTERVAL, RESET_RESEND_TIMER_NO); |
| 76 | } else |
| 77 | reset->conn_loss_counter++; |
| 78 | break; |
| 79 | case EV_N_CONNECT: |
| 80 | reset->conn_loss_counter = 0; |
| 81 | break; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /* Timer callback to retransmit the reset signal */ |
| 86 | static int fsm_reset_ack_timeout_cb(struct osmo_fsm_inst *fi) |
| 87 | { |
| 88 | struct a_reset_ctx *reset = (struct a_reset_ctx *)fi->priv; |
Philipp Maier | 8ae3c92 | 2017-11-09 11:17:24 +0100 | [diff] [blame] | 89 | OSMO_ASSERT(reset->fsm); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 90 | |
Philipp Maier | b8acdcd | 2017-11-24 11:39:10 +0100 | [diff] [blame] | 91 | LOGPFSML(reset->fsm, LOGL_NOTICE, "(re)sending BSSMAP RESET message...\n"); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 92 | |
| 93 | reset->cb(reset->priv); |
| 94 | |
| 95 | osmo_fsm_inst_state_chg(fi, ST_DISC, RESET_RESEND_INTERVAL, RESET_RESEND_TIMER_NO); |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | static struct osmo_fsm_state fsm_states[] = { |
| 100 | [ST_DISC] = { |
| 101 | .in_event_mask = (1 << EV_RESET_ACK), |
| 102 | .out_state_mask = (1 << ST_DISC) | (1 << ST_CONN), |
| 103 | .name = "DISC", |
| 104 | .action = fsm_disc_cb, |
| 105 | }, |
| 106 | [ST_CONN] = { |
| 107 | .in_event_mask = (1 << EV_N_DISCONNECT) | (1 << EV_N_CONNECT), |
| 108 | .out_state_mask = (1 << ST_DISC) | (1 << ST_CONN), |
| 109 | .name = "CONN", |
| 110 | .action = fsm_conn_cb, |
| 111 | }, |
| 112 | }; |
| 113 | |
| 114 | /* State machine definition */ |
| 115 | static struct osmo_fsm fsm = { |
Harald Welte | 6556d3cb | 2017-10-25 03:28:03 +0200 | [diff] [blame] | 116 | .name = "A-RESET", |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 117 | .states = fsm_states, |
| 118 | .num_states = ARRAY_SIZE(fsm_states), |
| 119 | .log_subsys = DMSC, |
| 120 | .timer_cb = fsm_reset_ack_timeout_cb, |
Philipp Maier | 5861678 | 2018-02-26 12:40:27 +0100 | [diff] [blame] | 121 | .event_names = fsm_event_names, |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | /* Create and start state machine which handles the reset/reset-ack procedure */ |
Harald Welte | b6777fb | 2018-02-08 23:59:19 +0100 | [diff] [blame] | 125 | struct a_reset_ctx *a_reset_alloc(const void *ctx, const char *name, void *cb, void *priv, |
| 126 | bool already_connected) |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 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 | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 141 | reset->conn_loss_counter = 0; |
Philipp Maier | 8ae3c92 | 2017-11-09 11:17:24 +0100 | [diff] [blame] | 142 | reset->fsm = osmo_fsm_inst_alloc(&fsm, NULL, NULL, LOGL_DEBUG, name); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 143 | OSMO_ASSERT(reset->fsm); |
| 144 | reset->fsm->priv = reset; |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 145 | |
Harald Welte | b6777fb | 2018-02-08 23:59:19 +0100 | [diff] [blame] | 146 | if (already_connected) |
| 147 | osmo_fsm_inst_state_chg(reset->fsm, ST_CONN, 0, 0); |
| 148 | else { |
| 149 | /* kick off reset-ack sending mechanism */ |
| 150 | osmo_fsm_inst_state_chg(reset->fsm, ST_DISC, RESET_RESEND_INTERVAL, |
| 151 | RESET_RESEND_TIMER_NO); |
| 152 | } |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 153 | |
| 154 | return reset; |
| 155 | } |
| 156 | |
| 157 | /* Tear down state machine */ |
| 158 | void a_reset_free(struct a_reset_ctx *reset) |
| 159 | { |
| 160 | OSMO_ASSERT(reset); |
| 161 | OSMO_ASSERT(reset->fsm); |
| 162 | |
| 163 | osmo_fsm_inst_free(reset->fsm); |
| 164 | reset->fsm = NULL; |
| 165 | |
| 166 | memset(reset, 0, sizeof(*reset)); |
| 167 | talloc_free(reset); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /* Confirm that we sucessfully received a reset acknowlege message */ |
| 171 | void a_reset_ack_confirm(struct a_reset_ctx *reset) |
| 172 | { |
| 173 | OSMO_ASSERT(reset); |
| 174 | OSMO_ASSERT(reset->fsm); |
| 175 | |
| 176 | osmo_fsm_inst_dispatch(reset->fsm, EV_RESET_ACK, reset); |
| 177 | } |
| 178 | |
| 179 | /* Report a failed connection */ |
| 180 | void a_reset_conn_fail(struct a_reset_ctx *reset) |
| 181 | { |
| 182 | /* If no reset context is supplied, just drop the info */ |
| 183 | if (!reset) |
| 184 | return; |
| 185 | |
| 186 | OSMO_ASSERT(reset->fsm); |
| 187 | |
| 188 | osmo_fsm_inst_dispatch(reset->fsm, EV_N_DISCONNECT, reset); |
| 189 | } |
| 190 | |
| 191 | /* Report a successful connection */ |
| 192 | void a_reset_conn_success(struct a_reset_ctx *reset) |
| 193 | { |
| 194 | /* If no reset context is supplied, just drop the info */ |
| 195 | if (!reset) |
| 196 | return; |
| 197 | |
| 198 | OSMO_ASSERT(reset->fsm); |
| 199 | |
| 200 | osmo_fsm_inst_dispatch(reset->fsm, EV_N_CONNECT, reset); |
| 201 | } |
| 202 | |
| 203 | /* Check if we have a connection to a specified msc */ |
| 204 | bool a_reset_conn_ready(struct a_reset_ctx *reset) |
| 205 | { |
| 206 | /* If no reset context is supplied, we assume that |
| 207 | * the connection can't be ready! */ |
| 208 | if (!reset) |
| 209 | return false; |
| 210 | |
| 211 | OSMO_ASSERT(reset->fsm); |
| 212 | if (reset->fsm->state == ST_CONN) |
| 213 | return true; |
| 214 | |
| 215 | return false; |
| 216 | } |