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 | |
| 41 | static const struct value_string fsm_state_names[] = { |
| 42 | {ST_DISC, "ST_DISC (disconnected)"}, |
| 43 | {ST_CONN, "ST_CONN (connected)"}, |
| 44 | {0, NULL}, |
| 45 | }; |
| 46 | |
| 47 | enum fsm_evt { |
| 48 | EV_RESET_ACK, /* got reset acknowlegement from remote end */ |
| 49 | EV_N_DISCONNECT, /* lost a connection */ |
| 50 | EV_N_CONNECT, /* made a successful connection */ |
| 51 | }; |
| 52 | |
| 53 | static const struct value_string fsm_evt_names[] = { |
| 54 | {EV_RESET_ACK, "EV_RESET_ACK"}, |
| 55 | {EV_N_DISCONNECT, "EV_N_DISCONNECT"}, |
| 56 | {EV_N_CONNECT, "EV_N_CONNECT"}, |
| 57 | {0, NULL}, |
| 58 | }; |
| 59 | |
| 60 | /* Disconnected state */ |
| 61 | static void fsm_disc_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 62 | { |
| 63 | struct a_reset_ctx *reset = (struct a_reset_ctx *)data; |
| 64 | OSMO_ASSERT(reset); |
Philipp Maier | 7e0c269 | 2017-11-07 12:47:59 +0100 | [diff] [blame^] | 65 | OSMO_ASSERT(reset->fsm); |
Philipp Maier | 39f62bb | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 66 | |
Philipp Maier | 7e0c269 | 2017-11-07 12:47:59 +0100 | [diff] [blame^] | 67 | LOGPFSML(reset->fsm, LOGL_NOTICE, "fsm-state (msc-reset): %s, fsm-event: %s\n", |
Philipp Maier | 39f62bb | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 68 | get_value_string(fsm_state_names, ST_CONN), get_value_string(fsm_evt_names, event)); |
| 69 | |
| 70 | reset->conn_loss_counter = 0; |
| 71 | osmo_fsm_inst_state_chg(fi, ST_CONN, 0, 0); |
| 72 | } |
| 73 | |
| 74 | /* Connected state */ |
| 75 | static void fsm_conn_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 76 | { |
| 77 | struct a_reset_ctx *reset = (struct a_reset_ctx *)data; |
| 78 | OSMO_ASSERT(reset); |
| 79 | |
Philipp Maier | 7e0c269 | 2017-11-07 12:47:59 +0100 | [diff] [blame^] | 80 | LOGPFSML(reset->fsm, LOGL_NOTICE, "fsm-state (msc-reset): %s, fsm-event: %s\n", |
Philipp Maier | 39f62bb | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 81 | get_value_string(fsm_state_names, ST_CONN), get_value_string(fsm_evt_names, event)); |
| 82 | |
| 83 | switch (event) { |
| 84 | case EV_N_DISCONNECT: |
| 85 | if (reset->conn_loss_counter >= BAD_CONNECTION_THRESOLD) { |
Philipp Maier | 7e0c269 | 2017-11-07 12:47:59 +0100 | [diff] [blame^] | 86 | LOGPFSML(reset->fsm, LOGL_NOTICE, "SIGTRAN connection down, reconnecting...\n"); |
Philipp Maier | 39f62bb | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 87 | osmo_fsm_inst_state_chg(fi, ST_DISC, RESET_RESEND_INTERVAL, RESET_RESEND_TIMER_NO); |
| 88 | } else |
| 89 | reset->conn_loss_counter++; |
| 90 | break; |
| 91 | case EV_N_CONNECT: |
| 92 | reset->conn_loss_counter = 0; |
| 93 | break; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /* Timer callback to retransmit the reset signal */ |
| 98 | static int fsm_reset_ack_timeout_cb(struct osmo_fsm_inst *fi) |
| 99 | { |
| 100 | struct a_reset_ctx *reset = (struct a_reset_ctx *)fi->priv; |
Philipp Maier | 7e0c269 | 2017-11-07 12:47:59 +0100 | [diff] [blame^] | 101 | OSMO_ASSERT(reset->fsm); |
Philipp Maier | 39f62bb | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 102 | |
Philipp Maier | 7e0c269 | 2017-11-07 12:47:59 +0100 | [diff] [blame^] | 103 | LOGPFSML(reset->fsm, LOGL_NOTICE, "reset-ack timeout (T%i) in state %s, resending...\n", fi->T, |
Philipp Maier | 39f62bb | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 104 | get_value_string(fsm_state_names, fi->state)); |
| 105 | |
| 106 | reset->cb(reset->priv); |
| 107 | |
| 108 | osmo_fsm_inst_state_chg(fi, ST_DISC, RESET_RESEND_INTERVAL, RESET_RESEND_TIMER_NO); |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static struct osmo_fsm_state fsm_states[] = { |
| 113 | [ST_DISC] = { |
| 114 | .in_event_mask = (1 << EV_RESET_ACK), |
| 115 | .out_state_mask = (1 << ST_DISC) | (1 << ST_CONN), |
| 116 | .name = "DISC", |
| 117 | .action = fsm_disc_cb, |
| 118 | }, |
| 119 | [ST_CONN] = { |
| 120 | .in_event_mask = (1 << EV_N_DISCONNECT) | (1 << EV_N_CONNECT), |
| 121 | .out_state_mask = (1 << ST_DISC) | (1 << ST_CONN), |
| 122 | .name = "CONN", |
| 123 | .action = fsm_conn_cb, |
| 124 | }, |
| 125 | }; |
| 126 | |
| 127 | /* State machine definition */ |
| 128 | static struct osmo_fsm fsm = { |
Harald Welte | 5de7285 | 2017-10-24 18:18:53 +0200 | [diff] [blame] | 129 | .name = "A-RESET", |
Philipp Maier | 39f62bb | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 130 | .states = fsm_states, |
| 131 | .num_states = ARRAY_SIZE(fsm_states), |
| 132 | .log_subsys = DMSC, |
| 133 | .timer_cb = fsm_reset_ack_timeout_cb, |
| 134 | }; |
| 135 | |
| 136 | /* Create and start state machine which handles the reset/reset-ack procedure */ |
| 137 | struct a_reset_ctx *a_reset_alloc(const void *ctx, const char *name, void *cb, void *priv) |
| 138 | { |
| 139 | OSMO_ASSERT(name); |
| 140 | |
| 141 | struct a_reset_ctx *reset; |
| 142 | |
| 143 | /* Register the fsm description (if not already done) */ |
| 144 | if (osmo_fsm_find_by_name(fsm.name) != &fsm) |
| 145 | osmo_fsm_register(&fsm); |
| 146 | |
| 147 | /* Allocate and configure a new fsm instance */ |
| 148 | reset = talloc_zero(ctx, struct a_reset_ctx); |
| 149 | OSMO_ASSERT(reset); |
| 150 | reset->priv = priv; |
| 151 | reset->cb = cb; |
Philipp Maier | 39f62bb | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 152 | reset->conn_loss_counter = 0; |
Philipp Maier | 7e0c269 | 2017-11-07 12:47:59 +0100 | [diff] [blame^] | 153 | reset->fsm = osmo_fsm_inst_alloc(&fsm, NULL, NULL, LOGL_DEBUG, name); |
Philipp Maier | 39f62bb | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 154 | OSMO_ASSERT(reset->fsm); |
| 155 | reset->fsm->priv = reset; |
Philipp Maier | 7e0c269 | 2017-11-07 12:47:59 +0100 | [diff] [blame^] | 156 | LOGPFSML(reset->fsm, LOGL_NOTICE, "reset handler fsm created.\n"); |
Philipp Maier | 39f62bb | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 157 | |
| 158 | /* kick off reset-ack sending mechanism */ |
| 159 | osmo_fsm_inst_state_chg(reset->fsm, ST_DISC, RESET_RESEND_INTERVAL, RESET_RESEND_TIMER_NO); |
| 160 | |
| 161 | return reset; |
| 162 | } |
| 163 | |
| 164 | /* Tear down state machine */ |
| 165 | void a_reset_free(struct a_reset_ctx *reset) |
| 166 | { |
| 167 | OSMO_ASSERT(reset); |
| 168 | OSMO_ASSERT(reset->fsm); |
| 169 | |
| 170 | osmo_fsm_inst_free(reset->fsm); |
| 171 | reset->fsm = NULL; |
| 172 | |
| 173 | memset(reset, 0, sizeof(*reset)); |
| 174 | talloc_free(reset); |
| 175 | |
Philipp Maier | 7e0c269 | 2017-11-07 12:47:59 +0100 | [diff] [blame^] | 176 | LOGPFSML(reset->fsm, LOGL_NOTICE, "reset handler fsm destroyed.\n"); |
Philipp Maier | 39f62bb | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /* Confirm that we sucessfully received a reset acknowlege message */ |
| 180 | void a_reset_ack_confirm(struct a_reset_ctx *reset) |
| 181 | { |
| 182 | OSMO_ASSERT(reset); |
| 183 | OSMO_ASSERT(reset->fsm); |
| 184 | |
| 185 | osmo_fsm_inst_dispatch(reset->fsm, EV_RESET_ACK, reset); |
| 186 | } |
| 187 | |
| 188 | /* Report a failed connection */ |
| 189 | void a_reset_conn_fail(struct a_reset_ctx *reset) |
| 190 | { |
| 191 | /* If no reset context is supplied, just drop the info */ |
| 192 | if (!reset) |
| 193 | return; |
| 194 | |
| 195 | OSMO_ASSERT(reset->fsm); |
| 196 | |
| 197 | osmo_fsm_inst_dispatch(reset->fsm, EV_N_DISCONNECT, reset); |
| 198 | } |
| 199 | |
| 200 | /* Report a successful connection */ |
| 201 | void a_reset_conn_success(struct a_reset_ctx *reset) |
| 202 | { |
| 203 | /* If no reset context is supplied, just drop the info */ |
| 204 | if (!reset) |
| 205 | return; |
| 206 | |
| 207 | OSMO_ASSERT(reset->fsm); |
| 208 | |
| 209 | osmo_fsm_inst_dispatch(reset->fsm, EV_N_CONNECT, reset); |
| 210 | } |
| 211 | |
| 212 | /* Check if we have a connection to a specified msc */ |
| 213 | bool a_reset_conn_ready(struct a_reset_ctx *reset) |
| 214 | { |
| 215 | /* If no reset context is supplied, we assume that |
| 216 | * the connection can't be ready! */ |
| 217 | if (!reset) |
| 218 | return false; |
| 219 | |
| 220 | OSMO_ASSERT(reset->fsm); |
| 221 | if (reset->fsm->state == ST_CONN) |
| 222 | return true; |
| 223 | |
| 224 | return false; |
| 225 | } |